brintos

brintos / llvm-project-archived public Read only

0
0
Text · 7.1 KiB · 4d2947d Raw
185 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "src/math/logf.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/macros/properties/cpu_features.h"19#include "src/__support/math/common_constants.h" // Lookup table for (1/f) and log(f)20 21// This is an algorithm for log(x) in single precision which is correctly22// rounded for all rounding modes, based on the implementation of log(x) from23// the RLIBM project at:24// https://people.cs.rutgers.edu/~sn349/rlibm25 26// Step 1 - Range reduction:27//   For x = 2^m * 1.mant, log(x) = m * log(2) + log(1.m)28//   If x is denormal, we normalize it by multiplying x by 2^23 and subtracting29//   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 hidden33// bit, and d be the difference (1.mant - f), i.e. the remaining 16 bits of the34// mantissa. Then we have the following approximation formula:35//   log(1.mant) = log(f) + log(1.mant / f)36//               = log(f) + log(1 + d/f)37//               ~ log(f) + P(d/f)38// since d/f is sufficiently small.39//   log(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 precision43// which provides correct rounding for all but few exception values.44//   For more detail about how this polynomial is obtained, please refer to the45// paper:46//   Lim, J. and Nagarakatte, S., "One Polynomial Approximation to Produce47// Correctly Rounded Results of an Elementary Function for Multiple48// Representations and Rounding Modes", Proceedings of the 49th ACM SIGPLAN49// Symposium on Principles of Programming Languages (POPL-2022), Philadelphia,50// USA, January 16-22, 2022.51// https://people.cs.rutgers.edu/~sn349/papers/rlibmall-popl-2022.pdf52 53namespace LIBC_NAMESPACE_DECL {54 55LLVM_LIBC_FUNCTION(float, logf, (float x)) {56  using namespace common_constants_internal;57  constexpr double LOG_2 = 0x1.62e42fefa39efp-1;58  using FPBits = typename fputil::FPBits<float>;59 60  FPBits xbits(x);61  uint32_t x_u = xbits.uintval();62 63  int m = -FPBits::EXP_BIAS;64 65  using fputil::round_result_slightly_down;66  using fputil::round_result_slightly_up;67 68  // Small inputs69  if (x_u < 0x4c5d65a5U) {70#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS71    // Hard-to-round cases.72    switch (x_u) {73    case 0x3f7f4d6fU: // x = 0x1.fe9adep-1f74      return round_result_slightly_up(-0x1.659ec8p-9f);75    case 0x41178febU: // x = 0x1.2f1fd6p+3f76      return round_result_slightly_up(0x1.1fcbcep+1f);77#ifdef LIBC_TARGET_CPU_HAS_FMA78    case 0x3f800000U: // x = 1.0f79      return 0.0f;80#else81    case 0x1e88452dU: // x = 0x1.108a5ap-66f82      return round_result_slightly_up(-0x1.6d7b18p+5f);83#endif // LIBC_TARGET_CPU_HAS_FMA84    }85#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS86    // Subnormal inputs.87    if (LIBC_UNLIKELY(x_u < FPBits::min_normal().uintval())) {88      if (x == 0.0f) {89        // Return -inf and raise FE_DIVBYZERO90        fputil::set_errno_if_required(ERANGE);91        fputil::raise_except_if_required(FE_DIVBYZERO);92        return FPBits::inf(Sign::NEG).get_val();93      }94      // Normalize denormal inputs.95      xbits = FPBits(xbits.get_val() * 0x1.0p23f);96      m -= 23;97      x_u = xbits.uintval();98    }99  } else {100#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS101    // Hard-to-round cases.102    switch (x_u) {103    case 0x4c5d65a5U: // x = 0x1.bacb4ap+25f104      return round_result_slightly_down(0x1.1e0696p+4f);105    case 0x65d890d3U: // x = 0x1.b121a6p+76f106      return round_result_slightly_down(0x1.a9a3f2p+5f);107    case 0x6f31a8ecU: // x = 0x1.6351d8p+95f108      return round_result_slightly_down(0x1.08b512p+6f);109    case 0x7a17f30aU: // x = 0x1.2fe614p+117f110      return round_result_slightly_up(0x1.451436p+6f);111#ifndef LIBC_TARGET_CPU_HAS_FMA_DOUBLE112    case 0x500ffb03U: // x = 0x1.1ff606p+33f113      return round_result_slightly_up(0x1.6fdd34p+4f);114    case 0x5cd69e88U: // x = 0x1.ad3d1p+58f115      return round_result_slightly_up(0x1.45c146p+5f);116    case 0x5ee8984eU: // x = 0x1.d1309cp+62f;117      return round_result_slightly_up(0x1.5c9442p+5f);118#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE119    }120#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS121    // Exceptional inputs.122    if (LIBC_UNLIKELY(x_u > FPBits::max_normal().uintval())) {123      if (x_u == 0x8000'0000U) {124        // Return -inf and raise FE_DIVBYZERO125        fputil::set_errno_if_required(ERANGE);126        fputil::raise_except_if_required(FE_DIVBYZERO);127        return FPBits::inf(Sign::NEG).get_val();128      }129      if (xbits.is_neg() && !xbits.is_nan()) {130        // Return NaN and raise FE_INVALID131        fputil::set_errno_if_required(EDOM);132        fputil::raise_except_if_required(FE_INVALID);133        return FPBits::quiet_nan().get_val();134      }135      // x is +inf or nan136      if (xbits.is_signaling_nan()) {137        fputil::raise_except_if_required(FE_INVALID);138        return FPBits::quiet_nan().get_val();139      }140 141      return x;142    }143  }144 145#ifndef LIBC_TARGET_CPU_HAS_FMA146  // Returning the correct +0 when x = 1.0 for non-FMA targets with FE_DOWNWARD147  // rounding mode.148  if (LIBC_UNLIKELY((x_u & 0x007f'ffffU) == 0))149    return static_cast<float>(150        static_cast<double>(m + xbits.get_biased_exponent()) * LOG_2);151#endif // LIBC_TARGET_CPU_HAS_FMA152 153  uint32_t mant = xbits.get_mantissa();154  // Extract 7 leading fractional bits of the mantissa155  int index = mant >> 16;156  // Add unbiased exponent. Add an extra 1 if the 7 leading fractional bits are157  // all 1's.158  m += static_cast<int>((x_u + (1 << 16)) >> 23);159 160  // Set bits to 1.m161  xbits.set_biased_exponent(0x7F);162 163  float u = xbits.get_val();164  double v;165#ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT166  v = static_cast<double>(fputil::multiply_add(u, R[index], -1.0f)); // Exact.167#else168  v = fputil::multiply_add(static_cast<double>(u), RD[index], -1.0); // Exact169#endif // LIBC_TARGET_CPU_HAS_FMA_FLOAT170 171  // Degree-5 polynomial approximation of log generated by Sollya with:172  // > P = fpminimax(log(1 + x)/x, 4, [|1, D...|], [-2^-8, 2^-7]);173  constexpr double COEFFS[4] = {-0x1.000000000fe63p-1, 0x1.555556e963c16p-2,174                                -0x1.000028dedf986p-2, 0x1.966681bfda7f7p-3};175  double v2 = v * v; // Exact176  double p2 = fputil::multiply_add(v, COEFFS[3], COEFFS[2]);177  double p1 = fputil::multiply_add(v, COEFFS[1], COEFFS[0]);178  double p0 = LOG_R[index] + v;179  double r = fputil::multiply_add(static_cast<double>(m), LOG_2,180                                  fputil::polyeval(v2, p0, p1, p2));181  return static_cast<float>(r);182}183 184} // namespace LIBC_NAMESPACE_DECL185