]> git.proxmox.com Git - wasi-libc.git/blame - libc-top-half/musl/src/math/exp2f.c
Update to musl 1.1.23.
[wasi-libc.git] / libc-top-half / musl / src / math / exp2f.c
CommitLineData
d4db3fa2
DG
1/*
2 * Single-precision 2^x function.
320054e8 3 *
d4db3fa2
DG
4 * Copyright (c) 2017-2018, Arm Limited.
5 * SPDX-License-Identifier: MIT
320054e8
DG
6 */
7
d4db3fa2
DG
8#include <math.h>
9#include <stdint.h>
320054e8 10#include "libm.h"
d4db3fa2 11#include "exp2f_data.h"
320054e8 12
d4db3fa2
DG
13/*
14EXP2F_TABLE_BITS = 5
15EXP2F_POLY_ORDER = 3
320054e8 16
d4db3fa2
DG
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*/
320054e8 22
d4db3fa2
DG
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 top12(float x)
29{
30 return asuint(x) >> 20;
31}
320054e8 32
320054e8
DG
33float exp2f(float x)
34{
d4db3fa2
DG
35 uint32_t abstop;
36 uint64_t ki, t;
37 double_t kd, xd, z, r, r2, y, s;
320054e8 38
d4db3fa2
DG
39 xd = (double_t)x;
40 abstop = top12(x) & 0x7ff;
41 if (predict_false(abstop >= top12(128.0f))) {
42 /* |x| >= 128 or x is nan. */
43 if (asuint(x) == asuint(-INFINITY))
44 return 0.0f;
45 if (abstop >= top12(INFINITY))
46 return x + x;
47 if (x > 0.0f)
48 return __math_oflowf(0);
49 if (x <= -150.0f)
50 return __math_uflowf(0);
320054e8
DG
51 }
52
d4db3fa2
DG
53 /* x = k/N + r with r in [-1/(2N), 1/(2N)] and int k. */
54 kd = eval_as_double(xd + SHIFT);
55 ki = asuint64(kd);
56 kd -= SHIFT; /* k/N for int k. */
57 r = xd - kd;
320054e8 58
d4db3fa2
DG
59 /* exp2(x) = 2^(k/N) * 2^r ~= s * (C0*r^3 + C1*r^2 + C2*r + 1) */
60 t = T[ki % N];
61 t += ki << (52 - EXP2F_TABLE_BITS);
62 s = asdouble(t);
63 z = C[0] * r + C[1];
64 r2 = r * r;
65 y = C[2] * r + 1;
66 y = z * r2 + y;
67 y = y * s;
68 return eval_as_float(y);
320054e8 69}