[llvm] [libc][math] Implement double precision expm1 function correctly rounded for all rounding modes. (PR #67048)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 27 12:07:00 PDT 2023
https://github.com/lntue updated https://github.com/llvm/llvm-project/pull/67048
>From 4c7b1407b8bdc6e7b52716cd06d866ba8e21a102 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue at google.com>
Date: Wed, 20 Sep 2023 06:51:43 -0400
Subject: [PATCH 1/2] [libc][math] Implement double precision expm1 function
correctly rounded for all rounding modes.
---
libc/config/darwin/arm/entrypoints.txt | 1 +
libc/config/linux/aarch64/entrypoints.txt | 1 +
libc/config/linux/riscv64/entrypoints.txt | 1 +
libc/config/linux/x86_64/entrypoints.txt | 1 +
libc/config/windows/entrypoints.txt | 1 +
libc/docs/math/index.rst | 6 +-
libc/spec/stdc.td | 2 +
libc/src/__support/FPUtil/FPBits.h | 6 +-
.../src/__support/FPUtil/except_value_utils.h | 16 +-
libc/src/math/CMakeLists.txt | 1 +
libc/src/math/expm1.h | 18 +
libc/src/math/generic/CMakeLists.txt | 27 +
libc/src/math/generic/exp.cpp | 2 +-
libc/src/math/generic/expm1.cpp | 493 ++++++++++++++++++
libc/src/math/generic/expm1f.cpp | 16 +-
libc/test/src/math/CMakeLists.txt | 14 +
libc/test/src/math/expm1_test.cpp | 120 +++++
libc/test/src/math/smoke/CMakeLists.txt | 13 +
libc/test/src/math/smoke/expm1_test.cpp | 39 ++
19 files changed, 763 insertions(+), 15 deletions(-)
create mode 100644 libc/src/math/expm1.h
create mode 100644 libc/src/math/generic/expm1.cpp
create mode 100644 libc/test/src/math/expm1_test.cpp
create mode 100644 libc/test/src/math/smoke/expm1_test.cpp
diff --git a/libc/config/darwin/arm/entrypoints.txt b/libc/config/darwin/arm/entrypoints.txt
index f3d2cabb2c141d1..ecdd2d0624fcf5d 100644
--- a/libc/config/darwin/arm/entrypoints.txt
+++ b/libc/config/darwin/arm/entrypoints.txt
@@ -135,6 +135,7 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.exp10f
libc.src.math.exp2
libc.src.math.exp2f
+ libc.src.math.expm1
libc.src.math.expm1f
libc.src.math.fabs
libc.src.math.fabsf
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index e0bf9800ec881c2..c0c2433594bbc97 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -249,6 +249,7 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.exp10f
libc.src.math.exp2
libc.src.math.exp2f
+ libc.src.math.expm1
libc.src.math.expm1f
libc.src.math.fabs
libc.src.math.fabsf
diff --git a/libc/config/linux/riscv64/entrypoints.txt b/libc/config/linux/riscv64/entrypoints.txt
index 2b2f2629f78ce67..a2db3576f80d744 100644
--- a/libc/config/linux/riscv64/entrypoints.txt
+++ b/libc/config/linux/riscv64/entrypoints.txt
@@ -258,6 +258,7 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.exp10f
libc.src.math.exp2
libc.src.math.exp2f
+ libc.src.math.expm1
libc.src.math.expm1f
libc.src.math.fabs
libc.src.math.fabsf
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index dcb8c6231432d35..4880ef5e4f2cee2 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -262,6 +262,7 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.exp10f
libc.src.math.exp2
libc.src.math.exp2f
+ libc.src.math.expm1
libc.src.math.expm1f
libc.src.math.fabs
libc.src.math.fabsf
diff --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index 26552fe7b07b514..1ee088e399438b7 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -134,6 +134,7 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.exp10f
libc.src.math.exp2
libc.src.math.exp2f
+ libc.src.math.expm1
libc.src.math.expm1f
libc.src.math.fabs
libc.src.math.fabsf
diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst
index 4e2ef031ab2444b..77b3c8b99a0c2bc 100644
--- a/libc/docs/math/index.rst
+++ b/libc/docs/math/index.rst
@@ -370,7 +370,7 @@ Higher Math Functions
+------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
| exp2l | | | | | | | | | | | | |
+------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
-| expm1 | | | | | | | | | | | | |
+| expm1 | |check| | |check| | | |check| | |check| | | | |check| | | | | |
+------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
| expm1f | |check| | |check| | | |check| | |check| | | | |check| | | | | |
+------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
@@ -484,9 +484,9 @@ cos |check| large
cosh |check|
erf |check|
exp |check| |check|
-exp10 |check|
+exp10 |check| |check|
exp2 |check| |check|
-expm1 |check|
+expm1 |check| |check|
fma |check| |check|
hypot |check| |check|
log |check| |check|
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 86de59d33feecd4..1e9043b841ff1d8 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -439,6 +439,8 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"exp2", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
FunctionSpec<"exp2f", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
+
+ FunctionSpec<"expm1", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
FunctionSpec<"expm1f", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
FunctionSpec<"remainderf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,
diff --git a/libc/src/__support/FPUtil/FPBits.h b/libc/src/__support/FPUtil/FPBits.h
index 326211669dce660..163b659d707d1f6 100644
--- a/libc/src/__support/FPUtil/FPBits.h
+++ b/libc/src/__support/FPUtil/FPBits.h
@@ -116,7 +116,7 @@ template <typename T> struct FPBits {
FPBits() : bits(0) {}
- LIBC_INLINE T get_val() const { return cpp::bit_cast<T>(bits); }
+ LIBC_INLINE constexpr T get_val() const { return cpp::bit_cast<T>(bits); }
LIBC_INLINE void set_val(T value) { bits = cpp::bit_cast<UIntType>(value); }
@@ -185,6 +185,10 @@ template <typename T> struct FPBits {
return bits;
}
+ LIBC_INLINE static constexpr FPBits<T> min_normal() {
+ return FPBits<T>(MIN_NORMAL);
+ }
+
LIBC_INLINE static constexpr T build_nan(UIntType v) {
FPBits<T> bits = inf();
bits.set_mantissa(v);
diff --git a/libc/src/__support/FPUtil/except_value_utils.h b/libc/src/__support/FPUtil/except_value_utils.h
index df1b9c74aab2819..70318048643bf04 100644
--- a/libc/src/__support/FPUtil/except_value_utils.h
+++ b/libc/src/__support/FPUtil/except_value_utils.h
@@ -100,15 +100,19 @@ template <typename T, size_t N> struct ExceptValues {
};
// Helper functions to set results for exceptional cases.
-LIBC_INLINE float round_result_slightly_down(float value_rn) {
- volatile float tmp = value_rn;
- tmp = tmp - 0x1.0p-100f;
+template<typename T>
+LIBC_INLINE T round_result_slightly_down(T value_rn) {
+ volatile T tmp = value_rn;
+ constexpr T MIN_NORMAL = FPBits<T>::min_normal().get_val();
+ tmp = tmp - MIN_NORMAL;
return tmp;
}
-LIBC_INLINE float round_result_slightly_up(float value_rn) {
- volatile float tmp = value_rn;
- tmp = tmp + 0x1.0p-100f;
+template <typename T>
+LIBC_INLINE T round_result_slightly_up(T value_rn) {
+ volatile T tmp = value_rn;
+ const T MIN_NORMAL = FPBits<T>::min_normal().get_val();
+ tmp = tmp + MIN_NORMAL;
return tmp;
}
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 8b2021cac8239fe..00e0e4e6a583987 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -88,6 +88,7 @@ add_math_entrypoint_object(exp2f)
add_math_entrypoint_object(exp10)
add_math_entrypoint_object(exp10f)
+add_math_entrypoint_object(expm1)
add_math_entrypoint_object(expm1f)
add_math_entrypoint_object(fabs)
diff --git a/libc/src/math/expm1.h b/libc/src/math/expm1.h
new file mode 100644
index 000000000000000..7ae354e459feb1f
--- /dev/null
+++ b/libc/src/math/expm1.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for expm1 -------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_MATH_EXPM1_H
+#define LLVM_LIBC_SRC_MATH_EXPM1_H
+
+namespace __llvm_libc {
+
+double expm1(double x);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_MATH_EXPM1_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 9b092c3d3e7e65f..843349ccdc196a7 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -697,6 +697,33 @@ add_entrypoint_object(
-O3
)
+add_entrypoint_object(
+ expm1
+ SRCS
+ expm1.cpp
+ HDRS
+ ../expm1.h
+ DEPENDS
+ .common_constants
+ .explogxf
+ libc.src.__support.CPP.bit
+ libc.src.__support.CPP.optional
+ libc.src.__support.FPUtil.dyadic_float
+ libc.src.__support.FPUtil.fenv_impl
+ libc.src.__support.FPUtil.fp_bits
+ libc.src.__support.FPUtil.multiply_add
+ libc.src.__support.FPUtil.nearest_integer
+ libc.src.__support.FPUtil.polyeval
+ libc.src.__support.FPUtil.rounding_mode
+ libc.src.__support.FPUtil.triple_double
+ libc.src.__support.macros.optimization
+ libc.include.errno
+ libc.src.errno.errno
+ libc.include.math
+ COMPILE_OPTIONS
+ -O3
+)
+
add_entrypoint_object(
expm1f
SRCS
diff --git a/libc/src/math/generic/exp.cpp b/libc/src/math/generic/exp.cpp
index c16b461c14ed870..16814e069627864 100644
--- a/libc/src/math/generic/exp.cpp
+++ b/libc/src/math/generic/exp.cpp
@@ -104,7 +104,7 @@ Float128 poly_approx_f128(const Float128 &dx) {
{false, -132, MType({0xaaaaaaaaaaaaaaab, 0xaaaaaaaaaaaaaaaa})}, // 1/24
{false, -134, MType({0x8888888888888889, 0x8888888888888888})}, // 1/120
{false, -137, MType({0x60b60b60b60b60b6, 0xb60b60b60b60b60b})}, // 1/720
- {false, -140, MType({0x00b00b00b00b00b0, 0xb00b00b00b00b00b})}, // 1/5040
+ {false, -140, MType({0x00d00d00d00d00d0, 0xd00d00d00d00d00d})}, // 1/5040
};
Float128 p = fputil::polyeval(dx, COEFFS_128[0], COEFFS_128[1], COEFFS_128[2],
diff --git a/libc/src/math/generic/expm1.cpp b/libc/src/math/generic/expm1.cpp
new file mode 100644
index 000000000000000..76befaf7cf37d43
--- /dev/null
+++ b/libc/src/math/generic/expm1.cpp
@@ -0,0 +1,493 @@
+//===-- Double-precision e^x - 1 function ---------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/expm1.h"
+#include "common_constants.h" // Lookup tables EXP_M1 and EXP_M2.
+#include "explogxf.h" // ziv_test_denorm.
+#include "src/__support/CPP/bit.h"
+#include "src/__support/CPP/optional.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/double_double.h"
+#include "src/__support/FPUtil/dyadic_float.h"
+#include "src/__support/FPUtil/except_value_utils.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/FPUtil/rounding_mode.h"
+#include "src/__support/FPUtil/triple_double.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+#include <errno.h>
+
+// #define DEBUGDEBUG
+
+#ifdef DEBUGDEBUG
+#include <iomanip>
+#include <iostream>
+#endif
+
+namespace __llvm_libc {
+
+using fputil::DoubleDouble;
+using fputil::TripleDouble;
+using Float128 = typename fputil::DyadicFloat<128>;
+
+// log2(e)
+constexpr double LOG2_E = 0x1.71547652b82fep+0;
+
+// Error bounds:
+// Errors when using double precision.
+// 0x1.8p-63;
+constexpr uint64_t ERR_D = 0x3c08000000000000;
+// Errors when using double-double precision.
+// 0x1.0p-99
+constexpr uint64_t ERR_DD = 0x39c0000000000000;
+
+// -2^-12 * log(2)
+// > a = -2^-12 * log(2);
+// > b = round(a, 30, RN);
+// > c = round(a - b, 30, RN);
+// > d = round(a - b - c, D, RN);
+// Errors < 1.5 * 2^-133
+constexpr double MLOG_2_EXP2_M12_HI = -0x1.62e42ffp-13;
+constexpr double MLOG_2_EXP2_M12_MID = 0x1.718432a1b0e26p-47;
+constexpr double MLOG_2_EXP2_M12_MID_30 = 0x1.718432ap-47;
+constexpr double MLOG_2_EXP2_M12_LO = 0x1.b0e2633fe0685p-79;
+
+// Polynomial approximations with double precision:
+// 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.
+LIBC_INLINE double poly_approx_d(double dx) {
+ // dx^2
+ double dx2 = dx * dx;
+ // c0 = 1 + dx / 2
+ double c0 = fputil::multiply_add(dx, 0.5, 1.0);
+ // c1 = 1/6 + dx / 24
+ double c1 =
+ fputil::multiply_add(dx, 0x1.5555555555555p-5, 0x1.5555555555555p-3);
+ // p = dx^2 * c1 + c0 = 1 + dx / 2 + dx^2 / 6 + dx^3 / 24
+ double p = fputil::multiply_add(dx2, c1, c0);
+ return p;
+}
+
+// Polynomial approximation with double-double precision:
+// Return expm1(dx) / dx ~ 1 + dx / 2 + dx^2 / 6 + ... + dx^6 / 5040
+// For |dx| < 2^-13 + 2^-30:
+// | output - expm1(dx) | < 2^-101
+DoubleDouble poly_approx_dd(const DoubleDouble &dx) {
+ // Taylor polynomial.
+ constexpr DoubleDouble COEFFS[] = {
+ {0, 0x1p0}, // 1
+ {0, 0x1p-1}, // 1/2
+ {0x1.5555555555555p-57, 0x1.5555555555555p-3}, // 1/6
+ {0x1.5555555555555p-59, 0x1.5555555555555p-5}, // 1/24
+ {0x1.1111111111111p-63, 0x1.1111111111111p-7}, // 1/120
+ {-0x1.f49f49f49f49fp-65, 0x1.6c16c16c16c17p-10}, // 1/720
+ {0x1.a01a01a01a01ap-73, 0x1.a01a01a01a01ap-13}, // 1/5040
+ };
+
+ DoubleDouble p = fputil::polyeval(dx, COEFFS[0], COEFFS[1], COEFFS[2],
+ COEFFS[3], COEFFS[4], COEFFS[5], COEFFS[6]);
+ return p;
+}
+
+// Polynomial approximation with 128-bit precision:
+// Return (exp(dx) - 1)/dx ~ 1 + dx / 2 + dx^2 / 6 + ... + dx^6 / 5040
+// For |dx| < 2^-13 + 2^-30:
+// | output - exp(dx) | < 2^-126.
+Float128 poly_approx_f128(const Float128 &dx) {
+ using MType = typename Float128::MantissaType;
+
+ constexpr Float128 COEFFS_128[]{
+ {false, -127, MType({0, 0x8000000000000000})}, // 1.0
+ {false, -128, MType({0, 0x8000000000000000})}, // 0.5
+ {false, -130, MType({0xaaaaaaaaaaaaaaab, 0xaaaaaaaaaaaaaaaa})}, // 1/6
+ {false, -132, MType({0xaaaaaaaaaaaaaaab, 0xaaaaaaaaaaaaaaaa})}, // 1/24
+ {false, -134, MType({0x8888888888888889, 0x8888888888888888})}, // 1/120
+ {false, -137, MType({0x60b60b60b60b60b6, 0xb60b60b60b60b60b})}, // 1/720
+ {false, -140, MType({0x00d00d00d00d00d0, 0xd00d00d00d00d00d})}, // 1/5040
+ };
+
+ Float128 p = fputil::polyeval(dx, COEFFS_128[0], COEFFS_128[1], COEFFS_128[2],
+ COEFFS_128[3], COEFFS_128[4], COEFFS_128[5],
+ COEFFS_128[6]);
+ return p;
+}
+
+#ifdef DEBUGDEBUG
+std::ostream &operator<<(std::ostream &OS, const Float128 &r) {
+ OS << (r.sign ? "-(" : "(") << r.mantissa.val[0] << " + " << r.mantissa.val[1]
+ << " * 2^64) * 2^" << r.exponent << "\n";
+ return OS;
+}
+
+std::ostream &operator<<(std::ostream &OS, const DoubleDouble &r) {
+ OS << std::hexfloat << r.hi << " + " << r.lo << std::defaultfloat << "\n";
+ return OS;
+}
+#endif
+
+// Compute exp(x) - 1 using 128-bit precision.
+// TODO(lntue): investigate triple-double precision implementation for this
+// step.
+Float128 expm1_f128(double x, double kd, int idx1, int idx2) {
+ using MType = typename Float128::MantissaType;
+ // Recalculate dx:
+
+ double t1 = fputil::multiply_add(kd, MLOG_2_EXP2_M12_HI, x); // exact
+ double t2 = kd * MLOG_2_EXP2_M12_MID_30; // exact
+ double t3 = kd * MLOG_2_EXP2_M12_LO; // Error < 2^-133
+
+ Float128 dx = fputil::quick_add(
+ Float128(t1), fputil::quick_add(Float128(t2), Float128(t3)));
+
+ // TODO: Skip recalculating exp_mid1 and exp_mid2.
+ Float128 exp_mid1 =
+ fputil::quick_add(Float128(EXP2_MID1[idx1].hi),
+ fputil::quick_add(Float128(EXP2_MID1[idx1].mid),
+ Float128(EXP2_MID1[idx1].lo)));
+
+ Float128 exp_mid2 =
+ fputil::quick_add(Float128(EXP2_MID2[idx2].hi),
+ fputil::quick_add(Float128(EXP2_MID2[idx2].mid),
+ Float128(EXP2_MID2[idx2].lo)));
+
+ Float128 exp_mid = fputil::quick_mul(exp_mid1, exp_mid2);
+
+ int hi = static_cast<int>(kd) >> 12;
+ Float128 minus_one{true, -127 - hi, MType({0, 0x8000000000000000})};
+
+ Float128 exp_mid_m1 = fputil::quick_add(exp_mid, minus_one);
+
+ Float128 p = poly_approx_f128(dx);
+
+ // r = exp_mid * (1 + dx * P) - 1
+ // = (exp_mid - 1) + (dx * exp_mid) * P
+ Float128 r =
+ fputil::multiply_add(fputil::quick_mul(exp_mid, dx), p, exp_mid_m1);
+
+ r.exponent += hi;
+
+#ifdef DEBUGDEBUG
+ std::cout << "=== VERY SLOW PASS ===\n"
+ << " kd: " << kd << "\n"
+ << " dx: " << dx << "exp_mid_m1: " << exp_mid_m1
+ << " exp_mid: " << exp_mid << " p: " << p
+ << " r: " << r << std::endl;
+#endif
+
+ return r;
+}
+
+// Compute exp(x) - 1 with double-double precision.
+DoubleDouble exp_double_double(double x, double kd, const DoubleDouble &exp_mid,
+ const DoubleDouble &hi_part) {
+ // Recalculate dx:
+ // dx = x - k * 2^-12 * log(2)
+ double t1 = fputil::multiply_add(kd, MLOG_2_EXP2_M12_HI, x); // exact
+ double t2 = kd * MLOG_2_EXP2_M12_MID_30; // exact
+ double t3 = kd * MLOG_2_EXP2_M12_LO; // Error < 2^-130
+
+ DoubleDouble dx = fputil::exact_add(t1, t2);
+ dx.lo += t3;
+
+ // Degree-6 Taylor polynomial approximation in double-double precision.
+ // | p - exp(x) | < 2^-100.
+ DoubleDouble p = poly_approx_dd(dx);
+
+ // Error bounds: 2^-99.
+ DoubleDouble r =
+ fputil::multiply_add(fputil::quick_mult(exp_mid, dx), p, hi_part);
+
+#ifdef DEBUGDEBUG
+ std::cout << "=== SLOW PASS ===\n"
+ << " dx: " << dx << " p: " << p << " r: " << r << std::endl;
+#endif
+
+ return r;
+}
+
+// Check for exceptional cases when
+// |x| <= 2^-53 or x < log(2^-54) or x >= 0x1.6232bdd7abcd3p+9
+double set_exceptional(double x) {
+ using FPBits = typename fputil::FPBits<double>;
+ using FloatProp = typename fputil::FloatProperties<double>;
+ FPBits xbits(x);
+
+ uint64_t x_u = xbits.uintval();
+ uint64_t x_abs = x_u & FloatProp::EXP_MANT_MASK;
+
+ // |x| <= 2^-53.
+ if (x_abs <= 0x3ca0'0000'0000'0000ULL) {
+ // expm1(x) ~ x.
+
+ if (LIBC_UNLIKELY(x_abs <= 0x0370'0000'0000'0000ULL)) {
+ if (LIBC_UNLIKELY(x_abs == 0))
+ return x;
+ // |x| <= 2^-968, need to scale up a bit before rounding, then scale it
+ // back down.
+ return 0x1.0p-200 * fputil::multiply_add(x, 0x1.0p+200, 0x1.0p-1022);
+ }
+
+ // 2^-968 < |x| <= 2^-53.
+ return fputil::round_result_slightly_up(x);
+ }
+
+ // x < log(2^-54) || x >= 0x1.6232bdd7abcd3p+9 or inf/nan.
+
+ // x < log(2^-54) or -inf/nan
+ if (x_u >= 0xc042'b708'8723'20e2ULL) {
+ // expm1(-Inf) = -1
+ if (xbits.is_inf())
+ return -1.0;
+
+ // exp(nan) = nan
+ if (xbits.is_nan())
+ return x;
+
+ return fputil::round_result_slightly_up(-1.0);
+ }
+
+ // x >= round(log(MAX_NORMAL), D, RU) = 0x1.62e42fefa39fp+9 or +inf/nan
+ // x is finite
+ if (x_u < 0x7ff0'0000'0000'0000ULL) {
+ int rounding = fputil::quick_get_round();
+ if (rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO)
+ return static_cast<double>(FPBits(FPBits::MAX_NORMAL));
+
+ fputil::set_errno_if_required(ERANGE);
+ fputil::raise_except_if_required(FE_OVERFLOW);
+ }
+ // x is +inf or nan
+ return x + static_cast<double>(FPBits::inf());
+}
+
+LLVM_LIBC_FUNCTION(double, expm1, (double x)) {
+ using FPBits = typename fputil::FPBits<double>;
+ using FloatProp = typename fputil::FloatProperties<double>;
+ FPBits xbits(x);
+
+ bool x_sign = xbits.get_sign();
+ uint64_t x_u = xbits.uintval();
+
+ // Upper bound: max normal number = 2^1023 * (2 - 2^-52)
+ // > round(log (2^1023 ( 2 - 2^-52 )), D, RU) = 0x1.62e42fefa39fp+9
+ // > round(log (2^1023 ( 2 - 2^-52 )), D, RD) = 0x1.62e42fefa39efp+9
+ // > round(log (2^1023 ( 2 - 2^-52 )), D, RN) = 0x1.62e42fefa39efp+9
+ // > round(exp(0x1.62e42fefa39fp+9), D, RN) = infty
+
+ // Lower bound: log(2^-54) = -0x1.2b708872320e2p5
+ // > round(log(2^-54), D, RN) = -0x1.2b708872320e2p5
+
+ // x < log(2^-54) or x >= 0x1.6232bdd7abcd3p+9 or |x| <= 2^-53.
+
+ if (LIBC_UNLIKELY(x_u >= 0xc042b708872320e2 ||
+ (x_u <= 0xbca0000000000000 && x_u >= 0x40862e42fefa39f0) ||
+ x_u <= 0x3ca0000000000000)) {
+ return set_exceptional(x);
+ }
+
+ // Now log(2^-54) <= x <= -2^-53 or 2^-53 <= x < log(2^1023 * (2 - 2^-52))
+
+ // Range reduction:
+ // Let x = log(2) * (hi + mid1 + mid2) + lo
+ // in which:
+ // hi is an integer
+ // mid1 * 2^6 is an integer
+ // mid2 * 2^12 is an integer
+ // then:
+ // exp(x) = 2^hi * 2^(mid1) * 2^(mid2) * exp(lo).
+ // With this formula:
+ // - multiplying by 2^hi is exact and cheap, simply by adding the exponent
+ // field.
+ // - 2^(mid1) and 2^(mid2) are stored in 2 x 64-element tables.
+ // - exp(lo) ~ 1 + lo + a0 * lo^2 + ...
+ //
+ // They can be defined by:
+ // hi + mid1 + mid2 = 2^(-12) * round(2^12 * log_2(e) * x)
+ // If we store L2E = round(log2(e), D, RN), then:
+ // log2(e) - L2E ~ 1.5 * 2^(-56)
+ // So the errors when computing in double precision is:
+ // | x * 2^12 * log_2(e) - D(x * 2^12 * L2E) | <=
+ // <= | x * 2^12 * log_2(e) - x * 2^12 * L2E | +
+ // + | x * 2^12 * L2E - D(x * 2^12 * L2E) |
+ // <= 2^12 * ( |x| * 1.5 * 2^-56 + eps(x)) for RN
+ // 2^12 * ( |x| * 1.5 * 2^-56 + 2*eps(x)) for other rounding modes.
+ // So if:
+ // hi + mid1 + mid2 = 2^(-12) * round(x * 2^12 * L2E) is computed entirely
+ // in double precision, the reduced argument:
+ // lo = x - log(2) * (hi + mid1 + mid2) is bounded by:
+ // |lo| <= 2^-13 + (|x| * 1.5 * 2^-56 + 2*eps(x))
+ // < 2^-13 + (1.5 * 2^9 * 1.5 * 2^-56 + 2*2^(9 - 52))
+ // < 2^-13 + 2^-41
+ //
+
+ // The following trick computes the round(x * L2E) more efficiently
+ // than using the rounding instructions, with the tradeoff for less accuracy,
+ // and hence a slightly larger range for the reduced argument `lo`.
+ //
+ // To be precise, since |x| < |log(2^-1075)| < 1.5 * 2^9,
+ // |x * 2^12 * L2E| < 1.5 * 2^9 * 1.5 < 2^23,
+ // So we can fit the rounded result round(x * 2^12 * L2E) in int32_t.
+ // Thus, the goal is to be able to use an additional addition and fixed width
+ // shift to get an int32_t representing round(x * 2^12 * L2E).
+ //
+ // Assuming int32_t using 2-complement representation, since the mantissa part
+ // of a double precision is unsigned with the leading bit hidden, if we add an
+ // extra constant C = 2^e1 + 2^e2 with e1 > e2 >= 2^25 to the product, the
+ // part that are < 2^e2 in resulted mantissa of (x*2^12*L2E + C) can be
+ // considered as a proper 2-complement representations of x*2^12*L2E.
+ //
+ // One small problem with this approach is that the sum (x*2^12*L2E + C) in
+ // double precision is rounded to the least significant bit of the dorminant
+ // factor C. In order to minimize the rounding errors from this addition, we
+ // want to minimize e1. Another constraint that we want is that after
+ // shifting the mantissa so that the least significant bit of int32_t
+ // corresponds to the unit bit of (x*2^12*L2E), the sign is correct without
+ // any adjustment. So combining these 2 requirements, we can choose
+ // C = 2^33 + 2^32, so that the sign bit corresponds to 2^31 bit, and hence
+ // after right shifting the mantissa, the resulting int32_t has correct sign.
+ // With this choice of C, the number of mantissa bits we need to shift to the
+ // right is: 52 - 33 = 19.
+ //
+ // Moreover, since the integer right shifts are equivalent to rounding down,
+ // we can add an extra 0.5 so that it will become round-to-nearest, tie-to-
+ // +infinity. So in particular, we can compute:
+ // hmm = x * 2^12 * L2E + C,
+ // where C = 2^33 + 2^32 + 2^-1, then if
+ // k = int32_t(lower 51 bits of double(x * 2^12 * L2E + C) >> 19),
+ // the reduced argument:
+ // lo = x - log(2) * 2^-12 * k is bounded by:
+ // |lo| <= 2^-13 + 2^-41 + 2^-12*2^-19
+ // = 2^-13 + 2^-31 + 2^-41.
+ //
+ // Finally, notice that k only uses the mantissa of x * 2^12 * L2E, so the
+ // exponent 2^12 is not needed. So we can simply define
+ // C = 2^(33 - 12) + 2^(32 - 12) + 2^(-13 - 12), and
+ // k = int32_t(lower 51 bits of double(x * L2E + C) >> 19).
+
+ // Rounding errors <= 2^-31 + 2^-41.
+ double tmp = fputil::multiply_add(x, LOG2_E, 0x1.8000'0000'4p21);
+ int k = static_cast<int>(cpp::bit_cast<uint64_t>(tmp) >> 19);
+ double kd = static_cast<double>(k);
+
+ uint32_t idx1 = (k >> 6) & 0x3f;
+ uint32_t idx2 = k & 0x3f;
+ int hi = k >> 12;
+
+ DoubleDouble exp_mid1{EXP2_MID1[idx1].mid, EXP2_MID1[idx1].hi};
+ DoubleDouble exp_mid2{EXP2_MID2[idx2].mid, EXP2_MID2[idx2].hi};
+
+ DoubleDouble exp_mid = fputil::quick_mult(exp_mid1, exp_mid2);
+
+ // -2^(-hi)
+ double one_scaled =
+ FPBits::create_value(true, FPBits::EXPONENT_BIAS - hi, 0).get_val();
+
+ // 2^(mid1 + mid2) - 2^(-hi)
+ DoubleDouble hi_part = x_sign ? fputil::exact_add(one_scaled, exp_mid.hi)
+ : fputil::exact_add(exp_mid.hi, one_scaled);
+
+ hi_part.lo += exp_mid.lo;
+
+ // |x - (hi + mid1 + mid2) * log(2) - dx| < 2^11 * eps(M_LOG_2_EXP2_M12.lo)
+ // = 2^11 * 2^-13 * 2^-52
+ // = 2^-54.
+ // |dx| < 2^-13 + 2^-30.
+ double lo_h = fputil::multiply_add(kd, MLOG_2_EXP2_M12_HI, x); // exact
+ double dx = fputil::multiply_add(kd, MLOG_2_EXP2_M12_MID, lo_h);
+
+ // We use the degree-4 Taylor polynomial to approximate exp(lo):
+ // exp(lo) ~ 1 + lo + lo^2 / 2 + lo^3 / 6 + lo^4 / 24 = 1 + lo * P(lo)
+ // So that the errors are bounded by:
+ // |P(lo) - expm1(lo)/lo| < |lo|^4 / 64 < 2^(-13 * 4) / 64 = 2^-58
+ // Let P_ be an evaluation of P where all intermediate computations are in
+ // double precision. Using either Horner's or Estrin's schemes, the evaluated
+ // errors can be bounded by:
+ // |P_(dx) - P(dx)| < 2^-51
+ // => |dx * P_(dx) - expm1(lo) | < 1.5 * 2^-64
+ // => 2^(mid1 + mid2) * |dx * P_(dx) - expm1(lo)| < 1.5 * 2^-63.
+ // Since we approximate
+ // 2^(mid1 + mid2) ~ exp_mid.hi + exp_mid.lo,
+ // We use the expression:
+ // (exp_mid.hi + exp_mid.lo) * (1 + dx * P_(dx)) ~
+ // ~ exp_mid.hi + (exp_mid.hi * dx * P_(dx) + exp_mid.lo)
+ // with errors bounded by 1.5 * 2^-63.
+
+ // Finally, we have the following approximation formula:
+ // expm1(x) = 2^hi * 2^(mid1 + mid2) * exp(lo) - 1
+ // = 2^hi * ( 2^(mid1 + mid2) * exp(lo) - 2^(-hi) )
+ // ~ 2^hi * ( (exp_mid.hi - 2^-hi) +
+ // + (exp_mid.hi * dx * P_(dx) + exp_mid.lo))
+
+ double mid_lo = dx * exp_mid.hi;
+
+ // Approximate expm1(dx)/dx ~ 1 + dx / 2 + dx^2 / 6 + dx^3 / 24.
+ double p = poly_approx_d(dx);
+
+ double lo = fputil::multiply_add(p, mid_lo, hi_part.lo);
+
+ uint64_t err = x_sign ? (static_cast<uint64_t>(-hi) << 52) : 0;
+
+ double err_d = cpp::bit_cast<double>(ERR_D + err);
+
+ double upper = hi_part.hi + (lo + err_d);
+ double lower = hi_part.hi + (lo - err_d);
+
+#ifdef DEBUGDEBUG
+ std::cout << "=== FAST PASS ===\n"
+ << " x: " << std::hexfloat << x << std::defaultfloat << "\n"
+ << " k: " << k << "\n"
+ << " idx1: " << idx1 << "\n"
+ << " idx2: " << idx2 << "\n"
+ << " hi: " << hi << "\n"
+ << " dx: " << std::hexfloat << dx << std::defaultfloat << "\n"
+ << "exp_mid: " << exp_mid << "hi_part: " << hi_part
+ << " mid_lo: " << std::hexfloat << mid_lo << std::defaultfloat
+ << "\n"
+ << " p: " << std::hexfloat << p << std::defaultfloat << "\n"
+ << " lo: " << std::hexfloat << lo << std::defaultfloat << "\n"
+ << " upper: " << std::hexfloat << upper << std::defaultfloat
+ << "\n"
+ << " lower: " << std::hexfloat << lower << std::defaultfloat
+ << "\n"
+ << std::endl;
+#endif
+
+ if (LIBC_LIKELY(upper == lower)) {
+ // to multiply by 2^hi, a fast way is to simply add hi to the exponent
+ // field.
+ int64_t exp_hi = static_cast<int64_t>(hi) << FloatProp::MANTISSA_WIDTH;
+ double r = cpp::bit_cast<double>(exp_hi + cpp::bit_cast<int64_t>(upper));
+ return r;
+ }
+
+ // Use double-double
+ DoubleDouble r_dd = exp_double_double(x, kd, exp_mid, hi_part);
+
+ double err_dd = cpp::bit_cast<double>(ERR_DD + err);
+
+ double upper_dd = r_dd.hi + (r_dd.lo + err_dd);
+ double lower_dd = r_dd.hi + (r_dd.lo - err_dd);
+
+ if (LIBC_LIKELY(upper_dd == lower_dd)) {
+ int64_t exp_hi = static_cast<int64_t>(hi) << FloatProp::MANTISSA_WIDTH;
+ double r = cpp::bit_cast<double>(exp_hi + cpp::bit_cast<int64_t>(upper_dd));
+ return r;
+ }
+
+ // Use 128-bit precision
+ Float128 r_f128 = expm1_f128(x, kd, idx1, idx2);
+
+ return static_cast<double>(r_f128);
+}
+
+} // namespace __llvm_libc
diff --git a/libc/src/math/generic/expm1f.cpp b/libc/src/math/generic/expm1f.cpp
index 811baec0e91038f..0d384a20e5a79a9 100644
--- a/libc/src/math/generic/expm1f.cpp
+++ b/libc/src/math/generic/expm1f.cpp
@@ -111,16 +111,24 @@ LLVM_LIBC_FUNCTION(float, expm1f, (float x)) {
#endif // LIBC_TARGET_CPU_HAS_FMA
}
+ constexpr double COEFFS[] = {
+ 0x1p-1, 0x1.55555555557ddp-3, 0x1.55555555552fap-5,
+ 0x1.111110fcd58b7p-7, 0x1.6c16c1717660bp-10,
+ 0x1.a0241f0006d62p-13, 0x1.a01e3f8d3c06p-16
+ };
+
// 2^-25 <= |x| < 2^-4
double xd = static_cast<double>(x);
double xsq = xd * xd;
// Degree-8 minimax polynomial generated by Sollya with:
// > display = hexadecimal;
// > P = fpminimax((expm1(x) - x)/x^2, 6, [|D...|], [-2^-4, 2^-4]);
- double r =
- fputil::polyeval(xd, 0x1p-1, 0x1.55555555557ddp-3, 0x1.55555555552fap-5,
- 0x1.111110fcd58b7p-7, 0x1.6c16c1717660bp-10,
- 0x1.a0241f0006d62p-13, 0x1.a01e3f8d3c06p-16);
+
+ double c0 = fputil::multiply_add(xd, COEFFS[1], COEFFS[0]);
+ double c1 = fputil::multiply_add(xd, COEFFS[3], COEFFS[2]);
+ double c2 = fputil::multiply_add(xd, COEFFS[5], COEFFS[4]);
+
+ double r = fputil::polyeval(xsq, c0, c1, c2, COEFFS[6]);
return static_cast<float>(fputil::multiply_add(r, xsq, xd));
}
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index d47e0e0ea4aad6e..2ba03676a633fff 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -1322,6 +1322,20 @@ add_fp_unittest(
libc.src.__support.FPUtil.fp_bits
)
+add_fp_unittest(
+ expm1_test
+ NEED_MPFR
+ SUITE
+ libc-math-unittests
+ SRCS
+ expm1_test.cpp
+ DEPENDS
+ libc.src.errno.errno
+ libc.include.math
+ libc.src.math.expm1
+ libc.src.__support.FPUtil.fp_bits
+)
+
add_fp_unittest(
log_test
NEED_MPFR
diff --git a/libc/test/src/math/expm1_test.cpp b/libc/test/src/math/expm1_test.cpp
new file mode 100644
index 000000000000000..abec550b91072e1
--- /dev/null
+++ b/libc/test/src/math/expm1_test.cpp
@@ -0,0 +1,120 @@
+//===-- Unittests for e^x - 1 ---------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/errno/libc_errno.h"
+#include "src/math/expm1.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+#include <math.h>
+
+#include <errno.h>
+#include <stdint.h>
+
+namespace mpfr = __llvm_libc::testing::mpfr;
+using __llvm_libc::testing::tlog;
+
+DECLARE_SPECIAL_CONSTANTS(double)
+
+TEST(LlvmLibcExpm1Test, TrickyInputs) {
+ constexpr int N = 21;
+ constexpr uint64_t INPUTS[N] = {
+ 0x3FD79289C6E6A5C0,
+ 0x3FD05DE80A173EA0, // 0x1.05de80a173eap-2
+ 0xbf1eb7a4cb841fcc, // -0x1.eb7a4cb841fccp-14
+ 0xbf19a61fb925970d,
+ 0x3fda7b764e2cf47a, // 0x1.a7b764e2cf47ap-2
+ 0xc04757852a4b93aa, // -0x1.757852a4b93aap+5
+ 0x4044c19e5712e377, // x=0x1.4c19e5712e377p+5
+ 0xbf19a61fb925970d, // x=-0x1.9a61fb925970dp-14
+ 0xc039a74cdab36c28, // x=-0x1.9a74cdab36c28p+4
+ 0xc085b3e4e2e3bba9, // x=-0x1.5b3e4e2e3bba9p+9
+ 0xc086960d591aec34, // x=-0x1.6960d591aec34p+9
+ 0xc086232c09d58d91, // x=-0x1.6232c09d58d91p+9
+ 0xc0874910d52d3051, // x=-0x1.74910d52d3051p9
+ 0xc0867a172ceb0990, // x=-0x1.67a172ceb099p+9
+ 0xc08ff80000000000, // x=-0x1.ff8p+9
+ 0xbc971547652b82fe, // x=-0x1.71547652b82fep-54
+ 0xbce465655f122ff6, // x=-0x1.465655f122ff6p-49
+ 0x3d1bc8ee6b28659a, // x=0x1.bc8ee6b28659ap-46
+ 0x3f18442b169f672d, // x=0x1.8442b169f672dp-14
+ 0xc02b4f0cfb15ca0f, // x=-0x1.b4f0cfb15ca0fp+3
+ 0xc042b708872320dd, // x=-0x1.2b708872320ddp+5
+ };
+ for (int i = 0; i < N; ++i) {
+ double x = double(FPBits(INPUTS[i]));
+ EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Expm1, x,
+ __llvm_libc::expm1(x), 0.5);
+ }
+}
+
+TEST(LlvmLibcExpm1Test, InDoubleRange) {
+ constexpr uint64_t COUNT = 1'231;
+ uint64_t START = __llvm_libc::fputil::FPBits<double>(0.25).uintval();
+ uint64_t STOP = __llvm_libc::fputil::FPBits<double>(4.0).uintval();
+ uint64_t STEP = (STOP - START) / COUNT;
+
+ auto test = [&](mpfr::RoundingMode rounding_mode) {
+ mpfr::ForceRoundingMode __r(rounding_mode);
+ if (!__r.success)
+ return;
+
+ uint64_t fails = 0;
+ uint64_t count = 0;
+ uint64_t cc = 0;
+ double mx, mr = 0.0;
+ double tol = 0.5;
+
+ for (uint64_t i = 0, v = START; i <= COUNT; ++i, v += STEP) {
+ double x = FPBits(v).get_val();
+ if (isnan(x) || isinf(x) || x < 0.0)
+ continue;
+ libc_errno = 0;
+ double result = __llvm_libc::expm1(x);
+ ++cc;
+ if (isnan(result) || isinf(result))
+ continue;
+
+ ++count;
+
+ if (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Expm1, x, result,
+ 0.5, rounding_mode)) {
+ ++fails;
+ while (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Expm1, x,
+ result, tol, rounding_mode)) {
+ mx = x;
+ mr = result;
+
+ if (tol > 1000.0)
+ break;
+
+ tol *= 2.0;
+ }
+ }
+ }
+ tlog << " Expm1 failed: " << fails << "/" << count << "/" << cc
+ << " tests.\n";
+ tlog << " Max ULPs is at most: " << static_cast<uint64_t>(tol) << ".\n";
+ if (fails) {
+ EXPECT_MPFR_MATCH(mpfr::Operation::Expm1, mx, mr, 0.5, rounding_mode);
+ }
+ };
+
+ tlog << " Test Rounding To Nearest...\n";
+ test(mpfr::RoundingMode::Nearest);
+
+ tlog << " Test Rounding Downward...\n";
+ test(mpfr::RoundingMode::Downward);
+
+ tlog << " Test Rounding Upward...\n";
+ test(mpfr::RoundingMode::Upward);
+
+ tlog << " Test Rounding Toward Zero...\n";
+ test(mpfr::RoundingMode::TowardZero);
+}
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 803f6d2b9389bbb..a9b31eaf6a4944c 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -1216,6 +1216,19 @@ add_fp_unittest(
libc.src.__support.FPUtil.fp_bits
)
+add_fp_unittest(
+ expm1_test
+ SUITE
+ libc-math-smoke-tests
+ SRCS
+ expm1_test.cpp
+ DEPENDS
+ libc.src.errno.errno
+ libc.include.math
+ libc.src.math.expm1
+ libc.src.__support.FPUtil.fp_bits
+)
+
add_fp_unittest(
log_test
SUITE
diff --git a/libc/test/src/math/smoke/expm1_test.cpp b/libc/test/src/math/smoke/expm1_test.cpp
new file mode 100644
index 000000000000000..502a9390fa63582
--- /dev/null
+++ b/libc/test/src/math/smoke/expm1_test.cpp
@@ -0,0 +1,39 @@
+//===-- Unittests for e^x - 1 ---------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/errno/libc_errno.h"
+#include "src/math/expm1.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+#include <math.h>
+
+#include <errno.h>
+#include <stdint.h>
+
+namespace mpfr = __llvm_libc::testing::mpfr;
+using __llvm_libc::testing::tlog;
+
+DECLARE_SPECIAL_CONSTANTS(double)
+
+TEST(LlvmLibcExpm1Test, SpecialNumbers) {
+ EXPECT_FP_EQ(aNaN, __llvm_libc::expm1(aNaN));
+ EXPECT_FP_EQ(inf, __llvm_libc::expm1(inf));
+ EXPECT_FP_EQ_ALL_ROUNDING(-1.0, __llvm_libc::expm1(neg_inf));
+ EXPECT_FP_EQ_WITH_EXCEPTION(inf, __llvm_libc::expm1(0x1.0p20), FE_OVERFLOW);
+ EXPECT_FP_EQ_ALL_ROUNDING(zero, __llvm_libc::expm1(zero));
+ EXPECT_FP_EQ_ALL_ROUNDING(neg_zero, __llvm_libc::expm1(neg_zero));
+ // |x| < 2^-53, expm1(x) = x
+ EXPECT_FP_EQ(-0x1.23456789abcdep-55,
+ __llvm_libc::expm1(-0x1.23456789abcdep-55));
+ EXPECT_FP_EQ(0x1.23456789abcdep-55,
+ __llvm_libc::expm1(0x1.23456789abcdep-55));
+ // log(2^-54)
+ EXPECT_FP_EQ(0x1.23456789a, __llvm_libc::expm1(-0x1.2b708872320e2p5));
+}
>From 0b9a9151bdc61df1e613da5a14da8d68ddaa1ccd Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Wed, 27 Sep 2023 19:05:22 +0000
Subject: [PATCH 2/2] Update comments on test values and add bazel target for
expm1.
---
libc/test/src/math/expm1_test.cpp | 12 ++++++------
.../bazel/llvm-project-overlay/libc/BUILD.bazel | 16 ++++++++++++++++
2 files changed, 22 insertions(+), 6 deletions(-)
diff --git a/libc/test/src/math/expm1_test.cpp b/libc/test/src/math/expm1_test.cpp
index abec550b91072e1..892fc409b303e55 100644
--- a/libc/test/src/math/expm1_test.cpp
+++ b/libc/test/src/math/expm1_test.cpp
@@ -25,12 +25,12 @@ DECLARE_SPECIAL_CONSTANTS(double)
TEST(LlvmLibcExpm1Test, TrickyInputs) {
constexpr int N = 21;
constexpr uint64_t INPUTS[N] = {
- 0x3FD79289C6E6A5C0,
- 0x3FD05DE80A173EA0, // 0x1.05de80a173eap-2
- 0xbf1eb7a4cb841fcc, // -0x1.eb7a4cb841fccp-14
- 0xbf19a61fb925970d,
- 0x3fda7b764e2cf47a, // 0x1.a7b764e2cf47ap-2
- 0xc04757852a4b93aa, // -0x1.757852a4b93aap+5
+ 0x3FD79289C6E6A5C0, // x=0x1.79289c6e6a5cp-2
+ 0x3FD05DE80A173EA0, // x=0x1.05de80a173eap-2
+ 0xbf1eb7a4cb841fcc, // x=-0x1.eb7a4cb841fccp-14
+ 0xbf19a61fb925970d, // x=-0x1.9a61fb925970dp-14
+ 0x3fda7b764e2cf47a, // x=0x1.a7b764e2cf47ap-2
+ 0xc04757852a4b93aa, // x=-0x1.757852a4b93aap+5
0x4044c19e5712e377, // x=0x1.4c19e5712e377p+5
0xbf19a61fb925970d, // x=-0x1.9a61fb925970dp-14
0xc039a74cdab36c28, // x=-0x1.9a74cdab36c28p+4
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 80ea75ecd8760e0..a795c1254d539cc 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -1278,6 +1278,22 @@ libc_math_function(
],
)
+libc_math_function(
+ name = "expm1",
+ additional_deps = [
+ ":__support_fputil_double_double",
+ ":__support_fputil_dyadic_float",
+ ":__support_fputil_multiply_add",
+ ":__support_fputil_nearest_integer",
+ ":__support_fputil_polyeval",
+ ":__support_fputil_rounding_mode",
+ ":__support_fputil_triple_double",
+ ":__support_macros_optimization",
+ ":common_constants",
+ ":explogxf",
+ ],
+)
+
libc_math_function(
name = "expm1f",
additional_deps = [
More information about the llvm-commits
mailing list