source: libcfa/src/bits/random.hfa @ a4d098c

ADTast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since a4d098c was 7812a7b5, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Fixed errors and warning with x86 build

  • Property mode set to 100644
File size: 1.5 KB
Line 
1#pragma once
2
3#include <stdint.h>
4
5//--------------------------------------------------
6#if defined(__SIZEOF_INT128__)
7        typedef __uint128_t __lehmer64_state_t;
8        static inline uint64_t __lehmer64( __lehmer64_state_t & state ) {
9                state *= 0xda942042e4dd58b5;
10                return state >> 64;
11        }
12
13//--------------------------------------------------
14        typedef uint64_t __wyhash64_state_t;
15        static inline uint64_t __wyhash64( __wyhash64_state_t & state ) {
16                state += 0x60bee2bee120fc15;
17                __uint128_t tmp;
18                tmp = (__uint128_t) state * 0xa3b195354a39b70d;
19                uint64_t m1 = (tmp >> 64) ^ tmp;
20                tmp = (__uint128_t)m1 * 0x1b03738712fad5c9;
21                uint64_t m2 = (tmp >> 64) ^ tmp;
22                return m2;
23        }
24#endif
25
26//--------------------------------------------------
27typedef uint64_t __xorshift64_state_t;
28static inline uint64_t __xorshift64( __xorshift64_state_t & state ) {
29        uint64_t x = state;
30        x ^= x << 13;
31        x ^= x >> 7;
32        x ^= x << 17;
33        return state = x;
34}
35
36//--------------------------------------------------
37typedef struct {
38  uint32_t a, b, c, d;
39  uint32_t counter;
40} __xorwow__state_t;
41
42/* The state array must be initialized to not be all zero in the first four words */
43static inline uint32_t __xorwow( __xorwow__state_t & state ) {
44        /* Algorithm "xorwow" from p. 5 of Marsaglia, "Xorshift RNGs" */
45        uint32_t t = state.d;
46
47        uint32_t const s = state.a;
48        state.d = state.c;
49        state.c = state.b;
50        state.b = s;
51
52        t ^= t >> 2;
53        t ^= t << 1;
54        t ^= s ^ (s << 4);
55        state.a = t;
56
57        state.counter += 362437;
58        return t + state.counter;
59}
Note: See TracBrowser for help on using the repository browser.