PostgreSQL Source Code  git master
pg_prng.c File Reference
#include "c.h"
#include <math.h>
#include "common/pg_prng.h"
#include "port/pg_bitutils.h"
Include dependency graph for pg_prng.c:

Go to the source code of this file.

Functions

static uint64 rotl (uint64 x, int bits)
 
static uint64 xoroshiro128ss (pg_prng_state *state)
 
static uint64 splitmix64 (uint64 *state)
 
void pg_prng_seed (pg_prng_state *state, uint64 seed)
 
void pg_prng_fseed (pg_prng_state *state, double fseed)
 
bool pg_prng_seed_check (pg_prng_state *state)
 
uint64 pg_prng_uint64 (pg_prng_state *state)
 
uint64 pg_prng_uint64_range (pg_prng_state *state, uint64 rmin, uint64 rmax)
 
int64 pg_prng_int64 (pg_prng_state *state)
 
int64 pg_prng_int64p (pg_prng_state *state)
 
uint32 pg_prng_uint32 (pg_prng_state *state)
 
int32 pg_prng_int32 (pg_prng_state *state)
 
int32 pg_prng_int32p (pg_prng_state *state)
 
double pg_prng_double (pg_prng_state *state)
 
bool pg_prng_bool (pg_prng_state *state)
 

Variables

pg_prng_state pg_global_prng_state
 

Function Documentation

◆ pg_prng_bool()

bool pg_prng_bool ( pg_prng_state state)

Definition at line 242 of file pg_prng.c.

243 {
244  uint64 v = xoroshiro128ss(state);
245 
246  return (bool) (v >> 63);
247 }
static uint64 xoroshiro128ss(pg_prng_state *state)
Definition: pg_prng.c:48
Definition: regguts.h:318

References xoroshiro128ss().

Referenced by gistchoose().

◆ pg_prng_double()

double pg_prng_double ( pg_prng_state state)

Definition at line 226 of file pg_prng.c.

227 {
228  uint64 v = xoroshiro128ss(state);
229 
230  /*
231  * As above, assume there's 52 mantissa bits in a double. This result
232  * could round to 1.0 if double's precision is less than that; but we
233  * assume IEEE float arithmetic elsewhere in Postgres, so this seems OK.
234  */
235  return ldexp((double) (v >> (64 - 52)), -52);
236 }

References xoroshiro128ss().

Referenced by check_log_duration(), computeIterativeZipfian(), doLog(), drandom(), explain_ExecutorStart(), geqo_rand(), get_normal_pair(), getExponentialRand(), getGaussianRand(), getPoissonRand(), perform_spin_delay(), sampler_random_fract(), and StartTransaction().

◆ pg_prng_fseed()

void pg_prng_fseed ( pg_prng_state state,
double  fseed 
)

Definition at line 96 of file pg_prng.c.

97 {
98  /* Assume there's about 52 mantissa bits; the sign contributes too. */
99  int64 seed = ((double) ((UINT64CONST(1) << 52) - 1)) * fseed;
100 
101  pg_prng_seed(state, (uint64) seed);
102 }
void pg_prng_seed(pg_prng_state *state, uint64 seed)
Definition: pg_prng.c:83

References pg_prng_seed().

Referenced by geqo_set_seed(), and setseed().

◆ pg_prng_int32()

int32 pg_prng_int32 ( pg_prng_state state)

Definition at line 201 of file pg_prng.c.

202 {
203  uint64 v = xoroshiro128ss(state);
204 
205  return (int32) (v >> 32);
206 }
signed int int32
Definition: c.h:429

References xoroshiro128ss().

Referenced by prepare_buf().

◆ pg_prng_int32p()

int32 pg_prng_int32p ( pg_prng_state state)

Definition at line 212 of file pg_prng.c.

213 {
214  uint64 v = xoroshiro128ss(state);
215 
216  return (int32) (v >> 33);
217 }

References xoroshiro128ss().

Referenced by create_and_test_bloom().

◆ pg_prng_int64()

int64 pg_prng_int64 ( pg_prng_state state)

Definition at line 167 of file pg_prng.c.

168 {
169  return (int64) xoroshiro128ss(state);
170 }

References xoroshiro128ss().

◆ pg_prng_int64p()

int64 pg_prng_int64p ( pg_prng_state state)

Definition at line 176 of file pg_prng.c.

177 {
178  return (int64) (xoroshiro128ss(state) & UINT64CONST(0x7FFFFFFFFFFFFFFF));
179 }

References xoroshiro128ss().

◆ pg_prng_seed()

void pg_prng_seed ( pg_prng_state state,
uint64  seed 
)

Definition at line 83 of file pg_prng.c.

84 {
85  state->s0 = splitmix64(&seed);
86  state->s1 = splitmix64(&seed);
87  /* Let's just make sure we didn't get all-zeroes */
88  (void) pg_prng_seed_check(state);
89 }
static uint64 splitmix64(uint64 *state)
Definition: pg_prng.c:66
bool pg_prng_seed_check(pg_prng_state *state)
Definition: pg_prng.c:108

References pg_prng_seed_check(), and splitmix64().

