Skip to content

Instantly share code, notes, and snippets.

@michelp
Created December 20, 2019 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michelp/8844418f749079aaafe1993bc40a4921 to your computer and use it in GitHub Desktop.
Save michelp/8844418f749079aaafe1993bc40a4921 to your computer and use it in GitHub Desktop.
BF_tuple = Record.make_c_struct([
('w', double),
('h', uint64),
('pi', uint64),
])
src = """
typedef struct BF_tuple {
double w;
uint64_t h;
uint64_t pi;
} BF_tuple;
typedef double (*bf_lmin)(BF_tuple*);
"""
f = FFI()
f.cdef(src)
data = f.new('BF_tuple[1]')
ptr = f.cast('BF_tuple*', data)
ptr[0].w = 0.1
ptr[0].h = 0
ptr[0].pi = 1
from numba import cffi_support
cffi_support.map_type(f.typeof('BF_tuple'), use_record_dtype=True)
sig = cffi_support.map_type(f.typeof('bf_lmin'), use_record_dtype=True)
from numba import cfunc, carray
@cfunc(sig)
def bf_lmin(z_):
z = carray(z_, 1)[0]
return z.w
# if (x[0].w[0] < y.w
# or (x.w == y.w and x.h < y.h)
# or (x.w == y.w and x.h == y.h and x.pi < y.pi)):
# if (z != x):
# z[0] = x[0]
# else:
# z[0] = y[0]
addr = int(f.cast('size_t', ptr))
print("address of data:", hex(addr))
bf_lmin.ctypes(addr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment