xref: /freebsd/contrib/arm-optimized-routines/math/aarch64/sve/sinhf.c (revision f3087bef11543b42e0d69b708f367097a4118d24)
1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner  * Single-precision SVE sinh(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 static const struct data
14*f3087befSAndrew Turner {
15*f3087befSAndrew Turner   struct sv_expm1f_data expm1f_consts;
16*f3087befSAndrew Turner   uint32_t halff, large_bound;
17*f3087befSAndrew Turner } data = {
18*f3087befSAndrew Turner   .expm1f_consts = SV_EXPM1F_DATA,
19*f3087befSAndrew Turner   .halff = 0x3f000000,
20*f3087befSAndrew Turner   /* 0x1.61814ep+6, above which expm1f helper overflows.  */
21*f3087befSAndrew Turner   .large_bound = 0x42b0c0a7,
22*f3087befSAndrew Turner };
23*f3087befSAndrew Turner 
24*f3087befSAndrew Turner static svfloat32_t NOINLINE
special_case(svfloat32_t x,svfloat32_t y,svbool_t pg)25*f3087befSAndrew Turner special_case (svfloat32_t x, svfloat32_t y, svbool_t pg)
26*f3087befSAndrew Turner {
27*f3087befSAndrew Turner   return sv_call_f32 (sinhf, x, y, pg);
28*f3087befSAndrew Turner }
29*f3087befSAndrew Turner 
30*f3087befSAndrew Turner /* Approximation for SVE single-precision sinh(x) using expm1.
31*f3087befSAndrew Turner    sinh(x) = (exp(x) - exp(-x)) / 2.
32*f3087befSAndrew Turner    The maximum error is 2.26 ULP:
33*f3087befSAndrew Turner    _ZGVsMxv_sinhf (0x1.e34a9ep-4) got 0x1.e469ep-4
34*f3087befSAndrew Turner 				 want 0x1.e469e4p-4.  */
SV_NAME_F1(sinh)35*f3087befSAndrew Turner svfloat32_t SV_NAME_F1 (sinh) (svfloat32_t x, const svbool_t pg)
36*f3087befSAndrew Turner {
37*f3087befSAndrew Turner   const struct data *d = ptr_barrier (&data);
38*f3087befSAndrew Turner   svfloat32_t ax = svabs_x (pg, x);
39*f3087befSAndrew Turner   svuint32_t sign
40*f3087befSAndrew Turner       = sveor_x (pg, svreinterpret_u32 (x), svreinterpret_u32 (ax));
41*f3087befSAndrew Turner   svfloat32_t halfsign = svreinterpret_f32 (svorr_x (pg, sign, d->halff));
42*f3087befSAndrew Turner 
43*f3087befSAndrew Turner   svbool_t special = svcmpge (pg, svreinterpret_u32 (ax), d->large_bound);
44*f3087befSAndrew Turner 
45*f3087befSAndrew Turner   /* Up to the point that expm1f overflows, we can use it to calculate sinhf
46*f3087befSAndrew Turner    using a slight rearrangement of the definition of asinh. This allows us to
47*f3087befSAndrew Turner    retain acceptable accuracy for very small inputs.  */
48*f3087befSAndrew Turner   svfloat32_t t = expm1f_inline (ax, pg, &d->expm1f_consts);
49*f3087befSAndrew Turner   t = svadd_x (pg, t, svdiv_x (pg, t, svadd_x (pg, t, 1.0)));
50*f3087befSAndrew Turner 
51*f3087befSAndrew Turner   /* Fall back to the scalar variant for any lanes which would cause
52*f3087befSAndrew Turner      expm1f to overflow.  */
53*f3087befSAndrew Turner   if (unlikely (svptest_any (pg, special)))
54*f3087befSAndrew Turner     return special_case (x, svmul_x (pg, t, halfsign), special);
55*f3087befSAndrew Turner 
56*f3087befSAndrew Turner   return svmul_x (svptrue_b32 (), t, halfsign);
57*f3087befSAndrew Turner }
58*f3087befSAndrew Turner 
59*f3087befSAndrew Turner TEST_SIG (SV, F, 1, sinh, -10.0, 10.0)
60*f3087befSAndrew Turner TEST_ULP (SV_NAME_F1 (sinh), 1.76)
61*f3087befSAndrew Turner TEST_DISABLE_FENV (SV_NAME_F1 (sinh))
62*f3087befSAndrew Turner TEST_SYM_INTERVAL (SV_NAME_F1 (sinh), 0, 0x1.6a09e8p-32, 1000)
63*f3087befSAndrew Turner TEST_SYM_INTERVAL (SV_NAME_F1 (sinh), 0x1.6a09e8p-32, 0x42b0c0a7, 100000)
64*f3087befSAndrew Turner TEST_SYM_INTERVAL (SV_NAME_F1 (sinh), 0x42b0c0a7, inf, 1000)
65*f3087befSAndrew Turner CLOSE_SVE_ATTR
66