xref: /aosp_15_r20/external/arm-optimized-routines/pl/math/v_asinhf_2u7.c (revision 412f47f9e737e10ed5cc46ec6a8d7fa2264f8a14)
1*412f47f9SXin Li /*
2*412f47f9SXin Li  * Single-precision vector asinh(x) function.
3*412f47f9SXin Li  *
4*412f47f9SXin Li  * Copyright (c) 2022-2023, Arm Limited.
5*412f47f9SXin Li  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6*412f47f9SXin Li  */
7*412f47f9SXin Li 
8*412f47f9SXin Li #include "v_math.h"
9*412f47f9SXin Li #include "pl_sig.h"
10*412f47f9SXin Li #include "pl_test.h"
11*412f47f9SXin Li #include "v_log1pf_inline.h"
12*412f47f9SXin Li 
13*412f47f9SXin Li #define SignMask v_u32 (0x80000000)
14*412f47f9SXin Li 
15*412f47f9SXin Li const static struct data
16*412f47f9SXin Li {
17*412f47f9SXin Li   struct v_log1pf_data log1pf_consts;
18*412f47f9SXin Li   uint32x4_t big_bound;
19*412f47f9SXin Li #if WANT_SIMD_EXCEPT
20*412f47f9SXin Li   uint32x4_t tiny_bound;
21*412f47f9SXin Li #endif
22*412f47f9SXin Li } data = {
23*412f47f9SXin Li   .log1pf_consts = V_LOG1PF_CONSTANTS_TABLE,
24*412f47f9SXin Li   .big_bound = V4 (0x5f800000), /* asuint(0x1p64).  */
25*412f47f9SXin Li #if WANT_SIMD_EXCEPT
26*412f47f9SXin Li   .tiny_bound = V4 (0x30800000) /* asuint(0x1p-30).  */
27*412f47f9SXin Li #endif
28*412f47f9SXin Li };
29*412f47f9SXin Li 
30*412f47f9SXin Li static float32x4_t NOINLINE VPCS_ATTR
special_case(float32x4_t x,float32x4_t y,uint32x4_t special)31*412f47f9SXin Li special_case (float32x4_t x, float32x4_t y, uint32x4_t special)
32*412f47f9SXin Li {
33*412f47f9SXin Li   return v_call_f32 (asinhf, x, y, special);
34*412f47f9SXin Li }
35*412f47f9SXin Li 
36*412f47f9SXin Li /* Single-precision implementation of vector asinh(x), using vector log1p.
37*412f47f9SXin Li    Worst-case error is 2.66 ULP, at roughly +/-0.25:
38*412f47f9SXin Li    __v_asinhf(0x1.01b04p-2) got 0x1.fe163ep-3 want 0x1.fe1638p-3.  */
V_NAME_F1(asinh)39*412f47f9SXin Li VPCS_ATTR float32x4_t V_NAME_F1 (asinh) (float32x4_t x)
40*412f47f9SXin Li {
41*412f47f9SXin Li   const struct data *dat = ptr_barrier (&data);
42*412f47f9SXin Li   uint32x4_t iax = vbicq_u32 (vreinterpretq_u32_f32 (x), SignMask);
43*412f47f9SXin Li   float32x4_t ax = vreinterpretq_f32_u32 (iax);
44*412f47f9SXin Li   uint32x4_t special = vcgeq_u32 (iax, dat->big_bound);
45*412f47f9SXin Li   float32x4_t special_arg = x;
46*412f47f9SXin Li 
47*412f47f9SXin Li #if WANT_SIMD_EXCEPT
48*412f47f9SXin Li   /* Sidestep tiny and large values to avoid inadvertently triggering
49*412f47f9SXin Li      under/overflow.  */
50*412f47f9SXin Li   special = vorrq_u32 (special, vcltq_u32 (iax, dat->tiny_bound));
51*412f47f9SXin Li   if (unlikely (v_any_u32 (special)))
52*412f47f9SXin Li     {
53*412f47f9SXin Li       ax = v_zerofy_f32 (ax, special);
54*412f47f9SXin Li       x = v_zerofy_f32 (x, special);
55*412f47f9SXin Li     }
56*412f47f9SXin Li #endif
57*412f47f9SXin Li 
58*412f47f9SXin Li   /* asinh(x) = log(x + sqrt(x * x + 1)).
59*412f47f9SXin Li      For positive x, asinh(x) = log1p(x + x * x / (1 + sqrt(x * x + 1))).  */
60*412f47f9SXin Li   float32x4_t d
61*412f47f9SXin Li       = vaddq_f32 (v_f32 (1), vsqrtq_f32 (vfmaq_f32 (v_f32 (1), x, x)));
62*412f47f9SXin Li   float32x4_t y = log1pf_inline (
63*412f47f9SXin Li       vaddq_f32 (ax, vdivq_f32 (vmulq_f32 (ax, ax), d)), dat->log1pf_consts);
64*412f47f9SXin Li 
65*412f47f9SXin Li   if (unlikely (v_any_u32 (special)))
66*412f47f9SXin Li     return special_case (special_arg, vbslq_f32 (SignMask, x, y), special);
67*412f47f9SXin Li   return vbslq_f32 (SignMask, x, y);
68*412f47f9SXin Li }
69*412f47f9SXin Li 
70*412f47f9SXin Li PL_SIG (V, F, 1, asinh, -10.0, 10.0)
71*412f47f9SXin Li PL_TEST_ULP (V_NAME_F1 (asinh), 2.17)
72*412f47f9SXin Li PL_TEST_EXPECT_FENV (V_NAME_F1 (asinh), WANT_SIMD_EXCEPT)
73*412f47f9SXin Li PL_TEST_INTERVAL (V_NAME_F1 (asinh), 0, 0x1p-12, 40000)
74*412f47f9SXin Li PL_TEST_INTERVAL (V_NAME_F1 (asinh), 0x1p-12, 1.0, 40000)
75*412f47f9SXin Li PL_TEST_INTERVAL (V_NAME_F1 (asinh), 1.0, 0x1p11, 40000)
76*412f47f9SXin Li PL_TEST_INTERVAL (V_NAME_F1 (asinh), 0x1p11, inf, 40000)
77*412f47f9SXin Li PL_TEST_INTERVAL (V_NAME_F1 (asinh), -0, -0x1p-12, 20000)
78*412f47f9SXin Li PL_TEST_INTERVAL (V_NAME_F1 (asinh), -0x1p-12, -1.0, 20000)
79*412f47f9SXin Li PL_TEST_INTERVAL (V_NAME_F1 (asinh), -1.0, -0x1p11, 20000)
80*412f47f9SXin Li PL_TEST_INTERVAL (V_NAME_F1 (asinh), -0x1p11, -inf, 20000)
81