srfi-4.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /* srfi-4.c --- Uniform numeric vector datatypes.
  2. *
  3. * Copyright (C) 2001, 2004, 2006, 2009, 2010, 2011, 2014 Free Software Foundation, Inc.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public License
  7. * as published by the Free Software Foundation; either version 3 of
  8. * the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301 USA
  19. */
  20. #ifdef HAVE_CONFIG_H
  21. # include <config.h>
  22. #endif
  23. #include "libguile/_scm.h"
  24. #include "libguile/__scm.h"
  25. #include "libguile/bdw-gc.h"
  26. #include "libguile/srfi-4.h"
  27. #include "libguile/bytevectors.h"
  28. #include "libguile/error.h"
  29. #include "libguile/eval.h"
  30. #include "libguile/extensions.h"
  31. #include "libguile/uniform.h"
  32. #include "libguile/validate.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 ((scm_t_int8*)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->element_type != ETYPE (TAG)) \
  113. scm_wrong_type_arg_msg (NULL, 0, h->array, #tag "vector"); \
  114. return ((ctype*) h->writable_elements) + h->base*width; \
  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. return scm_##tag##vector_writable_elements (uvec, h, lenp, incp); \
  121. } \
  122. ctype *scm_##tag##vector_writable_elements (SCM uvec, \
  123. scm_t_array_handle *h, \
  124. size_t *lenp, ssize_t *incp) \
  125. { \
  126. size_t byte_width = width * sizeof (ctype); \
  127. if (!scm_is_bytevector (uvec) \
  128. || (scm_c_bytevector_length (uvec) % byte_width)) \
  129. scm_wrong_type_arg_msg (NULL, 0, uvec, #tag "vector"); \
  130. scm_array_get_handle (uvec, h); \
  131. if (lenp) \
  132. *lenp = scm_c_bytevector_length (uvec) / byte_width; \
  133. if (incp) \
  134. *incp = 1; \
  135. return ((ctype *)h->writable_elements); \
  136. }
  137. #define MOD "srfi srfi-4"
  138. DEFINE_SRFI_4_PROXIES (u8);
  139. DEFINE_SRFI_4_C_FUNCS (U8, u8, scm_t_uint8, 1);
  140. DEFINE_SRFI_4_PROXIES (s8);
  141. DEFINE_SRFI_4_C_FUNCS (S8, s8, scm_t_int8, 1);
  142. DEFINE_SRFI_4_PROXIES (u16);
  143. DEFINE_SRFI_4_C_FUNCS (U16, u16, scm_t_uint16, 1);
  144. DEFINE_SRFI_4_PROXIES (s16);
  145. DEFINE_SRFI_4_C_FUNCS (S16, s16, scm_t_int16, 1);
  146. DEFINE_SRFI_4_PROXIES (u32);
  147. DEFINE_SRFI_4_C_FUNCS (U32, u32, scm_t_uint32, 1);
  148. DEFINE_SRFI_4_PROXIES (s32);
  149. DEFINE_SRFI_4_C_FUNCS (S32, s32, scm_t_int32, 1);
  150. DEFINE_SRFI_4_PROXIES (u64);
  151. DEFINE_SRFI_4_C_FUNCS (U64, u64, scm_t_uint64, 1);
  152. DEFINE_SRFI_4_PROXIES (s64);
  153. DEFINE_SRFI_4_C_FUNCS (S64, s64, scm_t_int64, 1);
  154. DEFINE_SRFI_4_PROXIES (f32);
  155. DEFINE_SRFI_4_C_FUNCS (F32, f32, float, 1);
  156. DEFINE_SRFI_4_PROXIES (f64);
  157. DEFINE_SRFI_4_C_FUNCS (F64, f64, double, 1);
  158. #undef MOD
  159. #define MOD "srfi srfi-4 gnu"
  160. DEFINE_SRFI_4_PROXIES (c32);
  161. DEFINE_SRFI_4_C_FUNCS (C32, c32, float, 2);
  162. DEFINE_SRFI_4_PROXIES (c64);
  163. DEFINE_SRFI_4_C_FUNCS (C64, c64, double, 2);
  164. #define DEFINE_SRFI_4_GNU_PROXIES(tag) \
  165. DEFPROXY100 (scm_any_to_##tag##vector, "any->" #tag "vector")
  166. #undef MOD
  167. #define MOD "srfi srfi-4 gnu"
  168. DEFINE_SRFI_4_GNU_PROXIES (u8);
  169. DEFINE_SRFI_4_GNU_PROXIES (s8);
  170. DEFINE_SRFI_4_GNU_PROXIES (u16);
  171. DEFINE_SRFI_4_GNU_PROXIES (s16);
  172. DEFINE_SRFI_4_GNU_PROXIES (u32);
  173. DEFINE_SRFI_4_GNU_PROXIES (s32);
  174. DEFINE_SRFI_4_GNU_PROXIES (u64);
  175. DEFINE_SRFI_4_GNU_PROXIES (s64);
  176. DEFINE_SRFI_4_GNU_PROXIES (f32);
  177. DEFINE_SRFI_4_GNU_PROXIES (f64);
  178. DEFINE_SRFI_4_GNU_PROXIES (c32);
  179. DEFINE_SRFI_4_GNU_PROXIES (c64);
  180. SCM_DEFINE (scm_make_srfi_4_vector, "make-srfi-4-vector", 2, 1, 0,
  181. (SCM type, SCM len, SCM fill),
  182. "Make a srfi-4 vector")
  183. #define FUNC_NAME s_scm_make_srfi_4_vector
  184. {
  185. int c_type;
  186. size_t c_len;
  187. for (c_type = 0; c_type <= SCM_ARRAY_ELEMENT_TYPE_LAST; c_type++)
  188. if (scm_is_eq (type, scm_i_array_element_types[c_type]))
  189. break;
  190. if (c_type > SCM_ARRAY_ELEMENT_TYPE_LAST)
  191. scm_wrong_type_arg_msg (FUNC_NAME, SCM_ARG1, type, "vector type");
  192. switch (c_type)
  193. {
  194. case SCM_ARRAY_ELEMENT_TYPE_U8:
  195. case SCM_ARRAY_ELEMENT_TYPE_S8:
  196. case SCM_ARRAY_ELEMENT_TYPE_U16:
  197. case SCM_ARRAY_ELEMENT_TYPE_S16:
  198. case SCM_ARRAY_ELEMENT_TYPE_U32:
  199. case SCM_ARRAY_ELEMENT_TYPE_S32:
  200. case SCM_ARRAY_ELEMENT_TYPE_U64:
  201. case SCM_ARRAY_ELEMENT_TYPE_S64:
  202. case SCM_ARRAY_ELEMENT_TYPE_F32:
  203. case SCM_ARRAY_ELEMENT_TYPE_F64:
  204. case SCM_ARRAY_ELEMENT_TYPE_C32:
  205. case SCM_ARRAY_ELEMENT_TYPE_C64:
  206. {
  207. SCM ret;
  208. c_len = scm_to_size_t (len);
  209. ret = scm_i_make_typed_bytevector (c_len, c_type);
  210. if (SCM_UNBNDP (fill) || scm_is_eq (len, SCM_INUM0))
  211. ; /* pass */
  212. else if (scm_is_true (scm_zero_p (fill)))
  213. memset (SCM_BYTEVECTOR_CONTENTS (ret), 0,
  214. SCM_BYTEVECTOR_LENGTH (ret));
  215. else
  216. {
  217. scm_t_array_handle h;
  218. size_t i;
  219. scm_array_get_handle (ret, &h);
  220. for (i = 0; i < c_len; i++)
  221. scm_array_handle_set (&h, i, fill);
  222. scm_array_handle_release (&h);
  223. }
  224. return ret;
  225. }
  226. default:
  227. scm_wrong_type_arg_msg (FUNC_NAME, SCM_ARG1, type, "uniform vector type");
  228. return SCM_BOOL_F; /* not reached */
  229. }
  230. }
  231. #undef FUNC_NAME
  232. void
  233. scm_init_srfi_4 (void)
  234. {
  235. #define REGISTER(tag, TAG) \
  236. scm_i_register_vector_constructor \
  237. (scm_i_array_element_types[SCM_ARRAY_ELEMENT_TYPE_##TAG], \
  238. scm_make_##tag##vector)
  239. REGISTER (u8, U8);
  240. REGISTER (s8, S8);
  241. REGISTER (u16, U16);
  242. REGISTER (s16, S16);
  243. REGISTER (u32, U32);
  244. REGISTER (s32, S32);
  245. REGISTER (u64, U64);
  246. REGISTER (s64, S64);
  247. REGISTER (f32, F32);
  248. REGISTER (f64, F64);
  249. REGISTER (c32, C32);
  250. REGISTER (c64, C64);
  251. #include "libguile/srfi-4.x"
  252. }
  253. /* End of srfi-4.c. */