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

Classes

struct  array2x64
struct  array1x64

Public Types

typedef unsigned result_type

Public Member Functions

 Philox (const uint64_t s)
 Philox (const unsigned hi, const unsigned lo=0x40ba6a95)
void seed (const unsigned hi, const unsigned lo=0x40ba6a95)
void seed64 (const uint64_t s)
Philoxindex (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

array1x64 bumpkey (array1x64 key)
uint64_t mulhilo64 (uint64_t a, uint64_t b, uint64_t *hip)
array2x64 round (array2x64 ctr, array1x64 key)
template<int R>
array2x64 philox2x64 (array2x64 ctr, array1x64 key)

Protected Attributes

array2x64 ctr
array1x64 key

Detailed Description

Definition at line 12 of file rand123.h.


Class Documentation

◆ Philox::array2x64

struct Philox::array2x64

Definition at line 60 of file rand123.h.

Class Members
uint64_t v[2]

◆ Philox::array1x64

struct Philox::array1x64

Definition at line 61 of file rand123.h.

Class Members
uint64_t v[1]

Member Typedef Documentation

◆ result_type

typedef unsigned Philox::result_type

Definition at line 57 of file rand123.h.

Constructor & Destructor Documentation

◆ Philox() [1/3]

Philox::Philox ( )
inline

Definition at line 14 of file rand123.h.

14: ctr(), key() { seed64(0xcf492074862957a3); }

◆ Philox() [2/3]

Philox::Philox ( const uint64_t s)
inline

Definition at line 15 of file rand123.h.

15: ctr(), key() { seed64(s); }

◆ Philox() [3/3]

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

Definition at line 17 of file rand123.h.

17: ctr(), key() { seed(hi, lo); }

Member Function Documentation

◆ seed()

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

Definition at line 18 of file rand123.h.

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

◆ seed64()

void Philox::seed64 ( const uint64_t s)
inline

Definition at line 20 of file rand123.h.

21 {
22 ctr= { { 0, 0x838ca1328d07d3ba } };
23 key= { { s } };
24 }

◆ index()

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

Definition at line 26 of file rand123.h.

27 {
28 ctr= { { i, 0x838ca1328d07d3ba } };
29 return *this;
30 }

◆ sample()

unsigned Philox::sample ( )
inline

Definition at line 32 of file rand123.h.

33 {
34 ctr.v[0]++;
35 return philox2x64<6>(ctr, key).v[0]; // 6 rounds
36 //~ return philox2x64<10>(ctr, key).v[0]; // 10 rounds
37 }

◆ sample_range()

unsigned Philox::sample_range ( const unsigned range)
inline

Definition at line 39 of file rand123.h.

40 {
41 // Efficiently Generating a Number in a Range
42 // cf http://www.pcg-random.org/posts/bounded-rands.html
43 unsigned divisor= ((-range) / range) + 1; // (2^32) / range
44 if(divisor == 0) return 0;
45
46 while(true)
47 {
48 unsigned x= sample() / divisor;
49 if(x < range) return x;
50 }
51 }

◆ operator()()

unsigned Philox::operator() ( )
inline

Definition at line 54 of file rand123.h.

54{ return sample(); }

◆ min()

constexpr unsigned Philox::min ( )
inlinestaticconstexpr

Definition at line 55 of file rand123.h.

55{ return 0; }

◆ max()

constexpr unsigned Philox::max ( )
inlinestaticconstexpr

Definition at line 56 of file rand123.h.

56{ return ~unsigned(0); }

◆ bumpkey()

array1x64 Philox::bumpkey ( array1x64 key)
inlineprotected

Definition at line 63 of file rand123.h.

64 {
65 key.v[0] += 0x9E3779B97F4A7C15UL;
66 return key;
67 }

◆ mulhilo64()

uint64_t Philox::mulhilo64 ( uint64_t a,
uint64_t b,
uint64_t * hip )
inlineprotected

Definition at line 69 of file rand123.h.

70 {
71 __uint128_t product = __uint128_t(a) * __uint128_t(b);
72 *hip= uint64_t(product >> 64);
73 return uint64_t(product);
74 }

◆ round()

array2x64 Philox::round ( array2x64 ctr,
array1x64 key )
inlineprotected

Definition at line 76 of file rand123.h.

77 {
78 uint64_t hi;
79 uint64_t lo = mulhilo64(0xD2B74407B1CE6E93UL, ctr.v[0], &hi);
80
81 return { { hi^key.v[0]^ctr.v[1], lo } };
82 }

◆ philox2x64()

template<int R>
array2x64 Philox::philox2x64 ( array2x64 ctr,
array1x64 key )
inlineprotected

Definition at line 85 of file rand123.h.

86 {
87 if(R>0) { ctr = round(ctr, key); }
88 if(R>1) { key = bumpkey(key); ctr = round(ctr, key); }
89 if(R>2) { key = bumpkey(key); ctr = round(ctr, key); }
90 if(R>3) { key = bumpkey(key); ctr = round(ctr, key); }
91 if(R>4) { key = bumpkey(key); ctr = round(ctr, key); }
92 if(R>5) { key = bumpkey(key); ctr = round(ctr, key); }
93 if(R>6) { key = bumpkey(key); ctr = round(ctr, key); }
94 if(R>7) { key = bumpkey(key); ctr = round(ctr, key); }
95 if(R>8) { key = bumpkey(key); ctr = round(ctr, key); }
96 if(R>9) { key = bumpkey(key); ctr = round(ctr, key); }
97 if(R>10) { key = bumpkey(key); ctr = round(ctr, key); }
98 if(R>11) { key = bumpkey(key); ctr = round(ctr, key); }
99 if(R>12) { key = bumpkey(key); ctr = round(ctr, key); }
100 if(R>13) { key = bumpkey(key); ctr = round(ctr, key); }
101 if(R>14) { key = bumpkey(key); ctr = round(ctr, key); }
102 if(R>15) { key = bumpkey(key); ctr = round(ctr, key); }
103
104 return ctr;
105 }

Member Data Documentation

◆ ctr

array2x64 Philox::ctr
protected

Definition at line 107 of file rand123.h.

◆ key

array1x64 Philox::key
protected

Definition at line 108 of file rand123.h.


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