1 | /* Single-precision pow 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 | /* |
---|
35 | POWF_LOG2_POLY_ORDER = 5 |
---|
36 | EXP2F_TABLE_BITS = 5 |
---|
37 | |
---|
38 | ULP error: 0.82 (~ 0.5 + relerr*2^24) |
---|
39 | relerr: 1.27 * 2^-26 (Relative error ~= 128*Ln2*relerr_log2 + relerr_exp2) |
---|
40 | relerr_log2: 1.83 * 2^-33 (Relative error of logx.) |
---|
41 | relerr_exp2: 1.69 * 2^-34 (Relative error of exp2(ylogx).) |
---|
42 | */ |
---|
43 | |
---|
44 | #define N (1 << POWF_LOG2_TABLE_BITS) |
---|
45 | #define T __powf_log2_data.tab |
---|
46 | #define A __powf_log2_data.poly |
---|
47 | #define OFF 0x3f330000 |
---|
48 | |
---|
49 | /* Subnormal input is normalized so ix has negative biased exponent. |
---|
50 | Output is multiplied by N (POWF_SCALE) if TOINT_INTRINICS is set. */ |
---|
51 | static inline double_t |
---|
52 | log2_inline (uint32_t ix) |
---|
53 | { |
---|
54 | /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */ |
---|
55 | double_t z, r, r2, r4, p, q, y, y0, invc, logc; |
---|
56 | uint32_t iz, top, tmp; |
---|
57 | int k, i; |
---|
58 | |
---|
59 | /* x = 2^k z; where z is in range [OFF,2*OFF] and exact. |
---|
60 | The range is split into N subintervals. |
---|
61 | The ith subinterval contains z and c is near its center. */ |
---|
62 | tmp = ix - OFF; |
---|
63 | i = (tmp >> (23 - POWF_LOG2_TABLE_BITS)) % N; |
---|
64 | top = tmp & 0xff800000; |
---|
65 | iz = ix - top; |
---|
66 | k = (int32_t) top >> (23 - POWF_SCALE_BITS); /* arithmetic shift */ |
---|
67 | invc = T[i].invc; |
---|
68 | logc = T[i].logc; |
---|
69 | z = (double_t) asfloat (iz); |
---|
70 | |
---|
71 | /* log2(x) = log1p(z/c-1)/ln2 + log2(c) + k */ |
---|
72 | r = z * invc - 1; |
---|
73 | y0 = logc + (double_t) k; |
---|
74 | |
---|
75 | /* Pipelined polynomial evaluation to approximate log1p(r)/ln2. */ |
---|
76 | r2 = r * r; |
---|
77 | y = A[0] * r + A[1]; |
---|
78 | p = A[2] * r + A[3]; |
---|
79 | r4 = r2 * r2; |
---|
80 | q = A[4] * r + y0; |
---|
81 | q = p * r2 + q; |
---|
82 | y = y * r4 + q; |
---|
83 | return y; |
---|
84 | } |
---|
85 | |
---|
86 | #undef N |
---|
87 | #undef T |
---|
88 | #define N (1 << EXP2F_TABLE_BITS) |
---|
89 | #define T __exp2f_data.tab |
---|
90 | #define SIGN_BIAS (1 << (EXP2F_TABLE_BITS + 11)) |
---|
91 | |
---|
92 | /* The output of log2 and thus the input of exp2 is either scaled by N |
---|
93 | (in case of fast toint intrinsics) or not. The unscaled xd must be |
---|
94 | in [-1021,1023], sign_bias sets the sign of the result. */ |
---|
95 | static inline double_t |
---|
96 | exp2_inline (double_t xd, unsigned long sign_bias) |
---|
97 | { |
---|
98 | uint64_t ki, ski, t; |
---|
99 | /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */ |
---|
100 | double_t kd, z, r, r2, y, s; |
---|
101 | |
---|
102 | #if TOINT_INTRINSICS |
---|
103 | # define C __exp2f_data.poly_scaled |
---|
104 | /* N*x = k + r with r in [-1/2, 1/2] */ |
---|
105 | kd = roundtoint (xd); /* k */ |
---|
106 | ki = converttoint (xd); |
---|
107 | #else |
---|
108 | # define C __exp2f_data.poly |
---|
109 | # define SHIFT __exp2f_data.shift_scaled |
---|
110 | /* x = k/N + r with r in [-1/(2N), 1/(2N)] */ |
---|
111 | kd = (double) (xd + SHIFT); /* Rounding to double precision is required. */ |
---|
112 | ki = asuint64 (kd); |
---|
113 | kd -= SHIFT; /* k/N */ |
---|
114 | #endif |
---|
115 | r = xd - kd; |
---|
116 | |
---|
117 | /* exp2(x) = 2^(k/N) * 2^r ~= s * (C0*r^3 + C1*r^2 + C2*r + 1) */ |
---|
118 | t = T[ki % N]; |
---|
119 | ski = ki + sign_bias; |
---|
120 | t += ski << (52 - EXP2F_TABLE_BITS); |
---|
121 | s = asdouble (t); |
---|
122 | z = C[0] * r + C[1]; |
---|
123 | r2 = r * r; |
---|
124 | y = C[2] * r + 1; |
---|
125 | y = z * r2 + y; |
---|
126 | y = y * s; |
---|
127 | return y; |
---|
128 | } |
---|
129 | |
---|
130 | /* Returns 0 if not int, 1 if odd int, 2 if even int. */ |
---|
131 | static inline int |
---|
132 | checkint (uint32_t iy) |
---|
133 | { |
---|
134 | int e = iy >> 23 & 0xff; |
---|
135 | if (e < 0x7f) |
---|
136 | return 0; |
---|
137 | if (e > 0x7f + 23) |
---|
138 | return 2; |
---|
139 | if (iy & ((1 << (0x7f + 23 - e)) - 1)) |
---|
140 | return 0; |
---|
141 | if (iy & (1 << (0x7f + 23 - e))) |
---|
142 | return 1; |
---|
143 | return 2; |
---|
144 | } |
---|
145 | |
---|
146 | static inline int |
---|
147 | zeroinfnan (uint32_t ix) |
---|
148 | { |
---|
149 | return 2 * ix - 1 >= 2u * 0x7f800000 - 1; |
---|
150 | } |
---|
151 | |
---|
152 | float |
---|
153 | powf (float x, float y) |
---|
154 | { |
---|
155 | unsigned long sign_bias = 0; |
---|
156 | uint32_t ix, iy; |
---|
157 | |
---|
158 | ix = asuint (x); |
---|
159 | iy = asuint (y); |
---|
160 | if (__builtin_expect (ix - 0x00800000 >= 0x7f800000 - 0x00800000 |
---|
161 | || zeroinfnan (iy), |
---|
162 | 0)) |
---|
163 | { |
---|
164 | /* Either (x < 0x1p-126 or inf or nan) or (y is 0 or inf or nan). */ |
---|
165 | if (__builtin_expect (zeroinfnan (iy), 0)) |
---|
166 | { |
---|
167 | if (2 * iy == 0) |
---|
168 | return issignalingf_inline (x) ? x + y : 1.0f; |
---|
169 | if (ix == 0x3f800000) |
---|
170 | return issignalingf_inline (y) ? x + y : 1.0f; |
---|
171 | if (2 * ix > 2u * 0x7f800000 || 2 * iy > 2u * 0x7f800000) |
---|
172 | return x + y; |
---|
173 | if (2 * ix == 2 * 0x3f800000) |
---|
174 | return 1.0f; |
---|
175 | if ((2 * ix < 2 * 0x3f800000) == !(iy & 0x80000000)) |
---|
176 | return 0.0f; /* |x|<1 && y==inf or |x|>1 && y==-inf. */ |
---|
177 | return y * y; |
---|
178 | } |
---|
179 | if (__builtin_expect (zeroinfnan (ix), 0)) |
---|
180 | { |
---|
181 | float_t x2 = x * x; |
---|
182 | if (ix & 0x80000000 && checkint (iy) == 1) |
---|
183 | { |
---|
184 | x2 = -x2; |
---|
185 | sign_bias = 1; |
---|
186 | } |
---|
187 | #if WANT_ERRNO |
---|
188 | if (2 * ix == 0 && iy & 0x80000000) |
---|
189 | return __math_divzerof (sign_bias); |
---|
190 | #endif |
---|
191 | return iy & 0x80000000 ? 1 / x2 : x2; |
---|
192 | } |
---|
193 | /* x and y are non-zero finite. */ |
---|
194 | if (ix & 0x80000000) |
---|
195 | { |
---|
196 | /* Finite x < 0. */ |
---|
197 | int yint = checkint (iy); |
---|
198 | if (yint == 0) |
---|
199 | return __math_invalidf (x); |
---|
200 | if (yint == 1) |
---|
201 | sign_bias = SIGN_BIAS; |
---|
202 | ix &= 0x7fffffff; |
---|
203 | } |
---|
204 | if (ix < 0x00800000) |
---|
205 | { |
---|
206 | /* Normalize subnormal x so exponent becomes negative. */ |
---|
207 | ix = asuint (x * 0x1p23f); |
---|
208 | ix &= 0x7fffffff; |
---|
209 | ix -= 23 << 23; |
---|
210 | } |
---|
211 | } |
---|
212 | double_t logx = log2_inline (ix); |
---|
213 | double_t ylogx = y * logx; /* Note: cannot overflow, y is single prec. */ |
---|
214 | if (__builtin_expect ((asuint64 (ylogx) >> 47 & 0xffff) |
---|
215 | >= asuint64 (126.0 * POWF_SCALE) >> 47, |
---|
216 | 0)) |
---|
217 | { |
---|
218 | /* |y*log(x)| >= 126. */ |
---|
219 | if (ylogx > 0x1.fffffffd1d571p+6 * POWF_SCALE) |
---|
220 | return __math_oflowf (sign_bias); |
---|
221 | if (ylogx <= -150.0 * POWF_SCALE) |
---|
222 | return __math_uflowf (sign_bias); |
---|
223 | #if WANT_ERRNO_UFLOW |
---|
224 | if (ylogx < -149.0 * POWF_SCALE) |
---|
225 | return __math_may_uflowf (sign_bias); |
---|
226 | #endif |
---|
227 | } |
---|
228 | return (float) exp2_inline (ylogx, sign_bias); |
---|
229 | } |
---|
230 | #endif /* !__OBSOLETE_MATH */ |
---|