The Gaudi Framework master (9cc87df9)
Loading...
Searching...
No Matches
Gaudi::Utils::QuasiRandom Namespace Reference

Create a hash with a large avalanche effect from 32 or 64 bit integers or a string. More...

Functions

std::uint32_t mix (std::uint32_t state)
 Create a hash with a large avalanche effect from a 32 bit integer.
std::uint32_t mix32 (std::uint32_t state, std::uint32_t extra)
 mix some 'extra' entropy into 'state' and return result
std::uint32_t mix64 (std::uint32_t state, uint64_t extra)
 mix some 'extra' entropy into 'state' and return result
std::uint32_t mixString (std::uint32_t state, const std::string &extra)
 mix some 'extra' entropy into 'state' and return result

Detailed Description

Create a hash with a large avalanche effect from 32 or 64 bit integers or a string.

note: the constants below are not arbitrary, but are picked carefully such that the bit shuffling has a large 'avalanche' effect... Code by Brett Mulvey (old link: http://bretm.home.comcast.net/~bretm/hash/)

note: as a result, you might call this a quasi-random (not to be confused with psuedo-random!) number generator, in that it generates an output which satisfies a requirement on the uniformity of its output distribution. (and not on the predictability of the next number in the sequence, based on knowledge of the preceding numbers)

note: another way to look at this is is as an (approximation of an) evaporating black hole: whatever you dump in to it, you get something uniformly distributed back ;-)

Function Documentation

◆ mix()

std::uint32_t Gaudi::Utils::QuasiRandom::mix ( std::uint32_t state)

Create a hash with a large avalanche effect from a 32 bit integer.

Definition at line 18 of file QuasiRandom.cpp.

18 {
19 state += ( state << 16 );
20 state ^= ( state >> 13 );
21 state += ( state << 4 );
22 state ^= ( state >> 7 );
23 state += ( state << 10 );
24 state ^= ( state >> 5 );
25 state += ( state << 8 );
26 state ^= ( state >> 16 );
27 return state;
28 }

◆ mix32()

std::uint32_t Gaudi::Utils::QuasiRandom::mix32 ( std::uint32_t state,
std::uint32_t extra )

mix some 'extra' entropy into 'state' and return result

Definition at line 30 of file QuasiRandom.cpp.

30{ return mix( state + extra ); }
std::uint32_t mix(std::uint32_t state)
Create a hash with a large avalanche effect from a 32 bit integer.

◆ mix64()

std::uint32_t Gaudi::Utils::QuasiRandom::mix64 ( std::uint32_t state,
uint64_t extra )

mix some 'extra' entropy into 'state' and return result

Definition at line 32 of file QuasiRandom.cpp.

32 {
33 state = mix32( state, std::uint32_t( extra ) );
34 return mix32( state, std::uint32_t( ( extra >> 32 ) ) );
35 }
std::uint32_t mix32(std::uint32_t state, std::uint32_t extra)
mix some 'extra' entropy into 'state' and return result

◆ mixString()

std::uint32_t Gaudi::Utils::QuasiRandom::mixString ( std::uint32_t state,
const std::string & extra )

mix some 'extra' entropy into 'state' and return result

Definition at line 37 of file QuasiRandom.cpp.

37 {
38 // prefix name with ' ' until the size is a multiple of 4.
39 std::string s = std::string( ( 4 - extra.size() % 4 ) % 4, ' ' ) + extra;
40 for ( std::size_t i = 0; i < s.size() / 4; ++i ) {
41 // FIXME: this might do something different on big endian vs. small endian machines...
42 std::uint32_t x = std::uint32_t( s[i * 4] ) | std::uint32_t( s[i * 4 + 1] ) << 8 |
43 std::uint32_t( s[i * 4 + 2] ) << 16 | std::uint32_t( s[i * 4 + 3] ) << 24;
44 state = mix32( state, x );
45 }
46 return state;
47 }
basic_string< char > string
size_type size() const noexcept