Skip to content

Commit 6813340

Browse files
KristofferClungben
andauthored
Rebase: Performance regression of scalar randn() between Julia 1.4 and 1.5 (#39319)
* definition of randn for scalars reverted to 1.4 * Added comments why this change is done Co-authored-by: Benjamin Lungwitz <52384612+lungben@users.noreply.github.com>
1 parent d6ef750 commit 6813340

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

stdlib/Random/src/normal.jl

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,26 @@ julia> randn(rng, ComplexF32, (2, 3))
3535
0.611224+1.56403im 0.355204-0.365563im 0.0905552+1.31012im
3636
```
3737
"""
38-
@inline randn(rng::AbstractRNG=default_rng()) = _randn(rng, rand(rng, UInt52Raw()))
38+
@inline function randn(rng::AbstractRNG=default_rng())
39+
#=
40+
When defining
41+
`@inline randn(rng::AbstractRNG=default_rng()) = _randn(rng, rand(rng, UInt52Raw()))`
42+
the function call to `_randn` is currently not inlined, resulting in slightly worse
43+
performance for scalar random normal numbers than repeating the code of `_randn`
44+
inside the following function.
45+
=#
46+
@inbounds begin
47+
r = rand(rng, UInt52Raw())
48+
49+
# the following code is identical to the one in `_randn(rng::AbstractRNG, r::UInt64)`
50+
r &= 0x000fffffffffffff
51+
rabs = Int64(r>>1) # One bit for the sign
52+
idx = rabs & 0xFF
53+
x = ifelse(r % Bool, -rabs, rabs)*wi[idx+1]
54+
rabs < ki[idx+1] && return x # 99.3% of the time we return here 1st try
55+
return randn_unlikely(rng, idx, rabs, x)
56+
end
57+
end
3958

4059
@inline function _randn(rng::AbstractRNG, r::UInt64)
4160
@inbounds begin

0 commit comments

Comments
 (0)