1 /* 2 * Single-precision vector 2^x function. 3 * 4 * Copyright (c) 2019, Arm Limited. 5 * SPDX-License-Identifier: MIT 6 */ 7 8 #include "mathlib.h" 9 #include "v_math.h" 10 #if V_SUPPORTED 11 12 static const float Poly[] = { 13 /* maxerr: 1.962 ulp. */ 14 0x1.59977ap-10f, 15 0x1.3ce9e4p-7f, 16 0x1.c6bd32p-5f, 17 0x1.ebf9bcp-3f, 18 0x1.62e422p-1f, 19 }; 20 #define C0 v_f32 (Poly[0]) 21 #define C1 v_f32 (Poly[1]) 22 #define C2 v_f32 (Poly[2]) 23 #define C3 v_f32 (Poly[3]) 24 #define C4 v_f32 (Poly[4]) 25 26 #define Shift v_f32 (0x1.8p23f) 27 28 VPCS_ATTR 29 static v_f32_t 30 specialcase (v_f32_t poly, v_f32_t n, v_u32_t e, v_f32_t absn, v_u32_t cmp1, v_f32_t scale) 31 { 32 /* 2^n may overflow, break it up into s1*s2. */ 33 v_u32_t b = v_cond_u32 (n <= v_f32 (0.0f)) & v_u32 (0x82000000); 34 v_f32_t s1 = v_as_f32_u32 (v_u32 (0x7f000000) + b); 35 v_f32_t s2 = v_as_f32_u32 (e - b); 36 v_u32_t cmp2 = v_cond_u32 (absn > v_f32 (192.0f)); 37 v_u32_t r2 = v_as_u32_f32 (s1 * s1); 38 v_u32_t r1 = v_as_u32_f32 (v_fma_f32 (poly, s2, s2) * s1); 39 /* Similar to r1 but avoids double rounding in the subnormal range. */ 40 v_u32_t r0 = v_as_u32_f32 (v_fma_f32 (poly, scale, scale)); 41 return v_as_f32_u32 ((cmp2 & r2) | (~cmp2 & cmp1 & r1) | (~cmp1 & r0)); 42 } 43 44 VPCS_ATTR 45 v_f32_t 46 V_NAME(exp2f) (v_f32_t x) 47 { 48 v_f32_t n, r, r2, scale, p, q, poly, absn; 49 v_u32_t cmp, e; 50 51 /* exp2(x) = 2^n (1 + poly(r)), with 1 + poly(r) in [1/sqrt(2),sqrt(2)] 52 x = n + r, with r in [-1/2, 1/2]. */ 53 #if 0 54 v_f32_t z; 55 z = x + Shift; 56 n = z - Shift; 57 r = x - n; 58 e = v_as_u32_f32 (z) << 23; 59 #else 60 n = v_round_f32 (x); 61 r = x - n; 62 e = v_as_u32_s32 (v_round_s32 (x)) << 23; 63 #endif 64 scale = v_as_f32_u32 (e + v_u32 (0x3f800000)); 65 absn = v_abs_f32 (n); 66 cmp = v_cond_u32 (absn > v_f32 (126.0f)); 67 r2 = r * r; 68 p = v_fma_f32 (C0, r, C1); 69 q = v_fma_f32 (C2, r, C3); 70 q = v_fma_f32 (p, r2, q); 71 p = C4 * r; 72 poly = v_fma_f32 (q, r2, p); 73 if (unlikely (v_any_u32 (cmp))) 74 return specialcase (poly, n, e, absn, cmp, scale); 75 return v_fma_f32 (poly, scale, scale); 76 } 77 VPCS_ALIAS 78 #endif 79