Skip to content

Commit

Permalink
Replace more ccall syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
lgoettgens committed Nov 4, 2024
1 parent 3286337 commit c35cca8
Show file tree
Hide file tree
Showing 32 changed files with 143 additions and 290 deletions.
2 changes: 1 addition & 1 deletion src/HeckeMoreStuff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ end
function Base.setprecision(x::BigFloat, p::Int)
setprecision(BigFloat, p) do
y = BigFloat()
ccall((:mpfr_set, :libmpfr), Nothing, (Ref{BigFloat}, Ref{BigFloat}, Int32), y, x, Base.MPFR.ROUNDING_MODE[])
@ccall libmpfr.mpfr_set(y::Ref{BigFloat}, x::Ref{BigFloat}, Base.MPFR.ROUNDING_MODE[]::Int32)::Nothing
return y
end
end
Expand Down
76 changes: 36 additions & 40 deletions src/Nemo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,14 @@ const NEW_FLINT =
const active_mem = Dict{UInt, Tuple{Symbol, UInt, Any}}()

function trace_malloc(n::UInt)
u = ccall(:jl_malloc, UInt, (UInt, ), n)
u = @ccall jl_malloc(n::UInt)::UInt
global active_mem
active_mem[u] = (:malloc, n, backtrace())
return u
end

function trace_calloc(n::UInt, s::UInt)
u = ccall(:jl_calloc, UInt, (UInt, UInt), n, s)
u = @ccall jl_calloc(n::UInt, s::UInt)::UInt
global active_mem
active_mem[u] = (:calloc, n*s, backtrace())
return u
Expand All @@ -246,12 +246,12 @@ function trace_free(n::UInt)
global active_mem
# @assert haskey(active_mem, n)
delete!(active_mem, n)
ccall(:jl_free, Nothing, (UInt, ), n)
@ccall jl_free(n::UInt)::Nothing
end

function trace_realloc(n::UInt, s::UInt)
global active_mem
p = ccall(:jl_realloc, UInt, (UInt, UInt), n, s)
p = @ccall jl_realloc(n::UInt, s::UInt)::UInt
# @assert haskey(active_mem, n)
delete!(active_mem, n)
active_mem[p] = (:realloc, s, backtrace())
Expand All @@ -260,14 +260,14 @@ end

function trace_counted_malloc(n::UInt)
global active_mem
p = ccall(:jl_gc_counted_malloc, UInt, (UInt, ), n)
p = @ccall jl_gc_counted_malloc(n::UInt)::UInt
active_mem[p] = (:counted_malloc, n, backtrace())
return p
end

function trace_counted_realloc(n::UInt, m::UInt, o::UInt)
global active_mem
p = ccall(:jl_gc_counted_realloc_with_old_size, UInt, (UInt, UInt, UInt), n, m, o)
p = @ccall jl_gc_counted_realloc_with_old_size(n::UInt, m::UInt, o::UInt)::UInt
# @assert n==0 || haskey(active_mem, n)
delete!(active_mem, n)
active_mem[p] = (:counted_realloc, o, backtrace())
Expand All @@ -278,7 +278,7 @@ function trace_counted_free(n::UInt, s::UInt)
global active_mem
# @assert haskey(active_mem, n)
delete!(active_mem, n)
ccall(:jl_gc_counted_free_with_size, Nothing, (UInt, UInt), n, s)
@ccall jl_gc_counted_free_with_size(n::UInt, s::UInt)::Nothing
end

function show_active(l::UInt = UInt(0), frames::Int = 2)
Expand All @@ -297,31 +297,31 @@ function trace_memory(b::Bool)
return
end
if b
ccall((:__gmp_set_memory_functions, :libgmp), Nothing,
(Ptr{Nothing},Ptr{Nothing},Ptr{Nothing}),
@cfunction(trace_counted_malloc, UInt, (UInt, )),
@cfunction(trace_counted_realloc, UInt, (UInt, UInt, UInt)),
@cfunction(trace_counted_free, Nothing, (UInt, UInt)))

