1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner * Single-precision vector log(1+x) function.
3*f3087befSAndrew Turner *
4*f3087befSAndrew Turner * Copyright (c) 2022-2024, Arm Limited.
5*f3087befSAndrew Turner * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6*f3087befSAndrew Turner */
7*f3087befSAndrew Turner
8*f3087befSAndrew Turner #include "v_math.h"
9*f3087befSAndrew Turner #include "test_sig.h"
10*f3087befSAndrew Turner #include "test_defs.h"
11*f3087befSAndrew Turner #include "v_log1pf_inline.h"
12*f3087befSAndrew Turner
13*f3087befSAndrew Turner #if WANT_SIMD_EXCEPT
14*f3087befSAndrew Turner
15*f3087befSAndrew Turner const static struct data
16*f3087befSAndrew Turner {
17*f3087befSAndrew Turner uint32x4_t minus_one, thresh;
18*f3087befSAndrew Turner struct v_log1pf_data d;
19*f3087befSAndrew Turner } data = {
20*f3087befSAndrew Turner .d = V_LOG1PF_CONSTANTS_TABLE,
21*f3087befSAndrew Turner .thresh = V4 (0x4b800000), /* asuint32(INFINITY) - TinyBound. */
22*f3087befSAndrew Turner .minus_one = V4 (0xbf800000),
23*f3087befSAndrew Turner };
24*f3087befSAndrew Turner
25*f3087befSAndrew Turner /* asuint32(0x1p-23). ulp=0.5 at 0x1p-23. */
26*f3087befSAndrew Turner # define TinyBound v_u32 (0x34000000)
27*f3087befSAndrew Turner
28*f3087befSAndrew Turner static float32x4_t NOINLINE VPCS_ATTR
special_case(float32x4_t x,uint32x4_t cmp,const struct data * d)29*f3087befSAndrew Turner special_case (float32x4_t x, uint32x4_t cmp, const struct data *d)
30*f3087befSAndrew Turner {
31*f3087befSAndrew Turner /* Side-step special lanes so fenv exceptions are not triggered
32*f3087befSAndrew Turner inadvertently. */
33*f3087befSAndrew Turner float32x4_t x_nospecial = v_zerofy_f32 (x, cmp);
34*f3087befSAndrew Turner return v_call_f32 (log1pf, x, log1pf_inline (x_nospecial, &d->d), cmp);
35*f3087befSAndrew Turner }
36*f3087befSAndrew Turner
37*f3087befSAndrew Turner /* Vector log1pf approximation using polynomial on reduced interval. Worst-case
38*f3087befSAndrew Turner error is 1.69 ULP:
39*f3087befSAndrew Turner _ZGVnN4v_log1pf(0x1.04418ap-2) got 0x1.cfcbd8p-3
40*f3087befSAndrew Turner want 0x1.cfcbdcp-3. */
V_NAME_F1(log1p)41*f3087befSAndrew Turner float32x4_t VPCS_ATTR NOINLINE V_NAME_F1 (log1p) (float32x4_t x)
42*f3087befSAndrew Turner {
43*f3087befSAndrew Turner const struct data *d = ptr_barrier (&data);
44*f3087befSAndrew Turner uint32x4_t ix = vreinterpretq_u32_f32 (x);
45*f3087befSAndrew Turner uint32x4_t ia = vreinterpretq_u32_f32 (vabsq_f32 (x));
46*f3087befSAndrew Turner
47*f3087befSAndrew Turner uint32x4_t special_cases
48*f3087befSAndrew Turner = vorrq_u32 (vcgeq_u32 (vsubq_u32 (ia, TinyBound), d->thresh),
49*f3087befSAndrew Turner vcgeq_u32 (ix, d->minus_one));
50*f3087befSAndrew Turner
51*f3087befSAndrew Turner if (unlikely (v_any_u32 (special_cases)))
52*f3087befSAndrew Turner return special_case (x, special_cases, d);
53*f3087befSAndrew Turner
54*f3087befSAndrew Turner return log1pf_inline (x, &d->d);
55*f3087befSAndrew Turner }
56*f3087befSAndrew Turner
57*f3087befSAndrew Turner #else
58*f3087befSAndrew Turner
59*f3087befSAndrew Turner const static struct v_log1pf_data data = V_LOG1PF_CONSTANTS_TABLE;
60*f3087befSAndrew Turner
61*f3087befSAndrew Turner static float32x4_t NOINLINE VPCS_ATTR
special_case(float32x4_t x,uint32x4_t cmp)62*f3087befSAndrew Turner special_case (float32x4_t x, uint32x4_t cmp)
63*f3087befSAndrew Turner {
64*f3087befSAndrew Turner return v_call_f32 (log1pf, x, log1pf_inline (x, ptr_barrier (&data)), cmp);
65*f3087befSAndrew Turner }
66*f3087befSAndrew Turner
67*f3087befSAndrew Turner /* Vector log1pf approximation using polynomial on reduced interval. Worst-case
68*f3087befSAndrew Turner error is 1.63 ULP:
69*f3087befSAndrew Turner _ZGVnN4v_log1pf(0x1.216d12p-2) got 0x1.fdcb12p-3
70*f3087befSAndrew Turner want 0x1.fdcb16p-3. */
V_NAME_F1(log1p)71*f3087befSAndrew Turner float32x4_t VPCS_ATTR NOINLINE V_NAME_F1 (log1p) (float32x4_t x)
72*f3087befSAndrew Turner {
73*f3087befSAndrew Turner uint32x4_t special_cases = vornq_u32 (vcleq_f32 (x, v_f32 (-1)),
74*f3087befSAndrew Turner vcaleq_f32 (x, v_f32 (0x1p127f)));
75*f3087befSAndrew Turner
76*f3087befSAndrew Turner if (unlikely (v_any_u32 (special_cases)))
77*f3087befSAndrew Turner return special_case (x, special_cases);
78*f3087befSAndrew Turner
79*f3087befSAndrew Turner return log1pf_inline (x, ptr_barrier (&data));
80*f3087befSAndrew Turner }
81*f3087befSAndrew Turner
82*f3087befSAndrew Turner #endif
83*f3087befSAndrew Turner
84*f3087befSAndrew Turner HALF_WIDTH_ALIAS_F1 (log1p)
85*f3087befSAndrew Turner
86*f3087befSAndrew Turner TEST_SIG (V, F, 1, log1p, -0.9, 10.0)
87*f3087befSAndrew Turner TEST_ULP (V_NAME_F1 (log1p), 1.20)
88*f3087befSAndrew Turner TEST_DISABLE_FENV_IF_NOT (V_NAME_F1 (log1p), WANT_SIMD_EXCEPT)
89*f3087befSAndrew Turner TEST_SYM_INTERVAL (V_NAME_F1 (log1p), 0.0, 0x1p-23, 30000)
90*f3087befSAndrew Turner TEST_SYM_INTERVAL (V_NAME_F1 (log1p), 0x1p-23, 1, 50000)
91*f3087befSAndrew Turner TEST_INTERVAL (V_NAME_F1 (log1p), 1, inf, 50000)
92*f3087befSAndrew Turner TEST_INTERVAL (V_NAME_F1 (log1p), -1.0, -inf, 1000)
93