Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: fix slowness of randn introduced by 84b6aec6 #8941

Merged
merged 1 commit into from
Nov 10, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions base/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,9 @@ const ziggurat_exp_r = 7.6971174701310497140446280481

@inline randi(rng::MersenneTwister=GLOBAL_RNG) = reinterpret(Uint64, rand_close1_open2(rng)) & 0x000fffffffffffff

# the @inline'd rand(rng) method makes randmtzig_randn slower
rand_noinline(rng::MersenneTwister) = rand(rng)

function randmtzig_randn(rng::MersenneTwister=GLOBAL_RNG)
@inbounds begin
while true
Expand All @@ -824,13 +827,13 @@ function randmtzig_randn(rng::MersenneTwister=GLOBAL_RNG)
return x # 99.3% of the time we return here 1st try
elseif idx == 0
while true
xx = -ziggurat_nor_inv_r*log(rand(rng))
yy = -log(rand(rng))
xx = -ziggurat_nor_inv_r*log(rand_noinline(rng))
yy = -log(rand_noinline(rng))
if yy+yy > xx*xx
return (rabs & 0x100) != 0x000000000 ? -ziggurat_nor_r-xx : ziggurat_nor_r+xx
end
end
elseif (fi[idx] - fi[idx+1])*rand(rng) + fi[idx+1] < exp(-0.5*x*x)
elseif (fi[idx] - fi[idx+1])*rand_noinline(rng) + fi[idx+1] < exp(-0.5*x*x)
return x # return from the triangular area
end
end
Expand Down