[libc-commits] [PATCH] D155130: [libc] Set min precision for strtofloat fuzz

Michael Jones via Phabricator via libc-commits libc-commits at lists.llvm.org
Wed Jul 12 15:23:49 PDT 2023


michaelrj created this revision.
michaelrj added a reviewer: lntue.
Herald added projects: libc-project, All.
Herald added a subscriber: libc-commits.
michaelrj requested review of this revision.

MPFR has a minimum precision of 2, but the strtofloat fuzz sometimes
would request a precision of 1 for the case of the minimum subnormal.
Now there is a minimum precision it can request.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D155130

Files:
  libc/fuzzing/stdlib/strtofloat_fuzz.cpp


Index: libc/fuzzing/stdlib/strtofloat_fuzz.cpp
===================================================================
--- libc/fuzzing/stdlib/strtofloat_fuzz.cpp
+++ libc/fuzzing/stdlib/strtofloat_fuzz.cpp
@@ -27,18 +27,22 @@
 // This function calculates the effective precision for a given float type and
 // exponent. Subnormals have a lower effective precision since they don't
 // necessarily use all of the bits of the mantissa.
-template <typename F> inline int effective_precision(int exponent) {
-  int full_precision = FloatProperties<F>::MANTISSA_PRECISION;
+template <typename F> inline constexpr int effective_precision(int exponent) {
+  const int full_precision = FloatProperties<F>::MANTISSA_PRECISION;
 
   // This is intended to be 0 when the exponent is the lowest normal and
   // increase as the exponent's magnitude increases.
-  int bits_below_normal = (-exponent) - (FloatProperties<F>::EXPONENT_BIAS - 1);
+  const int bits_below_normal =
+      (-exponent) - (FloatProperties<F>::EXPONENT_BIAS - 1);
 
-  // This comparison is optimized out by the compiler.
-  if (bits_below_normal >= 0 && bits_below_normal < full_precision - 1) {
-    // The precision should be the normal, full precision, minus the bits lost
-    // by this being a subnormal, minus one for the implicit leading one.
-    return full_precision - bits_below_normal - 1;
+  // The precision should be the normal, full precision, minus the bits lost
+  // by this being a subnormal, minus one for the implicit leading one.
+  const int bits_if_subnormal = full_precision - bits_below_normal - 1;
+
+  // bits_if_subnormal must be at least 2 because that's the minimum MPFR
+  // precision.
+  if (bits_below_normal >= 0 && bits_if_subnormal >= 2) {
+    return bits_if_subnormal;
   }
   return full_precision;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D155130.539767.patch
Type: text/x-patch
Size: 1804 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20230712/1a79f9d6/attachment-0001.bin>


More information about the libc-commits mailing list