]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Ensure std::generate_canonical doesn't return 1.
authorEdward Smith-Rowland <3dw4rd@verizon.net>
Wed, 26 Aug 2015 21:27:09 +0000 (21:27 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Wed, 26 Aug 2015 21:27:09 +0000 (22:27 +0100)
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.

Co-Authored-By: Jonathan Wakely <jwakely@redhat.com>
From-SVN: r227233

libstdc++-v3/ChangeLog
libstdc++-v3/include/bits/random.tcc
libstdc++-v3/testsuite/26_numerics/random/uniform_real_distribution/operators/64351.cc [new file with mode: 0644]

index 0e99af68374c5034c639aa36ac9e839d57bd8a79..106b23a641d8b426086b0b5710666fcd7681e744 100644 (file)
@@ -1,3 +1,13 @@
+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.
+
 2015-08-26  Jonathan Wakely  <jwakely@redhat.com>
 
        * include/bits/shared_ptr.h (__enable_shared_from_this_helper): Use
index 4fdbcfcf48105250d0bd251705e10377c08f7897..a6d966b28d78f4838cb13ab8ca2015d2490ade7f 100644 (file)
@@ -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 (file)
index 0000000..3de4412
--- /dev/null
@@ -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();
+}