xref: /freebsd/contrib/arm-optimized-routines/pl/math/sv_log10_2u5.c (revision aa1a8ff2d6dbc51ef058f46f3db5a8bb77967145)
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 "pl_sig.h"
10 #include "pl_test.h"
11 #include "poly_sve_f64.h"
12 
13 #define Min 0x0010000000000000
14 #define Max 0x7ff0000000000000
15 #define Thres 0x7fe0000000000000 /* Max - Min.  */
16 #define Off 0x3fe6900900000000
17 #define N (1 << V_LOG10_TABLE_BITS)
18 
19 static svfloat64_t NOINLINE
20 special_case (svfloat64_t x, svfloat64_t y, svbool_t special)
21 {
22   return sv_call_f64 (log10, x, y, special);
23 }
24 
25 /* SVE log10 algorithm.
26    Maximum measured error is 2.46 ulps.
27    SV_NAME_D1 (log10)(0x1.131956cd4b627p+0) got 0x1.fffbdf6eaa669p-6
28 					   want 0x1.fffbdf6eaa667p-6.  */
29 svfloat64_t SV_NAME_D1 (log10) (svfloat64_t x, const svbool_t pg)
30 {
31   svuint64_t ix = svreinterpret_u64 (x);
32   svbool_t special = svcmpge (pg, svsub_x (pg, ix, Min), Thres);
33 
34   /* x = 2^k z; where z is in range [Off,2*Off) and exact.
35      The range is split into N subintervals.
36      The ith subinterval contains z and c is near its center.  */
37   svuint64_t tmp = svsub_x (pg, ix, Off);
38   svuint64_t i = svlsr_x (pg, tmp, 51 - V_LOG10_TABLE_BITS);
39   i = svand_x (pg, i, (N - 1) << 1);
40   svfloat64_t k = svcvt_f64_x (pg, svasr_x (pg, svreinterpret_s64 (tmp), 52));
41   svfloat64_t z = svreinterpret_f64 (
42       svsub_x (pg, ix, svand_x (pg, tmp, 0xfffULL << 52)));
43 
44   /* log(x) = k*log(2) + log(c) + log(z/c).  */
45   svfloat64_t invc = svld1_gather_index (pg, &__v_log10_data.table[0].invc, i);
46   svfloat64_t logc
47       = svld1_gather_index (pg, &__v_log10_data.table[0].log10c, i);
48 
49   /* We approximate log(z/c) with a polynomial P(x) ~= log(x + 1):
50      r = z/c - 1 (we look up precomputed 1/c)
51      log(z/c) ~= P(r).  */
52   svfloat64_t r = svmad_x (pg, invc, z, -1.0);
53 
54   /* hi = log(c) + k*log(2).  */
55   svfloat64_t w = svmla_x (pg, logc, r, __v_log10_data.invln10);
56   svfloat64_t hi = svmla_x (pg, w, k, __v_log10_data.log10_2);
57 
58   /* y = r2*(A0 + r*A1 + r2*(A2 + r*A3 + r2*A4)) + hi.  */
59   svfloat64_t r2 = svmul_x (pg, r, r);
60   svfloat64_t y = sv_pw_horner_4_f64_x (pg, r, r2, __v_log10_data.poly);
61 
62   if (unlikely (svptest_any (pg, special)))
63     return special_case (x, svmla_x (svnot_z (pg, special), hi, r2, y),
64 			 special);
65   return svmla_x (pg, hi, r2, y);
66 }
67 
68 PL_SIG (SV, D, 1, log10, 0.01, 11.1)
69 PL_TEST_ULP (SV_NAME_D1 (log10), 1.97)
70 PL_TEST_INTERVAL (SV_NAME_D1 (log10), -0.0, -0x1p126, 100)
71 PL_TEST_INTERVAL (SV_NAME_D1 (log10), 0x1p-149, 0x1p-126, 4000)
72 PL_TEST_INTERVAL (SV_NAME_D1 (log10), 0x1p-126, 0x1p-23, 50000)
73 PL_TEST_INTERVAL (SV_NAME_D1 (log10), 0x1p-23, 1.0, 50000)
74 PL_TEST_INTERVAL (SV_NAME_D1 (log10), 1.0, 100, 50000)
75 PL_TEST_INTERVAL (SV_NAME_D1 (log10), 100, inf, 50000)
76