• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #if SV_SUPPORTED
13 
14 #define P(i) __v_log2f_data.poly[i]
15 
16 #define Ln2 (0x1.62e43p-1f) /* 0x3f317218.  */
17 #define Min (0x00800000)
18 #define Max (0x7f800000)
19 #define Mask (0x007fffff)
20 #define Off (0x3f2aaaab) /* 0.666667.  */
21 
22 static NOINLINE sv_f32_t
specialcase(sv_f32_t x,sv_f32_t y,svbool_t cmp)23 specialcase (sv_f32_t x, sv_f32_t y, svbool_t cmp)
24 {
25   return sv_call_f32 (log2f, x, y, cmp);
26 }
27 
28 /* Optimised implementation of SVE log2f, using the same algorithm
29    and polynomial as Neon log2f. Maximum error is 2.48 ULPs:
30    __sv_log2f(0x1.558174p+0) got 0x1.a9be84p-2
31 			    want 0x1.a9be8p-2.  */
32 sv_f32_t
__sv_log2f_x(sv_f32_t x,const svbool_t pg)33 __sv_log2f_x (sv_f32_t x, const svbool_t pg)
34 {
35   sv_u32_t u = sv_as_u32_f32 (x);
36   svbool_t special
37     = svcmpge_u32 (pg, svsub_n_u32_x (pg, u, Min), sv_u32 (Max - Min));
38 
39   /* x = 2^n * (1+r), where 2/3 < 1+r < 4/3.  */
40   u = svsub_n_u32_x (pg, u, Off);
41   sv_f32_t n = sv_to_f32_s32_x (pg, svasr_n_s32_x (pg, sv_as_s32_u32 (u),
42 						   23)); /* Sign-extend.  */
43   u = svand_n_u32_x (pg, u, Mask);
44   u = svadd_n_u32_x (pg, u, Off);
45   sv_f32_t r = svsub_n_f32_x (pg, sv_as_f32_u32 (u), 1.0f);
46 
47   /* y = log2(1+r) + n.  */
48   sv_f32_t r2 = svmul_f32_x (pg, r, r);
49 
50   /* Evaluate polynomial using pairwise Horner scheme.  */
51   sv_f32_t p67 = sv_fma_n_f32_x (pg, P (7), r, sv_f32 (P (6)));
52   sv_f32_t p45 = sv_fma_n_f32_x (pg, P (5), r, sv_f32 (P (4)));
53   sv_f32_t p23 = sv_fma_n_f32_x (pg, P (3), r, sv_f32 (P (2)));
54   sv_f32_t p01 = sv_fma_n_f32_x (pg, P (1), r, sv_f32 (P (0)));
55   sv_f32_t y;
56   y = sv_fma_n_f32_x (pg, P (8), r2, p67);
57   y = sv_fma_f32_x (pg, y, r2, p45);
58   y = sv_fma_f32_x (pg, y, r2, p23);
59   y = sv_fma_f32_x (pg, y, r2, p01);
60   y = sv_fma_f32_x (pg, y, r, n);
61 
62   if (unlikely (svptest_any (pg, special)))
63     return specialcase (x, y, special);
64   return y;
65 }
66 
67 PL_ALIAS (__sv_log2f_x, _ZGVsMxv_log2f)
68 
69 PL_SIG (SV, F, 1, log2, 0.01, 11.1)
70 PL_TEST_ULP (__sv_log2f, 1.99)
71 PL_TEST_EXPECT_FENV_ALWAYS (__sv_log2f)
72 PL_TEST_INTERVAL (__sv_log2f, -0.0, -0x1p126, 4000)
73 PL_TEST_INTERVAL (__sv_log2f, 0.0, 0x1p-126, 4000)
74 PL_TEST_INTERVAL (__sv_log2f, 0x1p-126, 0x1p-23, 50000)
75 PL_TEST_INTERVAL (__sv_log2f, 0x1p-23, 1.0, 50000)
76 PL_TEST_INTERVAL (__sv_log2f, 1.0, 100, 50000)
77 PL_TEST_INTERVAL (__sv_log2f, 100, inf, 50000)
78 
79 #endif // SV_SUPPORTED
80