array-handle.h 4.4 KB

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