[PATCH] PR libstdc++/80137 use std::nextafter instead of looping

Jonathan Wakely jwakely@redhat.com
Tue Mar 28 16:14:00 GMT 2017


For gcc-6 I fixed a bug in generate_canonical that allowed it to
return a value outside the permitted range (due to rounding), but that
was done by looping until we got a value that was in range, which
violates the complexity requirements.

John Salmon suggested a better fix in bugzilla, which is what is used
here.

	PR libstdc++/80137
	* include/bits/random.tcc (generate_canonical): Use std::nextafter
	or numeric_limits::epsilon() to reduce out-of-range values.
	* testsuite/26_numerics/random/uniform_real_distribution/operators/
	64351.cc: Verify complexity requirement is met.

Tested powerpc64le-linux, committed to trunk.

This needs to be backported to gcc-6-branch too.

-------------- next part --------------
commit 611ce493bb4d47c153ef8b07721433f9d62dfe0d
Author: redi <redi@138bc75d-0d04-0410-961f-82ee72b054a4>
Date:   Tue Mar 28 16:09:49 2017 +0000

    PR libstdc++/80137 use std::nextafter instead of looping
    
    	PR libstdc++/80137
    	* include/bits/random.tcc (generate_canonical): Use std::nextafter
    	or numeric_limits::epsilon() to reduce out-of-range values.
    	* testsuite/26_numerics/random/uniform_real_distribution/operators/
    	64351.cc: Verify complexity requirement is met.
    
    git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@246542 138bc75d-0d04-0410-961f-82ee72b054a4

diff --git a/libstdc++-v3/include/bits/random.tcc b/libstdc++-v3/include/bits/random.tcc
index 2d5582a..df05ebe 100644
--- a/libstdc++-v3/include/bits/random.tcc
+++ b/libstdc++-v3/include/bits/random.tcc
@@ -3323,18 +3323,23 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       const size_t __m = std::max<size_t>(1UL,
 					  (__b + __log2r - 1UL) / __log2r);
       _RealType __ret;
-      do
+      _RealType __sum = _RealType(0);
+      _RealType __tmp = _RealType(1);
+      for (size_t __k = __m; __k != 0; --__k)
 	{
-	  _RealType __sum = _RealType(0);
-	  _RealType __tmp = _RealType(1);
-	  for (size_t __k = __m; __k != 0; --__k)
-	    {
-	      __sum += _RealType(__urng() - __urng.min()) * __tmp;
-	      __tmp *= __r;
-	    }
-	  __ret = __sum / __tmp;
+	  __sum += _RealType(__urng() - __urng.min()) * __tmp;
+	  __tmp *= __r;
+	}
+      __ret = __sum / __tmp;
+      if (__builtin_expect(__ret >= _RealType(1), 0))
+	{
+#if _GLIBCXX_USE_C99_MATH_TR1
+	  __ret = std::nextafter(_RealType(1), _RealType(0));
+#else
+	  __ret = _RealType(1)
+	    - std::numeric_limits<_RealType>::epsilon() / _RealType(2);
+#endif
 	}
-      while (__builtin_expect(__ret >= _RealType(1), 0));
       return __ret;
     }
 
diff --git a/libstdc++-v3/testsuite/26_numerics/random/uniform_real_distribution/operators/64351.cc b/libstdc++-v3/testsuite/26_numerics/random/uniform_real_distribution/operators/64351.cc
index ddb67e8..229fe9c 100644
--- a/libstdc++-v3/testsuite/26_numerics/random/uniform_real_distribution/operators/64351.cc
+++ b/libstdc++-v3/testsuite/26_numerics/random/uniform_real_distribution/operators/64351.cc
@@ -42,10 +42,18 @@ test02()
   std::mt19937 rng(8890);
   std::seed_seq sequence{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
   rng.seed(sequence);
-  rng.discard(12 * 629143 + 6);
-  float n =
-    std::generate_canonical<float, std::numeric_limits<float>::digits>(rng);
-  VERIFY( n != 1.f );
+  rng.discard(12 * 629143);
+  std::mt19937 rng2{rng};
+  for (int i = 0; i < 20; ++i)
+  {
+    float n =
+      std::generate_canonical<float, std::numeric_limits<float>::digits>(rng);
+    VERIFY( n != 1.f );
+
+    // PR libstdc++/80137
+    rng2.discard(1);
+    VERIFY( rng == rng2 );
+  }
 }
 
 int


More information about the Libstdc++ mailing list