131914882SAlex Richardson /* 231914882SAlex Richardson * Single-precision e^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 [-ln2/64, ln2/64] (before rounding.) 2031914882SAlex Richardson Wrong count: 170635 (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 InvLn2N __exp2f_data.invln2_scaled 2631914882SAlex Richardson #define T __exp2f_data.tab 2731914882SAlex Richardson #define C __exp2f_data.poly_scaled 2831914882SAlex Richardson 2931914882SAlex Richardson static inline uint32_t 3031914882SAlex Richardson top12 (float x) 3131914882SAlex Richardson { 3231914882SAlex Richardson return asuint (x) >> 20; 3331914882SAlex Richardson } 3431914882SAlex Richardson 3531914882SAlex Richardson float 3631914882SAlex Richardson expf (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 (88.0f))) 4631914882SAlex Richardson { 4731914882SAlex Richardson /* |x| >= 88 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 > 0x1.62e42ep6f) /* x > log(0x1p128) ~= 88.72 */ 5331914882SAlex Richardson return __math_oflowf (0); 5431914882SAlex Richardson if (x < -0x1.9fe368p6f) /* x < log(0x1p-150) ~= -103.97 */ 5531914882SAlex Richardson return __math_uflowf (0); 5631914882SAlex Richardson #if WANT_ERRNO_UFLOW 5731914882SAlex Richardson if (x < -0x1.9d1d9ep6f) /* x < log(0x1p-149) ~= -103.28 */ 5831914882SAlex Richardson return __math_may_uflowf (0); 5931914882SAlex Richardson #endif 6031914882SAlex Richardson } 6131914882SAlex Richardson 6231914882SAlex Richardson /* x*N/Ln2 = k + r with r in [-1/2, 1/2] and int k. */ 6331914882SAlex Richardson z = InvLn2N * xd; 6431914882SAlex Richardson 6531914882SAlex Richardson /* Round and convert z to int, the result is in [-150*N, 128*N] and 6631914882SAlex Richardson ideally nearest int is used, otherwise the magnitude of r can be 6731914882SAlex Richardson bigger which gives larger approximation error. */ 6831914882SAlex Richardson #if TOINT_INTRINSICS 6931914882SAlex Richardson kd = roundtoint (z); 7031914882SAlex Richardson ki = converttoint (z); 7131914882SAlex Richardson #else 7231914882SAlex Richardson # define SHIFT __exp2f_data.shift 7331914882SAlex Richardson kd = eval_as_double (z + SHIFT); 7431914882SAlex Richardson ki = asuint64 (kd); 7531914882SAlex Richardson kd -= SHIFT; 7631914882SAlex Richardson #endif 7731914882SAlex Richardson r = z - kd; 7831914882SAlex Richardson 7931914882SAlex Richardson /* exp(x) = 2^(k/N) * 2^(r/N) ~= s * (C0*r^3 + C1*r^2 + C2*r + 1) */ 8031914882SAlex Richardson t = T[ki % N]; 8131914882SAlex Richardson t += ki << (52 - EXP2F_TABLE_BITS); 8231914882SAlex Richardson s = asdouble (t); 8331914882SAlex Richardson z = C[0] * r + C[1]; 8431914882SAlex Richardson r2 = r * r; 8531914882SAlex Richardson y = C[2] * r + 1; 8631914882SAlex Richardson y = z * r2 + y; 8731914882SAlex Richardson y = y * s; 8831914882SAlex Richardson return eval_as_float (y); 8931914882SAlex Richardson } 9031914882SAlex Richardson #if USE_GLIBC_ABI 9131914882SAlex Richardson strong_alias (expf, __expf_finite) 9231914882SAlex Richardson hidden_alias (expf, __ieee754_expf) 9331914882SAlex Richardson #endif 94*f3087befSAndrew Turner 95*f3087befSAndrew Turner TEST_SIG (S, F, 1, exp, -9.9, 9.9) 96*f3087befSAndrew Turner TEST_ULP (expf, 0.01) 97*f3087befSAndrew Turner TEST_ULP_NONNEAREST (expf, 0.5) 98*f3087befSAndrew Turner TEST_INTERVAL (expf, 0, 0xffff0000, 10000) 99*f3087befSAndrew Turner TEST_SYM_INTERVAL (expf, 0x1p-14, 0x1p8, 500000) 100