ccall((:__flint_set_memory_functions, libflint), Nothing,
(Ptr{Nothing},Ptr{Nothing},Ptr{Nothing},Ptr{Nothing}),
@cfunction(trace_malloc, UInt, (UInt, )),
@cfunction(trace_calloc, UInt, (UInt, UInt)),
@cfunction(trace_realloc, UInt, (UInt, UInt)),
@cfunction(trace_free, Nothing, (UInt, )))
@ccall libgmp.__gmp_set_memory_functions(
@cfunction(trace_counted_malloc, UInt, (UInt, ))::Ptr{Nothing},
@cfunction(trace_counted_realloc, UInt, (UInt, UInt, UInt))::Ptr{Nothing},
@cfunction(trace_counted_free, Nothing, (UInt, UInt))::Ptr{Nothing},
)::Nothing

@ccall libflint.__flint_set_memory_functions(
@cfunction(trace_malloc, UInt, (UInt, ))::Ptr{Nothing},
@cfunction(trace_calloc, UInt, (UInt, UInt))::Ptr{Nothing},
@cfunction(trace_realloc, UInt, (UInt, UInt))::Ptr{Nothing},
@cfunction(trace_free, Nothing, (UInt, ))::Ptr{Nothing},
)::Nothing
else
ccall((:__gmp_set_memory_functions, :libgmp), Nothing,
(Ptr{Nothing},Ptr{Nothing},Ptr{Nothing}),
cglobal(:jl_gc_counted_malloc),
cglobal(:jl_gc_counted_realloc_with_old_size),
cglobal(:jl_gc_counted_free_with_size))

ccall((:__flint_set_memory_functions, libflint), Nothing,
(Ptr{Nothing},Ptr{Nothing},Ptr{Nothing},Ptr{Nothing}),
cglobal(:jl_malloc),
cglobal(:jl_calloc),
cglobal(:jl_realloc),
cglobal(:jl_free))
@ccall libgmp.__gmp_set_memory_functions(
cglobal(:jl_gc_counted_malloc)::Ptr{Nothing},
cglobal(:jl_gc_counted_realloc_with_old_size)::Ptr{Nothing},
cglobal(:jl_gc_counted_free_with_size)::Ptr{Nothing},
)::Nothing

@ccall libflint.__flint_set_memory_functions(
cglobal(:jl_malloc)::Ptr{Nothing},
cglobal(:jl_calloc)::Ptr{Nothing},
cglobal(:jl_realloc)::Ptr{Nothing},
cglobal(:jl_free)::Ptr{Nothing},
)::Nothing
end
end

Expand All @@ -340,12 +340,10 @@ function __init__()
__isthreaded[] = get(ENV, "NEMO_THREADED", "") == "1"

if __isthreaded[]
ccall((:__gmp_set_memory_functions, :libgmp), Nothing,
(Int, Int, Int), 0, 0, 0)
@ccall libgmp.__gmp_set_memory_functions(0::Int, 0::Int, 0::Int)::Nothing
end

ccall((:flint_set_abort, libflint), Nothing,
(Ptr{Nothing},), @cfunction(flint_abort, Nothing, ()))
@ccall libflint.flint_set_abort(@cfunction(flint_abort, Nothing, ())::Ptr{Nothing})::Nothing

if AbstractAlgebra.should_show_banner() && get(ENV, "NEMO_PRINT_BANNER", "true") != "false"
println("")
Expand Down Expand Up @@ -374,7 +372,7 @@ function flint_set_num_threads(a::Int)
end

function flint_cleanup()
ccall((:flint_cleanup, libflint), Nothing, ())
@ccall libflint.flint_cleanup()::Nothing
end

###############################################################################
Expand Down Expand Up @@ -544,17 +542,15 @@ if NEW_FLINT
# gmp_state needs to be initialised
@ccall libflint._flint_rand_init_gmp_state(a::Ref{rand_ctx})::Cvoid
end
ccall((:__gmp_randseed, :libgmp), Cvoid, (Ptr{Cvoid}, Ref{BigInt}), a.gmp_state, seed)
@ccall libgmp.__gmp_randseed(a.gmp_state::Ptr{Cvoid}, seed::Ref{BigInt})::Cvoid
end
else
flint_randseed!(a::rand_ctx, seed1::UInt, seed2::UInt) =
@ccall libflint.flint_randseed(a::Ref{rand_ctx}, seed1::UInt, seed2::UInt)::Cvoid

