Skip to content

Commit e767d3e

Browse files
authored
Special case Geometric(OneHalf()) (#1934)
* Special case Geometric(OneHalf()) * Remove OneHalf and branch at sample time * Use 1//2 instead of 0.5
1 parent ceb6343 commit e767d3e

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/univariate/discrete/geometric.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,14 @@ cf(d::Geometric, t::Real) = laplace_transform(d, -t*im)
136136

137137
### Sampling
138138

139-
rand(rng::AbstractRNG, d::Geometric) = floor(Int,-randexp(rng) / log1p(-d.p))
139+
# Inlining is required to hoist the d.p == 1//2 check when generating in bulk
140+
@inline function rand(rng::AbstractRNG, d::Geometric)
141+
if d.p == 1//2
142+
leading_zeros(rand(rng, UInt)) # This branch is a performance optimization
143+
else
144+
floor(Int,-randexp(rng) / log1p(-d.p))
145+
end
146+
end
140147

141148
### Model Fitting
142149

0 commit comments

Comments
 (0)