Path: blob/main/contrib/llvm-project/libc/src/__support/math/expf.h
213799 views
//===-- Implementation header for expf --------------------------*- 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_EXPF_H9#define LLVM_LIBC_SRC___SUPPORT_MATH_EXPF_H1011#include "exp_float_constants.h" // Lookup tables EXP_M1 and EXP_M2.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/multiply_add.h"16#include "src/__support/FPUtil/nearest_integer.h"17#include "src/__support/FPUtil/rounding_mode.h"18#include "src/__support/common.h"19#include "src/__support/macros/config.h"20#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY2122namespace LIBC_NAMESPACE_DECL {2324namespace math {2526static constexpr float expf(float x) {27using FPBits = typename fputil::FPBits<float>;28FPBits xbits(x);2930uint32_t x_u = xbits.uintval();31uint32_t x_abs = x_u & 0x7fff'ffffU;3233#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS34// Exceptional values35if (LIBC_UNLIKELY(x_u == 0xc236'bd8cU)) { // x = -0x1.6d7b18p+5f36return 0x1.108a58p-66f - x * 0x1.0p-95f;37}38#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS3940// When |x| >= 89, |x| < 2^-25, or x is nan41if (LIBC_UNLIKELY(x_abs >= 0x42b2'0000U || x_abs <= 0x3280'0000U)) {42// |x| < 2^-2543if (xbits.get_biased_exponent() <= 101) {44return 1.0f + x;45}4647// When x < log(2^-150) or nan48if (xbits.uintval() >= 0xc2cf'f1b5U) {49// exp(-Inf) = 050if (xbits.is_inf())51return 0.0f;52// exp(nan) = nan53if (xbits.is_nan())54return x;55if (fputil::fenv_is_round_up())56return FPBits::min_subnormal().get_val();57fputil::set_errno_if_required(ERANGE);58fputil::raise_except_if_required(FE_UNDERFLOW);59return 0.0f;60}61// x >= 89 or nan62if (xbits.is_pos() && (xbits.uintval() >= 0x42b2'0000)) {63// x is finite64if (xbits.uintval() < 0x7f80'0000U) {65int rounding = fputil::quick_get_round();66if (rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO)67return FPBits::max_normal().get_val();6869fputil::set_errno_if_required(ERANGE);70fputil::raise_except_if_required(FE_OVERFLOW);71}72// x is +inf or nan73return x + FPBits::inf().get_val();74}75}76// For -104 < x < 89, to compute exp(x), we perform the following range77// reduction: find hi, mid, lo such that:78// x = hi + mid + lo, in which79// hi is an integer,80// mid * 2^7 is an integer81// -2^(-8) <= lo < 2^-8.82// In particular,83// hi + mid = round(x * 2^7) * 2^(-7).84// Then,85// exp(x) = exp(hi + mid + lo) = exp(hi) * exp(mid) * exp(lo).86// We store exp(hi) and exp(mid) in the lookup tables EXP_M1 and EXP_M287// respectively. exp(lo) is computed using a degree-4 minimax polynomial88// generated by Sollya.8990// x_hi = (hi + mid) * 2^7 = round(x * 2^7).91float kf = fputil::nearest_integer(x * 0x1.0p7f);92// Subtract (hi + mid) from x to get lo.93double xd = static_cast<double>(fputil::multiply_add(kf, -0x1.0p-7f, x));94int x_hi = static_cast<int>(kf);95x_hi += 104 << 7;96// hi = x_hi >> 797double exp_hi = EXP_M1[x_hi >> 7];98// mid * 2^7 = x_hi & 0x0000'007fU;99double exp_mid = EXP_M2[x_hi & 0x7f];100// Degree-4 minimax polynomial generated by Sollya with the following101// commands:102// > display = hexadecimal;103// > Q = fpminimax(expm1(x)/x, 3, [|D...|], [-2^-8, 2^-8]);104// > Q;105double exp_lo =106fputil::polyeval(xd, 0x1p0, 0x1.ffffffffff777p-1, 0x1.000000000071cp-1,1070x1.555566668e5e7p-3, 0x1.55555555ef243p-5);108return static_cast<float>(exp_hi * exp_mid * exp_lo);109}110111} // namespace math112113} // namespace LIBC_NAMESPACE_DECL114115#endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXPF_H116117118