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