1 /* 2 * Single-precision vector log function. 3 * 4 * Copyright (c) 2019-2023, Arm Limited. 5 * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception 6 */ 7 8 #include "mathlib.h" 9 #include "v_math.h" 10 11 static const struct data 12 { 13 uint32x4_t min_norm; 14 uint16x8_t special_bound; 15 float32x4_t poly[7]; 16 float32x4_t ln2, tiny_bound; 17 uint32x4_t off, mantissa_mask; 18 } data = { 19 /* 3.34 ulp error. */ 20 .poly = { V4 (-0x1.3e737cp-3f), V4 (0x1.5a9aa2p-3f), V4 (-0x1.4f9934p-3f), 21 V4 (0x1.961348p-3f), V4 (-0x1.00187cp-2f), V4 (0x1.555d7cp-2f), 22 V4 (-0x1.ffffc8p-2f) }, 23 .ln2 = V4 (0x1.62e43p-1f), 24 .tiny_bound = V4 (0x1p-126), 25 .min_norm = V4 (0x00800000), 26 .special_bound = V8 (0x7f00), /* asuint32(inf) - min_norm. */ 27 .off = V4 (0x3f2aaaab), /* 0.666667. */ 28 .mantissa_mask = V4 (0x007fffff) 29 }; 30 31 #define P(i) d->poly[7 - i] 32 33 static float32x4_t VPCS_ATTR NOINLINE 34 special_case (float32x4_t x, float32x4_t y, float32x4_t r2, float32x4_t p, 35 uint16x4_t cmp) 36 { 37 /* Fall back to scalar code. */ 38 return v_call_f32 (logf, x, vfmaq_f32 (p, y, r2), vmovl_u16 (cmp)); 39 } 40 41 float32x4_t VPCS_ATTR V_NAME_F1 (log) (float32x4_t x) 42 { 43 const struct data *d = ptr_barrier (&data); 44 float32x4_t n, p, q, r, r2, y; 45 uint32x4_t u; 46 uint16x4_t cmp; 47 48 u = vreinterpretq_u32_f32 (x); 49 cmp = vcge_u16 (vsubhn_u32 (u, d->min_norm), 50 vget_low_u16 (d->special_bound)); 51 52 /* x = 2^n * (1+r), where 2/3 < 1+r < 4/3. */ 53 u = vsubq_u32 (u, d->off); 54 n = vcvtq_f32_s32 ( 55 vshrq_n_s32 (vreinterpretq_s32_u32 (u), 23)); /* signextend. */ 56 u = vandq_u32 (u, d->mantissa_mask); 57 u = vaddq_u32 (u, d->off); 58 r = vsubq_f32 (vreinterpretq_f32_u32 (u), v_f32 (1.0f)); 59 60 /* y = log(1+r) + n*ln2. */ 61 r2 = vmulq_f32 (r, r); 62 /* n*ln2 + r + r2*(P1 + r*P2 + r2*(P3 + r*P4 + r2*(P5 + r*P6 + r2*P7))). */ 63 p = vfmaq_f32 (P (5), P (6), r); 64 q = vfmaq_f32 (P (3), P (4), r); 65 y = vfmaq_f32 (P (1), P (2), r); 66 p = vfmaq_f32 (p, P (7), r2); 67 q = vfmaq_f32 (q, p, r2); 68 y = vfmaq_f32 (y, q, r2); 69 p = vfmaq_f32 (r, d->ln2, n); 70 71 if (unlikely (v_any_u16h (cmp))) 72 return special_case (x, y, r2, p, cmp); 73 return vfmaq_f32 (p, y, r2); 74 } 75