1//===-- Implementation header for log2f -------------------------*- C++ -*-===//
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_LOG2F_H
10#define LLVM_LIBC_SRC___SUPPORT_MATH_LOG2F_H
11
12#include "common_constants.h" // Lookup table for (1/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
22// This is a correctly-rounded algorithm for log2(x) in single precision with
23// round-to-nearest, tie-to-even mode from 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, log2(x) = m + log2(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// log2(1.mant) = log2(f) + log2(1.mant / f)
36// = log2(f) + log2(1 + d/f)
37// ~ log2(f) + P(d/f)
38// since d/f is sufficiently small.
39// log2(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// papers:
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, Jan. 16-22, 2022.
51// https://people.cs.rutgers.edu/~sn349/papers/rlibmall-popl-2022.pdf
52// Aanjaneya, M., Lim, J., and Nagarakatte, S., "RLibm-Prog: Progressive
53// Polynomial Approximations for Fast Correctly Rounded Math Libraries",
54// Dept. of Comp. Sci., Rutgets U., Technical Report DCS-TR-758, Nov. 2021.
55// https://arxiv.org/pdf/2111.12852.pdf.
56
57namespace LIBC_NAMESPACE_DECL {
58
59namespace math {
60
61LIBC_INLINE float log2f(float x) {
62 using namespace common_constants_internal;
63 using FPBits = typename fputil::FPBits<float>;
64
65 FPBits xbits(x);
66 uint32_t x_u = xbits.uintval();
67
68 // Hard to round value(s).
69 using fputil::round_result_slightly_up;
70
71 int m = -FPBits::EXP_BIAS;
72
73 // log2(1.0f) = 0.0f.
74 if (LIBC_UNLIKELY(x_u == 0x3f80'0000U))
75 return 0.0f;
76
77 // Exceptional inputs.
78 if (LIBC_UNLIKELY(x_u < FPBits::min_normal().uintval() ||
79 x_u > FPBits::max_normal().uintval())) {
80 if (x == 0.0f) {
81 fputil::set_errno_if_required(ERANGE);
82 fputil::raise_except_if_required(FE_DIVBYZERO);
83 return FPBits::inf(sign: Sign::NEG).get_val();
84 }
85 if (xbits.is_neg() && !xbits.is_nan()) {
86 fputil::set_errno_if_required(EDOM);
87 fputil::raise_except_if_required(FE_INVALID);
88 return FPBits::quiet_nan().get_val();
89 }
90 if (xbits.is_inf_or_nan()) {
91 return x;
92 }
93 // Normalize denormal inputs.
94 xbits = FPBits(xbits.get_val() * 0x1.0p23f);
95 m -= 23;
96 }
97
98 m += xbits.get_biased_exponent();
99 int index = xbits.get_mantissa() >> 16;
100 // Set bits to 1.m
101 xbits.set_biased_exponent(0x7F);
102
103 float u = xbits.get_val();
104#ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT
105 double v =
106 static_cast<double>(fputil::multiply_add(u, R[index], -1.0f)); // Exact.
107#else
108 double v =
109 fputil::multiply_add(x: static_cast<double>(u), y: RD[index], z: -1.0); // Exact
110#endif // LIBC_TARGET_CPU_HAS_FMA_FLOAT
111
112 double extra_factor = static_cast<double>(m) + LOG2_R[index];
113
114 // Degree-5 polynomial approximation of log2 generated by Sollya with:
115 // > P = fpminimax(log2(1 + x)/x, 4, [|1, D...|], [-2^-8, 2^-7]);
116 constexpr double COEFFS[5] = {0x1.71547652b8133p0, -0x1.71547652d1e33p-1,
117 0x1.ec70a098473dep-2, -0x1.7154c5ccdf121p-2,
118 0x1.2514fd90a130ap-2};
119
120 double vsq = v * v; // Exact
121 double c0 = fputil::multiply_add(x: v, y: COEFFS[0], z: extra_factor);
122 double c1 = fputil::multiply_add(x: v, y: COEFFS[2], z: COEFFS[1]);
123 double c2 = fputil::multiply_add(x: v, y: COEFFS[4], z: COEFFS[3]);
124
125 double r = fputil::polyeval(x: vsq, a0: c0, a: c1, a: c2);
126
127 return static_cast<float>(r);
128}
129
130} // namespace math
131} // namespace LIBC_NAMESPACE_DECL
132
133#endif // LLVM_LIBC_SRC___SUPPORT_MATH_LOG2F_H
134