131914882SAlex Richardson /* 231914882SAlex Richardson * Single-precision e^x function. 331914882SAlex Richardson * 431914882SAlex Richardson * Copyright (c) 2017-2019, Arm Limited. 5*072a4ba8SAndrew 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" 1131914882SAlex Richardson 1231914882SAlex Richardson /* 1331914882SAlex Richardson EXP2F_TABLE_BITS = 5 1431914882SAlex Richardson EXP2F_POLY_ORDER = 3 1531914882SAlex Richardson 1631914882SAlex Richardson ULP error: 0.502 (nearest rounding.) 1731914882SAlex Richardson Relative error: 1.69 * 2^-34 in [-ln2/64, ln2/64] (before rounding.) 1831914882SAlex Richardson Wrong count: 170635 (all nearest rounding wrong results with fma.) 1931914882SAlex Richardson Non-nearest ULP error: 1 (rounded ULP error) 2031914882SAlex Richardson */ 2131914882SAlex Richardson 2231914882SAlex Richardson #define N (1 << EXP2F_TABLE_BITS) 2331914882SAlex Richardson #define InvLn2N __exp2f_data.invln2_scaled 2431914882SAlex Richardson #define T __exp2f_data.tab 2531914882SAlex Richardson #define C __exp2f_data.poly_scaled 2631914882SAlex Richardson 2731914882SAlex Richardson static inline uint32_t 2831914882SAlex Richardson top12 (float x) 2931914882SAlex Richardson { 3031914882SAlex Richardson return asuint (x) >> 20; 3131914882SAlex Richardson } 3231914882SAlex Richardson 3331914882SAlex Richardson float 3431914882SAlex Richardson expf (float x) 3531914882SAlex Richardson { 3631914882SAlex Richardson uint32_t abstop; 3731914882SAlex Richardson uint64_t ki, t; 3831914882SAlex Richardson /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */ 3931914882SAlex Richardson double_t kd, xd, z, r, r2, y, s; 4031914882SAlex Richardson 4131914882SAlex Richardson xd = (double_t) x; 4231914882SAlex Richardson abstop = top12 (x) & 0x7ff; 4331914882SAlex Richardson if (unlikely (abstop >= top12 (88.0f))) 4431914882SAlex Richardson { 4531914882SAlex Richardson /* |x| >= 88 or x is nan. */ 4631914882SAlex Richardson if (asuint (x) == asuint (-INFINITY)) 4731914882SAlex Richardson return 0.0f; 4831914882SAlex Richardson if (abstop >= top12 (INFINITY)) 4931914882SAlex Richardson return x + x; 5031914882SAlex Richardson if (x > 0x1.62e42ep6f) /* x > log(0x1p128) ~= 88.72 */ 5131914882SAlex Richardson return __math_oflowf (0); 5231914882SAlex Richardson if (x < -0x1.9fe368p6f) /* x < log(0x1p-150) ~= -103.97 */ 5331914882SAlex Richardson return __math_uflowf (0); 5431914882SAlex Richardson #if WANT_ERRNO_UFLOW 5531914882SAlex Richardson if (x < -0x1.9d1d9ep6f) /* x < log(0x1p-149) ~= -103.28 */ 5631914882SAlex Richardson return __math_may_uflowf (0); 5731914882SAlex Richardson #endif 5831914882SAlex Richardson } 5931914882SAlex Richardson 6031914882SAlex Richardson /* x*N/Ln2 = k + r with r in [-1/2, 1/2] and int k. */ 6131914882SAlex Richardson z = InvLn2N * xd; 6231914882SAlex Richardson 6331914882SAlex Richardson /* Round and convert z to int, the result is in [-150*N, 128*N] and 6431914882SAlex Richardson ideally nearest int is used, otherwise the magnitude of r can be 6531914882SAlex Richardson bigger which gives larger approximation error. */ 6631914882SAlex Richardson #if TOINT_INTRINSICS 6731914882SAlex Richardson kd = roundtoint (z); 6831914882SAlex Richardson ki = converttoint (z); 6931914882SAlex Richardson #else 7031914882SAlex Richardson # define SHIFT __exp2f_data.shift 7131914882SAlex Richardson kd = eval_as_double (z + SHIFT); 7231914882SAlex Richardson ki = asuint64 (kd); 7331914882SAlex Richardson kd -= SHIFT; 7431914882SAlex Richardson #endif 7531914882SAlex Richardson r = z - kd; 7631914882SAlex Richardson 7731914882SAlex Richardson /* exp(x) = 2^(k/N) * 2^(r/N) ~= s * (C0*r^3 + C1*r^2 + C2*r + 1) */ 7831914882SAlex Richardson t = T[ki % N]; 7931914882SAlex Richardson t += ki << (52 - EXP2F_TABLE_BITS); 8031914882SAlex Richardson s = asdouble (t); 8131914882SAlex Richardson z = C[0] * r + C[1]; 8231914882SAlex Richardson r2 = r * r; 8331914882SAlex Richardson y = C[2] * r + 1; 8431914882SAlex Richardson y = z * r2 + y; 8531914882SAlex Richardson y = y * s; 8631914882SAlex Richardson return eval_as_float (y); 8731914882SAlex Richardson } 8831914882SAlex Richardson #if USE_GLIBC_ABI 8931914882SAlex Richardson strong_alias (expf, __expf_finite) 9031914882SAlex Richardson hidden_alias (expf, __ieee754_expf) 9131914882SAlex Richardson #endif 92