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