Skip to content

Commit

Permalink
Unsafe ops for ZZRingElem, QQFieldElem (#1905)
Browse files Browse the repository at this point in the history
Move flintify calls down into unsafe ops

Also reorder method defs to add,sub,mul
  • Loading branch information
fingolfin authored Oct 17, 2024
1 parent 1e48bb0 commit 50132ae
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 74 deletions.
111 changes: 57 additions & 54 deletions src/flint/fmpq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ conj(x::QQFieldElem) = x
for T in (ZZRingElem, Int, UInt)
@eval begin
+(a::QQFieldElem, b::$T) = add!(QQFieldElem(), a, b)
+(a::$T, b::QQFieldElem) = b + a
+(a::$T, b::QQFieldElem) = add!(QQFieldElem(), a, b)
end
end

Expand All @@ -308,7 +308,7 @@ end
for T in (ZZRingElem, Int, UInt)
@eval begin
-(a::QQFieldElem, b::$T) = sub!(QQFieldElem(), a, b)
-(a::$T, b::QQFieldElem) = neg!(b - a)
-(a::$T, b::QQFieldElem) = sub!(QQFieldElem(), a, b)
end
end

Expand All @@ -318,19 +318,13 @@ end
-(a::QQFieldElem, b::Rational{T}) where {T <: Integer} = a - QQFieldElem(b)
-(a::Rational{T}, b::QQFieldElem) where {T <: Integer} = QQFieldElem(a) - b

for T in (ZZRingElem, Int, UInt)
for T in (ZZRingElem, Int, UInt, Integer, Rational)
@eval begin
*(a::QQFieldElem, b::$T) = mul!(QQFieldElem(), a, b)
*(a::$T, b::QQFieldElem) = b * a
*(a::$T, b::QQFieldElem) = mul!(QQFieldElem(), a, b)
end
end

*(a::QQFieldElem, b::Integer) = a * flintify(b)
*(a::Integer, b::QQFieldElem) = b * a

*(a::QQFieldElem, b::Rational{T}) where {T <: Integer} = a * QQFieldElem(b)
*(a::Rational{T}, b::QQFieldElem) where {T <: Integer} = b * a

*(a::QQFieldElem, b::AbstractFloat) = Rational(a) * b
*(a::AbstractFloat, b::QQFieldElem) = a * Rational(b)

Expand Down Expand Up @@ -1071,20 +1065,14 @@ function set!(c::QQFieldElemOrPtr, a::ZZRingElemOrPtr, b::ZZRingElemOrPtr)
return c
end

function set!(c::QQFieldElemOrPtr, a::ZZRingElemOrPtr)
function set!(c::QQFieldElemOrPtr, a::Union{Integer,ZZRingElemOrPtr})
GC.@preserve c begin
set!(_num_ptr(c), a)
one!(_den_ptr(c))
end
return c
end

function set!(c::Ptr{QQFieldElem}, a::ZZRingElemOrPtr)
set!(_num_ptr(c), a)
one!(_den_ptr(c))
return c
end

function numerator!(z::ZZRingElem, y::QQFieldElem)
ccall((:fmpq_numerator, libflint), Cvoid, (Ref{ZZRingElem}, Ref{QQFieldElem}), z, y)
return z
Expand All @@ -1095,37 +1083,7 @@ function denominator!(z::ZZRingElem, y::QQFieldElem)
return z
end

function mul!(c::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::QQFieldElemOrPtr)
ccall((:fmpq_mul, libflint), Nothing,
(Ref{QQFieldElem}, Ref{QQFieldElem}, Ref{QQFieldElem}), c, a, b)
return c
end

function mul!(c::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::ZZRingElemOrPtr)
ccall((:fmpq_mul_fmpz, libflint), Nothing,
(Ref{QQFieldElem}, Ref{QQFieldElem}, Ref{ZZRingElem}), c, a, b)
return c
end

function mul!(c::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::Int)
ccall((:fmpq_mul_si, libflint), Nothing,
(Ref{QQFieldElem}, Ref{QQFieldElem}, Int), c, a, b)
return c
end

function mul!(c::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::UInt)
ccall((:fmpq_mul_ui, libflint), Nothing,
(Ref{QQFieldElem}, Ref{QQFieldElem}, Int), c, a, b)
return c
end

mul!(c::QQFieldElemOrPtr, a::Union{ZZRingElemOrPtr, Int, UInt}, b::QQFieldElemOrPtr) = mul!(c, b, a)

function addmul!(c::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::QQFieldElemOrPtr)
ccall((:fmpq_addmul, libflint), Nothing,
(Ref{QQFieldElem}, Ref{QQFieldElem}, Ref{QQFieldElem}), c, a, b)
return c
end
#

function add!(c::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::QQFieldElemOrPtr)
ccall((:fmpq_add, libflint), Nothing,
Expand All @@ -1151,13 +1109,10 @@ function add!(c::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::UInt)
return c
end

add!(c::QQFieldElemOrPtr, a::Union{ZZRingElemOrPtr, Int, UInt}, b::QQFieldElemOrPtr) = add!(c, b, a)
add!(c::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::Union{Integer, Rational}) = add!(c, a, flintify(b))
add!(c::QQFieldElemOrPtr, a::Union{ZZRingElemOrPtr, Integer, Rational}, b::QQFieldElemOrPtr) = add!(c, b, a)

function submul!(c::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::QQFieldElemOrPtr)
ccall((:fmpq_submul, libflint), Nothing,
(Ref{QQFieldElem}, Ref{QQFieldElem}, Ref{QQFieldElem}), c, a, b)
return c
end
#

function sub!(z::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::QQFieldElemOrPtr)
ccall((:fmpq_sub, libflint), Nothing, (Ref{QQFieldElem}, Ref{QQFieldElem}, Ref{QQFieldElem}), z, a, b)
Expand All @@ -1182,6 +1137,54 @@ function sub!(z::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::UInt)
return z
end

sub!(c::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::Union{Integer, Rational}) = sub!(c, a, flintify(b))
sub!(c::QQFieldElemOrPtr, a::Union{ZZRingElemOrPtr, Integer, Rational}, b::QQFieldElemOrPtr) = neg!(sub!(c, b, a))

#

function mul!(c::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::QQFieldElemOrPtr)
ccall((:fmpq_mul, libflint), Nothing,
(Ref{QQFieldElem}, Ref{QQFieldElem}, Ref{QQFieldElem}), c, a, b)
return c
end

function mul!(c::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::ZZRingElemOrPtr)
ccall((:fmpq_mul_fmpz, libflint), Nothing,
(Ref{QQFieldElem}, Ref{QQFieldElem}, Ref{ZZRingElem}), c, a, b)
return c
end

function mul!(c::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::Int)
ccall((:fmpq_mul_si, libflint), Nothing,
(Ref{QQFieldElem}, Ref{QQFieldElem}, Int), c, a, b)
return c
end

function mul!(c::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::UInt)
ccall((:fmpq_mul_ui, libflint), Nothing,
(Ref{QQFieldElem}, Ref{QQFieldElem}, UInt), c, a, b)
return c
end

mul!(c::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::Union{Integer, Rational}) = mul!(c, a, flintify(b))
mul!(c::QQFieldElemOrPtr, a::Union{ZZRingElemOrPtr, Integer, Rational}, b::QQFieldElemOrPtr) = mul!(c, b, a)

#

function addmul!(c::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::QQFieldElemOrPtr)
ccall((:fmpq_addmul, libflint), Nothing,
(Ref{QQFieldElem}, Ref{QQFieldElem}, Ref{QQFieldElem}), c, a, b)
return c
end

function submul!(c::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::QQFieldElemOrPtr)
ccall((:fmpq_submul, libflint), Nothing,
(Ref{QQFieldElem}, Ref{QQFieldElem}, Ref{QQFieldElem}), c, a, b)
return c
end

#

function divexact!(z::QQFieldElemOrPtr, a::QQFieldElemOrPtr, b::QQFieldElemOrPtr)
ccall((:fmpq_div, libflint), Nothing, (Ref{QQFieldElem}, Ref{QQFieldElem}, Ref{QQFieldElem}), z, a, b)
return z
Expand Down
51 changes: 31 additions & 20 deletions src/flint/fmpz.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2544,12 +2544,16 @@ function set!(z::ZZRingElemOrPtr, a::UInt)
return z
end

set!(z::ZZRingElemOrPtr, a::Integer) = set!(z, flintify(a))

function swap!(a::ZZRingElemOrPtr, b::ZZRingElemOrPtr)
ccall((:fmpz_swap, libflint), Nothing,
(Ref{ZZRingElem}, Ref{ZZRingElem}),
a, b)
end

#

function add!(z::ZZRingElemOrPtr, x::ZZRingElemOrPtr, y::ZZRingElemOrPtr)
ccall((:fmpz_add, libflint), Nothing,
(Ref{ZZRingElem}, Ref{ZZRingElem}, Ref{ZZRingElem}), z, x, y)
Expand All @@ -2574,7 +2578,9 @@ function add!(a::ZZRingElemOrPtr, b::ZZRingElemOrPtr, c::Ptr{Int})
end

add!(z::ZZRingElemOrPtr, a::ZZRingElemOrPtr, b::Integer) = add!(z, a, flintify(b))
add!(z::ZZRingElemOrPtr, x::Int, y::ZZRingElemOrPtr) = add!(z, y, x)
add!(z::ZZRingElemOrPtr, x::Integer, y::ZZRingElemOrPtr) = add!(z, y, x)

#

function sub!(z::ZZRingElemOrPtr, a::ZZRingElemOrPtr, b::ZZRingElemOrPtr)
ccall((:fmpz_sub, libflint), Nothing,
Expand All @@ -2597,15 +2603,10 @@ function sub!(z::ZZRingElemOrPtr, a::ZZRingElemOrPtr, b::UInt)
return z
end

function sub!(z::ZZRingElemOrPtr, a::ZZRingElemOrPtr, b::Integer)
return sub!(z, a, flintify(b))
end

function sub!(z::ZZRingElemOrPtr, b::Integer, a::ZZRingElemOrPtr)
sub!(z, a, b)
return neg!(z, z)
end
sub!(z::ZZRingElemOrPtr, a::ZZRingElemOrPtr, b::Integer) = sub!(z, a, flintify(b))
sub!(z::ZZRingElemOrPtr, a::Integer, b::ZZRingElemOrPtr) = neg!(sub!(z, b, a))

#

function mul!(z::ZZRingElemOrPtr, x::ZZRingElemOrPtr, y::ZZRingElemOrPtr)
ccall((:fmpz_mul, libflint), Nothing,
Expand All @@ -2626,14 +2627,15 @@ function mul!(z::ZZRingElemOrPtr, x::ZZRingElemOrPtr, y::UInt)
end

mul!(z::ZZRingElemOrPtr, a::ZZRingElemOrPtr, b::Integer) = mul!(z, a, flintify(b))

mul!(z::ZZRingElemOrPtr, x::Integer, y::ZZRingElemOrPtr) = mul!(z, y, x)

function mul!(a::ZZRingElemOrPtr, b::ZZRingElemOrPtr, c::Ptr{Int})
ccall((:fmpz_mul, libflint), Nothing, (Ref{ZZRingElem}, Ref{ZZRingElem}, Ptr{Int}), a, b, c)
return a
end

#

function addmul!(z::ZZRingElemOrPtr, x::ZZRingElemOrPtr, y::ZZRingElemOrPtr)
ccall((:fmpz_addmul, libflint), Nothing,
(Ref{ZZRingElem}, Ref{ZZRingElem}, Ref{ZZRingElem}), z, x, y)
Expand All @@ -2652,6 +2654,9 @@ function addmul!(z::ZZRingElemOrPtr, x::ZZRingElemOrPtr, y::UInt)
return z
end

addmul!(z::ZZRingElemOrPtr, x::ZZRingElemOrPtr, y::Integer) = addmul!(z, x, flintify(y))
addmul!(z::ZZRingElemOrPtr, x::Integer, y::ZZRingElemOrPtr) = addmul!(z, y, x)

# ignore fourth argument
addmul!(z::ZZRingElemOrPtr, x::ZZRingElemOrPtr, y::Union{ZZRingElemOrPtr,Integer}, ::ZZRingElemOrPtr) = addmul!(z, x, y)

Expand All @@ -2673,6 +2678,8 @@ function submul!(z::ZZRingElemOrPtr, x::ZZRingElemOrPtr, y::UInt)
return z
end

submul!(z::ZZRingElemOrPtr, x::ZZRingElemOrPtr, y::Integer) = submul!(z, x, flintify(y))

# ignore fourth argument
submul!(z::ZZRingElemOrPtr, x::ZZRingElemOrPtr, y::Union{ZZRingElemOrPtr,Integer}, ::ZZRingElemOrPtr) = submul!(z, x, y)

Expand All @@ -2698,24 +2705,28 @@ function fmms!(r::ZZRingElemOrPtr, a::ZZRingElemOrPtr, b::ZZRingElemOrPtr, c::ZZ
return r
end

#

function divexact!(z::ZZRingElemOrPtr, a::ZZRingElemOrPtr, b::UInt)
ccall((:fmpz_divexact_ui, libflint), Nothing,
(Ref{ZZRingElem}, Ref{ZZRingElem}, UInt),
z, a, b)
function divexact!(z::ZZRingElemOrPtr, a::ZZRingElemOrPtr, b::ZZRingElemOrPtr)
@ccall libflint.fmpz_divexact(z::Ref{ZZRingElem}, a::Ref{ZZRingElem}, b::Ref{ZZRingElem})::Nothing
return z
end

function divexact!(z::ZZRingElemOrPtr, a::ZZRingElemOrPtr, b::ZZRingElemOrPtr)
ccall((:fmpz_divexact, libflint), Nothing,
(Ref{ZZRingElem}, Ref{ZZRingElem}, Ref{ZZRingElem}),
z, a, b)
function divexact!(z::ZZRingElemOrPtr, a::ZZRingElemOrPtr, b::Int)
@ccall libflint.fmpz_divexact_si(z::Ref{ZZRingElem}, a::Ref{ZZRingElem}, b::Int)::Nothing
return z
end

divexact!(z::ZZRingElemOrPtr, b::ZZRingElemOrPtr) = divexact!(z, z, b)
function divexact!(z::ZZRingElemOrPtr, a::ZZRingElemOrPtr, b::UInt)
@ccall libflint.fmpz_divexact_ui(z::Ref{ZZRingElem}, a::Ref{ZZRingElem}, b::UInt)::Nothing
return z
end

divexact!(z::ZZRingElemOrPtr, a::ZZRingElemOrPtr, b::Integer) = divexact!(z, a, flintify(b))

#

function pow!(z::ZZRingElemOrPtr, a::ZZRingElemOrPtr, b::Union{Int, UInt})
function pow!(z::ZZRingElemOrPtr, a::ZZRingElemOrPtr, b::Integer)
ccall((:fmpz_pow_ui, libflint), Nothing,
(Ref{ZZRingElem}, Ref{ZZRingElem}, UInt),
z, a, UInt(b))
Expand Down

0 comments on commit 50132ae

Please sign in to comment.