1*072a4ba8SAndrew Turner /* 2*072a4ba8SAndrew Turner * Single-precision vector log10 function. 3*072a4ba8SAndrew Turner * 4*072a4ba8SAndrew Turner * Copyright (c) 2020-2023, Arm Limited. 5*072a4ba8SAndrew Turner * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception 6*072a4ba8SAndrew Turner */ 7*072a4ba8SAndrew Turner 8*072a4ba8SAndrew Turner #include "v_math.h" 9*072a4ba8SAndrew Turner #include "mathlib.h" 10*072a4ba8SAndrew Turner #include "pl_sig.h" 11*072a4ba8SAndrew Turner #include "pl_test.h" 12*072a4ba8SAndrew Turner 13*072a4ba8SAndrew Turner #if V_SUPPORTED 14*072a4ba8SAndrew Turner 15*072a4ba8SAndrew Turner #define P(i) v_f32 (__v_log10f_poly[i]) 16*072a4ba8SAndrew Turner 17*072a4ba8SAndrew Turner #define Ln2 v_f32 (0x1.62e43p-1f) /* 0x3f317218. */ 18*072a4ba8SAndrew Turner #define InvLn10 v_f32 (0x1.bcb7b2p-2f) 19*072a4ba8SAndrew Turner #define Min v_u32 (0x00800000) 20*072a4ba8SAndrew Turner #define Max v_u32 (0x7f800000) 21*072a4ba8SAndrew Turner #define Mask v_u32 (0x007fffff) 22*072a4ba8SAndrew Turner #define Off v_u32 (0x3f2aaaab) /* 0.666667. */ 23*072a4ba8SAndrew Turner 24*072a4ba8SAndrew Turner VPCS_ATTR 25*072a4ba8SAndrew Turner NOINLINE static v_f32_t 26*072a4ba8SAndrew Turner specialcase (v_f32_t x, v_f32_t y, v_u32_t cmp) 27*072a4ba8SAndrew Turner { 28*072a4ba8SAndrew Turner /* Fall back to scalar code. */ 29*072a4ba8SAndrew Turner return v_call_f32 (log10f, x, y, cmp); 30*072a4ba8SAndrew Turner } 31*072a4ba8SAndrew Turner 32*072a4ba8SAndrew Turner /* Our fast implementation of v_log10f uses a similar approach as v_logf. 33*072a4ba8SAndrew Turner With the same offset as v_logf (i.e., 2/3) it delivers about 3.3ulps with 34*072a4ba8SAndrew Turner order 9. This is more efficient than using a low order polynomial computed in 35*072a4ba8SAndrew Turner double precision. 36*072a4ba8SAndrew Turner Maximum error: 3.305ulps (nearest rounding.) 37*072a4ba8SAndrew Turner __v_log10f(0x1.555c16p+0) got 0x1.ffe2fap-4 38*072a4ba8SAndrew Turner want 0x1.ffe2f4p-4 -0.304916 ulp err 2.80492. */ 39*072a4ba8SAndrew Turner VPCS_ATTR 40*072a4ba8SAndrew Turner v_f32_t V_NAME (log10f) (v_f32_t x) 41*072a4ba8SAndrew Turner { 42*072a4ba8SAndrew Turner v_f32_t n, o, p, q, r, r2, y; 43*072a4ba8SAndrew Turner v_u32_t u, cmp; 44*072a4ba8SAndrew Turner 45*072a4ba8SAndrew Turner u = v_as_u32_f32 (x); 46*072a4ba8SAndrew Turner cmp = v_cond_u32 (u - Min >= Max - Min); 47*072a4ba8SAndrew Turner 48*072a4ba8SAndrew Turner /* x = 2^n * (1+r), where 2/3 < 1+r < 4/3. */ 49*072a4ba8SAndrew Turner u -= Off; 50*072a4ba8SAndrew Turner n = v_to_f32_s32 (v_as_s32_u32 (u) >> 23); /* signextend. */ 51*072a4ba8SAndrew Turner u &= Mask; 52*072a4ba8SAndrew Turner u += Off; 53*072a4ba8SAndrew Turner r = v_as_f32_u32 (u) - v_f32 (1.0f); 54*072a4ba8SAndrew Turner 55*072a4ba8SAndrew Turner /* y = log10(1+r) + n*log10(2). */ 56*072a4ba8SAndrew Turner r2 = r * r; 57*072a4ba8SAndrew Turner /* (n*ln2 + r)*InvLn10 + r2*(P0 + r*P1 + r2*(P2 + r*P3 + r2*(P4 + r*P5 + 58*072a4ba8SAndrew Turner r2*(P6+r*P7))). */ 59*072a4ba8SAndrew Turner o = v_fma_f32 (P (7), r, P (6)); 60*072a4ba8SAndrew Turner p = v_fma_f32 (P (5), r, P (4)); 61*072a4ba8SAndrew Turner q = v_fma_f32 (P (3), r, P (2)); 62*072a4ba8SAndrew Turner y = v_fma_f32 (P (1), r, P (0)); 63*072a4ba8SAndrew Turner p = v_fma_f32 (o, r2, p); 64*072a4ba8SAndrew Turner q = v_fma_f32 (p, r2, q); 65*072a4ba8SAndrew Turner y = v_fma_f32 (q, r2, y); 66*072a4ba8SAndrew Turner /* Using p = Log10(2)*n + r*InvLn(10) is slightly faster 67*072a4ba8SAndrew Turner but less accurate. */ 68*072a4ba8SAndrew Turner p = v_fma_f32 (Ln2, n, r); 69*072a4ba8SAndrew Turner y = v_fma_f32 (y, r2, p * InvLn10); 70*072a4ba8SAndrew Turner 71*072a4ba8SAndrew Turner if (unlikely (v_any_u32 (cmp))) 72*072a4ba8SAndrew Turner return specialcase (x, y, cmp); 73*072a4ba8SAndrew Turner return y; 74*072a4ba8SAndrew Turner } 75*072a4ba8SAndrew Turner VPCS_ALIAS 76*072a4ba8SAndrew Turner 77*072a4ba8SAndrew Turner PL_SIG (V, F, 1, log10, 0.01, 11.1) 78*072a4ba8SAndrew Turner PL_TEST_ULP (V_NAME (log10f), 2.81) 79*072a4ba8SAndrew Turner PL_TEST_EXPECT_FENV_ALWAYS (V_NAME (log10f)) 80*072a4ba8SAndrew Turner PL_TEST_INTERVAL (V_NAME (log10f), 0, 0xffff0000, 10000) 81*072a4ba8SAndrew Turner PL_TEST_INTERVAL (V_NAME (log10f), 0x1p-4, 0x1p4, 500000) 82*072a4ba8SAndrew Turner #endif 83