[libc-commits] [libc] [libc][math] fix-exp (PR #148670)

Muhammad Bassiouni via libc-commits libc-commits at lists.llvm.org
Mon Jul 14 10:35:26 PDT 2025


https://github.com/bassiounix updated https://github.com/llvm/llvm-project/pull/148670

>From 2e0f077b71ebd0474acbc0c026a7cc6504d8d2fd Mon Sep 17 00:00:00 2001
From: sribee8 <sriya.pratipati at gmail.com>
Date: Mon, 14 Jul 2025 16:36:24 +0000
Subject: [PATCH] [libc] exp fuzz tests (#148086)

Created fuzz tests for exp functions

---------

Co-authored-by: Sriya Pratipati <sriyap at google.com>
---
 libc/fuzzing/math/CMakeLists.txt | 36 ++++++++++++++++++++++++++++++
 libc/fuzzing/math/exp10_fuzz.cpp | 38 ++++++++++++++++++++++++++++++++
 libc/fuzzing/math/exp2_fuzz.cpp  | 38 ++++++++++++++++++++++++++++++++
 libc/fuzzing/math/exp_fuzz.cpp   | 38 ++++++++++++++++++++++++++++++++
 libc/fuzzing/math/expm1_fuzz.cpp | 38 ++++++++++++++++++++++++++++++++
 libc/src/__support/math/exp.h    | 28 +++++++++++------------
 6 files changed, 202 insertions(+), 14 deletions(-)
 create mode 100644 libc/fuzzing/math/exp10_fuzz.cpp
 create mode 100644 libc/fuzzing/math/exp2_fuzz.cpp
 create mode 100644 libc/fuzzing/math/exp_fuzz.cpp
 create mode 100644 libc/fuzzing/math/expm1_fuzz.cpp

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/src/__support/math/exp.h b/libc/src/__support/math/exp.h
index 5c43e753ea687..554cc012f66a6 100644
--- a/libc/src/__support/math/exp.h
+++ b/libc/src/__support/math/exp.h
@@ -36,15 +36,15 @@ using Float128 = typename fputil::DyadicFloat<128>;
 using LIBC_NAMESPACE::operator""_u128;
 
 // log2(e)
-static constexpr double LOG2_E = 0x1.71547652b82fep+0;
+static double LOG2_E = 0x1.71547652b82fep+0;
 
 // Error bounds:
 // Errors when using double precision.
-static constexpr double ERR_D = 0x1.8p-63;
+static 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;
+static double ERR_DD = 0x1.0p-99;
 #endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS
 
 // -2^-12 * log(2)
@@ -53,12 +53,12 @@ static constexpr double ERR_DD = 0x1.0p-99;
 // > 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;
+static double MLOG_2_EXP2_M12_HI = -0x1.62e42ffp-13;
+static 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;
+static double MLOG_2_EXP2_M12_MID_30 = 0x1.718432ap-47;
+static double MLOG_2_EXP2_M12_LO = 0x1.b0e2633fe0685p-79;
 #endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS
 
 namespace {
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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)
@@ -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);
 
@@ -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);
 



More information about the libc-commits mailing list