1 //===-- Single-precision log10(x) function --------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "src/math/log10f.h"
10 #include "common_constants.h" // Lookup table for (1/f)
11 #include "src/__support/FPUtil/BasicOperations.h"
12 #include "src/__support/FPUtil/FEnvImpl.h"
13 #include "src/__support/FPUtil/FMA.h"
14 #include "src/__support/FPUtil/FPBits.h"
15 #include "src/__support/FPUtil/PolyEval.h"
16 #include "src/__support/common.h"
17 
18 // This is an algorithm for log10(x) in single precision which is
19 // correctly rounded for all rounding modes, based on the implementation of
20 // log10(x) from the RLIBM project at:
21 // https://people.cs.rutgers.edu/~sn349/rlibm
22 
23 // Step 1 - Range reduction:
24 //   For x = 2^m * 1.mant, log(x) = m * log10(2) + log10(1.m)
25 //   If x is denormal, we normalize it by multiplying x by 2^23 and subtracting
26 //   m by 23.
27 
28 // Step 2 - Another range reduction:
29 //   To compute log(1.mant), let f be the highest 8 bits including the hidden
30 // bit, and d be the difference (1.mant - f), i.e. the remaining 16 bits of the
31 // mantissa. Then we have the following approximation formula:
32 //   log10(1.mant) = log10(f) + log10(1.mant / f)
33 //                 = log10(f) + log10(1 + d/f)
34 //                 ~ log10(f) + P(d/f)
35 // since d/f is sufficiently small.
36 //   log10(f) and 1/f are then stored in two 2^7 = 128 entries look-up tables.
37 
38 // Step 3 - Polynomial approximation:
39 //   To compute P(d/f), we use a single degree-5 polynomial in double precision
40 // which provides correct rounding for all but few exception values.
41 //   For more detail about how this polynomial is obtained, please refer to the
42 // papers:
43 //   Lim, J. and Nagarakatte, S., "One Polynomial Approximation to Produce
44 // Correctly Rounded Results of an Elementary Function for Multiple
45 // Representations and Rounding Modes", Proceedings of the 49th ACM SIGPLAN
46 // Symposium on Principles of Programming Languages (POPL-2022), Philadelphia,
47 // USA, Jan. 16-22, 2022.
48 // https://people.cs.rutgers.edu/~sn349/papers/rlibmall-popl-2022.pdf
49 //   Aanjaneya, M., Lim, J., and Nagarakatte, S., "RLibm-Prog: Progressive
50 // Polynomial Approximations for Fast Correctly Rounded Math Libraries",
51 // Dept. of Comp. Sci., Rutgets U., Technical Report DCS-TR-758, Nov. 2021.
52 // https://arxiv.org/pdf/2111.12852.pdf.
53 
54 namespace __llvm_libc {
55 
56 // Exact power of 10 in float:
57 
58 // Lookup table for log10(f) = log10(1 + n*2^(-7)) where n = 0..127.
59 static constexpr double LOG10_F[128] = {
60     0x0.0000000000000p+0, 0x1.bafd47221ed26p-9, 0x1.b9476a4fcd10fp-8,
61     0x1.49b0851443684p-7, 0x1.b5e908eb13790p-7, 0x1.10a83a8446c78p-6,
62     0x1.45f4f5acb8be0p-6, 0x1.7adc3df3b1ff8p-6, 0x1.af5f92b00e610p-6,
63     0x1.e3806acbd058fp-6, 0x1.0ba01a8170000p-5, 0x1.25502c0fc314cp-5,
64     0x1.3ed1199a5e425p-5, 0x1.58238eeb353dap-5, 0x1.71483427d2a99p-5,
65     0x1.8a3fadeb847f4p-5, 0x1.a30a9d609efeap-5, 0x1.bba9a058dfd84p-5,
66     0x1.d41d5164facb4p-5, 0x1.ec6647eb58808p-5, 0x1.02428c1f08016p-4,
67     0x1.0e3d29d81165ep-4, 0x1.1a23445501816p-4, 0x1.25f5215eb594ap-4,
68     0x1.31b3055c47118p-4, 0x1.3d5d335c53179p-4, 0x1.48f3ed1df48fbp-4,
69     0x1.5477731973e85p-4, 0x1.5fe80488af4fdp-4, 0x1.6b45df6f3e2c9p-4,
70     0x1.769140a2526fdp-4, 0x1.81ca63d05a44ap-4, 0x1.8cf183886480dp-4,
71     0x1.9806d9414a209p-4, 0x1.a30a9d609efeap-4, 0x1.adfd07416be07p-4,
72     0x1.b8de4d3ab3d98p-4, 0x1.c3aea4a5c6effp-4, 0x1.ce6e41e463da5p-4,
73     0x1.d91d5866aa99cp-4, 0x1.e3bc1ab0e19fep-4, 0x1.ee4aba610f204p-4,
74     0x1.f8c9683468191p-4, 0x1.019c2a064b486p-3, 0x1.06cbd67a6c3b6p-3,
75     0x1.0bf3d0937c41cp-3, 0x1.11142f0811357p-3, 0x1.162d082ac9d10p-3,
76     0x1.1b3e71ec94f7bp-3, 0x1.204881dee8777p-3, 0x1.254b4d35e7d3cp-3,
77     0x1.2a46e8ca7ba2ap-3, 0x1.2f3b691c5a001p-3, 0x1.3428e2540096dp-3,
78     0x1.390f6844a0b83p-3, 0x1.3def0e6dfdf85p-3, 0x1.42c7e7fe3fc02p-3,
79     0x1.479a07d3b6411p-3, 0x1.4c65807e93338p-3, 0x1.512a644296c3dp-3,
80     0x1.55e8c518b10f8p-3, 0x1.5aa0b4b0988fap-3, 0x1.5f52447255c92p-3,
81     0x1.63fd857fc49bbp-3, 0x1.68a288b60b7fcp-3, 0x1.6d415eaf0906bp-3,
82     0x1.71da17c2b7e80p-3, 0x1.766cc40889e85p-3, 0x1.7af97358b9e04p-3,
83     0x1.7f80354d952a0p-3, 0x1.84011944bcb75p-3, 0x1.887c2e605e119p-3,
84     0x1.8cf183886480dp-3, 0x1.9161276ba2978p-3, 0x1.95cb2880f45bap-3,
85     0x1.9a2f95085a45cp-3, 0x1.9e8e7b0c0d4bep-3, 0x1.a2e7e8618c2d2p-3,
86     0x1.a73beaaaa22f4p-3, 0x1.ab8a8f56677fcp-3, 0x1.afd3e3a23b680p-3,
87     0x1.b417f49ab8807p-3, 0x1.b856cf1ca3105p-3, 0x1.bc907fd5d1c40p-3,
88     0x1.c0c5134610e26p-3, 0x1.c4f495c0002a2p-3, 0x1.c91f1369eb7cap-3,
89     0x1.cd44983e9e7bdp-3, 0x1.d165300e333f7p-3, 0x1.d580e67edc43dp-3,
90     0x1.d997c70da9b47p-3, 0x1.dda9dd0f4a329p-3, 0x1.e1b733b0c7381p-3,
91     0x1.e5bfd5f83d342p-3, 0x1.e9c3cec58f807p-3, 0x1.edc328d3184afp-3,
92     0x1.f1bdeeb654901p-3, 0x1.f5b42ae08c407p-3, 0x1.f9a5e79f76ac5p-3,
93     0x1.fd932f1ddb4d6p-3, 0x1.00be05b217844p-2, 0x1.02b0432c96ff0p-2,
94     0x1.04a054e139004p-2, 0x1.068e3fa282e3dp-2, 0x1.087a0832fa7acp-2,
95     0x1.0a63b3456c819p-2, 0x1.0c4b457d3193dp-2, 0x1.0e30c36e71a7fp-2,
96     0x1.1014319e661bdp-2, 0x1.11f594839a5bdp-2, 0x1.13d4f0862b2e1p-2,
97     0x1.15b24a0004a92p-2, 0x1.178da53d1ee01p-2, 0x1.1967067bb94b8p-2,
98     0x1.1b3e71ec94f7bp-2, 0x1.1d13ebb32d7f9p-2, 0x1.1ee777e5f0dc3p-2,
99     0x1.20b91a8e76105p-2, 0x1.2288d7a9b2b64p-2, 0x1.2456b3282f786p-2,
100     0x1.2622b0ee3b79dp-2, 0x1.27ecd4d41eb67p-2, 0x1.29b522a64b609p-2,
101     0x1.2b7b9e258e422p-2, 0x1.2d404b073e27ep-2, 0x1.2f032cf56a5bep-2,
102     0x1.30c4478f0835fp-2, 0x1.32839e681fc62p-2};
103 
104 LLVM_LIBC_FUNCTION(float, log10f, (float x)) {
105   constexpr double LOG10_2 = 0x1.34413509f79ffp-2;
106 
107   using FPBits = typename fputil::FPBits<float>;
108   FPBits xbits(x);
109   double m = 0.0;
110 
111   // Exact powers of 10 and other hard-to-round cases.
112   switch (xbits.uintval()) {
113   case 0x4120'0000U: // x = 10
114     return 1.0f;
115   case 0x42c8'0000U: // x = 100
116     return 2.0f;
117   case 0x447a'0000U: // x = 1,000
118     return 3.0f;
119   case 0x461c'4000U: // x = 10,000
120     return 4.0f;
121   case 0x47c3'5000U: // x = 100,000
122     return 5.0f;
123   case 0x4974'2400U: // x = 1,000,000
124     return 6.0f;
125   case 0x4b18'9680U: // x = 10,000,000
126     return 7.0f;
127   case 0x4cbe'bc20U: // x = 100,000,000
128     return 8.0f;
129   case 0x4e6e'6b28U: // x = 1,000,000,000
130     return 9.0f;
131   case 0x5015'02f9U: // x = 10,000,000,000
132     return 10.0f;
133   case 0x4f13'4f83U: // x = 2471461632.0
134     if (fputil::get_round() == FE_UPWARD)
135       return 0x1.2c9314p+3f;
136     break;
137   case 0x7956'ba5eU: { // x = 69683218960000541503257137270226944.0
138     int round_mode = fputil::get_round();
139     if (round_mode == FE_DOWNWARD || round_mode == FE_TOWARDZERO)
140       return 0x1.16bebap+5f;
141     break;
142   }
143   }
144 
145   if (xbits.uintval() < FPBits::MIN_NORMAL ||
146       xbits.uintval() > FPBits::MAX_NORMAL) {
147     if (xbits.is_zero()) {
148       return static_cast<float>(FPBits::neg_inf());
149     }
150     if (xbits.get_sign() && !xbits.is_nan()) {
151       return FPBits::build_nan(1 << (fputil::MantissaWidth<float>::VALUE - 1));
152     }
153     if (xbits.is_inf_or_nan()) {
154       return x;
155     }
156     // Normalize denormal inputs.
157     xbits.set_val(xbits.get_val() * 0x1.0p23f);
158     m -= 23.0;
159   }
160 
161   m += static_cast<double>(xbits.get_exponent());
162   // Set bits to 1.m
163   xbits.set_unbiased_exponent(0x7F);
164   int f_index = xbits.get_mantissa() >> 16;
165 
166   FPBits f = xbits;
167   f.bits &= ~0x0000'FFFF;
168 
169   double d = static_cast<float>(xbits) - static_cast<float>(f);
170   d *= ONE_OVER_F[f_index];
171 
172   double extra_factor = fputil::multiply_add(m, LOG10_2, LOG10_F[f_index]);
173 
174   double r = fputil::polyeval(d, extra_factor, 0x1.bcb7b1526e4c5p-2,
175                               -0x1.bcb7b1518a5e9p-3, 0x1.287a72a6f716p-3,
176                               -0x1.bcadb40b85565p-4, 0x1.5e0bc97f97e22p-4);
177 
178   return static_cast<float>(r);
179 }
180 
181 } // namespace __llvm_libc
182