Skip to content

[libc][math] fix-exp #148670

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions libc/src/__support/math/exp.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ namespace {
// Return expm1(dx) / x ~ 1 + dx / 2 + dx^2 / 6 + dx^3 / 24.
// For |dx| < 2^-13 + 2^-30:
// | output - expm1(dx) / dx | < 2^-51.
static constexpr double poly_approx_d(double dx) {
static double poly_approx_d(double dx) {
// dx^2
double dx2 = dx * dx;
// c0 = 1 + dx / 2
Expand All @@ -85,7 +85,7 @@ static constexpr double poly_approx_d(double dx) {
// Return exp(dx) ~ 1 + dx + dx^2 / 2 + ... + dx^6 / 720
// For |dx| < 2^-13 + 2^-30:
// | output - exp(dx) | < 2^-101
static constexpr DoubleDouble poly_approx_dd(const DoubleDouble &dx) {
static DoubleDouble poly_approx_dd(const DoubleDouble &dx) {
// Taylor polynomial.
constexpr DoubleDouble COEFFS[] = {
{0, 0x1p0}, // 1
Expand All @@ -106,7 +106,7 @@ static constexpr DoubleDouble poly_approx_dd(const DoubleDouble &dx) {
// Return exp(dx) ~ 1 + dx + dx^2 / 2 + ... + dx^7 / 5040
// For |dx| < 2^-13 + 2^-30:
// | output - exp(dx) | < 2^-126.
static constexpr Float128 poly_approx_f128(const Float128 &dx) {
static Float128 poly_approx_f128(const Float128 &dx) {
constexpr Float128 COEFFS_128[]{
{Sign::POS, -127, 0x80000000'00000000'00000000'00000000_u128}, // 1.0
{Sign::POS, -127, 0x80000000'00000000'00000000'00000000_u128}, // 1.0
Expand All @@ -127,7 +127,7 @@ static constexpr Float128 poly_approx_f128(const Float128 &dx) {
// Compute exp(x) using 128-bit precision.
// TODO(lntue): investigate triple-double precision implementation for this
// step.
static constexpr Float128 exp_f128(double x, double kd, int idx1, int idx2) {
static Float128 exp_f128(double x, double kd, int idx1, int idx2) {
// Recalculate dx:

double t1 = fputil::multiply_add(kd, MLOG_2_EXP2_M12_HI, x); // exact
Expand Down Expand Up @@ -160,7 +160,7 @@ static constexpr Float128 exp_f128(double x, double kd, int idx1, int idx2) {
}

// Compute exp(x) with double-double precision.
static constexpr DoubleDouble exp_double_double(double x, double kd,
static DoubleDouble exp_double_double(double x, double kd,
const DoubleDouble &exp_mid) {
// Recalculate dx:
// dx = x - k * 2^-12 * log(2)
Expand All @@ -184,7 +184,7 @@ static constexpr DoubleDouble exp_double_double(double x, double kd,

// Check for exceptional cases when
// |x| <= 2^-53 or x < log(2^-1075) or x >= 0x1.6232bdd7abcd3p+9
static constexpr double set_exceptional(double x) {
static double set_exceptional(double x) {
using FPBits = typename fputil::FPBits<double>;
FPBits xbits(x);

Expand Down Expand Up @@ -234,7 +234,7 @@ static constexpr double set_exceptional(double x) {

namespace math {

static constexpr double exp(double x) {
static double exp(double x) {
using FPBits = typename fputil::FPBits<double>;
FPBits xbits(x);

Expand Down