pcg_random.hpp 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784
  1. /*
  2. * PCG Random Number Generation for C++
  3. *
  4. * Copyright 2014-2017 Melissa O'Neill <oneill@pcg-random.org>,
  5. * and the PCG Project contributors.
  6. *
  7. * SPDX-License-Identifier: (Apache-2.0 OR MIT)
  8. *
  9. * Licensed under the Apache License, Version 2.0 (provided in
  10. * LICENSE-APACHE.txt and at http://www.apache.org/licenses/LICENSE-2.0)
  11. * or under the MIT license (provided in LICENSE-MIT.txt and at
  12. * http://opensource.org/licenses/MIT), at your option. This file may not
  13. * be copied, modified, or distributed except according to those terms.
  14. *
  15. * Distributed on an "AS IS" BASIS, WITHOUT WARRANTY OF ANY KIND, either
  16. * express or implied. See your chosen license for details.
  17. *
  18. * For additional information about the PCG random number generation scheme,
  19. * visit http://www.pcg-random.org/.
  20. */
  21. /*
  22. * This code provides the reference implementation of the PCG family of
  23. * random number generators. The code is complex because it implements
  24. *
  25. * - several members of the PCG family, specifically members corresponding
  26. * to the output functions:
  27. * - XSH RR (good for 64-bit state, 32-bit output)
  28. * - XSH RS (good for 64-bit state, 32-bit output)
  29. * - XSL RR (good for 128-bit state, 64-bit output)
  30. * - RXS M XS (statistically most powerful generator)
  31. * - XSL RR RR (good for 128-bit state, 128-bit output)
  32. * - and RXS, RXS M, XSH, XSL (mostly for testing)
  33. * - at potentially *arbitrary* bit sizes
  34. * - with four different techniques for random streams (MCG, one-stream
  35. * LCG, settable-stream LCG, unique-stream LCG)
  36. * - and the extended generation schemes allowing arbitrary periods
  37. * - with all features of C++11 random number generation (and more),
  38. * some of which are somewhat painful, including
  39. * - initializing with a SeedSequence which writes 32-bit values
  40. * to memory, even though the state of the generator may not
  41. * use 32-bit values (it might use smaller or larger integers)
  42. * - I/O for RNGs and a prescribed format, which needs to handle
  43. * the issue that 8-bit and 128-bit integers don't have working
  44. * I/O routines (e.g., normally 8-bit = char, not integer)
  45. * - equality and inequality for RNGs
  46. * - and a number of convenience typedefs to mask all the complexity
  47. *
  48. * The code employes a fairly heavy level of abstraction, and has to deal
  49. * with various C++ minutia. If you're looking to learn about how the PCG
  50. * scheme works, you're probably best of starting with one of the other
  51. * codebases (see www.pcg-random.org). But if you're curious about the
  52. * constants for the various output functions used in those other, simpler,
  53. * codebases, this code shows how they are calculated.
  54. *
  55. * On the positive side, at least there are convenience typedefs so that you
  56. * can say
  57. *
  58. * pcg32 myRNG;
  59. *
  60. * rather than:
  61. *
  62. * pcg_detail::engine<
  63. * uint32_t, // Output Type
  64. * uint64_t, // State Type
  65. * pcg_detail::xsh_rr_mixin<uint32_t, uint64_t>, true, // Output Func
  66. * pcg_detail::specific_stream<uint64_t>, // Stream Kind
  67. * pcg_detail::default_multiplier<uint64_t> // LCG Mult
  68. * > myRNG;
  69. *
  70. */
  71. #ifndef PCG_RAND_HPP_INCLUDED
  72. #define PCG_RAND_HPP_INCLUDED 1
  73. #include <algorithm>
  74. #include <cinttypes>
  75. #include <cstddef>
  76. #include <cstdlib>
  77. #include <cstring>
  78. #include <cassert>
  79. #include <limits>
  80. #include <iostream>
  81. #include <iterator>
  82. #include <type_traits>
  83. #include <utility>
  84. #include <locale>
  85. #include <new>
  86. #include <stdexcept>
  87. #ifdef _MSC_VER
  88. #pragma warning(disable:4146)
  89. #endif
  90. #ifdef _MSC_VER
  91. #define PCG_ALWAYS_INLINE _forceinline
  92. #elif __GNUC__
  93. #define PCG_ALWAYS_INLINE __attribute__((always_inline))
  94. #else
  95. #define PCG_ALWAYS_INLINE inline
  96. #endif
  97. /*
  98. * The pcg_extras namespace contains some support code that is likely to
  99. * be useful for a variety of RNGs, including:
  100. * - 128-bit int support for platforms where it isn't available natively
  101. * - bit twiddling operations
  102. * - I/O of 128-bit and 8-bit integers
  103. * - Handling the evilness of SeedSeq
  104. * - Support for efficiently producing random numbers less than a given
  105. * bound
  106. */
  107. #include "pcg_extras.hpp"
  108. // NOLINTBEGIN(*)
  109. namespace DB
  110. {
  111. struct PcgSerializer;
  112. struct PcgDeserializer;
  113. }
  114. namespace pcg_detail {
  115. using namespace pcg_extras;
  116. /*
  117. * The LCG generators need some constants to function. This code lets you
  118. * look up the constant by *type*. For example
  119. *
  120. * default_multiplier<uint32_t>::multiplier()
  121. *
  122. * gives you the default multiplier for 32-bit integers. We use the name
  123. * of the constant and not a generic word like value to allow these classes
  124. * to be used as mixins.
  125. */
  126. template <typename T>
  127. struct default_multiplier {
  128. // Not defined for an arbitrary type
  129. };
  130. template <typename T>
  131. struct default_increment {
  132. // Not defined for an arbitrary type
  133. };
  134. #define PCG_DEFINE_CONSTANT(type, what, kind, constant) \
  135. template <> \
  136. struct what ## _ ## kind<type> { \
  137. static constexpr type kind() { \
  138. return constant; \
  139. } \
  140. };
  141. PCG_DEFINE_CONSTANT(uint8_t, default, multiplier, 141U)
  142. PCG_DEFINE_CONSTANT(uint8_t, default, increment, 77U)
  143. PCG_DEFINE_CONSTANT(uint16_t, default, multiplier, 12829U)
  144. PCG_DEFINE_CONSTANT(uint16_t, default, increment, 47989U)
  145. PCG_DEFINE_CONSTANT(uint32_t, default, multiplier, 747796405U)
  146. PCG_DEFINE_CONSTANT(uint32_t, default, increment, 2891336453U)
  147. PCG_DEFINE_CONSTANT(uint64_t, default, multiplier, 6364136223846793005ULL)
  148. PCG_DEFINE_CONSTANT(uint64_t, default, increment, 1442695040888963407ULL)
  149. PCG_DEFINE_CONSTANT(pcg128_t, default, multiplier,
  150. PCG_128BIT_CONSTANT(2549297995355413924ULL,4865540595714422341ULL))
  151. PCG_DEFINE_CONSTANT(pcg128_t, default, increment,
  152. PCG_128BIT_CONSTANT(6364136223846793005ULL,1442695040888963407ULL))
  153. /*
  154. * Each PCG generator is available in four variants, based on how it applies
  155. * the additive constant for its underlying LCG; the variations are:
  156. *
  157. * single stream - all instances use the same fixed constant, thus
  158. * the RNG always somewhere in same sequence
  159. * mcg - adds zero, resulting in a single stream and reduced
  160. * period
  161. * specific stream - the constant can be changed at any time, selecting
  162. * a different random sequence
  163. * unique stream - the constant is based on the memory address of the
  164. * object, thus every RNG has its own unique sequence
  165. *
  166. * This variation is provided though mixin classes which define a function
  167. * value called increment() that returns the nesessary additive constant.
  168. */
  169. /*
  170. * unique stream
  171. */
  172. template <typename itype>
  173. class unique_stream {
  174. protected:
  175. static constexpr bool is_mcg = false;
  176. // Is never called, but is provided for symmetry with specific_stream
  177. void set_stream(...)
  178. {
  179. abort();
  180. }
  181. public:
  182. typedef itype state_type;
  183. constexpr itype increment() const {
  184. return itype(reinterpret_cast<unsigned long>(this) | 1);
  185. }
  186. constexpr itype stream() const
  187. {
  188. return increment() >> 1;
  189. }
  190. static constexpr bool can_specify_stream = false;
  191. static constexpr size_t streams_pow2()
  192. {
  193. return (sizeof(itype) < sizeof(size_t) ? sizeof(itype)
  194. : sizeof(size_t))*8 - 1u;
  195. }
  196. protected:
  197. constexpr unique_stream() = default;
  198. };
  199. /*
  200. * no stream (mcg)
  201. */
  202. template <typename itype>
  203. class no_stream {
  204. protected:
  205. static constexpr bool is_mcg = true;
  206. // Is never called, but is provided for symmetry with specific_stream
  207. void set_stream(...)
  208. {
  209. abort();
  210. }
  211. public:
  212. typedef itype state_type;
  213. static constexpr itype increment() {
  214. return 0;
  215. }
  216. static constexpr bool can_specify_stream = false;
  217. static constexpr size_t streams_pow2()
  218. {
  219. return 0u;
  220. }
  221. protected:
  222. constexpr no_stream() = default;
  223. };
  224. /*
  225. * single stream/sequence (oneseq)
  226. */
  227. template <typename itype>
  228. class oneseq_stream : public default_increment<itype> {
  229. protected:
  230. static constexpr bool is_mcg = false;
  231. // Is never called, but is provided for symmetry with specific_stream
  232. void set_stream(...)
  233. {
  234. abort();
  235. }
  236. public:
  237. typedef itype state_type;
  238. static constexpr itype stream()
  239. {
  240. return default_increment<itype>::increment() >> 1;
  241. }
  242. static constexpr bool can_specify_stream = false;
  243. static constexpr size_t streams_pow2()
  244. {
  245. return 0u;
  246. }
  247. protected:
  248. constexpr oneseq_stream() = default;
  249. };
  250. /*
  251. * specific stream
  252. */
  253. template <typename itype>
  254. class specific_stream {
  255. protected:
  256. static constexpr bool is_mcg = false;
  257. itype inc_ = default_increment<itype>::increment();
  258. public:
  259. typedef itype state_type;
  260. typedef itype stream_state;
  261. constexpr itype increment() const {
  262. return inc_;
  263. }
  264. itype stream()
  265. {
  266. return inc_ >> 1;
  267. }
  268. void set_stream(itype specific_seq)
  269. {
  270. inc_ = (specific_seq << 1) | 1;
  271. }
  272. static constexpr bool can_specify_stream = true;
  273. static constexpr size_t streams_pow2()
  274. {
  275. return (sizeof(itype)*8) - 1u;
  276. }
  277. protected:
  278. specific_stream() = default;
  279. specific_stream(itype specific_seq)
  280. : inc_(itype(specific_seq << 1) | itype(1U))
  281. {
  282. // Nothing (else) to do.
  283. }
  284. };
  285. /*
  286. * This is where it all comes together. This function joins together three
  287. * mixin classes which define
  288. * - the LCG additive constant (the stream)
  289. * - the LCG multiplier
  290. * - the output function
  291. * in addition, we specify the type of the LCG state, and the result type,
  292. * and whether to use the pre-advance version of the state for the output
  293. * (increasing instruction-level parallelism) or the post-advance version
  294. * (reducing register pressure).
  295. *
  296. * Given the high level of parameterization, the code has to use some
  297. * template-metaprogramming tricks to handle some of the subtle variations
  298. * involved.
  299. */
  300. template <typename xtype, typename itype,
  301. typename output_mixin,
  302. bool output_previous = true,
  303. typename stream_mixin = oneseq_stream<itype>,
  304. typename multiplier_mixin = default_multiplier<itype> >
  305. class engine : protected output_mixin,
  306. public stream_mixin,
  307. protected multiplier_mixin {
  308. protected:
  309. itype state_;
  310. struct can_specify_stream_tag {};
  311. struct no_specifiable_stream_tag {};
  312. using stream_mixin::increment;
  313. using multiplier_mixin::multiplier;
  314. public:
  315. typedef xtype result_type;
  316. typedef itype state_type;
  317. static constexpr size_t period_pow2()
  318. {
  319. return sizeof(state_type)*8 - 2*stream_mixin::is_mcg;
  320. }
  321. // It would be nice to use std::numeric_limits for these, but
  322. // we can't be sure that it'd be defined for the 128-bit types.
  323. static constexpr result_type min()
  324. {
  325. return result_type(0UL);
  326. }
  327. static constexpr result_type max()
  328. {
  329. return result_type(~result_type(0UL));
  330. }
  331. protected:
  332. itype bump(itype state)
  333. {
  334. return state * multiplier() + increment();
  335. }
  336. itype base_generate()
  337. {
  338. return state_ = bump(state_);
  339. }
  340. itype base_generate0()
  341. {
  342. itype old_state = state_;
  343. state_ = bump(state_);
  344. return old_state;
  345. }
  346. public:
  347. result_type operator()()
  348. {
  349. if (output_previous)
  350. return this->output(base_generate0());
  351. else
  352. return this->output(base_generate());
  353. }
  354. result_type operator()(result_type upper_bound)
  355. {
  356. return bounded_rand(*this, upper_bound);
  357. }
  358. protected:
  359. static itype advance(itype state, itype delta,
  360. itype cur_mult, itype cur_plus);
  361. static itype distance(itype cur_state, itype newstate, itype cur_mult,
  362. itype cur_plus, itype mask = ~itype(0U));
  363. itype distance(itype newstate, itype mask = itype(~itype(0U))) const
  364. {
  365. return distance(state_, newstate, multiplier(), increment(), mask);
  366. }
  367. public:
  368. void advance(itype delta)
  369. {
  370. state_ = advance(state_, delta, this->multiplier(), this->increment());
  371. }
  372. void backstep(itype delta)
  373. {
  374. advance(-delta);
  375. }
  376. void discard(itype delta)
  377. {
  378. advance(delta);
  379. }
  380. bool wrapped()
  381. {
  382. if (stream_mixin::is_mcg) {
  383. // For MCGs, the low order two bits never change. In this
  384. // implementation, we keep them fixed at 3 to make this test
  385. // easier.
  386. return state_ == 3;
  387. } else {
  388. return state_ == 0;
  389. }
  390. }
  391. engine(itype state = itype(0xcafef00dd15ea5e5ULL))
  392. : state_(this->is_mcg ? state|state_type(3U)
  393. : bump(state + this->increment()))
  394. {
  395. // Nothing else to do.
  396. }
  397. // This function may or may not exist. It thus has to be a template
  398. // to use SFINAE; users don't have to worry about its template-ness.
  399. template <typename sm = stream_mixin>
  400. engine(itype state, typename sm::stream_state stream_seed)
  401. : stream_mixin(stream_seed),
  402. state_(this->is_mcg ? state|state_type(3U)
  403. : bump(state + this->increment()))
  404. {
  405. // Nothing else to do.
  406. }
  407. template<typename SeedSeq>
  408. engine(SeedSeq&& seedSeq, typename std::enable_if<
  409. !stream_mixin::can_specify_stream
  410. && !std::is_convertible<SeedSeq, itype>::value
  411. && !std::is_convertible<SeedSeq, engine>::value,
  412. no_specifiable_stream_tag>::type = {})
  413. : engine(generate_one<itype>(std::forward<SeedSeq>(seedSeq)))
  414. {
  415. // Nothing else to do.
  416. }
  417. template<typename SeedSeq>
  418. engine(SeedSeq&& seedSeq, typename std::enable_if<
  419. stream_mixin::can_specify_stream
  420. && !std::is_convertible<SeedSeq, itype>::value
  421. && !std::is_convertible<SeedSeq, engine>::value,
  422. can_specify_stream_tag>::type = {})
  423. : engine(generate_one<itype,1,2>(seedSeq),
  424. generate_one<itype,0,2>(seedSeq))
  425. {
  426. // Nothing else to do.
  427. }
  428. template<typename... Args>
  429. void seed(Args&&... args)
  430. {
  431. new (this) engine(std::forward<Args>(args)...);
  432. }
  433. template <typename xtype1, typename itype1,
  434. typename output_mixin1, bool output_previous1,
  435. typename stream_mixin_lhs, typename multiplier_mixin_lhs,
  436. typename stream_mixin_rhs, typename multiplier_mixin_rhs>
  437. friend bool operator==(const engine<xtype1,itype1,
  438. output_mixin1,output_previous1,
  439. stream_mixin_lhs, multiplier_mixin_lhs>&,
  440. const engine<xtype1,itype1,
  441. output_mixin1,output_previous1,
  442. stream_mixin_rhs, multiplier_mixin_rhs>&);
  443. template <typename xtype1, typename itype1,
  444. typename output_mixin1, bool output_previous1,
  445. typename stream_mixin_lhs, typename multiplier_mixin_lhs,
  446. typename stream_mixin_rhs, typename multiplier_mixin_rhs>
  447. friend itype1 operator-(const engine<xtype1,itype1,
  448. output_mixin1,output_previous1,
  449. stream_mixin_lhs, multiplier_mixin_lhs>&,
  450. const engine<xtype1,itype1,
  451. output_mixin1,output_previous1,
  452. stream_mixin_rhs, multiplier_mixin_rhs>&);
  453. template <typename CharT, typename Traits,
  454. typename xtype1, typename itype1,
  455. typename output_mixin1, bool output_previous1,
  456. typename stream_mixin1, typename multiplier_mixin1>
  457. friend std::basic_ostream<CharT,Traits>&
  458. operator<<(std::basic_ostream<CharT,Traits>& out,
  459. const engine<xtype1,itype1,
  460. output_mixin1,output_previous1,
  461. stream_mixin1, multiplier_mixin1>&);
  462. template <typename CharT, typename Traits,
  463. typename xtype1, typename itype1,
  464. typename output_mixin1, bool output_previous1,
  465. typename stream_mixin1, typename multiplier_mixin1>
  466. friend std::basic_istream<CharT,Traits>&
  467. operator>>(std::basic_istream<CharT,Traits>& in,
  468. engine<xtype1, itype1,
  469. output_mixin1, output_previous1,
  470. stream_mixin1, multiplier_mixin1>& rng);
  471. friend ::DB::PcgSerializer;
  472. friend ::DB::PcgDeserializer;
  473. };
  474. template <typename CharT, typename Traits,
  475. typename xtype, typename itype,
  476. typename output_mixin, bool output_previous,
  477. typename stream_mixin, typename multiplier_mixin>
  478. std::basic_ostream<CharT,Traits>&
  479. operator<<(std::basic_ostream<CharT,Traits>& out,
  480. const engine<xtype,itype,
  481. output_mixin,output_previous,
  482. stream_mixin, multiplier_mixin>& rng)
  483. {
  484. auto orig_flags = out.flags(std::ios_base::dec | std::ios_base::left);
  485. auto space = out.widen(' ');
  486. auto orig_fill = out.fill();
  487. out << rng.multiplier() << space
  488. << rng.increment() << space
  489. << rng.state_;
  490. out.flags(orig_flags);
  491. out.fill(orig_fill);
  492. return out;
  493. }
  494. template <typename CharT, typename Traits,
  495. typename xtype, typename itype,
  496. typename output_mixin, bool output_previous,
  497. typename stream_mixin, typename multiplier_mixin>
  498. std::basic_istream<CharT,Traits>&
  499. operator>>(std::basic_istream<CharT,Traits>& in,
  500. engine<xtype,itype,
  501. output_mixin,output_previous,
  502. stream_mixin, multiplier_mixin>& rng)
  503. {
  504. auto orig_flags = in.flags(std::ios_base::dec | std::ios_base::skipws);
  505. itype multiplier, increment, state;
  506. in >> multiplier >> increment >> state;
  507. if (!in.fail()) {
  508. bool good = true;
  509. if (multiplier != rng.multiplier()) {
  510. good = false;
  511. } else if (rng.can_specify_stream) {
  512. rng.set_stream(increment >> 1);
  513. } else if (increment != rng.increment()) {
  514. good = false;
  515. }
  516. if (good) {
  517. rng.state_ = state;
  518. } else {
  519. in.clear(std::ios::failbit);
  520. }
  521. }
  522. in.flags(orig_flags);
  523. return in;
  524. }
  525. template <typename xtype, typename itype,
  526. typename output_mixin, bool output_previous,
  527. typename stream_mixin, typename multiplier_mixin>
  528. itype engine<xtype,itype,output_mixin,output_previous,stream_mixin,
  529. multiplier_mixin>::advance(
  530. itype state, itype delta, itype cur_mult, itype cur_plus)
  531. {
  532. // The method used here is based on Brown, "Random Number Generation
  533. // with Arbitrary Stride,", Transactions of the American Nuclear
  534. // Society (Nov. 1994). The algorithm is very similar to fast
  535. // exponentiation.
  536. //
  537. // Even though delta is an unsigned integer, we can pass a
  538. // signed integer to go backwards, it just goes "the long way round".
  539. constexpr itype ZERO = 0u; // itype may be a non-trivial types, so
  540. constexpr itype ONE = 1u; // we define some ugly constants.
  541. itype acc_mult = 1;
  542. itype acc_plus = 0;
  543. while (delta > ZERO) {
  544. if (delta & ONE) {
  545. acc_mult *= cur_mult;
  546. acc_plus = acc_plus*cur_mult + cur_plus;
  547. }
  548. cur_plus = (cur_mult+ONE)*cur_plus;
  549. cur_mult *= cur_mult;
  550. delta >>= 1;
  551. }
  552. return acc_mult * state + acc_plus;
  553. }
  554. template <typename xtype, typename itype,
  555. typename output_mixin, bool output_previous,
  556. typename stream_mixin, typename multiplier_mixin>
  557. itype engine<xtype,itype,output_mixin,output_previous,stream_mixin,
  558. multiplier_mixin>::distance(
  559. itype cur_state, itype newstate, itype cur_mult, itype cur_plus, itype mask)
  560. {
  561. constexpr itype ONE = 1u; // itype could be weird, so use constant
  562. itype the_bit = stream_mixin::is_mcg ? itype(4u) : itype(1u);
  563. itype distance = 0u;
  564. while ((cur_state & mask) != (newstate & mask)) {
  565. if ((cur_state & the_bit) != (newstate & the_bit)) {
  566. cur_state = cur_state * cur_mult + cur_plus;
  567. distance |= the_bit;
  568. }
  569. assert((cur_state & the_bit) == (newstate & the_bit));
  570. the_bit <<= 1;
  571. cur_plus = (cur_mult+ONE)*cur_plus;
  572. cur_mult *= cur_mult;
  573. }
  574. return stream_mixin::is_mcg ? distance >> 2 : distance;
  575. }
  576. template <typename xtype, typename itype,
  577. typename output_mixin, bool output_previous,
  578. typename stream_mixin_lhs, typename multiplier_mixin_lhs,
  579. typename stream_mixin_rhs, typename multiplier_mixin_rhs>
  580. itype operator-(const engine<xtype,itype,
  581. output_mixin,output_previous,
  582. stream_mixin_lhs, multiplier_mixin_lhs>& lhs,
  583. const engine<xtype,itype,
  584. output_mixin,output_previous,
  585. stream_mixin_rhs, multiplier_mixin_rhs>& rhs)
  586. {
  587. static_assert(
  588. std::is_same<stream_mixin_lhs, stream_mixin_rhs>::value &&
  589. std::is_same<multiplier_mixin_lhs, multiplier_mixin_rhs>::value,
  590. "Incomparable generators");
  591. return rhs.distance(lhs.state_);
  592. }
  593. template <typename xtype, typename itype,
  594. typename output_mixin, bool output_previous,
  595. typename stream_mixin_lhs, typename multiplier_mixin_lhs,
  596. typename stream_mixin_rhs, typename multiplier_mixin_rhs>
  597. bool operator==(const engine<xtype,itype,
  598. output_mixin,output_previous,
  599. stream_mixin_lhs, multiplier_mixin_lhs>& lhs,
  600. const engine<xtype,itype,
  601. output_mixin,output_previous,
  602. stream_mixin_rhs, multiplier_mixin_rhs>& rhs)
  603. {
  604. return (lhs.multiplier() == rhs.multiplier())
  605. && (lhs.increment() == rhs.increment())
  606. && (lhs.state_ == rhs.state_);
  607. }
  608. template <typename xtype, typename itype,
  609. typename output_mixin, bool output_previous,
  610. typename stream_mixin_lhs, typename multiplier_mixin_lhs,
  611. typename stream_mixin_rhs, typename multiplier_mixin_rhs>
  612. inline bool operator!=(const engine<xtype,itype,
  613. output_mixin,output_previous,
  614. stream_mixin_lhs, multiplier_mixin_lhs>& lhs,
  615. const engine<xtype,itype,
  616. output_mixin,output_previous,
  617. stream_mixin_rhs, multiplier_mixin_rhs>& rhs)
  618. {
  619. return !operator==(lhs,rhs);
  620. }
  621. template <typename xtype, typename itype,
  622. template<typename XT,typename IT> class output_mixin,
  623. bool output_previous = (sizeof(itype) <= 8)>
  624. using oneseq_base = engine<xtype, itype,
  625. output_mixin<xtype, itype>, output_previous,
  626. oneseq_stream<itype> >;
  627. template <typename xtype, typename itype,
  628. template<typename XT,typename IT> class output_mixin,
  629. bool output_previous = (sizeof(itype) <= 8)>
  630. using unique_base = engine<xtype, itype,
  631. output_mixin<xtype, itype>, output_previous,
  632. unique_stream<itype> >;
  633. template <typename xtype, typename itype,
  634. template<typename XT,typename IT> class output_mixin,
  635. bool output_previous = (sizeof(itype) <= 8)>
  636. using setseq_base = engine<xtype, itype,
  637. output_mixin<xtype, itype>, output_previous,
  638. specific_stream<itype> >;
  639. template <typename xtype, typename itype,
  640. template<typename XT,typename IT> class output_mixin,
  641. bool output_previous = (sizeof(itype) <= 8)>
  642. using mcg_base = engine<xtype, itype,
  643. output_mixin<xtype, itype>, output_previous,
  644. no_stream<itype> >;
  645. /*
  646. * OUTPUT FUNCTIONS.
  647. *
  648. * These are the core of the PCG generation scheme. They specify how to
  649. * turn the base LCG's internal state into the output value of the final
  650. * generator.
  651. *
  652. * They're implemented as mixin classes.
  653. *
  654. * All of the classes have code that is written to allow it to be applied
  655. * at *arbitrary* bit sizes, although in practice they'll only be used at
  656. * standard sizes supported by C++.
  657. */
  658. /*
  659. * XSH RS -- high xorshift, followed by a random shift
  660. *
  661. * Fast. A good performer.
  662. */
  663. template <typename xtype, typename itype>
  664. struct xsh_rs_mixin {
  665. static xtype output(itype internal)
  666. {
  667. constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
  668. constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype) * 8);
  669. constexpr bitcount_t sparebits = bits - xtypebits;
  670. constexpr bitcount_t opbits =
  671. sparebits-5 >= 64 ? 5
  672. : sparebits-4 >= 32 ? 4
  673. : sparebits-3 >= 16 ? 3
  674. : sparebits-2 >= 4 ? 2
  675. : sparebits-1 >= 1 ? 1
  676. : 0;
  677. constexpr bitcount_t mask = (1 << opbits) - 1;
  678. constexpr bitcount_t maxrandshift = mask;
  679. constexpr bitcount_t topspare = opbits;
  680. constexpr bitcount_t bottomspare = sparebits - topspare;
  681. constexpr bitcount_t xshift = topspare + (xtypebits+maxrandshift)/2;
  682. bitcount_t rshift =
  683. opbits ? bitcount_t(internal >> (bits - opbits)) & mask : 0;
  684. internal ^= internal >> xshift;
  685. xtype result = xtype(internal >> (bottomspare - maxrandshift + rshift));
  686. return result;
  687. }
  688. };
  689. /*
  690. * XSH RR -- high xorshift, followed by a random rotate
  691. *
  692. * Fast. A good performer. Slightly better statistically than XSH RS.
  693. */
  694. template <typename xtype, typename itype>
  695. struct xsh_rr_mixin {
  696. static xtype output(itype internal)
  697. {
  698. constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
  699. constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype)*8);
  700. constexpr bitcount_t sparebits = bits - xtypebits;
  701. constexpr bitcount_t wantedopbits =
  702. xtypebits >= 128 ? 7
  703. : xtypebits >= 64 ? 6
  704. : xtypebits >= 32 ? 5
  705. : xtypebits >= 16 ? 4
  706. : 3;
  707. constexpr bitcount_t opbits =
  708. sparebits >= wantedopbits ? wantedopbits
  709. : sparebits;
  710. constexpr bitcount_t amplifier = wantedopbits - opbits;
  711. constexpr bitcount_t mask = (1 << opbits) - 1;
  712. constexpr bitcount_t topspare = opbits;
  713. constexpr bitcount_t bottomspare = sparebits - topspare;
  714. constexpr bitcount_t xshift = (topspare + xtypebits)/2;
  715. bitcount_t rot = opbits ? bitcount_t(internal >> (bits - opbits)) & mask
  716. : 0;
  717. bitcount_t amprot = (rot << amplifier) & mask;
  718. internal ^= internal >> xshift;
  719. xtype result = xtype(internal >> bottomspare);
  720. result = rotr(result, amprot);
  721. return result;
  722. }
  723. };
  724. /*
  725. * RXS -- random xorshift
  726. */
  727. template <typename xtype, typename itype>
  728. struct rxs_mixin {
  729. static xtype output_rxs(itype internal)
  730. {
  731. constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
  732. constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype)*8);
  733. constexpr bitcount_t shift = bits - xtypebits;
  734. constexpr bitcount_t extrashift = (xtypebits - shift)/2;
  735. bitcount_t rshift = shift > 64+8 ? (internal >> (bits - 6)) & 63
  736. : shift > 32+4 ? (internal >> (bits - 5)) & 31
  737. : shift > 16+2 ? (internal >> (bits - 4)) & 15
  738. : shift > 8+1 ? (internal >> (bits - 3)) & 7
  739. : shift > 4+1 ? (internal >> (bits - 2)) & 3
  740. : shift > 2+1 ? (internal >> (bits - 1)) & 1
  741. : 0;
  742. internal ^= internal >> (shift + extrashift - rshift);
  743. xtype result = internal >> rshift;
  744. return result;
  745. }
  746. };
  747. /*
  748. * RXS M XS -- random xorshift, mcg multiply, fixed xorshift
  749. *
  750. * The most statistically powerful generator, but all those steps
  751. * make it slower than some of the others. We give it the rottenest jobs.
  752. *
  753. * Because it's usually used in contexts where the state type and the
  754. * result type are the same, it is a permutation and is thus invertable.
  755. * We thus provide a function to invert it. This function is used to
  756. * for the "inside out" generator used by the extended generator.
  757. */
  758. /* Defined type-based concepts for the multiplication step. They're actually
  759. * all derived by truncating the 128-bit, which was computed to be a good
  760. * "universal" constant.
  761. */
  762. template <typename T>
  763. struct mcg_multiplier {
  764. // Not defined for an arbitrary type
  765. };
  766. template <typename T>
  767. struct mcg_unmultiplier {
  768. // Not defined for an arbitrary type
  769. };
  770. PCG_DEFINE_CONSTANT(uint8_t, mcg, multiplier, 217U)
  771. PCG_DEFINE_CONSTANT(uint8_t, mcg, unmultiplier, 105U)
  772. PCG_DEFINE_CONSTANT(uint16_t, mcg, multiplier, 62169U)
  773. PCG_DEFINE_CONSTANT(uint16_t, mcg, unmultiplier, 28009U)
  774. PCG_DEFINE_CONSTANT(uint32_t, mcg, multiplier, 277803737U)
  775. PCG_DEFINE_CONSTANT(uint32_t, mcg, unmultiplier, 2897767785U)
  776. PCG_DEFINE_CONSTANT(uint64_t, mcg, multiplier, 12605985483714917081ULL)
  777. PCG_DEFINE_CONSTANT(uint64_t, mcg, unmultiplier, 15009553638781119849ULL)
  778. PCG_DEFINE_CONSTANT(pcg128_t, mcg, multiplier,
  779. PCG_128BIT_CONSTANT(17766728186571221404ULL, 12605985483714917081ULL))
  780. PCG_DEFINE_CONSTANT(pcg128_t, mcg, unmultiplier,
  781. PCG_128BIT_CONSTANT(14422606686972528997ULL, 15009553638781119849ULL))
  782. template <typename xtype, typename itype>
  783. struct rxs_m_xs_mixin {
  784. static xtype output(itype internal)
  785. {
  786. constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype) * 8);
  787. constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
  788. constexpr bitcount_t opbits = xtypebits >= 128 ? 6
  789. : xtypebits >= 64 ? 5
  790. : xtypebits >= 32 ? 4
  791. : xtypebits >= 16 ? 3
  792. : 2;
  793. constexpr bitcount_t shift = bits - xtypebits;
  794. constexpr bitcount_t mask = (1 << opbits) - 1;
  795. bitcount_t rshift =
  796. opbits ? bitcount_t(internal >> (bits - opbits)) & mask : 0;
  797. internal ^= internal >> (opbits + rshift);
  798. internal *= mcg_multiplier<itype>::multiplier();
  799. xtype result = internal >> shift;
  800. result ^= result >> ((2U*xtypebits+2U)/3U);
  801. return result;
  802. }
  803. static itype unoutput(itype internal)
  804. {
  805. constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
  806. constexpr bitcount_t opbits = bits >= 128 ? 6
  807. : bits >= 64 ? 5
  808. : bits >= 32 ? 4
  809. : bits >= 16 ? 3
  810. : 2;
  811. constexpr bitcount_t mask = (1 << opbits) - 1;
  812. internal = unxorshift(internal, bits, (2U*bits+2U)/3U);
  813. internal *= mcg_unmultiplier<itype>::unmultiplier();
  814. bitcount_t rshift = opbits ? (internal >> (bits - opbits)) & mask : 0;
  815. internal = unxorshift(internal, bits, opbits + rshift);
  816. return internal;
  817. }
  818. };
  819. /*
  820. * RXS M -- random xorshift, mcg multiply
  821. */
  822. template <typename xtype, typename itype>
  823. struct rxs_m_mixin {
  824. static xtype output(itype internal)
  825. {
  826. constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype) * 8);
  827. constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
  828. constexpr bitcount_t opbits = xtypebits >= 128 ? 6
  829. : xtypebits >= 64 ? 5
  830. : xtypebits >= 32 ? 4
  831. : xtypebits >= 16 ? 3
  832. : 2;
  833. constexpr bitcount_t shift = bits - xtypebits;
  834. constexpr bitcount_t mask = (1 << opbits) - 1;
  835. bitcount_t rshift = opbits ? (internal >> (bits - opbits)) & mask : 0;
  836. internal ^= internal >> (opbits + rshift);
  837. internal *= mcg_multiplier<itype>::multiplier();
  838. xtype result = internal >> shift;
  839. return result;
  840. }
  841. };
  842. /*
  843. * XSL RR -- fixed xorshift (to low bits), random rotate
  844. *
  845. * Useful for 128-bit types that are split across two CPU registers.
  846. */
  847. template <typename xtype, typename itype>
  848. struct xsl_rr_mixin {
  849. static xtype output(itype internal)
  850. {
  851. constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype) * 8);
  852. constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
  853. constexpr bitcount_t sparebits = bits - xtypebits;
  854. constexpr bitcount_t wantedopbits = xtypebits >= 128 ? 7
  855. : xtypebits >= 64 ? 6
  856. : xtypebits >= 32 ? 5
  857. : xtypebits >= 16 ? 4
  858. : 3;
  859. constexpr bitcount_t opbits = sparebits >= wantedopbits ? wantedopbits
  860. : sparebits;
  861. constexpr bitcount_t amplifier = wantedopbits - opbits;
  862. constexpr bitcount_t mask = (1 << opbits) - 1;
  863. constexpr bitcount_t topspare = sparebits;
  864. constexpr bitcount_t bottomspare = sparebits - topspare;
  865. constexpr bitcount_t xshift = (topspare + xtypebits) / 2;
  866. bitcount_t rot =
  867. opbits ? bitcount_t(internal >> (bits - opbits)) & mask : 0;
  868. bitcount_t amprot = (rot << amplifier) & mask;
  869. internal ^= internal >> xshift;
  870. xtype result = xtype(internal >> bottomspare);
  871. result = rotr(result, amprot);
  872. return result;
  873. }
  874. };
  875. /*
  876. * XSL RR RR -- fixed xorshift (to low bits), random rotate (both parts)
  877. *
  878. * Useful for 128-bit types that are split across two CPU registers.
  879. * If you really want an invertable 128-bit RNG, I guess this is the one.
  880. */
  881. template <typename T> struct halfsize_trait {};
  882. template <> struct halfsize_trait<pcg128_t> { typedef uint64_t type; };
  883. template <> struct halfsize_trait<uint64_t> { typedef uint32_t type; };
  884. template <> struct halfsize_trait<uint32_t> { typedef uint16_t type; };
  885. template <> struct halfsize_trait<uint16_t> { typedef uint8_t type; };
  886. template <typename xtype, typename itype>
  887. struct xsl_rr_rr_mixin {
  888. typedef typename halfsize_trait<itype>::type htype;
  889. static itype output(itype internal)
  890. {
  891. constexpr bitcount_t htypebits = bitcount_t(sizeof(htype) * 8);
  892. constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
  893. constexpr bitcount_t sparebits = bits - htypebits;
  894. constexpr bitcount_t wantedopbits = htypebits >= 128 ? 7
  895. : htypebits >= 64 ? 6
  896. : htypebits >= 32 ? 5
  897. : htypebits >= 16 ? 4
  898. : 3;
  899. constexpr bitcount_t opbits = sparebits >= wantedopbits ? wantedopbits
  900. : sparebits;
  901. constexpr bitcount_t amplifier = wantedopbits - opbits;
  902. constexpr bitcount_t mask = (1 << opbits) - 1;
  903. constexpr bitcount_t topspare = sparebits;
  904. constexpr bitcount_t xshift = (topspare + htypebits) / 2;
  905. bitcount_t rot =
  906. opbits ? bitcount_t(internal >> (bits - opbits)) & mask : 0;
  907. bitcount_t amprot = (rot << amplifier) & mask;
  908. internal ^= internal >> xshift;
  909. htype lowbits = htype(internal);
  910. lowbits = rotr(lowbits, amprot);
  911. htype highbits = htype(internal >> topspare);
  912. bitcount_t rot2 = lowbits & mask;
  913. bitcount_t amprot2 = (rot2 << amplifier) & mask;
  914. highbits = rotr(highbits, amprot2);
  915. return (itype(highbits) << topspare) ^ itype(lowbits);
  916. }
  917. };
  918. /*
  919. * XSH -- fixed xorshift (to high bits)
  920. *
  921. * You shouldn't use this at 64-bits or less.
  922. */
  923. template <typename xtype, typename itype>
  924. struct xsh_mixin {
  925. static xtype output(itype internal)
  926. {
  927. constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype) * 8);
  928. constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
  929. constexpr bitcount_t sparebits = bits - xtypebits;
  930. constexpr bitcount_t topspare = 0;
  931. constexpr bitcount_t bottomspare = sparebits - topspare;
  932. constexpr bitcount_t xshift = (topspare + xtypebits) / 2;
  933. internal ^= internal >> xshift;
  934. xtype result = internal >> bottomspare;
  935. return result;
  936. }
  937. };
  938. /*
  939. * XSL -- fixed xorshift (to low bits)
  940. *
  941. * You shouldn't use this at 64-bits or less.
  942. */
  943. template <typename xtype, typename itype>
  944. struct xsl_mixin {
  945. inline xtype output(itype internal)
  946. {
  947. constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype) * 8);
  948. constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
  949. constexpr bitcount_t sparebits = bits - xtypebits;
  950. constexpr bitcount_t topspare = sparebits;
  951. constexpr bitcount_t bottomspare = sparebits - topspare;
  952. constexpr bitcount_t xshift = (topspare + xtypebits) / 2;
  953. internal ^= internal >> xshift;
  954. xtype result = internal >> bottomspare;
  955. return result;
  956. }
  957. };
  958. /* ---- End of Output Functions ---- */
  959. template <typename baseclass>
  960. struct inside_out : private baseclass {
  961. inside_out() = delete;
  962. typedef typename baseclass::result_type result_type;
  963. typedef typename baseclass::state_type state_type;
  964. static_assert(sizeof(result_type) == sizeof(state_type),
  965. "Require a RNG whose output function is a permutation");
  966. static bool external_step(result_type& randval, size_t i)
  967. {
  968. state_type state = baseclass::unoutput(randval);
  969. state = state * baseclass::multiplier() + baseclass::increment()
  970. + state_type(i*2);
  971. result_type result = baseclass::output(state);
  972. randval = result;
  973. state_type zero =
  974. baseclass::is_mcg ? state & state_type(3U) : state_type(0U);
  975. return result == zero;
  976. }
  977. static bool external_advance(result_type& randval, size_t i,
  978. result_type delta, bool forwards = true)
  979. {
  980. state_type state = baseclass::unoutput(randval);
  981. state_type mult = baseclass::multiplier();
  982. state_type inc = baseclass::increment() + state_type(i*2);
  983. state_type zero =
  984. baseclass::is_mcg ? state & state_type(3U) : state_type(0U);
  985. state_type dist_to_zero = baseclass::distance(state, zero, mult, inc);
  986. bool crosses_zero =
  987. forwards ? dist_to_zero <= delta
  988. : (-dist_to_zero) <= delta;
  989. if (!forwards)
  990. delta = -delta;
  991. state = baseclass::advance(state, delta, mult, inc);
  992. randval = baseclass::output(state);
  993. return crosses_zero;
  994. }
  995. };
  996. template <bitcount_t table_pow2, bitcount_t advance_pow2, typename baseclass, typename extvalclass, bool kdd = true>
  997. class extended : public baseclass {
  998. public:
  999. typedef typename baseclass::state_type state_type;
  1000. typedef typename baseclass::result_type result_type;
  1001. typedef inside_out<extvalclass> insideout;
  1002. private:
  1003. static constexpr bitcount_t rtypebits = sizeof(result_type)*8;
  1004. static constexpr bitcount_t stypebits = sizeof(state_type)*8;
  1005. static constexpr bitcount_t tick_limit_pow2 = 64U;
  1006. static constexpr size_t table_size = 1UL << table_pow2;
  1007. static constexpr size_t table_shift = stypebits - table_pow2;
  1008. static constexpr state_type table_mask =
  1009. (state_type(1U) << table_pow2) - state_type(1U);
  1010. static constexpr bool may_tick =
  1011. (advance_pow2 < stypebits) && (advance_pow2 < tick_limit_pow2);
  1012. static constexpr size_t tick_shift = stypebits - advance_pow2;
  1013. static constexpr state_type tick_mask =
  1014. may_tick ? state_type(
  1015. (uint64_t(1) << (advance_pow2*may_tick)) - 1)
  1016. // ^-- stupidity to appease GCC warnings
  1017. : ~state_type(0U);
  1018. static constexpr bool may_tock = stypebits < tick_limit_pow2;
  1019. result_type data_[table_size];
  1020. PCG_NOINLINE void advance_table();
  1021. PCG_NOINLINE void advance_table(state_type delta, bool isForwards = true);
  1022. result_type& get_extended_value()
  1023. {
  1024. state_type state = this->state_;
  1025. if (kdd && baseclass::is_mcg) {
  1026. // The low order bits of an MCG are constant, so drop them.
  1027. state >>= 2;
  1028. }
  1029. size_t index = kdd ? state & table_mask
  1030. : state >> table_shift;
  1031. if (may_tick) {
  1032. bool tick = kdd ? (state & tick_mask) == state_type(0u)
  1033. : (state >> tick_shift) == state_type(0u);
  1034. if (tick)
  1035. advance_table();
  1036. }
  1037. if (may_tock) {
  1038. bool tock = state == state_type(0u);
  1039. if (tock)
  1040. advance_table();
  1041. }
  1042. return data_[index];
  1043. }
  1044. public:
  1045. static constexpr size_t period_pow2()
  1046. {
  1047. return baseclass::period_pow2() + table_size*extvalclass::period_pow2();
  1048. }
  1049. PCG_ALWAYS_INLINE result_type operator()()
  1050. {
  1051. result_type rhs = get_extended_value();
  1052. result_type lhs = this->baseclass::operator()();
  1053. return lhs ^ rhs;
  1054. }
  1055. result_type operator()(result_type upper_bound)
  1056. {
  1057. return bounded_rand(*this, upper_bound);
  1058. }
  1059. void set(result_type wanted)
  1060. {
  1061. result_type& rhs = get_extended_value();
  1062. result_type lhs = this->baseclass::operator()();
  1063. rhs = lhs ^ wanted;
  1064. }
  1065. void advance(state_type distance, bool forwards = true);
  1066. void backstep(state_type distance)
  1067. {
  1068. advance(distance, false);
  1069. }
  1070. extended(const result_type* data)
  1071. : baseclass()
  1072. {
  1073. datainit(data);
  1074. }
  1075. extended(const result_type* data, state_type seed)
  1076. : baseclass(seed)
  1077. {
  1078. datainit(data);
  1079. }
  1080. // This function may or may not exist. It thus has to be a template
  1081. // to use SFINAE; users don't have to worry about its template-ness.
  1082. template <typename bc = baseclass>
  1083. extended(const result_type* data, state_type seed,
  1084. typename bc::stream_state stream_seed)
  1085. : baseclass(seed, stream_seed)
  1086. {
  1087. datainit(data);
  1088. }
  1089. extended()
  1090. : baseclass()
  1091. {
  1092. selfinit();
  1093. }
  1094. extended(state_type seed)
  1095. : baseclass(seed)
  1096. {
  1097. selfinit();
  1098. }
  1099. // This function may or may not exist. It thus has to be a template
  1100. // to use SFINAE; users don't have to worry about its template-ness.
  1101. template <typename bc = baseclass>
  1102. extended(state_type seed, typename bc::stream_state stream_seed)
  1103. : baseclass(seed, stream_seed)
  1104. {
  1105. selfinit();
  1106. }
  1107. private:
  1108. void selfinit();
  1109. void datainit(const result_type* data);
  1110. public:
  1111. template<typename SeedSeq, typename = typename std::enable_if<
  1112. !std::is_convertible<SeedSeq, result_type>::value
  1113. && !std::is_convertible<SeedSeq, extended>::value>::type>
  1114. extended(SeedSeq&& seedSeq)
  1115. : baseclass(seedSeq)
  1116. {
  1117. generate_to<table_size>(seedSeq, data_);
  1118. }
  1119. template<typename... Args>
  1120. void seed(Args&&... args)
  1121. {
  1122. new (this) extended(std::forward<Args>(args)...);
  1123. }
  1124. template <bitcount_t table_pow2_, bitcount_t advance_pow2_,
  1125. typename baseclass_, typename extvalclass_, bool kdd_>
  1126. friend bool operator==(const extended<table_pow2_, advance_pow2_,
  1127. baseclass_, extvalclass_, kdd_>&,
  1128. const extended<table_pow2_, advance_pow2_,
  1129. baseclass_, extvalclass_, kdd_>&);
  1130. template <typename CharT, typename Traits,
  1131. bitcount_t table_pow2_, bitcount_t advance_pow2_,
  1132. typename baseclass_, typename extvalclass_, bool kdd_>
  1133. friend std::basic_ostream<CharT,Traits>&
  1134. operator<<(std::basic_ostream<CharT,Traits>& out,
  1135. const extended<table_pow2_, advance_pow2_,
  1136. baseclass_, extvalclass_, kdd_>&);
  1137. template <typename CharT, typename Traits,
  1138. bitcount_t table_pow2_, bitcount_t advance_pow2_,
  1139. typename baseclass_, typename extvalclass_, bool kdd_>
  1140. friend std::basic_istream<CharT,Traits>&
  1141. operator>>(std::basic_istream<CharT,Traits>& in,
  1142. extended<table_pow2_, advance_pow2_,
  1143. baseclass_, extvalclass_, kdd_>&);
  1144. };
  1145. template <bitcount_t table_pow2, bitcount_t advance_pow2,
  1146. typename baseclass, typename extvalclass, bool kdd>
  1147. void extended<table_pow2,advance_pow2,baseclass,extvalclass,kdd>::datainit(
  1148. const result_type* data)
  1149. {
  1150. for (size_t i = 0; i < table_size; ++i)
  1151. data_[i] = data[i];
  1152. }
  1153. template <bitcount_t table_pow2, bitcount_t advance_pow2,
  1154. typename baseclass, typename extvalclass, bool kdd>
  1155. void extended<table_pow2,advance_pow2,baseclass,extvalclass,kdd>::selfinit()
  1156. {
  1157. // We need to fill the extended table with something, and we have
  1158. // very little provided data, so we use the base generator to
  1159. // produce values. Although not ideal (use a seed sequence, folks!),
  1160. // unexpected correlations are mitigated by
  1161. // - using XOR differences rather than the number directly
  1162. // - the way the table is accessed, its values *won't* be accessed
  1163. // in the same order the were written.
  1164. // - any strange correlations would only be apparent if we
  1165. // were to backstep the generator so that the base generator
  1166. // was generating the same values again
  1167. result_type xdiff = baseclass::operator()() - baseclass::operator()();
  1168. for (size_t i = 0; i < table_size; ++i) {
  1169. data_[i] = baseclass::operator()() ^ xdiff;
  1170. }
  1171. }
  1172. template <bitcount_t table_pow2, bitcount_t advance_pow2,
  1173. typename baseclass, typename extvalclass, bool kdd>
  1174. bool operator==(const extended<table_pow2, advance_pow2,
  1175. baseclass, extvalclass, kdd>& lhs,
  1176. const extended<table_pow2, advance_pow2,
  1177. baseclass, extvalclass, kdd>& rhs)
  1178. {
  1179. auto& base_lhs = static_cast<const baseclass&>(lhs);
  1180. auto& base_rhs = static_cast<const baseclass&>(rhs);
  1181. return base_lhs == base_rhs
  1182. && std::equal(
  1183. std::begin(lhs.data_), std::end(lhs.data_),
  1184. std::begin(rhs.data_)
  1185. );
  1186. }
  1187. template <bitcount_t table_pow2, bitcount_t advance_pow2,
  1188. typename baseclass, typename extvalclass, bool kdd>
  1189. inline bool operator!=(const extended<table_pow2, advance_pow2,
  1190. baseclass, extvalclass, kdd>& lhs,
  1191. const extended<table_pow2, advance_pow2,
  1192. baseclass, extvalclass, kdd>& rhs)
  1193. {
  1194. return !operator==(lhs, rhs);
  1195. }
  1196. template <typename CharT, typename Traits,
  1197. bitcount_t table_pow2, bitcount_t advance_pow2,
  1198. typename baseclass, typename extvalclass, bool kdd>
  1199. std::basic_ostream<CharT,Traits>&
  1200. operator<<(std::basic_ostream<CharT,Traits>& out,
  1201. const extended<table_pow2, advance_pow2,
  1202. baseclass, extvalclass, kdd>& rng)
  1203. {
  1204. auto orig_flags = out.flags(std::ios_base::dec | std::ios_base::left);
  1205. auto space = out.widen(' ');
  1206. auto orig_fill = out.fill();
  1207. out << rng.multiplier() << space
  1208. << rng.increment() << space
  1209. << rng.state_;
  1210. for (const auto& datum : rng.data_)
  1211. out << space << datum;
  1212. out.flags(orig_flags);
  1213. out.fill(orig_fill);
  1214. return out;
  1215. }
  1216. template <typename CharT, typename Traits,
  1217. bitcount_t table_pow2, bitcount_t advance_pow2,
  1218. typename baseclass, typename extvalclass, bool kdd>
  1219. std::basic_istream<CharT,Traits>&
  1220. operator>>(std::basic_istream<CharT,Traits>& in,
  1221. extended<table_pow2, advance_pow2,
  1222. baseclass, extvalclass, kdd>& rng)
  1223. {
  1224. extended<table_pow2, advance_pow2, baseclass, extvalclass> new_rng;
  1225. auto& base_rng = static_cast<baseclass&>(new_rng);
  1226. in >> base_rng;
  1227. if (in.fail())
  1228. return in;
  1229. auto orig_flags = in.flags(std::ios_base::dec | std::ios_base::skipws);
  1230. for (auto& datum : new_rng.data_) {
  1231. in >> datum;
  1232. if (in.fail())
  1233. goto bail;
  1234. }
  1235. rng = new_rng;
  1236. bail:
  1237. in.flags(orig_flags);
  1238. return in;
  1239. }
  1240. template <bitcount_t table_pow2, bitcount_t advance_pow2,
  1241. typename baseclass, typename extvalclass, bool kdd>
  1242. void
  1243. extended<table_pow2,advance_pow2,baseclass,extvalclass,kdd>::advance_table()
  1244. {
  1245. bool carry = false;
  1246. for (size_t i = 0; i < table_size; ++i) {
  1247. if (carry) {
  1248. carry = insideout::external_step(data_[i],i+1);
  1249. }
  1250. bool carry2 = insideout::external_step(data_[i],i+1);
  1251. carry = carry || carry2;
  1252. }
  1253. }
  1254. template <bitcount_t table_pow2, bitcount_t advance_pow2,
  1255. typename baseclass, typename extvalclass, bool kdd>
  1256. void
  1257. extended<table_pow2,advance_pow2,baseclass,extvalclass,kdd>::advance_table(
  1258. state_type delta, bool isForwards)
  1259. {
  1260. typedef typename baseclass::state_type base_state_t;
  1261. typedef typename extvalclass::state_type ext_state_t;
  1262. constexpr bitcount_t basebits = sizeof(base_state_t)*8;
  1263. constexpr bitcount_t extbits = sizeof(ext_state_t)*8;
  1264. static_assert(basebits <= extbits || advance_pow2 > 0,
  1265. "Current implementation might overflow its carry");
  1266. base_state_t carry = 0;
  1267. for (size_t i = 0; i < table_size; ++i) {
  1268. base_state_t total_delta = carry + delta;
  1269. ext_state_t trunc_delta = ext_state_t(total_delta);
  1270. if (basebits > extbits) {
  1271. carry = total_delta >> extbits;
  1272. } else {
  1273. carry = 0;
  1274. }
  1275. carry +=
  1276. insideout::external_advance(data_[i],i+1, trunc_delta, isForwards);
  1277. }
  1278. }
  1279. template <bitcount_t table_pow2, bitcount_t advance_pow2,
  1280. typename baseclass, typename extvalclass, bool kdd>
  1281. void extended<table_pow2,advance_pow2,baseclass,extvalclass,kdd>::advance(
  1282. state_type distance, bool forwards)
  1283. {
  1284. static_assert(kdd,
  1285. "Efficient advance is too hard for non-kdd extension. "
  1286. "For a weak advance, cast to base class");
  1287. state_type zero =
  1288. baseclass::is_mcg ? this->state_ & state_type(3U) : state_type(0U);
  1289. if (may_tick) {
  1290. state_type ticks = distance >> (advance_pow2*may_tick);
  1291. // ^-- stupidity to appease GCC
  1292. // warnings
  1293. state_type adv_mask =
  1294. baseclass::is_mcg ? tick_mask << 2 : tick_mask;
  1295. state_type next_advance_distance = this->distance(zero, adv_mask);
  1296. if (!forwards)
  1297. next_advance_distance = (-next_advance_distance) & tick_mask;
  1298. if (next_advance_distance < (distance & tick_mask)) {
  1299. ++ticks;
  1300. }
  1301. if (ticks)
  1302. advance_table(ticks, forwards);
  1303. }
  1304. if (forwards) {
  1305. if (may_tock && this->distance(zero) <= distance)
  1306. advance_table();
  1307. baseclass::advance(distance);
  1308. } else {
  1309. if (may_tock && -(this->distance(zero)) <= distance)
  1310. advance_table(state_type(1U), false);
  1311. baseclass::advance(-distance);
  1312. }
  1313. }
  1314. } // namespace pcg_detail
  1315. namespace pcg_engines {
  1316. using namespace pcg_detail;
  1317. /* Predefined types for XSH RS */
  1318. typedef oneseq_base<uint8_t, uint16_t, xsh_rs_mixin> oneseq_xsh_rs_16_8;
  1319. typedef oneseq_base<uint16_t, uint32_t, xsh_rs_mixin> oneseq_xsh_rs_32_16;
  1320. typedef oneseq_base<uint32_t, uint64_t, xsh_rs_mixin> oneseq_xsh_rs_64_32;
  1321. typedef oneseq_base<uint64_t, pcg128_t, xsh_rs_mixin> oneseq_xsh_rs_128_64;
  1322. typedef unique_base<uint8_t, uint16_t, xsh_rs_mixin> unique_xsh_rs_16_8;
  1323. typedef unique_base<uint16_t, uint32_t, xsh_rs_mixin> unique_xsh_rs_32_16;
  1324. typedef unique_base<uint32_t, uint64_t, xsh_rs_mixin> unique_xsh_rs_64_32;
  1325. typedef unique_base<uint64_t, pcg128_t, xsh_rs_mixin> unique_xsh_rs_128_64;
  1326. typedef setseq_base<uint8_t, uint16_t, xsh_rs_mixin> setseq_xsh_rs_16_8;
  1327. typedef setseq_base<uint16_t, uint32_t, xsh_rs_mixin> setseq_xsh_rs_32_16;
  1328. typedef setseq_base<uint32_t, uint64_t, xsh_rs_mixin> setseq_xsh_rs_64_32;
  1329. typedef setseq_base<uint64_t, pcg128_t, xsh_rs_mixin> setseq_xsh_rs_128_64;
  1330. typedef mcg_base<uint8_t, uint16_t, xsh_rs_mixin> mcg_xsh_rs_16_8;
  1331. typedef mcg_base<uint16_t, uint32_t, xsh_rs_mixin> mcg_xsh_rs_32_16;
  1332. typedef mcg_base<uint32_t, uint64_t, xsh_rs_mixin> mcg_xsh_rs_64_32;
  1333. typedef mcg_base<uint64_t, pcg128_t, xsh_rs_mixin> mcg_xsh_rs_128_64;
  1334. /* Predefined types for XSH RR */
  1335. typedef oneseq_base<uint8_t, uint16_t, xsh_rr_mixin> oneseq_xsh_rr_16_8;
  1336. typedef oneseq_base<uint16_t, uint32_t, xsh_rr_mixin> oneseq_xsh_rr_32_16;
  1337. typedef oneseq_base<uint32_t, uint64_t, xsh_rr_mixin> oneseq_xsh_rr_64_32;
  1338. typedef oneseq_base<uint64_t, pcg128_t, xsh_rr_mixin> oneseq_xsh_rr_128_64;
  1339. typedef unique_base<uint8_t, uint16_t, xsh_rr_mixin> unique_xsh_rr_16_8;
  1340. typedef unique_base<uint16_t, uint32_t, xsh_rr_mixin> unique_xsh_rr_32_16;
  1341. typedef unique_base<uint32_t, uint64_t, xsh_rr_mixin> unique_xsh_rr_64_32;
  1342. typedef unique_base<uint64_t, pcg128_t, xsh_rr_mixin> unique_xsh_rr_128_64;
  1343. typedef setseq_base<uint8_t, uint16_t, xsh_rr_mixin> setseq_xsh_rr_16_8;
  1344. typedef setseq_base<uint16_t, uint32_t, xsh_rr_mixin> setseq_xsh_rr_32_16;
  1345. typedef setseq_base<uint32_t, uint64_t, xsh_rr_mixin> setseq_xsh_rr_64_32;
  1346. typedef setseq_base<uint64_t, pcg128_t, xsh_rr_mixin> setseq_xsh_rr_128_64;
  1347. typedef mcg_base<uint8_t, uint16_t, xsh_rr_mixin> mcg_xsh_rr_16_8;
  1348. typedef mcg_base<uint16_t, uint32_t, xsh_rr_mixin> mcg_xsh_rr_32_16;
  1349. typedef mcg_base<uint32_t, uint64_t, xsh_rr_mixin> mcg_xsh_rr_64_32;
  1350. typedef mcg_base<uint64_t, pcg128_t, xsh_rr_mixin> mcg_xsh_rr_128_64;
  1351. /* Predefined types for RXS M XS */
  1352. typedef oneseq_base<uint8_t, uint8_t, rxs_m_xs_mixin> oneseq_rxs_m_xs_8_8;
  1353. typedef oneseq_base<uint16_t, uint16_t, rxs_m_xs_mixin> oneseq_rxs_m_xs_16_16;
  1354. typedef oneseq_base<uint32_t, uint32_t, rxs_m_xs_mixin> oneseq_rxs_m_xs_32_32;
  1355. typedef oneseq_base<uint64_t, uint64_t, rxs_m_xs_mixin> oneseq_rxs_m_xs_64_64;
  1356. typedef oneseq_base<pcg128_t, pcg128_t, rxs_m_xs_mixin> oneseq_rxs_m_xs_128_128;
  1357. typedef unique_base<uint8_t, uint8_t, rxs_m_xs_mixin> unique_rxs_m_xs_8_8;
  1358. typedef unique_base<uint16_t, uint16_t, rxs_m_xs_mixin> unique_rxs_m_xs_16_16;
  1359. typedef unique_base<uint32_t, uint32_t, rxs_m_xs_mixin> unique_rxs_m_xs_32_32;
  1360. typedef unique_base<uint64_t, uint64_t, rxs_m_xs_mixin> unique_rxs_m_xs_64_64;
  1361. typedef unique_base<pcg128_t, pcg128_t, rxs_m_xs_mixin> unique_rxs_m_xs_128_128;
  1362. typedef setseq_base<uint8_t, uint8_t, rxs_m_xs_mixin> setseq_rxs_m_xs_8_8;
  1363. typedef setseq_base<uint16_t, uint16_t, rxs_m_xs_mixin> setseq_rxs_m_xs_16_16;
  1364. typedef setseq_base<uint32_t, uint32_t, rxs_m_xs_mixin> setseq_rxs_m_xs_32_32;
  1365. typedef setseq_base<uint64_t, uint64_t, rxs_m_xs_mixin> setseq_rxs_m_xs_64_64;
  1366. typedef setseq_base<pcg128_t, pcg128_t, rxs_m_xs_mixin> setseq_rxs_m_xs_128_128;
  1367. // MCG versions don't make sense here, so aren't defined.
  1368. /* Predefined types for XSL RR (only defined for "large" types) */
  1369. typedef oneseq_base<uint32_t, uint64_t, xsl_rr_mixin> oneseq_xsl_rr_64_32;
  1370. typedef oneseq_base<uint64_t, pcg128_t, xsl_rr_mixin> oneseq_xsl_rr_128_64;
  1371. typedef unique_base<uint32_t, uint64_t, xsl_rr_mixin> unique_xsl_rr_64_32;
  1372. typedef unique_base<uint64_t, pcg128_t, xsl_rr_mixin> unique_xsl_rr_128_64;
  1373. typedef setseq_base<uint32_t, uint64_t, xsl_rr_mixin> setseq_xsl_rr_64_32;
  1374. typedef setseq_base<uint64_t, pcg128_t, xsl_rr_mixin> setseq_xsl_rr_128_64;
  1375. typedef mcg_base<uint32_t, uint64_t, xsl_rr_mixin> mcg_xsl_rr_64_32;
  1376. typedef mcg_base<uint64_t, pcg128_t, xsl_rr_mixin> mcg_xsl_rr_128_64;
  1377. /* Predefined types for XSL RR RR (only defined for "large" types) */
  1378. typedef oneseq_base<uint64_t, uint64_t, xsl_rr_rr_mixin>
  1379. oneseq_xsl_rr_rr_64_64;
  1380. typedef oneseq_base<pcg128_t, pcg128_t, xsl_rr_rr_mixin>
  1381. oneseq_xsl_rr_rr_128_128;
  1382. typedef unique_base<uint64_t, uint64_t, xsl_rr_rr_mixin>
  1383. unique_xsl_rr_rr_64_64;
  1384. typedef unique_base<pcg128_t, pcg128_t, xsl_rr_rr_mixin>
  1385. unique_xsl_rr_rr_128_128;
  1386. typedef setseq_base<uint64_t, uint64_t, xsl_rr_rr_mixin>
  1387. setseq_xsl_rr_rr_64_64;
  1388. typedef setseq_base<pcg128_t, pcg128_t, xsl_rr_rr_mixin>
  1389. setseq_xsl_rr_rr_128_128;
  1390. // MCG versions don't make sense here, so aren't defined.
  1391. /* Extended generators */
  1392. template <bitcount_t table_pow2, bitcount_t advance_pow2,
  1393. typename BaseRNG, bool kdd = true>
  1394. using ext_std8 = pcg_detail::extended<table_pow2, advance_pow2, BaseRNG,
  1395. oneseq_rxs_m_xs_8_8, kdd>;
  1396. template <bitcount_t table_pow2, bitcount_t advance_pow2,
  1397. typename BaseRNG, bool kdd = true>
  1398. using ext_std16 = pcg_detail::extended<table_pow2, advance_pow2, BaseRNG,
  1399. oneseq_rxs_m_xs_16_16, kdd>;
  1400. template <bitcount_t table_pow2, bitcount_t advance_pow2,
  1401. typename BaseRNG, bool kdd = true>
  1402. using ext_std32 = pcg_detail::extended<table_pow2, advance_pow2, BaseRNG,
  1403. oneseq_rxs_m_xs_32_32, kdd>;
  1404. template <bitcount_t table_pow2, bitcount_t advance_pow2,
  1405. typename BaseRNG, bool kdd = true>
  1406. using ext_std64 = pcg_detail::extended<table_pow2, advance_pow2, BaseRNG,
  1407. oneseq_rxs_m_xs_64_64, kdd>;
  1408. template <bitcount_t table_pow2, bitcount_t advance_pow2, bool kdd = true>
  1409. using ext_oneseq_rxs_m_xs_32_32 =
  1410. ext_std32<table_pow2, advance_pow2, oneseq_rxs_m_xs_32_32, kdd>;
  1411. template <bitcount_t table_pow2, bitcount_t advance_pow2, bool kdd = true>
  1412. using ext_mcg_xsh_rs_64_32 =
  1413. ext_std32<table_pow2, advance_pow2, mcg_xsh_rs_64_32, kdd>;
  1414. template <bitcount_t table_pow2, bitcount_t advance_pow2, bool kdd = true>
  1415. using ext_oneseq_xsh_rs_64_32 =
  1416. ext_std32<table_pow2, advance_pow2, oneseq_xsh_rs_64_32, kdd>;
  1417. template <bitcount_t table_pow2, bitcount_t advance_pow2, bool kdd = true>
  1418. using ext_setseq_xsh_rr_64_32 =
  1419. ext_std32<table_pow2, advance_pow2, setseq_xsh_rr_64_32, kdd>;
  1420. template <bitcount_t table_pow2, bitcount_t advance_pow2, bool kdd = true>
  1421. using ext_mcg_xsl_rr_128_64 =
  1422. ext_std64<table_pow2, advance_pow2, mcg_xsl_rr_128_64, kdd>;
  1423. template <bitcount_t table_pow2, bitcount_t advance_pow2, bool kdd = true>
  1424. using ext_oneseq_xsl_rr_128_64 =
  1425. ext_std64<table_pow2, advance_pow2, oneseq_xsl_rr_128_64, kdd>;
  1426. template <bitcount_t table_pow2, bitcount_t advance_pow2, bool kdd = true>
  1427. using ext_setseq_xsl_rr_128_64 =
  1428. ext_std64<table_pow2, advance_pow2, setseq_xsl_rr_128_64, kdd>;
  1429. } // namespace pcg_engines
  1430. typedef pcg_engines::setseq_xsh_rr_64_32 pcg32;
  1431. typedef pcg_engines::oneseq_xsh_rr_64_32 pcg32_oneseq;
  1432. typedef pcg_engines::unique_xsh_rr_64_32 pcg32_unique;
  1433. typedef pcg_engines::mcg_xsh_rs_64_32 pcg32_fast;
  1434. typedef pcg_engines::setseq_xsl_rr_128_64 pcg64;
  1435. typedef pcg_engines::oneseq_xsl_rr_128_64 pcg64_oneseq;
  1436. typedef pcg_engines::unique_xsl_rr_128_64 pcg64_unique;
  1437. typedef pcg_engines::mcg_xsl_rr_128_64 pcg64_fast;
  1438. typedef pcg_engines::setseq_rxs_m_xs_8_8 pcg8_once_insecure;
  1439. typedef pcg_engines::setseq_rxs_m_xs_16_16 pcg16_once_insecure;
  1440. typedef pcg_engines::setseq_rxs_m_xs_32_32 pcg32_once_insecure;
  1441. typedef pcg_engines::setseq_rxs_m_xs_64_64 pcg64_once_insecure;
  1442. typedef pcg_engines::setseq_xsl_rr_rr_128_128 pcg128_once_insecure;
  1443. typedef pcg_engines::oneseq_rxs_m_xs_8_8 pcg8_oneseq_once_insecure;
  1444. typedef pcg_engines::oneseq_rxs_m_xs_16_16 pcg16_oneseq_once_insecure;
  1445. typedef pcg_engines::oneseq_rxs_m_xs_32_32 pcg32_oneseq_once_insecure;
  1446. typedef pcg_engines::oneseq_rxs_m_xs_64_64 pcg64_oneseq_once_insecure;
  1447. typedef pcg_engines::oneseq_xsl_rr_rr_128_128 pcg128_oneseq_once_insecure;
  1448. // These two extended RNGs provide two-dimensionally equidistributed
  1449. // 32-bit generators. pcg32_k2_fast occupies the same space as pcg64,
  1450. // and can be called twice to generate 64 bits, but does not required
  1451. // 128-bit math; on 32-bit systems, it's faster than pcg64 as well.
  1452. typedef pcg_engines::ext_setseq_xsh_rr_64_32<1,16,true> pcg32_k2;
  1453. typedef pcg_engines::ext_oneseq_xsh_rs_64_32<1,32,true> pcg32_k2_fast;
  1454. // These eight extended RNGs have about as much state as arc4random
  1455. //
  1456. // - the k variants are k-dimensionally equidistributed
  1457. // - the c variants offer better crypographic security
  1458. //
  1459. // (just how good the cryptographic security is is an open question)
  1460. typedef pcg_engines::ext_setseq_xsh_rr_64_32<6,16,true> pcg32_k64;
  1461. typedef pcg_engines::ext_mcg_xsh_rs_64_32<6,32,true> pcg32_k64_oneseq;
  1462. typedef pcg_engines::ext_oneseq_xsh_rs_64_32<6,32,true> pcg32_k64_fast;
  1463. typedef pcg_engines::ext_setseq_xsh_rr_64_32<6,16,false> pcg32_c64;
  1464. typedef pcg_engines::ext_oneseq_xsh_rs_64_32<6,32,false> pcg32_c64_oneseq;
  1465. typedef pcg_engines::ext_mcg_xsh_rs_64_32<6,32,false> pcg32_c64_fast;
  1466. typedef pcg_engines::ext_setseq_xsl_rr_128_64<5,16,true> pcg64_k32;
  1467. typedef pcg_engines::ext_oneseq_xsl_rr_128_64<5,128,true> pcg64_k32_oneseq;
  1468. typedef pcg_engines::ext_mcg_xsl_rr_128_64<5,128,true> pcg64_k32_fast;
  1469. typedef pcg_engines::ext_setseq_xsl_rr_128_64<5,16,false> pcg64_c32;
  1470. typedef pcg_engines::ext_oneseq_xsl_rr_128_64<5,128,false> pcg64_c32_oneseq;
  1471. typedef pcg_engines::ext_mcg_xsl_rr_128_64<5,128,false> pcg64_c32_fast;
  1472. // These eight extended RNGs have more state than the Mersenne twister
  1473. //
  1474. // - the k variants are k-dimensionally equidistributed
  1475. // - the c variants offer better crypographic security
  1476. //
  1477. // (just how good the cryptographic security is is an open question)
  1478. typedef pcg_engines::ext_setseq_xsh_rr_64_32<10,16,true> pcg32_k1024;
  1479. typedef pcg_engines::ext_oneseq_xsh_rs_64_32<10,32,true> pcg32_k1024_fast;
  1480. typedef pcg_engines::ext_setseq_xsh_rr_64_32<10,16,false> pcg32_c1024;
  1481. typedef pcg_engines::ext_oneseq_xsh_rs_64_32<10,32,false> pcg32_c1024_fast;
  1482. typedef pcg_engines::ext_setseq_xsl_rr_128_64<10,16,true> pcg64_k1024;
  1483. typedef pcg_engines::ext_oneseq_xsl_rr_128_64<10,128,true> pcg64_k1024_fast;
  1484. typedef pcg_engines::ext_setseq_xsl_rr_128_64<10,16,false> pcg64_c1024;
  1485. typedef pcg_engines::ext_oneseq_xsl_rr_128_64<10,128,false> pcg64_c1024_fast;
  1486. // These generators have an insanely huge period (2^524352), and is suitable
  1487. // for silly party tricks, such as dumping out 64 KB ZIP files at an arbitrary
  1488. // point in the future. [Actually, over the full period of the generator, it
  1489. // will produce every 64 KB ZIP file 2^64 times!]
  1490. typedef pcg_engines::ext_setseq_xsh_rr_64_32<14,16,true> pcg32_k16384;
  1491. typedef pcg_engines::ext_oneseq_xsh_rs_64_32<14,32,true> pcg32_k16384_fast;
  1492. #ifdef _MSC_VER
  1493. #pragma warning(default:4146)
  1494. #endif
  1495. // NOLINTEND(*)
  1496. #endif // PCG_RAND_HPP_INCLUDED