Skip to content

Commit 76d6a08

Browse files
authored
Add fast isodd for BigInt (#35873)
1 parent 8ef29e6 commit 76d6a08

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

base/gmp.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import .Base: *, +, -, /, <, <<, >>, >>>, <=, ==, >, >=, ^, (~), (&), (|), xor,
1010
sum, trailing_zeros, trailing_ones, count_ones, tryparse_internal,
1111
bin, oct, dec, hex, isequal, invmod, _prevpow2, _nextpow2, ndigits0zpb,
1212
widen, signed, unsafe_trunc, trunc, iszero, isone, big, flipsign, signbit,
13-
sign, hastypemax
13+
sign, hastypemax, isodd
1414

1515
if Clong == Int32
1616
const ClongMax = Union{Int8, Int16, Int32}
@@ -342,6 +342,8 @@ end
342342

343343
rem(x::Integer, ::Type{BigInt}) = BigInt(x)
344344

345+
isodd(x::BigInt) = MPZ.tstbit(x, 0)
346+
345347
function (::Type{T})(x::BigInt) where T<:Base.BitUnsigned
346348
if sizeof(T) < sizeof(Limb)
347349
convert(T, convert(Limb,x))

test/gmp.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,9 @@ end
412412
# Issue #24298
413413
@test mod(BigInt(6), UInt(5)) == mod(6, 5)
414414

415+
@test iseven(zero(BigInt))
416+
@test isodd(BigInt(typemax(UInt)))
417+
415418
@testset "cmp has values in [-1, 0, 1], issue #28780" begin
416419
# _rand produces values whose log2 is better distributed than rand
417420
_rand(::Type{BigInt}, n=1000) = let x = big(2)^rand(1:rand(1:n))

0 commit comments

Comments
 (0)