xref: /aosp_15_r20/external/arm-optimized-routines/pl/math/v_expm1_2u5.c (revision 412f47f9e737e10ed5cc46ec6a8d7fa2264f8a14)
1*412f47f9SXin Li /*
2*412f47f9SXin Li  * Double-precision vector exp(x) - 1 function.
3*412f47f9SXin Li  *
4*412f47f9SXin Li  * Copyright (c) 2022-2024, Arm Limited.
5*412f47f9SXin Li  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6*412f47f9SXin Li  */
7*412f47f9SXin Li 
8*412f47f9SXin Li #include "v_math.h"
9*412f47f9SXin Li #include "poly_advsimd_f64.h"
10*412f47f9SXin Li #include "pl_sig.h"
11*412f47f9SXin Li #include "pl_test.h"
12*412f47f9SXin Li 
13*412f47f9SXin Li static const struct data
14*412f47f9SXin Li {
15*412f47f9SXin Li   float64x2_t poly[11];
16*412f47f9SXin Li   float64x2_t invln2;
17*412f47f9SXin Li   double ln2[2];
18*412f47f9SXin Li   float64x2_t shift;
19*412f47f9SXin Li   int64x2_t exponent_bias;
20*412f47f9SXin Li #if WANT_SIMD_EXCEPT
21*412f47f9SXin Li   uint64x2_t thresh, tiny_bound;
22*412f47f9SXin Li #else
23*412f47f9SXin Li   float64x2_t oflow_bound;
24*412f47f9SXin Li #endif
25*412f47f9SXin Li } data = {
26*412f47f9SXin Li   /* Generated using fpminimax, with degree=12 in [log(2)/2, log(2)/2].  */
27*412f47f9SXin Li   .poly = { V2 (0x1p-1), V2 (0x1.5555555555559p-3), V2 (0x1.555555555554bp-5),
28*412f47f9SXin Li 	    V2 (0x1.111111110f663p-7), V2 (0x1.6c16c16c1b5f3p-10),
29*412f47f9SXin Li 	    V2 (0x1.a01a01affa35dp-13), V2 (0x1.a01a018b4ecbbp-16),
30*412f47f9SXin Li 	    V2 (0x1.71ddf82db5bb4p-19), V2 (0x1.27e517fc0d54bp-22),
31*412f47f9SXin Li 	    V2 (0x1.af5eedae67435p-26), V2 (0x1.1f143d060a28ap-29) },
32*412f47f9SXin Li   .invln2 = V2 (0x1.71547652b82fep0),
33*412f47f9SXin Li   .ln2 = { 0x1.62e42fefa39efp-1, 0x1.abc9e3b39803fp-56 },
34*412f47f9SXin Li   .shift = V2 (0x1.8p52),
35*412f47f9SXin Li   .exponent_bias = V2 (0x3ff0000000000000),
36*412f47f9SXin Li #if WANT_SIMD_EXCEPT
37*412f47f9SXin Li   /* asuint64(oflow_bound) - asuint64(0x1p-51), shifted left by 1 for abs
38*412f47f9SXin Li      compare.  */
39*412f47f9SXin Li   .thresh = V2 (0x78c56fa6d34b552),
40*412f47f9SXin Li   /* asuint64(0x1p-51) << 1.  */
41*412f47f9SXin Li   .tiny_bound = V2 (0x3cc0000000000000 << 1),
42*412f47f9SXin Li #else
43*412f47f9SXin Li   /* Value above which expm1(x) should overflow. Absolute value of the
44*412f47f9SXin Li      underflow bound is greater than this, so it catches both cases - there is
45*412f47f9SXin Li      a small window where fallbacks are triggered unnecessarily.  */
46*412f47f9SXin Li   .oflow_bound = V2 (0x1.62b7d369a5aa9p+9),
47*412f47f9SXin Li #endif
48*412f47f9SXin Li };
49*412f47f9SXin Li 
50*412f47f9SXin Li static float64x2_t VPCS_ATTR NOINLINE
special_case(float64x2_t x,float64x2_t y,uint64x2_t special)51*412f47f9SXin Li special_case (float64x2_t x, float64x2_t y, uint64x2_t special)
52*412f47f9SXin Li {
53*412f47f9SXin Li   return v_call_f64 (expm1, x, y, special);
54*412f47f9SXin Li }
55*412f47f9SXin Li 
56*412f47f9SXin Li /* Double-precision vector exp(x) - 1 function.
57*412f47f9SXin Li    The maximum error observed error is 2.18 ULP:
58*412f47f9SXin Li    _ZGVnN2v_expm1 (0x1.634ba0c237d7bp-2) got 0x1.a8b9ea8d66e22p-2
59*412f47f9SXin Li 					want 0x1.a8b9ea8d66e2p-2.  */
V_NAME_D1(expm1)60*412f47f9SXin Li float64x2_t VPCS_ATTR V_NAME_D1 (expm1) (float64x2_t x)
61*412f47f9SXin Li {
62*412f47f9SXin Li   const struct data *d = ptr_barrier (&data);
63*412f47f9SXin Li 
64*412f47f9SXin Li   uint64x2_t ix = vreinterpretq_u64_f64 (x);
65*412f47f9SXin Li 
66*412f47f9SXin Li #if WANT_SIMD_EXCEPT
67*412f47f9SXin Li   /* If fp exceptions are to be triggered correctly, fall back to scalar for
68*412f47f9SXin Li      |x| < 2^-51, |x| > oflow_bound, Inf & NaN. Add ix to itself for
69*412f47f9SXin Li      shift-left by 1, and compare with thresh which was left-shifted offline -
70*412f47f9SXin Li      this is effectively an absolute compare.  */
71*412f47f9SXin Li   uint64x2_t special
72*412f47f9SXin Li       = vcgeq_u64 (vsubq_u64 (vaddq_u64 (ix, ix), d->tiny_bound), d->thresh);
73*412f47f9SXin Li   if (unlikely (v_any_u64 (special)))
74*412f47f9SXin Li     x = v_zerofy_f64 (x, special);
75*412f47f9SXin Li #else
76*412f47f9SXin Li   /* Large input, NaNs and Infs.  */
77*412f47f9SXin Li   uint64x2_t special = vcageq_f64 (x, d->oflow_bound);
78*412f47f9SXin Li #endif
79*412f47f9SXin Li 
80*412f47f9SXin Li   /* Reduce argument to smaller range:
81*412f47f9SXin Li      Let i = round(x / ln2)
82*412f47f9SXin Li      and f = x - i * ln2, then f is in [-ln2/2, ln2/2].
83*412f47f9SXin Li      exp(x) - 1 = 2^i * (expm1(f) + 1) - 1
84*412f47f9SXin Li      where 2^i is exact because i is an integer.  */
85*412f47f9SXin Li   float64x2_t n = vsubq_f64 (vfmaq_f64 (d->shift, d->invln2, x), d->shift);
86*412f47f9SXin Li   int64x2_t i = vcvtq_s64_f64 (n);
87*412f47f9SXin Li   float64x2_t ln2 = vld1q_f64 (&d->ln2[0]);
88*412f47f9SXin Li   float64x2_t f = vfmsq_laneq_f64 (x, n, ln2, 0);
89*412f47f9SXin Li   f = vfmsq_laneq_f64 (f, n, ln2, 1);
90*412f47f9SXin Li 
91*412f47f9SXin Li   /* Approximate expm1(f) using polynomial.
92*412f47f9SXin Li      Taylor expansion for expm1(x) has the form:
93*412f47f9SXin Li 	 x + ax^2 + bx^3 + cx^4 ....
94*412f47f9SXin Li      So we calculate the polynomial P(f) = a + bf + cf^2 + ...
95*412f47f9SXin Li      and assemble the approximation expm1(f) ~= f + f^2 * P(f).  */
96*412f47f9SXin Li   float64x2_t f2 = vmulq_f64 (f, f);
97*412f47f9SXin Li   float64x2_t f4 = vmulq_f64 (f2, f2);
98*412f47f9SXin Li   float64x2_t f8 = vmulq_f64 (f4, f4);
99*412f47f9SXin Li   float64x2_t p = vfmaq_f64 (f, f2, v_estrin_10_f64 (f, f2, f4, f8, d->poly));
100*412f47f9SXin Li 
101*412f47f9SXin Li   /* Assemble the result.
102*412f47f9SXin Li      expm1(x) ~= 2^i * (p + 1) - 1
103*412f47f9SXin Li      Let t = 2^i.  */
104*412f47f9SXin Li   int64x2_t u = vaddq_s64 (vshlq_n_s64 (i, 52), d->exponent_bias);
105*412f47f9SXin Li   float64x2_t t = vreinterpretq_f64_s64 (u);
106*412f47f9SXin Li 
107*412f47f9SXin Li   if (unlikely (v_any_u64 (special)))
108*412f47f9SXin Li     return special_case (vreinterpretq_f64_u64 (ix),
109*412f47f9SXin Li 			 vfmaq_f64 (vsubq_f64 (t, v_f64 (1.0)), p, t),
110*412f47f9SXin Li 			 special);
111*412f47f9SXin Li 
112*412f47f9SXin Li   /* expm1(x) ~= p * t + (t - 1).  */
113*412f47f9SXin Li   return vfmaq_f64 (vsubq_f64 (t, v_f64 (1.0)), p, t);
114*412f47f9SXin Li }
115*412f47f9SXin Li 
116*412f47f9SXin Li PL_SIG (V, D, 1, expm1, -9.9, 9.9)
117*412f47f9SXin Li PL_TEST_ULP (V_NAME_D1 (expm1), 1.68)
118*412f47f9SXin Li PL_TEST_EXPECT_FENV (V_NAME_D1 (expm1), WANT_SIMD_EXCEPT)
119*412f47f9SXin Li PL_TEST_SYM_INTERVAL (V_NAME_D1 (expm1), 0, 0x1p-51, 1000)
120*412f47f9SXin Li PL_TEST_SYM_INTERVAL (V_NAME_D1 (expm1), 0x1p-51, 0x1.62b7d369a5aa9p+9, 100000)
121*412f47f9SXin Li PL_TEST_SYM_INTERVAL (V_NAME_D1 (expm1), 0x1.62b7d369a5aa9p+9, inf, 100)
122