xref: /aosp_15_r20/external/llvm-libc/src/math/generic/logf.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Single-precision log(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/logf.h"
10 #include "common_constants.h" // Lookup table for (1/f) and log(f)
11 #include "src/__support/FPUtil/FEnvImpl.h"
12 #include "src/__support/FPUtil/FPBits.h"
13 #include "src/__support/FPUtil/PolyEval.h"
14 #include "src/__support/FPUtil/except_value_utils.h"
15 #include "src/__support/FPUtil/multiply_add.h"
16 #include "src/__support/common.h"
17 #include "src/__support/macros/config.h"
18 #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
19 #include "src/__support/macros/properties/cpu_features.h"
20 
21 // This is an algorithm for log(x) in single precision which is correctly
22 // rounded for all rounding modes, based on the implementation of log(x) from
23 // the RLIBM project at:
24 // https://people.cs.rutgers.edu/~sn349/rlibm
25 
26 // Step 1 - Range reduction:
27 //   For x = 2^m * 1.mant, log(x) = m * log(2) + log(1.m)
28 //   If x is denormal, we normalize it by multiplying x by 2^23 and subtracting
29 //   m by 23.
30 
31 // Step 2 - Another range reduction:
32 //   To compute log(1.mant), let f be the highest 8 bits including the hidden
33 // bit, and d be the difference (1.mant - f), i.e. the remaining 16 bits of the
34 // mantissa. Then we have the following approximation formula:
35 //   log(1.mant) = log(f) + log(1.mant / f)
36 //               = log(f) + log(1 + d/f)
37 //               ~ log(f) + P(d/f)
38 // since d/f is sufficiently small.
39 //   log(f) and 1/f are then stored in two 2^7 = 128 entries look-up tables.
40 
41 // Step 3 - Polynomial approximation:
42 //   To compute P(d/f), we use a single degree-5 polynomial in double precision
43 // which provides correct rounding for all but few exception values.
44 //   For more detail about how this polynomial is obtained, please refer to the
45 // paper:
46 //   Lim, J. and Nagarakatte, S., "One Polynomial Approximation to Produce
47 // Correctly Rounded Results of an Elementary Function for Multiple
48 // Representations and Rounding Modes", Proceedings of the 49th ACM SIGPLAN
49 // Symposium on Principles of Programming Languages (POPL-2022), Philadelphia,
50 // USA, January 16-22, 2022.
51 // https://people.cs.rutgers.edu/~sn349/papers/rlibmall-popl-2022.pdf
52 
53 namespace LIBC_NAMESPACE_DECL {
54 
55 LLVM_LIBC_FUNCTION(float, logf, (float x)) {
56   constexpr double LOG_2 = 0x1.62e42fefa39efp-1;
57   using FPBits = typename fputil::FPBits<float>;
58 
59   FPBits xbits(x);
60   uint32_t x_u = xbits.uintval();
61 
62   int m = -FPBits::EXP_BIAS;
63 
64   using fputil::round_result_slightly_down;
65   using fputil::round_result_slightly_up;
66 
67   // Small inputs
68   if (x_u < 0x4c5d65a5U) {
69     // Hard-to-round cases.
70     switch (x_u) {
71     case 0x3f7f4d6fU: // x = 0x1.fe9adep-1f
72       return round_result_slightly_up(-0x1.659ec8p-9f);
73     case 0x41178febU: // x = 0x1.2f1fd6p+3f
74       return round_result_slightly_up(0x1.1fcbcep+1f);
75 #ifdef LIBC_TARGET_CPU_HAS_FMA
76     case 0x3f800000U: // x = 1.0f
77       return 0.0f;
78 #else
79     case 0x1e88452dU: // x = 0x1.108a5ap-66f
80       return round_result_slightly_up(-0x1.6d7b18p+5f);
81 #endif // LIBC_TARGET_CPU_HAS_FMA
82     }
83     // Subnormal inputs.
84     if (LIBC_UNLIKELY(x_u < FPBits::min_normal().uintval())) {
85       if (x == 0.0f) {
86         // Return -inf and raise FE_DIVBYZERO
87         fputil::set_errno_if_required(ERANGE);
88         fputil::raise_except_if_required(FE_DIVBYZERO);
89         return FPBits::inf(Sign::NEG).get_val();
90       }
91       // Normalize denormal inputs.
92       xbits = FPBits(xbits.get_val() * 0x1.0p23f);
93       m -= 23;
94       x_u = xbits.uintval();
95     }
96   } else {
97     // Hard-to-round cases.
98     switch (x_u) {
99     case 0x4c5d65a5U: // x = 0x1.bacb4ap+25f
100       return round_result_slightly_down(0x1.1e0696p+4f);
101     case 0x65d890d3U: // x = 0x1.b121a6p+76f
102       return round_result_slightly_down(0x1.a9a3f2p+5f);
103     case 0x6f31a8ecU: // x = 0x1.6351d8p+95f
104       return round_result_slightly_down(0x1.08b512p+6f);
105     case 0x7a17f30aU: // x = 0x1.2fe614p+117f
106       return round_result_slightly_up(0x1.451436p+6f);
107 #ifndef LIBC_TARGET_CPU_HAS_FMA
108     case 0x500ffb03U: // x = 0x1.1ff606p+33f
109       return round_result_slightly_up(0x1.6fdd34p+4f);
110     case 0x5cd69e88U: // x = 0x1.ad3d1p+58f
111       return round_result_slightly_up(0x1.45c146p+5f);
112     case 0x5ee8984eU: // x = 0x1.d1309cp+62f;
113       return round_result_slightly_up(0x1.5c9442p+5f);
114 #endif // LIBC_TARGET_CPU_HAS_FMA
115     }
116     // Exceptional inputs.
117     if (LIBC_UNLIKELY(x_u > FPBits::max_normal().uintval())) {
118       if (x_u == 0x8000'0000U) {
119         // Return -inf and raise FE_DIVBYZERO
120         fputil::set_errno_if_required(ERANGE);
121         fputil::raise_except_if_required(FE_DIVBYZERO);
122         return FPBits::inf(Sign::NEG).get_val();
123       }
124       if (xbits.is_neg() && !xbits.is_nan()) {
125         // Return NaN and raise FE_INVALID
126         fputil::set_errno_if_required(EDOM);
127         fputil::raise_except_if_required(FE_INVALID);
128         return FPBits::quiet_nan().get_val();
129       }
130       // x is +inf or nan
131       return x;
132     }
133   }
134 
135 #ifndef LIBC_TARGET_CPU_HAS_FMA
136   // Returning the correct +0 when x = 1.0 for non-FMA targets with FE_DOWNWARD
137   // rounding mode.
138   if (LIBC_UNLIKELY((x_u & 0x007f'ffffU) == 0))
139     return static_cast<float>(
140         static_cast<double>(m + xbits.get_biased_exponent()) * LOG_2);
141 #endif // LIBC_TARGET_CPU_HAS_FMA
142 
143   uint32_t mant = xbits.get_mantissa();
144   // Extract 7 leading fractional bits of the mantissa
145   int index = mant >> 16;
146   // Add unbiased exponent. Add an extra 1 if the 7 leading fractional bits are
147   // all 1's.
148   m += static_cast<int>((x_u + (1 << 16)) >> 23);
149 
150   // Set bits to 1.m
151   xbits.set_biased_exponent(0x7F);
152 
153   float u = xbits.get_val();
154   double v;
155 #ifdef LIBC_TARGET_CPU_HAS_FMA
156   v = static_cast<double>(fputil::multiply_add(u, R[index], -1.0f)); // Exact.
157 #else
158   v = fputil::multiply_add(static_cast<double>(u), RD[index], -1.0); // Exact
159 #endif // LIBC_TARGET_CPU_HAS_FMA
160 
161   // Degree-5 polynomial approximation of log generated by Sollya with:
162   // > P = fpminimax(log(1 + x)/x, 4, [|1, D...|], [-2^-8, 2^-7]);
163   constexpr double COEFFS[4] = {-0x1.000000000fe63p-1, 0x1.555556e963c16p-2,
164                                 -0x1.000028dedf986p-2, 0x1.966681bfda7f7p-3};
165   double v2 = v * v; // Exact
166   double p2 = fputil::multiply_add(v, COEFFS[3], COEFFS[2]);
167   double p1 = fputil::multiply_add(v, COEFFS[1], COEFFS[0]);
168   double p0 = LOG_R[index] + v;
169   double r = fputil::multiply_add(static_cast<double>(m), LOG_2,
170                                   fputil::polyeval(v2, p0, p1, p2));
171   return static_cast<float>(r);
172 }
173 
174 } // namespace LIBC_NAMESPACE_DECL
175