1bbbf1280Sopenharmony_ci/*
2bbbf1280Sopenharmony_ci * Single-precision 2^x function.
3bbbf1280Sopenharmony_ci *
4bbbf1280Sopenharmony_ci * Copyright (c) 2017-2018, 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 [-1/64, 1/64] (before rounding.)
18bbbf1280Sopenharmony_ciWrong count: 168353 (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 T __exp2f_data.tab
24bbbf1280Sopenharmony_ci#define C __exp2f_data.poly
25bbbf1280Sopenharmony_ci#define SHIFT __exp2f_data.shift_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_ciexp2f (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 (128.0f)))
44bbbf1280Sopenharmony_ci    {
45bbbf1280Sopenharmony_ci      /* |x| >= 128 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 > 0.0f)
51bbbf1280Sopenharmony_ci	return __math_oflowf (0);
52bbbf1280Sopenharmony_ci      if (x <= -150.0f)
53bbbf1280Sopenharmony_ci	return __math_uflowf (0);
54bbbf1280Sopenharmony_ci#if WANT_ERRNO_UFLOW
55bbbf1280Sopenharmony_ci      if (x < -149.0f)
56bbbf1280Sopenharmony_ci	return __math_may_uflowf (0);
57bbbf1280Sopenharmony_ci#endif
58bbbf1280Sopenharmony_ci    }
59bbbf1280Sopenharmony_ci
60bbbf1280Sopenharmony_ci  /* x = k/N + r with r in [-1/(2N), 1/(2N)] and int k.  */
61bbbf1280Sopenharmony_ci  kd = eval_as_double (xd + SHIFT);
62bbbf1280Sopenharmony_ci  ki = asuint64 (kd);
63bbbf1280Sopenharmony_ci  kd -= SHIFT; /* k/N for int k.  */
64bbbf1280Sopenharmony_ci  r = xd - kd;
65bbbf1280Sopenharmony_ci
66bbbf1280Sopenharmony_ci  /* exp2(x) = 2^(k/N) * 2^r ~= s * (C0*r^3 + C1*r^2 + C2*r + 1) */
67bbbf1280Sopenharmony_ci  t = T[ki % N];
68bbbf1280Sopenharmony_ci  t += ki << (52 - EXP2F_TABLE_BITS);
69bbbf1280Sopenharmony_ci  s = asdouble (t);
70bbbf1280Sopenharmony_ci  z = C[0] * r + C[1];
71bbbf1280Sopenharmony_ci  r2 = r * r;
72bbbf1280Sopenharmony_ci  y = C[2] * r + 1;
73bbbf1280Sopenharmony_ci  y = z * r2 + y;
74bbbf1280Sopenharmony_ci  y = y * s;
75bbbf1280Sopenharmony_ci  return eval_as_float (y);
76bbbf1280Sopenharmony_ci}
77bbbf1280Sopenharmony_ci#if USE_GLIBC_ABI
78bbbf1280Sopenharmony_cistrong_alias (exp2f, __exp2f_finite)
79bbbf1280Sopenharmony_cihidden_alias (exp2f, __ieee754_exp2f)
80bbbf1280Sopenharmony_ci#endif
81