brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.6 KiB · c2231f0 Raw
142 lines · cpp
1//===-- Half-precision e^x - 1 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/expm1f16.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/FPUtil/rounding_mode.h"19#include "src/__support/common.h"20#include "src/__support/macros/config.h"21#include "src/__support/macros/optimization.h"22#include "src/__support/math/expxf16_utils.h"23 24namespace LIBC_NAMESPACE_DECL {25 26#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS27static constexpr fputil::ExceptValues<float16, 1> EXPM1F16_EXCEPTS_LO = {{28    // (input, RZ output, RU offset, RD offset, RN offset)29    // x = 0x1.564p-5, expm1f16(x) = 0x1.5d4p-5 (RZ)30    {0x2959U, 0x2975U, 1U, 0U, 1U},31}};32 33#ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT34static constexpr size_t N_EXPM1F16_EXCEPTS_HI = 2;35#else36static constexpr size_t N_EXPM1F16_EXCEPTS_HI = 3;37#endif38 39static constexpr fputil::ExceptValues<float16, N_EXPM1F16_EXCEPTS_HI>40    EXPM1F16_EXCEPTS_HI = {{41        // (input, RZ output, RU offset, RD offset, RN offset)42        // x = 0x1.c34p+0, expm1f16(x) = 0x1.34cp+2 (RZ)43        {0x3f0dU, 0x44d3U, 1U, 0U, 1U},44        // x = -0x1.e28p-3, expm1f16(x) = -0x1.adcp-3 (RZ)45        {0xb38aU, 0xb2b7U, 0U, 1U, 1U},46#ifndef LIBC_TARGET_CPU_HAS_FMA_FLOAT47        // x = 0x1.a08p-3, exp10m1f(x) = 0x1.cdcp-3 (RZ)48        {0x3282U, 0x3337U, 1U, 0U, 0U},49#endif50    }};51#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS52 53LLVM_LIBC_FUNCTION(float16, expm1f16, (float16 x)) {54  using namespace math::expxf16_internal;55  using FPBits = fputil::FPBits<float16>;56  FPBits x_bits(x);57 58  uint16_t x_u = x_bits.uintval();59  uint16_t x_abs = x_u & 0x7fffU;60 61  // When |x| <= 2^(-3), or |x| >= -11 * log(2), or x is NaN.62  if (LIBC_UNLIKELY(x_abs <= 0x3000U || x_abs >= 0x47a0U)) {63    // expm1(NaN) = NaN64    if (x_bits.is_nan()) {65      if (x_bits.is_signaling_nan()) {66        fputil::raise_except_if_required(FE_INVALID);67        return FPBits::quiet_nan().get_val();68      }69 70      return x;71    }72 73    // expm1(+/-0) = +/-074    if (x_abs == 0)75      return x;76 77    // When x >= 16 * log(2).78    if (x_bits.is_pos() && x_abs >= 0x498cU) {79      // expm1(+inf) = +inf80      if (x_bits.is_inf())81        return FPBits::inf().get_val();82 83      switch (fputil::quick_get_round()) {84      case FE_TONEAREST:85      case FE_UPWARD:86        fputil::set_errno_if_required(ERANGE);87        fputil::raise_except_if_required(FE_OVERFLOW | FE_INEXACT);88        return FPBits::inf().get_val();89      default:90        return FPBits::max_normal().get_val();91      }92    }93 94    // When x <= -11 * log(2).95    if (x_u >= 0xc7a0U) {96      // expm1(-inf) = -197      if (x_bits.is_inf())98        return FPBits::one(Sign::NEG).get_val();99 100      // When x > -0x1.0ap+3, round(expm1(x), HP, RN) = -1.101      if (x_u > 0xc828U)102        return fputil::round_result_slightly_up(103            FPBits::one(Sign::NEG).get_val());104      // When x <= -0x1.0ap+3, round(expm1(x), HP, RN) = -0x1.ffcp-1.105      return fputil::round_result_slightly_down(106          fputil::cast<float16>(-0x1.ffcp-1));107    }108 109    // When 0 < |x| <= 2^(-3).110    if (x_abs <= 0x3000U && !x_bits.is_zero()) {111 112#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS113      if (auto r = EXPM1F16_EXCEPTS_LO.lookup(x_u);114          LIBC_UNLIKELY(r.has_value()))115        return r.value();116#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS117 118      float xf = x;119      // Degree-5 minimax polynomial generated by Sollya with the following120      // commands:121      //   > display = hexadecimal;122      //   > P = fpminimax(expm1(x)/x, 4, [|SG...|], [-2^-3, 2^-3]);123      //   > x * P;124      return fputil::cast<float16>(125          xf * fputil::polyeval(xf, 0x1p+0f, 0x1.fffff8p-2f, 0x1.555556p-3f,126                                0x1.55905ep-5f, 0x1.1124c2p-7f));127    }128  }129 130#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS131  if (auto r = EXPM1F16_EXCEPTS_HI.lookup(x_u); LIBC_UNLIKELY(r.has_value()))132    return r.value();133#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS134 135  // exp(x) = exp(hi + mid) * exp(lo)136  auto [exp_hi_mid, exp_lo] = exp_range_reduction(x);137  // expm1(x) = exp(hi + mid) * exp(lo) - 1138  return fputil::cast<float16>(fputil::multiply_add(exp_hi_mid, exp_lo, -1.0f));139}140 141} // namespace LIBC_NAMESPACE_DECL142