Permalink
Browse files

base/gmp.jl: simplify <<, >> and >>>; return arg for zero shifts.

Since our BigInts are immutable by convention, we might as well
return the same BigInt object whenever we can. The >> operators is
arguably more fundamental since only arithmetic shift is defined
for BigInts. It's handy to have >>> around as a "just shift the
damned bits, please" operator.
  • Loading branch information...
1 parent c49e4e5 commit 2e86b761986eb87d56486ea7588f3b959bbe7f06 @StefanKarpinski StefanKarpinski committed Feb 28, 2014
Showing with 7 additions and 5 deletions.
  1. +7 −5 base/gmp.jl
View
@@ -262,21 +262,23 @@ for (fJ, fC) in ((:-, :neg), (:~, :com))
end
end
-function <<(x::BigInt, c::Uint)
+function <<(x::BigInt, c::Int32)
+ c < 0 && throw(DomainError())
+ c == 0 && return x
z = BigInt()
ccall((:__gmpz_mul_2exp, :libgmp), Void, (Ptr{BigInt}, Ptr{BigInt}, Culong), &z, &x, c)
return z
end
-function >>>(x::BigInt, c::Uint)
+function >>(x::BigInt, c::Int32)
+ c < 0 && throw(DomainError())
+ c == 0 && return x
z = BigInt()
ccall((:__gmpz_fdiv_q_2exp, :libgmp), Void, (Ptr{BigInt}, Ptr{BigInt}, Culong), &z, &x, c)
return z
end
-<<(x::BigInt, c::Int32) = c < 0 ? throw(DomainError()) : x << uint(c)
->>>(x::BigInt, c::Int32) = c < 0 ? throw(DomainError()) : x >>> uint(c)
->>(x::BigInt, c::Int32) = x >>> c
+>>>(x::BigInt, c::Int32) = x >> c
trailing_zeros(x::BigInt) = int(ccall((:__gmpz_scan1, :libgmp), Culong, (Ptr{BigInt}, Culong), &x, 0))
trailing_ones(x::BigInt) = int(ccall((:__gmpz_scan0, :libgmp), Culong, (Ptr{BigInt}, Culong), &x, 0))

0 comments on commit 2e86b76

Please sign in to comment.