1 /*
2 * Double-precision vector tanpi(x) function.
3 *
4 * Copyright (c) 2024, Arm Limited.
5 * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6 */
7
8 #include "sv_math.h"
9 #include "test_sig.h"
10 #include "test_defs.h"
11
12 const static struct v_tanpi_data
13 {
14 double c0, c2, c4, c6, c8, c10, c12;
15 double c1, c3, c5, c7, c9, c11, c13, c14;
16 } tanpi_data = {
17 /* Coefficents for tan(pi * x) computed with fpminimax
18 on [ 0x1p-1022 0x1p-2 ]
19 approx rel error: 0x1.7eap-55
20 approx abs error: 0x1.7eap-55. */
21 .c0 = 0x1.921fb54442d18p1, /* pi. */
22 .c1 = 0x1.4abbce625be52p3, .c2 = 0x1.466bc6775b0f9p5,
23 .c3 = 0x1.45fff9b426f5ep7, .c4 = 0x1.45f4730dbca5cp9,
24 .c5 = 0x1.45f3265994f85p11, .c6 = 0x1.45f4234b330cap13,
25 .c7 = 0x1.45dca11be79ebp15, .c8 = 0x1.47283fc5eea69p17,
26 .c9 = 0x1.3a6d958cdefaep19, .c10 = 0x1.927896baee627p21,
27 .c11 = -0x1.89333f6acd922p19, .c12 = 0x1.5d4e912bb8456p27,
28 .c13 = -0x1.a854d53ab6874p29, .c14 = 0x1.1b76de7681424p32,
29 };
30
31 /* Approximation for double-precision vector tanpi(x)
32 The maximum error is 3.06 ULP:
33 _ZGVsMxv_tanpi(0x1.0a4a07dfcca3ep-1) got -0x1.fa30112702c98p+3
34 want -0x1.fa30112702c95p+3. */
SV_NAME_D1(tanpi)35 svfloat64_t SV_NAME_D1 (tanpi) (svfloat64_t x, const svbool_t pg)
36 {
37 const struct v_tanpi_data *d = ptr_barrier (&tanpi_data);
38
39 svfloat64_t n = svrintn_x (pg, x);
40
41 /* inf produces nan that propagates. */
42 svfloat64_t xr = svsub_x (pg, x, n);
43 svfloat64_t ar = svabd_x (pg, x, n);
44 svbool_t flip = svcmpgt (pg, ar, 0.25);
45 svfloat64_t r = svsel (flip, svsubr_x (pg, ar, 0.5), ar);
46
47 /* Order-14 pairwise Horner. */
48 svfloat64_t r2 = svmul_x (pg, r, r);
49 svfloat64_t r4 = svmul_x (pg, r2, r2);
50
51 svfloat64_t c_1_3 = svld1rq (pg, &d->c1);
52 svfloat64_t c_5_7 = svld1rq (pg, &d->c5);
53 svfloat64_t c_9_11 = svld1rq (pg, &d->c9);
54 svfloat64_t c_13_14 = svld1rq (pg, &d->c13);
55 svfloat64_t p01 = svmla_lane (sv_f64 (d->c0), r2, c_1_3, 0);
56 svfloat64_t p23 = svmla_lane (sv_f64 (d->c2), r2, c_1_3, 1);
57 svfloat64_t p45 = svmla_lane (sv_f64 (d->c4), r2, c_5_7, 0);
58 svfloat64_t p67 = svmla_lane (sv_f64 (d->c6), r2, c_5_7, 1);
59 svfloat64_t p89 = svmla_lane (sv_f64 (d->c8), r2, c_9_11, 0);
60 svfloat64_t p1011 = svmla_lane (sv_f64 (d->c10), r2, c_9_11, 1);
61 svfloat64_t p1213 = svmla_lane (sv_f64 (d->c12), r2, c_13_14, 0);
62
63 svfloat64_t p = svmla_lane (p1213, r4, c_13_14, 1);
64 p = svmad_x (pg, p, r4, p1011);
65 p = svmad_x (pg, p, r4, p89);
66 p = svmad_x (pg, p, r4, p67);
67 p = svmad_x (pg, p, r4, p45);
68 p = svmad_x (pg, p, r4, p23);
69 p = svmad_x (pg, p, r4, p01);
70 p = svmul_x (pg, r, p);
71
72 svfloat64_t p_recip = svdivr_x (pg, p, 1.0);
73 svfloat64_t y = svsel (flip, p_recip, p);
74
75 svuint64_t sign
76 = sveor_x (pg, svreinterpret_u64 (xr), svreinterpret_u64 (ar));
77 return svreinterpret_f64 (svorr_x (pg, svreinterpret_u64 (y), sign));
78 }
79
80 #if WANT_TRIGPI_TESTS
81 TEST_DISABLE_FENV (SV_NAME_D1 (tanpi))
82 TEST_ULP (SV_NAME_D1 (tanpi), 2.57)
83 TEST_SYM_INTERVAL (SV_NAME_D1 (tanpi), 0, 0x1p-31, 50000)
84 TEST_SYM_INTERVAL (SV_NAME_D1 (tanpi), 0x1p-31, 0.5, 50000)
85 TEST_SYM_INTERVAL (SV_NAME_D1 (tanpi), 0.5, 1.0, 200000)
86 TEST_SYM_INTERVAL (SV_NAME_D1 (tanpi), 1.0, 0x1p23, 50000)
87 TEST_SYM_INTERVAL (SV_NAME_D1 (tanpi), 0x1p23, inf, 50000)
88 #endif
89 CLOSE_SVE_ATTR
90