linear_congruential_engine.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef _LIBCPP___RANDOM_LINEAR_CONGRUENTIAL_ENGINE_H
  9. #define _LIBCPP___RANDOM_LINEAR_CONGRUENTIAL_ENGINE_H
  10. #include <__config>
  11. #include <__random/is_seed_sequence.h>
  12. #include <__type_traits/enable_if.h>
  13. #include <__type_traits/integral_constant.h>
  14. #include <__type_traits/is_unsigned.h>
  15. #include <cstdint>
  16. #include <iosfwd>
  17. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  18. # pragma GCC system_header
  19. #endif
  20. _LIBCPP_PUSH_MACROS
  21. #include <__undef_macros>
  22. _LIBCPP_BEGIN_NAMESPACE_STD
  23. template <unsigned long long __a, unsigned long long __c,
  24. unsigned long long __m, unsigned long long _Mp,
  25. bool _MightOverflow = (__a != 0 && __m != 0 && __m-1 > (_Mp-__c)/__a),
  26. bool _OverflowOK = ((__m | (__m-1)) > __m), // m = 2^n
  27. bool _SchrageOK = (__a != 0 && __m != 0 && __m % __a <= __m / __a)> // r <= q
  28. struct __lce_alg_picker
  29. {
  30. static_assert(__a != 0 || __m != 0 || !_MightOverflow || _OverflowOK || _SchrageOK,
  31. "The current values of a, c, and m cannot generate a number "
  32. "within bounds of linear_congruential_engine.");
  33. static _LIBCPP_CONSTEXPR const bool __use_schrage = _MightOverflow &&
  34. !_OverflowOK &&
  35. _SchrageOK;
  36. };
  37. template <unsigned long long __a, unsigned long long __c,
  38. unsigned long long __m, unsigned long long _Mp,
  39. bool _UseSchrage = __lce_alg_picker<__a, __c, __m, _Mp>::__use_schrage>
  40. struct __lce_ta;
  41. // 64
  42. template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
  43. struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), true>
  44. {
  45. typedef unsigned long long result_type;
  46. _LIBCPP_INLINE_VISIBILITY
  47. static result_type next(result_type __x)
  48. {
  49. // Schrage's algorithm
  50. const result_type __q = __m / __a;
  51. const result_type __r = __m % __a;
  52. const result_type __t0 = __a * (__x % __q);
  53. const result_type __t1 = __r * (__x / __q);
  54. __x = __t0 + (__t0 < __t1) * __m - __t1;
  55. __x += __c - (__x >= __m - __c) * __m;
  56. return __x;
  57. }
  58. };
  59. template <unsigned long long __a, unsigned long long __m>
  60. struct __lce_ta<__a, 0, __m, (unsigned long long)(~0), true>
  61. {
  62. typedef unsigned long long result_type;
  63. _LIBCPP_INLINE_VISIBILITY
  64. static result_type next(result_type __x)
  65. {
  66. // Schrage's algorithm
  67. const result_type __q = __m / __a;
  68. const result_type __r = __m % __a;
  69. const result_type __t0 = __a * (__x % __q);
  70. const result_type __t1 = __r * (__x / __q);
  71. __x = __t0 + (__t0 < __t1) * __m - __t1;
  72. return __x;
  73. }
  74. };
  75. template <unsigned long long __a, unsigned long long __c, unsigned long long __m>
  76. struct __lce_ta<__a, __c, __m, (unsigned long long)(~0), false>
  77. {
  78. typedef unsigned long long result_type;
  79. _LIBCPP_INLINE_VISIBILITY
  80. static result_type next(result_type __x)
  81. {
  82. return (__a * __x + __c) % __m;
  83. }
  84. };
  85. template <unsigned long long __a, unsigned long long __c>
  86. struct __lce_ta<__a, __c, 0, (unsigned long long)(~0), false>
  87. {
  88. typedef unsigned long long result_type;
  89. _LIBCPP_INLINE_VISIBILITY
  90. static result_type next(result_type __x)
  91. {
  92. return __a * __x + __c;
  93. }
  94. };
  95. // 32
  96. template <unsigned long long _Ap, unsigned long long _Cp, unsigned long long _Mp>
  97. struct __lce_ta<_Ap, _Cp, _Mp, unsigned(~0), true>
  98. {
  99. typedef unsigned result_type;
  100. _LIBCPP_INLINE_VISIBILITY
  101. static result_type next(result_type __x)
  102. {
  103. const result_type __a = static_cast<result_type>(_Ap);
  104. const result_type __c = static_cast<result_type>(_Cp);
  105. const result_type __m = static_cast<result_type>(_Mp);
  106. // Schrage's algorithm
  107. const result_type __q = __m / __a;
  108. const result_type __r = __m % __a;
  109. const result_type __t0 = __a * (__x % __q);
  110. const result_type __t1 = __r * (__x / __q);
  111. __x = __t0 + (__t0 < __t1) * __m - __t1;
  112. __x += __c - (__x >= __m - __c) * __m;
  113. return __x;
  114. }
  115. };
  116. template <unsigned long long _Ap, unsigned long long _Mp>
  117. struct __lce_ta<_Ap, 0, _Mp, unsigned(~0), true>
  118. {
  119. typedef unsigned result_type;
  120. _LIBCPP_INLINE_VISIBILITY
  121. static result_type next(result_type __x)
  122. {
  123. const result_type __a = static_cast<result_type>(_Ap);
  124. const result_type __m = static_cast<result_type>(_Mp);
  125. // Schrage's algorithm
  126. const result_type __q = __m / __a;
  127. const result_type __r = __m % __a;
  128. const result_type __t0 = __a * (__x % __q);
  129. const result_type __t1 = __r * (__x / __q);
  130. __x = __t0 + (__t0 < __t1) * __m - __t1;
  131. return __x;
  132. }
  133. };
  134. template <unsigned long long _Ap, unsigned long long _Cp, unsigned long long _Mp>
  135. struct __lce_ta<_Ap, _Cp, _Mp, unsigned(~0), false>
  136. {
  137. typedef unsigned result_type;
  138. _LIBCPP_INLINE_VISIBILITY
  139. static result_type next(result_type __x)
  140. {
  141. const result_type __a = static_cast<result_type>(_Ap);
  142. const result_type __c = static_cast<result_type>(_Cp);
  143. const result_type __m = static_cast<result_type>(_Mp);
  144. return (__a * __x + __c) % __m;
  145. }
  146. };
  147. template <unsigned long long _Ap, unsigned long long _Cp>
  148. struct __lce_ta<_Ap, _Cp, 0, unsigned(~0), false>
  149. {
  150. typedef unsigned result_type;
  151. _LIBCPP_INLINE_VISIBILITY
  152. static result_type next(result_type __x)
  153. {
  154. const result_type __a = static_cast<result_type>(_Ap);
  155. const result_type __c = static_cast<result_type>(_Cp);
  156. return __a * __x + __c;
  157. }
  158. };
  159. // 16
  160. template <unsigned long long __a, unsigned long long __c, unsigned long long __m, bool __b>
  161. struct __lce_ta<__a, __c, __m, (unsigned short)(~0), __b>
  162. {
  163. typedef unsigned short result_type;
  164. _LIBCPP_INLINE_VISIBILITY
  165. static result_type next(result_type __x)
  166. {
  167. return static_cast<result_type>(__lce_ta<__a, __c, __m, unsigned(~0)>::next(__x));
  168. }
  169. };
  170. template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
  171. class _LIBCPP_TEMPLATE_VIS linear_congruential_engine;
  172. template <class _CharT, class _Traits,
  173. class _Up, _Up _Ap, _Up _Cp, _Up _Np>
  174. _LIBCPP_INLINE_VISIBILITY
  175. basic_ostream<_CharT, _Traits>&
  176. operator<<(basic_ostream<_CharT, _Traits>& __os,
  177. const linear_congruential_engine<_Up, _Ap, _Cp, _Np>&);
  178. template <class _CharT, class _Traits,
  179. class _Up, _Up _Ap, _Up _Cp, _Up _Np>
  180. _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
  181. operator>>(basic_istream<_CharT, _Traits>& __is,
  182. linear_congruential_engine<_Up, _Ap, _Cp, _Np>& __x);
  183. template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
  184. class _LIBCPP_TEMPLATE_VIS linear_congruential_engine
  185. {
  186. public:
  187. // types
  188. typedef _UIntType result_type;
  189. private:
  190. result_type __x_;
  191. static _LIBCPP_CONSTEXPR const result_type _Mp = result_type(~0);
  192. static_assert(__m == 0 || __a < __m, "linear_congruential_engine invalid parameters");
  193. static_assert(__m == 0 || __c < __m, "linear_congruential_engine invalid parameters");
  194. static_assert(is_unsigned<_UIntType>::value, "_UIntType must be unsigned type");
  195. public:
  196. static _LIBCPP_CONSTEXPR const result_type _Min = __c == 0u ? 1u : 0u;
  197. static _LIBCPP_CONSTEXPR const result_type _Max = __m - _UIntType(1u);
  198. static_assert(_Min < _Max, "linear_congruential_engine invalid parameters");
  199. // engine characteristics
  200. static _LIBCPP_CONSTEXPR const result_type multiplier = __a;
  201. static _LIBCPP_CONSTEXPR const result_type increment = __c;
  202. static _LIBCPP_CONSTEXPR const result_type modulus = __m;
  203. _LIBCPP_INLINE_VISIBILITY
  204. static _LIBCPP_CONSTEXPR result_type min() {return _Min;}
  205. _LIBCPP_INLINE_VISIBILITY
  206. static _LIBCPP_CONSTEXPR result_type max() {return _Max;}
  207. static _LIBCPP_CONSTEXPR const result_type default_seed = 1u;
  208. // constructors and seeding functions
  209. #ifndef _LIBCPP_CXX03_LANG
  210. _LIBCPP_INLINE_VISIBILITY
  211. linear_congruential_engine() : linear_congruential_engine(default_seed) {}
  212. _LIBCPP_INLINE_VISIBILITY
  213. explicit linear_congruential_engine(result_type __s) { seed(__s); }
  214. #else
  215. _LIBCPP_INLINE_VISIBILITY
  216. explicit linear_congruential_engine(result_type __s = default_seed) {
  217. seed(__s);
  218. }
  219. #endif
  220. template<class _Sseq, __enable_if_t<__is_seed_sequence<_Sseq, linear_congruential_engine>::value, int> = 0>
  221. _LIBCPP_INLINE_VISIBILITY
  222. explicit linear_congruential_engine(_Sseq& __q)
  223. {seed(__q);}
  224. _LIBCPP_INLINE_VISIBILITY
  225. void seed(result_type __s = default_seed)
  226. {seed(integral_constant<bool, __m == 0>(),
  227. integral_constant<bool, __c == 0>(), __s);}
  228. template<class _Sseq, __enable_if_t<__is_seed_sequence<_Sseq, linear_congruential_engine>::value, int> = 0>
  229. _LIBCPP_INLINE_VISIBILITY
  230. void
  231. seed(_Sseq& __q)
  232. {__seed(__q, integral_constant<unsigned,
  233. 1 + (__m == 0 ? (sizeof(result_type) * __CHAR_BIT__ - 1)/32
  234. : (__m > 0x100000000ull))>());}
  235. // generating functions
  236. _LIBCPP_INLINE_VISIBILITY
  237. result_type operator()()
  238. {return __x_ = static_cast<result_type>(__lce_ta<__a, __c, __m, _Mp>::next(__x_));}
  239. _LIBCPP_INLINE_VISIBILITY
  240. void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
  241. friend _LIBCPP_INLINE_VISIBILITY
  242. bool operator==(const linear_congruential_engine& __x,
  243. const linear_congruential_engine& __y)
  244. {return __x.__x_ == __y.__x_;}
  245. friend _LIBCPP_INLINE_VISIBILITY
  246. bool operator!=(const linear_congruential_engine& __x,
  247. const linear_congruential_engine& __y)
  248. {return !(__x == __y);}
  249. private:
  250. _LIBCPP_INLINE_VISIBILITY
  251. void seed(true_type, true_type, result_type __s) {__x_ = __s == 0 ? 1 : __s;}
  252. _LIBCPP_INLINE_VISIBILITY
  253. void seed(true_type, false_type, result_type __s) {__x_ = __s;}
  254. _LIBCPP_INLINE_VISIBILITY
  255. void seed(false_type, true_type, result_type __s) {__x_ = __s % __m == 0 ?
  256. 1 : __s % __m;}
  257. _LIBCPP_INLINE_VISIBILITY
  258. void seed(false_type, false_type, result_type __s) {__x_ = __s % __m;}
  259. template<class _Sseq>
  260. _LIBCPP_HIDE_FROM_ABI void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
  261. template<class _Sseq>
  262. _LIBCPP_HIDE_FROM_ABI void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
  263. template <class _CharT, class _Traits,
  264. class _Up, _Up _Ap, _Up _Cp, _Up _Np>
  265. friend
  266. basic_ostream<_CharT, _Traits>&
  267. operator<<(basic_ostream<_CharT, _Traits>& __os,
  268. const linear_congruential_engine<_Up, _Ap, _Cp, _Np>&);
  269. template <class _CharT, class _Traits,
  270. class _Up, _Up _Ap, _Up _Cp, _Up _Np>
  271. friend
  272. basic_istream<_CharT, _Traits>&
  273. operator>>(basic_istream<_CharT, _Traits>& __is,
  274. linear_congruential_engine<_Up, _Ap, _Cp, _Np>& __x);
  275. };
  276. template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
  277. _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type
  278. linear_congruential_engine<_UIntType, __a, __c, __m>::multiplier;
  279. template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
  280. _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type
  281. linear_congruential_engine<_UIntType, __a, __c, __m>::increment;
  282. template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
  283. _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type
  284. linear_congruential_engine<_UIntType, __a, __c, __m>::modulus;
  285. template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
  286. _LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type
  287. linear_congruential_engine<_UIntType, __a, __c, __m>::default_seed;
  288. template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
  289. template<class _Sseq>
  290. void
  291. linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
  292. integral_constant<unsigned, 1>)
  293. {
  294. const unsigned __k = 1;
  295. uint32_t __ar[__k+3];
  296. __q.generate(__ar, __ar + __k + 3);
  297. result_type __s = static_cast<result_type>(__ar[3] % __m);
  298. __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
  299. }
  300. template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
  301. template<class _Sseq>
  302. void
  303. linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q,
  304. integral_constant<unsigned, 2>)
  305. {
  306. const unsigned __k = 2;
  307. uint32_t __ar[__k+3];
  308. __q.generate(__ar, __ar + __k + 3);
  309. result_type __s = static_cast<result_type>((__ar[3] +
  310. ((uint64_t)__ar[4] << 32)) % __m);
  311. __x_ = __c == 0 && __s == 0 ? result_type(1) : __s;
  312. }
  313. template <class _CharT, class _Traits,
  314. class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
  315. inline _LIBCPP_INLINE_VISIBILITY
  316. basic_ostream<_CharT, _Traits>&
  317. operator<<(basic_ostream<_CharT, _Traits>& __os,
  318. const linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
  319. {
  320. __save_flags<_CharT, _Traits> __lx(__os);
  321. typedef basic_ostream<_CharT, _Traits> _Ostream;
  322. __os.flags(_Ostream::dec | _Ostream::left);
  323. __os.fill(__os.widen(' '));
  324. return __os << __x.__x_;
  325. }
  326. template <class _CharT, class _Traits,
  327. class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
  328. _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
  329. operator>>(basic_istream<_CharT, _Traits>& __is,
  330. linear_congruential_engine<_UIntType, __a, __c, __m>& __x)
  331. {
  332. __save_flags<_CharT, _Traits> __lx(__is);
  333. typedef basic_istream<_CharT, _Traits> _Istream;
  334. __is.flags(_Istream::dec | _Istream::skipws);
  335. _UIntType __t;
  336. __is >> __t;
  337. if (!__is.fail())
  338. __x.__x_ = __t;
  339. return __is;
  340. }
  341. typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647>
  342. minstd_rand0;
  343. typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647>
  344. minstd_rand;
  345. _LIBCPP_END_NAMESPACE_STD
  346. _LIBCPP_POP_MACROS
  347. #endif // _LIBCPP___RANDOM_LINEAR_CONGRUENTIAL_ENGINE_H