-
Notifications
You must be signed in to change notification settings - Fork 58
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
Introduce low-level analogs for Flint structs #1889
Draft
fingolfin
wants to merge
3
commits into
Nemocas:master
Choose a base branch
from
fingolfin:mh/ctypes
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+283
−37
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,255 @@ | ||
############################################################################### | ||
# | ||
# Flint C types | ||
# | ||
############################################################################### | ||
|
||
module Flint | ||
|
||
# | ||
# C types (names provided to ease automatic conversion of struct definitions) | ||
# | ||
const int = Cint | ||
const char = Cchar | ||
|
||
# | ||
# from flint.h | ||
# | ||
const ulong = Culong | ||
const slong = Clong | ||
|
||
const flint_bitcnt_t = ulong | ||
const nn_ptr = Ptr{ulong} | ||
|
||
const fmpz = Clong | ||
|
||
const fmpz_t = Ptr{fmpz} | ||
|
||
struct fmpq | ||
num::fmpz | ||
den::fmpz | ||
end | ||
|
||
const fmpq_t = Ptr{fmpq} | ||
|
||
struct nmod_t | ||
n::ulong | ||
ninv::ulong | ||
norm::flint_bitcnt_t | ||
end | ||
|
||
# | ||
# from limb_types.h | ||
# | ||
|
||
const FLINT_MAX_FACTORS_IN_LIMB = 15 | ||
|
||
struct n_factor_t | ||
num::int | ||
exp::NTuple{FLINT_MAX_FACTORS_IN_LIMB, int} | ||
p::NTuple{FLINT_MAX_FACTORS_IN_LIMB, ulong} | ||
end | ||
|
||
struct n_primes_struct | ||
small_i::slong | ||
small_num::slong | ||
small_primes::Ptr{Cuint} | ||
|
||
sieve_a::ulong | ||
sieve_b::ulong | ||
sieve_i::slong | ||
sieve_num::slong | ||
sieve::Ptr{char} | ||
end | ||
|
||
# | ||
# from fmpz_types.h | ||
# | ||
|
||
struct zz_struct | ||
alloc::int | ||
size::int | ||
ptr::nn_ptr | ||
end | ||
|
||
const zz_ptr = Ptr{zz_struct} | ||
|
||
struct fmpz_factor_struct | ||
sign::int | ||
p::Ptr{fmpz} | ||
exp::Ptr{ulong} | ||
alloc::slong | ||
num::slong | ||
end | ||
|
||
struct fmpz_preinvn_struct | ||
dinv::nn_ptr | ||
n::slong | ||
norm::flint_bitcnt_t | ||
end | ||
|
||
struct fmpz_poly_struct | ||
coeffs::Ptr{fmpz} | ||
alloc::slong | ||
length::slong | ||
end | ||
|
||
struct fmpz_poly_factor_struct | ||
c::fmpz | ||
p::Ptr{fmpz_poly_struct} | ||
exp::Ptr{slong} | ||
num::slong | ||
alloc::slong | ||
end | ||
|
||
struct fmpz_mat_struct | ||
entries::Ptr{fmpz} | ||
r::slong | ||
c::slong | ||
rows::Ptr{Ptr{fmpz}} | ||
end | ||
|
||
struct fmpz_poly_mat_struct | ||
entries::Ptr{fmpz_poly_struct} | ||
r::slong | ||
c::slong | ||
rows::Ptr{Ptr{fmpz_poly_struct}} | ||
end | ||
|
||
struct fmpz_mpoly_struct | ||
coeffs::Ptr{fmpz} | ||
exps::Ptr{ulong} | ||
alloc::slong | ||
length::slong | ||
bits::flint_bitcnt_t | ||
end | ||
|
||
struct fmpz_mpoly_factor_struct | ||
constant::fmpz_t | ||
constant_den::fmpz_t | ||
poly::Ptr{fmpz_mpoly_struct} | ||
exp::Ptr{fmpz} | ||
num::slong | ||
alloc::slong | ||
end | ||
|
||
struct fmpz_poly_q_struct | ||
num::Ptr{fmpz_poly_struct} | ||
den::Ptr{fmpz_poly_struct} | ||
end | ||
|
||
struct fmpz_mpoly_q_struct | ||
num::fmpz_mpoly_struct | ||
den::fmpz_mpoly_struct | ||
end | ||
|
||
struct fmpzi_struct | ||
a::fmpz | ||
b::fmpz | ||
end | ||
|
||
|
||
# | ||
# from nmod_types.h | ||
# | ||
struct nmod_mat_struct | ||
entries::Ptr{ulong} | ||
r::slong | ||
c::slong | ||
rows::Ptr{Ptr{ulong}} | ||
mod::nmod_t | ||
end | ||
|
||
struct nmod_poly_struct | ||
coeffs::nn_ptr | ||
alloc::slong | ||
length::slong | ||
mod::nmod_t | ||
end | ||
|
||
const nmod_poly_t = Ptr{nmod_poly_struct} | ||
|
||
struct nmod_poly_factor_struct | ||
p::Ptr{nmod_poly_struct} | ||
exp::Ptr{slong} | ||
num::slong | ||
alloc::slong | ||
end | ||
|
||
struct nmod_poly_mat_struct | ||
entries::Ptr{nmod_poly_struct} | ||
r::slong | ||
c::slong | ||
rows::Ptr{Ptr{nmod_poly_struct}} | ||
modulus::ulong | ||
end | ||
|
||
struct nmod_mpoly_struct | ||
coeffs::Ptr{ulong} | ||
exps::Ptr{ulong} | ||
length::slong | ||
bits::flint_bitcnt_t | ||
coeffs_alloc::slong | ||
exps_alloc::slong | ||
end | ||
|
||
struct nmod_mpoly_factor_struct | ||
constant::ulong | ||
poly::Ptr{nmod_mpoly_struct} | ||
exp::Ptr{fmpz} | ||
num::slong | ||
alloc::slong | ||
end | ||
|
||
# | ||
# from fq_nmod_types.h | ||
# | ||
|
||
const fq_nmod_struct = nmod_poly_struct | ||
|
||
struct fq_nmod_ctx_struct | ||
mod::nmod_t | ||
|
||
sparse_modulus::int | ||
is_conway::int | ||
|
||
a::Ptr{ulong} | ||
j::Ptr{slong} | ||
len::slong | ||
|
||
modulus::nmod_poly_t | ||
inv::nmod_poly_t | ||
|
||
var::Ptr{char} | ||
end | ||
|
||
struct fq_nmod_mat_struct | ||
entries::Ptr{fq_nmod_struct} | ||
r::slong | ||
c::slong | ||
rows::Ptr{Ptr{fq_nmod_struct}} | ||
end | ||
|
||
struct fq_nmod_poly_struct | ||
coeffs::Ptr{fq_nmod_struct} | ||
alloc::slong | ||
length::slong | ||
end | ||
|
||
struct fq_nmod_poly_factor_struct | ||
poly::Ptr{fq_nmod_poly_struct} | ||
exp::Ptr{slong} | ||
num::slong | ||
alloc::slong | ||
end | ||
|
||
struct fq_nmod_mpoly_struct | ||
coeffs::Ptr{ulong} | ||
exps::Ptr{ulong} | ||
length::slong | ||
bits::flint_bitcnt_t | ||
coeffs_alloc::slong | ||
exps_alloc::slong | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -6,6 +6,8 @@ | |||||
|
||||||
const _err_dim_negative = ErrorException("Dimensions must be non-negative") | ||||||
|
||||||
include("CTypes.jl") | ||||||
|
||||||
############################################################################### | ||||||
# | ||||||
# ZZRing / ZZRingElem | ||||||
|
@@ -42,7 +44,7 @@ integer_ring() = ZZRing() | |||||
|
||||||
@doc zz_ring_doc | ||||||
mutable struct ZZRingElem <: RingElem | ||||||
d::Int | ||||||
d::Flint.fmpz | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
consistency with the other structs |
||||||
|
||||||
function ZZRingElem() | ||||||
z = new() | ||||||
|
@@ -92,11 +94,7 @@ function _fmpz_clear_fn(a::ZZRingElem) | |||||
end | ||||||
|
||||||
mutable struct fmpz_factor | ||||||
sign::Cint | ||||||
p::Ptr{Nothing} # Array of fmpz_struct's | ||||||
exp::Ptr{UInt} | ||||||
alloc::Int | ||||||
num::Int | ||||||
data::Flint.fmpz_factor_struct | ||||||
|
||||||
function fmpz_factor() | ||||||
z = new() | ||||||
|
@@ -123,9 +121,7 @@ end | |||||
############################################################################### | ||||||
|
||||||
mutable struct n_factor | ||||||
num::Cint | ||||||
exp::NTuple{15, Cint} | ||||||
p::NTuple{15, UInt} | ||||||
data::Flint.n_factor_t | ||||||
|
||||||
function n_factor() | ||||||
z = new() | ||||||
|
@@ -240,9 +236,7 @@ end | |||||
const FmpzPolyID = CacheDictType{Symbol, ZZPolyRing}() | ||||||
|
||||||
mutable struct ZZPolyRingElem <: PolyRingElem{ZZRingElem} | ||||||
coeffs::Ptr{Nothing} | ||||||
alloc::Int | ||||||
length::Int | ||||||
data::Flint.fmpz_poly_struct | ||||||
parent::ZZPolyRing | ||||||
|
||||||
function ZZPolyRingElem() | ||||||
|
@@ -272,11 +266,7 @@ function _fmpz_poly_clear_fn(a::ZZPolyRingElem) | |||||
end | ||||||
|
||||||
mutable struct fmpz_poly_factor | ||||||
d::Int # ZZRingElem | ||||||
p::Ptr{ZZPolyRingElem} # array of flint fmpz_poly_struct's | ||||||
exp::Ptr{Int} | ||||||
num::Int | ||||||
alloc::Int | ||||||
data::Flint.fmpz_poly_factor_struct | ||||||
|
||||||
function fmpz_poly_factor() | ||||||
z = new() | ||||||
|
@@ -542,12 +532,7 @@ end | |||||
const NmodPolyRingID = CacheDictType{Tuple{zzModRing, Symbol}, zzModPolyRing}() | ||||||
|
||||||
mutable struct zzModPolyRingElem <: PolyRingElem{zzModRingElem} | ||||||
coeffs::Ptr{Nothing} | ||||||
alloc::Int | ||||||
length::Int | ||||||
mod_n::UInt | ||||||
mod_ninv::UInt | ||||||
mod_norm::UInt | ||||||
data::Flint.nmod_poly_struct | ||||||
parent::zzModPolyRing | ||||||
|
||||||
function zzModPolyRingElem(n::UInt) | ||||||
|
@@ -666,6 +651,8 @@ mutable struct fpPolyRingElem <: PolyRingElem{fpFieldElem} | |||||
mod_n::UInt | ||||||
mod_ninv::UInt | ||||||
mod_norm::UInt | ||||||
# end of flint struct | ||||||
|
||||||
parent::fpPolyRing | ||||||
|
||||||
function fpPolyRingElem(n::UInt) | ||||||
|
@@ -2946,6 +2933,8 @@ mutable struct fpRelPowerSeriesRingElem <: RelPowerSeriesRingElem{fpFieldElem} | |||||
mod_n::UInt | ||||||
mod_ninv::UInt | ||||||
mod_norm::UInt | ||||||
# end of flint struct | ||||||
|
||||||
prec::Int | ||||||
val::Int | ||||||
parent::fpRelPowerSeriesRing | ||||||
|
@@ -3038,6 +3027,8 @@ mutable struct zzModRelPowerSeriesRingElem <: RelPowerSeriesRingElem{zzModRingEl | |||||
mod_n::UInt | ||||||
mod_ninv::UInt | ||||||
mod_norm::UInt | ||||||
# end of flint struct | ||||||
|
||||||
prec::Int | ||||||
val::Int | ||||||
parent::zzModRelPowerSeriesRing | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Things I have no thought through yet: which of these should we do (and why):
fmpz_poly_struct
astruct
, andZZPolyRingElem
amutable struct
with finalizer (this is what we have now)fmpz_poly_struct
amutable struct
, with finalizer, andZZPolyRingElem
astruct
mutable struct
(and one of them has finalizer)I did 1 because this needed the least number of changes and this PR is just an incomplete PoC
Pro for 2 would be that several high-level Julia structs wrap the same underlying FLINT structs (e.g.
fmpz_poly_struct
is wrapped byZZPolyRingElem
,ZZAbsPowerSeriesRingElem
andZZRelPowerSeriesRingElem
) and so they could use the same finalizer (well technically they already could do that now).Anyway, I did not try methods 2 and 3, they might not work at all for fundamental reasons, or maybe method 1 (what this PR has now) is fundamentally flawed -- gotta find some time to experiment more. But if someone else has insights, please share them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would find it most intuitive if the finalized is attached to the fmpz_poly_struct as that is the most fundamental data structure that occupies foreign memory. And as you pointed out, that removes some duplication between multiple Nemo types using the same flint primitives