Vowpal Wabbit
rand48.cc
Go to the documentation of this file.
1 // A quick implementation similar to drand48 for cross-platform compatibility
2 #include <cstdint>
3 //
4 // NB: the 'ULL' suffix is not part of the constant it is there to
5 // prevent truncation of constant to (32-bit long) when compiling
6 // in a 32-bit env: warning: integer constant is too large for "long" type
7 //
8 #ifdef __clang__
9 #pragma clang diagnostic ignored "-Wc++11-long-long"
10 #endif
11 constexpr uint64_t a = 0xeece66d5deece66dULL;
12 constexpr uint64_t c = 2147483647;
13 
14 constexpr int bias = 127 << 23;
15 
16 float merand48(uint64_t& initial)
17 {
18  static_assert(sizeof(int) == sizeof(float), "Floats and ints are converted between, they must be the same size.");
19  initial = a * initial + c;
20  int32_t temp = ((initial >> 25) & 0x7FFFFF) | bias;
21  return reinterpret_cast<float&>(temp) - 1;
22 }
23 
24 float merand48_noadvance(uint64_t v) { return merand48(v); }
float merand48_noadvance(uint64_t v)
Definition: rand48.cc:24
float merand48(uint64_t &initial)
Definition: rand48.cc:16
constexpr uint64_t a
Definition: rand48.cc:11
constexpr int bias
Definition: rand48.cc:14
constexpr uint64_t c
Definition: rand48.cc:12