1*412f47f9SXin Li /*
2*412f47f9SXin Li * Single-precision vector atanh(x) function.
3*412f47f9SXin Li *
4*412f47f9SXin Li * Copyright (c) 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 "sv_math.h"
9*412f47f9SXin Li #include "mathlib.h"
10*412f47f9SXin Li #include "pl_sig.h"
11*412f47f9SXin Li #include "pl_test.h"
12*412f47f9SXin Li
13*412f47f9SXin Li #include "sv_log1pf_inline.h"
14*412f47f9SXin Li
15*412f47f9SXin Li #define One (0x3f800000)
16*412f47f9SXin Li #define Half (0x3f000000)
17*412f47f9SXin Li
18*412f47f9SXin Li static svfloat32_t NOINLINE
special_case(svfloat32_t x,svfloat32_t y,svbool_t special)19*412f47f9SXin Li special_case (svfloat32_t x, svfloat32_t y, svbool_t special)
20*412f47f9SXin Li {
21*412f47f9SXin Li return sv_call_f32 (atanhf, x, y, special);
22*412f47f9SXin Li }
23*412f47f9SXin Li
24*412f47f9SXin Li /* Approximation for vector single-precision atanh(x) using modified log1p.
25*412f47f9SXin Li The maximum error is 2.28 ULP:
26*412f47f9SXin Li _ZGVsMxv_atanhf(0x1.ff1194p-5) got 0x1.ffbbbcp-5
27*412f47f9SXin Li want 0x1.ffbbb6p-5. */
SV_NAME_F1(atanh)28*412f47f9SXin Li svfloat32_t SV_NAME_F1 (atanh) (svfloat32_t x, const svbool_t pg)
29*412f47f9SXin Li {
30*412f47f9SXin Li svfloat32_t ax = svabs_x (pg, x);
31*412f47f9SXin Li svuint32_t iax = svreinterpret_u32 (ax);
32*412f47f9SXin Li svuint32_t sign = sveor_x (pg, svreinterpret_u32 (x), iax);
33*412f47f9SXin Li svfloat32_t halfsign = svreinterpret_f32 (svorr_x (pg, sign, Half));
34*412f47f9SXin Li svbool_t special = svcmpge (pg, iax, One);
35*412f47f9SXin Li
36*412f47f9SXin Li /* Computation is performed based on the following sequence of equality:
37*412f47f9SXin Li * (1+x)/(1-x) = 1 + 2x/(1-x). */
38*412f47f9SXin Li svfloat32_t y = svadd_x (pg, ax, ax);
39*412f47f9SXin Li y = svdiv_x (pg, y, svsub_x (pg, sv_f32 (1), ax));
40*412f47f9SXin Li /* ln((1+x)/(1-x)) = ln(1+2x/(1-x)) = ln(1 + y). */
41*412f47f9SXin Li y = sv_log1pf_inline (y, pg);
42*412f47f9SXin Li
43*412f47f9SXin Li if (unlikely (svptest_any (pg, special)))
44*412f47f9SXin Li return special_case (x, svmul_x (pg, halfsign, y), special);
45*412f47f9SXin Li
46*412f47f9SXin Li return svmul_x (pg, halfsign, y);
47*412f47f9SXin Li }
48*412f47f9SXin Li
49*412f47f9SXin Li PL_SIG (SV, F, 1, atanh, -1.0, 1.0)
50*412f47f9SXin Li PL_TEST_ULP (SV_NAME_F1 (atanh), 2.59)
51*412f47f9SXin Li /* atanh is asymptotic at 1, which is the default control value - have to set
52*412f47f9SXin Li -c 0 specially to ensure fp exceptions are triggered correctly (choice of
53*412f47f9SXin Li control lane is irrelevant if fp exceptions are disabled). */
54*412f47f9SXin Li PL_TEST_SYM_INTERVAL_C (SV_NAME_F1 (atanh), 0, 0x1p-12, 1000, 0)
55*412f47f9SXin Li PL_TEST_SYM_INTERVAL_C (SV_NAME_F1 (atanh), 0x1p-12, 1, 20000, 0)
56*412f47f9SXin Li PL_TEST_SYM_INTERVAL_C (SV_NAME_F1 (atanh), 1, inf, 1000, 0)
57