1bbbf1280Sopenharmony_ci/*
2bbbf1280Sopenharmony_ci * Single-precision e^x function.
3bbbf1280Sopenharmony_ci *
4bbbf1280Sopenharmony_ci * Copyright (c) 2017-2019, Arm Limited.
5bbbf1280Sopenharmony_ci * SPDX-License-Identifier: MIT
6bbbf1280Sopenharmony_ci */
7bbbf1280Sopenharmony_ci
8bbbf1280Sopenharmony_ci#include <math.h>
9bbbf1280Sopenharmony_ci#include <stdint.h>
10bbbf1280Sopenharmony_ci#include "math_config.h"
11bbbf1280Sopenharmony_ci
12bbbf1280Sopenharmony_ci/*
13bbbf1280Sopenharmony_ciEXP2F_TABLE_BITS = 5
14bbbf1280Sopenharmony_ciEXP2F_POLY_ORDER = 3
15bbbf1280Sopenharmony_ci
16bbbf1280Sopenharmony_ciULP error: 0.502 (nearest rounding.)
17bbbf1280Sopenharmony_ciRelative error: 1.69 * 2^-34 in [-ln2/64, ln2/64] (before rounding.)
18bbbf1280Sopenharmony_ciWrong count: 170635 (all nearest rounding wrong results with fma.)
19bbbf1280Sopenharmony_ciNon-nearest ULP error: 1 (rounded ULP error)
20bbbf1280Sopenharmony_ci*/
21bbbf1280Sopenharmony_ci
22bbbf1280Sopenharmony_ci#define N (1 << EXP2F_TABLE_BITS)
23bbbf1280Sopenharmony_ci#define InvLn2N __exp2f_data.invln2_scaled
24bbbf1280Sopenharmony_ci#define T __exp2f_data.tab
25bbbf1280Sopenharmony_ci#define C __exp2f_data.poly_scaled
26bbbf1280Sopenharmony_ci
27bbbf1280Sopenharmony_cistatic inline uint32_t
28bbbf1280Sopenharmony_citop12 (float x)
29bbbf1280Sopenharmony_ci{
30bbbf1280Sopenharmony_ci  return asuint (x) >> 20;
31bbbf1280Sopenharmony_ci}
32bbbf1280Sopenharmony_ci
33bbbf1280Sopenharmony_cifloat
34bbbf1280Sopenharmony_ciexpf (float x)
35bbbf1280Sopenharmony_ci{
36bbbf1280Sopenharmony_ci  uint32_t abstop;
37bbbf1280Sopenharmony_ci  uint64_t ki, t;
38bbbf1280Sopenharmony_ci  /* double_t for better performance on targets with FLT_EVAL_METHOD==2.  */
39bbbf1280Sopenharmony_ci  double_t kd, xd, z, r, r2, y, s;
40bbbf1280Sopenharmony_ci
41bbbf1280Sopenharmony_ci  xd = (double_t) x;
42bbbf1280Sopenharmony_ci  abstop = top12 (x) & 0x7ff;
43bbbf1280Sopenharmony_ci  if (unlikely (abstop >= top12 (88.0f)))
44bbbf1280Sopenharmony_ci    {
45bbbf1280Sopenharmony_ci      /* |x| >= 88 or x is nan.  */
46bbbf1280Sopenharmony_ci      if (asuint (x) == asuint (-INFINITY))
47bbbf1280Sopenharmony_ci	return 0.0f;
48bbbf1280Sopenharmony_ci      if (abstop >= top12 (INFINITY))
49bbbf1280Sopenharmony_ci	return x + x;
50bbbf1280Sopenharmony_ci      if (x > 0x1.62e42ep6f) /* x > log(0x1p128) ~= 88.72 */
51bbbf1280Sopenharmony_ci	return __math_oflowf (0);
52bbbf1280Sopenharmony_ci      if (x < -0x1.9fe368p6f) /* x < log(0x1p-150) ~= -103.97 */
53bbbf1280Sopenharmony_ci	return __math_uflowf (0);
54bbbf1280Sopenharmony_ci#if WANT_ERRNO_UFLOW
55bbbf1280Sopenharmony_ci      if (x < -0x1.9d1d9ep6f) /* x < log(0x1p-149) ~= -103.28 */
56bbbf1280Sopenharmony_ci	return __math_may_uflowf (0);
57bbbf1280Sopenharmony_ci#endif
58bbbf1280Sopenharmony_ci    }
59bbbf1280Sopenharmony_ci
60bbbf1280Sopenharmony_ci  /* x*N/Ln2 = k + r with r in [-1/2, 1/2] and int k.  */
61bbbf1280Sopenharmony_ci  z = InvLn2N * xd;
62bbbf1280Sopenharmony_ci
63bbbf1280Sopenharmony_ci  /* Round and convert z to int, the result is in [-150*N, 128*N] and
64bbbf1280Sopenharmony_ci     ideally nearest int is used, otherwise the magnitude of r can be
65bbbf1280Sopenharmony_ci     bigger which gives larger approximation error.  */
66bbbf1280Sopenharmony_ci#if TOINT_INTRINSICS
67bbbf1280Sopenharmony_ci  kd = roundtoint (z);
68bbbf1280Sopenharmony_ci  ki = converttoint (z);
69bbbf1280Sopenharmony_ci#else
70bbbf1280Sopenharmony_ci# define SHIFT __exp2f_data.shift
71bbbf1280Sopenharmony_ci  kd = eval_as_double (z + SHIFT);
72bbbf1280Sopenharmony_ci  ki = asuint64 (kd);
73bbbf1280Sopenharmony_ci  kd -= SHIFT;
74bbbf1280Sopenharmony_ci#endif
75bbbf1280Sopenharmony_ci  r = z - kd;
76bbbf1280Sopenharmony_ci
77bbbf1280Sopenharmony_ci  /* exp(x) = 2^(k/N) * 2^(r/N) ~= s * (C0*r^3 + C1*r^2 + C2*r + 1) */
78bbbf1280Sopenharmony_ci  t = T[ki % N];
79bbbf1280Sopenharmony_ci  t += ki << (52 - EXP2F_TABLE_BITS);
80bbbf1280Sopenharmony_ci  s = asdouble (t);
81bbbf1280Sopenharmony_ci  z = C[0] * r + C[1];
82bbbf1280Sopenharmony_ci  r2 = r * r;
83bbbf1280Sopenharmony_ci  y = C[2] * r + 1;
84bbbf1280Sopenharmony_ci  y = z * r2 + y;
85bbbf1280Sopenharmony_ci  y = y * s;
86bbbf1280Sopenharmony_ci  return eval_as_float (y);
87bbbf1280Sopenharmony_ci}
88bbbf1280Sopenharmony_ci#if USE_GLIBC_ABI
89bbbf1280Sopenharmony_cistrong_alias (expf, __expf_finite)
90bbbf1280Sopenharmony_cihidden_alias (expf, __ieee754_expf)
91bbbf1280Sopenharmony_ci#endif
92