]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/src/c++11/random.cc
3 // Copyright (C) 2012 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
27 #if defined __i386__ || defined __x86_64__
32 namespace std
_GLIBCXX_VISIBILITY(default)
38 _M_strtoul(const std::string
& __str
)
40 unsigned long __ret
= 5489UL;
41 if (__str
!= "mt19937")
43 const char* __nptr
= __str
.c_str();
45 __ret
= std::strtoul(__nptr
, &__endptr
, 0);
46 if (*__nptr
== '\0' || *__endptr
!= '\0')
47 std::__throw_runtime_error(__N("random_device::_M_strtoul"
48 "(const std::string&)"));
53 #if defined __i386__ || defined __x86_64__
55 __attribute__ ((target("rdrnd")))
58 unsigned int retries
= 100;
61 while (__builtin_ia32_rdrand32_step(&val
) == 0)
63 std::__throw_runtime_error(__N("random_device::__x86_rdrand(void)"));
72 random_device::_M_init(const std::string
& token
)
74 const char *fname
= token
.c_str();
76 if (token
== "default")
78 #if defined __i386__ || defined __x86_64__
79 unsigned int eax
, ebx
, ecx
, edx
;
80 __cpuid(0, eax
, ebx
, ecx
, edx
);
81 // Check for "GenuineIntel"
82 if (ebx
== 0x756e6547 && ecx
== 0x6c65746e && edx
== 0x49656e69)
84 __cpuid(1, eax
, ebx
, ecx
, edx
);
93 fname
= "/dev/urandom";
95 else if (token
!= "/dev/urandom" && token
!= "/dev/random")
97 std::__throw_runtime_error(__N("random_device::"
98 "random_device(const std::string&)"));
100 _M_file
= std::fopen(fname
, "rb");
106 random_device::_M_init_pretr1(const std::string
& token
)
109 _M_mt
.seed(_M_strtoul(token
));
113 random_device::_M_fini()
116 std::fclose(_M_file
);
119 random_device::result_type
120 random_device::_M_getval()
122 #if (defined __i386__ || defined __x86_64__)
124 return __x86_rdrand();
128 std::fread(reinterpret_cast<void*>(&__ret
), sizeof(result_type
),
133 random_device::result_type
134 random_device::_M_getval_pretr1()
139 template class mersenne_twister_engine
<
145 0xefc60000UL
, 18, 1812433253UL>;