source: trunk/libs/newlib/src/newlib/libm/common/sf_exp2.c @ 567

Last change on this file since 567 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 3.1 KB
Line 
1/* Single-precision 2^x function.
2   Copyright (c) 2017 ARM Ltd.  All rights reserved.
3
4   Redistribution and use in source and binary forms, with or without
5   modification, are permitted provided that the following conditions
6   are met:
7   1. Redistributions of source code must retain the above copyright
8      notice, this list of conditions and the following disclaimer.
9   2. Redistributions in binary form must reproduce the above copyright
10      notice, this list of conditions and the following disclaimer in the
11      documentation and/or other materials provided with the distribution.
12   3. The name of the company may not be used to endorse or promote
13      products derived from this software without specific prior written
14      permission.
15
16   THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS AND ANY EXPRESS OR IMPLIED
17   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19   IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
26
27#include "fdlibm.h"
28#if !__OBSOLETE_MATH
29
30#include <math.h>
31#include <stdint.h>
32#include "math_config.h"
33
34/*
35EXP2F_TABLE_BITS = 5
36EXP2F_POLY_ORDER = 3
37
38ULP error: 0.502 (nearest rounding.)
39Relative error: 1.69 * 2^-34 in [-1/64, 1/64] (before rounding.)
40Wrong count: 168353 (all nearest rounding wrong results with fma.)
41Non-nearest ULP error: 1 (rounded ULP error)
42*/
43
44#define N (1 << EXP2F_TABLE_BITS)
45#define T __exp2f_data.tab
46#define C __exp2f_data.poly
47#define SHIFT __exp2f_data.shift_scaled
48
49static inline uint32_t
50top12 (float x)
51{
52  return asuint (x) >> 20;
53}
54
55float
56exp2f (float x)
57{
58  uint32_t abstop;
59  uint64_t ki, t;
60  /* double_t for better performance on targets with FLT_EVAL_METHOD==2.  */
61  double_t kd, xd, z, r, r2, y, s;
62
63  xd = (double_t) x;
64  abstop = top12 (x) & 0x7ff;
65  if (__builtin_expect (abstop >= top12 (128.0f), 0))
66    {
67      /* |x| >= 128 or x is nan.  */
68      if (asuint (x) == asuint (-INFINITY))
69        return 0.0f;
70      if (abstop >= top12 (INFINITY))
71        return x + x;
72      if (x > 0.0f)
73        return __math_oflowf (0);
74      if (x <= -150.0f)
75        return __math_uflowf (0);
76#if WANT_ERRNO_UFLOW
77      if (x < -149.0f)
78        return __math_may_uflowf (0);
79#endif
80    }
81
82  /* x = k/N + r with r in [-1/(2N), 1/(2N)] and int k.  */
83  kd = (double) (xd + SHIFT); /* Rounding to double precision is required.  */
84  ki = asuint64 (kd);
85  kd -= SHIFT; /* k/N for int k.  */
86  r = xd - kd;
87
88  /* exp2(x) = 2^(k/N) * 2^r ~= s * (C0*r^3 + C1*r^2 + C2*r + 1) */
89  t = T[ki % N];
90  t += ki << (52 - EXP2F_TABLE_BITS);
91  s = asdouble (t);
92  z = C[0] * r + C[1];
93  r2 = r * r;
94  y = C[2] * r + 1;
95  y = z * r2 + y;
96  y = y * s;
97  return (float) y;
98}
99#endif /* !__OBSOLETE_MATH */
Note: See TracBrowser for help on using the repository browser.