Referenced by choose_dsm_implementation(), drandom(), InitProcessGlobals(), initRandomState(), main(), permute(), pg_prng_fseed(), sampler_random_init_state(), and set_random_seed().

◆ pg_prng_seed_check()

bool pg_prng_seed_check ( pg_prng_state state)

Definition at line 108 of file pg_prng.c.

109 {
110  /*
111  * If the seeding mechanism chanced to produce all-zeroes, insert
112  * something nonzero. Anything would do; use Knuth's LCG parameters.
113  */
114  if (unlikely(state->s0 == 0 && state->s1 == 0))
115  {
116  state->s0 = UINT64CONST(0x5851F42D4C957F2D);
117  state->s1 = UINT64CONST(0x14057B7EF767814F);
118  }
119 
120  /* As a convenience for the pg_prng_strong_seed macro, return true */
121  return true;
122 }
#define unlikely(x)
Definition: c.h:273

References unlikely.

Referenced by pg_prng_seed().

◆ pg_prng_uint32()

uint32 pg_prng_uint32 ( pg_prng_state state)

Definition at line 185 of file pg_prng.c.

186 {
187  /*
188  * Although xoroshiro128** is not known to have any weaknesses in
189  * randomness of low-order bits, we prefer to use the upper bits of its
190  * result here and below.
191  */
192  uint64 v = xoroshiro128ss(state);
193 
194  return (uint32) (v >> 32);
195 }
unsigned int uint32
Definition: c.h:441

References xoroshiro128ss().

Referenced by _bt_findinsertloc(), acquire_sample_rows(), anl_init_selection_state(), anl_random_fract(), choose_dsm_implementation(), dsm_create(), dsm_postmaster_startup(), ExecInitSampleScan(), InitProcessGlobals(), make_main_region_dsm_handle(), reservoir_init_selection_state(), and test_huge_distances().

◆ pg_prng_uint64()

uint64 pg_prng_uint64 ( pg_prng_state state)

Definition at line 128 of file pg_prng.c.

129 {
130  return xoroshiro128ss(state);
131 }

References xoroshiro128ss().

Referenced by bt_check_every_level(), initRandomState(), main(), and permute().

◆ pg_prng_uint64_range()

uint64 pg_prng_uint64_range ( pg_prng_state state,
uint64  rmin,
uint64  rmax 
)

Definition at line 138 of file pg_prng.c.

139 {
140  uint64 val;
141 
142  if (likely(rmax > rmin))
143  {
144  /*
145  * Use bitmask rejection method to generate an offset in 0..range.
146  * Each generated val is less than twice "range", so on average we
147  * should not have to iterate more than twice.
148  */
149  uint64 range = rmax - rmin;
150  uint32 rshift = 63 - pg_leftmost_one_pos64(range);
151 
152  do
153  {
154  val = xoroshiro128ss(state) >> rshift;
155  } while (val > range);
156  }
157  else
158  val = 0;
159 
160  return rmin + val;
161 }
#define likely(x)
Definition: c.h:272
long val
Definition: informix.c:664
static int pg_leftmost_one_pos64(uint64 word)
Definition: pg_bitutils.h:49
static struct cvec * range(struct vars *v, chr a, chr b, int cases)
Definition: regc_locale.c:412

References likely, pg_leftmost_one_pos64(), range(), val, and xoroshiro128ss().

Referenced by geqo_randint(), GetPermutation(), getrand(), permute(), SetTempTablespaces(), spgdoinsert(), test_pattern(), and testdelete().

◆ rotl()

static uint64 rotl ( uint64  x,
int  bits 
)
inlinestatic

Definition at line 35 of file pg_prng.c.

36 {
37  return (x << bits) | (x >> (64 - bits));
38 }
int x
Definition: isn.c:71

References x.

Referenced by xoroshiro128ss().

◆ splitmix64()

static uint64 splitmix64 ( uint64 *  state)
static

Definition at line 66 of file pg_prng.c.

67 {
68  /* state update */
69  uint64 val = (*state += UINT64CONST(0x9E3779B97f4A7C15));
70 
71  /* value extraction */
72  val = (val ^ (val >> 30)) * UINT64CONST(0xBF58476D1CE4E5B9);
73  val = (val ^ (val >> 27)) * UINT64CONST(0x94D049BB133111EB);
74 
75  return val ^ (val >> 31);
76 }

References val.

Referenced by pg_prng_seed().

◆ xoroshiro128ss()

static uint64 xoroshiro128ss ( pg_prng_state state)
static

Definition at line 48 of file pg_prng.c.

49 {
50  uint64 s0 = state->s0,
51  sx = state->s1 ^ s0,
52  val = rotl(s0 * 5, 7) * 9;
53 
54  /* update state */
55  state->s0 = rotl(s0, 24) ^ sx ^ (sx << 16);
56  state->s1 = rotl(sx, 37);
57 
58  return val;
59 }
static uint64 rotl(uint64 x, int bits)
Definition: pg_prng.c:35

References rotl(), and val.

Referenced by pg_prng_bool(), pg_prng_double(), pg_prng_int32(), pg_prng_int32p(), pg_prng_int64(), pg_prng_int64p(), pg_prng_uint32(), pg_prng_uint64(), and pg_prng_uint64_range().

Variable Documentation

◆ pg_global_prng_state