1*412f47f9SXin Li /*
2*412f47f9SXin Li * Single-precision vector cosh(x) function.
3*412f47f9SXin Li *
4*412f47f9SXin Li * Copyright (c) 2022-2024, 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 "v_expf_inline.h"
9*412f47f9SXin Li #include "v_math.h"
10*412f47f9SXin Li #include "mathlib.h"
11*412f47f9SXin Li #include "pl_sig.h"
12*412f47f9SXin Li #include "pl_test.h"
13*412f47f9SXin Li
14*412f47f9SXin Li static const struct data
15*412f47f9SXin Li {
16*412f47f9SXin Li struct v_expf_data expf_consts;
17*412f47f9SXin Li uint32x4_t tiny_bound, special_bound;
18*412f47f9SXin Li } data = {
19*412f47f9SXin Li .expf_consts = V_EXPF_DATA,
20*412f47f9SXin Li .tiny_bound = V4 (0x20000000), /* 0x1p-63: Round to 1 below this. */
21*412f47f9SXin Li /* 0x1.5a92d8p+6: expf overflows above this, so have to use special case. */
22*412f47f9SXin Li .special_bound = V4 (0x42ad496c),
23*412f47f9SXin Li };
24*412f47f9SXin Li
25*412f47f9SXin Li #if !WANT_SIMD_EXCEPT
26*412f47f9SXin Li static float32x4_t NOINLINE VPCS_ATTR
special_case(float32x4_t x,float32x4_t y,uint32x4_t special)27*412f47f9SXin Li special_case (float32x4_t x, float32x4_t y, uint32x4_t special)
28*412f47f9SXin Li {
29*412f47f9SXin Li return v_call_f32 (coshf, x, y, special);
30*412f47f9SXin Li }
31*412f47f9SXin Li #endif
32*412f47f9SXin Li
33*412f47f9SXin Li /* Single-precision vector cosh, using vector expf.
34*412f47f9SXin Li Maximum error is 2.38 ULP:
35*412f47f9SXin Li _ZGVnN4v_coshf (0x1.e8001ep+1) got 0x1.6a491ep+4
36*412f47f9SXin Li want 0x1.6a4922p+4. */
V_NAME_F1(cosh)37*412f47f9SXin Li float32x4_t VPCS_ATTR V_NAME_F1 (cosh) (float32x4_t x)
38*412f47f9SXin Li {
39*412f47f9SXin Li const struct data *d = ptr_barrier (&data);
40*412f47f9SXin Li
41*412f47f9SXin Li float32x4_t ax = vabsq_f32 (x);
42*412f47f9SXin Li uint32x4_t iax = vreinterpretq_u32_f32 (ax);
43*412f47f9SXin Li uint32x4_t special = vcgeq_u32 (iax, d->special_bound);
44*412f47f9SXin Li
45*412f47f9SXin Li #if WANT_SIMD_EXCEPT
46*412f47f9SXin Li /* If fp exceptions are to be triggered correctly, fall back to the scalar
47*412f47f9SXin Li variant for all inputs if any input is a special value or above the bound
48*412f47f9SXin Li at which expf overflows. */
49*412f47f9SXin Li if (unlikely (v_any_u32 (special)))
50*412f47f9SXin Li return v_call_f32 (coshf, x, x, v_u32 (-1));
51*412f47f9SXin Li
52*412f47f9SXin Li uint32x4_t tiny = vcleq_u32 (iax, d->tiny_bound);
53*412f47f9SXin Li /* If any input is tiny, avoid underflow exception by fixing tiny lanes of
54*412f47f9SXin Li input to 0, which will generate no exceptions. */
55*412f47f9SXin Li if (unlikely (v_any_u32 (tiny)))
56*412f47f9SXin Li ax = v_zerofy_f32 (ax, tiny);
57*412f47f9SXin Li #endif
58*412f47f9SXin Li
59*412f47f9SXin Li /* Calculate cosh by exp(x) / 2 + exp(-x) / 2. */
60*412f47f9SXin Li float32x4_t t = v_expf_inline (ax, &d->expf_consts);
61*412f47f9SXin Li float32x4_t half_t = vmulq_n_f32 (t, 0.5);
62*412f47f9SXin Li float32x4_t half_over_t = vdivq_f32 (v_f32 (0.5), t);
63*412f47f9SXin Li
64*412f47f9SXin Li #if WANT_SIMD_EXCEPT
65*412f47f9SXin Li if (unlikely (v_any_u32 (tiny)))
66*412f47f9SXin Li return vbslq_f32 (tiny, v_f32 (1), vaddq_f32 (half_t, half_over_t));
67*412f47f9SXin Li #else
68*412f47f9SXin Li if (unlikely (v_any_u32 (special)))
69*412f47f9SXin Li return special_case (x, vaddq_f32 (half_t, half_over_t), special);
70*412f47f9SXin Li #endif
71*412f47f9SXin Li
72*412f47f9SXin Li return vaddq_f32 (half_t, half_over_t);
73*412f47f9SXin Li }
74*412f47f9SXin Li
75*412f47f9SXin Li PL_SIG (V, F, 1, cosh, -10.0, 10.0)
76*412f47f9SXin Li PL_TEST_ULP (V_NAME_F1 (cosh), 1.89)
77*412f47f9SXin Li PL_TEST_EXPECT_FENV (V_NAME_F1 (cosh), WANT_SIMD_EXCEPT)
78*412f47f9SXin Li PL_TEST_SYM_INTERVAL (V_NAME_F1 (cosh), 0, 0x1p-63, 100)
79*412f47f9SXin Li PL_TEST_SYM_INTERVAL (V_NAME_F1 (cosh), 0x1p-63, 1, 1000)
80*412f47f9SXin Li PL_TEST_SYM_INTERVAL (V_NAME_F1 (cosh), 1, 0x1.5a92d8p+6, 80000)
81*412f47f9SXin Li PL_TEST_SYM_INTERVAL (V_NAME_F1 (cosh), 0x1.5a92d8p+6, inf, 2000)
82