From cfc2af8639ec177a1c447ca1ecd233e04bd45cbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A1ll=20Haraldsson?= Date: Wed, 8 Sep 2021 11:24:30 +0000 Subject: [PATCH 1/3] Partial fix for RNG on 1.7 See #42150. SIMD code seems to have same error, not considered here. --- stdlib/Random/src/Xoshiro.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/Random/src/Xoshiro.jl b/stdlib/Random/src/Xoshiro.jl index 40da3c5ff1722..d39a41276a7e1 100644 --- a/stdlib/Random/src/Xoshiro.jl +++ b/stdlib/Random/src/Xoshiro.jl @@ -54,7 +54,7 @@ rng_native_52(::Xoshiro) = UInt64 @inline function rand(rng::Xoshiro, ::SamplerType{UInt64}) s0, s1, s2, s3 = rng.s0, rng.s1, rng.s2, rng.s3 tmp = s0 + s3 - res = tmp << 23 | tmp >> 41 + res = ((tmp << 23) | (tmp >> 41)) + s0 t = s1 << 17 s2 = xor(s2, s0) s3 = xor(s3, s1) @@ -103,7 +103,7 @@ end task = current_task() s0, s1, s2, s3 = task.rngState0, task.rngState1, task.rngState2, task.rngState3 tmp = s0 + s3 - res = tmp << 23 | tmp >> 41 + res = ((tmp << 23) | (tmp >> 41)) + s0 t = s1 << 17 s2 = xor(s2, s0) s3 = xor(s3, s1) From fc1ee934587a82e12efee5797428488164472857 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A1ll=20Haraldsson?= Date: Wed, 8 Sep 2021 11:26:48 +0000 Subject: [PATCH 2/3] Fix RNG --- src/task.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/task.c b/src/task.c index 88d4eac0863c9..d133b24fc727b 100644 --- a/src/task.c +++ b/src/task.c @@ -667,7 +667,7 @@ JL_DLLEXPORT uint64_t jl_tasklocal_genrandom(jl_task_t *task) JL_NOTSAFEPOINT uint64_t s2 = task->rngState2; uint64_t s3 = task->rngState3; - uint64_t t = s0 << 17; + uint64_t t = s1 << 17; uint64_t tmp = s0 + s3; uint64_t res = ((tmp << 23) | (tmp >> 41)) + s0; s2 ^= s0; From 2b303ac16fb8ef8512c2fed34bc27c5350d5bcff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A1ll=20Haraldsson?= Date: Fri, 10 Sep 2021 10:33:58 +0000 Subject: [PATCH 3/3] Fix SIMD too --- stdlib/Random/src/XoshiroSimd.jl | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/stdlib/Random/src/XoshiroSimd.jl b/stdlib/Random/src/XoshiroSimd.jl index e115533bb6fef..72d865a14c1cf 100644 --- a/stdlib/Random/src/XoshiroSimd.jl +++ b/stdlib/Random/src/XoshiroSimd.jl @@ -158,7 +158,7 @@ end i = 0 while i+8 <= len - res = _rotl23(_plus(s0,s3)) + res = _plus(_rotl23(_plus(s0, s3)), s0) unsafe_store!(reinterpret(Ptr{UInt64}, dst + i), f(res, T)) t = _shl17(s1) s2 = _xor(s2, s0) @@ -170,7 +170,7 @@ end i += 8 end if i < len - res = _rotl23(_plus(s0,s3)) + res = _plus(_rotl23(_plus(s0, s3)), s0) t = _shl17(s1) s2 = _xor(s2, s0) s3 = _xor(s3, s1) @@ -200,7 +200,7 @@ end i = 0 while i+8 <= len - res = _rotl23(_plus(s0,s3)) + res = _plus(_rotl23(_plus(s0, s3)), s0) shift = 0 while i+8 <= len && shift < 8 resLoc = _and(_lshr(res, shift), 0x0101010101010101) @@ -219,7 +219,7 @@ end end if i < len # we may overgenerate some bytes here, if len mod 64 <= 56 and len mod 8 != 0 - res = _rotl23(_plus(s0,s3)) + res = _plus(_rotl23(_plus(s0, s3)), s0) resLoc = _and(res, 0x0101010101010101) ref = Ref(resLoc) ccall(:memcpy, Ptr{Cvoid}, (Ptr{UInt8}, Ptr{UInt64}, Csize_t), dst+i, ref, len-i) @@ -245,7 +245,7 @@ end i = 0 while i + 8*N <= len - res = _rotl23(_plus(s0,s3)) + res = _plus(_rotl23(_plus(s0, s3)), s0) t = _shl17(s1) s2 = _xor(s2, s0) s3 = _xor(s3, s1) @@ -264,7 +264,7 @@ end msk = ntuple(i->VecElement(0x0101010101010101), Val(N)) i = 0 while i + 64*N <= len - res = _rotl23(_plus(s0,s3)) + res = _plus(_rotl23(_plus(s0, s3)), s0) t = _shl17(s1) s2 = _xor(s2, s0) s3 = _xor(s3, s1)