Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use xoroshiro128+ random generator (everywhere besides BSON code).
- Loading branch information
Showing
93 changed files
with
481 additions
and
406 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include "tpt-rand.h" | ||
#include <cstdlib> | ||
|
||
/* xoroshiro128+ by David Blackman and Sebastiano Vigna */ | ||
|
||
static inline uint64_t rotl(const uint64_t x, int k) { | ||
return (x << k) | (x >> (64 - k)); | ||
} | ||
|
||
uint64_t RandomGen::next(void) { | ||
const uint64_t s0 = s[0]; | ||
uint64_t s1 = s[1]; | ||
const uint64_t result = s0 + s1; | ||
|
||
s1 ^= s0; | ||
s[0] = rotl(s0, 55) ^ s1 ^ (s1 << 14); // a, b | ||
s[1] = rotl(s1, 36); // c | ||
|
||
return result; | ||
} | ||
|
||
unsigned int RandomGen::operator()() | ||
{ | ||
return next()&0xFFFFFFFF; | ||
} | ||
|
||
unsigned int RandomGen::between(unsigned int lower, unsigned int upper) | ||
{ | ||
unsigned int r = (*this)(); | ||
|
||
return r % (upper - lower + 1) + lower; | ||
} | ||
|
||
float RandomGen::uniform01() | ||
{ | ||
return static_cast<float>(random_gen())/(float)0xFFFFFFFF; | ||
} | ||
|
||
RandomGen::RandomGen() | ||
{ | ||
s[0] = 1; | ||
s[1] = 2; | ||
} | ||
|
||
void RandomGen::seed(unsigned int sd) | ||
{ | ||
s[0] = sd; | ||
s[1] = sd; | ||
} | ||
|
||
RandomGen random_gen; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#ifndef TPT_RAND_ | ||
#define TPT_RAND_ | ||
|
||
#include <stdint.h> | ||
|
||
class RandomGen | ||
{ | ||
private: | ||
uint64_t s[2]; | ||
uint64_t next(void); | ||
public: | ||
unsigned int operator()(); | ||
unsigned int between(unsigned int lower, unsigned int upper); | ||
float uniform01(); | ||
|
||
RandomGen(); | ||
void seed(unsigned int sd); | ||
}; | ||
|
||
extern RandomGen random_gen; | ||
|
||
#endif /* TPT_RAND_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,6 +70,7 @@ | |
struct playerst; | ||
|
||
#include "ElementClasses.h" | ||
#include "common/tpt-rand.h" | ||
|
||
|
||
#endif /* ELEMENTS_H_ */ |
Oops, something went wrong.
cda029f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:)
cda029f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that's nice. next(), when compiled in release mode, is 14 instructions (at least on my laptop)
All low level, high speed instructions. Plus, almost everything it does is directly operating on registers, not memory, making it even faster (1-2 uOPs).
Only thing that should be different for standard builds is RORX not being there, as RORX is a newer instruction and as such less systems have it.