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

source code of libc/AOR_v20.02/math/powf.c