source: libcfa/src/bits/random.hfa@ 78de1e5

ADT ast-experimental
Last change on this file since 78de1e5 was 9fce2572, checked in by Peter A. Buhr <pabuhr@…>, 3 years ago

32-bit update of PRNG

  • Property mode set to 100644
File size: 3.5 KB
RevLine 
[e57de69]1//
2// Cforall Version 1.0.0 Copyright (C) 2022 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// random.hfa --
8//
9// Author : Peter A. Buhr
10// Created On : Fri Jan 14 07:18:11 2022
11// Last Modified By : Peter A. Buhr
[9fce2572]12// Last Modified On : Mon Nov 21 17:50:12 2022
13// Update Count : 15
[e57de69]14//
15
[13c5e19]16#pragma once
17
18#include <stdint.h>
19
[9fce2572]20// Set default PRNG for architecture size.
[d2ad151]21#ifdef __x86_64__ // 64-bit architecture
22#define LEHMER64
23#else // 32-bit architecture
24#define XORSHIFT_6_21_7
25#endif // __x86_64__
26
[9fce2572]27// C/CFA PRNG name and random-state.
28#ifdef LEHMER64
29#define PRNG_NAME lehmer64
30#define PRNG_ARG_T __uint128_t
31#endif // LEHMER64
32
33#ifdef XORSHIFT_6_21_7
34#define PRNG_NAME xorshift_6_21_7
35#define PRNG_ARG_T uint32_t
36#endif // XORSHIFT_6_21_7
37
38#ifdef __cforall // don't include in C code (invoke.h)
39
[e57de69]40// Pipelined to allow out-of-order overlap with reduced dependencies. Critically, the current random state is returned
41// (copied), and then compute and store the next random value.
[611f29d]42
[7812a7b5]43#if defined(__SIZEOF_INT128__)
[e57de69]44//--------------------------------------------------
[611f29d]45 static inline uint64_t lehmer64( __uint128_t & state ) {
46 __uint128_t ret = state;
[7812a7b5]47 state *= 0xda942042e4dd58b5;
[611f29d]48 return ret >> 64;
[7812a7b5]49 }
[13c5e19]50
51//--------------------------------------------------
[611f29d]52 static inline uint64_t wyhash64( uint64_t & state ) {
[7812a7b5]53 state += 0x60bee2bee120fc15;
54 __uint128_t tmp;
55 tmp = (__uint128_t) state * 0xa3b195354a39b70d;
56 uint64_t m1 = (tmp >> 64) ^ tmp;
57 tmp = (__uint128_t)m1 * 0x1b03738712fad5c9;
58 uint64_t m2 = (tmp >> 64) ^ tmp;
59 return m2;
60 }
61#endif
[13c5e19]62
63//--------------------------------------------------
[611f29d]64static inline uint64_t xorshift_13_7_17( uint64_t & state ) {
65 uint64_t ret = state;
66 state ^= state << 13;
67 state ^= state >> 7;
68 state ^= state << 17;
69 return ret;
[13c5e19]70}
71
[611f29d]72//--------------------------------------------------
73static inline uint32_t xorshift_6_21_7( uint32_t & state ) {
74 uint32_t ret = state;
75 state ^= state << 6;
76 state ^= state >> 21;
77 state ^= state << 7;
78 return ret;
79} // xorshift_6_21_7
80
[13c5e19]81//--------------------------------------------------
82typedef struct {
[9fce2572]83 uint32_t a, b, c, d;
84 uint32_t counter;
[611f29d]85} xorwow__state_t;
[13c5e19]86
[e57de69]87// The state array must be initialized to not be all zero in the first four words.
[611f29d]88static inline uint32_t xorwow( xorwow__state_t & state ) {
[e57de69]89 // Algorithm "xorwow" from p. 5 of Marsaglia, "Xorshift RNGs".
[611f29d]90 uint32_t ret = state.a + state.counter;
[13c5e19]91 uint32_t t = state.d;
92
93 uint32_t const s = state.a;
94 state.d = state.c;
95 state.c = state.b;
96 state.b = s;
97
98 t ^= t >> 2;
99 t ^= t << 1;
100 t ^= s ^ (s << 4);
101 state.a = t;
102
103 state.counter += 362437;
[611f29d]104 return ret;
105}
106
107//--------------------------------------------------
108static inline uint32_t LCG( uint32_t & state ) { // linear congruential generator
109 uint32_t ret = state;
110 state = 36969 * (state & 65535) + (state >> 16); // 36969 is NOT prime! No not change it!
111 return ret;
112} // LCG
113
114//--------------------------------------------------
115#define M (1_l64u << 48_l64u)
116#define A (25214903917_l64u)
117#define AI (18446708753438544741_l64u)
118#define C (11_l64u)
119#define D (16_l64u)
120
[e57de69]121// Bi-directional LCG random-number generator
[611f29d]122static inline uint32_t LCGBI_fwd( uint64_t & state ) {
123 state = (A * state + C) & (M - 1);
124 return state >> D;
[4177592f]125}
[611f29d]126
127static inline uint32_t LCGBI_bck( uint64_t & state ) {
128 unsigned int r = state >> D;
129 state = AI * (state - C) & (M - 1);
130 return r;
131}
132
133#undef M
134#undef A
135#undef AI
136#undef C
137#undef D
[9fce2572]138
139#endif // __cforall
Note: See TracBrowser for help on using the repository browser.