xref: /freebsd/contrib/arm-optimized-routines/pl/math/v_log2f_2u5.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 /*
2  * Single-precision vector 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 "v_math.h"
9 #include "pairwise_hornerf.h"
10 #include "pl_sig.h"
11 #include "pl_test.h"
12 #if V_SUPPORTED
13 
14 #define C(i) v_f32 (__v_log2f_data.poly[i])
15 
16 #define Ln2 v_f32 (0x1.62e43p-1f) /* 0x3f317218 */
17 #define Min v_u32 (0x00800000)
18 #define Max v_u32 (0x7f800000)
19 #define Mask v_u32 (0x007fffff)
20 #define Off v_u32 (0x3f2aaaab) /* 0.666667 */
21 
22 VPCS_ATTR
23 NOINLINE static v_f32_t
24 specialcase (v_f32_t x, v_f32_t y, v_u32_t cmp)
25 {
26   /* Fall back to scalar code.  */
27   return v_call_f32 (log2f, x, y, cmp);
28 }
29 
30 /* Fast implementation for single precision log2,
31    relies on same argument reduction as Neon logf.
32    Maximum error: 2.48 ULPs
33    __v_log2f(0x1.558174p+0) got 0x1.a9be84p-2
34 			   want 0x1.a9be8p-2.  */
35 VPCS_ATTR
36 v_f32_t V_NAME (log2f) (v_f32_t x)
37 {
38   v_u32_t u = v_as_u32_f32 (x);
39   v_u32_t cmp = v_cond_u32 (u - Min >= Max - Min);
40 
41   /* x = 2^n * (1+r), where 2/3 < 1+r < 4/3.  */
42   u -= Off;
43   v_f32_t n = v_to_f32_s32 (v_as_s32_u32 (u) >> 23); /* signextend.  */
44   u &= Mask;
45   u += Off;
46   v_f32_t r = v_as_f32_u32 (u) - v_f32 (1.0f);
47 
48   /* y = log2(1+r) + n.  */
49   v_f32_t r2 = r * r;
50   v_f32_t p = PAIRWISE_HORNER_8 (r, r2, C);
51   v_f32_t y = v_fma_f32 (p, r, n);
52 
53   if (unlikely (v_any_u32 (cmp)))
54     return specialcase (x, y, cmp);
55   return y;
56 }
57 VPCS_ALIAS
58 
59 PL_SIG (V, F, 1, log2, 0.01, 11.1)
60 PL_TEST_ULP (V_NAME (log2f), 1.99)
61 PL_TEST_EXPECT_FENV_ALWAYS (V_NAME (log2f))
62 PL_TEST_INTERVAL (V_NAME (log2f), -0.0, -0x1p126, 100)
63 PL_TEST_INTERVAL (V_NAME (log2f), 0x1p-149, 0x1p-126, 4000)
64 PL_TEST_INTERVAL (V_NAME (log2f), 0x1p-126, 0x1p-23, 50000)
65 PL_TEST_INTERVAL (V_NAME (log2f), 0x1p-23, 1.0, 50000)
66 PL_TEST_INTERVAL (V_NAME (log2f), 1.0, 100, 50000)
67 PL_TEST_INTERVAL (V_NAME (log2f), 100, inf, 50000)
68 #endif
69