blob: 94b32538aa0de9c7e47ea3df9a5c60b7851bbeed [file] [log] [blame]
Szabolcs Nagy86067a72017-08-11 15:35:12 +01001/*
Szabolcs Nagy1b945972018-05-14 14:46:40 +01002 * Single-precision 2^x function.
Szabolcs Nagy86067a72017-08-11 15:35:12 +01003 *
Szabolcs Nagy11253b02018-11-12 11:10:57 +00004 * Copyright (c) 2017-2018, Arm Limited.
5 * SPDX-License-Identifier: MIT
Szabolcs Nagy86067a72017-08-11 15:35:12 +01006 */
7
8#include <math.h>
9#include <stdint.h>
10#include "math_config.h"
11
12/*
13EXP2F_TABLE_BITS = 5
14EXP2F_POLY_ORDER = 3
15
16ULP error: 0.502 (nearest rounding.)
17Relative error: 1.69 * 2^-34 in [-1/64, 1/64] (before rounding.)
18Wrong count: 168353 (all nearest rounding wrong results with fma.)
19Non-nearest ULP error: 1 (rounded ULP error)
20*/
21
22#define N (1 << EXP2F_TABLE_BITS)
23#define T __exp2f_data.tab
24#define C __exp2f_data.poly
25#define SHIFT __exp2f_data.shift_scaled
26
27static inline uint32_t
28top12 (float x)
29{
30 return asuint (x) >> 20;
31}
32
33float
Szabolcs Nagy0d51c042018-04-25 13:26:22 +010034exp2f (float x)
Szabolcs Nagy86067a72017-08-11 15:35:12 +010035{
36 uint32_t abstop;
37 uint64_t ki, t;
38 /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */
39 double_t kd, xd, z, r, r2, y, s;
40
41 xd = (double_t) x;
42 abstop = top12 (x) & 0x7ff;
Szabolcs Nagyb21378a2018-05-11 13:29:44 +010043 if (unlikely (abstop >= top12 (128.0f)))
Szabolcs Nagy86067a72017-08-11 15:35:12 +010044 {
45 /* |x| >= 128 or x is nan. */
46 if (asuint (x) == asuint (-INFINITY))
47 return 0.0f;
48 if (abstop >= top12 (INFINITY))
49 return x + x;
50 if (x > 0.0f)
51 return __math_oflowf (0);
52 if (x <= -150.0f)
53 return __math_uflowf (0);
54#if WANT_ERRNO_UFLOW
55 if (x < -149.0f)
56 return __math_may_uflowf (0);
57#endif
58 }
59
60 /* x = k/N + r with r in [-1/(2N), 1/(2N)] and int k. */
Szabolcs Nagy5eb9d192018-05-11 13:20:04 +010061 kd = eval_as_double (xd + SHIFT);
Szabolcs Nagy86067a72017-08-11 15:35:12 +010062 ki = asuint64 (kd);
63 kd -= SHIFT; /* k/N for int k. */
64 r = xd - kd;
65
66 /* exp2(x) = 2^(k/N) * 2^r ~= s * (C0*r^3 + C1*r^2 + C2*r + 1) */
67 t = T[ki % N];
68 t += ki << (52 - EXP2F_TABLE_BITS);
69 s = asdouble (t);
70 z = C[0] * r + C[1];
71 r2 = r * r;
72 y = C[2] * r + 1;
73 y = z * r2 + y;
74 y = y * s;
Szabolcs Nagy04884bd2018-12-07 14:58:51 +000075 return eval_as_float (y);
Szabolcs Nagy86067a72017-08-11 15:35:12 +010076}
Szabolcs Nagyb7d568d2018-06-06 12:26:56 +010077#if USE_GLIBC_ABI
78strong_alias (exp2f, __exp2f_finite)
79hidden_alias (exp2f, __ieee754_exp2f)
80#endif