1 /*
2 * Single-precision vector exp(x) - 1 function.
3 *
4 * Copyright (c) 2022-2023, Arm Limited.
5 * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6 */
7
8 #include "v_math.h"
9 #include "pl_sig.h"
10 #include "pl_test.h"
11
12 #if V_SUPPORTED
13
14 #define Shift v_f32 (0x1.8p23f)
15 #define InvLn2 v_f32 (0x1.715476p+0f)
16 #define MLn2hi v_f32 (-0x1.62e4p-1f)
17 #define MLn2lo v_f32 (-0x1.7f7d1cp-20f)
18 #define AbsMask (0x7fffffff)
19 #define One (0x3f800000)
20 #define SpecialBound \
21 (0x42af5e20) /* asuint(0x1.5ebc4p+6). Largest value of x for which expm1(x) \
22 should round to -1. */
23 #define TinyBound (0x34000000) /* asuint(0x1p-23). */
24
25 #define C(i) v_f32 (__expm1f_poly[i])
26
27 /* Single-precision vector exp(x) - 1 function.
28 The maximum error is 1.51 ULP:
29 expm1f(0x1.8baa96p-2) got 0x1.e2fb9p-2
30 want 0x1.e2fb94p-2. */
31 VPCS_ATTR
V_NAME(expm1f)32 v_f32_t V_NAME (expm1f) (v_f32_t x)
33 {
34 v_u32_t ix = v_as_u32_f32 (x);
35 v_u32_t ax = ix & AbsMask;
36
37 #if WANT_SIMD_EXCEPT
38 /* If fp exceptions are to be triggered correctly, fall back to the scalar
39 variant for all lanes if any of them should trigger an exception. */
40 v_u32_t special
41 = v_cond_u32 ((ax >= SpecialBound) | (ix == 0x80000000) | (ax < TinyBound));
42 if (unlikely (v_any_u32 (special)))
43 return v_call_f32 (expm1f, x, x, v_u32 (0xffffffff));
44 #else
45 /* Handles very large values (+ve and -ve), +/-NaN, +/-Inf and -0. */
46 v_u32_t special = v_cond_u32 ((ax >= SpecialBound) | (ix == 0x80000000));
47 #endif
48
49 /* Reduce argument to smaller range:
50 Let i = round(x / ln2)
51 and f = x - i * ln2, then f is in [-ln2/2, ln2/2].
52 exp(x) - 1 = 2^i * (expm1(f) + 1) - 1
53 where 2^i is exact because i is an integer. */
54 v_f32_t j = v_fma_f32 (InvLn2, x, Shift) - Shift;
55 v_s32_t i = v_to_s32_f32 (j);
56 v_f32_t f = v_fma_f32 (j, MLn2hi, x);
57 f = v_fma_f32 (j, MLn2lo, f);
58
59 /* Approximate expm1(f) using polynomial.
60 Taylor expansion for expm1(x) has the form:
61 x + ax^2 + bx^3 + cx^4 ....
62 So we calculate the polynomial P(f) = a + bf + cf^2 + ...
63 and assemble the approximation expm1(f) ~= f + f^2 * P(f). */
64
65 v_f32_t p = v_fma_f32 (C (4), f, C (3));
66 p = v_fma_f32 (p, f, C (2));
67 p = v_fma_f32 (p, f, C (1));
68 p = v_fma_f32 (p, f, C (0));
69 p = v_fma_f32 (f * f, p, f);
70
71 /* Assemble the result.
72 expm1(x) ~= 2^i * (p + 1) - 1
73 Let t = 2^i. */
74 v_f32_t t = v_as_f32_u32 (v_as_u32_s32 (i << 23) + One);
75 /* expm1(x) ~= p * t + (t - 1). */
76 v_f32_t y = v_fma_f32 (p, t, t - 1);
77
78 #if !WANT_SIMD_EXCEPT
79 if (unlikely (v_any_u32 (special)))
80 return v_call_f32 (expm1f, x, y, special);
81 #endif
82
83 return y;
84 }
85 VPCS_ALIAS
86
87 PL_SIG (V, F, 1, expm1, -9.9, 9.9)
88 PL_TEST_ULP (V_NAME (expm1f), 1.02)
89 PL_TEST_EXPECT_FENV (V_NAME (expm1f), WANT_SIMD_EXCEPT)
90 PL_TEST_INTERVAL (V_NAME (expm1f), 0, 0x1p-23, 1000)
91 PL_TEST_INTERVAL (V_NAME (expm1f), -0, -0x1p-23, 1000)
92 PL_TEST_INTERVAL (V_NAME (expm1f), 0x1p-23, 0x1.644716p6, 1000000)
93 PL_TEST_INTERVAL (V_NAME (expm1f), -0x1p-23, -0x1.9bbabcp+6, 1000000)
94 #endif
95