Path: blob/main/contrib/llvm-project/libc/src/__support/math/expf16.h
213799 views
//===-- Implementation header for expf16 ------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_EXPF16_H9#define LLVM_LIBC_SRC___SUPPORT_MATH_EXPF16_H1011#include "include/llvm-libc-macros/float16-macros.h"1213#ifdef LIBC_TYPES_HAS_FLOAT161415#include "hdr/errno_macros.h"16#include "hdr/fenv_macros.h"17#include "src/__support/FPUtil/FEnvImpl.h"18#include "src/__support/FPUtil/FPBits.h"19#include "src/__support/FPUtil/PolyEval.h"20#include "src/__support/FPUtil/cast.h"21#include "src/__support/FPUtil/except_value_utils.h"22#include "src/__support/FPUtil/rounding_mode.h"23#include "src/__support/common.h"24#include "src/__support/macros/config.h"25#include "src/__support/macros/optimization.h"2627#include "expf16_utils.h"2829namespace LIBC_NAMESPACE_DECL {3031namespace math {3233static constexpr float16 expf16(float16 x) {34#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS35constexpr fputil::ExceptValues<float16, 2> EXPF16_EXCEPTS_LO = {{36// (input, RZ output, RU offset, RD offset, RN offset)37// x = 0x1.de4p-8, expf16(x) = 0x1.01cp+0 (RZ)38{0x1f79U, 0x3c07U, 1U, 0U, 0U},39// x = 0x1.73cp-6, expf16(x) = 0x1.05cp+0 (RZ)40{0x25cfU, 0x3c17U, 1U, 0U, 0U},41}};4243constexpr fputil::ExceptValues<float16, 3> EXPF16_EXCEPTS_HI = {{44// (input, RZ output, RU offset, RD offset, RN offset)45// x = 0x1.c34p+0, expf16(x) = 0x1.74cp+2 (RZ)46{0x3f0dU, 0x45d3U, 1U, 0U, 1U},47// x = -0x1.488p-5, expf16(x) = 0x1.ebcp-1 (RZ)48{0xa922U, 0x3bafU, 1U, 0U, 0U},49// x = -0x1.55p-5, expf16(x) = 0x1.ebp-1 (RZ)50{0xa954U, 0x3bacU, 1U, 0U, 0U},51}};52#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS5354using FPBits = fputil::FPBits<float16>;55FPBits x_bits(x);5657uint16_t x_u = x_bits.uintval();58uint16_t x_abs = x_u & 0x7fffU;5960// When 0 < |x| <= 2^(-5), or |x| >= 12, or x is NaN.61if (LIBC_UNLIKELY(x_abs <= 0x2800U || x_abs >= 0x4a00U)) {62// exp(NaN) = NaN63if (x_bits.is_nan()) {64if (x_bits.is_signaling_nan()) {65fputil::raise_except_if_required(FE_INVALID);66return FPBits::quiet_nan().get_val();67}6869return x;70}7172// When x >= 12.73if (x_bits.is_pos() && x_abs >= 0x4a00U) {74// exp(+inf) = +inf75if (x_bits.is_inf())76return FPBits::inf().get_val();7778switch (fputil::quick_get_round()) {79case FE_TONEAREST:80case FE_UPWARD:81fputil::set_errno_if_required(ERANGE);82fputil::raise_except_if_required(FE_OVERFLOW);83return FPBits::inf().get_val();84default:85return FPBits::max_normal().get_val();86}87}8889// When x <= -18.90if (x_u >= 0xcc80U) {91// exp(-inf) = +092if (x_bits.is_inf())93return FPBits::zero().get_val();9495fputil::set_errno_if_required(ERANGE);96fputil::raise_except_if_required(FE_UNDERFLOW | FE_INEXACT);9798switch (fputil::quick_get_round()) {99case FE_UPWARD:100return FPBits::min_subnormal().get_val();101default:102return FPBits::zero().get_val();103}104}105106// When 0 < |x| <= 2^(-5).107if (x_abs <= 0x2800U && !x_bits.is_zero()) {108#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS109if (auto r = EXPF16_EXCEPTS_LO.lookup(x_u); LIBC_UNLIKELY(r.has_value()))110return r.value();111#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS112113float xf = x;114// Degree-3 minimax polynomial generated by Sollya with the following115// commands:116// > display = hexadecimal;117// > P = fpminimax(expm1(x)/x, 2, [|SG...|], [-2^-5, 2^-5]);118// > 1 + x * P;119return fputil::cast<float16>(120fputil::polyeval(xf, 0x1p+0f, 0x1p+0f, 0x1.0004p-1f, 0x1.555778p-3f));121}122}123124#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS125if (auto r = EXPF16_EXCEPTS_HI.lookup(x_u); LIBC_UNLIKELY(r.has_value()))126return r.value();127#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS128129// exp(x) = exp(hi + mid) * exp(lo)130auto [exp_hi_mid, exp_lo] = exp_range_reduction(x);131return fputil::cast<float16>(exp_hi_mid * exp_lo);132}133134} // namespace math135136} // namespace LIBC_NAMESPACE_DECL137138#endif // LIBC_TYPES_HAS_FLOAT16139140#endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXPF16_H141142143