gKit2 light
Loading...
Searching...
No Matches
Philox32 Struct Reference

Classes

struct  array2x32
struct  array1x32

Public Types

typedef unsigned result_type

Public Member Functions

 Philox32 (const uint64_t s)
 Philox32 (const unsigned hi, const unsigned lo=0x40ba6a95)
void seed (const unsigned hi, const unsigned lo=0x40ba6a95)
void seed64 (const uint64_t s)
Philox32index (const uint64_t i)
unsigned sample ()
unsigned sample_range (const unsigned range)
unsigned operator() ()

Static Public Member Functions

static constexpr unsigned min ()
static constexpr unsigned max ()

Protected Member Functions

array1x32 bumpkey (array1x32 key)
unsigned mulhilo32 (unsigned a, unsigned b, unsigned *hip)
array2x32 round (array2x32 ctr, array1x32 key)
template<int R>
array2x32 philox2x32 (array2x32 ctr, array1x32 key)

Protected Attributes

uint64_t n
array1x32 key

Detailed Description

Definition at line 114 of file rand123.h.


Class Documentation

◆ Philox32::array2x32

struct Philox32::array2x32

Definition at line 162 of file rand123.h.

Class Members
unsigned v[2]

◆ Philox32::array1x32

struct Philox32::array1x32

Definition at line 163 of file rand123.h.

Class Members
unsigned v[1]

Member Typedef Documentation

◆ result_type

typedef unsigned Philox32::result_type

Definition at line 159 of file rand123.h.

Constructor & Destructor Documentation

◆ Philox32() [1/3]

Philox32::Philox32 ( )
inline

Definition at line 116 of file rand123.h.

116: n(), key() { seed64(0xcf492074862957a3); }

◆ Philox32() [2/3]

Philox32::Philox32 ( const uint64_t s)
inline

Definition at line 117 of file rand123.h.

117: n(), key() { seed(s); }

◆ Philox32() [3/3]

Philox32::Philox32 ( const unsigned hi,
const unsigned lo = 0x40ba6a95 )
inline

Definition at line 118 of file rand123.h.

118: n(), key() { seed(hi, lo); }

Member Function Documentation

◆ seed()

void Philox32::seed ( const unsigned hi,
const unsigned lo = 0x40ba6a95 )
inline

Definition at line 120 of file rand123.h.

120{ seed64(uint64_t(hi) << 32 | uint64_t(lo)); }

◆ seed64()

void Philox32::seed64 ( const uint64_t s)
inline

Definition at line 121 of file rand123.h.

122 {
123 n= 0;
124 key= { { unsigned(s >> 32) } };
125 }

◆ index()

Philox32 & Philox32::index ( const uint64_t i)
inline

Definition at line 127 of file rand123.h.

128 {
129 n= i;
130 return *this;
131 }

◆ sample()

unsigned Philox32::sample ( )
inline

Definition at line 133 of file rand123.h.

134 {
135 n++;
136 array2x32 ctr= { { unsigned(n >>32), unsigned(n) } };
137 return philox2x32<6>(ctr, key).v[0]; // 6 rounds
138 //~ return philox2x32<10>(ctr, key).v[0]; // 10 rounds
139 }

◆ sample_range()

unsigned Philox32::sample_range ( const unsigned range)
inline

Definition at line 141 of file rand123.h.

142 {
143 // Efficiently Generating a Number in a Range
144 // cf http://www.pcg-random.org/posts/bounded-rands.html
145 unsigned divisor= ((-range) / range) + 1; // (2^32) / range
146 if(divisor == 0) return 0;
147
148 while(true)
149 {
150 unsigned x= sample() / divisor;
151 if(x < range) return x;
152 }
153 }

◆ operator()()

unsigned Philox32::operator() ( )
inline

Definition at line 156 of file rand123.h.

156{ return sample(); }

◆ min()

constexpr unsigned Philox32::min ( )
inlinestaticconstexpr

Definition at line 157 of file rand123.h.

157{ return 0; }

◆ max()

constexpr unsigned Philox32::max ( )
inlinestaticconstexpr

Definition at line 158 of file rand123.h.

158{ return ~unsigned(0); }

◆ bumpkey()

array1x32 Philox32::bumpkey ( array1x32 key)
inlineprotected

Definition at line 165 of file rand123.h.

166 {
167 key.v[0] += 0x9E3779B9;
168 return key;
169 }

◆ mulhilo32()

unsigned Philox32::mulhilo32 ( unsigned a,
unsigned b,
unsigned * hip )
inlineprotected

Definition at line 171 of file rand123.h.

172 {
173 uint64_t product = uint64_t(a) * uint64_t(b);
174 *hip= unsigned(product >> 32);
175 return unsigned(product);
176 }

◆ round()

array2x32 Philox32::round ( array2x32 ctr,
array1x32 key )
inlineprotected

Definition at line 178 of file rand123.h.

179 {
180 uint32_t hi, lo= mulhilo32(0xd256d193, ctr.v[0], &hi);
181
182 return { { hi^key.v[0]^ctr.v[1], lo } };
183 }

◆ philox2x32()

template<int R>
array2x32 Philox32::philox2x32 ( array2x32 ctr,
array1x32 key )
inlineprotected

Definition at line 186 of file rand123.h.

187 {
188 if(R>0) { ctr = round(ctr, key); }
189 if(R>1) { key = bumpkey(key); ctr = round(ctr, key); }
190 if(R>2) { key = bumpkey(key); ctr = round(ctr, key); }
191 if(R>3) { key = bumpkey(key); ctr = round(ctr, key); }
192 if(R>4) { key = bumpkey(key); ctr = round(ctr, key); }
193 if(R>5) { key = bumpkey(key); ctr = round(ctr, key); }
194 if(R>6) { key = bumpkey(key); ctr = round(ctr, key); }
195 if(R>7) { key = bumpkey(key); ctr = round(ctr, key); }
196 if(R>8) { key = bumpkey(key); ctr = round(ctr, key); }
197 if(R>9) { key = bumpkey(key); ctr = round(ctr, key); }
198 if(R>10) { key = bumpkey(key); ctr = round(ctr, key); }
199 if(R>11) { key = bumpkey(key); ctr = round(ctr, key); }
200 if(R>12) { key = bumpkey(key); ctr = round(ctr, key); }
201 if(R>13) { key = bumpkey(key); ctr = round(ctr, key); }
202 if(R>14) { key = bumpkey(key); ctr = round(ctr, key); }
203 if(R>15) { key = bumpkey(key); ctr = round(ctr, key); }
204
205 return ctr;
206 }

Member Data Documentation

◆ n

uint64_t Philox32::n
protected

Definition at line 208 of file rand123.h.

◆ key

array1x32 Philox32::key
protected

Definition at line 209 of file rand123.h.


The documentation for this struct was generated from the following file: