xref: /freebsd/contrib/arm-optimized-routines/math/exp2f.c (revision 31914882fca502069810b9e9ddea4bcd8136a4f4)
1*31914882SAlex Richardson /*
2*31914882SAlex Richardson  * Single-precision 2^x function.
3*31914882SAlex Richardson  *
4*31914882SAlex Richardson  * Copyright (c) 2017-2018, Arm Limited.
5*31914882SAlex Richardson  * SPDX-License-Identifier: MIT
6*31914882SAlex Richardson  */
7*31914882SAlex Richardson 
8*31914882SAlex Richardson #include <math.h>
9*31914882SAlex Richardson #include <stdint.h>
10*31914882SAlex Richardson #include "math_config.h"
11*31914882SAlex Richardson 
12*31914882SAlex Richardson /*
13*31914882SAlex Richardson EXP2F_TABLE_BITS = 5
14*31914882SAlex Richardson EXP2F_POLY_ORDER = 3
15*31914882SAlex Richardson 
16*31914882SAlex Richardson ULP error: 0.502 (nearest rounding.)
17*31914882SAlex Richardson Relative error: 1.69 * 2^-34 in [-1/64, 1/64] (before rounding.)
18*31914882SAlex Richardson Wrong count: 168353 (all nearest rounding wrong results with fma.)
19*31914882SAlex Richardson Non-nearest ULP error: 1 (rounded ULP error)
20*31914882SAlex Richardson */
21*31914882SAlex Richardson 
22*31914882SAlex Richardson #define N (1 << EXP2F_TABLE_BITS)
23*31914882SAlex Richardson #define T __exp2f_data.tab
24*31914882SAlex Richardson #define C __exp2f_data.poly
25*31914882SAlex Richardson #define SHIFT __exp2f_data.shift_scaled
26*31914882SAlex Richardson 
27*31914882SAlex Richardson static inline uint32_t
28*31914882SAlex Richardson top12 (float x)
29*31914882SAlex Richardson {
30*31914882SAlex Richardson   return asuint (x) >> 20;
31*31914882SAlex Richardson }
32*31914882SAlex Richardson 
33*31914882SAlex Richardson float
34*31914882SAlex Richardson exp2f (float x)
35*31914882SAlex Richardson {
36*31914882SAlex Richardson   uint32_t abstop;
37*31914882SAlex Richardson   uint64_t ki, t;
38*31914882SAlex Richardson   /* double_t for better performance on targets with FLT_EVAL_METHOD==2.  */
39*31914882SAlex Richardson   double_t kd, xd, z, r, r2, y, s;
40*31914882SAlex Richardson 
41*31914882SAlex Richardson   xd = (double_t) x;
42*31914882SAlex Richardson   abstop = top12 (x) & 0x7ff;
43*31914882SAlex Richardson   if (unlikely (abstop >= top12 (128.0f)))
44*31914882SAlex Richardson     {
45*31914882SAlex Richardson       /* |x| >= 128 or x is nan.  */
46*31914882SAlex Richardson       if (asuint (x) == asuint (-INFINITY))
47*31914882SAlex Richardson 	return 0.0f;
48*31914882SAlex Richardson       if (abstop >= top12 (INFINITY))
49*31914882SAlex Richardson 	return x + x;
50*31914882SAlex Richardson       if (x > 0.0f)
51*31914882SAlex Richardson 	return __math_oflowf (0);
52*31914882SAlex Richardson       if (x <= -150.0f)
53*31914882SAlex Richardson 	return __math_uflowf (0);
54*31914882SAlex Richardson #if WANT_ERRNO_UFLOW
55*31914882SAlex Richardson       if (x < -149.0f)
56*31914882SAlex Richardson 	return __math_may_uflowf (0);
57*31914882SAlex Richardson #endif
58*31914882SAlex Richardson     }
59*31914882SAlex Richardson 
60*31914882SAlex Richardson   /* x = k/N + r with r in [-1/(2N), 1/(2N)] and int k.  */
61*31914882SAlex Richardson   kd = eval_as_double (xd + SHIFT);
62*31914882SAlex Richardson   ki = asuint64 (kd);
63*31914882SAlex Richardson   kd -= SHIFT; /* k/N for int k.  */
64*31914882SAlex Richardson   r = xd - kd;
65*31914882SAlex Richardson 
66*31914882SAlex Richardson   /* exp2(x) = 2^(k/N) * 2^r ~= s * (C0*r^3 + C1*r^2 + C2*r + 1) */
67*31914882SAlex Richardson   t = T[ki % N];
68*31914882SAlex Richardson   t += ki << (52 - EXP2F_TABLE_BITS);
69*31914882SAlex Richardson   s = asdouble (t);
70*31914882SAlex Richardson   z = C[0] * r + C[1];
71*31914882SAlex Richardson   r2 = r * r;
72*31914882SAlex Richardson   y = C[2] * r + 1;
73*31914882SAlex Richardson   y = z * r2 + y;
74*31914882SAlex Richardson   y = y * s;
75*31914882SAlex Richardson   return eval_as_float (y);
76*31914882SAlex Richardson }
77*31914882SAlex Richardson #if USE_GLIBC_ABI
78*31914882SAlex Richardson strong_alias (exp2f, __exp2f_finite)
79*31914882SAlex Richardson hidden_alias (exp2f, __ieee754_exp2f)
80*31914882SAlex Richardson #endif
81