Skip to content

Commit

Permalink
Improve conversion; also include flint.h
Browse files Browse the repository at this point in the history
  • Loading branch information
lgoettgens committed Jan 30, 2025
1 parent 465444f commit 3fa24e7
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 80 deletions.
21 changes: 2 additions & 19 deletions etc/FlintCTypes_template.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,13 @@ const slong = Int


#
# from flint.h
# from `./configure`
#

const flint_bitcnt_t = ulong
const nn_ptr = Ptr{ulong}
const nn_srcptr = Ptr{ulong}
const FLINT_BITS = UInt == UInt64 ? 64 : 32

struct nmod_t
n::mp_limb_t
ninv::mp_limb_t
norm::flint_bitcnt_t
end

const fmpz = slong
const fmpz_t = Ptr{fmpz}

struct fmpq
num::fmpz
den::fmpz
end

const fmpq_t = Ptr{fmpq}

@include_c_header("flint.h")

@include_c_header("limb_types.h")

Expand Down
55 changes: 33 additions & 22 deletions etc/generate_FLINT_structs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,21 @@ function expand_templates(input, flintdir)
return replace(input, substitutions...)
end

const regex_typedef_struct_fields_name = r"typedef struct\s*\{([^{}]+)\}\s*([a-z_]+);"
const regex_typedef_struct_fields_ptrname = r"typedef struct\s*\{([^{}]+)\}\s*([a-z_]+)\[1\];"
const regex_struct_structname_fields = r"struct *([a-z_]+)\s*\{([^{}]+)\}\s*;"
const regex_typedef_struct_structname_fields_name = r"typedef struct *([a-z_]+)\s*\{([^{}]+)\}\s*([a-z_]+);"
const regex_typedef_struct_fields_name = r"^typedef struct\s*\{([^{}]+)\}\s*([a-z_]+);"m
const regex_typedef_struct_fields_ptrname = r"^typedef struct\s*\{([^{}]+)\}\s*([a-z_]+)\[1\];"m
const regex_struct_structname_fields = r"^struct *([a-z_]+)\s*\{([^{}]+)\}\s*;"m
const regex_typedef_struct_structname_fields_name = r"^typedef struct *([a-z_]+)\s*\{([^{}]+)\}\s*([a-z_]+);"m

const regex_typedef_enum_values_name = r"typedef enum\s*\{([^{}]+)\}\s*([a-z_]+);"
const regex_enum_enumname_values = r"enum *([a-z_]+)\s*\{([^{}]+)\}\s*;"
const regex_typedef_enum_enumname_values_name = r"typedef enum *([a-z_]+)\s*\{([^{}]+)\}\s*([a-z_]+);"
const regex_typedef_enum_values_name = r"^typedef enum\s*\{([^{}]+)\}\s*([a-z_]+);"m
const regex_enum_enumname_values = r"^enum *([a-z_]+)\s*\{([^{}]+)\}\s*;"m
const regex_typedef_enum_enumname_values_name = r"^typedef enum *([a-z_]+)\s*\{([^{}]+)\}\s*([a-z_]+);"m

