1 /* 2 * Single-precision vector/SVE log2 function. 3 * 4 * Copyright (c) 2022-2023, Arm Limited. 5 * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception 6 */ 7 8 #include "sv_math.h" 9 #include "pl_sig.h" 10 #include "pl_test.h" 11 12 static const struct data 13 { 14 float poly_02468[5]; 15 float poly_1357[4]; 16 } data = { 17 .poly_1357 = { 18 /* Coefficients copied from the AdvSIMD routine, then rearranged so that coeffs 19 1, 3, 5 and 7 can be loaded as a single quad-word, hence used with _lane 20 variant of MLA intrinsic. */ 21 -0x1.715458p-1f, -0x1.7171a4p-2f, -0x1.e5143ep-3f, -0x1.c675bp-3f 22 }, 23 .poly_02468 = { 0x1.715476p0f, 0x1.ec701cp-2f, 0x1.27a0b8p-2f, 24 0x1.9d8ecap-3f, 0x1.9e495p-3f }, 25 }; 26 27 #define Min (0x00800000) 28 #define Max (0x7f800000) 29 #define Thres (0x7f000000) /* Max - Min. */ 30 #define MantissaMask (0x007fffff) 31 #define Off (0x3f2aaaab) /* 0.666667. */ 32 33 static svfloat32_t NOINLINE 34 special_case (svfloat32_t x, svfloat32_t y, svbool_t cmp) 35 { 36 return sv_call_f32 (log2f, x, y, cmp); 37 } 38 39 /* Optimised implementation of SVE log2f, using the same algorithm 40 and polynomial as AdvSIMD log2f. 41 Maximum error is 2.48 ULPs: 42 SV_NAME_F1 (log2)(0x1.558174p+0) got 0x1.a9be84p-2 43 want 0x1.a9be8p-2. */ 44 svfloat32_t SV_NAME_F1 (log2) (svfloat32_t x, const svbool_t pg) 45 { 46 const struct data *d = ptr_barrier (&data); 47 48 svuint32_t u = svreinterpret_u32 (x); 49 svbool_t special = svcmpge (pg, svsub_x (pg, u, Min), Thres); 50 51 /* x = 2^n * (1+r), where 2/3 < 1+r < 4/3. */ 52 u = svsub_x (pg, u, Off); 53 svfloat32_t n = svcvt_f32_x ( 54 pg, svasr_x (pg, svreinterpret_s32 (u), 23)); /* Sign-extend. */ 55 u = svand_x (pg, u, MantissaMask); 56 u = svadd_x (pg, u, Off); 57 svfloat32_t r = svsub_x (pg, svreinterpret_f32 (u), 1.0f); 58 59 /* y = log2(1+r) + n. */ 60 svfloat32_t r2 = svmul_x (pg, r, r); 61 62 /* Evaluate polynomial using pairwise Horner scheme. */ 63 svfloat32_t p_1357 = svld1rq (svptrue_b32 (), &d->poly_1357[0]); 64 svfloat32_t q_01 = svmla_lane (sv_f32 (d->poly_02468[0]), r, p_1357, 0); 65 svfloat32_t q_23 = svmla_lane (sv_f32 (d->poly_02468[1]), r, p_1357, 1); 66 svfloat32_t q_45 = svmla_lane (sv_f32 (d->poly_02468[2]), r, p_1357, 2); 67 svfloat32_t q_67 = svmla_lane (sv_f32 (d->poly_02468[3]), r, p_1357, 3); 68 svfloat32_t y = svmla_x (pg, q_67, r2, sv_f32 (d->poly_02468[4])); 69 y = svmla_x (pg, q_45, r2, y); 70 y = svmla_x (pg, q_23, r2, y); 71 y = svmla_x (pg, q_01, r2, y); 72 73 if (unlikely (svptest_any (pg, special))) 74 return special_case (x, svmla_x (svnot_z (pg, special), n, r, y), special); 75 return svmla_x (pg, n, r, y); 76 } 77 78 PL_SIG (SV, F, 1, log2, 0.01, 11.1) 79 PL_TEST_ULP (SV_NAME_F1 (log2), 1.99) 80 PL_TEST_EXPECT_FENV_ALWAYS (SV_NAME_F1 (log2)) 81 PL_TEST_INTERVAL (SV_NAME_F1 (log2), -0.0, -0x1p126, 4000) 82 PL_TEST_INTERVAL (SV_NAME_F1 (log2), 0.0, 0x1p-126, 4000) 83 PL_TEST_INTERVAL (SV_NAME_F1 (log2), 0x1p-126, 0x1p-23, 50000) 84 PL_TEST_INTERVAL (SV_NAME_F1 (log2), 0x1p-23, 1.0, 50000) 85 PL_TEST_INTERVAL (SV_NAME_F1 (log2), 1.0, 100, 50000) 86 PL_TEST_INTERVAL (SV_NAME_F1 (log2), 100, inf, 50000) 87