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