array-handle.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 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. struct scm_t_array_handle;
  26. typedef SCM (*scm_i_t_array_ref) (SCM, size_t);
  27. typedef void (*scm_i_t_array_set) (SCM, size_t, SCM);
  28. typedef struct
  29. {
  30. scm_t_bits tag;
  31. scm_t_bits mask;
  32. scm_i_t_array_ref vref;
  33. scm_i_t_array_set vset;
  34. void (*get_handle)(SCM, struct scm_t_array_handle*);
  35. } scm_t_array_implementation;
  36. #define SCM_ARRAY_IMPLEMENTATION(tag_,mask_,vref_,vset_,handle_) \
  37. SCM_SNARF_INIT ({ \
  38. scm_t_array_implementation impl; \
  39. impl.tag = tag_; impl.mask = mask_; \
  40. impl.vref = vref_; impl.vset = vset_; \
  41. impl.get_handle = handle_; \
  42. scm_i_register_array_implementation (&impl); \
  43. })
  44. SCM_INTERNAL void scm_i_register_array_implementation (scm_t_array_implementation *impl);
  45. SCM_INTERNAL scm_t_array_implementation* scm_i_array_implementation_for_obj (SCM obj);
  46. typedef struct scm_t_array_dim
  47. {
  48. ssize_t lbnd;
  49. ssize_t ubnd;
  50. ssize_t inc;
  51. } scm_t_array_dim;
  52. typedef enum
  53. {
  54. SCM_ARRAY_ELEMENT_TYPE_SCM = 0, /* SCM values */
  55. SCM_ARRAY_ELEMENT_TYPE_CHAR = 1, /* characters */
  56. SCM_ARRAY_ELEMENT_TYPE_BIT = 2, /* packed numeric values */
  57. SCM_ARRAY_ELEMENT_TYPE_VU8 = 3,
  58. SCM_ARRAY_ELEMENT_TYPE_U8 = 4,
  59. SCM_ARRAY_ELEMENT_TYPE_S8 = 5,
  60. SCM_ARRAY_ELEMENT_TYPE_U16 = 6,
  61. SCM_ARRAY_ELEMENT_TYPE_S16 = 7,
  62. SCM_ARRAY_ELEMENT_TYPE_U32 = 8,
  63. SCM_ARRAY_ELEMENT_TYPE_S32 = 9,
  64. SCM_ARRAY_ELEMENT_TYPE_U64 = 10,
  65. SCM_ARRAY_ELEMENT_TYPE_S64 = 11,
  66. SCM_ARRAY_ELEMENT_TYPE_F32 = 12,
  67. SCM_ARRAY_ELEMENT_TYPE_F64 = 13,
  68. SCM_ARRAY_ELEMENT_TYPE_C32 = 14,
  69. SCM_ARRAY_ELEMENT_TYPE_C64 = 15,
  70. SCM_ARRAY_ELEMENT_TYPE_LAST = 15
  71. } scm_t_array_element_type;
  72. SCM_INTERNAL SCM scm_i_array_element_types[];
  73. typedef struct scm_t_array_handle {
  74. SCM root;
  75. scm_t_array_implementation *impl;
  76. /* `Base' is an offset into elements or writable_elements, corresponding to
  77. the first element in the array. It would be nicer just to adjust the
  78. elements/writable_elements pointer, but we can't because that element might
  79. not even be byte-addressable, as is the case with bitvectors. A nicer
  80. solution would be, well, nice.
  81. */
  82. size_t base;
  83. size_t ndims; /* ndims == the rank of the array */
  84. scm_t_array_dim *dims;
  85. scm_t_array_dim dim0;
  86. scm_t_array_element_type element_type;
  87. const void *elements;
  88. void *writable_elements;
  89. } scm_t_array_handle;
  90. #define scm_array_handle_rank(h) ((h)->ndims)
  91. #define scm_array_handle_dims(h) ((h)->dims)
  92. SCM_API void scm_array_get_handle (SCM array, scm_t_array_handle *h);
  93. SCM_API ssize_t scm_array_handle_pos (scm_t_array_handle *h, SCM indices);
  94. SCM_API ssize_t scm_array_handle_pos_1 (scm_t_array_handle *h, ssize_t idx0);
  95. SCM_API ssize_t scm_array_handle_pos_2 (scm_t_array_handle *h, ssize_t idx0, ssize_t idx1);
  96. SCM_API SCM scm_array_handle_element_type (scm_t_array_handle *h);
  97. SCM_API void scm_array_handle_release (scm_t_array_handle *h);
  98. SCM_API const SCM* scm_array_handle_elements (scm_t_array_handle *h);
  99. SCM_API SCM* scm_array_handle_writable_elements (scm_t_array_handle *h);
  100. SCM_INLINE SCM scm_array_handle_ref (scm_t_array_handle *h, ssize_t pos);
  101. SCM_INLINE void scm_array_handle_set (scm_t_array_handle *h, ssize_t pos, SCM val);
  102. #if SCM_CAN_INLINE || defined SCM_INLINE_C_IMPLEMENTING_INLINES
  103. /* Either inlining, or being included from inline.c. */
  104. SCM_INLINE_IMPLEMENTATION SCM
  105. scm_array_handle_ref (scm_t_array_handle *h, ssize_t p)
  106. {
  107. if (SCM_UNLIKELY (p < 0 && ((size_t)-p) > h->base))
  108. /* catch overflow */
  109. scm_out_of_range (NULL, scm_from_ssize_t (p));
  110. /* perhaps should catch overflow here too */
  111. return h->impl->vref (h->root, h->base + p);
  112. }
  113. SCM_INLINE_IMPLEMENTATION void
  114. scm_array_handle_set (scm_t_array_handle *h, ssize_t p, SCM v)
  115. {
  116. if (SCM_UNLIKELY (p < 0 && ((size_t)-p) > h->base))
  117. /* catch overflow */
  118. scm_out_of_range (NULL, scm_from_ssize_t (p));
  119. /* perhaps should catch overflow here too */
  120. h->impl->vset (h->root, h->base + p, v);
  121. }
  122. #endif
  123. SCM_INTERNAL void scm_init_array_handle (void);
  124. #endif /* SCM_ARRAY_HANDLE_H */
  125. /*
  126. Local Variables:
  127. c-file-style: "gnu"
  128. End:
  129. */