Skip to content

[libc][math] Refactor exp implementation to header-only in src/__support/math folder. #148761

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

Merged
merged 3 commits into from
Jul 15, 2025

Conversation

bassiounix
Copy link
Contributor

@llvmbot
Copy link
Member

llvmbot commented Jul 15, 2025

@llvm/pr-subscribers-backend-risc-v
@llvm/pr-subscribers-llvm-ir
@llvm/pr-subscribers-libc

@llvm/pr-subscribers-backend-mips

Author: Muhammad Bassiouni (bassiounix)

Changes
  • [libc][math] Refactor exp implementation to header-only in src/__support/math folder.
  • Reapply "[libc][math] Refactor exp implementation to header-only in src/__support/math folder." (#148668)

Patch is 109.58 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/148761.diff

27 Files Affected:

  • (modified) libc/fuzzing/math/CMakeLists.txt (+36)
  • (added) libc/fuzzing/math/exp10_fuzz.cpp (+38)
  • (added) libc/fuzzing/math/exp2_fuzz.cpp (+38)
  • (added) libc/fuzzing/math/exp_fuzz.cpp (+38)
  • (added) libc/fuzzing/math/expm1_fuzz.cpp (+38)
  • (modified) libc/shared/math.h (+1)
  • (added) libc/shared/math/exp.h (+23)
  • (modified) libc/src/__support/math/CMakeLists.txt (+39)
  • (added) libc/src/__support/math/exp.h (+448)
  • (added) libc/src/__support/math/exp_constants.h (+174)
  • (added) libc/src/__support/math/exp_utils.h (+72)
  • (modified) libc/src/math/generic/CMakeLists.txt (+3-19)
  • (modified) libc/src/math/generic/common_constants.cpp (-157)
  • (modified) libc/src/math/generic/common_constants.h (+1-6)
  • (modified) libc/src/math/generic/exp.cpp (+2-427)
  • (modified) libc/src/math/generic/explogxf.h (+2-55)
  • (modified) llvm/include/llvm/IR/RuntimeLibcalls.td (+29-23)
  • (modified) llvm/lib/IR/RuntimeLibcalls.cpp (+11-11)
  • (modified) llvm/lib/Target/RISCV/RISCVOptWInstrs.cpp (+9-1)
  • (modified) llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp (+50-19)
  • (modified) llvm/test/CodeGen/Mips/ldexp.ll (-18)
  • (modified) llvm/test/CodeGen/RISCV/sextw-removal.ll (+1-2)
  • (added) llvm/test/CodeGen/Thumb2/LowOverheadLoops/lsr-le-cost.ll (+300)
  • (modified) llvm/test/CodeGen/Thumb2/LowOverheadLoops/sibling-loops.ll (+27-22)
  • (modified) llvm/test/DebugInfo/ARM/hardware-loop-phi-insertion.ll (+1-1)
  • (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (+44-13)
  • (modified) utils/bazel/llvm-project-overlay/mlir/python/BUILD.bazel (+26)
diff --git a/libc/fuzzing/math/CMakeLists.txt b/libc/fuzzing/math/CMakeLists.txt
index e3c29651917fc..bf2be5c0b3cea 100644
--- a/libc/fuzzing/math/CMakeLists.txt
+++ b/libc/fuzzing/math/CMakeLists.txt
@@ -62,6 +62,42 @@ add_libc_fuzzer(
     libc.src.math.nextafterl
 )
 
+add_libc_fuzzer(
+  exp_fuzz
+  NEED_MPFR
+  SRCS
+    exp_fuzz.cpp
+  DEPENDS
+    libc.src.math.exp
+)
+
+add_libc_fuzzer(
+  exp10_fuzz
+  NEED_MPFR
+  SRCS
+    exp10_fuzz.cpp
+  DEPENDS
+    libc.src.math.exp10
+)
+
+add_libc_fuzzer(
+  exp2_fuzz
+  NEED_MPFR
+  SRCS
+    exp2_fuzz.cpp
+  DEPENDS
+    libc.src.math.exp2
+)
+
+add_libc_fuzzer(
+  expm1_fuzz
+  NEED_MPFR
+  SRCS
+    expm1_fuzz.cpp
+  DEPENDS
+    libc.src.math.expm1
+)
+
 add_libc_fuzzer(
   asin_fuzz
   NEED_MPFR
diff --git a/libc/fuzzing/math/exp10_fuzz.cpp b/libc/fuzzing/math/exp10_fuzz.cpp
new file mode 100644
index 0000000000000..2baef03a264a4
--- /dev/null
+++ b/libc/fuzzing/math/exp10_fuzz.cpp
@@ -0,0 +1,38 @@
+//===-- exp10_fuzz.cpp ----------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// Fuzzing test for llvm-libc exp10 implementation.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/math/exp10.h"
+#include "utils/MPFRWrapper/mpfr_inc.h"
+#include <math.h>
+
+extern "C" int LLVMFuzzerTestOneInput(double x) {
+  // remove NaN and inf
+  if (isnan(x) || isinf(x))
+    return 0;
+  // signed zeros already tested in unit tests
+  if (signbit(x) && x == 0.0)
+    return 0;
+  mpfr_t input;
+  mpfr_init2(input, 53);
+  mpfr_set_d(input, x, MPFR_RNDN);
+  int output = mpfr_exp10(input, input, MPFR_RNDN);
+  mpfr_subnormalize(input, output, MPFR_RNDN);
+  double to_compare = mpfr_get_d(input, MPFR_RNDN);
+
+  double result = LIBC_NAMESPACE::exp10(x);
+
+  if (result != to_compare)
+    __builtin_trap();
+
+  mpfr_clear(input);
+  return 0;
+}
diff --git a/libc/fuzzing/math/exp2_fuzz.cpp b/libc/fuzzing/math/exp2_fuzz.cpp
new file mode 100644
index 0000000000000..8a2959047a6ca
--- /dev/null
+++ b/libc/fuzzing/math/exp2_fuzz.cpp
@@ -0,0 +1,38 @@
+//===-- exp2_fuzz.cpp -----------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// Fuzzing test for llvm-libc exp2 implementation.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/math/exp2.h"
+#include "utils/MPFRWrapper/mpfr_inc.h"
+#include <math.h>
+
+extern "C" int LLVMFuzzerTestOneInput(double x) {
+  // remove NaN and inf
+  if (isnan(x) || isinf(x))
+    return 0;
+  // signed zeros already tested in unit tests
+  if (signbit(x) && x == 0.0)
+    return 0;
+  mpfr_t input;
+  mpfr_init2(input, 53);
+  mpfr_set_d(input, x, MPFR_RNDN);
+  int output = mpfr_exp2(input, input, MPFR_RNDN);
+  mpfr_subnormalize(input, output, MPFR_RNDN);
+  double to_compare = mpfr_get_d(input, MPFR_RNDN);
+
+  double result = LIBC_NAMESPACE::exp2(x);
+
+  if (result != to_compare)
+    __builtin_trap();
+
+  mpfr_clear(input);
+  return 0;
+}
diff --git a/libc/fuzzing/math/exp_fuzz.cpp b/libc/fuzzing/math/exp_fuzz.cpp
new file mode 100644
index 0000000000000..97bc12dfa64c9
--- /dev/null
+++ b/libc/fuzzing/math/exp_fuzz.cpp
@@ -0,0 +1,38 @@
+//===-- exp_fuzz.cpp ------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// Fuzzing test for llvm-libc exp implementation.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/math/exp.h"
+#include "utils/MPFRWrapper/mpfr_inc.h"
+#include <math.h>
+
+extern "C" int LLVMFuzzerTestOneInput(double x) {
+  // remove NaN and inf
+  if (isnan(x) || isinf(x))
+    return 0;
+  // signed zeros already tested in unit tests
+  if (signbit(x) && x == 0.0)
+    return 0;
+  mpfr_t input;
+  mpfr_init2(input, 53);
+  mpfr_set_d(input, x, MPFR_RNDN);
+  int output = mpfr_exp(input, input, MPFR_RNDN);
+  mpfr_subnormalize(input, output, MPFR_RNDN);
+  double to_compare = mpfr_get_d(input, MPFR_RNDN);
+
+  double result = LIBC_NAMESPACE::exp(x);
+
+  if (result != to_compare)
+    __builtin_trap();
+
+  mpfr_clear(input);
+  return 0;
+}
diff --git a/libc/fuzzing/math/expm1_fuzz.cpp b/libc/fuzzing/math/expm1_fuzz.cpp
new file mode 100644
index 0000000000000..db507bb02b1d7
--- /dev/null
+++ b/libc/fuzzing/math/expm1_fuzz.cpp
@@ -0,0 +1,38 @@
+//===-- expm1_fuzz.cpp ----------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// Fuzzing test for llvm-libc expm1 implementation.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/math/expm1.h"
+#include "utils/MPFRWrapper/mpfr_inc.h"
+#include <math.h>
+
+extern "C" int LLVMFuzzerTestOneInput(double x) {
+  // remove NaN and inf
+  if (isnan(x) || isinf(x))
+    return 0;
+  // signed zeros already tested in unit tests
+  if (signbit(x) && x == 0.0)
+    return 0;
+  mpfr_t input;
+  mpfr_init2(input, 53);
+  mpfr_set_d(input, x, MPFR_RNDN);
+  int output = mpfr_expm1(input, input, MPFR_RNDN);
+  mpfr_subnormalize(input, output, MPFR_RNDN);
+  double to_compare = mpfr_get_d(input, MPFR_RNDN);
+
+  double result = LIBC_NAMESPACE::expm1(x);
+
+  if (result != to_compare)
+    __builtin_trap();
+
+  mpfr_clear(input);
+  return 0;
+}
diff --git a/libc/shared/math.h b/libc/shared/math.h
index b2f1a03e0940d..3012cbb938816 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -11,6 +11,7 @@
 
 #include "libc_common.h"
 
+#include "math/exp.h"
 #include "math/expf.h"
 #include "math/expf16.h"
 #include "math/frexpf.h"
diff --git a/libc/shared/math/exp.h b/libc/shared/math/exp.h
new file mode 100644
index 0000000000000..7cdd6331e613a
--- /dev/null
+++ b/libc/shared/math/exp.h
@@ -0,0 +1,23 @@
+//===-- Shared exp function -------------------------------------*- 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_SHARED_MATH_EXP_H
+#define LLVM_LIBC_SHARED_MATH_EXP_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/exp.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::exp;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_EXP_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 900c0ab04d3a3..f7ef9e7694fe6 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -110,3 +110,42 @@ add_header_library(
   DEPENDS
     libc.src.__support.FPUtil.manipulation_functions
 )
+
+add_header_library(
+  exp_constants
+  HDRS
+    exp_constants.h
+  DEPENDS
+    libc.src.__support.FPUtil.triple_double
+)
+
+add_header_library(
+  exp_utils
+  HDRS
+    exp_utils.h
+  DEPENDS
+    libc.src.__support.CPP.optional
+    libc.src.__support.CPP.bit
+    libc.src.__support.FPUtil.fp_bits
+)
+
+add_header_library(
+  exp
+  HDRS
+    exp.h
+  DEPENDS
+    .exp_constants
+    .exp_utils
+    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.integer_literals
+    libc.src.__support.macros.optimization
+)
diff --git a/libc/src/__support/math/exp.h b/libc/src/__support/math/exp.h
new file mode 100644
index 0000000000000..5c8ea089635ef
--- /dev/null
+++ b/libc/src/__support/math/exp.h
@@ -0,0 +1,448 @@
+//===-- Implementation header for exp ---------------------------*- 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___SUPPORT_MATH_EXP_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_EXP_H
+
+#include "exp_constants.h"
+#include "exp_utils.h"
+#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/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/integer_literals.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+using fputil::DoubleDouble;
+using fputil::TripleDouble;
+using Float128 = typename fputil::DyadicFloat<128>;
+
+using LIBC_NAMESPACE::operator""_u128;
+
+// log2(e)
+static constexpr double LOG2_E = 0x1.71547652b82fep+0;
+
+// Error bounds:
+// Errors when using double precision.
+static constexpr double ERR_D = 0x1.8p-63;
+
+#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+// Errors when using double-double precision.
+static constexpr double ERR_DD = 0x1.0p-99;
+#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+
+// -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
+static constexpr double MLOG_2_EXP2_M12_HI = -0x1.62e42ffp-13;
+static constexpr double MLOG_2_EXP2_M12_MID = 0x1.718432a1b0e26p-47;
+
+#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+static constexpr double MLOG_2_EXP2_M12_MID_30 = 0x1.718432ap-47;
+static constexpr double MLOG_2_EXP2_M12_LO = 0x1.b0e2633fe0685p-79;
+#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+
+namespace {
+
+// 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.
+static 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;
+}
+
+#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+// Polynomial approximation with double-double precision:
+// Return exp(dx) ~ 1 + dx + dx^2 / 2 + ... + dx^6 / 720
+// For |dx| < 2^-13 + 2^-30:
+//   | output - exp(dx) | < 2^-101
+static DoubleDouble poly_approx_dd(const DoubleDouble &dx) {
+  // Taylor polynomial.
+  constexpr DoubleDouble COEFFS[] = {
+      {0, 0x1p0},                                      // 1
+      {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
+  };
+
+  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 + dx^2 / 2 + ... + dx^7 / 5040
+// For |dx| < 2^-13 + 2^-30:
+//   | output - exp(dx) | < 2^-126.
+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
+      {Sign::POS, -128, 0x80000000'00000000'00000000'00000000_u128}, // 0.5
+      {Sign::POS, -130, 0xaaaaaaaa'aaaaaaaa'aaaaaaaa'aaaaaaab_u128}, // 1/6
+      {Sign::POS, -132, 0xaaaaaaaa'aaaaaaaa'aaaaaaaa'aaaaaaab_u128}, // 1/24
+      {Sign::POS, -134, 0x88888888'88888888'88888888'88888889_u128}, // 1/120
+      {Sign::POS, -137, 0xb60b60b6'0b60b60b'60b60b60'b60b60b6_u128}, // 1/720
+      {Sign::POS, -140, 0xd00d00d0'0d00d00d'00d00d00'd00d00d0_u128}, // 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], COEFFS_128[7]);
+  return p;
+}
+
+// Compute exp(x) using 128-bit precision.
+// TODO(lntue): investigate triple-double precision implementation for this
+// step.
+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
+  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);
+
+  Float128 p = poly_approx_f128(dx);
+
+  Float128 r = fputil::quick_mul(exp_mid, p);
+
+  r.exponent += static_cast<int>(kd) >> 12;
+
+  return r;
+}
+
+// Compute exp(x) with double-double precision.
+static DoubleDouble exp_double_double(double x, double kd,
+                                                const DoubleDouble &exp_mid) {
+  // 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::quick_mult(exp_mid, p);
+
+  return r;
+}
+#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+
+// Check for exceptional cases when
+// |x| <= 2^-53 or x < log(2^-1075) or x >= 0x1.6232bdd7abcd3p+9
+static double set_exceptional(double x) {
+  using FPBits = typename fputil::FPBits<double>;
+  FPBits xbits(x);
+
+  uint64_t x_u = xbits.uintval();
+  uint64_t x_abs = xbits.abs().uintval();
+
+  // |x| <= 2^-53
+  if (x_abs <= 0x3ca0'0000'0000'0000ULL) {
+    // exp(x) ~ 1 + x
+    return 1 + x;
+  }
+
+  // x <= log(2^-1075) || x >= 0x1.6232bdd7abcd3p+9 or inf/nan.
+
+  // x <= log(2^-1075) or -inf/nan
+  if (x_u >= 0xc087'4910'd52d'3052ULL) {
+    // exp(-Inf) = 0
+    if (xbits.is_inf())
+      return 0.0;
+
+    // exp(nan) = nan
+    if (xbits.is_nan())
+      return x;
+
+    if (fputil::quick_get_round() == FE_UPWARD)
+      return FPBits::min_subnormal().get_val();
+    fputil::set_errno_if_required(ERANGE);
+    fputil::raise_except_if_required(FE_UNDERFLOW);
+    return 0.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 FPBits::max_normal().get_val();
+
+    fputil::set_errno_if_required(ERANGE);
+    fputil::raise_except_if_required(FE_OVERFLOW);
+  }
+  // x is +inf or nan
+  return x + FPBits::inf().get_val();
+}
+
+} // namespace
+
+namespace math {
+
+static double exp(double x) {
+  using FPBits = typename fputil::FPBits<double>;
+  FPBits xbits(x);
+
+  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: min denormal number / 2 = 2^-1075
+  // > round(log(2^-1075), D, RN) = -0x1.74910d52d3052p9
+
+  // Another lower bound: min normal number = 2^-1022
+  // > round(log(2^-1022), D, RN) = -0x1.6232bdd7abcd2p9
+
+  // x < log(2^-1075) or x >= 0x1.6232bdd7abcd3p+9 or |x| < 2^-53.
+  if (LIBC_UNLIKELY(x_u >= 0xc0874910d52d3052 ||
+                    (x_u < 0xbca0000000000000 && x_u >= 0x40862e42fefa39f0) ||
+                    x_u < 0x3ca0000000000000)) {
+    return set_exceptional(x);
+  }
+
+  // Now log(2^-1075) <= 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
+  // sh...
[truncated]

Copy link

github-actions bot commented Jul 15, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@lntue lntue merged commit d969ec9 into llvm:main Jul 15, 2025
17 of 19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants