| 1 |
// |
| 2 |
// SPDX-License-Identifier: BSD-3-Clause |
| 3 |
// Copyright Contributors to the OpenEXR Project. |
| 4 |
// |
| 5 |
|
| 6 |
//----------------------------------------------------------------------------- |
| 7 |
// |
| 8 |
// Routines that generate pseudo-random numbers compatible |
| 9 |
// with the standard erand48(), nrand48(), etc. functions. |
| 10 |
// |
| 11 |
//----------------------------------------------------------------------------- |
| 12 |
|
| 13 |
#include "ImathRandom.h" |
| 14 |
#include <cstdint> |
| 15 |
|
| 16 |
IMATH_INTERNAL_NAMESPACE_SOURCE_ENTER |
| 17 |
namespace |
| 18 |
{ |
| 19 |
|
| 20 |
// |
| 21 |
// Static state used by Imath::drand48(), Imath::lrand48() and Imath::srand48() |
| 22 |
// |
| 23 |
|
| 24 |
unsigned short staticState[3] = { 0, 0, 0 }; |
| 25 |
|
| 26 |
void |
| 27 |
rand48Next (unsigned short state[3]) |
| 28 |
{ |
| 29 |
// |
| 30 |
// drand48() and friends are all based on a linear congruential |
| 31 |
// sequence, |
| 32 |
// |
| 33 |
// x[n+1] = (a * x[n] + c) % m, |
| 34 |
// |
| 35 |
// where a and c are as specified below, and m == (1 << 48) |
| 36 |
// |
| 37 |
|
| 38 |
static const uint64_t a = uint64_t (0x5deece66dLL); |
| 39 |
static const uint64_t c = uint64_t (0xbLL); |
| 40 |
|
| 41 |
// |
| 42 |
// Assemble the 48-bit value x[n] from the |
| 43 |
// three 16-bit values stored in state. |
| 44 |
// |
| 45 |
|
| 46 |
// clang-format off |
| 47 |
uint64_t x = (uint64_t (state[2]) << 32) | |
| 48 |
(uint64_t (state[1]) << 16) | |
| 49 |
uint64_t (state[0]); |
| 50 |
// clang-format on |
| 51 |
|
| 52 |
// |
| 53 |
// Compute x[n+1], except for the "modulo m" part. |
| 54 |
// |
| 55 |
|
| 56 |
x = a * x + c; |
| 57 |
|
| 58 |
// |
| 59 |
// Disassemble the 48 least significant bits of x[n+1] into |
| 60 |
// three 16-bit values. Discard the 16 most significant bits; |
| 61 |
// this takes care of the "modulo m" operation. |
| 62 |
// |
| 63 |
// We assume that sizeof (unsigned short) == 2. |
| 64 |
// |
| 65 |
|
| 66 |
state[2] = (unsigned short) (x >> 32); |
| 67 |
state[1] = (unsigned short) (x >> 16); |
| 68 |
state[0] = (unsigned short) (x); |
| 69 |
} |
| 70 |
|
| 71 |
} // namespace |
| 72 |
|
| 73 |
/// |
| 74 |
/// Generate double-precision floating-point values between 0.0 and 1.0: |
| 75 |
/// |
| 76 |
/// The exponent is set to 0x3ff, which indicates a value greater |
| 77 |
/// than or equal to 1.0, and less than 2.0. The 48 most significant |
| 78 |
/// bits of the significand (mantissa) are filled with pseudo-random |
| 79 |
/// bits generated by rand48Next(). The remaining 4 bits are a copy |
| 80 |
/// of the 4 most significant bits of the significand. This results |
| 81 |
/// in bit patterns between 0x3ff0000000000000 and 0x3fffffffffffffff, |
| 82 |
/// which correspond to uniformly distributed floating-point values |
| 83 |
/// between 1.0 and 1.99999999999999978. Subtracting 1.0 from those |
| 84 |
/// values produces numbers between 0.0 and 0.99999999999999978, that |
| 85 |
/// is, between 0.0 and 1.0-DBL_EPSILON. |
| 86 |
double |
| 87 |
erand48 (unsigned short state[3]) |
| 88 |
{ |
| 89 |
rand48Next (state); |
| 90 |
|
| 91 |
union |
| 92 |
{ |
| 93 |
double d; |
| 94 |
uint64_t i; |
| 95 |
} u; |
| 96 |
|
| 97 |
// clang-format off |
| 98 |
u.i = (uint64_t (0x3ff) << 52) | // sign and exponent |
| 99 |
(uint64_t (state[2]) << 36) | // significand |
| 100 |
(uint64_t (state[1]) << 20) | |
| 101 |
(uint64_t (state[0]) << 4) | |
| 102 |
(uint64_t (state[2]) >> 12); |
| 103 |
// clang-format on |
| 104 |
|
| 105 |
return u.d - 1; |
| 106 |
} |
| 107 |
|
| 108 |
/// Return erand48() |
| 109 |
double |
| 110 |
drand48() |
| 111 |
{ |
| 112 |
return IMATH_INTERNAL_NAMESPACE::erand48 (staticState); |
| 113 |
} |
| 114 |
|
| 115 |
/// Generate uniformly distributed integers between 0 and 0x7fffffff. |
| 116 |
long int |
| 117 |
nrand48 (unsigned short state[3]) |
| 118 |
{ |
| 119 |
rand48Next (state); |
| 120 |
|
| 121 |
// clang-format off |
| 122 |
return ((long int) (state[2]) << 15) | |
| 123 |
((long int) (state[1]) >> 1); |
| 124 |
// clang-format on |
| 125 |
} |
| 126 |
|
| 127 |
/// Return nrand48() |
| 128 |
long int |
| 129 |
lrand48() |
| 130 |
{ |
| 131 |
return IMATH_INTERNAL_NAMESPACE::nrand48 (staticState); |
| 132 |
} |
| 133 |
|
| 134 |
void |
| 135 |
srand48 (long int seed) |
| 136 |
{ |
| 137 |
staticState[2] = (unsigned short) (seed >> 16); |
| 138 |
staticState[1] = (unsigned short) (seed); |
| 139 |
staticState[0] = 0x330e; |
| 140 |
} |
| 141 |
|
| 142 |
float |
| 143 |
Rand32::nextf() |
| 144 |
{ |
| 145 |
// |
| 146 |
// Generate single-precision floating-point values between 0.0 and 1.0: |
| 147 |
// |
| 148 |
// The exponent is set to 0x7f, which indicates a value greater than |
| 149 |
// or equal to 1.0, and less than 2.0. The 23 bits of the significand |
| 150 |
// (mantissa) are filled with pseudo-random bits generated by |
| 151 |
// Rand32::next(). This results in in bit patterns between 0x3f800000 |
| 152 |
// and 0x3fffffff, which correspond to uniformly distributed floating- |
| 153 |
// point values between 1.0 and 1.99999988. Subtracting 1.0 from |
| 154 |
// those values produces numbers between 0.0 and 0.99999988, that is, |
| 155 |
// between 0.0 and 1.0-FLT_EPSILON. |
| 156 |
// |
| 157 |
|
| 158 |
next(); |
| 159 |
|
| 160 |
union |
| 161 |
{ |
| 162 |
float f; |
| 163 |
unsigned int i; |
| 164 |
} u; |
| 165 |
|
| 166 |
u.i = 0x3f800000 | (_state & 0x7fffff); |
| 167 |
return u.f - 1; |
| 168 |
} |
| 169 |
|
| 170 |
IMATH_INTERNAL_NAMESPACE_SOURCE_EXIT |