Skip to content
Merged
Show file tree
Hide file tree
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
22 changes: 18 additions & 4 deletions src/implementations/BigInt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# Base.BigInt.

mutability(::Type{BigInt}) = IsMutable()
mutability(::Type{BigInt}, ::typeof(copy), ::Vararg{Type}) = IsMutable()

# Copied from `deepcopy_internal` implementation in Julia:
# https://github.com/JuliaLang/julia/blob/7d41d1eb610cad490cbaece8887f9bbd2a775021/base/gmp.jl#L772
Expand All @@ -17,8 +18,21 @@ mutable_copy(x::BigInt) = Base.GMP.MPZ.set(x)

promote_operation(::typeof(copy), ::Type{BigInt}) = BigInt

function operate_to!(out::BigInt, ::typeof(copy), in::BigInt)
Base.GMP.MPZ.set!(out, in)
function operate_to!(
out::BigInt,
::typeof(copy),
in::Union{Bool,Int8,Int16,Int32,Clong,UInt8,UInt16,UInt32,Culong,BigInt},
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a big fan of calling a variable in, it will make Julia parsers and interpeters trip because it's used in x in set

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, however that is what the surrounding code does. So I followed the pattern for consistency.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, then that's a separate issue

)
let f!
if in isa BigInt
f! = Base.GMP.MPZ.set!
elseif in isa Union{Bool,Unsigned}
f! = Base.GMP.MPZ.set_ui!
elseif in isa Signed
f! = Base.GMP.MPZ.set_si!
end
f!(out, in)
end
return out
end

Expand All @@ -28,13 +42,13 @@ operate!(::typeof(copy), x::BigInt) = x

promote_operation(::typeof(zero), ::Type{BigInt}) = BigInt

operate!(::typeof(zero), x::BigInt) = Base.GMP.MPZ.set_si!(x, 0)
operate!(::typeof(zero), x::BigInt) = operate_to!(x, copy, false)

# one

promote_operation(::typeof(one), ::Type{BigInt}) = BigInt

operate!(::typeof(one), x::BigInt) = Base.GMP.MPZ.set_si!(x, 1)
operate!(::typeof(one), x::BigInt) = operate_to!(x, copy, true)

# +

Expand Down
43 changes: 43 additions & 0 deletions test/test_basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,49 @@ test_copy_Rational_Int() = _test_copy(Rational{Int})

test_copy_Rational_BigInt() = _test_copy(Rational{BigInt})

function test_copy_BigInt_smallint()
function example_ints(t::Type{<:Integer})
if t <: Signed
[
typemin(t),
typemin(t) + t(1),
t(-2),
t(-1),
t(0),
t(1),
t(2),
typemax(t) - t(1),
typemax(t),
]
elseif t <: Unsigned
[t(0), t(1), t(2), typemax(t) - t(1), typemax(t)]
elseif t == Bool
[false, true]
else
throw(ArgumentError("unknown type"))
end
end
@testset "`copy` small integer to `BigInt`" begin
for f! in (MA.operate_to!, MA.operate_to!!)
for typ in [Bool, Int8, Int16, Int32, UInt8, UInt16, UInt32]
for x in example_ints(typ)
@test let y = BigInt(3)
x == @inferred f!(y, copy, x)
end
@test let y = BigInt(3)
y === @inferred f!(y, copy, x)
end
let y = BigInt(3), x = typ(0), f! = f!
alloc_test(0) do
return f!(y, copy, x)
end
end
end
end
end
end
end

function _test_mutating_step_range(::Type{T}) where {T}
r = MA.MutatingStepRange(T(2), T(3), T(9))
expected = MA.mutability(T) isa MA.IsMutable ? 8 * ones(T, 3) : T[2, 5, 8]
Expand Down
Loading