diff --git a/stdlib/Random/src/normal.jl b/stdlib/Random/src/normal.jl index dc5ac5101e39d..8638d3d62c624 100644 --- a/stdlib/Random/src/normal.jl +++ b/stdlib/Random/src/normal.jl @@ -35,7 +35,26 @@ julia> randn(rng, ComplexF32, (2, 3)) 0.611224+1.56403im 0.355204-0.365563im 0.0905552+1.31012im ``` """ -@inline randn(rng::AbstractRNG=default_rng()) = _randn(rng, rand(rng, UInt52Raw())) +@inline function randn(rng::AbstractRNG=default_rng()) + #= + When defining + `@inline randn(rng::AbstractRNG=default_rng()) = _randn(rng, rand(rng, UInt52Raw()))` + the function call to `_randn` is currently not inlined, resulting in slightly worse + performance for scalar random normal numbers than repeating the code of `_randn` + inside the following function. + =# + @inbounds begin + r = rand(rng, UInt52Raw()) + + # the following code is identical to the one in `_randn(rng::AbstractRNG, r::UInt64)` + r &= 0x000fffffffffffff + rabs = Int64(r>>1) # One bit for the sign + idx = rabs & 0xFF + x = ifelse(r % Bool, -rabs, rabs)*wi[idx+1] + rabs < ki[idx+1] && return x # 99.3% of the time we return here 1st try + return randn_unlikely(rng, idx, rabs, x) + end +end @inline function _randn(rng::AbstractRNG, r::UInt64) @inbounds begin