import sys import numpy as np import numba.core.typing.cffi_utils as cffi_support from numba.tests.support import import_dynamic, temp_directory from numba.core.types import complex128 def load_inline_module(): """ Create an inline module, return the corresponding ffi and dll objects. """ from cffi import FFI # We can't rely on libc availability on Windows anymore, so we use our # own compiled wrappers (see https://bugs.python.org/issue23606). defs = """ double _numba_test_sin(double x); double _numba_test_cos(double x); double _numba_test_funcptr(double (*func)(double)); bool _numba_test_boolean(void); """ ffi = FFI() ffi.cdef(defs) # Load the _helperlib namespace from numba import _helperlib return ffi, ffi.dlopen(_helperlib.__file__) def load_ool_module(): """ Compile an out-of-line module, return the corresponding ffi and module objects. """ from cffi import FFI numba_complex = """ typedef struct _numba_complex { double real; double imag; } numba_complex; """ bool_define = """ #ifdef _MSC_VER #define false 0 #define true 1 #define bool int #else #include #endif """ defs = numba_complex + """ bool boolean(void); double sin(double x); double cos(double x); int foo(int a, int b, int c); void vsSin(int n, float* x, float* y); void vdSin(int n, double* x, double* y); void vector_real(numba_complex *c, double *real, int n); void vector_imag(numba_complex *c, double *imag, int n); """ source = numba_complex + bool_define + """ static bool boolean(void) { return true; } static int foo(int a, int b, int c) { return a + b * c; } void vsSin(int n, float* x, float* y) { int i; for (i=0; i 0: return fa(x) else: return fb(x) def use_user_defined_symbols(): return cffi_foo(1, 2, 3) # The from_buffer method is member of cffi.FFI, and also of CompiledFFI objects # (cffi_usecases_ool.ffi is a CompiledFFI object) so we use both in these # functions. def vector_sin_float32(x, y): vsSin(len(x), ffi.from_buffer(x), ffi_ool.from_buffer(y)) def vector_sin_float64(x, y): vdSin(len(x), ffi.from_buffer(x), ffi_ool.from_buffer(y)) # For testing pointer to structs from buffers def vector_extract_real(x, y): vector_real(ffi.from_buffer(x), ffi.from_buffer(y), len(x)) def vector_extract_imag(x, y): vector_imag(ffi.from_buffer(x), ffi.from_buffer(y), len(x))