xref: /freebsd/contrib/arm-optimized-routines/pl/math/sv_log10_2u5.c (revision 2e3f49888ec8851bafb22011533217487764fdb0)
1 /*
2  * Double-precision SVE log10(x) function.
3  *
4  * Copyright (c) 2022-2023, Arm Limited.
5  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6  */
7 
8 #include "sv_math.h"
9 #include "math_config.h"
10 #include "pl_sig.h"
11 #include "pl_test.h"
12 
13 #if SV_SUPPORTED
14 
15 #define OFF 0x3fe6900900000000
16 #define N (1 << V_LOG10_TABLE_BITS)
17 
18 #define A(i) __v_log10_data.poly[i]
19 
20 static inline sv_f64_t
21 specialcase (sv_f64_t x, sv_f64_t y, svbool_t special)
22 {
23   return sv_call_f64 (log10, x, y, special);
24 }
25 
26 /* SVE log10 algorithm. Maximum measured error is 2.46 ulps.
27    __sv_log10(0x1.131956cd4b627p+0) got 0x1.fffbdf6eaa669p-6
28 				   want 0x1.fffbdf6eaa667p-6.  */
29 sv_f64_t
30 __sv_log10_x (sv_f64_t x, const svbool_t pg)
31 {
32   sv_u64_t ix = sv_as_u64_f64 (x);
33   sv_u64_t top = svlsr_n_u64_x (pg, ix, 48);
34 
35   svbool_t is_special_case
36     = svcmpge_n_u64 (pg, svsub_n_u64_x (pg, top, 0x0010), 0x07ff0 - 0x0010);
37 
38   /* x = 2^k z; where z is in range [OFF,2*OFF) and exact.
39      The range is split into N subintervals.
40      The ith subinterval contains z and c is near its center.  */
41   sv_u64_t tmp = svsub_n_u64_x (pg, ix, OFF);
42   sv_u64_t i
43     = sv_mod_n_u64_x (pg, svlsr_n_u64_x (pg, tmp, 52 - V_LOG10_TABLE_BITS), N);
44   sv_f64_t k
45     = sv_to_f64_s64_x (pg, svasr_n_s64_x (pg, sv_as_s64_u64 (tmp), 52));
46   sv_f64_t z = sv_as_f64_u64 (
47     svsub_u64_x (pg, ix, svand_n_u64_x (pg, tmp, 0xfffULL << 52)));
48 
49   /* log(x) = k*log(2) + log(c) + log(z/c).  */
50 
51   sv_u64_t idx = svmul_n_u64_x (pg, i, 2);
52   sv_f64_t invc = sv_lookup_f64_x (pg, &__v_log10_data.tab[0].invc, idx);
53   sv_f64_t logc = sv_lookup_f64_x (pg, &__v_log10_data.tab[0].log10c, idx);
54 
55   /* We approximate log(z/c) with a polynomial P(x) ~= log(x + 1):
56      r = z/c - 1 (we look up precomputed 1/c)
57      log(z/c) ~= P(r).  */
58   sv_f64_t r = sv_fma_f64_x (pg, z, invc, sv_f64 (-1.0));
59 
60   /* hi = log(c) + k*log(2).  */
61   sv_f64_t w = sv_fma_n_f64_x (pg, __v_log10_data.invln10, r, logc);
62   sv_f64_t hi = sv_fma_n_f64_x (pg, __v_log10_data.log10_2, k, w);
63 
64   /* y = r2*(A0 + r*A1 + r2*(A2 + r*A3 + r2*A4)) + hi.  */
65   sv_f64_t r2 = svmul_f64_x (pg, r, r);
66   sv_f64_t y = sv_fma_n_f64_x (pg, A (3), r, sv_f64 (A (2)));
67   sv_f64_t p = sv_fma_n_f64_x (pg, A (1), r, sv_f64 (A (0)));
68   y = sv_fma_n_f64_x (pg, A (4), r2, y);
69   y = sv_fma_f64_x (pg, y, r2, p);
70   y = sv_fma_f64_x (pg, y, r2, hi);
71 
72   if (unlikely (svptest_any (pg, is_special_case)))
73     {
74       return specialcase (x, y, is_special_case);
75     }
76   return y;
77 }
78 
79 PL_ALIAS (__sv_log10_x, _ZGVsMxv_log10)
80 
81 PL_SIG (SV, D, 1, log10, 0.01, 11.1)
82 PL_TEST_ULP (__sv_log10, 1.97)
83 PL_TEST_INTERVAL (__sv_log10, -0.0, -0x1p126, 100)
84 PL_TEST_INTERVAL (__sv_log10, 0x1p-149, 0x1p-126, 4000)
85 PL_TEST_INTERVAL (__sv_log10, 0x1p-126, 0x1p-23, 50000)
86 PL_TEST_INTERVAL (__sv_log10, 0x1p-23, 1.0, 50000)
87 PL_TEST_INTERVAL (__sv_log10, 1.0, 100, 50000)
88 PL_TEST_INTERVAL (__sv_log10, 100, inf, 50000)
89 #endif
90