1 /* Single-precision pow function.
2 Copyright (c) 2017-2018 Arm Ltd. All rights reserved.
4 SPDX-License-Identifier: BSD-3-Clause
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
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
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. */
34 #include "math_config.h"
37 POWF_LOG2_POLY_ORDER = 5
40 ULP error: 0.82 (~ 0.5 + relerr*2^24)
41 relerr: 1.27 * 2^-26 (Relative error ~= 128*Ln2*relerr_log2 + relerr_exp2)
42 relerr_log2: 1.83 * 2^-33 (Relative error of logx.)
43 relerr_exp2: 1.69 * 2^-34 (Relative error of exp2(ylogx).)
46 #define N (1 << POWF_LOG2_TABLE_BITS)
47 #define T __powf_log2_data.tab
48 #define A __powf_log2_data.poly
49 #define OFF 0x3f330000
51 /* Subnormal input is normalized so ix has negative biased exponent.
52 Output is multiplied by N (POWF_SCALE) if TOINT_INTRINICS is set. */
53 static inline double_t
54 log2_inline (uint32_t ix
)
56 /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */
57 double_t z
, r
, r2
, r4
, p
, q
, y
, y0
, invc
, logc
;
58 uint32_t iz
, top
, tmp
;
61 /* x = 2^k z; where z is in range [OFF,2*OFF] and exact.
62 The range is split into N subintervals.
63 The ith subinterval contains z and c is near its center. */
65 i
= (tmp
>> (23 - POWF_LOG2_TABLE_BITS
)) % N
;
66 top
= tmp
& 0xff800000;
68 k
= (int32_t) top
>> (23 - POWF_SCALE_BITS
); /* arithmetic shift */
71 z
= (double_t
) asfloat (iz
);
73 /* log2(x) = log1p(z/c-1)/ln2 + log2(c) + k */
75 y0
= logc
+ (double_t
) k
;
77 /* Pipelined polynomial evaluation to approximate log1p(r)/ln2. */
90 #define N (1 << EXP2F_TABLE_BITS)
91 #define T __exp2f_data.tab
92 #define SIGN_BIAS (1 << (EXP2F_TABLE_BITS + 11))
94 /* The output of log2 and thus the input of exp2 is either scaled by N
95 (in case of fast toint intrinsics) or not. The unscaled xd must be
96 in [-1021,1023], sign_bias sets the sign of the result. */
97 static inline double_t
98 exp2_inline (double_t xd
, uint32_t sign_bias
)
101 /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */
102 double_t kd
, z
, r
, r2
, y
, s
;
105 # define C __exp2f_data.poly_scaled
106 /* N*x = k + r with r in [-1/2, 1/2] */
107 kd
= roundtoint (xd
); /* k */
108 ki
= converttoint (xd
);
110 # define C __exp2f_data.poly
111 # define SHIFT __exp2f_data.shift_scaled
112 /* x = k/N + r with r in [-1/(2N), 1/(2N)] */
113 kd
= (double) (xd
+ SHIFT
); /* Rounding to double precision is required. */
115 kd
-= SHIFT
; /* k/N */
119 /* exp2(x) = 2^(k/N) * 2^r ~= s * (C0*r^3 + C1*r^2 + C2*r + 1) */
121 ski
= ki
+ sign_bias
;
122 t
+= ski
<< (52 - EXP2F_TABLE_BITS
);
132 /* Returns 0 if not int, 1 if odd int, 2 if even int. The argument is
133 the bit representation of a non-zero finite floating-point value. */
135 checkint (uint32_t iy
)
137 int e
= iy
>> 23 & 0xff;
142 if (iy
& ((1 << (0x7f + 23 - e
)) - 1))
144 if (iy
& (1 << (0x7f + 23 - e
)))
150 zeroinfnan (uint32_t ix
)
152 return 2 * ix
- 1 >= 2u * 0x7f800000 - 1;
156 powf (float x
, float y
)
158 uint32_t sign_bias
= 0;
163 if (__builtin_expect (ix
- 0x00800000 >= 0x7f800000 - 0x00800000
167 /* Either (x < 0x1p-126 or inf or nan) or (y is 0 or inf or nan). */
168 if (__builtin_expect (zeroinfnan (iy
), 0))
171 return issignalingf_inline (x
) ? x
+ y
: 1.0f
;
172 if (ix
== 0x3f800000)
173 return issignalingf_inline (y
) ? x
+ y
: 1.0f
;
174 if (2 * ix
> 2u * 0x7f800000 || 2 * iy
> 2u * 0x7f800000)
176 if (2 * ix
== 2 * 0x3f800000)
178 if ((2 * ix
< 2 * 0x3f800000) == !(iy
& 0x80000000))
179 return 0.0f
; /* |x|<1 && y==inf or |x|>1 && y==-inf. */
182 if (__builtin_expect (zeroinfnan (ix
), 0))
185 if (ix
& 0x80000000 && checkint (iy
) == 1)
191 if (2 * ix
== 0 && iy
& 0x80000000)
192 return __math_divzerof (sign_bias
);
194 return iy
& 0x80000000 ? 1 / x2
: x2
;
196 /* x and y are non-zero finite. */
200 int yint
= checkint (iy
);
202 return __math_invalidf (x
);
204 sign_bias
= SIGN_BIAS
;
209 /* Normalize subnormal x so exponent becomes negative. */
210 ix
= asuint (x
* 0x1p
23f
);
215 double_t logx
= log2_inline (ix
);
216 double_t ylogx
= y
* logx
; /* Note: cannot overflow, y is single prec. */
217 if (__builtin_expect ((asuint64 (ylogx
) >> 47 & 0xffff)
218 >= asuint64 (126.0 * POWF_SCALE
) >> 47,
221 /* |y*log(x)| >= 126. */
222 if (ylogx
> 0x1.fffffffd1d571p
+6 * POWF_SCALE
)
223 /* |x^y| > 0x1.ffffffp127. */
224 return __math_oflowf (sign_bias
);
225 if (WANT_ROUNDING
&& WANT_ERRNO
226 && ylogx
> 0x1.fffffffa3aae2p
+6 * POWF_SCALE
)
227 /* |x^y| > 0x1.fffffep127, check if we round away from 0. */
229 && eval_as_float (1.0f
+ opt_barrier_float (0x1p
-25f
)) != 1.0f
)
231 && eval_as_float (-1.0f
- opt_barrier_float (0x1p
-25f
))
233 return __math_oflowf (sign_bias
);
234 if (ylogx
<= -150.0 * POWF_SCALE
)
235 return __math_uflowf (sign_bias
);
237 if (ylogx
< -149.0 * POWF_SCALE
)
238 return __math_may_uflowf (sign_bias
);
241 return (float) exp2_inline (ylogx
, sign_bias
);
243 #endif /* !__OBSOLETE_MATH */