Skip to content

Commit

Permalink
Add the C interface for hsl_subset
Browse files Browse the repository at this point in the history
  • Loading branch information
amontoison committed Dec 26, 2024
1 parent 6ec91c1 commit f20d587
Show file tree
Hide file tree
Showing 12 changed files with 4,361 additions and 1 deletion.
21 changes: 20 additions & 1 deletion gen/rewriter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ end
function hsl_subset_rewrite!(path::String, name::String, optimized::Bool)
text = read(path, String)
structures = ""
info_structures = Tuple{String, String, Bool}[]
if optimized
text = replace(text, "struct " => "mutable struct ")
text = replace(text, "hsl_longc_" => Int64)
Expand Down Expand Up @@ -166,14 +167,18 @@ function hsl_subset_rewrite!(path::String, name::String, optimized::Bool)
routine = replace(routine, "ipc_" => ipc_)
routine = replace(routine, "rpc_" => rpc_)

# Update the type of the structures
routine = replace(routine, "_d}" => "_d{$rpc_,$ipc_}}")
routine = replace(routine, "_i}" => "_i{$rpc_,$ipc_}}")

# Float128 should be passed by value as a Cfloat128
routine = replace(routine, "::Float128" => "::Cfloat128")

text = text * routine * "\n"
end
elseif contains(code, "struct")
structure = code * "end\n"
structure_name = split(split(code, "struct ")[2], "\n")[1]
structure_name = split(split(code, "struct ")[2], "\n")[1] |> String
generic_structure_name = structure_name[1:end-2] |> String
generic_structure_name = 'M' * generic_structure_name[2:end]
generic_structure_name = replace(generic_structure_name, "_solve_control" => "SolveControl")
Expand All @@ -186,8 +191,10 @@ function hsl_subset_rewrite!(path::String, name::String, optimized::Bool)
structure = replace(structure, "ipc_" => "INT")
if !contains(code, "rpc_")
structure = replace(structure, structure_name => generic_structure_name * "{INT}")
push!(info_structures, (structure_name, generic_structure_name, false))
else
structure = replace(structure, structure_name => generic_structure_name * "{T,INT}")
push!(info_structures, (structure_name, generic_structure_name, true))
end
structures = structures * structure * "\n"
else
Expand All @@ -198,6 +205,18 @@ function hsl_subset_rewrite!(path::String, name::String, optimized::Bool)
text = structures * "\n" * text
startswith(text, '\n') && (text = text[2:end])

# Rename the structures in the wrappers
for (old_struct, new_struct, bool) in info_structures
if bool
text = replace(text, "Ptr{$old_struct" => "Ref{$new_struct")
else
for precision in ("Float32", "Float64", "Float128")
text = replace(text, "Ptr{$old_struct{$precision,Int32}}" => "Ref{$new_struct{Int32}}")
text = replace(text, "Ptr{$old_struct{$precision,Int64}}" => "Ref{$new_struct{Int64}}")
end
end
end

write(path, text)
format_file(path, YASStyle())
end
445 changes: 445 additions & 0 deletions src/C/hsl_subset/hsl_ma48.jl

Large diffs are not rendered by default.

Loading

0 comments on commit f20d587

Please sign in to comment.