srfi-4.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 "generalized-vectors.h"
  27. #include "gsubr.h"
  28. #include "modules.h"
  29. #include "numbers.h"
  30. #include "uniform.h"
  31. #include "variable.h"
  32. #include "srfi-4.h"
  33. #define DEFINE_SCHEME_PROXY100(cname, modname, scmname) \
  34. SCM cname (SCM arg1) \
  35. { \
  36. static SCM var = SCM_BOOL_F; \
  37. if (scm_is_false (var)) \
  38. var = scm_c_module_lookup (scm_c_resolve_module (modname), scmname); \
  39. return scm_call_1 (SCM_VARIABLE_REF (var), arg1); \
  40. }
  41. #define DEFINE_SCHEME_PROXY001(cname, modname, scmname) \
  42. SCM cname (SCM args) \
  43. { \
  44. static SCM var = SCM_BOOL_F; \
  45. if (scm_is_false (var)) \
  46. var = scm_c_module_lookup (scm_c_resolve_module (modname), scmname); \
  47. return scm_apply_0 (SCM_VARIABLE_REF (var), args); \
  48. }
  49. #define DEFINE_SCHEME_PROXY110(cname, modname, scmname) \
  50. SCM cname (SCM arg1, SCM opt1) \
  51. { \
  52. static SCM var = SCM_BOOL_F; \
  53. if (scm_is_false (var)) \
  54. var = scm_c_module_lookup (scm_c_resolve_module (modname), scmname); \
  55. if (SCM_UNBNDP (opt1)) \
  56. return scm_call_1 (SCM_VARIABLE_REF (var), arg1); \
  57. else \
  58. return scm_call_2 (SCM_VARIABLE_REF (var), arg1, opt1); \
  59. }
  60. #define DEFINE_SCHEME_PROXY200(cname, modname, scmname) \
  61. SCM cname (SCM arg1, SCM arg2) \
  62. { \
  63. static SCM var = SCM_BOOL_F; \
  64. if (scm_is_false (var)) \
  65. var = scm_c_module_lookup (scm_c_resolve_module (modname), scmname); \
  66. return scm_call_2 (SCM_VARIABLE_REF (var), arg1, arg2); \
  67. }
  68. #define DEFINE_SCHEME_PROXY300(cname, modname, scmname) \
  69. SCM cname (SCM arg1, SCM arg2, SCM arg3) \
  70. { \
  71. static SCM var = SCM_BOOL_F; \
  72. if (scm_is_false (var)) \
  73. var = scm_c_module_lookup (scm_c_resolve_module (modname), scmname); \
  74. return scm_call_3 (SCM_VARIABLE_REF (var), arg1, arg2, arg3); \
  75. }
  76. #define DEFPROXY100(cname, scmname) \
  77. DEFINE_SCHEME_PROXY100 (cname, MOD, scmname)
  78. #define DEFPROXY110(cname, scmname) \
  79. DEFINE_SCHEME_PROXY110 (cname, MOD, scmname)
  80. #define DEFPROXY001(cname, scmname) \
  81. DEFINE_SCHEME_PROXY001 (cname, MOD, scmname)
  82. #define DEFPROXY200(cname, scmname) \
  83. DEFINE_SCHEME_PROXY200 (cname, MOD, scmname)
  84. #define DEFPROXY300(cname, scmname) \
  85. DEFINE_SCHEME_PROXY300 (cname, MOD, scmname)
  86. #define DEFVECT(sym, str, func)\
  87. #define DEFINE_SRFI_4_PROXIES(tag) \
  88. DEFPROXY100 (scm_##tag##vector_p, #tag "vector?"); \
  89. DEFPROXY110 (scm_make_##tag##vector, "make-" #tag "vector"); \
  90. DEFPROXY001 (scm_##tag##vector, #tag "vector"); \
  91. DEFPROXY100 (scm_##tag##vector_length, #tag "vector-length"); \
  92. DEFPROXY200 (scm_##tag##vector_ref, #tag "vector-ref"); \
  93. DEFPROXY300 (scm_##tag##vector_set_x, #tag "vector-set!"); \
  94. DEFPROXY100 (scm_list_to_##tag##vector, "list->"#tag "vector"); \
  95. DEFPROXY100 (scm_##tag##vector_to_list, #tag "vector->list"); \
  96. #define ETYPE(TAG) \
  97. SCM_ARRAY_ELEMENT_TYPE_##TAG
  98. #define DEFINE_SRFI_4_C_FUNCS(TAG, tag, ctype, width) \
  99. SCM scm_take_##tag##vector (ctype *data, size_t n) \
  100. { \
  101. return scm_c_take_typed_bytevector ((int8_t*)data, n, ETYPE (TAG), \
  102. SCM_BOOL_F); \
  103. } \
  104. const ctype* scm_array_handle_##tag##_elements (scm_t_array_handle *h) \
  105. { \
  106. if (h->element_type != ETYPE (TAG)) \
  107. scm_wrong_type_arg_msg (NULL, 0, h->array, #tag "vector"); \
  108. return ((const ctype *) h->elements) + h->base*width; \
  109. } \
  110. ctype* scm_array_handle_##tag##_writable_elements (scm_t_array_handle *h) \
  111. { \
  112. if (h->writable_elements != h->elements) \
  113. scm_wrong_type_arg_msg (NULL, 0, h->array, "mutable " #tag "vector"); \
  114. return (ctype *) scm_array_handle_##tag##_elements (h); \
  115. } \
  116. const ctype *scm_##tag##vector_elements (SCM uvec, \
  117. scm_t_array_handle *h, \
  118. size_t *lenp, ssize_t *incp) \
  119. { \
  120. size_t byte_width = width * sizeof (ctype); \
  121. if (!scm_is_bytevector (uvec) \
  122. || (scm_c_bytevector_length (uvec) % byte_width)) \
  123. scm_wrong_type_arg_msg (NULL, 0, uvec, #tag "vector"); \
  124. scm_array_get_handle (uvec, h); \
  125. if (lenp) \
  126. *lenp = scm_c_bytevector_length (uvec) / byte_width; \
  127. if (incp) \
  128. *incp = 1; \
  129. return ((const ctype *) h->elements); \
  130. } \
  131. ctype *scm_##tag##vector_writable_elements (SCM uvec, \
  132. scm_t_array_handle *h, \
  133. size_t *lenp, ssize_t *incp) \
  134. { \
  135. const ctype *ret = scm_##tag##vector_elements (uvec, h, lenp, incp);\
  136. if (h->writable_elements != h->elements) \
  137. scm_wrong_type_arg_msg (NULL, 0, h->array, "mutable " #tag "vector"); \
  138. return (ctype *) ret; \
  139. }
  140. #define MOD "srfi srfi-4"
  141. DEFINE_SRFI_4_PROXIES (u8);
  142. DEFINE_SRFI_4_C_FUNCS (U8, u8, uint8_t, 1);
  143. DEFINE_SRFI_4_PROXIES (s8);
  144. DEFINE_SRFI_4_C_FUNCS (S8, s8, int8_t, 1);
  145. DEFINE_SRFI_4_PROXIES (u16);
  146. DEFINE_SRFI_4_C_FUNCS (U16, u16, uint16_t, 1);
  147. DEFINE_SRFI_4_PROXIES (s16);
  148. DEFINE_SRFI_4_C_FUNCS (S16, s16, int16_t, 1);
  149. DEFINE_SRFI_4_PROXIES (u32);
  150. DEFINE_SRFI_4_C_FUNCS (U32, u32, uint32_t, 1);
  151. DEFINE_SRFI_4_PROXIES (s32);
  152. DEFINE_SRFI_4_C_FUNCS (S32, s32, int32_t, 1);
  153. DEFINE_SRFI_4_PROXIES (u64);
  154. DEFINE_SRFI_4_C_FUNCS (U64, u64, uint64_t, 1);
  155. DEFINE_SRFI_4_PROXIES (s64);
  156. DEFINE_SRFI_4_C_FUNCS (S64, s64, int64_t, 1);
  157. DEFINE_SRFI_4_PROXIES (f32);
  158. DEFINE_SRFI_4_C_FUNCS (F32, f32, float, 1);
  159. DEFINE_SRFI_4_PROXIES (f64);
  160. DEFINE_SRFI_4_C_FUNCS (F64, f64, double, 1);
  161. #undef MOD
  162. #define MOD "srfi srfi-4 gnu"
  163. DEFINE_SRFI_4_PROXIES (c32);
  164. DEFINE_SRFI_4_C_FUNCS (C32, c32, float, 2);
  165. DEFINE_SRFI_4_PROXIES (c64);
  166. DEFINE_SRFI_4_C_FUNCS (C64, c64, double, 2);
  167. #define DEFINE_SRFI_4_GNU_PROXIES(tag) \
  168. DEFPROXY100 (scm_any_to_##tag##vector, "any->" #tag "vector")
  169. #undef MOD
  170. #define MOD "srfi srfi-4 gnu"
  171. DEFINE_SRFI_4_GNU_PROXIES (u8);
  172. DEFINE_SRFI_4_GNU_PROXIES (s8);
  173. DEFINE_SRFI_4_GNU_PROXIES (u16);
  174. DEFINE_SRFI_4_GNU_PROXIES (s16);
  175. DEFINE_SRFI_4_GNU_PROXIES (u32);
  176. DEFINE_SRFI_4_GNU_PROXIES (s32);
  177. DEFINE_SRFI_4_GNU_PROXIES (u64);
  178. DEFINE_SRFI_4_GNU_PROXIES (s64);
  179. DEFINE_SRFI_4_GNU_PROXIES (f32);
  180. DEFINE_SRFI_4_GNU_PROXIES (f64);
  181. DEFINE_SRFI_4_GNU_PROXIES (c32);
  182. DEFINE_SRFI_4_GNU_PROXIES (c64);
  183. SCM_DEFINE (scm_make_srfi_4_vector, "make-srfi-4-vector", 2, 1, 0,
  184. (SCM type, SCM len, SCM fill),
  185. "Make a srfi-4 vector")
  186. #define FUNC_NAME s_scm_make_srfi_4_vector
  187. {
  188. int c_type;
  189. size_t c_len;
  190. for (c_type = 0; c_type <= SCM_ARRAY_ELEMENT_TYPE_LAST; c_type++)
  191. if (scm_is_eq (type, scm_i_array_element_types[c_type]))
  192. break;
  193. if (c_type > SCM_ARRAY_ELEMENT_TYPE_LAST)
  194. scm_wrong_type_arg_msg (FUNC_NAME, SCM_ARG1, type, "vector type");
  195. switch (c_type)
  196. {
  197. case SCM_ARRAY_ELEMENT_TYPE_U8:
  198. case SCM_ARRAY_ELEMENT_TYPE_S8:
  199. case SCM_ARRAY_ELEMENT_TYPE_U16:
  200. case SCM_ARRAY_ELEMENT_TYPE_S16:
  201. case SCM_ARRAY_ELEMENT_TYPE_U32:
  202. case SCM_ARRAY_ELEMENT_TYPE_S32:
  203. case SCM_ARRAY_ELEMENT_TYPE_U64:
  204. case SCM_ARRAY_ELEMENT_TYPE_S64:
  205. case SCM_ARRAY_ELEMENT_TYPE_F32:
  206. case SCM_ARRAY_ELEMENT_TYPE_F64:
  207. case SCM_ARRAY_ELEMENT_TYPE_C32:
  208. case SCM_ARRAY_ELEMENT_TYPE_C64:
  209. {
  210. SCM ret;
  211. c_len = scm_to_size_t (len);
  212. ret = scm_i_make_typed_bytevector (c_len, c_type);
  213. if (SCM_UNBNDP (fill) || scm_is_eq (len, SCM_INUM0))
  214. ; /* pass */
  215. else if (scm_is_true (scm_zero_p (fill)))
  216. memset (SCM_BYTEVECTOR_CONTENTS (ret), 0,
  217. SCM_BYTEVECTOR_LENGTH (ret));
  218. else
  219. {
  220. scm_t_array_handle h;
  221. size_t i;
  222. scm_array_get_handle (ret, &h);
  223. for (i = 0; i < c_len; i++)
  224. scm_array_handle_set (&h, i, fill);
  225. scm_array_handle_release (&h);
  226. }
  227. return ret;
  228. }
  229. default:
  230. scm_wrong_type_arg_msg (FUNC_NAME, SCM_ARG1, type, "uniform vector type");
  231. return SCM_BOOL_F; /* not reached */
  232. }
  233. }
  234. #undef FUNC_NAME
  235. void
  236. scm_init_srfi_4 (void)
  237. {
  238. #define REGISTER(tag, TAG) \
  239. scm_i_register_vector_constructor \
  240. (scm_i_array_element_types[SCM_ARRAY_ELEMENT_TYPE_##TAG], \
  241. scm_make_##tag##vector)
  242. REGISTER (u8, U8);
  243. REGISTER (s8, S8);
  244. REGISTER (u16, U16);
  245. REGISTER (s16, S16);
  246. REGISTER (u32, U32);
  247. REGISTER (s32, S32);
  248. REGISTER (u64, U64);
  249. REGISTER (s64, S64);
  250. REGISTER (f32, F32);
  251. REGISTER (f64, F64);
  252. REGISTER (c32, C32);
  253. REGISTER (c64, C64);
  254. #include "srfi-4.x"
  255. }
  256. /* End of srfi-4.c. */