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

refactor Base.GMP.MPZ.import! to be consistent with Base.GMP.MPZ.export! #56903

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
18 changes: 9 additions & 9 deletions base/gmp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -251,21 +251,21 @@ get_str!(x, a, b::BigInt) = (ccall((:__gmpz_get_str,libgmp), Ptr{Cchar}, (Ptr{Cc
set_str!(x::BigInt, a, b) = Int(ccall((:__gmpz_set_str, libgmp), Cint, (mpz_t, Ptr{UInt8}, Cint), x, a, b))
get_d(a::BigInt) = ccall((:__gmpz_get_d, libgmp), Cdouble, (mpz_t,), a)

function export!(a::AbstractVector{T}, n::BigInt; order::Integer=-1, nails::Integer=0, endian::Integer=0) where {T<:Base.BitInteger}
stride(a, 1) == 1 || throw(ArgumentError("a must have stride 1"))
function export!(x::AbstractVector{T}, n::BigInt; order::Integer=-1, nails::Integer=0, endian::Integer=0) where {T<:Base.BitInteger}
stride(x, 1) == 1 || throw(ArgumentError("x must have stride 1"))
ndigits = cld(sizeinbase(n, 2), 8*sizeof(T) - nails)
length(a) < ndigits && resize!(a, ndigits)
length(x) < ndigits && resize!(x, ndigits)
count = Ref{Csize_t}()
ccall((:__gmpz_export, libgmp), Ptr{T}, (Ptr{T}, Ref{Csize_t}, Cint, Csize_t, Cint, Csize_t, mpz_t),
a, count, order, sizeof(T), endian, nails, n)
@assert count[] ≤ length(a)
return a, Int(count[])
ccall((:__gmpz_export, libgmp),
Ptr{T},
(Ptr{T}, Ref{Csize_t}, Cint, Csize_t, Cint, Csize_t, mpz_t),
x, count, order, sizeof(T), endian, nails, n)
@assert count[] ≤ length(x)
return x, Int(count[])
end

limbs_write!(x::BigInt, a) = ccall((:__gmpz_limbs_write, libgmp), Ptr{Limb}, (mpz_t, Clong), x, a)
limbs_finish!(x::BigInt, a) = ccall((:__gmpz_limbs_finish, libgmp), Cvoid, (mpz_t, Clong), x, a)
import!(x::BigInt, a, b, c, d, e, f) = ccall((:__gmpz_import, libgmp), Cvoid,
(mpz_t, Csize_t, Cint, Csize_t, Cint, Csize_t, Ptr{Cvoid}), x, a, b, c, d, e, f)

setbit!(x, a) = (ccall((:__gmpz_setbit, libgmp), Cvoid, (mpz_t, bitcnt_t), x, a); x)
tstbit(a::BigInt, b) = ccall((:__gmpz_tstbit, libgmp), Cint, (mpz_t, bitcnt_t), a, b) % Bool
Expand Down
38 changes: 38 additions & 0 deletions test/gmp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,44 @@ end
@test string(big(0), base = rand(2:62), pad = 0) == ""
end

@testset "Base.GMP.MPZ.import!/export!" begin

function Base_GMP_MPZ_import!(x::BigInt, n::AbstractVector{T}; order::Integer=-1, nails::Integer=0, endian::Integer=0) where {T<:Base.BitInteger}
ccall((:__gmpz_import, Base.GMP.MPZ.libgmp),
Cvoid,
(Base.GMP.MPZ.mpz_t, Csize_t, Cint, Csize_t, Cint, Csize_t, Ptr{Cvoid}),
x, length(n), order, sizeof(T), endian, nails, n)
return x
end
# test import
bytes_to_import_from = Vector{UInt8}([1, 0])
int_to_import_to = BigInt()
Base_GMP_MPZ_import!(int_to_import_to, bytes_to_import_from, order=0)
@test int_to_import_to == BigInt(256)

# test export
int_to_export_from = BigInt(256)
bytes_to_export_to = Vector{UInt8}(undef, 2)
Base.GMP.MPZ.export!(bytes_to_export_to, int_to_export_from, order=0)
@test all(bytes_to_export_to .== bytes_to_import_from)

# test both composed import(export) is identity
int_to_export_from = BigInt(256)
bytes_to_export_to = Vector{UInt8}(undef, 2)
Base.GMP.MPZ.export!(bytes_to_export_to, int_to_export_from, order=0)
int_to_import_to = BigInt()
Base_GMP_MPZ_import!(int_to_import_to, bytes_to_export_to, order=0)
@test int_to_export_from == int_to_import_to

# test both composed export(import) is identity
bytes_to_import_from = Vector{UInt8}([1, 0])
int_to_import_to = BigInt()
Base_GMP_MPZ_import!(int_to_import_to, bytes_to_import_from, order=0)
bytes_to_export_to = Vector{UInt8}(undef, 2)
Base.GMP.MPZ.export!(bytes_to_export_to, int_to_export_from, order=0)
@test all(bytes_to_export_to .== bytes_to_import_from)
end

@test isqrt(big(4)) == 2
@test isqrt(big(5)) == 2

Expand Down
Loading