1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner * Single-precision SVE tanh(x) function.
3*f3087befSAndrew Turner *
4*f3087befSAndrew Turner * Copyright (c) 2023-2024, Arm Limited.
5*f3087befSAndrew Turner * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6*f3087befSAndrew Turner */
7*f3087befSAndrew Turner
8*f3087befSAndrew Turner #include "sv_math.h"
9*f3087befSAndrew Turner #include "test_sig.h"
10*f3087befSAndrew Turner #include "test_defs.h"
11*f3087befSAndrew Turner #include "sv_expm1f_inline.h"
12*f3087befSAndrew Turner
13*f3087befSAndrew Turner /* Largest value of x for which tanhf(x) rounds to 1 (or -1 for negative). */
14*f3087befSAndrew Turner #define BoringBound 0x1.205966p+3f
15*f3087befSAndrew Turner
16*f3087befSAndrew Turner static const struct data
17*f3087befSAndrew Turner {
18*f3087befSAndrew Turner struct sv_expm1f_data expm1f_consts;
19*f3087befSAndrew Turner uint32_t onef, special_bound;
20*f3087befSAndrew Turner float boring_bound;
21*f3087befSAndrew Turner } data = {
22*f3087befSAndrew Turner .expm1f_consts = SV_EXPM1F_DATA,
23*f3087befSAndrew Turner .onef = 0x3f800000,
24*f3087befSAndrew Turner .special_bound = 0x7f800000,
25*f3087befSAndrew Turner .boring_bound = BoringBound,
26*f3087befSAndrew Turner };
27*f3087befSAndrew Turner
28*f3087befSAndrew Turner static svfloat32_t NOINLINE
special_case(svfloat32_t x,svbool_t pg,svbool_t is_boring,svfloat32_t boring,svfloat32_t q,svbool_t special)29*f3087befSAndrew Turner special_case (svfloat32_t x, svbool_t pg, svbool_t is_boring,
30*f3087befSAndrew Turner svfloat32_t boring, svfloat32_t q, svbool_t special)
31*f3087befSAndrew Turner {
32*f3087befSAndrew Turner svfloat32_t y
33*f3087befSAndrew Turner = svsel_f32 (is_boring, boring, svdiv_x (pg, q, svadd_x (pg, q, 2.0)));
34*f3087befSAndrew Turner return sv_call_f32 (tanhf, x, y, special);
35*f3087befSAndrew Turner }
36*f3087befSAndrew Turner
37*f3087befSAndrew Turner /* Approximation for single-precision SVE tanh(x), using a simplified
38*f3087befSAndrew Turner version of expm1f. The maximum error is 2.57 ULP:
39*f3087befSAndrew Turner _ZGVsMxv_tanhf (0x1.fc1832p-5) got 0x1.fb71a4p-5
40*f3087befSAndrew Turner want 0x1.fb71aap-5. */
SV_NAME_F1(tanh)41*f3087befSAndrew Turner svfloat32_t SV_NAME_F1 (tanh) (svfloat32_t x, const svbool_t pg)
42*f3087befSAndrew Turner {
43*f3087befSAndrew Turner const struct data *d = ptr_barrier (&data);
44*f3087befSAndrew Turner
45*f3087befSAndrew Turner svfloat32_t ax = svabs_x (pg, x);
46*f3087befSAndrew Turner svuint32_t iax = svreinterpret_u32 (ax);
47*f3087befSAndrew Turner svuint32_t sign = sveor_x (pg, svreinterpret_u32 (x), iax);
48*f3087befSAndrew Turner svfloat32_t boring = svreinterpret_f32 (svorr_x (pg, sign, d->onef));
49*f3087befSAndrew Turner svbool_t special = svcmpgt (pg, iax, d->special_bound);
50*f3087befSAndrew Turner svbool_t is_boring = svacgt (pg, x, d->boring_bound);
51*f3087befSAndrew Turner
52*f3087befSAndrew Turner /* tanh(x) = (e^2x - 1) / (e^2x + 1). */
53*f3087befSAndrew Turner svfloat32_t q = expm1f_inline (svmul_x (svptrue_b32 (), x, 2.0), pg,
54*f3087befSAndrew Turner &d->expm1f_consts);
55*f3087befSAndrew Turner
56*f3087befSAndrew Turner if (unlikely (svptest_any (pg, special)))
57*f3087befSAndrew Turner return special_case (x, pg, is_boring, boring, q, special);
58*f3087befSAndrew Turner svfloat32_t y = svdiv_x (pg, q, svadd_x (pg, q, 2.0));
59*f3087befSAndrew Turner return svsel_f32 (is_boring, boring, y);
60*f3087befSAndrew Turner }
61*f3087befSAndrew Turner
62*f3087befSAndrew Turner TEST_SIG (SV, F, 1, tanh, -10.0, 10.0)
63*f3087befSAndrew Turner TEST_ULP (SV_NAME_F1 (tanh), 2.07)
64*f3087befSAndrew Turner TEST_DISABLE_FENV (SV_NAME_F1 (tanh))
65*f3087befSAndrew Turner TEST_SYM_INTERVAL (SV_NAME_F1 (tanh), 0, 0x1p-23, 1000)
66*f3087befSAndrew Turner TEST_SYM_INTERVAL (SV_NAME_F1 (tanh), 0x1p-23, BoringBound, 100000)
67*f3087befSAndrew Turner TEST_SYM_INTERVAL (SV_NAME_F1 (tanh), BoringBound, inf, 100)
68*f3087befSAndrew Turner CLOSE_SVE_ATTR
69