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
53namespace LIBC_NAMESPACE_DECL {
54
55LLVM_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#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
70 // Hard-to-round cases.
71 switch (x_u) {
72 case 0x3f7f4d6fU: // x = 0x1.fe9adep-1f
73 return round_result_slightly_up(-0x1.659ec8p-9f);
74 case 0x41178febU: // x = 0x1.2f1fd6p+3f
75 return round_result_slightly_up(0x1.1fcbcep+1f);
76#ifdef LIBC_TARGET_CPU_HAS_FMA
77 case 0x3f800000U: // x = 1.0f
78 return 0.0f;
79#else
80 case 0x1e88452dU: // x = 0x1.108a5ap-66f
81 return round_result_slightly_up(-0x1.6d7b18p+5f);
82#endif // LIBC_TARGET_CPU_HAS_FMA
83 }
84#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
85 // Subnormal inputs.
86 if (LIBC_UNLIKELY(x_u < FPBits::min_normal().uintval())) {
87 if (x == 0.0f) {
88 // Return -inf and raise FE_DIVBYZERO
89 fputil::set_errno_if_required(ERANGE);
90 fputil::raise_except_if_required(FE_DIVBYZERO);
91 return FPBits::inf(Sign::NEG).get_val();
92 }
93 // Normalize denormal inputs.
94 xbits = FPBits(xbits.get_val() * 0x1.0p23f);
95 m -= 23;
96 x_u = xbits.uintval();
97 }
98 } else {
99#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
100 // Hard-to-round cases.
101 switch (x_u) {
102 case 0x4c5d65a5U: // x = 0x1.bacb4ap+25f
103 return round_result_slightly_down(0x1.1e0696p+4f);
104 case 0x65d890d3U: // x = 0x1.b121a6p+76f
105 return round_result_slightly_down(0x1.a9a3f2p+5f);
106 case 0x6f31a8ecU: // x = 0x1.6351d8p+95f
107 return round_result_slightly_down(0x1.08b512p+6f);
108 case 0x7a17f30aU: // x = 0x1.2fe614p+117f
109 return round_result_slightly_up(0x1.451436p+6f);
110#ifndef LIBC_TARGET_CPU_HAS_FMA_DOUBLE
111 case 0x500ffb03U: // x = 0x1.1ff606p+33f
112 return round_result_slightly_up(0x1.6fdd34p+4f);
113 case 0x5cd69e88U: // x = 0x1.ad3d1p+58f
114 return round_result_slightly_up(0x1.45c146p+5f);
115 case 0x5ee8984eU: // x = 0x1.d1309cp+62f;
116 return round_result_slightly_up(0x1.5c9442p+5f);
117#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE
118 }
119#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
120 // Exceptional inputs.
121 if (LIBC_UNLIKELY(x_u > FPBits::max_normal().uintval())) {
122 if (x_u == 0x8000'0000U) {
123 // Return -inf and raise FE_DIVBYZERO
124 fputil::set_errno_if_required(ERANGE);
125 fputil::raise_except_if_required(FE_DIVBYZERO);
126 return FPBits::inf(Sign::NEG).get_val();
127 }
128 if (xbits.is_neg() && !xbits.is_nan()) {
129 // Return NaN and raise FE_INVALID
130 fputil::set_errno_if_required(EDOM);
131 fputil::raise_except_if_required(FE_INVALID);
132 return FPBits::quiet_nan().get_val();
133 }
134 // x is +inf or nan
135 if (xbits.is_signaling_nan()) {
136 fputil::raise_except_if_required(FE_INVALID);
137 return FPBits::quiet_nan().get_val();
138 }
139
140 return x;
141 }
142 }
143
144#ifndef LIBC_TARGET_CPU_HAS_FMA
145 // Returning the correct +0 when x = 1.0 for non-FMA targets with FE_DOWNWARD
146 // rounding mode.
147 if (LIBC_UNLIKELY((x_u & 0x007f'ffffU) == 0))
148 return static_cast<float>(
149 static_cast<double>(m + xbits.get_biased_exponent()) * LOG_2);
150#endif // LIBC_TARGET_CPU_HAS_FMA
151
152 uint32_t mant = xbits.get_mantissa();
153 // Extract 7 leading fractional bits of the mantissa
154 int index = mant >> 16;
155 // Add unbiased exponent. Add an extra 1 if the 7 leading fractional bits are
156 // all 1's.
157 m += static_cast<int>((x_u + (1 << 16)) >> 23);
158
159 // Set bits to 1.m
160 xbits.set_biased_exponent(0x7F);
161
162 float u = xbits.get_val();
163 double v;
164#ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT
165 v = static_cast<double>(fputil::multiply_add(u, R[index], -1.0f)); // Exact.
166#else
167 v = fputil::multiply_add(static_cast<double>(u), RD[index], -1.0); // Exact
168#endif // LIBC_TARGET_CPU_HAS_FMA_FLOAT
169
170 // Degree-5 polynomial approximation of log generated by Sollya with:
171 // > P = fpminimax(log(1 + x)/x, 4, [|1, D...|], [-2^-8, 2^-7]);
172 constexpr double COEFFS[4] = {-0x1.000000000fe63p-1, 0x1.555556e963c16p-2,
173 -0x1.000028dedf986p-2, 0x1.966681bfda7f7p-3};
174 double v2 = v * v; // Exact
175 double p2 = fputil::multiply_add(v, COEFFS[3], COEFFS[2]);
176 double p1 = fputil::multiply_add(v, COEFFS[1], COEFFS[0]);
177 double p0 = LOG_R[index] + v;
178 double r = fputil::multiply_add(static_cast<double>(m), LOG_2,
179 fputil::polyeval(v2, p0, p1, p2));
180 return static_cast<float>(r);
181}
182
183} // namespace LIBC_NAMESPACE_DECL
184

source code of libc/src/math/generic/logf.cpp