1 /*
2  * Single-precision vector asinh(x) 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 "include/mathlib.h"
10 #include "pl_sig.h"
11 #include "pl_test.h"
12 
13 #if V_SUPPORTED
14 
15 #define SignMask v_u32 (0x80000000)
16 #define One v_f32 (1.0f)
17 #define BigBound v_u32 (0x5f800000)  /* asuint(0x1p64).  */
18 #define TinyBound v_u32 (0x30800000) /* asuint(0x1p-30).  */
19 
20 #include "v_log1pf_inline.h"
21 
22 static NOINLINE v_f32_t
23 specialcase (v_f32_t x, v_f32_t y, v_u32_t special)
24 {
25   return v_call_f32 (asinhf, x, y, special);
26 }
27 
28 /* Single-precision implementation of vector asinh(x), using vector log1p.
29    Worst-case error is 2.66 ULP, at roughly +/-0.25:
30    __v_asinhf(0x1.01b04p-2) got 0x1.fe163ep-3 want 0x1.fe1638p-3.  */
31 VPCS_ATTR v_f32_t V_NAME (asinhf) (v_f32_t x)
32 {
33   v_u32_t ix = v_as_u32_f32 (x);
34   v_u32_t iax = ix & ~SignMask;
35   v_u32_t sign = ix & SignMask;
36   v_f32_t ax = v_as_f32_u32 (iax);
37   v_u32_t special = v_cond_u32 (iax >= BigBound);
38 
39 #if WANT_SIMD_EXCEPT
40   /* Sidestep tiny and large values to avoid inadvertently triggering
41      under/overflow.  */
42   special |= v_cond_u32 (iax < TinyBound);
43   if (unlikely (v_any_u32 (special)))
44     ax = v_sel_f32 (special, One, ax);
45 #endif
46 
47   /* asinh(x) = log(x + sqrt(x * x + 1)).
48      For positive x, asinh(x) = log1p(x + x * x / (1 + sqrt(x * x + 1))).  */
49   v_f32_t d = One + v_sqrt_f32 (ax * ax + One);
50   v_f32_t y = log1pf_inline (ax + ax * ax / d);
51   y = v_as_f32_u32 (sign | v_as_u32_f32 (y));
52 
53   if (unlikely (v_any_u32 (special)))
54     return specialcase (x, y, special);
55   return y;
56 }
57 VPCS_ALIAS
58 
59 PL_SIG (V, F, 1, asinh, -10.0, 10.0)
60 PL_TEST_ULP (V_NAME (asinhf), 2.17)
61 PL_TEST_EXPECT_FENV (V_NAME (asinhf), WANT_SIMD_EXCEPT)
62 PL_TEST_INTERVAL (V_NAME (asinhf), 0, 0x1p-12, 40000)
63 PL_TEST_INTERVAL (V_NAME (asinhf), 0x1p-12, 1.0, 40000)
64 PL_TEST_INTERVAL (V_NAME (asinhf), 1.0, 0x1p11, 40000)
65 PL_TEST_INTERVAL (V_NAME (asinhf), 0x1p11, inf, 40000)
66 PL_TEST_INTERVAL (V_NAME (asinhf), 0, -0x1p-12, 20000)
67 PL_TEST_INTERVAL (V_NAME (asinhf), -0x1p-12, -1.0, 20000)
68 PL_TEST_INTERVAL (V_NAME (asinhf), -1.0, -0x1p11, 20000)
69 PL_TEST_INTERVAL (V_NAME (asinhf), -0x1p11, -inf, 20000)
70 #endif
71