PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/rpython/rlib/test/test_jit_libffi.py

https://bitbucket.org/oberstet/pypy
Python | 39 lines | 31 code | 4 blank | 4 comment | 0 complexity | 134b44c00827de7e65add4e499f3b27d MD5 | raw file
Possible License(s): Apache-2.0
  1. import math
  2. import ctypes
  3. from rpython.rtyper.lltypesystem import lltype, rffi
  4. from rpython.rlib import clibffi
  5. from rpython.rlib.rarithmetic import intmask
  6. from rpython.rlib.jit_libffi import CIF_DESCRIPTION
  7. from rpython.rlib.jit_libffi import jit_ffi_prep_cif, jit_ffi_call
  8. math_sin = intmask(ctypes.cast(ctypes.CDLL(None).sin, ctypes.c_void_p).value)
  9. math_sin = rffi.cast(rffi.VOIDP, math_sin)
  10. def test_jit_ffi_call():
  11. cd = lltype.malloc(CIF_DESCRIPTION, 1, flavor='raw')
  12. cd.abi = clibffi.FFI_DEFAULT_ABI
  13. cd.nargs = 1
  14. cd.rtype = clibffi.cast_type_to_ffitype(rffi.DOUBLE)
  15. atypes = lltype.malloc(clibffi.FFI_TYPE_PP.TO, 1, flavor='raw')
  16. atypes[0] = clibffi.cast_type_to_ffitype(rffi.DOUBLE)
  17. cd.atypes = atypes
  18. cd.exchange_size = 64 # 64 bytes of exchange data
  19. cd.exchange_result = 24
  20. cd.exchange_result_libffi = 24
  21. cd.exchange_args[0] = 16
  22. #
  23. jit_ffi_prep_cif(cd)
  24. #
  25. assert rffi.sizeof(rffi.DOUBLE) == 8
  26. exb = lltype.malloc(rffi.DOUBLEP.TO, 8, flavor='raw')
  27. exb[2] = 1.23
  28. jit_ffi_call(cd, math_sin, rffi.cast(rffi.CCHARP, exb))
  29. res = exb[3]
  30. lltype.free(exb, flavor='raw')
  31. #
  32. lltype.free(atypes, flavor='raw')
  33. lltype.free(cd, flavor='raw')
  34. #
  35. assert res == math.sin(1.23)