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