function convert_struct(str)
substitutions = Pair{Regex,Union{SubstitutionString, Function}}[
substitutions = Pair{Regex,Union{SubstitutionString,Function}}[
regex_typedef_struct_fields_name => s"struct \2\1end", # whole typedef struct construct
regex_struct_structname_fields => s"struct struct_\1\2end", # whole struct construct
regex_typedef_struct_fields_ptrname => s"struct struct_\2\1end\nconst \2 = Ptr{struct_\2}", # whole typedef struct construct with [1]
regex_typedef_struct_structname_fields_name => s"struct \3\2end\nconst struct_\1 = \3", # whole typedef struct construct with two names
r"^( +)const "m => s"\1", # remove `const` from fields
r"^ +([a-z_]+) +([A-Za-z0-9_]+);"m => s" \2::\1", # simple fields (one to five declared on one line)
r"^ +([a-z_]+) +([A-Za-z0-9_]+) *, *([A-Za-z0-9_]+);"m => s" \2::\1\n \3::\1",
r"^ +([a-z_]+) +([A-Za-z0-9_]+) *, *([A-Za-z0-9_]+) *, *([A-Za-z0-9_]+);"m => s" \2::\1\n \3::\1\n \4::\1",
Expand All @@ -74,7 +73,7 @@ function convert_struct(str)
end

function convert_enum(str)
substitutions = Pair{Regex,Union{SubstitutionString, Function}}[
substitutions = Pair{Regex,Union{SubstitutionString,Function}}[
regex_typedef_enum_values_name => s"@enum \2 begin\1end", # whole typedef enum construct
regex_enum_enumname_values => s"@enum enum_\1 begin\2end", # whole enum construct
regex_typedef_enum_enumname_values_name => s"@enum \3 begin\2end\nconst enum_\1 = \3", # whole typedef enum construct with two names
Expand All @@ -87,26 +86,34 @@ function convert_enum(str)
end

function c2julia(str::String)
substitutions = Pair{Regex,Union{SubstitutionString, Function}}[
preprocessing = Pair{Regex,Union{SubstitutionString,Function}}[
r"unsigned int" => s"unsigned_int", # convert `unsigned int` to a single token
r"unsigned char" => s"unsigned_char", # convert `unsigned char` to a single token
r" const " => s" ", # remove all `const`
r"^//(.*)$"m => s"#\1", # line comment
r"/\*(.*?)\*/"s => s"#=\1=#", # block comment
]
for substitution in preprocessing
str = replace(str, substitution)
end
substitutions = Pair{Regex,Union{SubstitutionString,Function}}[
regex_typedef_struct_fields_name => convert_struct, # whole typedef struct construct
regex_typedef_struct_fields_ptrname => convert_struct, # whole typedef struct construct with [1]
regex_struct_structname_fields => convert_struct, # whole struct construct
regex_typedef_struct_structname_fields_name => convert_struct, # whole typedef struct construct with two names
regex_typedef_enum_values_name => convert_enum, # whole typedef enum construct
regex_enum_enumname_values => convert_enum, # whole enum construct
regex_typedef_enum_enumname_values_name => convert_enum, # whole typedef enum construct with two names
r"^typedef ([A-Za-z_]+) ([A-Za-z_]+);"m => s"const \2 = \1", # simple typedef
r"^typedef ([A-Za-z_]+) ([A-Za-z_]+)\[1\];"m => s"const \2 = Ptr{\1}", # pointer typedef
r"^#define ([A-Za-z_]+) ([A-Za-z0-9_]+)"m => s"const \1 = \2", # defines used as typedef
r"^//(.*)$"m => s"#\1", # line comment
r"/\*(.*?)\*/"s => s"#=\1=#", # block comment
r"^typedef +([A-Za-z_]+) +([A-Za-z_]+);"m => s"const \2 = \1", # simple typedef
r"^typedef +([A-Za-z_]+) *\* *([A-Za-z_]+);"m => s"const \2 = Ptr{\1}", # pointer typedef
r"^typedef +([A-Za-z_]+) +([A-Za-z_]+)\[1\];"m => s"const \2 = Ptr{\1}", # pointer typedef
r"^#define +([A-Za-z_]+) +(\d+) *$"m => s"const \1 = \2", # defines of integer constants
]
for substitution in substitutions
str = replace(str, substitution)
end
return str
combined_regex = Regex(join(map(re -> re.pattern, first.(substitutions)), "|"), "m")
output = join(
map(m -> replace(m.match, substitutions...), eachmatch(combined_regex, str)), "\n\n"
)
return output
end

file_header_notice(FLINT_jll_version) = """
Expand All @@ -123,11 +130,15 @@ file_header_notice(FLINT_jll_version) = """
#
################################################################################

function expand_template_file(infile::String, outfile::String, flintpath::String, file_header::String)
function expand_template_file(
infile::String, outfile::String, flintpath::String, file_header::String
)
@info "Expanding file" infile outfile
open(outfile, "w") do io
write(io, file_header)
write(io, expand_templates(read(infile, String), joinpath(flintpath, "include", "flint")))
write(
io, expand_templates(read(infile, String), joinpath(flintpath, "include", "flint"))
)
end
end

Expand Down
100 changes: 61 additions & 39 deletions src/flint/FlintCTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,56 @@ const slong = Int


#
# from flint.h
# from `./configure`
#

const FLINT_BITS = UInt == UInt64 ? 64 : 32


###############################################################################
# begin flint.h

const __FLINT_VERSION = 3

const __FLINT_VERSION_MINOR = 2

const __FLINT_VERSION_PATCHLEVEL = 0

const flint_bitcnt_t = ulong

const nn_ptr = Ptr{ulong}

const nn_srcptr = Ptr{ulong}
const FLINT_BITS = UInt == UInt64 ? 64 : 32

const thread_pool_handle = int

struct flint_rand_struct
__gmp_state::Ptr{void}
__randval::ulong
__randval2::ulong
end

const flint_rand_t = Ptr{flint_rand_struct}

@enum flint_err_t begin
FLINT_ERROR #= general error =#
FLINT_OVERFLOW #= overflow =#
FLINT_IMPINV #= impossible inverse =#
FLINT_DOMERR #= domain error =#
FLINT_DIVZERO #= divide by zero =#
FLINT_EXPOF #= exponent overflow =#
FLINT_INEXACT #= inexact error =#
FLINT_TEST_FAIL #= test fail =#
end

struct nmod_t
n::mp_limb_t
ninv::mp_limb_t
n::ulong
ninv::ulong
norm::flint_bitcnt_t
end

const fmpz = slong

const fmpz_t = Ptr{fmpz}

struct fmpq
Expand All @@ -49,6 +84,11 @@ end

const fmpq_t = Ptr{fmpq}

const MPZ_MIN_ALLOC = 2

# end flint.h
###############################################################################


###############################################################################
# begin limb_types.h
Expand Down Expand Up @@ -154,9 +194,9 @@ struct zz_struct
ptr::nn_ptr
end

typedef zz_struct * zz_ptr;
typedef const zz_struct * zz_srcptr;
#define FMPZ_TO_ZZ(x) ((zz_ptr) ((ulong) (x) << 2))
const zz_ptr = Ptr{zz_struct}

const zz_srcptr = Ptr{zz_struct}

struct fmpz_factor_struct
sign::int
Expand Down Expand Up @@ -279,14 +319,6 @@ end

const fmpq_poly_t = Ptr{fmpq_poly_struct}

#=
A polynomial f is represented as
content * zpoly,
where zpoly should have positive leading coefficient and trivial content.
If f is zero, then the representation should have
content = 0 and zpoly = 0
=#

struct fmpq_mpoly_struct #= non zero case: | zero case: =#
content::fmpq_t #= positive or negative content | zero =#
zpoly::fmpz_mpoly_t #= contentless poly, lc is positive | zero =#
Expand Down Expand Up @@ -375,6 +407,7 @@ const fmpz_mod_mpoly_factor_t = Ptr{fmpz_mod_mpoly_factor_struct}
# begin fq_nmod_types.h

const fq_nmod_t = nmod_poly_t

const fq_nmod_struct = nmod_poly_struct

struct fq_nmod_ctx_struct
Expand Down Expand Up @@ -497,6 +530,7 @@ const fq_zech_poly_factor_t = Ptr{fq_zech_poly_factor_struct}
# begin fq_types.h

const fq_t = fmpz_poly_t

const fq_struct = fmpz_poly_struct

struct fq_ctx_struct
Expand Down Expand Up @@ -607,40 +641,42 @@ const padic_poly_t = Ptr{padic_poly_struct}
###############################################################################
# begin n_poly_types.h

#= arrays of ulong =#
struct n_poly_struct
coeffs::Ptr{ulong}
alloc::slong
length::slong
end

const n_poly_t = Ptr{n_poly_struct}

const n_fq_poly_struct = n_poly_struct

const n_fq_poly_t = n_poly_t

#= arrays of arrays of ulong =#
struct n_bpoly_struct
coeffs::Ptr{n_poly_struct}
alloc::slong
length::slong
end

const n_bpoly_t = Ptr{n_bpoly_struct}

const n_fq_bpoly_struct = n_bpoly_struct

const n_fq_bpoly_t = n_bpoly_t

#= arrays of arrays of arrays of ulong =#
struct n_tpoly_struct
coeffs::Ptr{n_bpoly_struct}
alloc::slong
length::slong
end

const n_tpoly_t = Ptr{n_tpoly_struct}

const n_fq_tpoly_struct = n_tpoly_struct

const n_fq_tpoly_t = n_tpoly_t

#= sparse arrays of ulong =#
struct n_polyu_struct
exps::Ptr{ulong}
coeffs::Ptr{ulong}
Expand All @@ -649,15 +685,11 @@ struct n_polyu_struct
end

const n_polyu_t = Ptr{n_polyu_struct}

const n_fq_polyu_struct = n_polyu_struct

const n_fq_polyu_t = n_polyu_t

#=
sparse arrays of arrays of ulong
n_polyu1n => one exponent is in the exps[i]
n_polyu2n => two exponents are packed into the exps[i]
...
=#
struct n_polyun_struct
coeffs::Ptr{n_poly_struct}
exps::Ptr{ulong}
Expand All @@ -666,10 +698,11 @@ struct n_polyun_struct
end

const n_polyun_t = Ptr{n_polyun_struct}

const n_fq_polyun_struct = n_polyun_struct

const n_fq_polyun_t = n_polyun_t

#= n_poly stack =#
struct n_poly_stack_struct
array::Ptr{Ptr{n_poly_struct}}
alloc::slong
Expand All @@ -678,7 +711,6 @@ end

const n_poly_stack_t = Ptr{n_poly_stack_struct}

#= n_bpoly stack =#
struct n_bpoly_stack_struct
array::Ptr{Ptr{n_bpoly_struct}}
alloc::slong
Expand Down Expand Up @@ -714,8 +746,6 @@ const nmod_eval_interp_t = Ptr{nmod_eval_interp_struct}
###############################################################################
# begin mpoly_types.h

#define MPOLY_MIN_BITS (UWORD(8)) #= minimum number of bits to pack into =#

@enum ordering_t begin
ORD_LEX
ORD_DEGLEX
Expand Down Expand Up @@ -769,7 +799,6 @@ end

const fq_nmod_mpoly_ctx_t = Ptr{fq_nmod_mpoly_ctx_struct}


struct struct_mpoly_void_ring_t
elem_size::slong
ctx::Ptr{void}
Expand Down Expand Up @@ -859,31 +888,24 @@ end

const mpoly_compression_t = Ptr{mpoly_compression_struct}

#=
nmod_mpolyn_t
multivariates with n_poly_t coefficients
=#
struct nmod_mpolyn_struct
coeffs::Ptr{n_poly_struct}
exps::Ptr{ulong}
alloc::slong
length::slong
bits::slong
end

const nmod_mpolyn_t = Ptr{nmod_mpolyn_struct}

#=
nmod_mpolyun_t
sparse univariates with nmod_mpolyn_t coefficients
with uniform bits and LEX ordering
=#
struct nmod_mpolyun_struct
coeffs::Ptr{nmod_mpolyn_struct}
exps::Ptr{ulong}
alloc::slong
length::slong
bits::flint_bitcnt_t #= default bits to construct coeffs =#
end

const nmod_mpolyun_t = Ptr{nmod_mpolyun_struct}

@enum nmod_gcds_ret_t begin
Expand Down

0 comments on commit 3fa24e7

Please sign in to comment.