array-handle.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #ifndef SCM_ARRAY_HANDLE_H
  2. #define SCM_ARRAY_HANDLE_H
  3. /* Copyright 1995-1997,1999-2001,2004,2006,2008-2009,2011,2013-2014,2018,2021
  4. Free Software Foundation, Inc.
  5. This file is part of Guile.
  6. Guile is free software: you can redistribute it and/or modify it
  7. under the terms of the GNU Lesser General Public License as published
  8. by the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Guile is distributed in the hope that it will be useful, but WITHOUT
  11. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  13. License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with Guile. If not, see
  16. <https://www.gnu.org/licenses/>. */
  17. #include "libguile/error.h"
  18. #include "libguile/inline.h"
  19. #include "libguile/numbers.h"
  20. #include "libguile/arrays.h"
  21. typedef SCM (*scm_t_vector_ref) (SCM, size_t);
  22. typedef void (*scm_t_vector_set) (SCM, size_t, SCM);
  23. typedef enum
  24. {
  25. SCM_ARRAY_ELEMENT_TYPE_SCM = 0, /* SCM values */
  26. SCM_ARRAY_ELEMENT_TYPE_CHAR = 1, /* characters */
  27. SCM_ARRAY_ELEMENT_TYPE_BIT = 2, /* packed numeric values */
  28. SCM_ARRAY_ELEMENT_TYPE_VU8 = 3,
  29. SCM_ARRAY_ELEMENT_TYPE_U8 = 4,
  30. SCM_ARRAY_ELEMENT_TYPE_S8 = 5,
  31. SCM_ARRAY_ELEMENT_TYPE_U16 = 6,
  32. SCM_ARRAY_ELEMENT_TYPE_S16 = 7,
  33. SCM_ARRAY_ELEMENT_TYPE_U32 = 8,
  34. SCM_ARRAY_ELEMENT_TYPE_S32 = 9,
  35. SCM_ARRAY_ELEMENT_TYPE_U64 = 10,
  36. SCM_ARRAY_ELEMENT_TYPE_S64 = 11,
  37. SCM_ARRAY_ELEMENT_TYPE_F32 = 12,
  38. SCM_ARRAY_ELEMENT_TYPE_F64 = 13,
  39. SCM_ARRAY_ELEMENT_TYPE_C32 = 14,
  40. SCM_ARRAY_ELEMENT_TYPE_C64 = 15,
  41. SCM_ARRAY_ELEMENT_TYPE_LAST = 15
  42. } scm_t_array_element_type;
  43. SCM_INTERNAL SCM scm_i_array_element_types[];
  44. typedef struct scm_t_array_handle {
  45. SCM array;
  46. /* `Base' is an offset into elements or writable_elements, corresponding to
  47. the first element in the array. It would be nicer just to adjust the
  48. elements/writable_elements pointer, but we can't because that element might
  49. not even be byte-addressable, as is the case with bitvectors. A nicer
  50. solution would be, well, nice.
  51. */
  52. size_t base;
  53. size_t ndims; /* ndims == the rank of the array */
  54. scm_t_array_dim *dims;
  55. scm_t_array_dim dim0;
  56. scm_t_array_element_type element_type;
  57. const void *elements;
  58. void *writable_elements;
  59. /* The backing store for the array, and its accessors. */
  60. SCM vector;
  61. scm_t_vector_ref vref;
  62. scm_t_vector_set vset;
  63. } scm_t_array_handle;
  64. #define scm_array_handle_rank(h) ((h)->ndims)
  65. #define scm_array_handle_dims(h) ((h)->dims)
  66. SCM_API void scm_array_get_handle (SCM array, scm_t_array_handle *h);
  67. SCM_API ssize_t scm_array_handle_pos (scm_t_array_handle *h, SCM indices);
  68. SCM_API ssize_t scm_array_handle_pos_1 (scm_t_array_handle *h, ssize_t idx0);
  69. SCM_API ssize_t scm_array_handle_pos_2 (scm_t_array_handle *h, ssize_t idx0, ssize_t idx1);
  70. SCM_API SCM scm_array_handle_element_type (scm_t_array_handle *h);
  71. SCM_API void scm_array_handle_release (scm_t_array_handle *h);
  72. SCM_API const SCM* scm_array_handle_elements (scm_t_array_handle *h);
  73. SCM_API SCM* scm_array_handle_writable_elements (scm_t_array_handle *h);
  74. SCM_INLINE SCM scm_array_handle_ref (scm_t_array_handle *h, ssize_t pos);
  75. SCM_INLINE void scm_array_handle_set (scm_t_array_handle *h, ssize_t pos, SCM val);
  76. #if SCM_CAN_INLINE || defined SCM_INLINE_C_IMPLEMENTING_INLINES
  77. /* Either inlining, or being included from inline.c. */
  78. SCM_INLINE_IMPLEMENTATION SCM
  79. scm_array_handle_ref (scm_t_array_handle *h, ssize_t p)
  80. {
  81. if (SCM_UNLIKELY (p < 0 && ((size_t)-p) > h->base))
  82. /* catch overflow */
  83. scm_out_of_range (NULL, scm_from_ssize_t (p));
  84. /* perhaps should catch overflow here too */
  85. return h->vref (h->vector, h->base + p);
  86. }
  87. SCM_INLINE_IMPLEMENTATION void
  88. scm_array_handle_set (scm_t_array_handle *h, ssize_t p, SCM v)
  89. {
  90. if (SCM_UNLIKELY (p < 0 && ((size_t)-p) > h->base))
  91. /* catch overflow */
  92. scm_out_of_range (NULL, scm_from_ssize_t (p));
  93. /* perhaps should catch overflow here too */
  94. h->vset (h->vector, h->base + p, v);
  95. }
  96. #endif
  97. SCM_INTERNAL void scm_init_array_handle (void);
  98. #endif /* SCM_ARRAY_HANDLE_H */