From: Edward Smith-Rowland <3dw4rd@verizon.net> Date: Wed, 26 Aug 2015 21:27:09 +0000 (+0000) Subject: Ensure std::generate_canonical doesn't return 1. X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=33df19a736d340fed203de05071eaeb2110383d6;p=gcc.git Ensure std::generate_canonical doesn't return 1. 2015-08-26 Edward Smith-Rowland <3dw4rd@verizon.net> Jonathan Wakely 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 From-SVN: r227233 --- diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 0e99af68374..106b23a641d 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,13 @@ +2015-08-26 Edward Smith-Rowland <3dw4rd@verizon.net> + Jonathan Wakely + + 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 * 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 4fdbcfcf481..a6d966b28d7 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(__urng.max()) - static_cast(__urng.min()) + 1.0L; const size_t __log2r = std::log(__r) / std::log(2.0L); - size_t __k = std::max(1UL, (__b + __log2r - 1UL) / __log2r); - _RealType __sum = _RealType(0); - _RealType __tmp = _RealType(1); - for (; __k != 0; --__k) + const size_t __m = std::max(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 00000000000..3de441234b5 --- /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 +// . + +// { dg-options "-std=gnu++11" } +// { dg-do run { target { ! simulator } } } + +#include +#include + +// libstdc++/64351 +void +test01() +{ + std::mt19937 rng(8890); + std::uniform_real_distribution 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::digits>(rng); + VERIFY( n != 1.f ); +} + +int +main() +{ + test01(); + test02(); +}