1 /*
2 * Single-precision SVE sinh(x) function.
3 *
4 * Copyright (c) 2023, Arm Limited.
5 * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6 */
7
8 #include "sv_math.h"
9 #include "pl_sig.h"
10 #include "pl_test.h"
11
12 #include "sv_expm1f_inline.h"
13
14 static const struct data
15 {
16 struct sv_expm1f_data expm1f_consts;
17 uint32_t halff, large_bound;
18 } data = {
19 .expm1f_consts = SV_EXPM1F_DATA,
20 .halff = 0x3f000000,
21 /* 0x1.61814ep+6, above which expm1f helper overflows. */
22 .large_bound = 0x42b0c0a7,
23 };
24
25 static svfloat32_t NOINLINE
special_case(svfloat32_t x,svfloat32_t y,svbool_t pg)26 special_case (svfloat32_t x, svfloat32_t y, svbool_t pg)
27 {
28 return sv_call_f32 (sinhf, x, y, pg);
29 }
30
31 /* Approximation for SVE single-precision sinh(x) using expm1.
32 sinh(x) = (exp(x) - exp(-x)) / 2.
33 The maximum error is 2.26 ULP:
34 _ZGVsMxv_sinhf (0x1.e34a9ep-4) got 0x1.e469ep-4
35 want 0x1.e469e4p-4. */
SV_NAME_F1(sinh)36 svfloat32_t SV_NAME_F1 (sinh) (svfloat32_t x, const svbool_t pg)
37 {
38 const struct data *d = ptr_barrier (&data);
39 svfloat32_t ax = svabs_x (pg, x);
40 svuint32_t sign
41 = sveor_x (pg, svreinterpret_u32 (x), svreinterpret_u32 (ax));
42 svfloat32_t halfsign = svreinterpret_f32 (svorr_x (pg, sign, d->halff));
43
44 svbool_t special = svcmpge (pg, svreinterpret_u32 (ax), d->large_bound);
45
46 /* Up to the point that expm1f overflows, we can use it to calculate sinhf
47 using a slight rearrangement of the definition of asinh. This allows us to
48 retain acceptable accuracy for very small inputs. */
49 svfloat32_t t = expm1f_inline (ax, pg, &d->expm1f_consts);
50 t = svadd_x (pg, t, svdiv_x (pg, t, svadd_x (pg, t, 1.0)));
51
52 /* Fall back to the scalar variant for any lanes which would cause
53 expm1f to overflow. */
54 if (unlikely (svptest_any (pg, special)))
55 return special_case (x, svmul_x (pg, t, halfsign), special);
56
57 return svmul_x (pg, t, halfsign);
58 }
59
60 PL_SIG (SV, F, 1, sinh, -10.0, 10.0)
61 PL_TEST_ULP (SV_NAME_F1 (sinh), 1.76)
62 PL_TEST_SYM_INTERVAL (SV_NAME_F1 (sinh), 0, 0x1.6a09e8p-32, 1000)
63 PL_TEST_SYM_INTERVAL (SV_NAME_F1 (sinh), 0x1.6a09e8p-32, 0x42b0c0a7, 100000)
64 PL_TEST_SYM_INTERVAL (SV_NAME_F1 (sinh), 0x42b0c0a7, inf, 1000)
65