131914882SAlex Richardson /* 231914882SAlex Richardson * Single-precision 2^x function. 331914882SAlex Richardson * 431914882SAlex Richardson * Copyright (c) 2017-2018, 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 [-1/64, 1/64] (before rounding.) 1831914882SAlex Richardson Wrong count: 168353 (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 T __exp2f_data.tab 2431914882SAlex Richardson #define C __exp2f_data.poly 2531914882SAlex Richardson #define SHIFT __exp2f_data.shift_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 exp2f (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 (128.0f))) 4431914882SAlex Richardson { 4531914882SAlex Richardson /* |x| >= 128 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 > 0.0f) 5131914882SAlex Richardson return __math_oflowf (0); 5231914882SAlex Richardson if (x <= -150.0f) 5331914882SAlex Richardson return __math_uflowf (0); 5431914882SAlex Richardson #if WANT_ERRNO_UFLOW 5531914882SAlex Richardson if (x < -149.0f) 5631914882SAlex Richardson return __math_may_uflowf (0); 5731914882SAlex Richardson #endif 5831914882SAlex Richardson } 5931914882SAlex Richardson 6031914882SAlex Richardson /* x = k/N + r with r in [-1/(2N), 1/(2N)] and int k. */ 6131914882SAlex Richardson kd = eval_as_double (xd + SHIFT); 6231914882SAlex Richardson ki = asuint64 (kd); 6331914882SAlex Richardson kd -= SHIFT; /* k/N for int k. */ 6431914882SAlex Richardson r = xd - kd; 6531914882SAlex Richardson 6631914882SAlex Richardson /* exp2(x) = 2^(k/N) * 2^r ~= s * (C0*r^3 + C1*r^2 + C2*r + 1) */ 6731914882SAlex Richardson t = T[ki % N]; 6831914882SAlex Richardson t += ki << (52 - EXP2F_TABLE_BITS); 6931914882SAlex Richardson s = asdouble (t); 7031914882SAlex Richardson z = C[0] * r + C[1]; 7131914882SAlex Richardson r2 = r * r; 7231914882SAlex Richardson y = C[2] * r + 1; 7331914882SAlex Richardson y = z * r2 + y; 7431914882SAlex Richardson y = y * s; 7531914882SAlex Richardson return eval_as_float (y); 7631914882SAlex Richardson } 7731914882SAlex Richardson #if USE_GLIBC_ABI 7831914882SAlex Richardson strong_alias (exp2f, __exp2f_finite) 7931914882SAlex Richardson hidden_alias (exp2f, __ieee754_exp2f) 8031914882SAlex Richardson #endif 81