xref: /aosp_15_r20/external/arm-optimized-routines/pl/math/v_expm1f_1u6.c (revision 412f47f9e737e10ed5cc46ec6a8d7fa2264f8a14)
1*412f47f9SXin Li /*
2*412f47f9SXin Li  * Single-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_f32.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   float32x4_t poly[5];
16*412f47f9SXin Li   float invln2_and_ln2[4];
17*412f47f9SXin Li   float32x4_t shift;
18*412f47f9SXin Li   int32x4_t exponent_bias;
19*412f47f9SXin Li #if WANT_SIMD_EXCEPT
20*412f47f9SXin Li   uint32x4_t thresh;
21*412f47f9SXin Li #else
22*412f47f9SXin Li   float32x4_t oflow_bound;
23*412f47f9SXin Li #endif
24*412f47f9SXin Li } data = {
25*412f47f9SXin Li   /* Generated using fpminimax with degree=5 in [-log(2)/2, log(2)/2].  */
26*412f47f9SXin Li   .poly = { V4 (0x1.fffffep-2), V4 (0x1.5554aep-3), V4 (0x1.555736p-5),
27*412f47f9SXin Li 	    V4 (0x1.12287cp-7), V4 (0x1.6b55a2p-10) },
28*412f47f9SXin Li   /* Stores constants: invln2, ln2_hi, ln2_lo, 0.  */
29*412f47f9SXin Li   .invln2_and_ln2 = { 0x1.715476p+0f, 0x1.62e4p-1f, 0x1.7f7d1cp-20f, 0 },
30*412f47f9SXin Li   .shift = V4 (0x1.8p23f),
31*412f47f9SXin Li   .exponent_bias = V4 (0x3f800000),
32*412f47f9SXin Li #if !WANT_SIMD_EXCEPT
33*412f47f9SXin Li   /* Value above which expm1f(x) should overflow. Absolute value of the
34*412f47f9SXin Li      underflow bound is greater than this, so it catches both cases - there is
35*412f47f9SXin Li      a small window where fallbacks are triggered unnecessarily.  */
36*412f47f9SXin Li   .oflow_bound = V4 (0x1.5ebc4p+6),
37*412f47f9SXin Li #else
38*412f47f9SXin Li   /* asuint(oflow_bound) - asuint(0x1p-23), shifted left by 1 for absolute
39*412f47f9SXin Li      compare.  */
40*412f47f9SXin Li   .thresh = V4 (0x1d5ebc40),
41*412f47f9SXin Li #endif
42*412f47f9SXin Li };
43*412f47f9SXin Li 
44*412f47f9SXin Li /* asuint(0x1p-23), shifted by 1 for abs compare.  */
45*412f47f9SXin Li #define TinyBound v_u32 (0x34000000 << 1)
46*412f47f9SXin Li 
47*412f47f9SXin Li static float32x4_t VPCS_ATTR NOINLINE
special_case(float32x4_t x,float32x4_t y,uint32x4_t special)48*412f47f9SXin Li special_case (float32x4_t x, float32x4_t y, uint32x4_t special)
49*412f47f9SXin Li {
50*412f47f9SXin Li   return v_call_f32 (expm1f, x, y, special);
51*412f47f9SXin Li }
52*412f47f9SXin Li 
53*412f47f9SXin Li /* Single-precision vector exp(x) - 1 function.
54*412f47f9SXin Li    The maximum error is 1.51 ULP:
55*412f47f9SXin Li    _ZGVnN4v_expm1f (0x1.8baa96p-2) got 0x1.e2fb9p-2
56*412f47f9SXin Li 				  want 0x1.e2fb94p-2.  */
V_NAME_F1(expm1)57*412f47f9SXin Li float32x4_t VPCS_ATTR V_NAME_F1 (expm1) (float32x4_t x)
58*412f47f9SXin Li {
59*412f47f9SXin Li   const struct data *d = ptr_barrier (&data);
60*412f47f9SXin Li   uint32x4_t ix = vreinterpretq_u32_f32 (x);
61*412f47f9SXin Li 
62*412f47f9SXin Li #if WANT_SIMD_EXCEPT
63*412f47f9SXin Li   /* If fp exceptions are to be triggered correctly, fall back to scalar for
64*412f47f9SXin Li      |x| < 2^-23, |x| > oflow_bound, Inf & NaN. Add ix to itself for
65*412f47f9SXin Li      shift-left by 1, and compare with thresh which was left-shifted offline -
66*412f47f9SXin Li      this is effectively an absolute compare.  */
67*412f47f9SXin Li   uint32x4_t special
68*412f47f9SXin Li       = vcgeq_u32 (vsubq_u32 (vaddq_u32 (ix, ix), TinyBound), d->thresh);
69*412f47f9SXin Li   if (unlikely (v_any_u32 (special)))
70*412f47f9SXin Li     x = v_zerofy_f32 (x, special);
71*412f47f9SXin Li #else
72*412f47f9SXin Li   /* Handles very large values (+ve and -ve), +/-NaN, +/-Inf.  */
73*412f47f9SXin Li   uint32x4_t special = vcagtq_f32 (x, d->oflow_bound);
74*412f47f9SXin Li #endif
75*412f47f9SXin Li 
76*412f47f9SXin Li   /* Reduce argument to smaller range:
77*412f47f9SXin Li      Let i = round(x / ln2)
78*412f47f9SXin Li      and f = x - i * ln2, then f is in [-ln2/2, ln2/2].
79*412f47f9SXin Li      exp(x) - 1 = 2^i * (expm1(f) + 1) - 1
80*412f47f9SXin Li      where 2^i is exact because i is an integer.  */
81*412f47f9SXin Li   float32x4_t invln2_and_ln2 = vld1q_f32 (d->invln2_and_ln2);
82*412f47f9SXin Li   float32x4_t j
83*412f47f9SXin Li       = vsubq_f32 (vfmaq_laneq_f32 (d->shift, x, invln2_and_ln2, 0), d->shift);
84*412f47f9SXin Li   int32x4_t i = vcvtq_s32_f32 (j);
85*412f47f9SXin Li   float32x4_t f = vfmsq_laneq_f32 (x, j, invln2_and_ln2, 1);
86*412f47f9SXin Li   f = vfmsq_laneq_f32 (f, j, invln2_and_ln2, 2);
87*412f47f9SXin Li 
88*412f47f9SXin Li   /* Approximate expm1(f) using polynomial.
89*412f47f9SXin Li      Taylor expansion for expm1(x) has the form:
90*412f47f9SXin Li 	 x + ax^2 + bx^3 + cx^4 ....
91*412f47f9SXin Li      So we calculate the polynomial P(f) = a + bf + cf^2 + ...
92*412f47f9SXin Li      and assemble the approximation expm1(f) ~= f + f^2 * P(f).  */
93*412f47f9SXin Li   float32x4_t p = v_horner_4_f32 (f, d->poly);
94*412f47f9SXin Li   p = vfmaq_f32 (f, vmulq_f32 (f, f), p);
95*412f47f9SXin Li 
96*412f47f9SXin Li   /* Assemble the result.
97*412f47f9SXin Li      expm1(x) ~= 2^i * (p + 1) - 1
98*412f47f9SXin Li      Let t = 2^i.  */
99*412f47f9SXin Li   int32x4_t u = vaddq_s32 (vshlq_n_s32 (i, 23), d->exponent_bias);
100*412f47f9SXin Li   float32x4_t t = vreinterpretq_f32_s32 (u);
101*412f47f9SXin Li 
102*412f47f9SXin Li   if (unlikely (v_any_u32 (special)))
103*412f47f9SXin Li     return special_case (vreinterpretq_f32_u32 (ix),
104*412f47f9SXin Li 			 vfmaq_f32 (vsubq_f32 (t, v_f32 (1.0f)), p, t),
105*412f47f9SXin Li 			 special);
106*412f47f9SXin Li 
107*412f47f9SXin Li   /* expm1(x) ~= p * t + (t - 1).  */
108*412f47f9SXin Li   return vfmaq_f32 (vsubq_f32 (t, v_f32 (1.0f)), p, t);
109*412f47f9SXin Li }
110*412f47f9SXin Li 
111*412f47f9SXin Li PL_SIG (V, F, 1, expm1, -9.9, 9.9)
112*412f47f9SXin Li PL_TEST_ULP (V_NAME_F1 (expm1), 1.02)
113*412f47f9SXin Li PL_TEST_EXPECT_FENV (V_NAME_F1 (expm1), WANT_SIMD_EXCEPT)
114*412f47f9SXin Li PL_TEST_SYM_INTERVAL (V_NAME_F1 (expm1), 0, 0x1p-23, 1000)
115*412f47f9SXin Li PL_TEST_INTERVAL (V_NAME_F1 (expm1), -0x1p-23, 0x1.5ebc4p+6, 1000000)
116*412f47f9SXin Li PL_TEST_INTERVAL (V_NAME_F1 (expm1), -0x1p-23, -0x1.9bbabcp+6, 1000000)
117*412f47f9SXin Li PL_TEST_INTERVAL (V_NAME_F1 (expm1), 0x1.5ebc4p+6, inf, 1000)
118*412f47f9SXin Li PL_TEST_INTERVAL (V_NAME_F1 (expm1), -0x1.9bbabcp+6, -inf, 1000)
119