Commit | Line | Data |
---|---|---|
f8579182 MV |
1 | /* srfi-4.c --- Homogeneous numeric vector datatypes. |
2 | * | |
3 | * Copyright (C) 2001, 2004 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 | |
7 | * License as published by the Free Software Foundation; either | |
8 | * version 2.1 of the License, or (at your option) any later version. | |
9 | * | |
10 | * This library is distributed in the hope that it will be useful, | |
11 | * but 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | */ | |
19 | ||
20 | #include <libguile.h> | |
21 | #include <string.h> | |
22 | #include <stdio.h> | |
23 | ||
24 | #include "libguile/srfi-4.h" | |
25 | #include "libguile/error.h" | |
26 | #include "libguile/read.h" | |
27 | #include "libguile/ports.h" | |
28 | #include "libguile/chars.h" | |
29 | ||
30 | /* Smob type code for homogeneous numeric vectors. */ | |
31 | int scm_tc16_uvec = 0; | |
32 | ||
33 | ||
34 | /* Accessor macros for the three components of a homogeneous numeric | |
35 | vector: | |
36 | - The type tag (one of the symbolic constants below). | |
37 | - The vector's length (counted in elements). | |
38 | - The address of the data area (holding the elements of the | |
39 | vector). */ | |
40 | #define SCM_UVEC_TYPE(u) (SCM_CELL_WORD_1(u)) | |
41 | #define SCM_UVEC_LENGTH(u) ((size_t)SCM_CELL_WORD_2(u)) | |
42 | #define SCM_UVEC_BASE(u) ((void *)SCM_CELL_WORD_3(u)) | |
43 | ||
44 | ||
45 | /* Symbolic constants encoding the various types of homogeneous | |
46 | numeric vectors. */ | |
47 | #define SCM_UVEC_U8 0 | |
48 | #define SCM_UVEC_S8 1 | |
49 | #define SCM_UVEC_U16 2 | |
50 | #define SCM_UVEC_S16 3 | |
51 | #define SCM_UVEC_U32 4 | |
52 | #define SCM_UVEC_S32 5 | |
53 | #define SCM_UVEC_U64 6 | |
54 | #define SCM_UVEC_S64 7 | |
55 | #define SCM_UVEC_F32 8 | |
56 | #define SCM_UVEC_F64 9 | |
cbdc8379 MV |
57 | #define SCM_UVEC_C32 10 |
58 | #define SCM_UVEC_C64 11 | |
f8579182 MV |
59 | |
60 | ||
61 | /* This array maps type tags to the size of the elements. */ | |
cbdc8379 | 62 | static const int uvec_sizes[12] = { |
f8579182 MV |
63 | 1, 1, |
64 | 2, 2, | |
65 | 4, 4, | |
66 | 8, 8, | |
cbdc8379 MV |
67 | sizeof(float), sizeof(double), |
68 | 2*sizeof(float), 2*sizeof(double) | |
f8579182 MV |
69 | }; |
70 | ||
cbdc8379 | 71 | static const char *uvec_tags[12] = { |
e0e49670 MV |
72 | "u8", "s8", |
73 | "u16", "s16", | |
74 | "u32", "s32", | |
75 | "u64", "s64", | |
cbdc8379 MV |
76 | "f32", "f64", |
77 | "c32", "c64", | |
e0e49670 MV |
78 | }; |
79 | ||
cbdc8379 | 80 | static const char *uvec_names[12] = { |
f8579182 MV |
81 | "u8vector", "s8vector", |
82 | "u16vector", "s16vector", | |
83 | "u32vector", "s32vector", | |
84 | "u64vector", "s64vector", | |
cbdc8379 MV |
85 | "f32vector", "f64vector", |
86 | "c32vector", "c64vector" | |
f8579182 MV |
87 | }; |
88 | ||
89 | /* ================================================================ */ | |
90 | /* SMOB procedures. */ | |
91 | /* ================================================================ */ | |
92 | ||
93 | ||
94 | /* Smob print hook for homogeneous vectors. */ | |
95 | static int | |
96 | uvec_print (SCM uvec, SCM port, scm_print_state *pstate) | |
97 | { | |
98 | union { | |
99 | scm_t_uint8 *u8; | |
100 | scm_t_int8 *s8; | |
101 | scm_t_uint16 *u16; | |
102 | scm_t_int16 *s16; | |
103 | scm_t_uint32 *u32; | |
104 | scm_t_int32 *s32; | |
105 | #if SCM_HAVE_T_INT64 | |
106 | scm_t_uint64 *u64; | |
107 | scm_t_int64 *s64; | |
108 | #endif | |
109 | float *f32; | |
110 | double *f64; | |
111 | } np; | |
112 | ||
113 | size_t i = 0; | |
114 | const size_t uvlen = SCM_UVEC_LENGTH (uvec); | |
f8579182 MV |
115 | void *uptr = SCM_UVEC_BASE (uvec); |
116 | ||
117 | switch (SCM_UVEC_TYPE (uvec)) | |
118 | { | |
e0e49670 MV |
119 | case SCM_UVEC_U8: np.u8 = (scm_t_uint8 *) uptr; break; |
120 | case SCM_UVEC_S8: np.s8 = (scm_t_int8 *) uptr; break; | |
121 | case SCM_UVEC_U16: np.u16 = (scm_t_uint16 *) uptr; break; | |
122 | case SCM_UVEC_S16: np.s16 = (scm_t_int16 *) uptr; break; | |
123 | case SCM_UVEC_U32: np.u32 = (scm_t_uint32 *) uptr; break; | |
124 | case SCM_UVEC_S32: np.s32 = (scm_t_int32 *) uptr; break; | |
f8579182 | 125 | #if SCM_HAVE_T_INT64 |
e0e49670 MV |
126 | case SCM_UVEC_U64: np.u64 = (scm_t_uint64 *) uptr; break; |
127 | case SCM_UVEC_S64: np.s64 = (scm_t_int64 *) uptr; break; | |
f8579182 | 128 | #endif |
e0e49670 MV |
129 | case SCM_UVEC_F32: np.f32 = (float *) uptr; break; |
130 | case SCM_UVEC_F64: np.f64 = (double *) uptr; break; | |
cbdc8379 MV |
131 | case SCM_UVEC_C32: np.f32 = (float *) uptr; break; |
132 | case SCM_UVEC_C64: np.f64 = (double *) uptr; break; | |
f8579182 MV |
133 | default: |
134 | abort (); /* Sanity check. */ | |
135 | break; | |
136 | } | |
137 | ||
138 | scm_putc ('#', port); | |
e0e49670 | 139 | scm_puts (uvec_tags [SCM_UVEC_TYPE (uvec)], port); |
f8579182 MV |
140 | scm_putc ('(', port); |
141 | ||
142 | while (i < uvlen) | |
143 | { | |
144 | if (i != 0) scm_puts (" ", port); | |
145 | switch (SCM_UVEC_TYPE (uvec)) | |
146 | { | |
147 | case SCM_UVEC_U8: scm_uintprint (*np.u8, 10, port); np.u8++; break; | |
148 | case SCM_UVEC_S8: scm_intprint (*np.s8, 10, port); np.s8++; break; | |
149 | case SCM_UVEC_U16: scm_uintprint (*np.u16, 10, port); np.u16++; break; | |
150 | case SCM_UVEC_S16: scm_intprint (*np.s16, 10, port); np.s16++; break; | |
151 | case SCM_UVEC_U32: scm_uintprint (*np.u32, 10, port); np.u32++; break; | |
152 | case SCM_UVEC_S32: scm_intprint (*np.s32, 10, port); np.s32++; break; | |
153 | #if SCM_HAVE_T_INT64 | |
154 | case SCM_UVEC_U64: scm_uintprint (*np.u64, 10, port); np.u64++; break; | |
155 | case SCM_UVEC_S64: scm_intprint (*np.s64, 10, port); np.s64++; break; | |
156 | #endif | |
157 | case SCM_UVEC_F32: scm_i_print_double (*np.f32, port); np.f32++; break; | |
158 | case SCM_UVEC_F64: scm_i_print_double (*np.f64, port); np.f64++; break; | |
cbdc8379 MV |
159 | case SCM_UVEC_C32: |
160 | scm_i_print_complex (np.f32[0], np.f32[1], port); | |
161 | np.f32 += 2; | |
162 | break; | |
163 | case SCM_UVEC_C64: | |
164 | scm_i_print_complex (np.f64[0], np.f64[1], port); | |
165 | np.f64 += 2; | |
166 | break; | |
f8579182 MV |
167 | default: |
168 | abort (); /* Sanity check. */ | |
169 | break; | |
170 | } | |
171 | i++; | |
172 | } | |
173 | scm_remember_upto_here_1 (uvec); | |
174 | scm_puts (")", port); | |
175 | return 1; | |
176 | } | |
177 | ||
e0e49670 MV |
178 | const char * |
179 | scm_i_uniform_vector_tag (SCM uvec) | |
180 | { | |
181 | return uvec_tags[SCM_UVEC_TYPE (uvec)]; | |
182 | } | |
183 | ||
f8579182 MV |
184 | static SCM |
185 | uvec_equalp (SCM a, SCM b) | |
186 | { | |
187 | SCM result = SCM_BOOL_T; | |
188 | if (SCM_UVEC_TYPE (a) != SCM_UVEC_TYPE (b)) | |
189 | result = SCM_BOOL_F; | |
190 | else if (SCM_UVEC_LENGTH (a) != SCM_UVEC_LENGTH (b)) | |
191 | result = SCM_BOOL_F; | |
192 | else if (memcmp (SCM_UVEC_BASE (a), SCM_UVEC_BASE (b), | |
193 | SCM_UVEC_LENGTH (a) * uvec_sizes[SCM_UVEC_TYPE(a)]) != 0) | |
194 | result = SCM_BOOL_F; | |
195 | ||
196 | scm_remember_upto_here_2 (a, b); | |
197 | return result; | |
198 | } | |
199 | ||
200 | /* Smob free hook for homogeneous numeric vectors. */ | |
201 | static size_t | |
202 | uvec_free (SCM uvec) | |
203 | { | |
204 | int type = SCM_UVEC_TYPE (uvec); | |
205 | scm_gc_free (SCM_UVEC_BASE (uvec), | |
206 | SCM_UVEC_LENGTH (uvec) * uvec_sizes[type], | |
207 | uvec_names[type]); | |
208 | return 0; | |
209 | } | |
210 | ||
211 | /* ================================================================ */ | |
212 | /* Utility procedures. */ | |
213 | /* ================================================================ */ | |
214 | ||
215 | static SCM_C_INLINE int | |
216 | is_uvec (int type, SCM obj) | |
217 | { | |
218 | return (SCM_SMOB_PREDICATE (scm_tc16_uvec, obj) | |
219 | && SCM_UVEC_TYPE (obj) == type); | |
220 | } | |
221 | ||
222 | static SCM_C_INLINE SCM | |
223 | uvec_p (int type, SCM obj) | |
224 | { | |
225 | return scm_from_bool (is_uvec (type, obj)); | |
226 | } | |
227 | ||
228 | static SCM_C_INLINE void | |
229 | uvec_assert (int type, SCM obj) | |
230 | { | |
231 | if (!is_uvec (type, obj)) | |
232 | scm_wrong_type_arg_msg (NULL, 0, obj, uvec_names[type]); | |
233 | } | |
234 | ||
faa00365 MV |
235 | static SCM |
236 | take_uvec (int type, const void *base, size_t len) | |
237 | { | |
238 | SCM_RETURN_NEWSMOB3 (scm_tc16_uvec, type, len, (scm_t_bits) base); | |
239 | } | |
240 | ||
f8579182 MV |
241 | /* Create a new, uninitialized homogeneous numeric vector of type TYPE |
242 | with space for LEN elements. */ | |
243 | static SCM | |
faa00365 | 244 | alloc_uvec (int type, size_t len) |
f8579182 | 245 | { |
faa00365 MV |
246 | void *base = scm_gc_malloc (len * uvec_sizes[type], uvec_names[type]); |
247 | return take_uvec (type, base, len); | |
f8579182 MV |
248 | } |
249 | ||
250 | /* GCC doesn't seem to want to optimize unused switch clauses away, | |
251 | so we use a big 'if' in the next two functions. | |
252 | */ | |
253 | ||
254 | static SCM_C_INLINE SCM | |
255 | uvec_fast_ref (int type, void *base, size_t c_idx) | |
256 | { | |
257 | if (type == SCM_UVEC_U8) | |
258 | return scm_from_uint8 (((scm_t_uint8*)base)[c_idx]); | |
259 | else if (type == SCM_UVEC_S8) | |
260 | return scm_from_int8 (((scm_t_int8*)base)[c_idx]); | |
261 | else if (type == SCM_UVEC_U16) | |
262 | return scm_from_uint16 (((scm_t_uint16*)base)[c_idx]); | |
263 | else if (type == SCM_UVEC_S16) | |
264 | return scm_from_int16 (((scm_t_int16*)base)[c_idx]); | |
265 | else if (type == SCM_UVEC_U32) | |
266 | return scm_from_uint32 (((scm_t_uint32*)base)[c_idx]); | |
267 | else if (type == SCM_UVEC_S32) | |
268 | return scm_from_int32 (((scm_t_int32*)base)[c_idx]); | |
269 | #if SCM_HAVE_T_INT64 | |
270 | else if (type == SCM_UVEC_U64) | |
271 | return scm_from_uint64 (((scm_t_uint64*)base)[c_idx]); | |
272 | else if (type == SCM_UVEC_S64) | |
273 | return scm_from_int64 (((scm_t_int64*)base)[c_idx]); | |
274 | #endif | |
275 | else if (type == SCM_UVEC_F32) | |
276 | return scm_from_double (((float*)base)[c_idx]); | |
277 | else if (type == SCM_UVEC_F64) | |
278 | return scm_from_double (((double*)base)[c_idx]); | |
cbdc8379 MV |
279 | else if (type == SCM_UVEC_C32) |
280 | return scm_c_make_rectangular (((float*)base)[2*c_idx], | |
281 | ((float*)base)[2*c_idx+1]); | |
282 | else if (type == SCM_UVEC_C64) | |
283 | return scm_c_make_rectangular (((double*)base)[2*c_idx], | |
284 | ((double*)base)[2*c_idx+1]); | |
f8579182 MV |
285 | } |
286 | ||
287 | static SCM_C_INLINE void | |
288 | uvec_fast_set_x (int type, void *base, size_t c_idx, SCM val) | |
289 | { | |
290 | if (type == SCM_UVEC_U8) | |
291 | (((scm_t_uint8*)base)[c_idx]) = scm_to_uint8 (val); | |
292 | else if (type == SCM_UVEC_S8) | |
293 | (((scm_t_int8*)base)[c_idx]) = scm_to_int8 (val); | |
294 | else if (type == SCM_UVEC_U16) | |
295 | (((scm_t_uint16*)base)[c_idx]) = scm_to_uint16 (val); | |
296 | else if (type == SCM_UVEC_S16) | |
297 | (((scm_t_int16*)base)[c_idx]) = scm_to_int16 (val); | |
298 | else if (type == SCM_UVEC_U32) | |
299 | (((scm_t_uint32*)base)[c_idx]) = scm_to_uint32 (val); | |
300 | else if (type == SCM_UVEC_S32) | |
301 | (((scm_t_int32*)base)[c_idx]) = scm_to_int32 (val); | |
302 | #if SCM_HAVE_T_INT64 | |
303 | else if (type == SCM_UVEC_U64) | |
304 | (((scm_t_uint64*)base)[c_idx]) = scm_to_uint64 (val); | |
305 | else if (type == SCM_UVEC_S64) | |
306 | (((scm_t_int64*)base)[c_idx]) = scm_to_int64 (val); | |
307 | #endif | |
308 | else if (type == SCM_UVEC_F32) | |
309 | (((float*)base)[c_idx]) = scm_to_double (val); | |
310 | else if (type == SCM_UVEC_F64) | |
311 | (((double*)base)[c_idx]) = scm_to_double (val); | |
cbdc8379 MV |
312 | else if (type == SCM_UVEC_C32) |
313 | { | |
314 | (((float*)base)[2*c_idx]) = scm_c_real_part (val); | |
315 | (((float*)base)[2*c_idx+1]) = scm_c_imag_part (val); | |
316 | } | |
317 | else if (type == SCM_UVEC_C64) | |
318 | { | |
319 | (((double*)base)[2*c_idx]) = scm_c_real_part (val); | |
320 | (((double*)base)[2*c_idx+1]) = scm_c_imag_part (val); | |
321 | } | |
f8579182 MV |
322 | } |
323 | ||
324 | static SCM_C_INLINE SCM | |
325 | make_uvec (int type, SCM len, SCM fill) | |
326 | { | |
327 | size_t c_len = scm_to_unsigned_integer (len, 0, SIZE_MAX / uvec_sizes[type]); | |
328 | SCM uvec = alloc_uvec (type, c_len); | |
329 | if (!SCM_UNBNDP (fill)) | |
330 | { | |
331 | size_t idx; | |
332 | void *base = SCM_UVEC_BASE (uvec); | |
333 | for (idx = 0; idx < c_len; idx++) | |
334 | uvec_fast_set_x (type, base, idx, fill); | |
335 | } | |
336 | return uvec; | |
337 | } | |
338 | ||
339 | static SCM_C_INLINE SCM | |
340 | uvec_length (int type, SCM uvec) | |
341 | { | |
342 | uvec_assert (type, uvec); | |
343 | return scm_from_size_t (SCM_UVEC_LENGTH (uvec)); | |
344 | } | |
345 | ||
346 | static SCM_C_INLINE SCM | |
347 | uvec_ref (int type, SCM uvec, SCM idx) | |
348 | { | |
349 | size_t c_idx; | |
350 | SCM res; | |
351 | ||
352 | uvec_assert (type, uvec); | |
353 | c_idx = scm_to_unsigned_integer (idx, 0, SCM_UVEC_LENGTH (uvec)-1); | |
354 | res = uvec_fast_ref (type, SCM_UVEC_BASE(uvec), c_idx); | |
355 | scm_remember_upto_here_1 (uvec); | |
356 | return res; | |
357 | } | |
358 | ||
359 | static SCM_C_INLINE SCM | |
360 | uvec_set_x (int type, SCM uvec, SCM idx, SCM val) | |
361 | { | |
362 | size_t c_idx; | |
363 | ||
364 | uvec_assert (type, uvec); | |
365 | c_idx = scm_to_unsigned_integer (idx, 0, SCM_UVEC_LENGTH (uvec)-1); | |
366 | uvec_fast_set_x (type, SCM_UVEC_BASE(uvec), c_idx, val); | |
367 | scm_remember_upto_here_1 (uvec); | |
368 | return SCM_UNSPECIFIED; | |
369 | } | |
370 | ||
371 | static SCM_C_INLINE SCM | |
372 | uvec_to_list (int type, SCM uvec) | |
373 | { | |
374 | size_t c_idx; | |
375 | void *base; | |
376 | SCM res = SCM_EOL; | |
377 | ||
378 | uvec_assert (type, uvec); | |
379 | c_idx = SCM_UVEC_LENGTH (uvec); | |
380 | base = SCM_UVEC_BASE (uvec); | |
381 | while (c_idx-- > 0) | |
382 | res = scm_cons (uvec_fast_ref (type, base, c_idx), res); | |
383 | scm_remember_upto_here_1 (uvec); | |
384 | return res; | |
385 | } | |
386 | ||
387 | static SCM_C_INLINE SCM | |
388 | list_to_uvec (int type, SCM list) | |
389 | { | |
390 | SCM uvec; | |
391 | void *base; | |
392 | long idx; | |
393 | long len = scm_ilength (list); | |
394 | if (len < 0) | |
395 | scm_wrong_type_arg_msg (NULL, 0, list, "proper list"); | |
396 | ||
397 | uvec = alloc_uvec (type, len); | |
398 | base = SCM_UVEC_BASE (uvec); | |
399 | idx = 0; | |
400 | while (scm_is_pair (list) && idx < len) | |
401 | { | |
402 | uvec_fast_set_x (type, base, idx, SCM_CAR (list)); | |
403 | list = SCM_CDR (list); | |
404 | idx++; | |
405 | } | |
406 | return uvec; | |
407 | } | |
408 | ||
90d4368c MV |
409 | static SCM |
410 | coerce_to_uvec (int type, SCM obj) | |
411 | { | |
412 | if (is_uvec (type, obj)) | |
413 | return obj; | |
414 | else if (scm_is_pair (obj)) | |
415 | return list_to_uvec (type, obj); | |
416 | else if (scm_is_true (scm_vector_p (obj))) | |
417 | { | |
418 | SCM len = scm_vector_length (obj); | |
419 | SCM uvec = make_uvec (type, len, SCM_UNDEFINED); | |
420 | size_t clen = scm_to_size_t (len), i; | |
421 | void *base = SCM_UVEC_BASE (uvec); | |
422 | for (i = 0; i < clen; i++) | |
423 | uvec_fast_set_x (type, base, i, SCM_VECTOR_REF (obj, i)); | |
424 | return uvec; | |
425 | } | |
426 | else if (scm_is_uniform_vector (obj)) | |
427 | { | |
428 | SCM len = scm_uniform_vector_length (obj); | |
429 | SCM uvec = make_uvec (type, len, SCM_UNDEFINED); | |
430 | size_t clen = scm_to_size_t (len), i; | |
431 | void *base = SCM_UVEC_BASE (uvec); | |
432 | for (i = 0; i < clen; i++) | |
433 | uvec_fast_set_x (type, base, i, | |
434 | scm_uniform_vector_ref (obj, scm_from_size_t (i))); | |
435 | return uvec; | |
436 | } | |
437 | else | |
438 | scm_wrong_type_arg_msg (NULL, 0, obj, "list, vector, or uniform vector"); | |
439 | } | |
440 | ||
cbdc8379 MV |
441 | static SCM *uvec_proc_vars[12] = { |
442 | &scm_i_proc_make_u8vector, | |
443 | &scm_i_proc_make_s8vector, | |
444 | &scm_i_proc_make_u16vector, | |
445 | &scm_i_proc_make_s16vector, | |
446 | &scm_i_proc_make_u32vector, | |
447 | &scm_i_proc_make_s32vector, | |
448 | &scm_i_proc_make_u64vector, | |
449 | &scm_i_proc_make_s64vector, | |
450 | &scm_i_proc_make_f32vector, | |
451 | &scm_i_proc_make_f64vector, | |
452 | &scm_i_proc_make_c32vector, | |
453 | &scm_i_proc_make_c64vector | |
454 | }; | |
f8579182 | 455 | |
e0e49670 | 456 | SCM |
cbdc8379 | 457 | scm_i_uniform_vector_creator (SCM uvec) |
e0e49670 | 458 | { |
cbdc8379 | 459 | return *(uvec_proc_vars[SCM_UVEC_TYPE(uvec)]); |
e0e49670 MV |
460 | } |
461 | ||
462 | int | |
463 | scm_is_uniform_vector (SCM obj) | |
464 | { | |
465 | return SCM_SMOB_PREDICATE (scm_tc16_uvec, obj); | |
466 | } | |
467 | ||
468 | size_t | |
469 | scm_c_uniform_vector_length (SCM v) | |
470 | { | |
471 | if (scm_is_uniform_vector (v)) | |
472 | return SCM_UVEC_LENGTH (v); | |
473 | else | |
474 | scm_wrong_type_arg_msg (NULL, 0, v, "uniform vector"); | |
475 | } | |
476 | ||
477 | size_t | |
478 | scm_c_uniform_vector_size (SCM v) | |
479 | { | |
480 | if (scm_is_uniform_vector (v)) | |
481 | return SCM_UVEC_LENGTH (v) * uvec_sizes[SCM_UVEC_TYPE (v)]; | |
482 | else | |
483 | scm_wrong_type_arg_msg (NULL, 0, v, "uniform vector"); | |
484 | } | |
485 | ||
486 | SCM_DEFINE (scm_uniform_vector_p, "uniform-vector?", 1, 0, 0, | |
487 | (SCM obj), | |
488 | "Return @code{#t} if @var{obj} is a uniform vector.") | |
489 | #define FUNC_NAME s_scm_uniform_vector_p | |
490 | { | |
491 | return scm_from_bool (scm_is_uniform_vector (obj)); | |
492 | } | |
493 | #undef FUNC_NAME | |
494 | ||
495 | SCM_DEFINE (scm_uniform_vector_ref, "uniform-vector-ref", 2, 0, 0, | |
496 | (SCM v, SCM idx), | |
497 | "Return the element at index @var{idx} of the\n" | |
498 | "homogenous numeric vector @var{v}.") | |
499 | #define FUNC_NAME s_scm_uniform_vector_ref | |
500 | { | |
501 | /* Support old argument convention. | |
502 | */ | |
503 | if (scm_is_pair (idx)) | |
504 | { | |
505 | if (!scm_is_null (SCM_CDR (idx))) | |
506 | scm_wrong_num_args (NULL); | |
507 | idx = SCM_CAR (idx); | |
508 | } | |
509 | ||
510 | if (scm_is_uniform_vector (v)) | |
511 | return uvec_ref (SCM_UVEC_TYPE (v), v, idx); | |
512 | else | |
513 | scm_wrong_type_arg_msg (NULL, 0, v, "uniform vector"); | |
514 | } | |
515 | #undef FUNC_NAME | |
516 | ||
517 | SCM_DEFINE (scm_uniform_vector_set_x, "uniform-vector-set!", 3, 0, 0, | |
518 | (SCM v, SCM idx, SCM val), | |
519 | "Set the element at index @var{idx} of the\n" | |
520 | "homogenous numeric vector @var{v} to @var{val}.") | |
521 | #define FUNC_NAME s_scm_uniform_vector_set_x | |
522 | { | |
523 | /* Support old argument convention. | |
524 | */ | |
525 | if (scm_is_pair (idx)) | |
526 | { | |
527 | if (!scm_is_null (SCM_CDR (idx))) | |
528 | scm_wrong_num_args (NULL); | |
529 | idx = SCM_CAR (idx); | |
530 | } | |
531 | ||
532 | if (scm_is_uniform_vector (v)) | |
533 | return uvec_set_x (SCM_UVEC_TYPE (v), v, idx, val); | |
534 | else | |
535 | scm_wrong_type_arg_msg (NULL, 0, v, "uniform vector"); | |
536 | } | |
537 | #undef FUNC_NAME | |
538 | ||
539 | SCM_DEFINE (scm_uniform_vector_to_list, "uniform-vector->list", 1, 0, 0, | |
540 | (SCM uvec), | |
541 | "Convert the homogeneous numeric vector @var{uvec} to a list.") | |
542 | #define FUNC_NAME s_uniform_vector_to_list | |
543 | { | |
544 | if (scm_is_uniform_vector (uvec)) | |
545 | return uvec_to_list (SCM_UVEC_TYPE (uvec), uvec); | |
546 | else | |
547 | scm_wrong_type_arg_msg (NULL, 0, uvec, "uniform vector"); | |
548 | } | |
549 | #undef FUNC_NAME | |
550 | ||
551 | void * | |
552 | scm_uniform_vector_elements (SCM uvec) | |
553 | { | |
554 | if (scm_is_uniform_vector (uvec)) | |
555 | return SCM_UVEC_BASE (uvec); | |
556 | else | |
557 | scm_wrong_type_arg_msg (NULL, 0, uvec, "uniform vector"); | |
558 | } | |
559 | ||
560 | void | |
561 | scm_uniform_vector_release (SCM uvec) | |
562 | { | |
563 | /* Nothing to do right now, but this function might come in handy | |
564 | when uniform vectors need to be locked when giving away a pointer | |
565 | to their elements. | |
faa00365 MV |
566 | |
567 | Also, a call to scm_uniform_vector acts like | |
568 | scm_remember_upto_here, which is needed in any case. | |
e0e49670 MV |
569 | */ |
570 | } | |
571 | ||
d44ff083 MV |
572 | void |
573 | scm_frame_uniform_vector_release (SCM uvec) | |
574 | { | |
575 | scm_frame_unwind_handler_with_scm (scm_uniform_vector_release, uvec, | |
576 | SCM_F_WIND_EXPLICITLY); | |
577 | } | |
578 | ||
e0e49670 MV |
579 | size_t |
580 | scm_uniform_vector_element_size (SCM uvec) | |
581 | { | |
582 | if (scm_is_uniform_vector (uvec)) | |
583 | return uvec_sizes[SCM_UVEC_TYPE (uvec)]; | |
584 | else | |
585 | scm_wrong_type_arg_msg (NULL, 0, uvec, "uniform vector"); | |
586 | } | |
587 | ||
588 | /* return the size of an element in a uniform array or 0 if type not | |
589 | found. */ | |
590 | size_t | |
591 | scm_uniform_element_size (SCM obj) | |
592 | { | |
e0e49670 MV |
593 | if (scm_is_uniform_vector (obj)) |
594 | return scm_uniform_vector_element_size (obj); | |
90d4368c MV |
595 | else if (SCM_BITVECTOR_P (obj)) |
596 | return sizeof (long); | |
597 | else | |
598 | return 0; | |
e0e49670 MV |
599 | } |
600 | ||
601 | SCM_DEFINE (scm_uniform_vector_length, "uniform-vector-length", 1, 0, 0, | |
602 | (SCM v), | |
603 | "Return the number of elements in @var{uve}.") | |
604 | #define FUNC_NAME s_scm_uniform_vector_length | |
605 | { | |
606 | if (scm_is_uniform_vector (v)) | |
607 | return scm_from_size_t (SCM_UVEC_LENGTH (v)); | |
90d4368c MV |
608 | else if (scm_is_string (v)) |
609 | return scm_string_length (v); | |
610 | else if (scm_is_true (scm_vector_p (v))) | |
611 | return scm_vector_length (v); | |
612 | else if (SCM_BITVECTOR_P (v)) | |
613 | return scm_from_size_t (SCM_BITVECTOR_LENGTH (v)); | |
614 | else | |
615 | scm_wrong_type_arg (NULL, 0, v); | |
e0e49670 MV |
616 | } |
617 | #undef FUNC_NAME | |
618 | ||
f8579182 MV |
619 | /* ================================================================ */ |
620 | /* Exported procedures. */ | |
621 | /* ================================================================ */ | |
622 | ||
e0e49670 MV |
623 | #define TYPE SCM_UVEC_U8 |
624 | #define TAG u8 | |
625 | #define CTYPE scm_t_uint8 | |
f8579182 MV |
626 | #include "libguile/srfi-4.i.c" |
627 | ||
e0e49670 MV |
628 | #define TYPE SCM_UVEC_S8 |
629 | #define TAG s8 | |
630 | #define CTYPE scm_t_int8 | |
f8579182 MV |
631 | #include "libguile/srfi-4.i.c" |
632 | ||
e0e49670 MV |
633 | #define TYPE SCM_UVEC_U16 |
634 | #define TAG u16 | |
635 | #define CTYPE scm_t_uint16 | |
f8579182 MV |
636 | #include "libguile/srfi-4.i.c" |
637 | ||
e0e49670 MV |
638 | #define TYPE SCM_UVEC_S16 |
639 | #define TAG s16 | |
640 | #define CTYPE scm_t_int16 | |
f8579182 MV |
641 | #include "libguile/srfi-4.i.c" |
642 | ||
e0e49670 MV |
643 | #define TYPE SCM_UVEC_U32 |
644 | #define TAG u32 | |
645 | #define CTYPE scm_t_uint32 | |
f8579182 MV |
646 | #include "libguile/srfi-4.i.c" |
647 | ||
e0e49670 MV |
648 | #define TYPE SCM_UVEC_S32 |
649 | #define TAG s32 | |
650 | #define CTYPE scm_t_int32 | |
f8579182 MV |
651 | #include "libguile/srfi-4.i.c" |
652 | ||
e0e49670 MV |
653 | #define TYPE SCM_UVEC_U64 |
654 | #define TAG u64 | |
655 | #define CTYPE scm_t_uint64 | |
f8579182 MV |
656 | #include "libguile/srfi-4.i.c" |
657 | ||
e0e49670 MV |
658 | #define TYPE SCM_UVEC_S64 |
659 | #define TAG s64 | |
660 | #define CTYPE scm_t_int64 | |
f8579182 MV |
661 | #include "libguile/srfi-4.i.c" |
662 | ||
e0e49670 MV |
663 | #define TYPE SCM_UVEC_F32 |
664 | #define TAG f32 | |
665 | #define CTYPE float | |
f8579182 MV |
666 | #include "libguile/srfi-4.i.c" |
667 | ||
e0e49670 MV |
668 | #define TYPE SCM_UVEC_F64 |
669 | #define TAG f64 | |
670 | #define CTYPE double | |
f8579182 MV |
671 | #include "libguile/srfi-4.i.c" |
672 | ||
cbdc8379 MV |
673 | #define TYPE SCM_UVEC_C32 |
674 | #define TAG c32 | |
675 | #define CTYPE float | |
676 | #include "libguile/srfi-4.i.c" | |
677 | ||
678 | #define TYPE SCM_UVEC_C64 | |
679 | #define TAG c64 | |
680 | #define CTYPE double | |
681 | #include "libguile/srfi-4.i.c" | |
682 | ||
683 | SCM scm_i_proc_make_u8vector; | |
684 | SCM scm_i_proc_make_s8vector; | |
685 | SCM scm_i_proc_make_u16vector; | |
686 | SCM scm_i_proc_make_s16vector; | |
687 | SCM scm_i_proc_make_u32vector; | |
688 | SCM scm_i_proc_make_s32vector; | |
689 | SCM scm_i_proc_make_u64vector; | |
690 | SCM scm_i_proc_make_s64vector; | |
691 | SCM scm_i_proc_make_f32vector; | |
692 | SCM scm_i_proc_make_f64vector; | |
693 | SCM scm_i_proc_make_c32vector; | |
694 | SCM scm_i_proc_make_c64vector; | |
f8579182 MV |
695 | |
696 | /* Create the smob type for homogeneous numeric vectors and install | |
697 | the primitives. */ | |
698 | void | |
699 | scm_init_srfi_4 (void) | |
700 | { | |
701 | scm_tc16_uvec = scm_make_smob_type ("uvec", 0); | |
702 | scm_set_smob_equalp (scm_tc16_uvec, uvec_equalp); | |
703 | scm_set_smob_free (scm_tc16_uvec, uvec_free); | |
704 | scm_set_smob_print (scm_tc16_uvec, uvec_print); | |
705 | #include "libguile/srfi-4.x" | |
cbdc8379 MV |
706 | |
707 | #define GETPROC(tag) \ | |
708 | scm_i_proc_make_##tag##vector = \ | |
709 | scm_variable_ref (scm_c_lookup ("make-"#tag"vector")) | |
710 | ||
711 | GETPROC (u8); | |
712 | GETPROC (s8); | |
713 | GETPROC (u16); | |
714 | GETPROC (s16); | |
715 | GETPROC (u32); | |
716 | GETPROC (s32); | |
717 | GETPROC (u64); | |
718 | GETPROC (s64); | |
719 | GETPROC (f32); | |
720 | GETPROC (f64); | |
721 | GETPROC (c32); | |
722 | GETPROC (c64); | |
f8579182 MV |
723 | } |
724 | ||
725 | /* End of srfi-4.c. */ |