1 /* 2 * Helper for single-precision routines which calculate exp(x) - 1 and do not 3 * need special-case handling 4 * 5 * Copyright (c) 2022-2024, Arm Limited. 6 * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception 7 */ 8 9 #ifndef MATH_V_EXPM1F_INLINE_H 10 #define MATH_V_EXPM1F_INLINE_H 11 12 #include "v_math.h" 13 14 struct v_expm1f_data 15 { 16 float32x4_t c0, c2; 17 int32x4_t exponent_bias; 18 float c1, c3, inv_ln2, c4; 19 float ln2_hi, ln2_lo; 20 }; 21 22 /* Coefficients generated using fpminimax with degree=5 in [-log(2)/2, 23 log(2)/2]. Exponent bias is asuint(1.0f). */ 24 #define V_EXPM1F_DATA \ 25 { \ 26 .c0 = V4 (0x1.fffffep-2), .c1 = 0x1.5554aep-3, .c2 = V4 (0x1.555736p-5), \ 27 .c3 = 0x1.12287cp-7, .c4 = 0x1.6b55a2p-10, \ 28 .exponent_bias = V4 (0x3f800000), .inv_ln2 = 0x1.715476p+0f, \ 29 .ln2_hi = 0x1.62e4p-1f, .ln2_lo = 0x1.7f7d1cp-20f, \ 30 } 31 32 static inline float32x4_t 33 expm1f_inline (float32x4_t x, const struct v_expm1f_data *d) 34 { 35 /* Helper routine for calculating exp(x) - 1. */ 36 37 float32x2_t ln2 = vld1_f32 (&d->ln2_hi); 38 float32x4_t lane_consts = vld1q_f32 (&d->c1); 39 40 /* Reduce argument: f in [-ln2/2, ln2/2], i is exact. */ 41 float32x4_t j = vrndaq_f32 (vmulq_laneq_f32 (x, lane_consts, 2)); 42 int32x4_t i = vcvtq_s32_f32 (j); 43 float32x4_t f = vfmsq_lane_f32 (x, j, ln2, 0); 44 f = vfmsq_lane_f32 (f, j, ln2, 1); 45 46 /* Approximate expm1(f) with polynomial P, expm1(f) ~= f + f^2 * P(f). */ 47 float32x4_t f2 = vmulq_f32 (f, f); 48 float32x4_t f4 = vmulq_f32 (f2, f2); 49 float32x4_t p01 = vfmaq_laneq_f32 (d->c0, f, lane_consts, 0); 50 float32x4_t p23 = vfmaq_laneq_f32 (d->c2, f, lane_consts, 1); 51 float32x4_t p = vfmaq_f32 (p01, f2, p23); 52 p = vfmaq_laneq_f32 (p, f4, lane_consts, 3); 53 p = vfmaq_f32 (f, f2, p); 54 55 /* t = 2^i. */ 56 int32x4_t u = vaddq_s32 (vshlq_n_s32 (i, 23), d->exponent_bias); 57 float32x4_t t = vreinterpretq_f32_s32 (u); 58 /* expm1(x) ~= p * t + (t - 1). */ 59 return vfmaq_f32 (vsubq_f32 (t, v_f32 (1.0f)), p, t); 60 } 61 62 #endif // MATH_V_EXPM1F_INLINE_H 63