brintos

brintos / llvm-project-archived public Read only

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