brintos

brintos / llvm-project-archived public Read only

0
0
Text · 5.3 KiB · 5b60323 Raw
155 lines · cpp
1//===-- Half-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/log2f16.h"10#include "hdr/errno_macros.h"11#include "hdr/fenv_macros.h"12#include "src/__support/FPUtil/FEnvImpl.h"13#include "src/__support/FPUtil/FPBits.h"14#include "src/__support/FPUtil/PolyEval.h"15#include "src/__support/FPUtil/cast.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"21#include "src/__support/macros/properties/cpu_features.h"22#include "src/__support/math/expxf16_utils.h"23 24namespace LIBC_NAMESPACE_DECL {25 26#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS27#ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT28static constexpr size_t N_LOG2F16_EXCEPTS = 2;29#else30static constexpr size_t N_LOG2F16_EXCEPTS = 9;31#endif32 33static constexpr fputil::ExceptValues<float16, N_LOG2F16_EXCEPTS>34    LOG2F16_EXCEPTS = {{35// (input, RZ output, RU offset, RD offset, RN offset)36#ifndef LIBC_TARGET_CPU_HAS_FMA_FLOAT37        // x = 0x1.224p-1, log2f16(x) = -0x1.a34p-1 (RZ)38        {0x3889U, 0xba8dU, 0U, 1U, 0U},39        // x = 0x1.e34p-1, log2f16(x) = -0x1.558p-4 (RZ)40        {0x3b8dU, 0xad56U, 0U, 1U, 0U},41#endif42        // x = 0x1.e8cp-1, log2f16(x) = -0x1.128p-4 (RZ)43        {0x3ba3U, 0xac4aU, 0U, 1U, 0U},44#ifndef LIBC_TARGET_CPU_HAS_FMA_FLOAT45        // x = 0x1.f98p-1, log2f16(x) = -0x1.2ep-6 (RZ)46        {0x3be6U, 0xa4b8U, 0U, 1U, 0U},47        // x = 0x1.facp-1, log2f16(x) = -0x1.e7p-7 (RZ)48        {0x3bebU, 0xa39cU, 0U, 1U, 1U},49#endif50        // x = 0x1.fb4p-1, log2f16(x) = -0x1.b88p-7 (RZ)51        {0x3bedU, 0xa2e2U, 0U, 1U, 1U},52#ifndef LIBC_TARGET_CPU_HAS_FMA_FLOAT53        // x = 0x1.fecp-1, log2f16(x) = -0x1.cep-9 (RZ)54        {0x3bfbU, 0x9b38U, 0U, 1U, 1U},55        // x = 0x1.ffcp-1, log2f16(x) = -0x1.714p-11 (RZ)56        {0x3bffU, 0x91c5U, 0U, 1U, 1U},57        // x = 0x1.224p+0, log2f16(x) = 0x1.72cp-3 (RZ)58        {0x3c89U, 0x31cbU, 1U, 0U, 1U},59#endif60    }};61#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS62 63LLVM_LIBC_FUNCTION(float16, log2f16, (float16 x)) {64  using namespace math::expxf16_internal;65  using FPBits = fputil::FPBits<float16>;66  FPBits x_bits(x);67 68  uint16_t x_u = x_bits.uintval();69 70  // If x <= 0, or x is 1, or x is +inf, or x is NaN.71  if (LIBC_UNLIKELY(x_u == 0U || x_u == 0x3c00U || x_u >= 0x7c00U)) {72    // log2(NaN) = NaN73    if (x_bits.is_nan()) {74      if (x_bits.is_signaling_nan()) {75        fputil::raise_except_if_required(FE_INVALID);76        return FPBits::quiet_nan().get_val();77      }78 79      return x;80    }81 82    // log2(+/-0) = −inf83    if ((x_u & 0x7fffU) == 0U) {84      fputil::raise_except_if_required(FE_DIVBYZERO);85      return FPBits::inf(Sign::NEG).get_val();86    }87 88    if (x_u == 0x3c00U)89      return FPBits::zero().get_val();90 91    // When x < 0.92    if (x_u > 0x8000U) {93      fputil::set_errno_if_required(EDOM);94      fputil::raise_except_if_required(FE_INVALID);95      return FPBits::quiet_nan().get_val();96    }97 98    // log2(+inf) = +inf99    return FPBits::inf().get_val();100  }101 102#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS103  if (auto r = LOG2F16_EXCEPTS.lookup(x_u); LIBC_UNLIKELY(r.has_value()))104    return r.value();105#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS106 107  // To compute log2(x), we perform the following range reduction:108  //   x = 2^m * 1.mant,109  //   log2(x) = m + log2(1.mant).110  // To compute log2(1.mant), let f be the highest 6 bits including the hidden111  // bit, and d be the difference (1.mant - f), i.e., the remaining 5 bits of112  // the mantissa, then:113  //   log2(1.mant) = log2(f) + log2(1.mant / f)114  //                = log2(f) + log2(1 + d/f)115  // since d/f is sufficiently small.116  // We store log2(f) and 1/f in the lookup tables LOG2F_F and ONE_OVER_F_F117  // respectively.118 119  int m = -FPBits::EXP_BIAS;120 121  // When x is subnormal, normalize it.122  if ((x_u & FPBits::EXP_MASK) == 0U) {123    // Can't pass an integer to fputil::cast directly.124    constexpr float NORMALIZE_EXP = 1U << FPBits::FRACTION_LEN;125    x_bits = FPBits(x_bits.get_val() * fputil::cast<float16>(NORMALIZE_EXP));126    x_u = x_bits.uintval();127    m -= FPBits::FRACTION_LEN;128  }129 130  uint16_t mant = x_bits.get_mantissa();131  // Leading 10 - 5 = 5 bits of the mantissa.132  int f = mant >> 5;133  // Unbiased exponent.134  m += x_u >> FPBits::FRACTION_LEN;135 136  // Set bits to 1.mant instead of 2^m * 1.mant.137  x_bits.set_biased_exponent(FPBits::EXP_BIAS);138  float mant_f = x_bits.get_val();139  // v = 1.mant * 1/f - 1 = d/f140  float v = fputil::multiply_add(mant_f, ONE_OVER_F_F[f], -1.0f);141 142  // Degree-3 minimax polynomial generated by Sollya with the following143  // commands:144  //   > display = hexadecimal;145  //   > P = fpminimax(log2(1 + x)/x, 2, [|SG...|], [-2^-5, 2^-5]);146  //   > x * P;147  float log2p1_d_over_f =148      v * fputil::polyeval(v, 0x1.715476p+0f, -0x1.71771ap-1f, 0x1.ecb38ep-2f);149  // log2(1.mant) = log2(f) + log2(1 + d/f)150  float log2_1_mant = LOG2F_F[f] + log2p1_d_over_f;151  return fputil::cast<float16>(static_cast<float>(m) + log2_1_mant);152}153 154} // namespace LIBC_NAMESPACE_DECL155