//===-- Single-precision log2(x) function ---------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #include "src/math/log2f.h" #include "common_constants.h" // Lookup table for (1/f) #include "src/__support/FPUtil/FEnvImpl.h" #include "src/__support/FPUtil/FPBits.h" #include "src/__support/FPUtil/PolyEval.h" #include "src/__support/FPUtil/except_value_utils.h" #include "src/__support/FPUtil/multiply_add.h" #include "src/__support/common.h" #include "src/__support/macros/config.h" #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY // This is a correctly-rounded algorithm for log2(x) in single precision with // round-to-nearest, tie-to-even mode from the RLIBM project at: // https://people.cs.rutgers.edu/~sn349/rlibm // Step 1 - Range reduction: // For x = 2^m * 1.mant, log2(x) = m + log2(1.m) // If x is denormal, we normalize it by multiplying x by 2^23 and subtracting // m by 23. // Step 2 - Another range reduction: // To compute log(1.mant), let f be the highest 8 bits including the hidden // bit, and d be the difference (1.mant - f), i.e. the remaining 16 bits of the // mantissa. Then we have the following approximation formula: // log2(1.mant) = log2(f) + log2(1.mant / f) // = log2(f) + log2(1 + d/f) // ~ log2(f) + P(d/f) // since d/f is sufficiently small. // log2(f) and 1/f are then stored in two 2^7 = 128 entries look-up tables. // Step 3 - Polynomial approximation: // To compute P(d/f), we use a single degree-5 polynomial in double precision // which provides correct rounding for all but few exception values. // For more detail about how this polynomial is obtained, please refer to the // papers: // Lim, J. and Nagarakatte, S., "One Polynomial Approximation to Produce // Correctly Rounded Results of an Elementary Function for Multiple // Representations and Rounding Modes", Proceedings of the 49th ACM SIGPLAN // Symposium on Principles of Programming Languages (POPL-2022), Philadelphia, // USA, Jan. 16-22, 2022. // https://people.cs.rutgers.edu/~sn349/papers/rlibmall-popl-2022.pdf // Aanjaneya, M., Lim, J., and Nagarakatte, S., "RLibm-Prog: Progressive // Polynomial Approximations for Fast Correctly Rounded Math Libraries", // Dept. of Comp. Sci., Rutgets U., Technical Report DCS-TR-758, Nov. 2021. // https://arxiv.org/pdf/2111.12852.pdf. namespace LIBC_NAMESPACE_DECL { LLVM_LIBC_FUNCTION(float, log2f, (float x)) { … } } // namespace LIBC_NAMESPACE_DECL