function flint_gmp_randseed!(a::rand_ctx, seed::BigInt)
@ccall libflint._flint_rand_init_gmp(a::Ref{rand_ctx})::Cvoid
ccall((:__gmp_randseed, :libgmp), Cvoid, (Ref{rand_ctx}, Ref{BigInt}),
a, # gmp_state is the first field of flint_rand_s
seed)
@ccall libgmp.__gmp_randseed(a::Ref{rand_ctx}, seed::Ref{BigInt})::Cvoid # gmp_state is the first field of flint_rand_s
end
end

Expand Down
14 changes: 5 additions & 9 deletions src/arb/ComplexPoly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,8 @@ function Base.divrem(x::ComplexPolyRingElem, y::ComplexPolyRingElem)
iszero(y) && throw(DivideError())
q = parent(x)()
r = parent(x)()
if (ccall((:acb_poly_divrem, libflint), Int,
(Ref{ComplexPolyRingElem}, Ref{ComplexPolyRingElem}, Ref{ComplexPolyRingElem}, Ref{ComplexPolyRingElem}, Int),
q, r, x, y, precision(Balls)) == 1)
success = @ccall libflint.acb_poly_divrem(q::Ref{ComplexPolyRingElem}, r::Ref{ComplexPolyRingElem}, x::Ref{ComplexPolyRingElem}, y::Ref{ComplexPolyRingElem}, precision(Balls)::Int)::Int
if success == 1
return (q, r)
else
throw(DivideError())
Expand Down Expand Up @@ -342,8 +341,7 @@ end
#function reverse(x::ComplexPolyRingElem, len::Int)
# len < 0 && throw(DomainError())
# z = parent(x)()
# ccall((:acb_poly_reverse, libflint), Nothing,
# (Ref{ComplexPolyRingElem}, Ref{ComplexPolyRingElem}, Int), z, x, len)
# @ccall libflint.acb_poly_reverse(z::Ref{ComplexPolyRingElem}, x::Ref{ComplexPolyRingElem}, len::Int)::Nothing
# return z
#end

