xref: /freebsd/contrib/arm-optimized-routines/pl/math/log10_2u.c (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1 /*
2  * Double-precision log10(x) function.
3  *
4  * Copyright (c) 2020-2023, Arm Limited.
5  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6  */
7 
8 #include "math_config.h"
9 #include "pl_sig.h"
10 #include "pl_test.h"
11 
12 /* Polynomial coefficients and lookup tables.  */
13 #define T __log10_data.tab
14 #define T2 __log10_data.tab2
15 #define B __log10_data.poly1
16 #define A __log10_data.poly
17 #define Ln2hi __log10_data.ln2hi
18 #define Ln2lo __log10_data.ln2lo
19 #define InvLn10 __log10_data.invln10
20 #define N (1 << LOG10_TABLE_BITS)
21 #define OFF 0x3fe6000000000000
22 #define LO asuint64 (1.0 - 0x1p-4)
23 #define HI asuint64 (1.0 + 0x1.09p-4)
24 
25 /* Top 16 bits of a double.  */
26 static inline uint32_t
27 top16 (double x)
28 {
29   return asuint64 (x) >> 48;
30 }
31 
32 /* Fast and low accuracy implementation of log10.
33    The implementation is similar to that of math/log, except that:
34    - Polynomials are computed for log10(1+r) with r on same intervals as log.
35    - Lookup parameters are scaled (at runtime) to switch from base e to base 10.
36    Many errors above 1.59 ulp are observed across the whole range of doubles.
37    The greatest observed error is 1.61 ulp, at around 0.965:
38    log10(0x1.dc8710333a29bp-1) got -0x1.fee26884905a6p-6
39 			      want -0x1.fee26884905a8p-6.  */
40 double
41 log10 (double x)
42 {
43   /* double_t for better performance on targets with FLT_EVAL_METHOD==2.  */
44   double_t w, z, r, r2, r3, y, invc, logc, kd, hi, lo;
45   uint64_t ix, iz, tmp;
46   uint32_t top;
47   int k, i;
48 
49   ix = asuint64 (x);
50   top = top16 (x);
51 
52   if (unlikely (ix - LO < HI - LO))
53     {
54       /* Handle close to 1.0 inputs separately.  */
55       /* Fix sign of zero with downward rounding when x==1.  */
56       if (WANT_ROUNDING && unlikely (ix == asuint64 (1.0)))
57 	return 0;
58       r = x - 1.0;
59       r2 = r * r;
60       r3 = r * r2;
61       y = r3
62 	  * (B[1] + r * B[2] + r2 * B[3]
63 	     + r3
64 		 * (B[4] + r * B[5] + r2 * B[6]
65 		    + r3 * (B[7] + r * B[8] + r2 * B[9] + r3 * B[10])));
66       /* Worst-case error is around 0.507 ULP.  */
67       w = r * 0x1p27;
68       double_t rhi = r + w - w;
69       double_t rlo = r - rhi;
70       w = rhi * rhi * B[0];
71       hi = r + w;
72       lo = r - hi + w;
73       lo += B[0] * rlo * (rhi + r);
74       y += lo;
75       y += hi;
76       /* Scale by 1/ln(10). Polynomial already contains scaling.  */
77       y = y * InvLn10;
78 
79       return eval_as_double (y);
80     }
81   if (unlikely (top - 0x0010 >= 0x7ff0 - 0x0010))
82     {
83       /* x < 0x1p-1022 or inf or nan.  */
84       if (ix * 2 == 0)
85 	return __math_divzero (1);
86       if (ix == asuint64 (INFINITY)) /* log10(inf) == inf.  */
87 	return x;
88       if ((top & 0x8000) || (top & 0x7ff0) == 0x7ff0)
89 	return __math_invalid (x);
90       /* x is subnormal, normalize it.  */
91       ix = asuint64 (x * 0x1p52);
92       ix -= 52ULL << 52;
93     }
94 
95   /* x = 2^k z; where z is in range [OFF,2*OFF) and exact.
96      The range is split into N subintervals.
97      The ith subinterval contains z and c is near its center.  */
98   tmp = ix - OFF;
99   i = (tmp >> (52 - LOG10_TABLE_BITS)) % N;
100   k = (int64_t) tmp >> 52; /* arithmetic shift.  */
101   iz = ix - (tmp & 0xfffULL << 52);
102   invc = T[i].invc;
103   logc = T[i].logc;
104   z = asdouble (iz);
105 
106   /* log(x) = log1p(z/c-1) + log(c) + k*Ln2.  */
107   /* r ~= z/c - 1, |r| < 1/(2*N).  */
108 #if HAVE_FAST_FMA
109   /* rounding error: 0x1p-55/N.  */
110   r = fma (z, invc, -1.0);
111 #else
112   /* rounding error: 0x1p-55/N + 0x1p-66.  */
113   r = (z - T2[i].chi - T2[i].clo) * invc;
114 #endif
115   kd = (double_t) k;
116 
117   /* w = log(c) + k*Ln2hi.  */
118   w = kd * Ln2hi + logc;
119   hi = w + r;
120   lo = w - hi + r + kd * Ln2lo;
121 
122   /* log10(x) = (w + r)/log(10) + (log10(1+r) - r/log(10)).  */
123   r2 = r * r; /* rounding error: 0x1p-54/N^2.  */
124 
125   /* Scale by 1/ln(10). Polynomial already contains scaling.  */
126   y = lo + r2 * A[0] + r * r2 * (A[1] + r * A[2] + r2 * (A[3] + r * A[4])) + hi;
127   y = y * InvLn10;
128 
129   return eval_as_double (y);
130 }
131 
132 // clang-format off
133 #if USE_GLIBC_ABI
134 strong_alias (log10, __log10_finite)
135 hidden_alias (log10, __ieee754_log10)
136 #if LDBL_MANT_DIG == 53
137 long double
138 log10l (long double x)
139 {
140   return log10 (x);
141 }
142 #endif
143 #endif
144 // clang-format on
145 
146 PL_SIG (S, D, 1, log10, 0.01, 11.1)
147 PL_TEST_ULP (log10, 1.11)
148 PL_TEST_INTERVAL (log10, 0, 0xffff000000000000, 10000)
149 PL_TEST_INTERVAL (log10, 0x1p-4, 0x1p4, 40000)
150 PL_TEST_INTERVAL (log10, 0, inf, 40000)
151