1 /* srfi-4.c --- Homogeneous numeric vector datatypes.
3 * Copyright (C) 2001, 2004 Free Software Foundation, Inc.
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.
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.
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
25 /* For brevity and maintainability, we define our own types for the
26 various integer and floating point types. */
27 typedef scm_t_uint8 int_u8
;
28 typedef scm_t_int8 int_s8
;
29 typedef scm_t_uint16 int_u16
;
30 typedef scm_t_int16 int_s16
;
31 typedef scm_t_uint32 int_u32
;
32 typedef scm_t_int32 int_s32
;
35 typedef scm_t_uint64 int_u64
;
36 #endif /* SCM_HAVE_T_UINT64 */
39 typedef scm_t_int64 int_s64
;
40 #endif /* SCM_HAVE_T_INT64 */
42 typedef float float_f32
;
43 typedef double float_f64
;
46 /* Smob type code for homogeneous numeric vectors. */
47 int scm_tc16_uvec
= 0;
50 /* Accessor macros for the three components of a homogeneous numeric
52 - The type tag (one of the symbolic constants below).
53 - The vector's length (counted in elements).
54 - The address of the data area (holding the elements of the
56 #define SCM_UVEC_TYPE(u) (SCM_CELL_WORD_1(u))
57 #define SCM_UVEC_LENGTH(u) (SCM_CELL_WORD_2(u))
58 #define SCM_UVEC_BASE(u) (SCM_CELL_OBJECT_3(u))
61 /* Symbolic constants encoding the various types of homogeneous
65 #define SCM_UVEC_U16 2
66 #define SCM_UVEC_S16 3
67 #define SCM_UVEC_U32 4
68 #define SCM_UVEC_S32 5
69 #define SCM_UVEC_U64 6
70 #define SCM_UVEC_S64 7
71 #define SCM_UVEC_F32 8
72 #define SCM_UVEC_F64 9
75 /* This array maps type tags to the size of the elements. */
76 static const int uvec_sizes
[10] = {1, 1, 2, 2, 4, 4, 8, 8, 4, 8};
81 // Print 64 bit integers. This should go away once we have a public
82 // scm_print_integer or similar that can print a scm_t_intmax.
85 print_int64 (scm_t_int64 num
, SCM port
, scm_print_state
*pstate
)
87 scm_iprin1 (scm_from_int64 (num
), port
, pstate
);
91 print_uint64 (scm_t_uint64 num
, SCM port
, scm_print_state
*pstate
)
93 scm_iprin1 (scm_from_uint64 (num
), port
, pstate
);
96 #endif /* SCM_HAVE_T_UINT64 */
98 /* ================================================================ */
99 /* SMOB procedures. */
100 /* ================================================================ */
103 /* Smob print hook for homogeneous vectors. */
105 uvec_print (SCM uvec
, SCM port
, scm_print_state
*pstate
)
123 const size_t uvlen
= SCM_UVEC_LENGTH (uvec
);
125 void *uptr
= SCM_UVEC_BASE (uvec
);
127 switch (SCM_UVEC_TYPE (uvec
))
129 case SCM_UVEC_U8
: tagstr
= "u8"; np
.u8
= (int_u8
*) uptr
; break;
130 case SCM_UVEC_S8
: tagstr
= "s8"; np
.s8
= (int_s8
*) uptr
; break;
131 case SCM_UVEC_U16
: tagstr
= "u16"; np
.u16
= (int_u16
*) uptr
; break;
132 case SCM_UVEC_S16
: tagstr
= "s16"; np
.s16
= (int_s16
*) uptr
; break;
133 case SCM_UVEC_U32
: tagstr
= "u32"; np
.u32
= (int_u32
*) uptr
; break;
134 case SCM_UVEC_S32
: tagstr
= "s32"; np
.s32
= (int_s32
*) uptr
; break;
136 case SCM_UVEC_U64
: tagstr
= "u64"; np
.u64
= (int_u64
*) uptr
; break;
137 case SCM_UVEC_S64
: tagstr
= "s64"; np
.s64
= (int_s64
*) uptr
; break;
139 case SCM_UVEC_F32
: tagstr
= "f32"; np
.f32
= (float_f32
*) uptr
; break;
140 case SCM_UVEC_F64
: tagstr
= "f64"; np
.f64
= (float_f64
*) uptr
; break;
142 abort (); /* Sanity check. */
146 scm_putc ('#', port
);
147 scm_puts (tagstr
, port
);
148 scm_putc ('(', port
);
152 if (i
!= 0) scm_puts (" ", port
);
153 switch (SCM_UVEC_TYPE (uvec
))
155 case SCM_UVEC_U8
: scm_intprint (*np
.u8
, 10, port
); np
.u8
++; break;
156 case SCM_UVEC_S8
: scm_intprint (*np
.s8
, 10, port
); np
.s8
++; break;
157 case SCM_UVEC_U16
: scm_intprint (*np
.u16
, 10, port
); np
.u16
++; break;
158 case SCM_UVEC_S16
: scm_intprint (*np
.s16
, 10, port
); np
.s16
++; break;
159 case SCM_UVEC_U32
: scm_intprint (*np
.u32
, 10, port
); np
.u32
++; break;
160 case SCM_UVEC_S32
: scm_intprint (*np
.s32
, 10, port
); np
.s32
++; break;
162 case SCM_UVEC_U64
: print_uint64 (*np
.u64
, port
, pstate
); np
.u64
++; break;
163 case SCM_UVEC_S64
: print_int64 (*np
.s64
, port
, pstate
); np
.s64
++; break;
166 scm_iprin1 (scm_from_double (*np
.f32
), port
, pstate
);
170 scm_iprin1 (scm_from_double (*np
.f64
), port
, pstate
);
174 abort (); /* Sanity check. */
179 scm_remember_upto_here_1 (uvec
);
180 scm_puts (")", port
);
185 /* Smob free hook for homogeneous numeric vectors. */
189 scm_gc_free (SCM_UVEC_BASE (uvec
),
190 SCM_UVEC_LENGTH (uvec
) * uvec_sizes
[SCM_UVEC_TYPE (uvec
)],
196 /* ================================================================ */
197 /* Utility procedures. */
198 /* ================================================================ */
201 /* Create a new, uninitialized homogeneous numeric vector of type TYPE
202 with space for LEN elements. */
204 make_uvec (const char * func_name
, int type
, int len
)
208 p
= scm_gc_malloc (len
* uvec_sizes
[type
], "uvec");
209 SCM_RETURN_NEWSMOB3 (scm_tc16_uvec
, type
, len
, p
);
213 /* ================================================================ */
215 /* ================================================================ */
218 SCM_DEFINE (scm_u8vector_p
, "u8vector?", 1, 0, 0,
220 "Return @code{#t} if @var{obj} is a vector of type u8,\n"
221 "@code{#f} otherwise.")
222 #define FUNC_NAME s_scm_u8vector_p
224 return scm_from_bool (SCM_SMOB_PREDICATE (scm_tc16_uvec
, obj
) &&
225 SCM_UVEC_TYPE (obj
) == SCM_UVEC_U8
);
230 SCM_DEFINE (scm_make_u8vector
, "make-u8vector", 1, 1, 0,
232 "Create a newly allocated homogeneous numeric vector which can\n"
233 "hold @var{len} elements. If @var{fill} is given, it is used to\n"
234 "initialize the elements, otherwise the contents of the vector\n"
236 #define FUNC_NAME s_scm_make_u8vector
243 count
= scm_to_size_t (len
);
244 uvec
= make_uvec (FUNC_NAME
, SCM_UVEC_U8
, count
);
245 if (SCM_UNBNDP (fill
))
248 f
= scm_to_uint8 (fill
);
249 p
= (int_u8
*) SCM_UVEC_BASE (uvec
);
257 SCM_DEFINE (scm_u8vector
, "u8vector", 0, 0, 1,
259 "Create a newly allocated homogeneous numeric vector containing\n"
260 "all argument values.")
261 #define FUNC_NAME s_scm_u8vector
263 SCM_VALIDATE_REST_ARGUMENT (l
);
264 return scm_list_to_u8vector (l
);
269 SCM_DEFINE (scm_u8vector_length
, "u8vector-length", 1, 0, 0,
271 "Return the number of elements in the homogeneous numeric vector\n"
273 #define FUNC_NAME s_scm_u8vector_length
275 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
276 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_U8
)
277 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
278 return scm_from_size_t (SCM_UVEC_LENGTH (uvec
));
283 SCM_DEFINE (scm_u8vector_ref
, "u8vector-ref", 2, 0, 0,
284 (SCM uvec
, SCM index
),
285 "Return the element at @var{index} in the homogeneous numeric\n"
286 "vector @var{uvec}.")
287 #define FUNC_NAME s_scm_u8vector_ref
291 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
292 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_U8
)
293 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
295 idx
= scm_to_unsigned_integer (index
, 0, SCM_UVEC_LENGTH (uvec
)-1);
296 return scm_from_uint8 (((int_u8
*) SCM_UVEC_BASE (uvec
))[idx
]);
301 SCM_DEFINE (scm_u8vector_set_x
, "u8vector-set!", 3, 0, 0,
302 (SCM uvec
, SCM index
, SCM value
),
303 "Set the element at @var{index} in the homogeneous numeric\n"
304 "vector @var{uvec} to @var{value}. The return value is not\n"
306 #define FUNC_NAME s_scm_u8vector_ref
310 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
311 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_U8
)
312 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
314 idx
= scm_to_unsigned_integer (index
, 0, SCM_UVEC_LENGTH (uvec
)-1);
315 ((int_u8
*) SCM_UVEC_BASE (uvec
))[idx
] = scm_to_uint8 (value
);
316 return SCM_UNSPECIFIED
;
321 SCM_DEFINE (scm_u8vector_to_list
, "u8vector->list", 1, 0, 0,
323 "Convert the homogeneous numeric vector @var{uvec} to a list.")
324 #define FUNC_NAME s_scm_u8vector_to_list
330 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
331 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_U8
)
332 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
334 idx
= SCM_UVEC_LENGTH (uvec
);
335 p
= (int_u8
*) SCM_UVEC_BASE (uvec
) + idx
;
339 res
= scm_cons (scm_from_uint8 (*p
), res
);
346 SCM_DEFINE (scm_list_to_u8vector
, "list->u8vector", 1, 0, 0,
348 "Convert the list @var{l}, which must only contain unsigned\n"
349 "8-bit values, to a numeric homogeneous vector.")
350 #define FUNC_NAME s_scm_list_to_u8vector
356 SCM_VALIDATE_LIST_COPYLEN (1, l
, n
);
358 uvec
= make_uvec (FUNC_NAME
, SCM_UVEC_U8
, n
);
359 p
= (int_u8
*) SCM_UVEC_BASE (uvec
);
360 while (SCM_CONSP (l
))
362 *p
++ = scm_to_uint8 (SCM_CAR (l
));
370 /* ================================================================ */
372 /* ================================================================ */
375 SCM_DEFINE (scm_s8vector_p
, "s8vector?", 1, 0, 0,
377 "Return @code{#t} if @var{obj} is a vector of type s8,\n"
378 "@code{#f} otherwise.")
379 #define FUNC_NAME s_scm_s8vector_p
381 return scm_from_bool (SCM_SMOB_PREDICATE (scm_tc16_uvec
, obj
) &&
382 SCM_UVEC_TYPE (obj
) == SCM_UVEC_S8
);
387 SCM_DEFINE (scm_make_s8vector
, "make-s8vector", 1, 1, 0,
389 "Create a newly allocated homogeneous numeric vector which can\n"
390 "hold @var{len} elements. If @var{fill} is given, it is used to\n"
391 "initialize the elements, otherwise the contents of the vector\n"
393 #define FUNC_NAME s_scm_make_s8vector
400 count
= scm_to_size_t (len
);
401 uvec
= make_uvec (FUNC_NAME
, SCM_UVEC_S8
, count
);
402 if (SCM_UNBNDP (fill
))
405 f
= scm_to_int8 (fill
);
406 p
= (int_s8
*) SCM_UVEC_BASE (uvec
);
414 SCM_DEFINE (scm_s8vector
, "s8vector", 0, 0, 1,
416 "Create a newly allocated homogeneous numeric vector containing\n"
417 "all argument values.")
418 #define FUNC_NAME s_scm_s8vector
420 SCM_VALIDATE_REST_ARGUMENT (l
);
421 return scm_list_to_s8vector (l
);
426 SCM_DEFINE (scm_s8vector_length
, "s8vector-length", 1, 0, 0,
428 "Return the number of elements in the homogeneous numeric vector\n"
430 #define FUNC_NAME s_scm_s8vector_length
432 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
433 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_S8
)
434 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
435 return scm_from_size_t (SCM_UVEC_LENGTH (uvec
));
440 SCM_DEFINE (scm_s8vector_ref
, "s8vector-ref", 2, 0, 0,
441 (SCM uvec
, SCM index
),
442 "Return the element at @var{index} in the homogeneous numeric\n"
443 "vector @var{uvec}.")
444 #define FUNC_NAME s_scm_s8vector_ref
448 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
449 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_S8
)
450 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
452 idx
= scm_to_unsigned_integer (index
, 0, SCM_UVEC_LENGTH (uvec
)-1);
453 return scm_from_uint8 (((int_s8
*) SCM_UVEC_BASE (uvec
))[idx
]);
458 SCM_DEFINE (scm_s8vector_set_x
, "s8vector-set!", 3, 0, 0,
459 (SCM uvec
, SCM index
, SCM value
),
460 "Set the element at @var{index} in the homogeneous numeric\n"
461 "vector @var{uvec} to @var{value}. The return value is not\n"
463 #define FUNC_NAME s_scm_s8vector_ref
467 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
468 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_S8
)
469 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
471 idx
= scm_to_unsigned_integer (index
, 0, SCM_UVEC_LENGTH (uvec
)-1);
472 ((int_s8
*) SCM_UVEC_BASE (uvec
))[idx
] = scm_to_int8 (value
);
473 return SCM_UNSPECIFIED
;
478 SCM_DEFINE (scm_s8vector_to_list
, "s8vector->list", 1, 0, 0,
480 "Convert the homogeneous numeric vector @var{uvec} to a list.")
481 #define FUNC_NAME s_scm_s8vector_to_list
487 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
488 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_S8
)
489 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
491 idx
= SCM_UVEC_LENGTH (uvec
);
492 p
= (int_s8
*) SCM_UVEC_BASE (uvec
) + idx
;
496 res
= scm_cons (scm_from_int8 (*p
), res
);
503 SCM_DEFINE (scm_list_to_s8vector
, "list->s8vector", 1, 0, 0,
505 "Convert the list @var{l}, which must only contain signed\n"
506 "8-bit values, to a numeric homogeneous vector.")
507 #define FUNC_NAME s_scm_list_to_s8vector
513 SCM_VALIDATE_LIST_COPYLEN (1, l
, n
);
515 uvec
= make_uvec (FUNC_NAME
, SCM_UVEC_S8
, n
);
516 p
= (int_s8
*) SCM_UVEC_BASE (uvec
);
517 while (SCM_CONSP (l
))
519 *p
++ = scm_to_int8 (SCM_CAR (l
));
527 /* ================================================================ */
528 /* U16 procedures. */
529 /* ================================================================ */
532 SCM_DEFINE (scm_u16vector_p
, "u16vector?", 1, 0, 0,
534 "Return @code{#t} if @var{obj} is a vector of type u16,\n"
535 "@code{#f} otherwise.")
536 #define FUNC_NAME s_scm_u16vector_p
538 return scm_from_bool (SCM_SMOB_PREDICATE (scm_tc16_uvec
, obj
) &&
539 SCM_UVEC_TYPE (obj
) == SCM_UVEC_U16
);
544 SCM_DEFINE (scm_make_u16vector
, "make-u16vector", 1, 1, 0,
546 "Create a newly allocated homogeneous numeric vector which can\n"
547 "hold @var{len} elements. If @var{fill} is given, it is used to\n"
548 "initialize the elements, otherwise the contents of the vector\n"
550 #define FUNC_NAME s_scm_make_u16vector
557 count
= scm_to_size_t (len
);
558 uvec
= make_uvec (FUNC_NAME
, SCM_UVEC_U16
, count
);
559 if (SCM_UNBNDP (fill
))
562 f
= scm_to_uint16 (fill
);
563 p
= (int_u16
*) SCM_UVEC_BASE (uvec
);
571 SCM_DEFINE (scm_u16vector
, "u16vector", 0, 0, 1,
573 "Create a newly allocated homogeneous numeric vector containing\n"
574 "all argument values.")
575 #define FUNC_NAME s_scm_u16vector
577 SCM_VALIDATE_REST_ARGUMENT (l
);
578 return scm_list_to_u16vector (l
);
583 SCM_DEFINE (scm_u16vector_length
, "u16vector-length", 1, 0, 0,
585 "Return the number of elements in the homogeneous numeric vector\n"
587 #define FUNC_NAME s_scm_u16vector_length
589 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
590 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_U16
)
591 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
592 return scm_from_size_t (SCM_UVEC_LENGTH (uvec
));
597 SCM_DEFINE (scm_u16vector_ref
, "u16vector-ref", 2, 0, 0,
598 (SCM uvec
, SCM index
),
599 "Return the element at @var{index} in the homogeneous numeric\n"
600 "vector @var{uvec}.")
601 #define FUNC_NAME s_scm_u16vector_ref
605 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
606 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_U16
)
607 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
609 idx
= scm_to_unsigned_integer (index
, 0, SCM_UVEC_LENGTH (uvec
)-1);
610 return scm_from_uint16 (((int_u16
*) SCM_UVEC_BASE (uvec
))[idx
]);
615 SCM_DEFINE (scm_u16vector_set_x
, "u16vector-set!", 3, 0, 0,
616 (SCM uvec
, SCM index
, SCM value
),
617 "Set the element at @var{index} in the homogeneous numeric\n"
618 "vector @var{uvec} to @var{value}. The return value is not\n"
620 #define FUNC_NAME s_scm_u16vector_ref
624 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
625 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_U16
)
626 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
628 idx
= scm_to_unsigned_integer (index
, 0, SCM_UVEC_LENGTH (uvec
)-1);
629 ((int_u16
*) SCM_UVEC_BASE (uvec
))[idx
] = scm_to_uint16 (value
);
630 return SCM_UNSPECIFIED
;
635 SCM_DEFINE (scm_u16vector_to_list
, "u16vector->list", 1, 0, 0,
637 "Convert the homogeneous numeric vector @var{uvec} to a list.")
638 #define FUNC_NAME s_scm_u16vector_to_list
644 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
645 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_U16
)
646 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
648 idx
= SCM_UVEC_LENGTH (uvec
);
649 p
= (int_u16
*) SCM_UVEC_BASE (uvec
) + idx
;
653 res
= scm_cons (scm_from_uint16 (*p
), res
);
660 SCM_DEFINE (scm_list_to_u16vector
, "list->u16vector", 1, 0, 0,
662 "Convert the list @var{l}, which must only contain unsigned\n"
663 "16-bit values, to a numeric homogeneous vector.")
664 #define FUNC_NAME s_scm_list_to_u16vector
670 SCM_VALIDATE_LIST_COPYLEN (1, l
, n
);
672 uvec
= make_uvec (FUNC_NAME
, SCM_UVEC_U16
, n
);
673 p
= (int_u16
*) SCM_UVEC_BASE (uvec
);
674 while (SCM_CONSP (l
))
676 *p
++ = scm_to_uint16 (SCM_CAR (l
));
684 /* ================================================================ */
685 /* S16 procedures. */
686 /* ================================================================ */
689 SCM_DEFINE (scm_s16vector_p
, "s16vector?", 1, 0, 0,
691 "Return @code{#t} if @var{obj} is a vector of type s16,\n"
692 "@code{#f} otherwise.")
693 #define FUNC_NAME s_scm_s16vector_p
695 return scm_from_bool (SCM_SMOB_PREDICATE (scm_tc16_uvec
, obj
) &&
696 SCM_UVEC_TYPE (obj
) == SCM_UVEC_S16
);
701 SCM_DEFINE (scm_make_s16vector
, "make-s16vector", 1, 1, 0,
703 "Create a newly allocated homogeneous numeric vector which can\n"
704 "hold @var{len} elements. If @var{fill} is given, it is used to\n"
705 "initialize the elements, otherwise the contents of the vector\n"
707 #define FUNC_NAME s_scm_make_s16vector
714 count
= scm_to_size_t (len
);
715 uvec
= make_uvec (FUNC_NAME
, SCM_UVEC_S16
, count
);
716 if (SCM_UNBNDP (fill
))
719 f
= scm_to_int16 (fill
);
720 p
= (int_s16
*) SCM_UVEC_BASE (uvec
);
728 SCM_DEFINE (scm_s16vector
, "s16vector", 0, 0, 1,
730 "Create a newly allocated homogeneous numeric vector containing\n"
731 "all argument values.")
732 #define FUNC_NAME s_scm_s16vector
734 SCM_VALIDATE_REST_ARGUMENT (l
);
735 return scm_list_to_s16vector (l
);
740 SCM_DEFINE (scm_s16vector_length
, "s16vector-length", 1, 0, 0,
742 "Return the number of elements in the homogeneous numeric vector\n"
744 #define FUNC_NAME s_scm_s16vector_length
746 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
747 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_S16
)
748 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
749 return scm_from_size_t (SCM_UVEC_LENGTH (uvec
));
754 SCM_DEFINE (scm_s16vector_ref
, "s16vector-ref", 2, 0, 0,
755 (SCM uvec
, SCM index
),
756 "Return the element at @var{index} in the homogeneous numeric\n"
757 "vector @var{uvec}.")
758 #define FUNC_NAME s_scm_s16vector_ref
762 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
763 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_S16
)
764 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
766 idx
= scm_to_unsigned_integer (index
, 0, SCM_UVEC_LENGTH (uvec
)-1);
767 return scm_from_int16 (((int_s16
*) SCM_UVEC_BASE (uvec
))[idx
]);
772 SCM_DEFINE (scm_s16vector_set_x
, "s16vector-set!", 3, 0, 0,
773 (SCM uvec
, SCM index
, SCM value
),
774 "Set the element at @var{index} in the homogeneous numeric\n"
775 "vector @var{uvec} to @var{value}. The return value is not\n"
777 #define FUNC_NAME s_scm_s16vector_ref
781 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
782 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_S16
)
783 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
785 idx
= scm_to_unsigned_integer (index
, 0, SCM_UVEC_LENGTH (uvec
)-1);
786 ((int_s16
*) SCM_UVEC_BASE (uvec
))[idx
] = scm_to_int16 (value
);
787 return SCM_UNSPECIFIED
;
792 SCM_DEFINE (scm_s16vector_to_list
, "s16vector->list", 1, 0, 0,
794 "Convert the homogeneous numeric vector @var{uvec} to a list.")
795 #define FUNC_NAME s_scm_s16vector_to_list
801 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
802 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_S16
)
803 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
805 idx
= SCM_UVEC_LENGTH (uvec
);
806 p
= (int_s16
*) SCM_UVEC_BASE (uvec
) + idx
;
810 res
= scm_cons (scm_from_int16 (*p
), res
);
817 SCM_DEFINE (scm_list_to_s16vector
, "list->s16vector", 1, 0, 0,
819 "Convert the list @var{l}, which must only contain signed\n"
820 "16-bit values, to a numeric homogeneous vector.")
821 #define FUNC_NAME s_scm_list_to_s16vector
827 SCM_VALIDATE_LIST_COPYLEN (1, l
, n
);
829 uvec
= make_uvec (FUNC_NAME
, SCM_UVEC_S16
, n
);
830 p
= (int_s16
*) SCM_UVEC_BASE (uvec
);
831 while (SCM_CONSP (l
))
833 *p
++ = scm_to_int16 (SCM_CAR (l
));
841 /* ================================================================ */
842 /* U32 procedures. */
843 /* ================================================================ */
846 SCM_DEFINE (scm_u32vector_p
, "u32vector?", 1, 0, 0,
848 "Return @code{#t} if @var{obj} is a vector of type u32,\n"
849 "@code{#f} otherwise.")
850 #define FUNC_NAME s_scm_u32vector_p
852 return scm_from_bool (SCM_SMOB_PREDICATE (scm_tc16_uvec
, obj
) &&
853 SCM_UVEC_TYPE (obj
) == SCM_UVEC_U32
);
858 SCM_DEFINE (scm_make_u32vector
, "make-u32vector", 1, 1, 0,
860 "Create a newly allocated homogeneous numeric vector which can\n"
861 "hold @var{len} elements. If @var{fill} is given, it is used to\n"
862 "initialize the elements, otherwise the contents of the vector\n"
864 #define FUNC_NAME s_scm_make_u32vector
871 count
= scm_to_size_t (len
);
872 uvec
= make_uvec (FUNC_NAME
, SCM_UVEC_U32
, count
);
873 if (SCM_UNBNDP (fill
))
876 f
= scm_to_uint32 (fill
);
877 p
= (int_u32
*) SCM_UVEC_BASE (uvec
);
885 SCM_DEFINE (scm_u32vector
, "u32vector", 0, 0, 1,
887 "Create a newly allocated homogeneous numeric vector containing\n"
888 "all argument values.")
889 #define FUNC_NAME s_scm_u32vector
891 SCM_VALIDATE_REST_ARGUMENT (l
);
892 return scm_list_to_u32vector (l
);
897 SCM_DEFINE (scm_u32vector_length
, "u32vector-length", 1, 0, 0,
899 "Return the number of elements in the homogeneous numeric vector\n"
901 #define FUNC_NAME s_scm_u32vector_length
903 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
904 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_U32
)
905 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
906 return scm_from_size_t (SCM_UVEC_LENGTH (uvec
));
911 SCM_DEFINE (scm_u32vector_ref
, "u32vector-ref", 2, 0, 0,
912 (SCM uvec
, SCM index
),
913 "Return the element at @var{index} in the homogeneous numeric\n"
914 "vector @var{uvec}.")
915 #define FUNC_NAME s_scm_u32vector_ref
919 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
920 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_U32
)
921 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
923 idx
= scm_to_unsigned_integer (index
, 0, SCM_UVEC_LENGTH (uvec
)-1);
924 return scm_from_uint32 (((int_u32
*) SCM_UVEC_BASE (uvec
))[idx
]);
929 SCM_DEFINE (scm_u32vector_set_x
, "u32vector-set!", 3, 0, 0,
930 (SCM uvec
, SCM index
, SCM value
),
931 "Set the element at @var{index} in the homogeneous numeric\n"
932 "vector @var{uvec} to @var{value}. The return value is not\n"
934 #define FUNC_NAME s_scm_u32vector_ref
938 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
939 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_U32
)
940 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
942 idx
= scm_to_unsigned_integer (index
, 0, SCM_UVEC_LENGTH (uvec
)-1);
943 ((int_u32
*) SCM_UVEC_BASE (uvec
))[idx
] = scm_to_uint32 (value
);
944 return SCM_UNSPECIFIED
;
949 SCM_DEFINE (scm_u32vector_to_list
, "u32vector->list", 1, 0, 0,
951 "Convert the homogeneous numeric vector @var{uvec} to a list.")
952 #define FUNC_NAME s_scm_u32vector_to_list
958 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
959 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_U32
)
960 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
962 idx
= SCM_UVEC_LENGTH (uvec
);
963 p
= (int_u32
*) SCM_UVEC_BASE (uvec
) + idx
;
967 res
= scm_cons (scm_from_uint32 (*p
), res
);
974 SCM_DEFINE (scm_list_to_u32vector
, "list->u32vector", 1, 0, 0,
976 "Convert the list @var{l}, which must only contain unsigned\n"
977 "32-bit values, to a numeric homogeneous vector.")
978 #define FUNC_NAME s_scm_list_to_u32vector
984 SCM_VALIDATE_LIST_COPYLEN (1, l
, n
);
986 uvec
= make_uvec (FUNC_NAME
, SCM_UVEC_U32
, n
);
987 p
= (int_u32
*) SCM_UVEC_BASE (uvec
);
988 while (SCM_CONSP (l
))
990 *p
++ = scm_to_uint32 (SCM_CAR (l
));
998 /* ================================================================ */
999 /* S32 procedures. */
1000 /* ================================================================ */
1003 SCM_DEFINE (scm_s32vector_p
, "s32vector?", 1, 0, 0,
1005 "Return @code{#t} if @var{obj} is a vector of type s32,\n"
1006 "@code{#f} otherwise.")
1007 #define FUNC_NAME s_scm_s32vector_p
1009 return scm_from_bool (SCM_SMOB_PREDICATE (scm_tc16_uvec
, obj
) &&
1010 SCM_UVEC_TYPE (obj
) == SCM_UVEC_S32
);
1015 SCM_DEFINE (scm_make_s32vector
, "make-s32vector", 1, 1, 0,
1016 (SCM len
, SCM fill
),
1017 "Create a newly allocated homogeneous numeric vector which can\n"
1018 "hold @var{len} elements. If @var{fill} is given, it is used to\n"
1019 "initialize the elements, otherwise the contents of the vector\n"
1021 #define FUNC_NAME s_scm_make_s32vector
1028 count
= scm_to_size_t (len
);
1029 uvec
= make_uvec (FUNC_NAME
, SCM_UVEC_S32
, count
);
1030 if (SCM_UNBNDP (fill
))
1033 f
= scm_to_int32 (fill
);
1034 p
= (int_s32
*) SCM_UVEC_BASE (uvec
);
1042 SCM_DEFINE (scm_s32vector
, "s32vector", 0, 0, 1,
1044 "Create a newly allocated homogeneous numeric vector containing\n"
1045 "all argument values.")
1046 #define FUNC_NAME s_scm_s32vector
1048 SCM_VALIDATE_REST_ARGUMENT (l
);
1049 return scm_list_to_s32vector (l
);
1054 SCM_DEFINE (scm_s32vector_length
, "s32vector-length", 1, 0, 0,
1056 "Return the number of elements in the homogeneous numeric vector\n"
1058 #define FUNC_NAME s_scm_s32vector_length
1060 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
1061 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_S32
)
1062 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
1063 return scm_from_size_t (SCM_UVEC_LENGTH (uvec
));
1068 SCM_DEFINE (scm_s32vector_ref
, "s32vector-ref", 2, 0, 0,
1069 (SCM uvec
, SCM index
),
1070 "Return the element at @var{index} in the homogeneous numeric\n"
1071 "vector @var{uvec}.")
1072 #define FUNC_NAME s_scm_s32vector_ref
1076 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
1077 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_S32
)
1078 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
1080 idx
= scm_to_unsigned_integer (index
, 0, SCM_UVEC_LENGTH (uvec
)-1);
1081 return scm_from_int32 (((int_s32
*) SCM_UVEC_BASE (uvec
))[idx
]);
1086 SCM_DEFINE (scm_s32vector_set_x
, "s32vector-set!", 3, 0, 0,
1087 (SCM uvec
, SCM index
, SCM value
),
1088 "Set the element at @var{index} in the homogeneous numeric\n"
1089 "vector @var{uvec} to @var{value}. The return value is not\n"
1091 #define FUNC_NAME s_scm_s32vector_ref
1095 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
1096 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_S32
)
1097 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
1099 idx
= scm_to_unsigned_integer (index
, 0, SCM_UVEC_LENGTH (uvec
)-1);
1100 ((int_s32
*) SCM_UVEC_BASE (uvec
))[idx
] = scm_to_int32 (value
);
1101 return SCM_UNSPECIFIED
;
1106 SCM_DEFINE (scm_s32vector_to_list
, "s32vector->list", 1, 0, 0,
1108 "Convert the homogeneous numeric vector @var{uvec} to a list.")
1109 #define FUNC_NAME s_scm_s32vector_to_list
1115 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
1116 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_S32
)
1117 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
1119 idx
= SCM_UVEC_LENGTH (uvec
);
1120 p
= (int_s32
*) SCM_UVEC_BASE (uvec
) + idx
;
1124 res
= scm_cons (scm_from_int32 (*p
), res
);
1131 SCM_DEFINE (scm_list_to_s32vector
, "list->s32vector", 1, 0, 0,
1133 "Convert the list @var{l}, which must only contain signed\n"
1134 "32-bit values, to a numeric homogeneous vector.")
1135 #define FUNC_NAME s_scm_list_to_s32vector
1141 SCM_VALIDATE_LIST_COPYLEN (1, l
, n
);
1143 uvec
= make_uvec (FUNC_NAME
, SCM_UVEC_S32
, n
);
1144 p
= (int_s32
*) SCM_UVEC_BASE (uvec
);
1145 while (SCM_CONSP (l
))
1147 *p
++ = scm_to_int32 (SCM_CAR (l
));
1155 #if SCM_HAVE_T_INT64
1157 /* ================================================================ */
1158 /* U64 procedures. */
1159 /* ================================================================ */
1162 SCM_DEFINE (scm_u64vector_p
, "u64vector?", 1, 0, 0,
1164 "Return @code{#t} if @var{obj} is a vector of type u64,\n"
1165 "@code{#f} otherwise.")
1166 #define FUNC_NAME s_scm_u64vector_p
1168 return scm_from_bool (SCM_SMOB_PREDICATE (scm_tc16_uvec
, obj
) &&
1169 SCM_UVEC_TYPE (obj
) == SCM_UVEC_U64
);
1174 SCM_DEFINE (scm_make_u64vector
, "make-u64vector", 1, 1, 0,
1175 (SCM len
, SCM fill
),
1176 "Create a newly allocated homogeneous numeric vector which can\n"
1177 "hold @var{len} elements. If @var{fill} is given, it is used to\n"
1178 "initialize the elements, otherwise the contents of the vector\n"
1180 #define FUNC_NAME s_scm_make_u64vector
1187 count
= scm_to_size_t (len
);
1188 uvec
= make_uvec (FUNC_NAME
, SCM_UVEC_U64
, count
);
1189 if (SCM_UNBNDP (fill
))
1192 f
= scm_to_uint64 (fill
);
1193 p
= (int_u64
*) SCM_UVEC_BASE (uvec
);
1201 SCM_DEFINE (scm_u64vector
, "u64vector", 0, 0, 1,
1203 "Create a newly allocated homogeneous numeric vector containing\n"
1204 "all argument values.")
1205 #define FUNC_NAME s_scm_u64vector
1207 SCM_VALIDATE_REST_ARGUMENT (l
);
1208 return scm_list_to_u64vector (l
);
1213 SCM_DEFINE (scm_u64vector_length
, "u64vector-length", 1, 0, 0,
1215 "Return the number of elements in the homogeneous numeric vector\n"
1217 #define FUNC_NAME s_scm_u64vector_length
1219 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
1220 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_U64
)
1221 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
1222 return scm_from_size_t (SCM_UVEC_LENGTH (uvec
));
1227 SCM_DEFINE (scm_u64vector_ref
, "u64vector-ref", 2, 0, 0,
1228 (SCM uvec
, SCM index
),
1229 "Return the element at @var{index} in the homogeneous numeric\n"
1230 "vector @var{uvec}.")
1231 #define FUNC_NAME s_scm_u64vector_ref
1235 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
1236 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_U64
)
1237 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
1239 idx
= scm_to_unsigned_integer (index
, 0, SCM_UVEC_LENGTH (uvec
)-1);
1240 return scm_from_uint64 (((int_u64
*) SCM_UVEC_BASE (uvec
))[idx
]);
1245 SCM_DEFINE (scm_u64vector_set_x
, "u64vector-set!", 3, 0, 0,
1246 (SCM uvec
, SCM index
, SCM value
),
1247 "Set the element at @var{index} in the homogeneous numeric\n"
1248 "vector @var{uvec} to @var{value}. The return value is not\n"
1250 #define FUNC_NAME s_scm_u64vector_ref
1254 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
1255 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_U64
)
1256 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
1258 idx
= scm_to_unsigned_integer (index
, 0, SCM_UVEC_LENGTH (uvec
)-1);
1259 ((int_u64
*) SCM_UVEC_BASE (uvec
))[idx
] = scm_to_uint64 (value
);
1260 return SCM_UNSPECIFIED
;
1265 SCM_DEFINE (scm_u64vector_to_list
, "u64vector->list", 1, 0, 0,
1267 "Convert the homogeneous numeric vector @var{uvec} to a list.")
1268 #define FUNC_NAME s_scm_u64vector_to_list
1274 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
1275 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_U64
)
1276 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
1278 idx
= SCM_UVEC_LENGTH (uvec
);
1279 p
= (int_u64
*) SCM_UVEC_BASE (uvec
) + idx
;
1283 res
= scm_cons (scm_from_long_long (*p
), res
);
1290 SCM_DEFINE (scm_list_to_u64vector
, "list->u64vector", 1, 0, 0,
1292 "Convert the list @var{l}, which must only contain unsigned\n"
1293 "64-bit values, to a numeric homogeneous vector.")
1294 #define FUNC_NAME s_scm_list_to_u64vector
1300 SCM_VALIDATE_LIST_COPYLEN (1, l
, n
);
1302 uvec
= make_uvec (FUNC_NAME
, SCM_UVEC_U64
, n
);
1303 p
= (int_u64
*) SCM_UVEC_BASE (uvec
);
1304 while (SCM_CONSP (l
))
1306 *p
++ = scm_to_uint64 (SCM_CAR (l
));
1314 /* ================================================================ */
1315 /* S64 procedures. */
1316 /* ================================================================ */
1319 SCM_DEFINE (scm_s64vector_p
, "s64vector?", 1, 0, 0,
1321 "Return @code{#t} if @var{obj} is a vector of type s64,\n"
1322 "@code{#f} otherwise.")
1323 #define FUNC_NAME s_scm_s64vector_p
1325 return scm_from_bool (SCM_SMOB_PREDICATE (scm_tc16_uvec
, obj
) &&
1326 SCM_UVEC_TYPE (obj
) == SCM_UVEC_S64
);
1331 SCM_DEFINE (scm_make_s64vector
, "make-s64vector", 1, 1, 0,
1332 (SCM len
, SCM fill
),
1333 "Create a newly allocated homogeneous numeric vector which can\n"
1334 "hold @var{len} elements. If @var{fill} is given, it is used to\n"
1335 "initialize the elements, otherwise the contents of the vector\n"
1337 #define FUNC_NAME s_scm_make_s64vector
1344 count
= scm_to_size_t (len
);
1345 uvec
= make_uvec (FUNC_NAME
, SCM_UVEC_S64
, count
);
1346 if (SCM_UNBNDP (fill
))
1349 f
= scm_to_int64 (fill
);
1350 p
= (int_s64
*) SCM_UVEC_BASE (uvec
);
1358 SCM_DEFINE (scm_s64vector
, "s64vector", 0, 0, 1,
1360 "Create a newly allocated homogeneous numeric vector containing\n"
1361 "all argument values.")
1362 #define FUNC_NAME s_scm_s64vector
1364 SCM_VALIDATE_REST_ARGUMENT (l
);
1365 return scm_list_to_s64vector (l
);
1370 SCM_DEFINE (scm_s64vector_length
, "s64vector-length", 1, 0, 0,
1372 "Return the number of elements in the homogeneous numeric vector\n"
1374 #define FUNC_NAME s_scm_s64vector_length
1376 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
1377 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_S64
)
1378 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
1379 return scm_from_size_t (SCM_UVEC_LENGTH (uvec
));
1384 SCM_DEFINE (scm_s64vector_ref
, "s64vector-ref", 2, 0, 0,
1385 (SCM uvec
, SCM index
),
1386 "Return the element at @var{index} in the homogeneous numeric\n"
1387 "vector @var{uvec}.")
1388 #define FUNC_NAME s_scm_s64vector_ref
1392 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
1393 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_S64
)
1394 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
1396 idx
= scm_to_unsigned_integer (index
, 0, SCM_UVEC_LENGTH (uvec
)-1);
1397 return scm_from_int64 (((int_s64
*) SCM_UVEC_BASE (uvec
))[idx
]);
1402 SCM_DEFINE (scm_s64vector_set_x
, "s64vector-set!", 3, 0, 0,
1403 (SCM uvec
, SCM index
, SCM value
),
1404 "Set the element at @var{index} in the homogeneous numeric\n"
1405 "vector @var{uvec} to @var{value}. The return value is not\n"
1407 #define FUNC_NAME s_scm_s64vector_ref
1411 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
1412 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_S64
)
1413 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
1415 idx
= scm_to_unsigned_integer (index
, 0, SCM_UVEC_LENGTH (uvec
)-1);
1416 ((int_s64
*) SCM_UVEC_BASE (uvec
))[idx
] = scm_to_int64 (value
);
1417 return SCM_UNSPECIFIED
;
1422 SCM_DEFINE (scm_s64vector_to_list
, "s64vector->list", 1, 0, 0,
1424 "Convert the homogeneous numeric vector @var{uvec} to a list.")
1425 #define FUNC_NAME s_scm_s64vector_to_list
1431 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
1432 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_S64
)
1433 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
1435 idx
= SCM_UVEC_LENGTH (uvec
);
1436 p
= (int_s64
*) SCM_UVEC_BASE (uvec
) + idx
;
1440 res
= scm_cons (scm_from_int64 (*p
), res
);
1447 SCM_DEFINE (scm_list_to_s64vector
, "list->s64vector", 1, 0, 0,
1449 "Convert the list @var{l}, which must only contain signed\n"
1450 "64-bit values, to a numeric homogeneous vector.")
1451 #define FUNC_NAME s_scm_list_to_s64vector
1457 SCM_VALIDATE_LIST_COPYLEN (1, l
, n
);
1459 uvec
= make_uvec (FUNC_NAME
, SCM_UVEC_S64
, n
);
1460 p
= (int_s64
*) SCM_UVEC_BASE (uvec
);
1461 while (SCM_CONSP (l
))
1463 *p
++ = scm_to_int64 (SCM_CAR (l
));
1470 #endif /* SCM_HAVE_T_INT64 */
1473 /* ================================================================ */
1474 /* F32 procedures. */
1475 /* ================================================================ */
1478 SCM_DEFINE (scm_f32vector_p
, "f32vector?", 1, 0, 0,
1480 "Return @code{#t} if @var{obj} is a vector of type f32,\n"
1481 "@code{#f} otherwise.")
1482 #define FUNC_NAME s_scm_f32vector_p
1484 return scm_from_bool (SCM_SMOB_PREDICATE (scm_tc16_uvec
, obj
) &&
1485 SCM_UVEC_TYPE (obj
) == SCM_UVEC_F32
);
1490 SCM_DEFINE (scm_make_f32vector
, "make-f32vector", 1, 1, 0,
1491 (SCM len
, SCM fill
),
1492 "Create a newly allocated homogeneous numeric vector which can\n"
1493 "hold @var{len} elements. If @var{fill} is given, it is used to\n"
1494 "initialize the elements, otherwise the contents of the vector\n"
1496 #define FUNC_NAME s_scm_make_f32vector
1503 count
= scm_to_size_t (len
);
1504 uvec
= make_uvec (FUNC_NAME
, SCM_UVEC_F32
, count
);
1505 if (SCM_UNBNDP (fill
))
1508 f
= scm_to_double (fill
);
1509 p
= (float_f32
*) SCM_UVEC_BASE (uvec
);
1517 SCM_DEFINE (scm_f32vector
, "f32vector", 0, 0, 1,
1519 "Create a newly allocated homogeneous numeric vector containing\n"
1520 "all argument values.")
1521 #define FUNC_NAME s_scm_f32vector
1523 SCM_VALIDATE_REST_ARGUMENT (l
);
1524 return scm_list_to_f32vector (l
);
1529 SCM_DEFINE (scm_f32vector_length
, "f32vector-length", 1, 0, 0,
1531 "Return the number of elements in the homogeneous numeric vector\n"
1533 #define FUNC_NAME s_scm_f32vector_length
1535 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
1536 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_F32
)
1537 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
1538 return scm_from_size_t (SCM_UVEC_LENGTH (uvec
));
1543 SCM_DEFINE (scm_f32vector_ref
, "f32vector-ref", 2, 0, 0,
1544 (SCM uvec
, SCM index
),
1545 "Return the element at @var{index} in the homogeneous numeric\n"
1546 "vector @var{uvec}.")
1547 #define FUNC_NAME s_scm_f32vector_ref
1551 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
1552 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_F32
)
1553 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
1555 idx
= scm_to_unsigned_integer (index
, 0, SCM_UVEC_LENGTH (uvec
)-1);
1556 return scm_from_double (((float_f32
*) SCM_UVEC_BASE (uvec
))[idx
]);
1561 SCM_DEFINE (scm_f32vector_set_x
, "f32vector-set!", 3, 0, 0,
1562 (SCM uvec
, SCM index
, SCM value
),
1563 "Set the element at @var{index} in the homogeneous numeric\n"
1564 "vector @var{uvec} to @var{value}. The return value is not\n"
1566 #define FUNC_NAME s_scm_f32vector_ref
1570 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
1571 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_F32
)
1572 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
1574 idx
= scm_to_unsigned_integer (index
, 0, SCM_UVEC_LENGTH (uvec
)-1);
1575 ((float_f32
*) SCM_UVEC_BASE (uvec
))[idx
] = scm_to_double (value
);
1576 return SCM_UNSPECIFIED
;
1581 SCM_DEFINE (scm_f32vector_to_list
, "f32vector->list", 1, 0, 0,
1583 "Convert the homogeneous numeric vector @var{uvec} to a list.")
1584 #define FUNC_NAME s_scm_f32vector_to_list
1590 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
1591 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_F32
)
1592 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
1594 idx
= SCM_UVEC_LENGTH (uvec
);
1595 p
= (float_f32
*) SCM_UVEC_BASE (uvec
) + idx
;
1599 res
= scm_cons (scm_from_double (*p
), res
);
1606 SCM_DEFINE (scm_list_to_f32vector
, "list->f32vector", 1, 0, 0,
1608 "Convert the list @var{l}, which must only contain unsigned\n"
1609 "8-bit values, to a numeric homogeneous vector.")
1610 #define FUNC_NAME s_scm_list_to_f32vector
1616 SCM_VALIDATE_LIST_COPYLEN (1, l
, n
);
1618 uvec
= make_uvec (FUNC_NAME
, SCM_UVEC_F32
, n
);
1619 p
= (float_f32
*) SCM_UVEC_BASE (uvec
);
1620 while (SCM_CONSP (l
))
1622 *p
++ = scm_to_double (SCM_CAR (l
));
1630 /* ================================================================ */
1631 /* F64 procedures. */
1632 /* ================================================================ */
1635 SCM_DEFINE (scm_f64vector_p
, "f64vector?", 1, 0, 0,
1637 "Return @code{#t} if @var{obj} is a vector of type f64,\n"
1638 "@code{#f} otherwise.")
1639 #define FUNC_NAME s_scm_f64vector_p
1641 return scm_from_bool (SCM_SMOB_PREDICATE (scm_tc16_uvec
, obj
) &&
1642 SCM_UVEC_TYPE (obj
) == SCM_UVEC_F64
);
1647 SCM_DEFINE (scm_make_f64vector
, "make-f64vector", 1, 1, 0,
1648 (SCM len
, SCM fill
),
1649 "Create a newly allocated homogeneous numeric vector which can\n"
1650 "hold @var{len} elements. If @var{fill} is given, it is used to\n"
1651 "initialize the elements, otherwise the contents of the vector\n"
1653 #define FUNC_NAME s_scm_make_f64vector
1660 count
= scm_to_size_t (len
);
1661 uvec
= make_uvec (FUNC_NAME
, SCM_UVEC_F64
, count
);
1662 if (SCM_UNBNDP (fill
))
1665 f
= scm_to_double (fill
);
1666 p
= (float_f64
*) SCM_UVEC_BASE (uvec
);
1674 SCM_DEFINE (scm_f64vector
, "f64vector", 0, 0, 1,
1676 "Create a newly allocated homogeneous numeric vector containing\n"
1677 "all argument values.")
1678 #define FUNC_NAME s_scm_f64vector
1680 SCM_VALIDATE_REST_ARGUMENT (l
);
1681 return scm_list_to_f64vector (l
);
1686 SCM_DEFINE (scm_f64vector_length
, "f64vector-length", 1, 0, 0,
1688 "Return the number of elements in the homogeneous numeric vector\n"
1690 #define FUNC_NAME s_scm_f64vector_length
1692 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
1693 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_F64
)
1694 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
1695 return scm_from_size_t (SCM_UVEC_LENGTH (uvec
));
1700 SCM_DEFINE (scm_f64vector_ref
, "f64vector-ref", 2, 0, 0,
1701 (SCM uvec
, SCM index
),
1702 "Return the element at @var{index} in the homogeneous numeric\n"
1703 "vector @var{uvec}.")
1704 #define FUNC_NAME s_scm_f64vector_ref
1708 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
1709 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_F64
)
1710 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
1712 idx
= scm_to_unsigned_integer (index
, 0, SCM_UVEC_LENGTH (uvec
)-1);
1713 return scm_from_double (((float_f64
*) SCM_UVEC_BASE (uvec
))[idx
]);
1718 SCM_DEFINE (scm_f64vector_set_x
, "f64vector-set!", 3, 0, 0,
1719 (SCM uvec
, SCM index
, SCM value
),
1720 "Set the element at @var{index} in the homogeneous numeric\n"
1721 "vector @var{uvec} to @var{value}. The return value is not\n"
1723 #define FUNC_NAME s_scm_f64vector_ref
1727 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
1728 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_F64
)
1729 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
1731 idx
= scm_to_unsigned_integer (index
, 0, SCM_UVEC_LENGTH (uvec
)-1);
1732 ((float_f64
*) SCM_UVEC_BASE (uvec
))[idx
] = scm_to_double (value
);
1733 return SCM_UNSPECIFIED
;
1738 SCM_DEFINE (scm_f64vector_to_list
, "f64vector->list", 1, 0, 0,
1740 "Convert the homogeneous numeric vector @var{uvec} to a list.")
1741 #define FUNC_NAME s_scm_f64vector_to_list
1747 SCM_VALIDATE_SMOB (1, uvec
, uvec
);
1748 if (SCM_UVEC_TYPE (uvec
) != SCM_UVEC_F64
)
1749 scm_wrong_type_arg (FUNC_NAME
, 1, uvec
);
1751 idx
= SCM_UVEC_LENGTH (uvec
);
1752 p
= (float_f64
*) SCM_UVEC_BASE (uvec
) + idx
;
1756 res
= scm_cons (scm_from_double (*p
), res
);
1763 SCM_DEFINE (scm_list_to_f64vector
, "list->f64vector", 1, 0, 0,
1765 "Convert the list @var{l}, which must only contain signed\n"
1766 "8-bit values, to a numeric homogeneous vector.")
1767 #define FUNC_NAME s_scm_list_to_f64vector
1773 SCM_VALIDATE_LIST_COPYLEN (1, l
, n
);
1775 uvec
= make_uvec (FUNC_NAME
, SCM_UVEC_F64
, n
);
1776 p
= (float_f64
*) SCM_UVEC_BASE (uvec
);
1777 while (SCM_CONSP (l
))
1779 *p
++ = scm_to_double (SCM_CAR (l
));
1787 /* Create the smob type for homogeneous numeric vectors and install
1790 scm_init_srfi_4 (void)
1792 scm_tc16_uvec
= scm_make_smob_type ("uvec", 0);
1793 scm_set_smob_free (scm_tc16_uvec
, uvec_free
);
1794 scm_set_smob_print (scm_tc16_uvec
, uvec_print
);
1795 #include "srfi/srfi-4.x"
1798 /* End of srfi-4.c. */