xref: /freebsd/contrib/arm-optimized-routines/math/aarch64/sve/coshf.c (revision f3087bef11543b42e0d69b708f367097a4118d24)
1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner  * Single-precision SVE cosh(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_expf_inline.h"
12*f3087befSAndrew Turner 
13*f3087befSAndrew Turner static const struct data
14*f3087befSAndrew Turner {
15*f3087befSAndrew Turner   struct sv_expf_data expf_consts;
16*f3087befSAndrew Turner   float special_bound;
17*f3087befSAndrew Turner } data = {
18*f3087befSAndrew Turner   .expf_consts = SV_EXPF_DATA,
19*f3087befSAndrew Turner   /* 0x1.5a92d8p+6: expf overflows above this, so have to use special case.  */
20*f3087befSAndrew Turner   .special_bound = 0x1.5a92d8p+6,
21*f3087befSAndrew Turner };
22*f3087befSAndrew Turner 
23*f3087befSAndrew Turner static svfloat32_t NOINLINE
special_case(svfloat32_t x,svfloat32_t half_e,svfloat32_t half_over_e,svbool_t pg)24*f3087befSAndrew Turner special_case (svfloat32_t x, svfloat32_t half_e, svfloat32_t half_over_e,
25*f3087befSAndrew Turner 	      svbool_t pg)
26*f3087befSAndrew Turner {
27*f3087befSAndrew Turner   return sv_call_f32 (coshf, x, svadd_x (svptrue_b32 (), half_e, half_over_e),
28*f3087befSAndrew Turner 		      pg);
29*f3087befSAndrew Turner }
30*f3087befSAndrew Turner 
31*f3087befSAndrew Turner /* Single-precision vector cosh, using vector expf.
32*f3087befSAndrew Turner    Maximum error is 2.77 ULP:
33*f3087befSAndrew Turner    _ZGVsMxv_coshf(-0x1.5b38f4p+1) got 0x1.e45946p+2
34*f3087befSAndrew Turner 				 want 0x1.e4594cp+2.  */
SV_NAME_F1(cosh)35*f3087befSAndrew Turner svfloat32_t SV_NAME_F1 (cosh) (svfloat32_t x, svbool_t pg)
36*f3087befSAndrew Turner {
37*f3087befSAndrew Turner   const struct data *d = ptr_barrier (&data);
38*f3087befSAndrew Turner 
39*f3087befSAndrew Turner   svbool_t special = svacge (pg, x, d->special_bound);
40*f3087befSAndrew Turner 
41*f3087befSAndrew Turner   /* Calculate cosh by exp(x) / 2 + exp(-x) / 2.
42*f3087befSAndrew Turner      Note that x is passed to exp here, rather than |x|. This is to avoid using
43*f3087befSAndrew Turner      destructive unary ABS for better register usage. However it means the
44*f3087befSAndrew Turner      routine is not exactly symmetrical, as the exp helper is slightly less
45*f3087befSAndrew Turner      accurate in the negative range.  */
46*f3087befSAndrew Turner   svfloat32_t e = expf_inline (x, pg, &d->expf_consts);
47*f3087befSAndrew Turner   svfloat32_t half_e = svmul_x (svptrue_b32 (), e, 0.5);
48*f3087befSAndrew Turner   svfloat32_t half_over_e = svdivr_x (pg, e, 0.5);
49*f3087befSAndrew Turner 
50*f3087befSAndrew Turner   if (unlikely (svptest_any (pg, special)))
51*f3087befSAndrew Turner     return special_case (x, half_e, half_over_e, special);
52*f3087befSAndrew Turner 
53*f3087befSAndrew Turner   return svadd_x (svptrue_b32 (), half_e, half_over_e);
54*f3087befSAndrew Turner }
55*f3087befSAndrew Turner 
56*f3087befSAndrew Turner TEST_SIG (SV, F, 1, cosh, -10.0, 10.0)
57*f3087befSAndrew Turner TEST_ULP (SV_NAME_F1 (cosh), 2.28)
58*f3087befSAndrew Turner TEST_DISABLE_FENV (SV_NAME_F1 (cosh))
59*f3087befSAndrew Turner TEST_SYM_INTERVAL (SV_NAME_F1 (cosh), 0, 0x1p-63, 100)
60*f3087befSAndrew Turner TEST_SYM_INTERVAL (SV_NAME_F1 (cosh), 0, 0x1.5a92d8p+6, 80000)
61*f3087befSAndrew Turner TEST_SYM_INTERVAL (SV_NAME_F1 (cosh), 0x1.5a92d8p+6, inf, 2000)
62*f3087befSAndrew Turner CLOSE_SVE_ATTR
63