diff mbox

libstdc++/64351 Ensure std::generate_canonical doesn't return 1.

Message ID 20150826145408.GF2631@redhat.com
State New
Headers show

Commit Message

Jonathan Wakely Aug. 26, 2015, 2:54 p.m. UTC
Ed posted this patch to https://gcc.gnu.org/PR64351 in January, I've
tested it and am committing it to trunk with a test.
diff mbox

Patch

commit 45f154a5f9172a17f6226b99b41cb9c0bd8d15ec
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Wed Aug 26 12:53:08 2015 +0100

    Ensure std::generate_canonical doesn't return 1.
    
    2015-08-26  Edward Smith-Rowland  <3dw4rd@verizon.net>
    	    Jonathan Wakely  <jwakely@redhat.com>
    
    	PR libstdc++/64351
    	PR libstdc++/63176
    	* include/bits/random.tcc (generate_canonical): Loop until we get a
    	result less than one.
    	* testsuite/26_numerics/random/uniform_real_distribution/operators/
    	64351.cc: New.

diff --git a/libstdc++-v3/include/bits/random.tcc b/libstdc++-v3/include/bits/random.tcc
index 4fdbcfc..a6d966b 100644
--- a/libstdc++-v3/include/bits/random.tcc
+++ b/libstdc++-v3/include/bits/random.tcc
@@ -3472,15 +3472,22 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
       const long double __r = static_cast<long double>(__urng.max())
 			    - static_cast<long double>(__urng.min()) + 1.0L;
       const size_t __log2r = std::log(__r) / std::log(2.0L);
-      size_t __k = std::max<size_t>(1UL, (__b + __log2r - 1UL) / __log2r);
-      _RealType __sum = _RealType(0);
-      _RealType __tmp = _RealType(1);
-      for (; __k != 0; --__k)
+      const size_t __m = std::max<size_t>(1UL,
+					  (__b + __log2r - 1UL) / __log2r);
+      _RealType __ret;
+      do
 	{
-	  __sum += _RealType(__urng() - __urng.min()) * __tmp;
-	  __tmp *= __r;
+	  _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;
 	}
-      return __sum / __tmp;
+      while (__builtin_expect(__ret >= _RealType(1), 0));
+      return __ret;
     }
 
 _GLIBCXX_END_NAMESPACE_VERSION
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
new file mode 100644
index 0000000..3de4412
--- /dev/null
+++ b/libstdc++-v3/testsuite/26_numerics/random/uniform_real_distribution/operators/64351.cc
@@ -0,0 +1,57 @@ 
+// Copyright (C) 2015 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++11" }
+// { dg-do run { target { ! simulator } } }
+
+#include <random>
+#include <testsuite_hooks.h>
+
+// libstdc++/64351
+void
+test01()
+{
+  std::mt19937 rng(8890);
+  std::uniform_real_distribution<float> dist;
+
+  rng.discard(30e6);
+  for (long i = 0; i < 10e6; ++i)
+    {
+      auto n = dist(rng);
+      VERIFY( n != 1.f );
+    }
+}
+
+// libstdc++/63176
+void
+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 );
+}
+
+int
+main()
+{
+  test01();
+  test02();
+}