array-handle.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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
  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. typedef SCM (*scm_t_vector_ref) (SCM, size_t);
  21. typedef void (*scm_t_vector_set) (SCM, size_t, SCM);
  22. typedef struct scm_t_array_dim
  23. {
  24. ssize_t lbnd;
  25. ssize_t ubnd;
  26. ssize_t inc;
  27. } scm_t_array_dim;
  28. typedef enum
  29. {
  30. SCM_ARRAY_ELEMENT_TYPE_SCM = 0, /* SCM values */
  31. SCM_ARRAY_ELEMENT_TYPE_CHAR = 1, /* characters */
  32. SCM_ARRAY_ELEMENT_TYPE_BIT = 2, /* packed numeric values */
  33. SCM_ARRAY_ELEMENT_TYPE_VU8 = 3,
  34. SCM_ARRAY_ELEMENT_TYPE_U8 = 4,
  35. SCM_ARRAY_ELEMENT_TYPE_S8 = 5,
  36. SCM_ARRAY_ELEMENT_TYPE_U16 = 6,
  37. SCM_ARRAY_ELEMENT_TYPE_S16 = 7,
  38. SCM_ARRAY_ELEMENT_TYPE_U32 = 8,
  39. SCM_ARRAY_ELEMENT_TYPE_S32 = 9,
  40. SCM_ARRAY_ELEMENT_TYPE_U64 = 10,
  41. SCM_ARRAY_ELEMENT_TYPE_S64 = 11,
  42. SCM_ARRAY_ELEMENT_TYPE_F32 = 12,
  43. SCM_ARRAY_ELEMENT_TYPE_F64 = 13,
  44. SCM_ARRAY_ELEMENT_TYPE_C32 = 14,
  45. SCM_ARRAY_ELEMENT_TYPE_C64 = 15,
  46. SCM_ARRAY_ELEMENT_TYPE_LAST = 15
  47. } scm_t_array_element_type;
  48. SCM_INTERNAL SCM scm_i_array_element_types[];
  49. typedef struct scm_t_array_handle {
  50. SCM array;
  51. /* `Base' is an offset into elements or writable_elements, corresponding to
  52. the first element in the array. It would be nicer just to adjust the
  53. elements/writable_elements pointer, but we can't because that element might
  54. not even be byte-addressable, as is the case with bitvectors. A nicer
  55. solution would be, well, nice.
  56. */
  57. size_t base;
  58. size_t ndims; /* the rank of the array */
  59. scm_t_array_dim *dims;
  60. scm_t_array_dim dim0;
  61. scm_t_array_element_type element_type;
  62. const void *elements;
  63. void *writable_elements;
  64. /* The backing store for the array, and its accessors. */
  65. SCM vector;
  66. scm_t_vector_ref vref;
  67. scm_t_vector_set vset;
  68. } scm_t_array_handle;
  69. #define scm_array_handle_rank(h) ((h)->ndims)
  70. #define scm_array_handle_dims(h) ((h)->dims)
  71. SCM_API void scm_array_get_handle (SCM array, scm_t_array_handle *h);
  72. SCM_API ssize_t scm_array_handle_pos (scm_t_array_handle *h, SCM indices);
  73. SCM_API ssize_t scm_array_handle_pos_1 (scm_t_array_handle *h, ssize_t idx0);
  74. SCM_API ssize_t scm_array_handle_pos_2 (scm_t_array_handle *h, ssize_t idx0, ssize_t idx1);
  75. SCM_API SCM scm_array_handle_element_type (scm_t_array_handle *h);
  76. SCM_API void scm_array_handle_release (scm_t_array_handle *h);
  77. SCM_API const SCM* scm_array_handle_elements (scm_t_array_handle *h);
  78. SCM_API SCM* scm_array_handle_writable_elements (scm_t_array_handle *h);
  79. SCM_INLINE SCM scm_array_handle_ref (scm_t_array_handle *h, ssize_t pos);
  80. SCM_INLINE void scm_array_handle_set (scm_t_array_handle *h, ssize_t pos, SCM val);
  81. #if SCM_CAN_INLINE || defined SCM_INLINE_C_IMPLEMENTING_INLINES
  82. /* Either inlining, or being included from inline.c. */
  83. SCM_INLINE_IMPLEMENTATION SCM
  84. scm_array_handle_ref (scm_t_array_handle *h, ssize_t p)
  85. {
  86. if (SCM_UNLIKELY (p < 0 && ((size_t)-p) > h->base))
  87. /* catch overflow */
  88. scm_out_of_range (NULL, scm_from_ssize_t (p));
  89. /* perhaps should catch overflow here too */
  90. return h->vref (h->vector, h->base + p);
  91. }
  92. SCM_INLINE_IMPLEMENTATION void
  93. scm_array_handle_set (scm_t_array_handle *h, ssize_t p, SCM v)
  94. {
  95. if (SCM_UNLIKELY (p < 0 && ((size_t)-p) > h->base))
  96. /* catch overflow */
  97. scm_out_of_range (NULL, scm_from_ssize_t (p));
  98. /* perhaps should catch overflow here too */
  99. h->vset (h->vector, h->base + p, v);
  100. }
  101. #endif
  102. SCM_INTERNAL void scm_init_array_handle (void);
  103. SCM_API const uint32_t *scm_array_handle_bit_elements (scm_t_array_handle *h);
  104. SCM_API uint32_t *scm_array_handle_bit_writable_elements (scm_t_array_handle *h);
  105. SCM_API size_t scm_array_handle_bit_elements_offset (scm_t_array_handle *h);
  106. SCM_API const uint32_t * scm_array1_bit_elements (SCM vec, size_t *lenp, ssize_t *incp, size_t *offp);
  107. SCM_API uint32_t * scm_array1_bit_writable_elements (SCM vec, size_t *lenp, ssize_t *incp, size_t *offp);
  108. #endif /* SCM_ARRAY_HANDLE_H */