GNU Radio Manual and C++ API Reference  3.10.9.1
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 #include <cstdint>
20 extern "C" {
21 #else
22 #include <stdint.h>
23 #endif
24 
25 
26 /*! \brief rotating left shift helper
27  * According to the original authors, this will on most platforms reduce to a single
28  * instruction
29  */
30 static inline uint64_t rotl(const uint64_t x, const int k)
31 {
32  return (x << k) | (x >> (64 - k));
33 }
34 
35 
36 /*! \brief generate the next random number and update the state.
37  * This is the workhorse, here!
38  */
39 static inline uint64_t xoroshiro128p_next(uint64_t* state)
40 {
41  const uint64_t s0 = state[0];
42  uint64_t s1 = state[1];
43  const uint64_t result = s0 + s1;
44 
45  s1 ^= s0;
46  state[0] = rotl(s0, 55) ^ s1 ^ (s1 << 14); // a, b
47  state[1] = rotl(s1, 36); // c
48 
49  return result;
50 }
51 
52 
53 /*! \brief Advance the internal state by 2^64 steps; useful when coordinating multiple
54  independent RNGs This is the jump function for the generator. It is equivalent to 2^64
55  calls to next(); it can be used to generate 2^64 non-overlapping subsequences for
56  parallel computations. */
57 static inline void xoroshiro128p_jump(uint64_t* state)
58 {
59  static const uint64_t JUMP[] = { 0xbeac0467eba5facb, 0xd86b048b86aa9922 };
60 
61  uint64_t s0 = 0;
62  uint64_t s1 = 0;
63  for (unsigned int i = 0; i < sizeof(JUMP) / sizeof(*JUMP); ++i) {
64  for (unsigned int b = 0; b < 64; ++b) {
65  if (JUMP[i] & UINT64_C(1) << b) {
66  s0 ^= state[0];
67  s1 ^= state[1];
68  }
69  xoroshiro128p_next(state);
70  }
71  }
72 
73  state[0] = s0;
74  state[1] = s1;
75 }
76 
77 /*! \brief step of the SPLITMIX64 RNG; only used internally for seeding
78  * This RNG isn't as good as XOROSHIRO128+, so it's only used to initialize a 128 bit
79  * state from a seed.
80  */
81 static inline uint64_t splitmix64_next(uint64_t* state)
82 {
83  uint64_t z = (*state += 0x9e3779b97f4a7c15);
84  z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
85  z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
86  return z ^ (z >> 31);
87 }
88 
89 /*! \brief Seed the 128 bit state from a 64 bit seed
90  */
91 static inline void xoroshiro128p_seed(uint64_t* state, const uint64_t seed)
92 {
93  state[0] = seed;
94  state[1] = splitmix64_next(state);
95  xoroshiro128p_jump(state);
96 }
97 #ifdef __cplusplus
98 }
99 #endif
100 #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:91
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:81
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:57
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:30
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:39