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