array-handle.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /* Copyright 1995-1998,2000-2006,2009,2011,2013-2014,2018
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <string.h>
  19. #include "arrays.h"
  20. #include "bitvectors.h"
  21. #include "bytevectors.h"
  22. #include "list.h"
  23. #include "numbers.h"
  24. #include "pairs.h"
  25. #include "strings.h"
  26. #include "symbols.h"
  27. #include "vectors.h"
  28. #include "array-handle.h"
  29. SCM scm_i_array_element_types[SCM_ARRAY_ELEMENT_TYPE_LAST + 1];
  30. /* Bytevectors as generalized vectors & arrays. */
  31. #define DEFINE_BYTEVECTOR_ACCESSORS(type, tag, infix) \
  32. static SCM \
  33. bytevector_##tag##_ref (SCM bv, size_t pos) \
  34. { \
  35. SCM idx = scm_from_size_t (pos * sizeof (type)); \
  36. return scm_bytevector_##infix##_ref (bv, idx); \
  37. } \
  38. static void \
  39. bytevector_##tag##_set (SCM bv, size_t pos, SCM val) \
  40. { \
  41. SCM idx = scm_from_size_t (pos * sizeof (type)); \
  42. scm_bytevector_##infix##_set_x (bv, idx, val); \
  43. }
  44. DEFINE_BYTEVECTOR_ACCESSORS (uint8_t, u8, u8);
  45. DEFINE_BYTEVECTOR_ACCESSORS (int8_t, s8, s8);
  46. DEFINE_BYTEVECTOR_ACCESSORS (uint16_t, u16, u16_native);
  47. DEFINE_BYTEVECTOR_ACCESSORS (int16_t, s16, s16_native);
  48. DEFINE_BYTEVECTOR_ACCESSORS (uint32_t, u32, u32_native);
  49. DEFINE_BYTEVECTOR_ACCESSORS (int32_t, s32, s32_native);
  50. DEFINE_BYTEVECTOR_ACCESSORS (uint64_t, u64, u64_native);
  51. DEFINE_BYTEVECTOR_ACCESSORS (int64_t, s64, s64_native);
  52. DEFINE_BYTEVECTOR_ACCESSORS (float, f32, ieee_single_native);
  53. DEFINE_BYTEVECTOR_ACCESSORS (double, f64, ieee_double_native);
  54. /* Since these functions are only called by Guile's C code, we can abort
  55. instead of throwing if there is an error. */
  56. static SCM
  57. bytevector_c32_ref (SCM bv, size_t pos)
  58. {
  59. char *c_bv;
  60. float real, imag;
  61. if (!SCM_BYTEVECTOR_P (bv))
  62. abort ();
  63. c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv);
  64. pos *= 2 * sizeof (float);
  65. if (pos + 2 * sizeof (float) - 1 >= SCM_BYTEVECTOR_LENGTH (bv))
  66. abort ();
  67. memcpy (&real, &c_bv[pos], sizeof (float));
  68. memcpy (&imag, &c_bv[pos + sizeof (float)], sizeof (float));
  69. return scm_c_make_rectangular (real, imag);
  70. }
  71. static SCM
  72. bytevector_c64_ref (SCM bv, size_t pos)
  73. {
  74. char *c_bv;
  75. double real, imag;
  76. if (!SCM_BYTEVECTOR_P (bv))
  77. abort ();
  78. c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv);
  79. pos *= 2 * sizeof (double);
  80. if (pos + 2 * sizeof (double) - 1 >= SCM_BYTEVECTOR_LENGTH (bv))
  81. abort ();
  82. memcpy (&real, &c_bv[pos], sizeof (double));
  83. memcpy (&imag, &c_bv[pos + sizeof (double)], sizeof (double));
  84. return scm_c_make_rectangular (real, imag);
  85. }
  86. static void
  87. bytevector_c32_set (SCM bv, size_t pos, SCM val)
  88. {
  89. char *c_bv;
  90. float real, imag;
  91. if (!SCM_BYTEVECTOR_P (bv))
  92. abort ();
  93. c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv);
  94. pos *= 2 * sizeof (float);
  95. if (pos + 2 * sizeof (float) - 1 >= SCM_BYTEVECTOR_LENGTH (bv))
  96. abort ();
  97. real = scm_c_real_part (val);
  98. imag = scm_c_imag_part (val);
  99. memcpy (&c_bv[pos], &real, sizeof (float));
  100. memcpy (&c_bv[pos + sizeof (float)], &imag, sizeof (float));
  101. }
  102. static void
  103. bytevector_c64_set (SCM bv, size_t pos, SCM val)
  104. {
  105. char *c_bv;
  106. double real, imag;
  107. if (!SCM_BYTEVECTOR_P (bv))
  108. abort ();
  109. c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv);
  110. pos *= 2 * sizeof (double);
  111. if (pos + 2 * sizeof (double) - 1 >= SCM_BYTEVECTOR_LENGTH (bv))
  112. abort ();
  113. real = scm_c_real_part (val);
  114. imag = scm_c_imag_part (val);
  115. memcpy (&c_bv[pos], &real, sizeof (double));
  116. memcpy (&c_bv[pos + sizeof (double)], &imag, sizeof (double));
  117. }
  118. static void
  119. initialize_vector_handle (scm_t_array_handle *h, size_t len,
  120. scm_t_array_element_type element_type,
  121. scm_t_vector_ref vref, scm_t_vector_set vset,
  122. const void *elements, int mutable_p)
  123. {
  124. h->base = 0;
  125. h->ndims = 1;
  126. h->dims = &h->dim0;
  127. h->dim0.lbnd = 0;
  128. h->dim0.ubnd = (ssize_t) (len - 1U);
  129. h->dim0.inc = 1;
  130. h->element_type = element_type;
  131. /* elements != writable_elements is used to check mutability later on.
  132. Ignore it if the array is empty. */
  133. h->elements = len==0 ? NULL : elements;
  134. h->writable_elements = mutable_p ? ((void *) h->elements) : NULL;
  135. h->vector = h->array;
  136. h->vref = vref;
  137. h->vset = vset;
  138. }
  139. void
  140. scm_array_get_handle (SCM array, scm_t_array_handle *h)
  141. {
  142. if (!SCM_HEAP_OBJECT_P (array))
  143. scm_wrong_type_arg_msg (NULL, 0, array, "array");
  144. h->array = array;
  145. switch (SCM_TYP7 (array))
  146. {
  147. case scm_tc7_string:
  148. initialize_vector_handle (h, scm_c_string_length (array),
  149. SCM_ARRAY_ELEMENT_TYPE_CHAR,
  150. scm_c_string_ref, scm_c_string_set_x,
  151. NULL,
  152. scm_i_string_is_mutable (array));
  153. break;
  154. case scm_tc7_vector:
  155. initialize_vector_handle (h, scm_c_vector_length (array),
  156. SCM_ARRAY_ELEMENT_TYPE_SCM,
  157. scm_c_vector_ref, scm_c_vector_set_x,
  158. SCM_I_VECTOR_WELTS (array),
  159. SCM_I_IS_MUTABLE_VECTOR (array));
  160. break;
  161. case scm_tc7_bitvector:
  162. initialize_vector_handle (h, scm_c_bitvector_length (array),
  163. SCM_ARRAY_ELEMENT_TYPE_BIT,
  164. scm_c_bitvector_ref, scm_c_bitvector_set_x,
  165. scm_i_bitvector_bits (array),
  166. scm_i_is_mutable_bitvector (array));
  167. break;
  168. case scm_tc7_bytevector:
  169. {
  170. size_t length;
  171. scm_t_array_element_type element_type;
  172. scm_t_vector_ref vref;
  173. scm_t_vector_set vset;
  174. element_type = SCM_BYTEVECTOR_ELEMENT_TYPE (array);
  175. length = SCM_BYTEVECTOR_TYPED_LENGTH (array);
  176. switch (element_type)
  177. {
  178. #define ACCESSOR_CASE(tag, TAG) \
  179. case SCM_ARRAY_ELEMENT_TYPE_##TAG: \
  180. vref = bytevector_##tag##_ref; \
  181. vset = bytevector_##tag##_set; \
  182. break
  183. case SCM_ARRAY_ELEMENT_TYPE_VU8:
  184. ACCESSOR_CASE(u8, U8);
  185. ACCESSOR_CASE(s8, S8);
  186. ACCESSOR_CASE(u16, U16);
  187. ACCESSOR_CASE(s16, S16);
  188. ACCESSOR_CASE(u32, U32);
  189. ACCESSOR_CASE(s32, S32);
  190. ACCESSOR_CASE(u64, U64);
  191. ACCESSOR_CASE(s64, S64);
  192. ACCESSOR_CASE(f32, F32);
  193. ACCESSOR_CASE(f64, F64);
  194. ACCESSOR_CASE(c32, C32);
  195. ACCESSOR_CASE(c64, C64);
  196. case SCM_ARRAY_ELEMENT_TYPE_SCM:
  197. case SCM_ARRAY_ELEMENT_TYPE_BIT:
  198. case SCM_ARRAY_ELEMENT_TYPE_CHAR:
  199. default:
  200. abort ();
  201. #undef ACCESSOR_CASE
  202. }
  203. initialize_vector_handle (h, length, element_type, vref, vset,
  204. SCM_BYTEVECTOR_CONTENTS (array),
  205. SCM_MUTABLE_BYTEVECTOR_P (array));
  206. }
  207. break;
  208. case scm_tc7_array:
  209. scm_array_get_handle (SCM_I_ARRAY_V (array), h);
  210. h->array = array;
  211. h->base = SCM_I_ARRAY_BASE (array);
  212. h->ndims = SCM_I_ARRAY_NDIM (array);
  213. h->dims = SCM_I_ARRAY_DIMS (array);
  214. break;
  215. default:
  216. scm_wrong_type_arg_msg (NULL, 0, array, "array");
  217. }
  218. }
  219. ssize_t
  220. scm_array_handle_pos (scm_t_array_handle *h, SCM indices)
  221. {
  222. scm_t_array_dim *s = scm_array_handle_dims (h);
  223. ssize_t pos = 0, i;
  224. size_t k = scm_array_handle_rank (h);
  225. while (k > 0 && scm_is_pair (indices))
  226. {
  227. i = scm_to_signed_integer (SCM_CAR (indices), s->lbnd, s->ubnd);
  228. pos += (i - s->lbnd) * s->inc;
  229. k--;
  230. s++;
  231. indices = SCM_CDR (indices);
  232. }
  233. if (k > 0 || !scm_is_null (indices))
  234. scm_misc_error (NULL, "wrong number of indices, expecting ~a",
  235. scm_list_1 (scm_from_size_t (scm_array_handle_rank (h))));
  236. return pos;
  237. }
  238. static void
  239. check_array_index_bounds (scm_t_array_dim *dim, ssize_t idx)
  240. {
  241. if (idx < dim->lbnd || idx > dim->ubnd)
  242. scm_error (scm_out_of_range_key, NULL, "Value out of range ~S to ~S: ~S",
  243. scm_list_3 (scm_from_ssize_t (dim->lbnd),
  244. scm_from_ssize_t (dim->ubnd),
  245. scm_from_ssize_t (idx)),
  246. scm_list_1 (scm_from_ssize_t (idx)));
  247. }
  248. ssize_t
  249. scm_array_handle_pos_1 (scm_t_array_handle *h, ssize_t idx0)
  250. {
  251. scm_t_array_dim *dim = scm_array_handle_dims (h);
  252. if (scm_array_handle_rank (h) != 1)
  253. scm_misc_error (NULL, "wrong number of indices, expecting ~A",
  254. scm_list_1 (scm_from_size_t (scm_array_handle_rank (h))));
  255. check_array_index_bounds (&dim[0], idx0);
  256. return (idx0 - dim[0].lbnd) * dim[0].inc;
  257. }
  258. ssize_t
  259. scm_array_handle_pos_2 (scm_t_array_handle *h, ssize_t idx0, ssize_t idx1)
  260. {
  261. scm_t_array_dim *dim = scm_array_handle_dims (h);
  262. if (scm_array_handle_rank (h) != 2)
  263. scm_misc_error (NULL, "wrong number of indices, expecting ~A",
  264. scm_list_1 (scm_from_size_t (scm_array_handle_rank (h))));
  265. check_array_index_bounds (&dim[0], idx0);
  266. check_array_index_bounds (&dim[1], idx1);
  267. return ((idx0 - dim[0].lbnd) * dim[0].inc
  268. + (idx1 - dim[1].lbnd) * dim[1].inc);
  269. }
  270. SCM
  271. scm_array_handle_element_type (scm_t_array_handle *h)
  272. {
  273. if (h->element_type < 0 || h->element_type > SCM_ARRAY_ELEMENT_TYPE_LAST)
  274. abort (); /* guile programming error */
  275. return scm_i_array_element_types[h->element_type];
  276. }
  277. void
  278. scm_array_handle_release (scm_t_array_handle *h)
  279. {
  280. /* Nothing to do here until arrays need to be reserved for real.
  281. */
  282. }
  283. // -----------------------------------------------
  284. // scm_array_handle_TYPE_(writable_)elements FIXME
  285. // -----------------------------------------------
  286. const SCM *
  287. scm_array_handle_elements (scm_t_array_handle *h)
  288. {
  289. if (h->element_type != SCM_ARRAY_ELEMENT_TYPE_SCM)
  290. scm_wrong_type_arg_msg (NULL, 0, h->array, "non-uniform array");
  291. return ((const SCM *) h->elements) + h->base;
  292. }
  293. SCM *
  294. scm_array_handle_writable_elements (scm_t_array_handle *h)
  295. {
  296. if (h->writable_elements != h->elements)
  297. scm_wrong_type_arg_msg (NULL, 0, h->array, "mutable array");
  298. return (SCM *) scm_array_handle_elements (h);
  299. }
  300. // -----------------------------------------------
  301. // scm_array1_TYPE_(writable_)elements FIXME
  302. // -----------------------------------------------
  303. const uint32_t *
  304. scm_array_handle_bit_elements (scm_t_array_handle *h)
  305. {
  306. if (h->element_type != SCM_ARRAY_ELEMENT_TYPE_BIT)
  307. scm_wrong_type_arg_msg (NULL, 0, h->array, "bit array");
  308. return ((const uint32_t *) h->elements) + h->base/32;
  309. }
  310. uint32_t *
  311. scm_array_handle_bit_writable_elements (scm_t_array_handle *h)
  312. {
  313. if (h->writable_elements != h->elements)
  314. scm_wrong_type_arg_msg (NULL, 0, h->array, "mutable bit array");
  315. return (uint32_t *) scm_array_handle_bit_elements (h);
  316. }
  317. size_t
  318. scm_array_handle_bit_elements_offset (scm_t_array_handle *h)
  319. {
  320. return h->base % 32;
  321. }
  322. const uint32_t *
  323. scm_array1_bit_elements (SCM vec, size_t *lenp, ssize_t *incp, size_t *offp)
  324. {
  325. scm_t_array_handle h;
  326. scm_array_get_handle (vec, &h);
  327. if (1 != scm_array_handle_rank (&h))
  328. {
  329. scm_array_handle_release (&h);
  330. scm_wrong_type_arg_msg (NULL, 0, vec, "rank 1 bit array");
  331. }
  332. else if (scm_is_bitvector (h.array))
  333. {
  334. scm_t_array_dim *dim = scm_array_handle_dims (&h);
  335. if (offp)
  336. *offp = scm_array_handle_bit_elements_offset (&h);
  337. if (lenp)
  338. *lenp = dim->ubnd - dim->lbnd + 1;
  339. if (incp)
  340. *incp = dim->inc;
  341. const uint32_t * val = ((const uint32_t *) h.elements) + h.base/32;
  342. scm_array_handle_release (&h);
  343. return val;
  344. }
  345. else
  346. {
  347. scm_array_handle_release (&h);
  348. scm_wrong_type_arg_msg (NULL, 0, vec, "rank 1 bit array");
  349. }
  350. }
  351. uint32_t *
  352. scm_array1_bit_writable_elements (SCM vec, size_t *lenp, ssize_t *incp, size_t *offp)
  353. {
  354. scm_t_array_handle h;
  355. scm_array_get_handle (vec, &h);
  356. if (1 != scm_array_handle_rank (&h))
  357. {
  358. scm_array_handle_release (&h);
  359. scm_wrong_type_arg_msg (NULL, 0, vec, "rank 1 mutable bit array");
  360. }
  361. else if (scm_i_is_mutable_bitvector (h.array))
  362. {
  363. scm_t_array_dim *dim = scm_array_handle_dims (&h);
  364. if (offp)
  365. *offp = scm_array_handle_bit_elements_offset (&h);
  366. if (lenp)
  367. *lenp = dim->ubnd - dim->lbnd + 1;
  368. if (incp)
  369. *incp = dim->inc;
  370. uint32_t * val = ((uint32_t *) h.elements) + h.base/32;
  371. scm_array_handle_release (&h);
  372. return val;
  373. }
  374. else
  375. {
  376. scm_array_handle_release (&h);
  377. scm_wrong_type_arg_msg (NULL, 0, vec, "rank 1 mutable bit array");
  378. }
  379. }
  380. void
  381. scm_init_array_handle (void)
  382. {
  383. #define DEFINE_ARRAY_TYPE(tag, TAG) \
  384. scm_i_array_element_types[SCM_ARRAY_ELEMENT_TYPE_##TAG] = scm_from_utf8_symbol (#tag)
  385. scm_i_array_element_types[SCM_ARRAY_ELEMENT_TYPE_SCM] = SCM_BOOL_T;
  386. DEFINE_ARRAY_TYPE (a, CHAR);
  387. DEFINE_ARRAY_TYPE (b, BIT);
  388. DEFINE_ARRAY_TYPE (vu8, VU8);
  389. DEFINE_ARRAY_TYPE (u8, U8);
  390. DEFINE_ARRAY_TYPE (s8, S8);
  391. DEFINE_ARRAY_TYPE (u16, U16);
  392. DEFINE_ARRAY_TYPE (s16, S16);
  393. DEFINE_ARRAY_TYPE (u32, U32);
  394. DEFINE_ARRAY_TYPE (s32, S32);
  395. DEFINE_ARRAY_TYPE (u64, U64);
  396. DEFINE_ARRAY_TYPE (s64, S64);
  397. DEFINE_ARRAY_TYPE (f32, F32);
  398. DEFINE_ARRAY_TYPE (f64, F64);
  399. DEFINE_ARRAY_TYPE (c32, C32);
  400. DEFINE_ARRAY_TYPE (c64, C64);
  401. #include "array-handle.x"
  402. }