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

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