Skip to content

Commit

Permalink
only unsafe_convert ptls once
Browse files Browse the repository at this point in the history
  • Loading branch information
IanButterworth committed Jul 23, 2024
1 parent 9e27434 commit 5176f22
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions base/partr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ const heaps_lock = [SpinLock(), SpinLock()]
cong(max::UInt32) = iszero(max) ? UInt32(0) : jl_rand_ptls(max) + UInt32(1)

function jl_rand_ptls(max::UInt32)
rngseed = Base.unsafe_load(Base.unsafe_convert(Ptr{UInt64}, Core.getptls()), 2) # TODO, less horrid
ptls = Base.unsafe_convert(Ptr{UInt64}, Core.getptls())
rngseed = Base.unsafe_load(ptls, 2)
# one-extend unbias back to 64-bits
val, seed = _cong(UInt64(max), rngseed)
Base.unsafe_store!(Base.unsafe_convert(Ptr{UInt64}, Core.getptls()), seed, 2)
Base.unsafe_store!(ptls, seed, 2)
return val % UInt32
end

Expand Down

0 comments on commit 5176f22

Please sign in to comment.