diff mbox

[v2] Implement non-trivial std::random_device::entropy (PR libstdc++/67578)

Message ID 20170524192810.GN12306@redhat.com
State New
Headers show

Commit Message

Jonathan Wakely May 24, 2017, 7:28 p.m. UTC
On 23/05/17 17:15 +0100, Jonathan Wakely wrote:
>On 22/05/17 22:28 +0800, Xi Ruoyao wrote:
>>The new patch is attached.  Just merged the patches you sent and fixed
>>the ChangeLog of gnu.ver and testsuite_abi.cc.
>>
>>(For fun:  I had mistakenly attached the Vim .swp file of the patch
>>and almost sent it. :-p)
>
>Ha, that *definitely* wouldnt' have applied, but I think I'd have
>noticed the reason sooner ;-)
>
>I've committed the patch now - thanks.
>
>
>>@@ -334,6 +335,7 @@ compatible.
>>    <listitem><para>GCC 5.1.0: GLIBCXX_3.4.21, CXXABI_1.3.9</para></listitem>
>>    <listitem><para>GCC 6.1.0: GLIBCXX_3.4.22, CXXABI_1.3.10</para></listitem>
>>    <listitem><para>GCC 7.1.0: GLIBCXX_3.4.23, CXXABI_1.3.11</para></listitem>
>>+    <listitem><para>GCC 8.0.0: GLIBCXX_3.4.24, CXXABI_1.3.10</para></listitem>
>
>Oops, this should have been CXXABI_1.3.11, I fixed that before
>committing it.
>
>>+    if (static_cast<unsigned>(ent) > sizeof(result_type) * 8)
>>+      return static_cast<double>(sizeof(result_type) * 8);
>
>After committing it I realised this should use __CHAR_BIT__ instead of
>hardcoding 8, which I'll fix shortly.

Fixed with this patch. Tested powerpc64le-linux, committed to trunk.
diff mbox

Patch

commit 26e91f1858aa054c28b521d86065966df4ba7099
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Wed May 24 17:41:34 2017 +0100

    Use CHAR_BIT instead of assuming 8 bits
    
    	* src/c++11/random.cc (random_device::_M_getentropy): Use __CHAR_BIT__
    	instead of fixed number of bits.

diff --git a/libstdc++-v3/src/c++11/random.cc b/libstdc++-v3/src/c++11/random.cc
index 5011cf2..ef17223 100644
--- a/libstdc++-v3/src/c++11/random.cc
+++ b/libstdc++-v3/src/c++11/random.cc
@@ -187,8 +187,9 @@  namespace std _GLIBCXX_VISIBILITY(default)
     if (ent < 0)
       return 0.0;
 
-    if (static_cast<unsigned>(ent) > sizeof(result_type) * 8)
-      return static_cast<double>(sizeof(result_type) * 8);
+    const int max = sizeof(result_type) * __CHAR_BIT__;
+    if (ent > max)
+      ent = max;
 
     return static_cast<double>(ent);
 #else