[libc-commits] [libc] 82451d0 - [libc] Updated exp fuzz tests (#148912)

via libc-commits libc-commits at lists.llvm.org
Wed Jul 16 11:36:20 PDT 2025


Author: sribee8
Date: 2025-07-16T11:36:17-07:00
New Revision: 82451d0b1341a9b6c01eaa5d27088ff9f3287853

URL: https://github.com/llvm/llvm-project/commit/82451d0b1341a9b6c01eaa5d27088ff9f3287853
DIFF: https://github.com/llvm/llvm-project/commit/82451d0b1341a9b6c01eaa5d27088ff9f3287853.diff

LOG: [libc] Updated exp fuzz tests (#148912)

Fuzz tests were previously in the wrong format, updated them to correct
format.

---------

Co-authored-by: Sriya Pratipati <sriyap at google.com>

Added: 
    

Modified: 
    libc/fuzzing/math/exp10_fuzz.cpp
    libc/fuzzing/math/exp2_fuzz.cpp
    libc/fuzzing/math/exp_fuzz.cpp
    libc/fuzzing/math/expm1_fuzz.cpp

Removed: 
    


################################################################################
diff  --git a/libc/fuzzing/math/exp10_fuzz.cpp b/libc/fuzzing/math/exp10_fuzz.cpp
index 2baef03a264a4..d939948b723a5 100644
--- a/libc/fuzzing/math/exp10_fuzz.cpp
+++ b/libc/fuzzing/math/exp10_fuzz.cpp
@@ -12,27 +12,40 @@
 
 #include "src/math/exp10.h"
 #include "utils/MPFRWrapper/mpfr_inc.h"
+#include <cstdint>
+#include <cstring>
+#include <iostream>
 #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;
+extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
   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);
+  for (size_t i = 0; i < size / sizeof(double); ++i) {
+    double x;
+    std::memcpy(&x, data, sizeof(double));
+    data += sizeof(double);
 
-  double result = LIBC_NAMESPACE::exp10(x);
+    // remove NaN and inf
+    if (isnan(x) || isinf(x))
+      continue;
+    // signed zeros already tested in unit tests
+    if (signbit(x) && x == 0.0)
+      continue;
 
-  if (result != to_compare)
-    __builtin_trap();
+    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) {
+      std::cout << std::hexfloat << "Failing input: " << x << std::endl;
+      std::cout << std::hexfloat << "Failing output: " << result << std::endl;
+      std::cout << std::hexfloat << "Expected: " << to_compare << std::endl;
+      __builtin_trap();
+    }
+  }
   mpfr_clear(input);
   return 0;
 }

diff  --git a/libc/fuzzing/math/exp2_fuzz.cpp b/libc/fuzzing/math/exp2_fuzz.cpp
index 8a2959047a6ca..a29d3c00da672 100644
--- a/libc/fuzzing/math/exp2_fuzz.cpp
+++ b/libc/fuzzing/math/exp2_fuzz.cpp
@@ -12,27 +12,40 @@
 
 #include "src/math/exp2.h"
 #include "utils/MPFRWrapper/mpfr_inc.h"
+#include <cstdint>
+#include <cstring>
+#include <iostream>
 #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;
+extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
   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);
+  for (size_t i = 0; i < size / sizeof(double); ++i) {
+    double x;
+    std::memcpy(&x, data, sizeof(double));
+    data += sizeof(double);
 
-  double result = LIBC_NAMESPACE::exp2(x);
+    // remove NaN and inf
+    if (isnan(x) || isinf(x))
+      continue;
+    // signed zeros already tested in unit tests
+    if (signbit(x) && x == 0.0)
+      continue;
 
-  if (result != to_compare)
-    __builtin_trap();
+    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) {
+      std::cout << std::hexfloat << "Failing input: " << x << std::endl;
+      std::cout << std::hexfloat << "Failing output: " << result << std::endl;
+      std::cout << std::hexfloat << "Expected: " << to_compare << std::endl;
+      __builtin_trap();
+    }
+  }
   mpfr_clear(input);
   return 0;
 }

diff  --git a/libc/fuzzing/math/exp_fuzz.cpp b/libc/fuzzing/math/exp_fuzz.cpp
index 97bc12dfa64c9..66823596dc6fa 100644
--- a/libc/fuzzing/math/exp_fuzz.cpp
+++ b/libc/fuzzing/math/exp_fuzz.cpp
@@ -12,27 +12,40 @@
 
 #include "src/math/exp.h"
 #include "utils/MPFRWrapper/mpfr_inc.h"
+#include <cstdint>
+#include <cstring>
+#include <iostream>
 #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;
+extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
   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);
+  for (size_t i = 0; i < size / sizeof(double); ++i) {
+    double x;
+    std::memcpy(&x, data, sizeof(double));
+    data += sizeof(double);
 
-  double result = LIBC_NAMESPACE::exp(x);
+    // remove NaN and inf
+    if (isnan(x) || isinf(x))
+      continue;
+    // signed zeros already tested in unit tests
+    if (signbit(x) && x == 0.0)
+      continue;
 
-  if (result != to_compare)
-    __builtin_trap();
+    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) {
+      std::cout << std::hexfloat << "Failing input: " << x << std::endl;
+      std::cout << std::hexfloat << "Failing output: " << result << std::endl;
+      std::cout << std::hexfloat << "Expected: " << to_compare << std::endl;
+      __builtin_trap();
+    }
+  }
   mpfr_clear(input);
   return 0;
 }

diff  --git a/libc/fuzzing/math/expm1_fuzz.cpp b/libc/fuzzing/math/expm1_fuzz.cpp
index db507bb02b1d7..0690e449c3d23 100644
--- a/libc/fuzzing/math/expm1_fuzz.cpp
+++ b/libc/fuzzing/math/expm1_fuzz.cpp
@@ -12,27 +12,40 @@
 
 #include "src/math/expm1.h"
 #include "utils/MPFRWrapper/mpfr_inc.h"
+#include <cstdint>
+#include <cstring>
+#include <iostream>
 #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;
+extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
   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);
+  for (size_t i = 0; i < size / sizeof(double); ++i) {
+    double x;
+    std::memcpy(&x, data, sizeof(double));
+    data += sizeof(double);
 
-  double result = LIBC_NAMESPACE::expm1(x);
+    // remove NaN and inf
+    if (isnan(x) || isinf(x))
+      continue;
+    // signed zeros already tested in unit tests
+    if (signbit(x) && x == 0.0)
+      continue;
 
-  if (result != to_compare)
-    __builtin_trap();
+    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) {
+      std::cout << std::hexfloat << "Failing input: " << x << std::endl;
+      std::cout << std::hexfloat << "Failing output: " << result << std::endl;
+      std::cout << std::hexfloat << "Expected: " << to_compare << std::endl;
+      __builtin_trap();
+    }
+  }
   mpfr_clear(input);
   return 0;
 }


        


More information about the libc-commits mailing list