131914882SAlex Richardson /*
231914882SAlex Richardson * Single-precision 2^x function.
331914882SAlex Richardson *
4*f3087befSAndrew Turner * Copyright (c) 2017-2024, Arm Limited.
5072a4ba8SAndrew Turner * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
631914882SAlex Richardson */
731914882SAlex Richardson
831914882SAlex Richardson #include <math.h>
931914882SAlex Richardson #include <stdint.h>
1031914882SAlex Richardson #include "math_config.h"
11*f3087befSAndrew Turner #include "test_defs.h"
12*f3087befSAndrew Turner #include "test_sig.h"
1331914882SAlex Richardson
1431914882SAlex Richardson /*
1531914882SAlex Richardson EXP2F_TABLE_BITS = 5
1631914882SAlex Richardson EXP2F_POLY_ORDER = 3
1731914882SAlex Richardson
1831914882SAlex Richardson ULP error: 0.502 (nearest rounding.)
1931914882SAlex Richardson Relative error: 1.69 * 2^-34 in [-1/64, 1/64] (before rounding.)
2031914882SAlex Richardson Wrong count: 168353 (all nearest rounding wrong results with fma.)
2131914882SAlex Richardson Non-nearest ULP error: 1 (rounded ULP error)
2231914882SAlex Richardson */
2331914882SAlex Richardson
2431914882SAlex Richardson #define N (1 << EXP2F_TABLE_BITS)
2531914882SAlex Richardson #define T __exp2f_data.tab
2631914882SAlex Richardson #define C __exp2f_data.poly
2731914882SAlex Richardson #define SHIFT __exp2f_data.shift_scaled
2831914882SAlex Richardson
2931914882SAlex Richardson static inline uint32_t
top12(float x)3031914882SAlex Richardson top12 (float x)
3131914882SAlex Richardson {
3231914882SAlex Richardson return asuint (x) >> 20;
3331914882SAlex Richardson }
3431914882SAlex Richardson
3531914882SAlex Richardson float
exp2f(float x)3631914882SAlex Richardson exp2f (float x)
3731914882SAlex Richardson {
3831914882SAlex Richardson uint32_t abstop;
3931914882SAlex Richardson uint64_t ki, t;
4031914882SAlex Richardson /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */
4131914882SAlex Richardson double_t kd, xd, z, r, r2, y, s;
4231914882SAlex Richardson
4331914882SAlex Richardson xd = (double_t) x;
4431914882SAlex Richardson abstop = top12 (x) & 0x7ff;
4531914882SAlex Richardson if (unlikely (abstop >= top12 (128.0f)))
4631914882SAlex Richardson {
4731914882SAlex Richardson /* |x| >= 128 or x is nan. */
4831914882SAlex Richardson if (asuint (x) == asuint (-INFINITY))
4931914882SAlex Richardson return 0.0f;
5031914882SAlex Richardson if (abstop >= top12 (INFINITY))
5131914882SAlex Richardson return x + x;
5231914882SAlex Richardson if (x > 0.0f)
5331914882SAlex Richardson return __math_oflowf (0);
5431914882SAlex Richardson if (x <= -150.0f)
5531914882SAlex Richardson return __math_uflowf (0);
5631914882SAlex Richardson #if WANT_ERRNO_UFLOW
5731914882SAlex Richardson if (x < -149.0f)
5831914882SAlex Richardson return __math_may_uflowf (0);
5931914882SAlex Richardson #endif
6031914882SAlex Richardson }
6131914882SAlex Richardson
6231914882SAlex Richardson /* x = k/N + r with r in [-1/(2N), 1/(2N)] and int k. */
6331914882SAlex Richardson kd = eval_as_double (xd + SHIFT);
6431914882SAlex Richardson ki = asuint64 (kd);
6531914882SAlex Richardson kd -= SHIFT; /* k/N for int k. */
6631914882SAlex Richardson r = xd - kd;
6731914882SAlex Richardson
6831914882SAlex Richardson /* exp2(x) = 2^(k/N) * 2^r ~= s * (C0*r^3 + C1*r^2 + C2*r + 1) */
6931914882SAlex Richardson t = T[ki % N];
7031914882SAlex Richardson t += ki << (52 - EXP2F_TABLE_BITS);
7131914882SAlex Richardson s = asdouble (t);
7231914882SAlex Richardson z = C[0] * r + C[1];
7331914882SAlex Richardson r2 = r * r;
7431914882SAlex Richardson y = C[2] * r + 1;
7531914882SAlex Richardson y = z * r2 + y;
7631914882SAlex Richardson y = y * s;
7731914882SAlex Richardson return eval_as_float (y);
7831914882SAlex Richardson }
7931914882SAlex Richardson #if USE_GLIBC_ABI
8031914882SAlex Richardson strong_alias (exp2f, __exp2f_finite)
8131914882SAlex Richardson hidden_alias (exp2f, __ieee754_exp2f)
8231914882SAlex Richardson #endif
83*f3087befSAndrew Turner
84*f3087befSAndrew Turner TEST_SIG (S, F, 1, exp2, -9.9, 9.9)
85*f3087befSAndrew Turner TEST_ULP (exp2f, 0.01)
86*f3087befSAndrew Turner TEST_ULP_NONNEAREST (exp2f, 0.5)
87*f3087befSAndrew Turner TEST_INTERVAL (exp2f, 0, 0xffff0000, 10000)
88*f3087befSAndrew Turner TEST_SYM_INTERVAL (exp2f, 0x1p-14, 0x1p8, 50000)
89