Skip to content

Commit

Permalink
Wrap Arb pointer functions into julia functions (#1921)
Browse files Browse the repository at this point in the history
  • Loading branch information
lgoettgens authored Oct 25, 2024
1 parent 06ec014 commit eea3f94
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 94 deletions.
11 changes: 11 additions & 0 deletions src/arb/ArbTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ mutable struct acb_struct
imag_rad_man::UInt
end

const arf_structOrPtr = Union{arf_struct, Ref{arf_struct}, Ptr{arf_struct}}
const mag_structOrPtr = Union{mag_struct, Ref{mag_struct}, Ptr{mag_struct}}
const arb_structOrPtr = Union{arb_struct, Ref{arb_struct}, Ptr{arb_struct}}
const acb_structOrPtr = Union{acb_struct, Ref{acb_struct}, Ptr{acb_struct}}

_mid_ptr(x::arb_structOrPtr) = @ccall libflint.arb_mid_ptr(x::Ref{arb_struct})::Ptr{arf_struct}
_rad_ptr(x::arb_structOrPtr) = @ccall libflint.arb_rad_ptr(x::Ref{arb_struct})::Ptr{mag_struct}

_real_ptr(x::acb_structOrPtr) = @ccall libflint.acb_real_ptr(x::Ref{acb_struct})::Ptr{arb_struct}
_imag_ptr(x::acb_structOrPtr) = @ccall libflint.acb_imag_ptr(x::Ref{acb_struct})::Ptr{arb_struct}

################################################################################
#
# Types and memory management for ArbField
Expand Down
43 changes: 23 additions & 20 deletions src/arb/Complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ characteristic(::ComplexField) = 0

function convert(::Type{ComplexF64}, x::ComplexFieldElem)
GC.@preserve x begin
re = ccall((:acb_real_ptr, libflint), Ptr{arb_struct}, (Ref{ComplexFieldElem}, ), x)
im = ccall((:acb_imag_ptr, libflint), Ptr{arb_struct}, (Ref{ComplexFieldElem}, ), x)
t = ccall((:arb_mid_ptr, libflint), Ptr{arf_struct}, (Ptr{RealFieldElem}, ), re)
u = ccall((:arb_mid_ptr, libflint), Ptr{arf_struct}, (Ptr{RealFieldElem}, ), im)
re = _real_ptr(x)
im = _imag_ptr(x)
t = _mid_ptr(re)
u = _mid_ptr(im)
# 4 == round to nearest
v = ccall((:arf_get_d, libflint), Float64, (Ptr{arf_struct}, Int), t, 4)
w = ccall((:arf_get_d, libflint), Float64, (Ptr{arf_struct}, Int), u, 4)
Expand Down Expand Up @@ -1616,6 +1616,9 @@ end
#
################################################################################

_real_ptr(x::ComplexFieldElemOrPtr) = @ccall libflint.acb_real_ptr(x::Ref{ComplexFieldElem})::Ptr{RealFieldElem}
_imag_ptr(x::ComplexFieldElemOrPtr) = @ccall libflint.acb_imag_ptr(x::Ref{ComplexFieldElem})::Ptr{RealFieldElem}

for (typeofx, passtoc) in ((ComplexFieldElem, Ref{ComplexFieldElem}), (Ptr{ComplexFieldElem}, Ptr{ComplexFieldElem}))
for (f,t) in (("acb_set_si", Int), ("acb_set_ui", UInt),
("acb_set_d", Float64))
Expand Down Expand Up @@ -1675,23 +1678,23 @@ for (typeofx, passtoc) in ((ComplexFieldElem, Ref{ComplexFieldElem}), (Ptr{Compl
end

function _acb_set(x::($typeofx), y::AbstractString, p::Int)
r = ccall((:acb_real_ptr, libflint), Ptr{RealFieldElem}, (($passtoc), ), x)
r = _real_ptr(x)
_arb_set(r, y, p)
i = ccall((:acb_imag_ptr, libflint), Ptr{RealFieldElem}, (($passtoc), ), x)
i = _imag_ptr(x)
zero!(i)
end

function _acb_set(x::($typeofx), y::BigFloat)
r = ccall((:acb_real_ptr, libflint), Ptr{RealFieldElem}, (($passtoc), ), x)
r = _real_ptr(x)
_arb_set(r, y)
i = ccall((:acb_imag_ptr, libflint), Ptr{RealFieldElem}, (($passtoc), ), x)
i = _imag_ptr(x)
zero!(i)
end

function _acb_set(x::($typeofx), y::BigFloat, p::Int)
r = ccall((:acb_real_ptr, libflint), Ptr{RealFieldElem}, (($passtoc), ), x)
r = _real_ptr(x)
_arb_set(r, y, p)
i = ccall((:acb_imag_ptr, libflint), Ptr{RealFieldElem}, (($passtoc), ), x)
i = _imag_ptr(x)
zero!(i)
end

Expand All @@ -1714,30 +1717,30 @@ for (typeofx, passtoc) in ((ComplexFieldElem, Ref{ComplexFieldElem}), (Ptr{Compl
end

function _acb_set(x::($typeofx), y::QQFieldElem, z::QQFieldElem, p::Int)
r = ccall((:acb_real_ptr, libflint), Ptr{RealFieldElem}, (($passtoc), ), x)
r = _real_ptr(x)
_arb_set(r, y, p)
i = ccall((:acb_imag_ptr, libflint), Ptr{RealFieldElem}, (($passtoc), ), x)
i = _imag_ptr(x)
_arb_set(i, z, p)
end

function _acb_set(x::($typeofx), y::T, z::T, p::Int) where {T <: AbstractString}
r = ccall((:acb_real_ptr, libflint), Ptr{RealFieldElem}, (($passtoc), ), x)
r = _real_ptr(x)
_arb_set(r, y, p)
i = ccall((:acb_imag_ptr, libflint), Ptr{RealFieldElem}, (($passtoc), ), x)
i = _imag_ptr(x)
_arb_set(i, z, p)
end

function _acb_set(x::($typeofx), y::Real, p::Int)
r = ccall((:acb_real_ptr, libflint), Ptr{RealFieldElem}, (($passtoc), ), x)
r = _real_ptr(x)
_arb_set(r, y, p)
i = ccall((:acb_imag_ptr, libflint), Ptr{RealFieldElem}, (($passtoc), ), x)
i = _imag_ptr(x)
zero!(i)
end

function _acb_set(x::($typeofx), y::Complex, p::Int)
r = ccall((:acb_real_ptr, libflint), Ptr{RealFieldElem}, (($passtoc), ), x)
r = _real_ptr(x)
_arb_set(r, real(y), p)
i = ccall((:acb_imag_ptr, libflint), Ptr{RealFieldElem}, (($passtoc), ), x)
i = _imag_ptr(x)
_arb_set(i, imag(y), p)
end

Expand All @@ -1746,9 +1749,9 @@ for (typeofx, passtoc) in ((ComplexFieldElem, Ref{ComplexFieldElem}), (Ptr{Compl
for T in (Real, ZZRingElem)
@eval begin
function _acb_set(x::($typeofx), y::($T), z::($T), p::Int)
r = ccall((:acb_real_ptr, libflint), Ptr{RealFieldElem}, (($passtoc), ), x)
r = _real_ptr(x)
_arb_set(r, y, p)
i = ccall((:acb_imag_ptr, libflint), Ptr{RealFieldElem}, (($passtoc), ), x)
i = _imag_ptr(x)
_arb_set(i, z, p)
end
end
Expand Down
4 changes: 2 additions & 2 deletions src/arb/ComplexMat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -686,10 +686,10 @@ bound for the infinity norm for every matrix in $x$
function bound_inf_norm(x::ComplexMatrix)
z = RealFieldElem()
GC.@preserve x z begin
t = ccall((:arb_rad_ptr, libflint), Ptr{mag_struct}, (Ref{RealFieldElem}, ), z)
t = _rad_ptr(z)
ccall((:acb_mat_bound_inf_norm, libflint), Nothing,
(Ptr{mag_struct}, Ref{ComplexMatrix}), t, x)
s = ccall((:arb_mid_ptr, libflint), Ptr{arf_struct}, (Ref{RealFieldElem}, ), z)
s = _mid_ptr(z)
ccall((:arf_set_mag, libflint), Nothing,
(Ptr{arf_struct}, Ptr{mag_struct}), s, t)
ccall((:mag_zero, libflint), Nothing,
Expand Down
21 changes: 9 additions & 12 deletions src/arb/ComplexPoly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -597,12 +597,10 @@ function roots(x::ComplexPolyRingElem; target=0, isolate_real=false, initial_pre
ok = true
if target > 0
for i = 0 : deg-1
re = ccall((:acb_real_ptr, libflint), Ptr{arb_struct},
(Ptr{acb_struct}, ), roots + i * sizeof(acb_struct))
im = ccall((:acb_imag_ptr, libflint), Ptr{arb_struct},
(Ptr{acb_struct}, ), roots + i * sizeof(acb_struct))
t = ccall((:arb_rad_ptr, libflint), Ptr{mag_struct}, (Ptr{ArbFieldElem}, ), re)
u = ccall((:arb_rad_ptr, libflint), Ptr{mag_struct}, (Ptr{ArbFieldElem}, ), im)
re = _real_ptr(roots + i * sizeof(acb_struct))
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,
Expand All @@ -620,10 +618,9 @@ function roots(x::ComplexPolyRingElem; target=0, isolate_real=false, initial_pre

if real_ok
for i = 0 : deg - 1
im = ccall((:acb_imag_ptr, libflint), Ptr{RealFieldElem},
(Ptr{ComplexFieldElem}, ), roots + i * sizeof(acb_struct))
if ccall((:arb_contains_zero, libflint), Bool, (Ptr{RealFieldElem}, ), im)
zero!(im)
im = _imag_ptr(roots + i * sizeof(acb_struct))
if ccall((:arb_contains_zero, libflint), Bool, (Ptr{arb_struct}, ), im)
ccall((:arb_zero, libflint), Nothing, (Ptr{arb_struct}, ), im)
end
end
end
Expand Down Expand Up @@ -669,10 +666,10 @@ function roots_upper_bound(x::ComplexPolyRingElem)
z = RealFieldElem()
p = precision(Balls)
GC.@preserve x z begin
t = ccall((:arb_rad_ptr, libflint), Ptr{mag_struct}, (Ref{RealFieldElem}, ), z)
t = _rad_ptr(z)
ccall((:acb_poly_root_bound_fujiwara, libflint), Nothing,
(Ptr{mag_struct}, Ref{ComplexPolyRingElem}), t, x)
s = ccall((:arb_mid_ptr, libflint), Ptr{arf_struct}, (Ref{RealFieldElem}, ), z)
s = _mid_ptr(z)
ccall((:arf_set_mag, libflint), Nothing, (Ptr{arf_struct}, Ptr{mag_struct}), s, t)
ccall((:arf_set_round, libflint), Nothing,
(Ptr{arf_struct}, Ptr{arf_struct}, Int, Cint), s, s, p, ARB_RND_CEIL)
Expand Down
17 changes: 8 additions & 9 deletions src/arb/Real.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ end

characteristic(::RealField) = 0

_mid_ptr(x::RealFieldElemOrPtr) = @ccall libflint.arb_mid_ptr(x::Ref{RealFieldElem})::Ptr{arf_struct}
_rad_ptr(x::RealFieldElemOrPtr) = @ccall libflint.arb_rad_ptr(x::Ref{RealFieldElem})::Ptr{mag_struct}

################################################################################
#
# Conversions
Expand Down Expand Up @@ -90,9 +93,7 @@ end
function _arb_get_arf(x::RealFieldElem, ::RoundingMode{:Nearest})
t = arf_struct()
GC.@preserve x begin
t1 = ccall((:arb_mid_ptr, libflint), Ptr{arf_struct},
(Ref{RealFieldElem}, ),
x)
t1 = _mid_ptr(x)
ccall((:arf_set, libflint), Nothing,
(Ref{arf_struct}, Ptr{arf_struct}),
t, t1)
Expand Down Expand Up @@ -2013,18 +2014,16 @@ for (typeofx, passtoc) in ((RealFieldElem, Ref{RealFieldElem}), (Ptr{RealFieldEl
end

function _arb_set(x::($typeofx), y::BigFloat)
m = ccall((:arb_mid_ptr, libflint), Ptr{arf_struct},
(($passtoc), ), x)
r = ccall((:arb_rad_ptr, libflint), Ptr{mag_struct},
(($passtoc), ), x)
m = _mid_ptr(x)
r = _rad_ptr(x)
ccall((:arf_set_mpfr, libflint), Nothing,
(Ptr{arf_struct}, Ref{BigFloat}), m, y)
ccall((:mag_zero, libflint), Nothing, (Ptr{mag_struct}, ), r)
end

function _arb_set(x::($typeofx), y::BigFloat, p::Int)
m = ccall((:arb_mid_ptr, libflint), Ptr{arf_struct}, (($passtoc), ), x)
r = ccall((:arb_rad_ptr, libflint), Ptr{mag_struct}, (($passtoc), ), x)
m = _mid_ptr(x)
r = _rad_ptr(x)
ccall((:arf_set_mpfr, libflint), Nothing,
(Ptr{arf_struct}, Ref{BigFloat}), m, y)
ccall((:mag_zero, libflint), Nothing, (Ptr{mag_struct}, ), r)
Expand Down
4 changes: 2 additions & 2 deletions src/arb/RealMat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -628,10 +628,10 @@ bound for the infinity norm for every matrix in $x$
function bound_inf_norm(x::RealMatrix)
z = RealFieldElem()
GC.@preserve x z begin
t = ccall((:arb_rad_ptr, libflint), Ptr{mag_struct}, (Ref{RealFieldElem}, ), z)
t = _rad_ptr(z)
ccall((:arb_mat_bound_inf_norm, libflint), Nothing,
(Ptr{mag_struct}, Ref{RealMatrix}), t, x)
s = ccall((:arb_mid_ptr, libflint), Ptr{arf_struct}, (Ref{RealFieldElem}, ), z)
s = _mid_ptr(z)
ccall((:arf_set_mag, libflint), Nothing,
(Ptr{arf_struct}, Ptr{mag_struct}), s, t)
ccall((:mag_zero, libflint), Nothing,
Expand Down
4 changes: 2 additions & 2 deletions src/arb/RealPoly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -569,10 +569,10 @@ function roots_upper_bound(x::RealPolyRingElem)
z = base_ring(x)()
p = precision(Balls)
GC.@preserve x z begin
t = ccall((:arb_rad_ptr, libflint), Ptr{mag_struct}, (Ref{RealFieldElem}, ), z)
t = _rad_ptr(z)
ccall((:arb_poly_root_bound_fujiwara, libflint), Nothing,
(Ptr{mag_struct}, Ref{RealPolyRingElem}), t, x)
s = ccall((:arb_mid_ptr, libflint), Ptr{arf_struct}, (Ref{RealFieldElem}, ), z)
s = _mid_ptr(z)
ccall((:arf_set_mag, libflint), Nothing, (Ptr{arf_struct}, Ptr{mag_struct}), s, t)
ccall((:arf_set_round, libflint), Nothing,
(Ptr{arf_struct}, Ptr{arf_struct}, Int, Cint), s, s, p, ARB_RND_CEIL)
Expand Down
43 changes: 23 additions & 20 deletions src/arb/acb.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ characteristic(::AcbField) = 0

function convert(::Type{ComplexF64}, x::AcbFieldElem)
GC.@preserve x begin
re = ccall((:acb_real_ptr, libflint), Ptr{arb_struct}, (Ref{AcbFieldElem}, ), x)
im = ccall((:acb_imag_ptr, libflint), Ptr{arb_struct}, (Ref{AcbFieldElem}, ), x)
t = ccall((:arb_mid_ptr, libflint), Ptr{arf_struct}, (Ptr{ArbFieldElem}, ), re)
u = ccall((:arb_mid_ptr, libflint), Ptr{arf_struct}, (Ptr{ArbFieldElem}, ), im)
re = _real_ptr(x)
im = _imag_ptr(x)
t = _mid_ptr(re)
u = _mid_ptr(im)
# 4 == round to nearest
v = ccall((:arf_get_d, libflint), Float64, (Ptr{arf_struct}, Int), t, 4)
w = ccall((:arf_get_d, libflint), Float64, (Ptr{arf_struct}, Int), u, 4)
Expand Down Expand Up @@ -1611,6 +1611,9 @@ end
#
################################################################################

_real_ptr(x::AcbFieldElemOrPtr) = @ccall libflint.acb_real_ptr(x::Ref{AcbFieldElem})::Ptr{ArbFieldElem}
_imag_ptr(x::AcbFieldElemOrPtr) = @ccall libflint.acb_imag_ptr(x::Ref{AcbFieldElem})::Ptr{ArbFieldElem}

for (typeofx, passtoc) in ((AcbFieldElem, Ref{AcbFieldElem}), (Ptr{AcbFieldElem}, Ptr{AcbFieldElem}))
for (f,t) in (("acb_set_si", Int), ("acb_set_ui", UInt),
("acb_set_d", Float64))
Expand Down Expand Up @@ -1670,23 +1673,23 @@ for (typeofx, passtoc) in ((AcbFieldElem, Ref{AcbFieldElem}), (Ptr{AcbFieldElem}
end

function _acb_set(x::($typeofx), y::AbstractString, p::Int)
r = ccall((:acb_real_ptr, libflint), Ptr{ArbFieldElem}, (($passtoc), ), x)
r = _real_ptr(x)
_arb_set(r, y, p)
i = ccall((:acb_imag_ptr, libflint), Ptr{ArbFieldElem}, (($passtoc), ), x)
i = _imag_ptr(x)
zero!(i)
end

function _acb_set(x::($typeofx), y::BigFloat)
r = ccall((:acb_real_ptr, libflint), Ptr{ArbFieldElem}, (($passtoc), ), x)
r = _real_ptr(x)
_arb_set(r, y)
i = ccall((:acb_imag_ptr, libflint), Ptr{ArbFieldElem}, (($passtoc), ), x)
i = _imag_ptr(x)
zero!(i)
end

function _acb_set(x::($typeofx), y::BigFloat, p::Int)
r = ccall((:acb_real_ptr, libflint), Ptr{ArbFieldElem}, (($passtoc), ), x)
r = _real_ptr(x)
_arb_set(r, y, p)
i = ccall((:acb_imag_ptr, libflint), Ptr{ArbFieldElem}, (($passtoc), ), x)
i = _imag_ptr(x)
zero!(i)
end

Expand All @@ -1709,30 +1712,30 @@ for (typeofx, passtoc) in ((AcbFieldElem, Ref{AcbFieldElem}), (Ptr{AcbFieldElem}
end

function _acb_set(x::($typeofx), y::QQFieldElem, z::QQFieldElem, p::Int)
r = ccall((:acb_real_ptr, libflint), Ptr{ArbFieldElem}, (($passtoc), ), x)
r = _real_ptr(x)
_arb_set(r, y, p)
i = ccall((:acb_imag_ptr, libflint), Ptr{ArbFieldElem}, (($passtoc), ), x)
i = _imag_ptr(x)
_arb_set(i, z, p)
end

function _acb_set(x::($typeofx), y::T, z::T, p::Int) where {T <: AbstractString}
r = ccall((:acb_real_ptr, libflint), Ptr{ArbFieldElem}, (($passtoc), ), x)
r = _real_ptr(x)
_arb_set(r, y, p)
i = ccall((:acb_imag_ptr, libflint), Ptr{ArbFieldElem}, (($passtoc), ), x)
i = _imag_ptr(x)
_arb_set(i, z, p)
end

function _acb_set(x::($typeofx), y::Real, p::Int)
r = ccall((:acb_real_ptr, libflint), Ptr{ArbFieldElem}, (($passtoc), ), x)
r = _real_ptr(x)
_arb_set(r, y, p)
i = ccall((:acb_imag_ptr, libflint), Ptr{ArbFieldElem}, (($passtoc), ), x)
i = _imag_ptr(x)
zero!(i)
end

function _acb_set(x::($typeofx), y::Complex, p::Int)
r = ccall((:acb_real_ptr, libflint), Ptr{ArbFieldElem}, (($passtoc), ), x)
r = _real_ptr(x)
_arb_set(r, real(y), p)
i = ccall((:acb_imag_ptr, libflint), Ptr{ArbFieldElem}, (($passtoc), ), x)
i = _imag_ptr(x)
_arb_set(i, imag(y), p)
end

Expand All @@ -1741,9 +1744,9 @@ for (typeofx, passtoc) in ((AcbFieldElem, Ref{AcbFieldElem}), (Ptr{AcbFieldElem}
for T in (Real, ZZRingElem)
@eval begin
function _acb_set(x::($typeofx), y::($T), z::($T), p::Int)
r = ccall((:acb_real_ptr, libflint), Ptr{ArbFieldElem}, (($passtoc), ), x)
r = _real_ptr(x)
_arb_set(r, y, p)
i = ccall((:acb_imag_ptr, libflint), Ptr{ArbFieldElem}, (($passtoc), ), x)
i = _imag_ptr(x)
_arb_set(i, z, p)
end
end
Expand Down
4 changes: 2 additions & 2 deletions src/arb/acb_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -688,10 +688,10 @@ bound for the infinity norm for every matrix in $x$
function bound_inf_norm(x::AcbMatrix)
z = ArbFieldElem()
GC.@preserve x z begin
t = ccall((:arb_rad_ptr, libflint), Ptr{mag_struct}, (Ref{ArbFieldElem}, ), z)
t = _rad_ptr(z)
ccall((:acb_mat_bound_inf_norm, libflint), Nothing,
(Ptr{mag_struct}, Ref{AcbMatrix}), t, x)
s = ccall((:arb_mid_ptr, libflint), Ptr{arf_struct}, (Ref{ArbFieldElem}, ), z)
s = _mid_ptr(z)
ccall((:arf_set_mag, libflint), Nothing,
(Ptr{arf_struct}, Ptr{mag_struct}), s, t)
ccall((:mag_zero, libflint), Nothing,
Expand Down
Loading

0 comments on commit eea3f94

Please sign in to comment.