srfi-4.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /* srfi-4.c --- Uniform numeric vector datatypes.
  2. Copyright 2001,2004,2006,2009-2011,2014,2018
  3. Free Software Foundation, Inc.
  4. This file is part of Guile.
  5. Guile is free software: you can redistribute it and/or modify it
  6. under the terms of the GNU Lesser General Public License as published
  7. by the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. Guile is distributed in the hope that it will be useful, but WITHOUT
  10. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  12. License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with Guile. If not, see
  15. <https://www.gnu.org/licenses/>. */
  16. #ifdef HAVE_CONFIG_H
  17. # include <config.h>
  18. #endif
  19. #include <string.h>
  20. #include "bdw-gc.h"
  21. #include "boolean.h"
  22. #include "bytevectors.h"
  23. #include "error.h"
  24. #include "eval.h"
  25. #include "extensions.h"
  26. #include "gsubr.h"
  27. #include "modules.h"
  28. #include "numbers.h"
  29. #include "uniform.h"
  30. #include "variable.h"
  31. #include "srfi-4.h"
  32. #define DEFINE_SCHEME_PROXY100(cname, modname, scmname) \
  33. SCM cname (SCM arg1) \
  34. { \
  35. static SCM var = SCM_BOOL_F; \
  36. if (scm_is_false (var)) \
  37. var = scm_c_module_lookup (scm_c_resolve_module (modname), scmname); \
  38. return scm_call_1 (SCM_VARIABLE_REF (var), arg1); \
  39. }
  40. #define DEFINE_SCHEME_PROXY001(cname, modname, scmname) \
  41. SCM cname (SCM args) \
  42. { \
  43. static SCM var = SCM_BOOL_F; \
  44. if (scm_is_false (var)) \
  45. var = scm_c_module_lookup (scm_c_resolve_module (modname), scmname); \
  46. return scm_apply_0 (SCM_VARIABLE_REF (var), args); \
  47. }
  48. #define DEFINE_SCHEME_PROXY110(cname, modname, scmname) \
  49. SCM cname (SCM arg1, SCM opt1) \
  50. { \
  51. static SCM var = SCM_BOOL_F; \
  52. if (scm_is_false (var)) \
  53. var = scm_c_module_lookup (scm_c_resolve_module (modname), scmname); \
  54. if (SCM_UNBNDP (opt1)) \
  55. return scm_call_1 (SCM_VARIABLE_REF (var), arg1); \
  56. else \
  57. return scm_call_2 (SCM_VARIABLE_REF (var), arg1, opt1); \
  58. }
  59. #define DEFINE_SCHEME_PROXY200(cname, modname, scmname) \
  60. SCM cname (SCM arg1, SCM arg2) \
  61. { \
  62. static SCM var = SCM_BOOL_F; \
  63. if (scm_is_false (var)) \
  64. var = scm_c_module_lookup (scm_c_resolve_module (modname), scmname); \
  65. return scm_call_2 (SCM_VARIABLE_REF (var), arg1, arg2); \
  66. }
  67. #define DEFINE_SCHEME_PROXY300(cname, modname, scmname) \
  68. SCM cname (SCM arg1, SCM arg2, SCM arg3) \
  69. { \
  70. static SCM var = SCM_BOOL_F; \
  71. if (scm_is_false (var)) \
  72. var = scm_c_module_lookup (scm_c_resolve_module (modname), scmname); \
  73. return scm_call_3 (SCM_VARIABLE_REF (var), arg1, arg2, arg3); \
  74. }
  75. #define DEFPROXY100(cname, scmname) \
  76. DEFINE_SCHEME_PROXY100 (cname, MOD, scmname)
  77. #define DEFPROXY110(cname, scmname) \
  78. DEFINE_SCHEME_PROXY110 (cname, MOD, scmname)
  79. #define DEFPROXY001(cname, scmname) \
  80. DEFINE_SCHEME_PROXY001 (cname, MOD, scmname)
  81. #define DEFPROXY200(cname, scmname) \
  82. DEFINE_SCHEME_PROXY200 (cname, MOD, scmname)
  83. #define DEFPROXY300(cname, scmname) \
  84. DEFINE_SCHEME_PROXY300 (cname, MOD, scmname)
  85. #define DEFVECT(sym, str, func)\
  86. #define DEFINE_SRFI_4_PROXIES(tag) \
  87. DEFPROXY100 (scm_##tag##vector_p, #tag "vector?"); \
  88. DEFPROXY110 (scm_make_##tag##vector, "make-" #tag "vector"); \
  89. DEFPROXY001 (scm_##tag##vector, #tag "vector"); \
  90. DEFPROXY100 (scm_##tag##vector_length, #tag "vector-length"); \
  91. DEFPROXY200 (scm_##tag##vector_ref, #tag "vector-ref"); \
  92. DEFPROXY300 (scm_##tag##vector_set_x, #tag "vector-set!"); \
  93. DEFPROXY100 (scm_list_to_##tag##vector, "list->"#tag "vector"); \
  94. DEFPROXY100 (scm_##tag##vector_to_list, #tag "vector->list"); \
  95. #define ETYPE(TAG) \
  96. SCM_ARRAY_ELEMENT_TYPE_##TAG
  97. #define DEFINE_SRFI_4_C_FUNCS(TAG, tag, ctype, width) \
  98. SCM scm_take_##tag##vector (ctype *data, size_t n) \
  99. { \
  100. return scm_c_take_typed_bytevector ((int8_t*)data, n, ETYPE (TAG), \
  101. SCM_BOOL_F); \
  102. } \
  103. const ctype* scm_array_handle_##tag##_elements (scm_t_array_handle *h) \
  104. { \
  105. if (h->element_type != ETYPE (TAG)) \
  106. scm_wrong_type_arg_msg (NULL, 0, h->array, #tag "vector"); \
  107. return ((const ctype *) h->elements) + h->base*width; \
  108. } \
  109. ctype* scm_array_handle_##tag##_writable_elements (scm_t_array_handle *h) \
  110. { \
  111. if (h->writable_elements != h->elements) \
  112. scm_wrong_type_arg_msg (NULL, 0, h->array, "mutable " #tag "vector"); \
  113. return (ctype *) scm_array_handle_##tag##_elements (h); \
  114. } \
  115. const ctype *scm_##tag##vector_elements (SCM uvec, size_t *lenp) \
  116. { \
  117. size_t byte_width = width * sizeof (ctype); \
  118. if (!scm_is_bytevector (uvec) \
  119. || (scm_c_bytevector_length (uvec) % byte_width)) \
  120. scm_wrong_type_arg_msg (NULL, 0, uvec, #tag "vector"); \
  121. if (lenp) \
  122. *lenp = SCM_BYTEVECTOR_LENGTH (uvec) / byte_width; \
  123. return ((const ctype *) SCM_BYTEVECTOR_CONTENTS(uvec)); \
  124. } \
  125. ctype *scm_##tag##vector_writable_elements (SCM uvec, size_t *lenp) \
  126. { \
  127. const ctype *ret = scm_##tag##vector_elements (uvec, lenp); \
  128. if (!SCM_MUTABLE_BYTEVECTOR_P (uvec)) \
  129. scm_wrong_type_arg_msg (NULL, 0, uvec, "mutable " #tag "vector"); \
  130. return (ctype *) ret; \
  131. }
  132. #define MOD "srfi srfi-4"
  133. DEFINE_SRFI_4_PROXIES (u8);
  134. DEFINE_SRFI_4_C_FUNCS (U8, u8, uint8_t, 1);
  135. DEFINE_SRFI_4_PROXIES (s8);
  136. DEFINE_SRFI_4_C_FUNCS (S8, s8, int8_t, 1);
  137. DEFINE_SRFI_4_PROXIES (u16);
  138. DEFINE_SRFI_4_C_FUNCS (U16, u16, uint16_t, 1);
  139. DEFINE_SRFI_4_PROXIES (s16);
  140. DEFINE_SRFI_4_C_FUNCS (S16, s16, int16_t, 1);
  141. DEFINE_SRFI_4_PROXIES (u32);
  142. DEFINE_SRFI_4_C_FUNCS (U32, u32, uint32_t, 1);
  143. DEFINE_SRFI_4_PROXIES (s32);
  144. DEFINE_SRFI_4_C_FUNCS (S32, s32, int32_t, 1);
  145. DEFINE_SRFI_4_PROXIES (u64);
  146. DEFINE_SRFI_4_C_FUNCS (U64, u64, uint64_t, 1);
  147. DEFINE_SRFI_4_PROXIES (s64);
  148. DEFINE_SRFI_4_C_FUNCS (S64, s64, int64_t, 1);
  149. DEFINE_SRFI_4_PROXIES (f32);
  150. DEFINE_SRFI_4_C_FUNCS (F32, f32, float, 1);
  151. DEFINE_SRFI_4_PROXIES (f64);
  152. DEFINE_SRFI_4_C_FUNCS (F64, f64, double, 1);
  153. #undef MOD
  154. #define MOD "srfi srfi-4 gnu"
  155. DEFINE_SRFI_4_PROXIES (c32);
  156. DEFINE_SRFI_4_C_FUNCS (C32, c32, float, 2);
  157. DEFINE_SRFI_4_PROXIES (c64);
  158. DEFINE_SRFI_4_C_FUNCS (C64, c64, double, 2);
  159. #define DEFINE_SRFI_4_GNU_PROXIES(tag) \
  160. DEFPROXY100 (scm_any_to_##tag##vector, "any->" #tag "vector")
  161. #undef MOD
  162. #define MOD "srfi srfi-4 gnu"
  163. DEFINE_SRFI_4_GNU_PROXIES (u8);
  164. DEFINE_SRFI_4_GNU_PROXIES (s8);
  165. DEFINE_SRFI_4_GNU_PROXIES (u16);
  166. DEFINE_SRFI_4_GNU_PROXIES (s16);
  167. DEFINE_SRFI_4_GNU_PROXIES (u32);
  168. DEFINE_SRFI_4_GNU_PROXIES (s32);
  169. DEFINE_SRFI_4_GNU_PROXIES (u64);
  170. DEFINE_SRFI_4_GNU_PROXIES (s64);
  171. DEFINE_SRFI_4_GNU_PROXIES (f32);
  172. DEFINE_SRFI_4_GNU_PROXIES (f64);
  173. DEFINE_SRFI_4_GNU_PROXIES (c32);
  174. DEFINE_SRFI_4_GNU_PROXIES (c64);
  175. SCM_DEFINE (scm_make_srfi_4_vector, "make-srfi-4-vector", 2, 1, 0,
  176. (SCM type, SCM len, SCM fill),
  177. "Make a srfi-4 vector")
  178. #define FUNC_NAME s_scm_make_srfi_4_vector
  179. {
  180. int c_type;
  181. size_t c_len;
  182. for (c_type = 0; c_type <= SCM_ARRAY_ELEMENT_TYPE_LAST; c_type++)
  183. if (scm_is_eq (type, scm_i_array_element_types[c_type]))
  184. break;
  185. if (c_type > SCM_ARRAY_ELEMENT_TYPE_LAST)
  186. scm_wrong_type_arg_msg (FUNC_NAME, SCM_ARG1, type, "vector type");
  187. switch (c_type)
  188. {
  189. case SCM_ARRAY_ELEMENT_TYPE_U8:
  190. case SCM_ARRAY_ELEMENT_TYPE_S8:
  191. case SCM_ARRAY_ELEMENT_TYPE_U16:
  192. case SCM_ARRAY_ELEMENT_TYPE_S16:
  193. case SCM_ARRAY_ELEMENT_TYPE_U32:
  194. case SCM_ARRAY_ELEMENT_TYPE_S32:
  195. case SCM_ARRAY_ELEMENT_TYPE_U64:
  196. case SCM_ARRAY_ELEMENT_TYPE_S64:
  197. case SCM_ARRAY_ELEMENT_TYPE_F32:
  198. case SCM_ARRAY_ELEMENT_TYPE_F64:
  199. case SCM_ARRAY_ELEMENT_TYPE_C32:
  200. case SCM_ARRAY_ELEMENT_TYPE_C64:
  201. {
  202. SCM ret;
  203. c_len = scm_to_size_t (len);
  204. ret = scm_i_make_typed_bytevector (c_len, c_type);
  205. if (SCM_UNBNDP (fill) || scm_is_eq (len, SCM_INUM0))
  206. ; /* pass */
  207. else if (scm_is_true (scm_zero_p (fill)))
  208. memset (SCM_BYTEVECTOR_CONTENTS (ret), 0,
  209. SCM_BYTEVECTOR_LENGTH (ret));
  210. else
  211. {
  212. scm_t_array_handle h;
  213. size_t i;
  214. scm_array_get_handle (ret, &h);
  215. for (i = 0; i < c_len; i++)
  216. scm_array_handle_set (&h, i, fill);
  217. scm_array_handle_release (&h);
  218. }
  219. return ret;
  220. }
  221. default:
  222. scm_wrong_type_arg_msg (FUNC_NAME, SCM_ARG1, type, "uniform vector type");
  223. return SCM_BOOL_F; /* not reached */
  224. }
  225. }
  226. #undef FUNC_NAME
  227. void
  228. scm_init_srfi_4 (void)
  229. {
  230. #include "srfi-4.x"
  231. }
  232. /* End of srfi-4.c. */