RicardasSim

python3, ctypes, arrays

Jun 18th, 2019
849
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.72 KB | None | 0 0
  1. /*
  2. file testc.py:
  3.  
  4. #!/usr/bin/env python3
  5.  
  6. from ctypes import *
  7.  
  8. lib = cdll.LoadLibrary('./libtestas.so.1.0')
  9.  
  10. f_array_1 = (c_float * 16)(*range(16))
  11. f_array_2 = (c_float * 16)(*range(16))
  12.  
  13. print("f_array_1:");
  14.  
  15. for i in range(0,16):
  16.     print(f_array_1[i], ' ', end = '', flush=True)
  17.  
  18. print("\nf_array_2 (before):")
  19.  
  20. for i in range(0,16):
  21.     f_array_2[i] = 0.0
  22.     print(f_array_2[i], ' ', end = '', flush=True)
  23.  
  24. retval = c_bool()
  25.  
  26. lib.arrTst.restype=c_bool
  27.  
  28. retval = lib.arrTst(byref(f_array_1), byref(f_array_2), 16)
  29.  
  30. if retval:
  31.     print("\nERROR: arrTst")
  32. else:
  33.     print("\nf_array_2 (after):")
  34.     for i in range(0,16):
  35.         print(f_array_2[i], ' ', end = '', flush=True)
  36.  
  37. print(" ")
  38.  
  39. */
  40.  
  41. /*
  42.  
  43. build shared library:
  44.  
  45. gcc -fPIC -c testas.c
  46. ld -shared -soname libtestas.so.1 -o libtestas.so.1.0 -lc testas.o
  47.  
  48.  
  49. file testc.py, make executable:
  50.  
  51. chmod +x testc.py
  52.  
  53. run:
  54. ./testc.py
  55.  
  56. */
  57.  
  58. //file testas.c:
  59.  
  60. #include <stdio.h>
  61. #include <stdlib.h>
  62. #include <stdbool.h>
  63. #include <stdint.h>
  64.  
  65. void _init()
  66. {
  67.     printf("init\n");
  68. }
  69.  
  70. void _fini()
  71. {
  72.     printf("fini\n");
  73. }
  74.  
  75. bool arrTst(float *inp, float *outp, uint32_t numOfE)
  76. {
  77.    
  78.     uint32_t offset = 0;
  79.    
  80.     if(offset<numOfE)
  81.     {
  82.         for(; offset<numOfE; ++offset)
  83.         {
  84.             outp[offset] = inp[offset];
  85.         }
  86.     }
  87.     else return 1;
  88.  
  89.     return 0;
  90. }
  91.  
  92. /*
  93. output:
  94.  
  95. init
  96. f_array_1:
  97. 0.0  1.0  2.0  3.0  4.0  5.0  6.0  7.0  8.0  9.0  10.0  11.0  12.0  13.0  14.0  15.0  
  98. f_array_2 (before):
  99. 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  
  100. f_array_2 (after):
  101. 0.0  1.0  2.0  3.0  4.0  5.0  6.0  7.0  8.0  9.0  10.0  11.0  12.0  13.0  14.0  15.0  
  102. fini
  103.  
  104. */
Advertisement
Add Comment
Please, Sign In to add comment