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