CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
pcg.h
Go to the documentation of this file.
1#pragma once
2
18
19#include <cx/cx.h>
20#include <cx/debug/assert.h>
21
22CX_C_BEGIN
23
49
54typedef struct PcgState {
55 uint64 state;
56 uint64 inc;
58
64
77void pcgSeed(_Out_ PcgState* rng, uint64 initstate, uint64 initseq);
78
90void pcgAutoSeed(_Out_ PcgState* rng);
91
93
99
107uint32 pcgRandom(_Inout_ PcgState* rng);
108
121uint32 pcgBounded(_Inout_ PcgState* rng, uint32 bound);
122
130bool pcgFlip(_Inout_ PcgState* rng);
131
142_meta_inline int32 pcgSBounded(_Inout_ PcgState* rng, int32 bound)
143{
144 devAssert(bound >= 0);
145 return (int32)pcgBounded(rng, bound);
146}
147
166_meta_inline uint32 pcgRange(_Inout_ PcgState* rng, uint32 lower, uint32 upper)
167{
168 if (lower >= upper)
169 return lower;
170
171 return lower + pcgBounded(rng, upper - lower + 1);
172}
173
191_meta_inline int32 pcgSRange(_Inout_ PcgState* rng, int32 lower, int32 upper)
192{
193 if (lower >= upper)
194 return lower;
195
196 return (int32)(lower + pcgBounded(rng, (uint32)(upper - lower + 1)));
197}
198
207uint64 pcgRandom64(_Inout_ PcgState* rng);
208
217uint64 pcgBounded64(_Inout_ PcgState* rng, uint64 bound);
218
230_meta_inline uint64 pcgRange64(_Inout_ PcgState* rng, uint64 lower, uint64 upper)
231{
232 if (lower >= upper)
233 return lower;
234
235 return lower + pcgBounded64(rng, upper - lower + 1);
236}
237
249_meta_inline int64 pcgSRange64(_Inout_ PcgState* rng, int64 lower, int64 upper)
250{
251 if (lower >= upper)
252 return lower;
253
254 return (int64)(lower + pcgBounded64(rng, (uint64)(upper - lower + 1)));
255}
256
274float32 pcgFRange(_Inout_ PcgState* rng, float32 lower, float32 upper);
275
288float64 pcgFRange64(_Inout_ PcgState* rng, float64 lower, float64 upper);
289
291
297
326void pcgAdvance(_Inout_ PcgState* rng, uint64 delta);
327
329
331
332CX_C_END
Runtime assertion macros and failure handling.
#define devAssert(expr)
Definition assert.h:138
void pcgAdvance(PcgState *rng, uint64 delta)
bool pcgFlip(PcgState *rng)
uint32 pcgBounded(PcgState *rng, uint32 bound)
float32 pcgFRange(PcgState *rng, float32 lower, float32 upper)
uint32 pcgRange(PcgState *rng, uint32 lower, uint32 upper)
Definition pcg.h:166
uint64 pcgRandom64(PcgState *rng)
uint64 pcgBounded64(PcgState *rng, uint64 bound)
uint32 pcgRandom(PcgState *rng)
int32 pcgSBounded(PcgState *rng, int32 bound)
Definition pcg.h:142
uint64 pcgRange64(PcgState *rng, uint64 lower, uint64 upper)
Definition pcg.h:230
int32 pcgSRange(PcgState *rng, int32 lower, int32 upper)
Definition pcg.h:191
int64 pcgSRange64(PcgState *rng, int64 lower, int64 upper)
Definition pcg.h:249
float64 pcgFRange64(PcgState *rng, float64 lower, float64 upper)
void pcgAutoSeed(PcgState *rng)
void pcgSeed(PcgState *rng, uint64 initstate, uint64 initseq)
Definition pcg.h:54
uint64 inc
Controls which RNG sequence (stream) is selected - must always be odd.
Definition pcg.h:56
uint64 state
RNG state - all values are possible.
Definition pcg.h:55