xref: /freebsd/contrib/arm-optimized-routines/math/aarch64/sve/log2f.c (revision dd21556857e8d40f66bf5ad54754d9d52669ebf7)
1 /*
2  * Single-precision vector/SVE log2 function.
3  *
4  * Copyright (c) 2022-2024, Arm Limited.
5  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6  */
7 
8 #include "sv_math.h"
9 #include "test_sig.h"
10 #include "test_defs.h"
11 
12 static const struct data
13 {
14   float poly_02468[5];
15   float poly_1357[4];
16   uint32_t off, lower;
17 } data = {
18   .poly_1357 = {
19     /* Coefficients copied from the AdvSIMD routine, then rearranged so that coeffs
20        1, 3, 5 and 7 can be loaded as a single quad-word, hence used with _lane
21        variant of MLA intrinsic.  */
22     -0x1.715458p-1f, -0x1.7171a4p-2f, -0x1.e5143ep-3f, -0x1.c675bp-3f
23   },
24   .poly_02468 = { 0x1.715476p0f, 0x1.ec701cp-2f, 0x1.27a0b8p-2f,
25 		  0x1.9d8ecap-3f, 0x1.9e495p-3f },
26   .off = 0x3f2aaaab,
27   /* Lower bound is the smallest positive normal float 0x00800000. For
28      optimised register use subnormals are detected after offset has been
29      subtracted, so lower bound is 0x0080000 - offset (which wraps around).  */
30   .lower = 0x00800000 - 0x3f2aaaab
31 };
32 
33 #define Thresh (0x7f000000) /* asuint32(inf) - 0x00800000.  */
34 #define MantissaMask (0x007fffff)
35 
36 static svfloat32_t NOINLINE
37 special_case (svuint32_t u_off, svfloat32_t p, svfloat32_t r2, svfloat32_t y,
38 	      svbool_t cmp)
39 {
40   return sv_call_f32 (
41       log2f, svreinterpret_f32 (svadd_x (svptrue_b32 (), u_off, data.off)),
42       svmla_x (svptrue_b32 (), p, r2, y), cmp);
43 }
44 
45 /* Optimised implementation of SVE log2f, using the same algorithm
46    and polynomial as AdvSIMD log2f.
47    Maximum error is 2.48 ULPs:
48    SV_NAME_F1 (log2)(0x1.558174p+0) got 0x1.a9be84p-2
49 				   want 0x1.a9be8p-2.  */
50 svfloat32_t SV_NAME_F1 (log2) (svfloat32_t x, const svbool_t pg)
51 {
52   const struct data *d = ptr_barrier (&data);
53 
54   svuint32_t u_off = svreinterpret_u32 (x);
55 
56   u_off = svsub_x (pg, u_off, d->off);
57   svbool_t special = svcmpge (pg, svsub_x (pg, u_off, d->lower), Thresh);
58 
59   /* x = 2^n * (1+r), where 2/3 < 1+r < 4/3.  */
60   svfloat32_t n = svcvt_f32_x (
61       pg, svasr_x (pg, svreinterpret_s32 (u_off), 23)); /* Sign-extend.  */
62   svuint32_t u = svand_x (pg, u_off, MantissaMask);
63   u = svadd_x (pg, u, d->off);
64   svfloat32_t r = svsub_x (pg, svreinterpret_f32 (u), 1.0f);
65 
66   /* y = log2(1+r) + n.  */
67   svfloat32_t r2 = svmul_x (svptrue_b32 (), r, r);
68 
69   /* Evaluate polynomial using pairwise Horner scheme.  */
70   svfloat32_t p_1357 = svld1rq (svptrue_b32 (), &d->poly_1357[0]);
71   svfloat32_t q_01 = svmla_lane (sv_f32 (d->poly_02468[0]), r, p_1357, 0);
72   svfloat32_t q_23 = svmla_lane (sv_f32 (d->poly_02468[1]), r, p_1357, 1);
73   svfloat32_t q_45 = svmla_lane (sv_f32 (d->poly_02468[2]), r, p_1357, 2);
74   svfloat32_t q_67 = svmla_lane (sv_f32 (d->poly_02468[3]), r, p_1357, 3);
75   svfloat32_t y = svmla_x (pg, q_67, r2, sv_f32 (d->poly_02468[4]));
76   y = svmla_x (pg, q_45, r2, y);
77   y = svmla_x (pg, q_23, r2, y);
78   y = svmla_x (pg, q_01, r2, y);
79 
80   if (unlikely (svptest_any (pg, special)))
81     return special_case (u_off, n, r, y, special);
82   return svmla_x (svptrue_b32 (), n, r, y);
83 }
84 
85 TEST_SIG (SV, F, 1, log2, 0.01, 11.1)
86 TEST_ULP (SV_NAME_F1 (log2), 1.99)
87 TEST_DISABLE_FENV (SV_NAME_F1 (log2))
88 TEST_INTERVAL (SV_NAME_F1 (log2), -0.0, -0x1p126, 4000)
89 TEST_INTERVAL (SV_NAME_F1 (log2), 0.0, 0x1p-126, 4000)
90 TEST_INTERVAL (SV_NAME_F1 (log2), 0x1p-126, 0x1p-23, 50000)
91 TEST_INTERVAL (SV_NAME_F1 (log2), 0x1p-23, 1.0, 50000)
92 TEST_INTERVAL (SV_NAME_F1 (log2), 1.0, 100, 50000)
93 TEST_INTERVAL (SV_NAME_F1 (log2), 100, inf, 50000)
94 CLOSE_SVE_ATTR
95