1 /* 2 * SVE helper for single-precision routines which calculate exp(x) and do 3 * not need special-case handling 4 * 5 * Copyright (c) 2023-2025, Arm Limited. 6 * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception 7 */ 8 9 #ifndef MATH_SV_EXPF_INLINE_H 10 #define MATH_SV_EXPF_INLINE_H 11 12 #include "sv_math.h" 13 #include "test_sig.h" 14 #include "test_defs.h" 15 16 struct sv_expf_data 17 { 18 float c1, c3, inv_ln2; 19 float ln2_lo, c0, c2, c4; 20 float ln2_hi, shift; 21 }; 22 23 /* Coefficients copied from the polynomial in AdvSIMD variant, reversed for 24 compatibility with polynomial helpers. Shift is 1.5*2^17 + 127. */ 25 #define SV_EXPF_DATA \ 26 { \ 27 /* Coefficients copied from the polynomial in AdvSIMD variant. */ \ 28 .c0 = 0x1.ffffecp-1f, .c1 = 0x1.fffdb6p-2f, .c2 = 0x1.555e66p-3f, \ 29 .c3 = 0x1.573e2ep-5f, .c4 = 0x1.0e4020p-7f, .inv_ln2 = 0x1.715476p+0f, \ 30 .ln2_hi = 0x1.62e4p-1f, .ln2_lo = 0x1.7f7d1cp-20f, \ 31 .shift = 0x1.803f8p17f, \ 32 } 33 34 #define C(i) sv_f32 (d->poly[i]) 35 36 static inline svfloat32_t 37 expf_inline (svfloat32_t x, const svbool_t pg, const struct sv_expf_data *d) 38 { 39 /* exp(x) = 2^n (1 + poly(r)), with 1 + poly(r) in [1/sqrt(2),sqrt(2)] 40 x = ln2*n + r, with r in [-ln2/2, ln2/2]. */ 41 42 svfloat32_t lane_consts = svld1rq (svptrue_b32 (), &d->ln2_lo); 43 44 /* n = round(x/(ln2/N)). */ 45 svfloat32_t z = svmad_x (pg, sv_f32 (d->inv_ln2), x, d->shift); 46 svfloat32_t n = svsub_x (pg, z, d->shift); 47 48 /* r = x - n*ln2/N. */ 49 svfloat32_t r = svmsb_x (pg, sv_f32 (d->ln2_hi), n, x); 50 r = svmls_lane (r, n, lane_consts, 0); 51 52 /* scale = 2^(n/N). */ 53 svfloat32_t scale = svexpa (svreinterpret_u32 (z)); 54 55 /* poly(r) = exp(r) - 1 ~= C0 r + C1 r^2 + C2 r^3 + C3 r^4 + C4 r^5. */ 56 svfloat32_t p12 = svmla_lane (sv_f32 (d->c1), r, lane_consts, 2); 57 svfloat32_t p34 = svmla_lane (sv_f32 (d->c3), r, lane_consts, 3); 58 svfloat32_t r2 = svmul_x (svptrue_b32 (), r, r); 59 svfloat32_t p14 = svmla_x (pg, p12, p34, r2); 60 svfloat32_t p0 = svmul_lane (r, lane_consts, 1); 61 svfloat32_t poly = svmla_x (pg, p0, r2, p14); 62 63 return svmla_x (pg, scale, scale, poly); 64 } 65 66 #endif // MATH_SV_EXPF_INLINE_H 67