GNU Radio Manual and C++ API Reference  v3.9.2.0-89-gb7c7001e
The Free & Open Software Radio Ecosystem
xoroshiro128p.h
Go to the documentation of this file.
1 /*
2  * Copyright 2018 Free Software Foundation, Inc.
3  *
4  * This file is part of GNU Radio
5  *
6  * SPDX-License-Identifier: GPL-3.0-or-later
7  *
8  */
9 
10 // Built on XOROSHIRO128+ by David Blackman and Sebastiano Vigna who put this
11 // under CC-0, colloquially known as "public domain (or as close you get to that
12 // in your local legislation)" see
13 // http://xoroshiro.di.unimi.it/xoroshiro128plus.c
14 // Conversion to a local state (original used global state) done by Marcus
15 // Müller, 2018.
16 #ifndef INCLUDED_XOROSHIRO128P_H
17 #define INCLUDED_XOROSHIRO128P_H
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 #include <cstdint>
23 
24 /*! \brief rotating left shift helper
25  * According to the original authors, this will on most platforms reduce to a single
26  * instruction
27  */
28 static inline uint64_t rotl(const uint64_t x, const int k)
29 {
30  return (x << k) | (x >> (64 - k));
31 }
32 
33 
34 /*! \brief generate the next random number and update the state.
35  * This is the workhorse, here!
36  */
37 static inline uint64_t xoroshiro128p_next(uint64_t* state)
38 {
39  const uint64_t s0 = state[0];
40  uint64_t s1 = state[1];
41  const uint64_t result = s0 + s1;
42 
43  s1 ^= s0;
44  state[0] = rotl(s0, 55) ^ s1 ^ (s1 << 14); // a, b
45  state[1] = rotl(s1, 36); // c
46 
47  return result;
48 }
49 
50 
51 /*! \brief Advance the internal state by 2^64 steps; useful when coordinating multiple
52  independent RNGs This is the jump function for the generator. It is equivalent to 2^64
53  calls to next(); it can be used to generate 2^64 non-overlapping subsequences for
54  parallel computations. */
55 static inline void xoroshiro128p_jump(uint64_t* state)
56 {
57  static const uint64_t JUMP[] = { 0xbeac0467eba5facb, 0xd86b048b86aa9922 };
58 
59  uint64_t s0 = 0;
60  uint64_t s1 = 0;
61  for (unsigned int i = 0; i < sizeof(JUMP) / sizeof(*JUMP); ++i) {
62  for (unsigned int b = 0; b < 64; ++b) {
63  if (JUMP[i] & UINT64_C(1) << b) {
64  s0 ^= state[0];
65  s1 ^= state[1];
66  }
67  xoroshiro128p_next(state);
68  }
69  }
70 
71  state[0] = s0;
72  state[1] = s1;
73 }
74 
75 /*! \brief step of the SPLITMIX64 RNG; only used internally for seeding
76  * This RNG isn't as good as XOROSHIRO128+, so it's only used to initialize a 128 bit
77  * state from a seed.
78  */
79 static inline uint64_t splitmix64_next(uint64_t* state)
80 {
81  uint64_t z = (*state += 0x9e3779b97f4a7c15);
82  z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
83  z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
84  return z ^ (z >> 31);
85 }
86 
87 /*! \brief Seed the 128 bit state from a 64 bit seed
88  */
89 static inline void xoroshiro128p_seed(uint64_t* state, const uint64_t seed)
90 {
91  state[0] = seed;
92  state[1] = splitmix64_next(state);
93  xoroshiro128p_jump(state);
94 }
95 #ifdef __cplusplus
96 }
97 #endif
98 #endif // Include guard
static void xoroshiro128p_seed(uint64_t *state, const uint64_t seed)
Seed the 128 bit state from a 64 bit seed.
Definition: xoroshiro128p.h:89
static uint64_t splitmix64_next(uint64_t *state)
step of the SPLITMIX64 RNG; only used internally for seeding This RNG isn't as good as XOROSHIRO128+,...
Definition: xoroshiro128p.h:79
static void xoroshiro128p_jump(uint64_t *state)
Advance the internal state by 2^64 steps; useful when coordinating multiple independent RNGs This is ...
Definition: xoroshiro128p.h:55
static uint64_t rotl(const uint64_t x, const int k)
rotating left shift helper According to the original authors, this will on most platforms reduce to a...
Definition: xoroshiro128p.h:28
static uint64_t xoroshiro128p_next(uint64_t *state)
generate the next random number and update the state. This is the workhorse, here!
Definition: xoroshiro128p.h:37