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