1 /* 2 * Helper for single-precision routines which calculate exp(x) and do not 3 * need special-case handling 4 * 5 * Copyright (c) 2019-2023, Arm Limited. 6 * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception 7 */ 8 9 #ifndef PL_MATH_V_EXPF_INLINE_H 10 #define PL_MATH_V_EXPF_INLINE_H 11 12 #include "v_math.h" 13 14 struct v_expf_data 15 { 16 float32x4_t poly[5]; 17 float32x4_t shift, invln2_and_ln2; 18 }; 19 20 /* maxerr: 1.45358 +0.5 ulp. */ 21 #define V_EXPF_DATA \ 22 { \ 23 .poly = { V4 (0x1.0e4020p-7f), V4 (0x1.573e2ep-5f), V4 (0x1.555e66p-3f), \ 24 V4 (0x1.fffdb6p-2f), V4 (0x1.ffffecp-1f) }, \ 25 .shift = V4 (0x1.8p23f), \ 26 .invln2_and_ln2 = { 0x1.715476p+0f, 0x1.62e4p-1f, 0x1.7f7d1cp-20f, 0 }, \ 27 } 28 29 #define ExponentBias v_u32 (0x3f800000) /* asuint(1.0f). */ 30 #define C(i) d->poly[i] 31 32 static inline float32x4_t 33 v_expf_inline (float32x4_t x, const struct v_expf_data *d) 34 { 35 /* Helper routine for calculating exp(x). 36 Copied from v_expf.c, with all special-case handling removed - the 37 calling routine should handle special values if required. */ 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 float32x4_t n, r, z; 42 z = vfmaq_laneq_f32 (d->shift, x, d->invln2_and_ln2, 0); 43 n = vsubq_f32 (z, d->shift); 44 r = vfmsq_laneq_f32 (x, n, d->invln2_and_ln2, 1); 45 r = vfmsq_laneq_f32 (r, n, d->invln2_and_ln2, 2); 46 uint32x4_t e = vshlq_n_u32 (vreinterpretq_u32_f32 (z), 23); 47 float32x4_t scale = vreinterpretq_f32_u32 (vaddq_u32 (e, ExponentBias)); 48 49 /* Custom order-4 Estrin avoids building high order monomial. */ 50 float32x4_t r2 = vmulq_f32 (r, r); 51 float32x4_t p, q, poly; 52 p = vfmaq_f32 (C (1), C (0), r); 53 q = vfmaq_f32 (C (3), C (2), r); 54 q = vfmaq_f32 (q, p, r2); 55 p = vmulq_f32 (C (4), r); 56 poly = vfmaq_f32 (p, q, r2); 57 return vfmaq_f32 (scale, poly, scale); 58 } 59 60 #endif // PL_MATH_V_EXPF_INLINE_H 61