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

Unsafe ops for ZZRingElem, QQFieldElem #1905

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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 @@
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 @@
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 @@
-(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 @@
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 @@
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 @@
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))

Check warning on line 1112 in src/flint/fmpq.jl

View check run for this annotation

Codecov / codecov/patch

src/flint/fmpq.jl#L1112

Added line #L1112 was not covered by tests
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 @@
return z
end

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

Check warning on line 1140 in src/flint/fmpq.jl

View check run for this annotation

Codecov / codecov/patch

src/flint/fmpq.jl#L1140

Added line #L1140 was not covered by tests
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,

Check warning on line 1164 in src/flint/fmpq.jl

View check run for this annotation

Codecov / codecov/patch

src/flint/fmpq.jl#L1163-L1164

Added lines #L1163 - L1164 were not covered by tests
(Ref{QQFieldElem}, Ref{QQFieldElem}, Int), c, a, b)
fingolfin marked this conversation as resolved.
Show resolved Hide resolved
return c

Check warning on line 1166 in src/flint/fmpq.jl

View check run for this annotation

Codecov / codecov/patch

src/flint/fmpq.jl#L1166

Added line #L1166 was not covered by tests
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 @@
return z
end

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

Check warning on line 2547 in src/flint/fmpz.jl

View check run for this annotation

Codecov / codecov/patch

src/flint/fmpz.jl#L2547

Added line #L2547 was not covered by tests

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 @@
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 @@
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 @@
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 @@
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)

Check warning on line 2658 in src/flint/fmpz.jl

View check run for this annotation

Codecov / codecov/patch

src/flint/fmpz.jl#L2657-L2658

Added lines #L2657 - L2658 were not covered by tests

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

Expand All @@ -2673,6 +2678,8 @@
return z
end

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

Check warning on line 2681 in src/flint/fmpz.jl

View check run for this annotation

Codecov / codecov/patch

src/flint/fmpz.jl#L2681

Added line #L2681 was not covered by tests

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

Expand All @@ -2698,24 +2705,28 @@
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

Check warning on line 2716 in src/flint/fmpz.jl

View check run for this annotation

Codecov / codecov/patch

src/flint/fmpz.jl#L2715-L2716

Added lines #L2715 - L2716 were not covered by tests
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))

Check warning on line 2725 in src/flint/fmpz.jl

View check run for this annotation

Codecov / codecov/patch

src/flint/fmpz.jl#L2725

Added line #L2725 was not covered by tests

#

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
Loading