1 /*
2  * Single-precision SVE asinh(x) function.
3  *
4  * Copyright (c) 2023, Arm Limited.
5  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6  */
7 
8 #include "sv_math.h"
9 #include "include/mathlib.h"
10 #include "pl_sig.h"
11 #include "pl_test.h"
12 
13 #include "sv_log1pf_inline.h"
14 
15 #define BigBound (0x5f800000)  /* asuint(0x1p64).  */
16 
17 static svfloat32_t NOINLINE
18 special_case (svfloat32_t x, svfloat32_t y, svbool_t special)
19 {
20   return sv_call_f32 (asinhf, x, y, special);
21 }
22 
23 /* Single-precision SVE asinh(x) routine. Implements the same algorithm as
24    vector asinhf and log1p.
25 
26    Maximum error is 2.48 ULPs:
27    SV_NAME_F1 (asinh) (0x1.008864p-3) got 0x1.ffbbbcp-4
28 				     want 0x1.ffbbb8p-4.  */
29 svfloat32_t SV_NAME_F1 (asinh) (svfloat32_t x, const svbool_t pg)
30 {
31   svfloat32_t ax = svabs_x (pg, x);
32   svuint32_t iax = svreinterpret_u32 (ax);
33   svuint32_t sign = sveor_x (pg, svreinterpret_u32 (x), iax);
34   svbool_t special = svcmpge (pg, iax, BigBound);
35 
36   /* asinh(x) = log(x + sqrt(x * x + 1)).
37      For positive x, asinh(x) = log1p(x + x * x / (1 + sqrt(x * x + 1))).  */
38   svfloat32_t ax2 = svmul_x (pg, ax, ax);
39   svfloat32_t d = svadd_x (pg, svsqrt_x (pg, svadd_x (pg, ax2, 1.0f)), 1.0f);
40   svfloat32_t y
41       = sv_log1pf_inline (svadd_x (pg, ax, svdiv_x (pg, ax2, d)), pg);
42 
43   if (unlikely (svptest_any (pg, special)))
44     return special_case (
45 	x, svreinterpret_f32 (svorr_x (pg, sign, svreinterpret_u32 (y))),
46 	special);
47   return svreinterpret_f32 (svorr_x (pg, sign, svreinterpret_u32 (y)));
48 }
49 
50 PL_SIG (SV, F, 1, asinh, -10.0, 10.0)
51 PL_TEST_ULP (SV_NAME_F1 (asinh), 1.98)
52 PL_TEST_SYM_INTERVAL (SV_NAME_F1 (asinh), 0, 0x1p-12, 4000)
53 PL_TEST_SYM_INTERVAL (SV_NAME_F1 (asinh), 0x1p-12, 1.0, 20000)
54 PL_TEST_SYM_INTERVAL (SV_NAME_F1 (asinh), 1.0, 0x1p64, 20000)
55 PL_TEST_SYM_INTERVAL (SV_NAME_F1 (asinh), 0x1p64, inf, 4000)
56