Revert "rand: use static instead of __global (#9057)"

This reverts commit ec2f731801.
pull/9084/head
Delyan Angelov 2021-03-03 08:36:56 +02:00
parent b65ec6e867
commit b89c6d7826
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
1 changed files with 29 additions and 39 deletions

View File

@ -12,21 +12,11 @@ pub struct PRNGConfigStruct {
seed []u32 = seed.time_seed_array(2) seed []u32 = seed.time_seed_array(2)
} }
__global ( default_rng &wyrand.WyRandRNG )
// init initializes the default RNG. // init initializes the default RNG.
fn init() { fn init() {
mut srng := unsafe { static_srng() } default_rng = new_default({})
(*srng) = new_default({})
}
[unsafe]
fn static_srng() &&wyrand.WyRandRNG {
static srng := &wyrand.WyRandRNG(0)
return &srng
}
[inline]
fn get_default() &wyrand.WyRandRNG {
return unsafe { *static_srng() }
} }
// new_default returns a new instance of the default RNG. If the seed is not provided, the current time will be used to seed the instance. // new_default returns a new instance of the default RNG. If the seed is not provided, the current time will be used to seed the instance.
@ -36,115 +26,115 @@ pub fn new_default(config PRNGConfigStruct) &wyrand.WyRandRNG {
return rng return rng
} }
// seed sets the given array of `u32` values as the seed for the `get_default()`. // seed sets the given array of `u32` values as the seed for the `default_rng`.
pub fn seed(seed []u32) { pub fn seed(seed []u32) {
get_default().seed(seed) default_rng.seed(seed)
} }
// u32 returns a uniformly distributed `u32` in range `[0, 2³²)`. // u32 returns a uniformly distributed `u32` in range `[0, 2³²)`.
pub fn u32() u32 { pub fn u32() u32 {
return get_default().u32() return default_rng.u32()
} }
// u64 returns a uniformly distributed `u64` in range `[0, 2⁶⁴)`. // u64 returns a uniformly distributed `u64` in range `[0, 2⁶⁴)`.
pub fn u64() u64 { pub fn u64() u64 {
return get_default().u64() return default_rng.u64()
} }
// u32n returns a uniformly distributed pseudorandom 32-bit signed positive `u32` in range `[0, max)`. // u32n returns a uniformly distributed pseudorandom 32-bit signed positive `u32` in range `[0, max)`.
pub fn u32n(max u32) u32 { pub fn u32n(max u32) u32 {
return get_default().u32n(max) return default_rng.u32n(max)
} }
// u64n returns a uniformly distributed pseudorandom 64-bit signed positive `u64` in range `[0, max)`. // u64n returns a uniformly distributed pseudorandom 64-bit signed positive `u64` in range `[0, max)`.
pub fn u64n(max u64) u64 { pub fn u64n(max u64) u64 {
return get_default().u64n(max) return default_rng.u64n(max)
} }
// u32_in_range returns a uniformly distributed pseudorandom 32-bit unsigned `u32` in range `[min, max)`. // u32_in_range returns a uniformly distributed pseudorandom 32-bit unsigned `u32` in range `[min, max)`.
pub fn u32_in_range(min u32, max u32) u32 { pub fn u32_in_range(min u32, max u32) u32 {
return get_default().u32_in_range(min, max) return default_rng.u32_in_range(min, max)
} }
// u64_in_range returns a uniformly distributed pseudorandom 64-bit unsigned `u64` in range `[min, max)`. // u64_in_range returns a uniformly distributed pseudorandom 64-bit unsigned `u64` in range `[min, max)`.
pub fn u64_in_range(min u64, max u64) u64 { pub fn u64_in_range(min u64, max u64) u64 {
return get_default().u64_in_range(min, max) return default_rng.u64_in_range(min, max)
} }
// int returns a uniformly distributed pseudorandom 32-bit signed (possibly negative) `int`. // int returns a uniformly distributed pseudorandom 32-bit signed (possibly negative) `int`.
pub fn int() int { pub fn int() int {
return get_default().int() return default_rng.int()
} }
// intn returns a uniformly distributed pseudorandom 32-bit signed positive `int` in range `[0, max)`. // intn returns a uniformly distributed pseudorandom 32-bit signed positive `int` in range `[0, max)`.
pub fn intn(max int) int { pub fn intn(max int) int {
return get_default().intn(max) return default_rng.intn(max)
} }
// byte returns a uniformly distributed pseudorandom 8-bit unsigned positive `byte`. // byte returns a uniformly distributed pseudorandom 8-bit unsigned positive `byte`.
pub fn byte() byte { pub fn byte() byte {
return byte(get_default().u32() & 0xff) return byte(default_rng.u32() & 0xff)
} }
// int_in_range returns a uniformly distributed pseudorandom 32-bit signed int in range `[min, max)`. // int_in_range returns a uniformly distributed pseudorandom 32-bit signed int in range `[min, max)`.
// Both `min` and `max` can be negative, but we must have `min < max`. // Both `min` and `max` can be negative, but we must have `min < max`.
pub fn int_in_range(min int, max int) int { pub fn int_in_range(min int, max int) int {
return get_default().int_in_range(min, max) return default_rng.int_in_range(min, max)
} }
// int31 returns a uniformly distributed pseudorandom 31-bit signed positive `int`. // int31 returns a uniformly distributed pseudorandom 31-bit signed positive `int`.
pub fn int31() int { pub fn int31() int {
return get_default().int31() return default_rng.int31()
} }
// i64 returns a uniformly distributed pseudorandom 64-bit signed (possibly negative) `i64`. // i64 returns a uniformly distributed pseudorandom 64-bit signed (possibly negative) `i64`.
pub fn i64() i64 { pub fn i64() i64 {
return get_default().i64() return default_rng.i64()
} }
// i64n returns a uniformly distributed pseudorandom 64-bit signed positive `i64` in range `[0, max)`. // i64n returns a uniformly distributed pseudorandom 64-bit signed positive `i64` in range `[0, max)`.
pub fn i64n(max i64) i64 { pub fn i64n(max i64) i64 {
return get_default().i64n(max) return default_rng.i64n(max)
} }
// i64_in_range returns a uniformly distributed pseudorandom 64-bit signed `i64` in range `[min, max)`. // i64_in_range returns a uniformly distributed pseudorandom 64-bit signed `i64` in range `[min, max)`.
pub fn i64_in_range(min i64, max i64) i64 { pub fn i64_in_range(min i64, max i64) i64 {
return get_default().i64_in_range(min, max) return default_rng.i64_in_range(min, max)
} }
// int63 returns a uniformly distributed pseudorandom 63-bit signed positive `i64`. // int63 returns a uniformly distributed pseudorandom 63-bit signed positive `i64`.
pub fn int63() i64 { pub fn int63() i64 {
return get_default().int63() return default_rng.int63()
} }
// f32 returns a uniformly distributed 32-bit floating point in range `[0, 1)`. // f32 returns a uniformly distributed 32-bit floating point in range `[0, 1)`.
pub fn f32() f32 { pub fn f32() f32 {
return get_default().f32() return default_rng.f32()
} }
// f64 returns a uniformly distributed 64-bit floating point in range `[0, 1)`. // f64 returns a uniformly distributed 64-bit floating point in range `[0, 1)`.
pub fn f64() f64 { pub fn f64() f64 {
return get_default().f64() return default_rng.f64()
} }
// f32n returns a uniformly distributed 32-bit floating point in range `[0, max)`. // f32n returns a uniformly distributed 32-bit floating point in range `[0, max)`.
pub fn f32n(max f32) f32 { pub fn f32n(max f32) f32 {
return get_default().f32n(max) return default_rng.f32n(max)
} }
// f64n returns a uniformly distributed 64-bit floating point in range `[0, max)`. // f64n returns a uniformly distributed 64-bit floating point in range `[0, max)`.
pub fn f64n(max f64) f64 { pub fn f64n(max f64) f64 {
return get_default().f64n(max) return default_rng.f64n(max)
} }
// f32_in_range returns a uniformly distributed 32-bit floating point in range `[min, max)`. // f32_in_range returns a uniformly distributed 32-bit floating point in range `[min, max)`.
pub fn f32_in_range(min f32, max f32) f32 { pub fn f32_in_range(min f32, max f32) f32 {
return get_default().f32_in_range(min, max) return default_rng.f32_in_range(min, max)
} }
// f64_in_range returns a uniformly distributed 64-bit floating point in range `[min, max)`. // f64_in_range returns a uniformly distributed 64-bit floating point in range `[min, max)`.
pub fn f64_in_range(min f64, max f64) f64 { pub fn f64_in_range(min f64, max f64) f64 {
return get_default().f64_in_range(min, max) return default_rng.f64_in_range(min, max)
} }
const ( const (
@ -192,7 +182,7 @@ pub fn uuid_v4() string {
mut d := byte(0) mut d := byte(0)
for i_buf < buflen { for i_buf < buflen {
mut c := 0 mut c := 0
x = get_default().u64() x = default_rng.u64()
// do most of the bit manipulation at once: // do most of the bit manipulation at once:
x &= 0x0F0F0F0F0F0F0F0F x &= 0x0F0F0F0F0F0F0F0F
x += 0x3030303030303030 x += 0x3030303030303030
@ -250,7 +240,7 @@ pub fn ulid_at_millisecond(unix_time_milli u64) string {
i-- i--
} }
// first rand set // first rand set
mut x := get_default().u64() mut x := default_rng.u64()
i = 10 i = 10
for i < 19 { for i < 19 {
unsafe { unsafe {
@ -260,7 +250,7 @@ pub fn ulid_at_millisecond(unix_time_milli u64) string {
i++ i++
} }
// second rand set // second rand set
x = get_default().u64() x = default_rng.u64()
for i < 26 { for i < 26 {
unsafe { unsafe {
buf[i] = rand.ulid_encoding[x & 0x1F] buf[i] = rand.ulid_encoding[x & 0x1F]