diff options
author | Edward Smith-Rowland <3dw4rd@verizon.net> | 2015-08-26 21:27:09 +0000 |
---|---|---|
committer | Jonathan Wakely <redi@gcc.gnu.org> | 2015-08-26 22:27:09 +0100 |
commit | 33df19a736d340fed203de05071eaeb2110383d6 (patch) | |
tree | 8eec2beadb20e9fa0350b2eb2acefcab3927bbf5 | |
parent | 6bc41b268ea0e16e001f73a9760a9c847c6aa87c (diff) | |
download | gcc-33df19a736d340fed203de05071eaeb2110383d6.zip gcc-33df19a736d340fed203de05071eaeb2110383d6.tar.gz gcc-33df19a736d340fed203de05071eaeb2110383d6.tar.bz2 |
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.
Co-Authored-By: Jonathan Wakely <jwakely@redhat.com>
From-SVN: r227233
-rw-r--r-- | libstdc++-v3/ChangeLog | 10 | ||||
-rw-r--r-- | libstdc++-v3/include/bits/random.tcc | 21 | ||||
-rw-r--r-- | libstdc++-v3/testsuite/26_numerics/random/uniform_real_distribution/operators/64351.cc | 57 |
3 files changed, 81 insertions, 7 deletions
diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 0e99af6..106b23a 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -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 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(); +} |