reimplement srfi-4 vectors on top of bytevectors
[bpt/guile.git] / libguile / srfi-4.c
1 /* srfi-4.c --- Uniform numeric vector datatypes.
2 *
3 * Copyright (C) 2001, 2004, 2006, 2009, 2010 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
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include "libguile/_scm.h"
26 #include "libguile/__scm.h"
27 #include "libguile/bdw-gc.h"
28 #include "libguile/srfi-4.h"
29 #include "libguile/bytevectors.h"
30 #include "libguile/error.h"
31 #include "libguile/eval.h"
32 #include "libguile/extensions.h"
33 #include "libguile/uniform.h"
34 #include "libguile/generalized-vectors.h"
35 #include "libguile/validate.h"
36
37
38 #define DEFINE_SCHEME_PROXY100(cname, modname, scmname) \
39 SCM cname (SCM arg1) \
40 { \
41 static SCM var = SCM_BOOL_F; \
42 if (scm_is_false (var)) \
43 var = scm_c_module_lookup (scm_c_resolve_module (modname), scmname); \
44 return scm_call_1 (SCM_VARIABLE_REF (var), arg1); \
45 }
46
47 #define DEFINE_SCHEME_PROXY001(cname, modname, scmname) \
48 SCM cname (SCM args) \
49 { \
50 static SCM var = SCM_BOOL_F; \
51 if (scm_is_false (var)) \
52 var = scm_c_module_lookup (scm_c_resolve_module (modname), scmname); \
53 return scm_apply_0 (SCM_VARIABLE_REF (var), args); \
54 }
55
56 #define DEFINE_SCHEME_PROXY110(cname, modname, scmname) \
57 SCM cname (SCM arg1, SCM opt1) \
58 { \
59 static SCM var = SCM_BOOL_F; \
60 if (scm_is_false (var)) \
61 var = scm_c_module_lookup (scm_c_resolve_module (modname), scmname); \
62 if (SCM_UNBNDP (opt1)) \
63 return scm_call_1 (SCM_VARIABLE_REF (var), arg1); \
64 else \
65 return scm_call_2 (SCM_VARIABLE_REF (var), arg1, opt1); \
66 }
67
68 #define DEFINE_SCHEME_PROXY200(cname, modname, scmname) \
69 SCM cname (SCM arg1, SCM arg2) \
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_2 (SCM_VARIABLE_REF (var), arg1, arg2); \
75 }
76
77 #define DEFINE_SCHEME_PROXY300(cname, modname, scmname) \
78 SCM cname (SCM arg1, SCM arg2, SCM arg3) \
79 { \
80 static SCM var = SCM_BOOL_F; \
81 if (scm_is_false (var)) \
82 var = scm_c_module_lookup (scm_c_resolve_module (modname), scmname); \
83 return scm_call_3 (SCM_VARIABLE_REF (var), arg1, arg2, arg3); \
84 }
85
86 #define DEFPROXY100(cname, scmname) \
87 DEFINE_SCHEME_PROXY100 (cname, MOD, scmname)
88 #define DEFPROXY110(cname, scmname) \
89 DEFINE_SCHEME_PROXY110 (cname, MOD, scmname)
90 #define DEFPROXY001(cname, scmname) \
91 DEFINE_SCHEME_PROXY001 (cname, MOD, scmname)
92 #define DEFPROXY200(cname, scmname) \
93 DEFINE_SCHEME_PROXY200 (cname, MOD, scmname)
94 #define DEFPROXY300(cname, scmname) \
95 DEFINE_SCHEME_PROXY300 (cname, MOD, scmname)
96
97 #define DEFVECT(sym, str, func)\
98
99 #define DEFINE_SRFI_4_PROXIES(tag) \
100 DEFPROXY100 (scm_##tag##vector_p, #tag "vector?"); \
101 DEFPROXY110 (scm_make_##tag##vector, "make-" #tag "vector"); \
102 DEFPROXY001 (scm_##tag##vector, #tag "vector"); \
103 DEFPROXY100 (scm_##tag##vector_length, #tag "vector-length"); \
104 DEFPROXY200 (scm_##tag##vector_ref, #tag "vector-ref"); \
105 DEFPROXY300 (scm_##tag##vector_set_x, #tag "vector-set!"); \
106 DEFPROXY100 (scm_list_to_##tag##vector, "list->"#tag "vector"); \
107 DEFPROXY100 (scm_##tag##vector_to_list, #tag "vector->list"); \
108
109
110 #define ETYPE(TAG) \
111 SCM_ARRAY_ELEMENT_TYPE_##TAG
112
113 #define DEFINE_SRFI_4_C_FUNCS(TAG, tag, ctype) \
114 SCM scm_take_##tag##vector (ctype *data, size_t n) \
115 { \
116 return scm_c_take_typed_bytevector ((scm_t_int8*)data, n, ETYPE (TAG)); \
117 } \
118 const ctype* scm_array_handle_##tag##_elements (scm_t_array_handle *h) \
119 { \
120 if (h->element_type != ETYPE (TAG)) \
121 scm_wrong_type_arg_msg (NULL, 0, h->array, #tag "vector"); \
122 return h->elements; \
123 } \
124 ctype* scm_array_handle_##tag##_writable_elements (scm_t_array_handle *h) \
125 { \
126 if (h->element_type != ETYPE (TAG)) \
127 scm_wrong_type_arg_msg (NULL, 0, h->array, #tag "vector"); \
128 return h->writable_elements; \
129 } \
130 const ctype *scm_##tag##vector_elements (SCM uvec, \
131 scm_t_array_handle *h, \
132 size_t *lenp, ssize_t *incp) \
133 { \
134 return scm_##tag##vector_writable_elements (uvec, h, lenp, incp); \
135 } \
136 ctype *scm_##tag##vector_writable_elements (SCM uvec, \
137 scm_t_array_handle *h, \
138 size_t *lenp, ssize_t *incp) \
139 { \
140 scm_uniform_vector_elements (uvec, h, lenp, incp); \
141 if (h->element_type == ETYPE (TAG)) \
142 return ((ctype*)h->writable_elements) + h->base; \
143 /* otherwise... */ \
144 else \
145 { \
146 size_t sfrom, sto, lfrom, lto; \
147 if (h->dims != &h->dim0) \
148 { \
149 h->dim0 = h->dims[0]; \
150 h->dims = &h->dim0; \
151 } \
152 sfrom = scm_i_array_element_type_sizes [h->element_type]; \
153 sto = scm_i_array_element_type_sizes [ETYPE (TAG)]; \
154 lfrom = h->dim0.ubnd - h->dim0.lbnd + 1; \
155 lto = lfrom * sfrom / sto; \
156 if (lto * sto != lfrom * sfrom) \
157 { \
158 scm_array_handle_release (h); \
159 scm_wrong_type_arg (#tag"vector-elements", SCM_ARG1, uvec); \
160 } \
161 h->dim0.ubnd = h->dim0.lbnd + lto; \
162 h->base = h->base * sto / sfrom; \
163 h->element_type = ETYPE (TAG); \
164 return ((ctype*)h->writable_elements) + h->base; \
165 } \
166 }
167
168
169 #define MOD "srfi srfi-4"
170
171 DEFINE_SRFI_4_PROXIES (u8);
172 DEFINE_SRFI_4_C_FUNCS (U8, u8, scm_t_uint8);
173
174 DEFINE_SRFI_4_PROXIES (s8);
175 DEFINE_SRFI_4_C_FUNCS (S8, s8, scm_t_int8);
176
177 DEFINE_SRFI_4_PROXIES (u16);
178 DEFINE_SRFI_4_C_FUNCS (U16, u16, scm_t_uint16);
179
180 DEFINE_SRFI_4_PROXIES (s16);
181 DEFINE_SRFI_4_C_FUNCS (S16, s16, scm_t_int16);
182
183 DEFINE_SRFI_4_PROXIES (u32);
184 DEFINE_SRFI_4_C_FUNCS (U32, u32, scm_t_uint32);
185
186 DEFINE_SRFI_4_PROXIES (s32);
187 DEFINE_SRFI_4_C_FUNCS (S32, s32, scm_t_int32);
188
189 DEFINE_SRFI_4_PROXIES (u64);
190 #if SCM_HAVE_T_INT64
191 DEFINE_SRFI_4_C_FUNCS (U64, u64, scm_t_uint64);
192 #endif
193
194 DEFINE_SRFI_4_PROXIES (s64);
195 #if SCM_HAVE_T_INT64
196 DEFINE_SRFI_4_C_FUNCS (S64, s64, scm_t_int64);
197 #endif
198
199 DEFINE_SRFI_4_PROXIES (f32);
200 DEFINE_SRFI_4_C_FUNCS (F32, f32, float);
201
202 DEFINE_SRFI_4_PROXIES (f64);
203 DEFINE_SRFI_4_C_FUNCS (F64, f64, double);
204
205 #undef MOD
206 #define MOD "srfi srfi-4 gnu"
207
208 DEFINE_SRFI_4_PROXIES (c32);
209 DEFINE_SRFI_4_C_FUNCS (C32, c32, float);
210
211 DEFINE_SRFI_4_PROXIES (c64);
212 DEFINE_SRFI_4_C_FUNCS (C64, c64, double);
213
214 #define DEFINE_SRFI_4_GNU_PROXIES(tag) \
215 DEFPROXY100 (scm_any_to_##tag##vector, "any->" #tag "vector")
216
217 #undef MOD
218 #define MOD "srfi srfi-4 gnu"
219 DEFINE_SRFI_4_GNU_PROXIES (u8);
220 DEFINE_SRFI_4_GNU_PROXIES (s8);
221 DEFINE_SRFI_4_GNU_PROXIES (u16);
222 DEFINE_SRFI_4_GNU_PROXIES (s16);
223 DEFINE_SRFI_4_GNU_PROXIES (u32);
224 DEFINE_SRFI_4_GNU_PROXIES (s32);
225 DEFINE_SRFI_4_GNU_PROXIES (u64);
226 DEFINE_SRFI_4_GNU_PROXIES (s64);
227 DEFINE_SRFI_4_GNU_PROXIES (f32);
228 DEFINE_SRFI_4_GNU_PROXIES (f64);
229 DEFINE_SRFI_4_GNU_PROXIES (c32);
230 DEFINE_SRFI_4_GNU_PROXIES (c64);
231
232
233 SCM_DEFINE (scm_make_srfi_4_vector, "make-srfi-4-vector", 2, 1, 0,
234 (SCM type, SCM len, SCM fill),
235 "Make a srfi-4 vector")
236 #define FUNC_NAME s_scm_make_srfi_4_vector
237 {
238 int i;
239 for (i = 0; i <= SCM_ARRAY_ELEMENT_TYPE_LAST; i++)
240 if (scm_is_eq (type, scm_i_array_element_types[i]))
241 break;
242 if (i > SCM_ARRAY_ELEMENT_TYPE_LAST)
243 scm_wrong_type_arg_msg (FUNC_NAME, SCM_ARG1, type, "vector type");
244 switch (i)
245 {
246 case SCM_ARRAY_ELEMENT_TYPE_U8:
247 case SCM_ARRAY_ELEMENT_TYPE_S8:
248 case SCM_ARRAY_ELEMENT_TYPE_U16:
249 case SCM_ARRAY_ELEMENT_TYPE_S16:
250 case SCM_ARRAY_ELEMENT_TYPE_U32:
251 case SCM_ARRAY_ELEMENT_TYPE_S32:
252 case SCM_ARRAY_ELEMENT_TYPE_U64:
253 case SCM_ARRAY_ELEMENT_TYPE_S64:
254 case SCM_ARRAY_ELEMENT_TYPE_F32:
255 case SCM_ARRAY_ELEMENT_TYPE_F64:
256 case SCM_ARRAY_ELEMENT_TYPE_C32:
257 case SCM_ARRAY_ELEMENT_TYPE_C64:
258 {
259 SCM ret = scm_i_make_typed_bytevector (scm_to_size_t (len), i);
260 if (SCM_UNBNDP (fill))
261 ; /* pass */
262 else if (scm_is_true (scm_zero_p (fill)))
263 memset (SCM_BYTEVECTOR_CONTENTS (ret), 0,
264 SCM_BYTEVECTOR_LENGTH (ret));
265 else
266 {
267 scm_t_array_handle h;
268 size_t len;
269 ssize_t pos, inc;
270 scm_uniform_vector_writable_elements (ret, &h, &len, &inc);
271 for (pos = 0; pos != h.dims[0].ubnd; pos += inc)
272 scm_array_handle_set (&h, pos, fill);
273 scm_array_handle_release (&h);
274 }
275 return ret;
276 }
277 default:
278 scm_wrong_type_arg_msg (FUNC_NAME, SCM_ARG1, type, "uniform vector type");
279 return SCM_BOOL_F; /* not reached */
280 }
281 }
282 #undef FUNC_NAME
283
284 void
285 scm_init_srfi_4 (void)
286 {
287 #define REGISTER(tag, TAG) \
288 scm_i_register_vector_constructor \
289 (scm_i_array_element_types[SCM_ARRAY_ELEMENT_TYPE_##TAG], \
290 scm_make_##tag##vector)
291
292 REGISTER (u8, U8);
293 REGISTER (s8, S8);
294 REGISTER (u16, U16);
295 REGISTER (s16, S16);
296 REGISTER (u32, U32);
297 REGISTER (s32, S32);
298 REGISTER (u64, U64);
299 REGISTER (s64, S64);
300 REGISTER (f32, F32);
301 REGISTER (f64, F64);
302 REGISTER (c32, C32);
303 REGISTER (c64, C64);
304
305 #include "libguile/srfi-4.x"
306 }
307
308 /* End of srfi-4.c. */