aboutsummaryrefslogtreecommitdiff
path: root/pl/math/atanhf_3u1.c
blob: fb90aa29c7a39089e77f8ed0ae7edb02e2a882d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
 * Single-precision atanh(x) function.
 *
 * Copyright (c) 2022-2023, Arm Limited.
 * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
 */

#include "math_config.h"
#include "mathlib.h"
#include "pl_sig.h"
#include "pl_test.h"

#define AbsMask 0x7fffffff
#define Half 0x3f000000
#define One 0x3f800000
#define Four 0x40800000
#define Ln2 0x1.62e43p-1f
#define TinyBound 0x39800000 /* 0x1p-12, below which atanhf(x) rounds to x. */

#define C(i) __log1pf_data.coeffs[i]

static inline float
eval_poly (float m)
{
  /* Approximate log(1+m) on [-0.25, 0.5] using Estrin scheme.  */
  float p_12 = fmaf (m, C (1), C (0));
  float p_34 = fmaf (m, C (3), C (2));
  float p_56 = fmaf (m, C (5), C (4));
  float p_78 = fmaf (m, C (7), C (6));

  float m2 = m * m;
  float p_02 = fmaf (m2, p_12, m);
  float p_36 = fmaf (m2, p_56, p_34);
  float p_79 = fmaf (m2, C (8), p_78);

  float m4 = m2 * m2;
  float p_06 = fmaf (m4, p_36, p_02);

  return fmaf (m4 * p_79, m4, p_06);
}

static inline float
log1pf_inline (float x)
{
  /* Helper for calculating log(x + 1). Copied from log1pf_2u1.c, with no
     special-case handling. See that file for details of the algorithm.  */
  float m = x + 1.0f;
  int k = (asuint (m) - 0x3f400000) & 0xff800000;
  float s = asfloat (Four - k);
  float m_scale = asfloat (asuint (x) - k) + fmaf (0.25f, s, -1.0f);
  float p = eval_poly (m_scale);
  float scale_back = (float) k * 0x1.0p-23f;
  return fmaf (scale_back, Ln2, p);
}

/* Approximation for single-precision inverse tanh(x), using a simplified
   version of log1p. Maximum error is 3.08 ULP:
   atanhf(0x1.ff0d5p-5) got 0x1.ffb768p-5
		       want 0x1.ffb76ep-5.  */
float
atanhf (float x)
{
  uint32_t ix = asuint (x);
  uint32_t iax = ix & AbsMask;
  uint32_t sign = ix & ~AbsMask;

  if (unlikely (iax < TinyBound))
    return x;

  if (iax == One)
    return __math_divzero (sign);

  if (unlikely (iax > One))
    return __math_invalidf (x);

  float halfsign = asfloat (Half | sign);
  float ax = asfloat (iax);
  return halfsign * log1pf_inline ((2 * ax) / (1 - ax));
}

PL_SIG (S, F, 1, atanh, -1.0, 1.0)
PL_TEST_ULP (atanhf, 2.59)
PL_TEST_INTERVAL (atanhf, 0, 0x1p-12, 500)
PL_TEST_INTERVAL (atanhf, 0x1p-12, 1, 200000)
PL_TEST_INTERVAL (atanhf, 1, inf, 1000)
PL_TEST_INTERVAL (atanhf, -0, -0x1p-12, 500)
PL_TEST_INTERVAL (atanhf, -0x1p-12, -1, 200000)
PL_TEST_INTERVAL (atanhf, -1, -inf, 1000)