aboutsummaryrefslogtreecommitdiff
path: root/pl/math/coshf_1u9.c
blob: c125c929aa77a92635eb9c2d9705b6b1e422b1c9 (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
/*
 * Single-precision cosh(x) function.
 *
 * Copyright (c) 2022-2023, Arm Limited.
 * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
 */

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

#define AbsMask 0x7fffffff
#define TinyBound 0x20000000 /* 0x1p-63: Round to 1 below this.  */
#define SpecialBound                                                           \
  0x42ad496c /* 0x1.5a92d8p+6: expf overflows above this, so have to use       \
		special case.  */

float
optr_aor_exp_f32 (float);

static NOINLINE float
specialcase (float x, uint32_t iax)
{
  if (iax == 0x7f800000)
    return INFINITY;
  if (iax > 0x7f800000)
    return __math_invalidf (x);
  if (iax <= TinyBound)
    /* For tiny x, avoid underflow by just returning 1.  */
    return 1;
  /* Otherwise SpecialBound <= |x| < Inf. x is too large to calculate exp(x)
     without overflow, so use exp(|x|/2) instead. For large x cosh(x) is
     dominated by exp(x), so return:
     cosh(x) ~= (exp(|x|/2))^2 / 2.  */
  float t = optr_aor_exp_f32 (asfloat (iax) / 2);
  return (0.5 * t) * t;
}

/* Approximation for single-precision cosh(x) using exp.
   cosh(x) = (exp(x) + exp(-x)) / 2.
   The maximum error is 1.89 ULP, observed for |x| > SpecialBound:
   coshf(0x1.65898cp+6) got 0x1.f00aep+127 want 0x1.f00adcp+127.
   The maximum error observed for TinyBound < |x| < SpecialBound is 1.02 ULP:
   coshf(0x1.50a3cp+0) got 0x1.ff21dcp+0 want 0x1.ff21dap+0.  */
float
coshf (float x)
{
  uint32_t ix = asuint (x);
  uint32_t iax = ix & AbsMask;
  float ax = asfloat (iax);

  if (unlikely (iax <= TinyBound || iax >= SpecialBound))
    {
      /* x is tiny, large or special.  */
      return specialcase (x, iax);
    }

  /* Compute cosh using the definition:
     coshf(x) = exp(x) / 2 + exp(-x) / 2.  */
  float t = optr_aor_exp_f32 (ax);
  return 0.5f * t + 0.5f / t;
}

PL_SIG (S, F, 1, cosh, -10.0, 10.0)
PL_TEST_ULP (coshf, 1.89)
PL_TEST_INTERVAL (coshf, 0, 0x1p-63, 100)
PL_TEST_INTERVAL (coshf, 0, 0x1.5a92d8p+6, 80000)
PL_TEST_INTERVAL (coshf, 0x1.5a92d8p+6, inf, 2000)
PL_TEST_INTERVAL (coshf, -0, -0x1p-63, 100)
PL_TEST_INTERVAL (coshf, -0, -0x1.5a92d8p+6, 80000)
PL_TEST_INTERVAL (coshf, -0x1.5a92d8p+6, -inf, 2000)