Path: blob/main/contrib/llvm-project/libcxx/include/__random/linear_congruential_engine.h
35233 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef _LIBCPP___RANDOM_LINEAR_CONGRUENTIAL_ENGINE_H9#define _LIBCPP___RANDOM_LINEAR_CONGRUENTIAL_ENGINE_H1011#include <__config>12#include <__random/is_seed_sequence.h>13#include <__type_traits/enable_if.h>14#include <__type_traits/integral_constant.h>15#include <__type_traits/is_unsigned.h>16#include <cstdint>17#include <iosfwd>1819#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)20# pragma GCC system_header21#endif2223_LIBCPP_PUSH_MACROS24#include <__undef_macros>2526_LIBCPP_BEGIN_NAMESPACE_STD2728enum __lce_alg_type {29_LCE_Full,30_LCE_Part,31_LCE_Schrage,32_LCE_Promote,33};3435template <unsigned long long __a,36unsigned long long __c,37unsigned long long __m,38unsigned long long _Mp,39bool _HasOverflow = (__a != 0ull && (__m & (__m - 1ull)) != 0ull), // a != 0, m != 0, m != 2^n40bool _Full = (!_HasOverflow || __m - 1ull <= (_Mp - __c) / __a), // (a * x + c) % m works41bool _Part = (!_HasOverflow || __m - 1ull <= _Mp / __a), // (a * x) % m works42bool _Schrage = (_HasOverflow && __m % __a <= __m / __a)> // r <= q43struct __lce_alg_picker {44static _LIBCPP_CONSTEXPR const __lce_alg_type __mode =45_Full ? _LCE_Full46: _Part ? _LCE_Part47: _Schrage ? _LCE_Schrage48: _LCE_Promote;4950#ifdef _LIBCPP_HAS_NO_INT12851static_assert(_Mp != (unsigned long long)(-1) || _Full || _Part || _Schrage,52"The current values for a, c, and m are not currently supported on platforms without __int128");53#endif54};5556template <unsigned long long __a,57unsigned long long __c,58unsigned long long __m,59unsigned long long _Mp,60__lce_alg_type _Mode = __lce_alg_picker<__a, __c, __m, _Mp>::__mode>61struct __lce_ta;6263// 646465#ifndef _LIBCPP_HAS_NO_INT12866template <unsigned long long _Ap, unsigned long long _Cp, unsigned long long _Mp>67struct __lce_ta<_Ap, _Cp, _Mp, (unsigned long long)(-1), _LCE_Promote> {68typedef unsigned long long result_type;69_LIBCPP_HIDE_FROM_ABI static result_type next(result_type __xp) {70__extension__ using __calc_type = unsigned __int128;71const __calc_type __a = static_cast<__calc_type>(_Ap);72const __calc_type __c = static_cast<__calc_type>(_Cp);73const __calc_type __m = static_cast<__calc_type>(_Mp);74const __calc_type __x = static_cast<__calc_type>(__xp);75return static_cast<result_type>((__a * __x + __c) % __m);76}77};78#endif7980template <unsigned long long __a, unsigned long long __c, unsigned long long __m>81struct __lce_ta<__a, __c, __m, (unsigned long long)(-1), _LCE_Schrage> {82typedef unsigned long long result_type;83_LIBCPP_HIDE_FROM_ABI static result_type next(result_type __x) {84// Schrage's algorithm85const result_type __q = __m / __a;86const result_type __r = __m % __a;87const result_type __t0 = __a * (__x % __q);88const result_type __t1 = __r * (__x / __q);89__x = __t0 + (__t0 < __t1) * __m - __t1;90__x += __c - (__x >= __m - __c) * __m;91return __x;92}93};9495template <unsigned long long __a, unsigned long long __m>96struct __lce_ta<__a, 0ull, __m, (unsigned long long)(-1), _LCE_Schrage> {97typedef unsigned long long result_type;98_LIBCPP_HIDE_FROM_ABI static result_type next(result_type __x) {99// Schrage's algorithm100const result_type __q = __m / __a;101const result_type __r = __m % __a;102const result_type __t0 = __a * (__x % __q);103const result_type __t1 = __r * (__x / __q);104__x = __t0 + (__t0 < __t1) * __m - __t1;105return __x;106}107};108109template <unsigned long long __a, unsigned long long __c, unsigned long long __m>110struct __lce_ta<__a, __c, __m, (unsigned long long)(-1), _LCE_Part> {111typedef unsigned long long result_type;112_LIBCPP_HIDE_FROM_ABI static result_type next(result_type __x) {113// Use (((a*x) % m) + c) % m114__x = (__a * __x) % __m;115__x += __c - (__x >= __m - __c) * __m;116return __x;117}118};119120template <unsigned long long __a, unsigned long long __c, unsigned long long __m>121struct __lce_ta<__a, __c, __m, (unsigned long long)(-1), _LCE_Full> {122typedef unsigned long long result_type;123_LIBCPP_HIDE_FROM_ABI static result_type next(result_type __x) { return (__a * __x + __c) % __m; }124};125126template <unsigned long long __a, unsigned long long __c>127struct __lce_ta<__a, __c, 0ull, (unsigned long long)(-1), _LCE_Full> {128typedef unsigned long long result_type;129_LIBCPP_HIDE_FROM_ABI static result_type next(result_type __x) { return __a * __x + __c; }130};131132// 32133134template <unsigned long long __a, unsigned long long __c, unsigned long long __m>135struct __lce_ta<__a, __c, __m, unsigned(-1), _LCE_Promote> {136typedef unsigned result_type;137_LIBCPP_HIDE_FROM_ABI static result_type next(result_type __x) {138return static_cast<result_type>(__lce_ta<__a, __c, __m, (unsigned long long)(-1)>::next(__x));139}140};141142template <unsigned long long _Ap, unsigned long long _Cp, unsigned long long _Mp>143struct __lce_ta<_Ap, _Cp, _Mp, unsigned(-1), _LCE_Schrage> {144typedef unsigned result_type;145_LIBCPP_HIDE_FROM_ABI static result_type next(result_type __x) {146const result_type __a = static_cast<result_type>(_Ap);147const result_type __c = static_cast<result_type>(_Cp);148const result_type __m = static_cast<result_type>(_Mp);149// Schrage's algorithm150const result_type __q = __m / __a;151const result_type __r = __m % __a;152const result_type __t0 = __a * (__x % __q);153const result_type __t1 = __r * (__x / __q);154__x = __t0 + (__t0 < __t1) * __m - __t1;155__x += __c - (__x >= __m - __c) * __m;156return __x;157}158};159160template <unsigned long long _Ap, unsigned long long _Mp>161struct __lce_ta<_Ap, 0ull, _Mp, unsigned(-1), _LCE_Schrage> {162typedef unsigned result_type;163_LIBCPP_HIDE_FROM_ABI static result_type next(result_type __x) {164const result_type __a = static_cast<result_type>(_Ap);165const result_type __m = static_cast<result_type>(_Mp);166// Schrage's algorithm167const result_type __q = __m / __a;168const result_type __r = __m % __a;169const result_type __t0 = __a * (__x % __q);170const result_type __t1 = __r * (__x / __q);171__x = __t0 + (__t0 < __t1) * __m - __t1;172return __x;173}174};175176template <unsigned long long _Ap, unsigned long long _Cp, unsigned long long _Mp>177struct __lce_ta<_Ap, _Cp, _Mp, unsigned(-1), _LCE_Part> {178typedef unsigned result_type;179_LIBCPP_HIDE_FROM_ABI static result_type next(result_type __x) {180const result_type __a = static_cast<result_type>(_Ap);181const result_type __c = static_cast<result_type>(_Cp);182const result_type __m = static_cast<result_type>(_Mp);183// Use (((a*x) % m) + c) % m184__x = (__a * __x) % __m;185__x += __c - (__x >= __m - __c) * __m;186return __x;187}188};189190template <unsigned long long _Ap, unsigned long long _Cp, unsigned long long _Mp>191struct __lce_ta<_Ap, _Cp, _Mp, unsigned(-1), _LCE_Full> {192typedef unsigned result_type;193_LIBCPP_HIDE_FROM_ABI static result_type next(result_type __x) {194const result_type __a = static_cast<result_type>(_Ap);195const result_type __c = static_cast<result_type>(_Cp);196const result_type __m = static_cast<result_type>(_Mp);197return (__a * __x + __c) % __m;198}199};200201template <unsigned long long _Ap, unsigned long long _Cp>202struct __lce_ta<_Ap, _Cp, 0ull, unsigned(-1), _LCE_Full> {203typedef unsigned result_type;204_LIBCPP_HIDE_FROM_ABI static result_type next(result_type __x) {205const result_type __a = static_cast<result_type>(_Ap);206const result_type __c = static_cast<result_type>(_Cp);207return __a * __x + __c;208}209};210211// 16212213template <unsigned long long __a, unsigned long long __c, unsigned long long __m, __lce_alg_type __mode>214struct __lce_ta<__a, __c, __m, (unsigned short)(-1), __mode> {215typedef unsigned short result_type;216_LIBCPP_HIDE_FROM_ABI static result_type next(result_type __x) {217return static_cast<result_type>(__lce_ta<__a, __c, __m, unsigned(-1)>::next(__x));218}219};220221template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>222class _LIBCPP_TEMPLATE_VIS linear_congruential_engine;223224template <class _CharT, class _Traits, class _Up, _Up _Ap, _Up _Cp, _Up _Np>225_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&226operator<<(basic_ostream<_CharT, _Traits>& __os, const linear_congruential_engine<_Up, _Ap, _Cp, _Np>&);227228template <class _CharT, class _Traits, class _Up, _Up _Ap, _Up _Cp, _Up _Np>229_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&230operator>>(basic_istream<_CharT, _Traits>& __is, linear_congruential_engine<_Up, _Ap, _Cp, _Np>& __x);231232template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>233class _LIBCPP_TEMPLATE_VIS linear_congruential_engine {234public:235// types236typedef _UIntType result_type;237238private:239result_type __x_;240241static _LIBCPP_CONSTEXPR const result_type _Mp = result_type(-1);242243static_assert(__m == 0 || __a < __m, "linear_congruential_engine invalid parameters");244static_assert(__m == 0 || __c < __m, "linear_congruential_engine invalid parameters");245static_assert(is_unsigned<_UIntType>::value, "_UIntType must be unsigned type");246247public:248static _LIBCPP_CONSTEXPR const result_type _Min = __c == 0u ? 1u : 0u;249static _LIBCPP_CONSTEXPR const result_type _Max = __m - _UIntType(1u);250static_assert(_Min < _Max, "linear_congruential_engine invalid parameters");251252// engine characteristics253static _LIBCPP_CONSTEXPR const result_type multiplier = __a;254static _LIBCPP_CONSTEXPR const result_type increment = __c;255static _LIBCPP_CONSTEXPR const result_type modulus = __m;256_LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR result_type min() { return _Min; }257_LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR result_type max() { return _Max; }258static _LIBCPP_CONSTEXPR const result_type default_seed = 1u;259260// constructors and seeding functions261#ifndef _LIBCPP_CXX03_LANG262_LIBCPP_HIDE_FROM_ABI linear_congruential_engine() : linear_congruential_engine(default_seed) {}263_LIBCPP_HIDE_FROM_ABI explicit linear_congruential_engine(result_type __s) { seed(__s); }264#else265_LIBCPP_HIDE_FROM_ABI explicit linear_congruential_engine(result_type __s = default_seed) { seed(__s); }266#endif267template <class _Sseq, __enable_if_t<__is_seed_sequence<_Sseq, linear_congruential_engine>::value, int> = 0>268_LIBCPP_HIDE_FROM_ABI explicit linear_congruential_engine(_Sseq& __q) {269seed(__q);270}271_LIBCPP_HIDE_FROM_ABI void seed(result_type __s = default_seed) {272seed(integral_constant<bool, __m == 0>(), integral_constant<bool, __c == 0>(), __s);273}274template <class _Sseq, __enable_if_t<__is_seed_sequence<_Sseq, linear_congruential_engine>::value, int> = 0>275_LIBCPP_HIDE_FROM_ABI void seed(_Sseq& __q) {276__seed(277__q,278integral_constant<unsigned,2791 + (__m == 0 ? (sizeof(result_type) * __CHAR_BIT__ - 1) / 32 : (__m > 0x100000000ull))>());280}281282// generating functions283_LIBCPP_HIDE_FROM_ABI result_type operator()() {284return __x_ = static_cast<result_type>(__lce_ta<__a, __c, __m, _Mp>::next(__x_));285}286_LIBCPP_HIDE_FROM_ABI void discard(unsigned long long __z) {287for (; __z; --__z)288operator()();289}290291friend _LIBCPP_HIDE_FROM_ABI bool292operator==(const linear_congruential_engine& __x, const linear_congruential_engine& __y) {293return __x.__x_ == __y.__x_;294}295friend _LIBCPP_HIDE_FROM_ABI bool296operator!=(const linear_congruential_engine& __x, const linear_congruential_engine& __y) {297return !(__x == __y);298}299300private:301_LIBCPP_HIDE_FROM_ABI void seed(true_type, true_type, result_type __s) { __x_ = __s == 0 ? 1 : __s; }302_LIBCPP_HIDE_FROM_ABI void seed(true_type, false_type, result_type __s) { __x_ = __s; }303_LIBCPP_HIDE_FROM_ABI void seed(false_type, true_type, result_type __s) { __x_ = __s % __m == 0 ? 1 : __s % __m; }304_LIBCPP_HIDE_FROM_ABI void seed(false_type, false_type, result_type __s) { __x_ = __s % __m; }305306template <class _Sseq>307_LIBCPP_HIDE_FROM_ABI void __seed(_Sseq& __q, integral_constant<unsigned, 1>);308template <class _Sseq>309_LIBCPP_HIDE_FROM_ABI void __seed(_Sseq& __q, integral_constant<unsigned, 2>);310311template <class _CharT, class _Traits, class _Up, _Up _Ap, _Up _Cp, _Up _Np>312friend basic_ostream<_CharT, _Traits>&313operator<<(basic_ostream<_CharT, _Traits>& __os, const linear_congruential_engine<_Up, _Ap, _Cp, _Np>&);314315template <class _CharT, class _Traits, class _Up, _Up _Ap, _Up _Cp, _Up _Np>316friend basic_istream<_CharT, _Traits>&317operator>>(basic_istream<_CharT, _Traits>& __is, linear_congruential_engine<_Up, _Ap, _Cp, _Np>& __x);318};319320template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>321_LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type322linear_congruential_engine<_UIntType, __a, __c, __m>::multiplier;323324template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>325_LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type326linear_congruential_engine<_UIntType, __a, __c, __m>::increment;327328template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>329_LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type330linear_congruential_engine<_UIntType, __a, __c, __m>::modulus;331332template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>333_LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type334linear_congruential_engine<_UIntType, __a, __c, __m>::default_seed;335336template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>337template <class _Sseq>338void linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q, integral_constant<unsigned, 1>) {339const unsigned __k = 1;340uint32_t __ar[__k + 3];341__q.generate(__ar, __ar + __k + 3);342result_type __s = static_cast<result_type>(__ar[3] % __m);343__x_ = __c == 0 && __s == 0 ? result_type(1) : __s;344}345346template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>347template <class _Sseq>348void linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q, integral_constant<unsigned, 2>) {349const unsigned __k = 2;350uint32_t __ar[__k + 3];351__q.generate(__ar, __ar + __k + 3);352result_type __s = static_cast<result_type>((__ar[3] + ((uint64_t)__ar[4] << 32)) % __m);353__x_ = __c == 0 && __s == 0 ? result_type(1) : __s;354}355356template <class _CharT, class _Traits, class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>357inline _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&358operator<<(basic_ostream<_CharT, _Traits>& __os, const linear_congruential_engine<_UIntType, __a, __c, __m>& __x) {359__save_flags<_CharT, _Traits> __lx(__os);360typedef basic_ostream<_CharT, _Traits> _Ostream;361__os.flags(_Ostream::dec | _Ostream::left);362__os.fill(__os.widen(' '));363return __os << __x.__x_;364}365366template <class _CharT, class _Traits, class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>367_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&368operator>>(basic_istream<_CharT, _Traits>& __is, linear_congruential_engine<_UIntType, __a, __c, __m>& __x) {369__save_flags<_CharT, _Traits> __lx(__is);370typedef basic_istream<_CharT, _Traits> _Istream;371__is.flags(_Istream::dec | _Istream::skipws);372_UIntType __t;373__is >> __t;374if (!__is.fail())375__x.__x_ = __t;376return __is;377}378379typedef linear_congruential_engine<uint_fast32_t, 16807, 0, 2147483647> minstd_rand0;380typedef linear_congruential_engine<uint_fast32_t, 48271, 0, 2147483647> minstd_rand;381382_LIBCPP_END_NAMESPACE_STD383384_LIBCPP_POP_MACROS385386#endif // _LIBCPP___RANDOM_LINEAR_CONGRUENTIAL_ENGINE_H387388389