From 7e9e7d9207879a3bf79126960c375d3d4110f9c0 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Thu, 17 Oct 2024 14:53:37 +0200 Subject: [PATCH 1/2] Unsafe ops for fq_default --- src/flint/fq_default.jl | 100 ++++++++++++++++++++-------------------- 1 file changed, 50 insertions(+), 50 deletions(-) diff --git a/src/flint/fq_default.jl b/src/flint/fq_default.jl index 1d77f3789..f93f928ea 100644 --- a/src/flint/fq_default.jl +++ b/src/flint/fq_default.jl @@ -403,9 +403,7 @@ end function +(x::FqFieldElem, y::FqFieldElem) if parent(x) === parent(y) z = parent(y)() - ccall((:fq_default_add, libflint), Nothing, - (Ref{FqFieldElem}, Ref{FqFieldElem}, Ref{FqFieldElem}, Ref{FqField}), z, x, y, y.parent) - return z + return add!(z, x, y) end return +(_promote(x, y)...) end @@ -413,9 +411,7 @@ end function -(x::FqFieldElem, y::FqFieldElem) if parent(x) === parent(y) z = parent(y)() - ccall((:fq_default_sub, libflint), Nothing, - (Ref{FqFieldElem}, Ref{FqFieldElem}, Ref{FqFieldElem}, Ref{FqField}), z, x, y, y.parent) - return z + return sub!(z, x, y) end return -(_promote(x, y)...) end @@ -423,9 +419,7 @@ end function *(x::FqFieldElem, y::FqFieldElem) if parent(x) === parent(y) z = parent(y)() - ccall((:fq_default_mul, libflint), Nothing, - (Ref{FqFieldElem}, Ref{FqFieldElem}, Ref{FqFieldElem}, Ref{FqField}), z, x, y, y.parent) - return z + return mul!(z, x, y) end return *(_promote(x, y)...) end @@ -436,43 +430,19 @@ end # ############################################################################### -function *(x::Int, y::FqFieldElem) - z = parent(y)() - ccall((:fq_default_mul_si, libflint), Nothing, - (Ref{FqFieldElem}, Ref{FqFieldElem}, Int, Ref{FqField}), z, y, x, y.parent) - return z -end - -*(x::Integer, y::FqFieldElem) = ZZRingElem(x)*y - -*(x::FqFieldElem, y::Integer) = y*x - -function *(x::ZZRingElem, y::FqFieldElem) - z = parent(y)() - ccall((:fq_default_mul_fmpz, libflint), Nothing, - (Ref{FqFieldElem}, Ref{FqFieldElem}, Ref{ZZRingElem}, Ref{FqField}), - z, y, x, y.parent) - return z +for jT in (Integer, ZZRingElem) + @eval begin + *(x::FqFieldElem, y::$jT) = mul!(parent(x)(), x, y) + *(x::$jT, y::FqFieldElem) = mul!(parent(y)(), x, y) + + +(x::FqFieldElem, y::$jT) = x + parent(x)(y) + +(x::$jT, y::FqFieldElem) = y + x + + -(x::FqFieldElem, y::$jT) = x - parent(x)(y) + -(x::$jT, y::FqFieldElem) = parent(y)(x) - y + end end -*(x::FqFieldElem, y::ZZRingElem) = y*x - -+(x::FqFieldElem, y::Integer) = x + parent(x)(y) - -+(x::Integer, y::FqFieldElem) = y + x - -+(x::FqFieldElem, y::ZZRingElem) = x + parent(x)(y) - -+(x::ZZRingElem, y::FqFieldElem) = y + x - --(x::FqFieldElem, y::Integer) = x - parent(x)(y) - --(x::Integer, y::FqFieldElem) = parent(y)(x) - y - --(x::FqFieldElem, y::ZZRingElem) = x - parent(x)(y) - --(x::ZZRingElem, y::FqFieldElem) = parent(y)(x) - y - ############################################################################### # # Powering @@ -675,20 +645,50 @@ function neg!(z::FqFieldElem, a::FqFieldElem) return z end -function mul!(z::FqFieldElem, x::FqFieldElem, y::FqFieldElem) - ccall((:fq_default_mul, libflint), Nothing, - (Ref{FqFieldElem}, Ref{FqFieldElem}, Ref{FqFieldElem}, Ref{FqField}), z, x, y, y.parent) +# + +function add!(z::FqFieldElem, x::FqFieldElem, y::FqFieldElem) + @ccall libflint.fq_default_add(z::Ref{FqFieldElem}, x::Ref{FqFieldElem}, y::Ref{FqFieldElem}, x.parent::Ref{FqField})::Nothing z.poly = nothing return z end -function add!(z::FqFieldElem, x::FqFieldElem, y::FqFieldElem) - ccall((:fq_default_add, libflint), Nothing, - (Ref{FqFieldElem}, Ref{FqFieldElem}, Ref{FqFieldElem}, Ref{FqField}), z, x, y, x.parent) +# + +function sub!(z::FqFieldElem, x::FqFieldElem, y::FqFieldElem) + @ccall libflint.fq_default_sub(z::Ref{FqFieldElem}, x::Ref{FqFieldElem}, y::Ref{FqFieldElem}, x.parent::Ref{FqField})::Nothing + z.poly = nothing + return z +end + +# + +function mul!(z::FqFieldElem, x::FqFieldElem, y::FqFieldElem) + @ccall libflint.fq_default_mul(z::Ref{FqFieldElem}, x::Ref{FqFieldElem}, y::Ref{FqFieldElem}, y.parent::Ref{FqField})::Nothing z.poly = nothing return z end +function mul!(z::FqFieldElem, x::FqFieldElem, y::ZZRingElemOrPtr) + @ccall libflint.fq_default_mul_fmpz(z::Ref{FqFieldElem}, x::Ref{FqFieldElem}, y::Ref{ZZRingElem}, x.parent::Ref{FqField})::Nothing + return z +end + +function mul!(z::FqFieldElem, x::FqFieldElem, y::Int) + @ccall libflint.fq_default_mul_si(z::Ref{FqFieldElem}, x::Ref{FqFieldElem}, y::Int, x.parent::Ref{FqField})::Nothing + return z +end + +function mul!(z::FqFieldElem, x::FqFieldElem, y::UInt) + @ccall libflint.fq_default_mul_ui(z::Ref{FqFieldElem}, x::Ref{FqFieldElem}, y::Int, x.parent::Ref{FqField})::Nothing + return z +end + +mul!(z::FqFieldElem, x::FqFieldElem, y::Integer) = mul!(z, x, flintify(y)) + +mul!(z::FqFieldElem, x::ZZRingElemOrPtr, y::FqFieldElem) = mul!(z, y, x) +mul!(z::FqFieldElem, x::Integer, y::FqFieldElem) = mul!(z, y, x) + ############################################################################### # # Random functions From ec7c11d2b1ea3774f50462a45083bde93697ed7f Mon Sep 17 00:00:00 2001 From: Max Horn Date: Fri, 18 Oct 2024 12:15:37 +0200 Subject: [PATCH 2/2] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Lars Göttgens --- src/flint/fq_default.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/flint/fq_default.jl b/src/flint/fq_default.jl index f93f928ea..30ca830c9 100644 --- a/src/flint/fq_default.jl +++ b/src/flint/fq_default.jl @@ -642,6 +642,7 @@ end function neg!(z::FqFieldElem, a::FqFieldElem) ccall((:fq_default_neg, libflint), Nothing, (Ref{FqFieldElem}, Ref{FqFieldElem}, Ref{FqField}), z, a, a.parent) + z.poly = nothing return z end @@ -671,16 +672,19 @@ end function mul!(z::FqFieldElem, x::FqFieldElem, y::ZZRingElemOrPtr) @ccall libflint.fq_default_mul_fmpz(z::Ref{FqFieldElem}, x::Ref{FqFieldElem}, y::Ref{ZZRingElem}, x.parent::Ref{FqField})::Nothing + z.poly = nothing return z end function mul!(z::FqFieldElem, x::FqFieldElem, y::Int) @ccall libflint.fq_default_mul_si(z::Ref{FqFieldElem}, x::Ref{FqFieldElem}, y::Int, x.parent::Ref{FqField})::Nothing + z.poly = nothing return z end function mul!(z::FqFieldElem, x::FqFieldElem, y::UInt) @ccall libflint.fq_default_mul_ui(z::Ref{FqFieldElem}, x::Ref{FqFieldElem}, y::Int, x.parent::Ref{FqField})::Nothing + z.poly = nothing return z end