Here is some sample code:
% Use ffi package for dynamic communication with C
:- use_module(library(ffi)).
% Add linasm to file search path,
% ./linasm/include/Array.h and ./linasm/liblinasm.so need to
% exist in the directory where this prolog file is located.
:- prolog_load_context(directory,Dir1),
assertz((
user:file_search_path(linasm,Dir) :-
absolute_file_name(Dir1/linasm,Dir)
)).
% Hook to pass -I flag
ffi:cpp_hook(path(gcc), ['-E', '-xc', '-I',IncDir, -]) :-
absolute_file_name(linasm(include),IncDir).
% Import sample function from headers
:- c_import("#include \"Array.h\"", % ffi:cpp_hook provides -I flag
[ linasm(lib/'liblinasm.so') ],
[
'Array_SubVector_sint64'( *int, % sint64_t target[],
*int, % const sint64_t source[],
int, % size_t size,
[void])
]).
try(Count) :-
array(1,Count,A1),
array(2,Count,A2),
time('Array_SubVector_sint64'(A1,A2,Count)),
time(read_array(0, Count, A1, List)),
format("Array1 - Array2 = ~w",[List]).
% Allocate a C array:
% Start, Start+1, Start+2, ...
array(Start,Count,Array) :-
End is Count + Start - 1,
time(
( numlist(Start,End,L),
c_alloc(Array, long[]=L),
print(array(Array))
)
).
% Read C array back into prolog List
read_array(I, Count, Data, [H|T]) :-
I < Count,
!,
c_load(Data[I], H),
I2 is I + 1,
read_array(I2, Count, Data, T).
read_array(_, _, _, []).
Query:
2 ?- try(10).
array(<C long[10]>(0x5652d646e140))
% 60 inferences, 0.000 CPU in 0.000 seconds (94% CPU, 1144187 Lips)
array(<C long[10]>(0x5652d646ff90))
% 60 inferences, 0.000 CPU in 0.000 seconds (58% CPU, 972842 Lips)
% 2 inferences, 0.000 CPU in 0.000 seconds (96% CPU, 223964 Lips)
% 152 inferences, 0.000 CPU in 0.000 seconds (96% CPU, 4106665 Lips)
Array1 - Array2 = [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1]
true.
Subtract array means the difference between two arrays, e.g. array1 - array2.