1//===-- Single-precision log2(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/log2f.h"
10#include "common_constants.h" // Lookup table for (1/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
19// This is a correctly-rounded algorithm for log2(x) in single precision with
20// round-to-nearest, tie-to-even mode from the RLIBM project at:
21// https://people.cs.rutgers.edu/~sn349/rlibm
22
23// Step 1 - Range reduction:
24// For x = 2^m * 1.mant, log2(x) = m + log2(1.m)
25// If x is denormal, we normalize it by multiplying x by 2^23 and subtracting
26// m by 23.
27
28// Step 2 - Another range reduction:
29// To compute log(1.mant), let f be the highest 8 bits including the hidden
30// bit, and d be the difference (1.mant - f), i.e. the remaining 16 bits of the
31// mantissa. Then we have the following approximation formula:
32// log2(1.mant) = log2(f) + log2(1.mant / f)
33// = log2(f) + log2(1 + d/f)
34// ~ log2(f) + P(d/f)
35// since d/f is sufficiently small.
36// log2(f) and 1/f are then stored in two 2^7 = 128 entries look-up tables.
37
38// Step 3 - Polynomial approximation:
39// To compute P(d/f), we use a single degree-5 polynomial in double precision
40// which provides correct rounding for all but few exception values.
41// For more detail about how this polynomial is obtained, please refer to the
42// papers:
43// Lim, J. and Nagarakatte, S., "One Polynomial Approximation to Produce
44// Correctly Rounded Results of an Elementary Function for Multiple
45// Representations and Rounding Modes", Proceedings of the 49th ACM SIGPLAN
46// Symposium on Principles of Programming Languages (POPL-2022), Philadelphia,
47// USA, Jan. 16-22, 2022.
48// https://people.cs.rutgers.edu/~sn349/papers/rlibmall-popl-2022.pdf
49// Aanjaneya, M., Lim, J., and Nagarakatte, S., "RLibm-Prog: Progressive
50// Polynomial Approximations for Fast Correctly Rounded Math Libraries",
51// Dept. of Comp. Sci., Rutgets U., Technical Report DCS-TR-758, Nov. 2021.
52// https://arxiv.org/pdf/2111.12852.pdf.
53
54namespace LIBC_NAMESPACE {
55
56LLVM_LIBC_FUNCTION(float, log2f, (float x)) {
57 using FPBits = typename fputil::FPBits<float>;
58
59 FPBits xbits(x);
60 uint32_t x_u = xbits.uintval();
61
62 // Hard to round value(s).
63 using fputil::round_result_slightly_up;
64
65 int m = -FPBits::EXP_BIAS;
66
67 // log2(1.0f) = 0.0f.
68 if (LIBC_UNLIKELY(x_u == 0x3f80'0000U))
69 return 0.0f;
70
71 // Exceptional inputs.
72 if (LIBC_UNLIKELY(x_u < FPBits::min_normal().uintval() ||
73 x_u > FPBits::max_normal().uintval())) {
74 if (xbits.is_zero()) {
75 fputil::set_errno_if_required(ERANGE);
76 fputil::raise_except_if_required(FE_DIVBYZERO);
77 return FPBits::inf(sign: Sign::NEG).get_val();
78 }
79 if (xbits.is_neg() && !xbits.is_nan()) {
80 fputil::set_errno_if_required(EDOM);
81 fputil::raise_except(FE_INVALID);
82 return FPBits::quiet_nan().get_val();
83 }
84 if (xbits.is_inf_or_nan()) {
85 return x;
86 }
87 // Normalize denormal inputs.
88 xbits = FPBits(xbits.get_val() * 0x1.0p23f);
89 m -= 23;
90 }
91
92 m += xbits.get_biased_exponent();
93 int index = xbits.get_mantissa() >> 16;
94 // Set bits to 1.m
95 xbits.set_biased_exponent(0x7F);
96
97 float u = xbits.get_val();
98 double v;
99#ifdef LIBC_TARGET_CPU_HAS_FMA
100 v = static_cast<double>(fputil::multiply_add(x: u, y: R[index], z: -1.0f)); // Exact.
101#else
102 v = fputil::multiply_add(static_cast<double>(u), RD[index], -1.0); // Exact
103#endif // LIBC_TARGET_CPU_HAS_FMA
104
105 double extra_factor = static_cast<double>(m) + LOG2_R[index];
106
107 // Degree-5 polynomial approximation of log2 generated by Sollya with:
108 // > P = fpminimax(log2(1 + x)/x, 4, [|1, D...|], [-2^-8, 2^-7]);
109 constexpr double COEFFS[5] = {0x1.71547652b8133p0, -0x1.71547652d1e33p-1,
110 0x1.ec70a098473dep-2, -0x1.7154c5ccdf121p-2,
111 0x1.2514fd90a130ap-2};
112
113 double vsq = v * v; // Exact
114 double c0 = fputil::multiply_add(x: v, y: COEFFS[0], z: extra_factor);
115 double c1 = fputil::multiply_add(x: v, y: COEFFS[2], z: COEFFS[1]);
116 double c2 = fputil::multiply_add(x: v, y: COEFFS[4], z: COEFFS[3]);
117
118 double r = fputil::polyeval(x: vsq, a0: c0, a: c1, a: c2);
119
120 return static_cast<float>(r);
121}
122
123} // namespace LIBC_NAMESPACE
124

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