Expand Down Expand Up @@ -565,10 +563,8 @@ function roots(x::ComplexPolyRingElem; target=0, isolate_real=false, initial_pre
im = _imag_ptr(roots + i * sizeof(acb_struct))
t = _rad_ptr(re)
u = _rad_ptr(im)
ok = ok && (ccall((:mag_cmp_2exp_si, libflint), Cint,
(Ptr{mag_struct}, Int), t, -target) <= 0)
ok = ok && (ccall((:mag_cmp_2exp_si, libflint), Cint,
(Ptr{mag_struct}, Int), u, -target) <= 0)
ok = ok && (@ccall libflint.mag_cmp_2exp_si(t::Ptr{mag_struct}, (-target)::Int)::Cint) <= 0
ok = ok && (@ccall libflint.mag_cmp_2exp_si(u::Ptr{mag_struct}, (-target)::Int)::Cint) <= 0
end
end

Expand Down
8 changes: 3 additions & 5 deletions src/arb/RealPoly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,8 @@ function Base.divrem(x::RealPolyRingElem, y::RealPolyRingElem)
iszero(y) && throw(DivideError())
q = parent(x)()
r = parent(x)()
if (ccall((:arb_poly_divrem, libflint), Int,
(Ref{RealPolyRingElem}, Ref{RealPolyRingElem}, Ref{RealPolyRingElem}, Ref{RealPolyRingElem}, Int),
q, r, x, y, precision(Balls)) == 1)
success = @ccall libflint.arb_poly_divrem(q::Ref{RealPolyRingElem}, r::Ref{RealPolyRingElem}, x::Ref{RealPolyRingElem}, y::Ref{RealPolyRingElem}, precision(Balls)::Int)::Int
if success == 1
return (q, r)
else
throw(DivideError())
Expand Down Expand Up @@ -329,8 +328,7 @@ end
#function reverse(x::RealPolyRingElem, len::Int)
# len < 0 && throw(DomainError())
# z = parent(x)()
# ccall((:arb_poly_reverse, libflint), Nothing,
# (Ref{RealPolyRingElem}, Ref{RealPolyRingElem}, Int), z, x, len)
# @ccall libflint.arb_poly_reverse(z::Ref{RealPolyRingElem}, x::Ref{RealPolyRingElem}, len::Int)::Nothing
# return z
#end

Expand Down
5 changes: 1 addition & 4 deletions src/arb/acb_calc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ function integrate(C::ComplexField, F, a, b;
else
t = BigFloat(abs_tol, RoundDown)
expo = Ref{Clong}()
d = ccall((:mpfr_get_d_2exp, :libmpfr), Float64,
(Ref{Clong}, Ref{BigFloat}, Cint),
expo, t,
Base.convert(Base.MPFR.MPFRRoundingMode, RoundDown))
d = @ccall libmpfr.mpfr_get_d_2exp(expo::Ref{Clong}, t::Ref{BigFloat},Base.convert(Base.MPFR.MPFRRoundingMode, RoundDown)::Cint)::Float64
@ccall libflint.mag_set_d(ctol::Ref{mag_struct}, d::Float64)::Nothing
@ccall libflint.mag_mul_2exp_si(ctol::Ref{mag_struct}, ctol::Ref{mag_struct}, Int(expo[])::Int)::Nothing
end
Expand Down
14 changes: 5 additions & 9 deletions src/arb/acb_poly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,8 @@ function Base.divrem(x::AcbPolyRingElem, y::AcbPolyRingElem)
iszero(y) && throw(DivideError())
q = parent(x)()
r = parent(x)()
if (ccall((:acb_poly_divrem, libflint), Int,
(Ref{AcbPolyRingElem}, Ref{AcbPolyRingElem}, Ref{AcbPolyRingElem}, Ref{AcbPolyRingElem}, Int),
q, r, x, y, precision(parent(x))) == 1)
success = @ccall libflint.acb_poly_divrem(q::Ref{AcbPolyRingElem}, r::Ref{AcbPolyRingElem}, x::Ref{AcbPolyRingElem}, y::Ref{AcbPolyRingElem}, precision(parent(x))::Int)::Int
if success == 1
return (q, r)
else
throw(DivideError())
Expand Down Expand Up @@ -352,8 +351,7 @@ end
#function reverse(x::AcbPolyRingElem, len::Int)
# len < 0 && throw(DomainError())
# z = parent(x)()
# ccall((:acb_poly_reverse, libflint), Nothing,
# (Ref{AcbPolyRingElem}, Ref{AcbPolyRingElem}, Int), z, x, len)
# @ccall libflint.acb_poly_reverse(z::Ref{AcbPolyRingElem}, x::Ref{AcbPolyRingElem}, len::Int)::Nothing
# return z
#end

Expand Down Expand Up @@ -561,10 +559,8 @@ function roots(x::AcbPolyRingElem; target=0, isolate_real=false, initial_prec=0,
im = _imag_ptr(roots + i * sizeof(acb_struct))
t = _rad_ptr(re)
u = _rad_ptr(im)
ok = ok && (ccall((:mag_cmp_2exp_si, libflint), Cint,
(Ptr{mag_struct}, Int), t, -target) <= 0)
ok = ok && (ccall((:mag_cmp_2exp_si, libflint), Cint,
(Ptr{mag_struct}, Int), u, -target) <= 0)
ok = ok && (@ccall libflint.mag_cmp_2exp_si(t::Ptr{mag_struct}, (-target)::Int)::Cint) <= 0
ok = ok && (@ccall libflint.mag_cmp_2exp_si(u::Ptr{mag_struct}, (-target)::Int)::Cint) <= 0
end
end

Expand Down
8 changes: 3 additions & 5 deletions src/arb/arb_poly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,8 @@ function Base.divrem(x::ArbPolyRingElem, y::ArbPolyRingElem)
iszero(y) && throw(DivideError())
q = parent(x)()
r = parent(x)()
if (ccall((:arb_poly_divrem, libflint), Int,
(Ref{ArbPolyRingElem}, Ref{ArbPolyRingElem}, Ref{ArbPolyRingElem}, Ref{ArbPolyRingElem}, Int),
q, r, x, y, precision(parent(x))) == 1)
success = @ccall libflint.arb_poly_divrem(q::Ref{ArbPolyRingElem}, r::Ref{ArbPolyRingElem}, x::Ref{ArbPolyRingElem}, y::Ref{ArbPolyRingElem}, precision(parent(x))::Int)::Int
if success == 1
return (q, r)
else
throw(DivideError())
Expand Down Expand Up @@ -344,8 +343,7 @@ end
#function reverse(x::ArbPolyRingElem, len::Int)
# len < 0 && throw(DomainError())
# z = parent(x)()
# ccall((:arb_poly_reverse, libflint), Nothing,
# (Ref{ArbPolyRingElem}, Ref{ArbPolyRingElem}, Int), z, x, len)
# @ccall libflint.arb_poly_reverse(z::Ref{ArbPolyRingElem}, x::Ref{ArbPolyRingElem}, len::Int)::Nothing
# return z
#end

Expand Down
8 changes: 3 additions & 5 deletions src/calcium/qqbar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ Equivalently, $\operatorname{csgn}(x) = x / \sqrt{x^2}$ except that the value is
at zero. The value is returned as a Julia integer.
"""
function csgn(a::QQBarFieldElem)
return QQBarFieldElem(Int(ccall((:qqbar_csgn, libflint), Cint, (Ref{QQBarFieldElem}, ), a)))
return QQBarFieldElem(Int(@ccall libflint.qqbar_csgn(a::Ref{QQBarFieldElem})::Cint))
end

@doc raw"""
Expand All @@ -776,8 +776,7 @@ end
Return the sign of the real part of `a` as a Julia integer.
"""
function sign_real(a::QQBarFieldElem)
return QQBarFieldElem(Int(ccall((:qqbar_sgn_re, libflint),
Cint, (Ref{QQBarFieldElem}, ), a)))
return QQBarFieldElem(Int(@ccall libflint.qqbar_sgn_re(a::Ref{QQBarFieldElem})::Cint))
end

@doc raw"""
Expand All @@ -786,8 +785,7 @@ end
Return the sign of the imaginary part of `a` as a Julia integer.
"""
function sign_imag(a::QQBarFieldElem)
return QQBarFieldElem(Int(ccall((:qqbar_sgn_im, libflint),
Cint, (Ref{QQBarFieldElem}, ), a)))
return QQBarFieldElem(Int(@ccall libflint.qqbar_sgn_im(a::Ref{QQBarFieldElem})::Cint))
end

function floor(a::QQBarFieldElem)
Expand Down
6 changes: 2 additions & 4 deletions src/flint/FlintTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4168,8 +4168,7 @@ mutable struct zzModMatrix <: MatElem{zzModRingElem}
t = ZZRingElem()
for i = 1:r
for j = 1:c
ccall((:fmpz_mod_ui, libflint), Nothing,
(Ref{ZZRingElem}, Ref{ZZRingElem}, UInt), t, arr[i, j], n)
@ccall libflint.fmpz_mod_ui(t::Ref{ZZRingElem}, arr[i, j]::Ref{ZZRingElem}, n::UInt)::Nothing
setindex_raw!(z, t, i, j)
end
end
Expand Down Expand Up @@ -4543,8 +4542,7 @@ mutable struct fpMatrix <: MatElem{fpFieldElem}
t = ZZRingElem()
for i = 1:r
for j = 1:c
ccall((:fmpz_mod_ui, libflint), Nothing,
(Ref{ZZRingElem}, Ref{ZZRingElem}, UInt), t, arr[i, j], n)
@ccall libflint.fmpz_mod_ui(t::Ref{ZZRingElem}, arr[i, j]::Ref{ZZRingElem}, n::UInt)::Nothing
setindex_raw!(z, t, i, j)
end
end
Expand Down
Loading

0 comments on commit c35cca8

Please sign in to comment.