xref: /freebsd/contrib/arm-optimized-routines/pl/math/sv_coshf_2u.c (revision 5a02ffc32e777041dd2dad4e651ed2a0865a0a5d)
1 /*
2  * Single-precision SVE cosh(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_expf_inline.h"
13 
14 static const struct data
15 {
16   struct sv_expf_data expf_consts;
17   uint32_t special_bound;
18 } data = {
19   .expf_consts = SV_EXPF_DATA,
20   /* 0x1.5a92d8p+6: expf overflows above this, so have to use special case.  */
21   .special_bound = 0x42ad496c,
22 };
23 
24 static svfloat32_t NOINLINE
special_case(svfloat32_t x,svfloat32_t y,svbool_t pg)25 special_case (svfloat32_t x, svfloat32_t y, svbool_t pg)
26 {
27   return sv_call_f32 (coshf, x, y, pg);
28 }
29 
30 /* Single-precision vector cosh, using vector expf.
31    Maximum error is 1.89 ULP:
32    _ZGVsMxv_coshf (-0x1.65898cp+6) got 0x1.f00aep+127
33 				  want 0x1.f00adcp+127.  */
SV_NAME_F1(cosh)34 svfloat32_t SV_NAME_F1 (cosh) (svfloat32_t x, svbool_t pg)
35 {
36   const struct data *d = ptr_barrier (&data);
37 
38   svfloat32_t ax = svabs_x (pg, x);
39   svbool_t special = svcmpge (pg, svreinterpret_u32 (ax), d->special_bound);
40 
41   /* Calculate cosh by exp(x) / 2 + exp(-x) / 2.  */
42   svfloat32_t t = expf_inline (ax, pg, &d->expf_consts);
43   svfloat32_t half_t = svmul_x (pg, t, 0.5);
44   svfloat32_t half_over_t = svdivr_x (pg, t, 0.5);
45 
46   if (unlikely (svptest_any (pg, special)))
47     return special_case (x, svadd_x (pg, half_t, half_over_t), special);
48 
49   return svadd_x (pg, half_t, half_over_t);
50 }
51 
52 PL_SIG (SV, F, 1, cosh, -10.0, 10.0)
53 PL_TEST_ULP (SV_NAME_F1 (cosh), 1.39)
54 PL_TEST_SYM_INTERVAL (SV_NAME_F1 (cosh), 0, 0x1p-63, 100)
55 PL_TEST_SYM_INTERVAL (SV_NAME_F1 (cosh), 0, 0x1.5a92d8p+6, 80000)
56 PL_TEST_SYM_INTERVAL (SV_NAME_F1 (cosh), 0x1.5a92d8p+6, inf, 2000)
57