xref: /freebsd/contrib/arm-optimized-routines/math/aarch64/sve/log.c (revision f3087bef11543b42e0d69b708f367097a4118d24)
1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner  * Double-precision SVE log(x) function.
3*f3087befSAndrew Turner  *
4*f3087befSAndrew Turner  * Copyright (c) 2020-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 
12*f3087befSAndrew Turner #define N (1 << V_LOG_TABLE_BITS)
13*f3087befSAndrew Turner #define Max (0x7ff0000000000000)
14*f3087befSAndrew Turner #define Min (0x0010000000000000)
15*f3087befSAndrew Turner #define Thresh (0x7fe0000000000000) /* Max - Min.  */
16*f3087befSAndrew Turner 
17*f3087befSAndrew Turner static const struct data
18*f3087befSAndrew Turner {
19*f3087befSAndrew Turner   double c0, c2;
20*f3087befSAndrew Turner   double c1, c3;
21*f3087befSAndrew Turner   double ln2, c4;
22*f3087befSAndrew Turner   uint64_t off;
23*f3087befSAndrew Turner } data = {
24*f3087befSAndrew Turner   .c0 = -0x1.ffffffffffff7p-2,
25*f3087befSAndrew Turner   .c1 = 0x1.55555555170d4p-2,
26*f3087befSAndrew Turner   .c2 = -0x1.0000000399c27p-2,
27*f3087befSAndrew Turner   .c3 = 0x1.999b2e90e94cap-3,
28*f3087befSAndrew Turner   .c4 = -0x1.554e550bd501ep-3,
29*f3087befSAndrew Turner   .ln2 = 0x1.62e42fefa39efp-1,
30*f3087befSAndrew Turner   .off = 0x3fe6900900000000,
31*f3087befSAndrew Turner };
32*f3087befSAndrew Turner 
33*f3087befSAndrew Turner static svfloat64_t NOINLINE
special_case(svfloat64_t hi,svuint64_t tmp,svfloat64_t y,svfloat64_t r2,svbool_t special,const struct data * d)34*f3087befSAndrew Turner special_case (svfloat64_t hi, svuint64_t tmp, svfloat64_t y, svfloat64_t r2,
35*f3087befSAndrew Turner 	      svbool_t special, const struct data *d)
36*f3087befSAndrew Turner {
37*f3087befSAndrew Turner   svfloat64_t x = svreinterpret_f64 (svadd_x (svptrue_b64 (), tmp, d->off));
38*f3087befSAndrew Turner   return sv_call_f64 (log, x, svmla_x (svptrue_b64 (), hi, r2, y), special);
39*f3087befSAndrew Turner }
40*f3087befSAndrew Turner 
41*f3087befSAndrew Turner /* Double-precision SVE log routine.
42*f3087befSAndrew Turner    Maximum measured error is 2.64 ulp:
43*f3087befSAndrew Turner    SV_NAME_D1 (log)(0x1.95e54bc91a5e2p+184) got 0x1.fffffffe88cacp+6
44*f3087befSAndrew Turner 					   want 0x1.fffffffe88cafp+6.  */
SV_NAME_D1(log)45*f3087befSAndrew Turner svfloat64_t SV_NAME_D1 (log) (svfloat64_t x, const svbool_t pg)
46*f3087befSAndrew Turner {
47*f3087befSAndrew Turner   const struct data *d = ptr_barrier (&data);
48*f3087befSAndrew Turner 
49*f3087befSAndrew Turner   svuint64_t ix = svreinterpret_u64 (x);
50*f3087befSAndrew Turner   svbool_t special = svcmpge (pg, svsub_x (pg, ix, Min), Thresh);
51*f3087befSAndrew Turner 
52*f3087befSAndrew Turner   /* x = 2^k z; where z is in range [Off,2*Off) and exact.
53*f3087befSAndrew Turner      The range is split into N subintervals.
54*f3087befSAndrew Turner      The ith subinterval contains z and c is near its center.  */
55*f3087befSAndrew Turner   svuint64_t tmp = svsub_x (pg, ix, d->off);
56*f3087befSAndrew Turner   /* Calculate table index = (tmp >> (52 - V_LOG_TABLE_BITS)) % N.
57*f3087befSAndrew Turner      The actual value of i is double this due to table layout.  */
58*f3087befSAndrew Turner   svuint64_t i
59*f3087befSAndrew Turner       = svand_x (pg, svlsr_x (pg, tmp, (51 - V_LOG_TABLE_BITS)), (N - 1) << 1);
60*f3087befSAndrew Turner   svuint64_t iz = svsub_x (pg, ix, svand_x (pg, tmp, 0xfffULL << 52));
61*f3087befSAndrew Turner   svfloat64_t z = svreinterpret_f64 (iz);
62*f3087befSAndrew Turner   /* Lookup in 2 global lists (length N).  */
63*f3087befSAndrew Turner   svfloat64_t invc = svld1_gather_index (pg, &__v_log_data.table[0].invc, i);
64*f3087befSAndrew Turner   svfloat64_t logc = svld1_gather_index (pg, &__v_log_data.table[0].logc, i);
65*f3087befSAndrew Turner 
66*f3087befSAndrew Turner   /* log(x) = log1p(z/c-1) + log(c) + k*Ln2.  */
67*f3087befSAndrew Turner   svfloat64_t kd = svcvt_f64_x (pg, svasr_x (pg, svreinterpret_s64 (tmp), 52));
68*f3087befSAndrew Turner   /* hi = r + log(c) + k*Ln2.  */
69*f3087befSAndrew Turner   svfloat64_t ln2_and_c4 = svld1rq_f64 (svptrue_b64 (), &d->ln2);
70*f3087befSAndrew Turner   svfloat64_t r = svmad_x (pg, invc, z, -1);
71*f3087befSAndrew Turner   svfloat64_t hi = svmla_lane_f64 (logc, kd, ln2_and_c4, 0);
72*f3087befSAndrew Turner   hi = svadd_x (pg, r, hi);
73*f3087befSAndrew Turner 
74*f3087befSAndrew Turner   /* y = r2*(A0 + r*A1 + r2*(A2 + r*A3 + r2*A4)) + hi.  */
75*f3087befSAndrew Turner   svfloat64_t odd_coeffs = svld1rq_f64 (svptrue_b64 (), &d->c1);
76*f3087befSAndrew Turner   svfloat64_t r2 = svmul_x (svptrue_b64 (), r, r);
77*f3087befSAndrew Turner   svfloat64_t y = svmla_lane_f64 (sv_f64 (d->c2), r, odd_coeffs, 1);
78*f3087befSAndrew Turner   svfloat64_t p = svmla_lane_f64 (sv_f64 (d->c0), r, odd_coeffs, 0);
79*f3087befSAndrew Turner   y = svmla_lane_f64 (y, r2, ln2_and_c4, 1);
80*f3087befSAndrew Turner   y = svmla_x (pg, p, r2, y);
81*f3087befSAndrew Turner 
82*f3087befSAndrew Turner   if (unlikely (svptest_any (pg, special)))
83*f3087befSAndrew Turner     return special_case (hi, tmp, y, r2, special, d);
84*f3087befSAndrew Turner   return svmla_x (pg, hi, r2, y);
85*f3087befSAndrew Turner }
86*f3087befSAndrew Turner 
87*f3087befSAndrew Turner TEST_SIG (SV, D, 1, log, 0.01, 11.1)
88*f3087befSAndrew Turner TEST_ULP (SV_NAME_D1 (log), 2.15)
89*f3087befSAndrew Turner TEST_DISABLE_FENV (SV_NAME_D1 (log))
90*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_D1 (log), -0.0, -inf, 1000)
91*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_D1 (log), 0, 0x1p-149, 1000)
92*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_D1 (log), 0x1p-149, 0x1p-126, 4000)
93*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_D1 (log), 0x1p-126, 0x1p-23, 50000)
94*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_D1 (log), 0x1p-23, 1.0, 50000)
95*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_D1 (log), 1.0, 100, 50000)
96*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_D1 (log), 100, inf, 50000)
97*f3087befSAndrew Turner CLOSE_SVE_ATTR
98