1 /* Copyright (C) 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
29 #include "libguile/_scm.h"
30 #include "libguile/extensions.h"
31 #include "libguile/bytevectors.h"
32 #include "libguile/strings.h"
33 #include "libguile/validate.h"
34 #include "libguile/arrays.h"
35 #include "libguile/array-handle.h"
36 #include "libguile/uniform.h"
37 #include "libguile/srfi-4.h"
40 #include <striconveh.h>
47 /* Assuming 32-bit longs. */
48 # define ULONG_MAX 4294967295UL
57 /* Convenience macros. These are used by the various templates (macros) that
58 are parameterized by integer signedness. */
59 #define INT8_T_signed scm_t_int8
60 #define INT8_T_unsigned scm_t_uint8
61 #define INT16_T_signed scm_t_int16
62 #define INT16_T_unsigned scm_t_uint16
63 #define INT32_T_signed scm_t_int32
64 #define INT32_T_unsigned scm_t_uint32
65 #define is_signed_int8(_x) (((_x) >= -128L) && ((_x) <= 127L))
66 #define is_unsigned_int8(_x) ((_x) <= 255UL)
67 #define is_signed_int16(_x) (((_x) >= -32768L) && ((_x) <= 32767L))
68 #define is_unsigned_int16(_x) ((_x) <= 65535UL)
69 #define is_signed_int32(_x) (((_x) >= -2147483648L) && ((_x) <= 2147483647L))
70 #define is_unsigned_int32(_x) ((_x) <= 4294967295UL)
71 #define SIGNEDNESS_signed 1
72 #define SIGNEDNESS_unsigned 0
74 #define INT_TYPE(_size, _sign) INT ## _size ## _T_ ## _sign
75 #define INT_SWAP(_size) bswap_ ## _size
76 #define INT_VALID_P(_size, _sign) is_ ## _sign ## _int ## _size
77 #define SIGNEDNESS(_sign) SIGNEDNESS_ ## _sign
80 #define INTEGER_ACCESSOR_PROLOGUE(_len, _sign) \
81 size_t c_len, c_index; \
84 SCM_VALIDATE_BYTEVECTOR (1, bv); \
85 c_index = scm_to_uint (index); \
87 c_len = SCM_BYTEVECTOR_LENGTH (bv); \
88 c_bv = (_sign char *) SCM_BYTEVECTOR_CONTENTS (bv); \
90 if (SCM_UNLIKELY (c_index + ((_len) >> 3UL) - 1 >= c_len)) \
91 scm_out_of_range (FUNC_NAME, index);
93 /* Template for fixed-size integer access (only 8, 16 or 32-bit). */
94 #define INTEGER_REF(_len, _sign) \
97 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
98 SCM_VALIDATE_SYMBOL (3, endianness); \
101 INT_TYPE (_len, _sign) c_result; \
103 memcpy (&c_result, &c_bv[c_index], (_len) / 8); \
104 if (!scm_is_eq (endianness, scm_i_native_endianness)) \
105 c_result = INT_SWAP (_len) (c_result); \
107 result = SCM_I_MAKINUM (c_result); \
112 /* Template for fixed-size integer access using the native endianness. */
113 #define INTEGER_NATIVE_REF(_len, _sign) \
116 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
119 INT_TYPE (_len, _sign) c_result; \
121 memcpy (&c_result, &c_bv[c_index], (_len) / 8); \
122 result = SCM_I_MAKINUM (c_result); \
127 /* Template for fixed-size integer modification (only 8, 16 or 32-bit). */
128 #define INTEGER_SET(_len, _sign) \
129 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
130 SCM_VALIDATE_SYMBOL (3, endianness); \
133 scm_t_signed_bits c_value; \
134 INT_TYPE (_len, _sign) c_value_short; \
136 if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
137 scm_wrong_type_arg (FUNC_NAME, 3, value); \
139 c_value = SCM_I_INUM (value); \
140 if (SCM_UNLIKELY (!INT_VALID_P (_len, _sign) (c_value))) \
141 scm_out_of_range (FUNC_NAME, value); \
143 c_value_short = (INT_TYPE (_len, _sign)) c_value; \
144 if (!scm_is_eq (endianness, scm_i_native_endianness)) \
145 c_value_short = INT_SWAP (_len) (c_value_short); \
147 memcpy (&c_bv[c_index], &c_value_short, (_len) / 8); \
150 return SCM_UNSPECIFIED;
152 /* Template for fixed-size integer modification using the native
154 #define INTEGER_NATIVE_SET(_len, _sign) \
155 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
158 scm_t_signed_bits c_value; \
159 INT_TYPE (_len, _sign) c_value_short; \
161 if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
162 scm_wrong_type_arg (FUNC_NAME, 3, value); \
164 c_value = SCM_I_INUM (value); \
165 if (SCM_UNLIKELY (!INT_VALID_P (_len, _sign) (c_value))) \
166 scm_out_of_range (FUNC_NAME, value); \
168 c_value_short = (INT_TYPE (_len, _sign)) c_value; \
170 memcpy (&c_bv[c_index], &c_value_short, (_len) / 8); \
173 return SCM_UNSPECIFIED;
177 /* Bytevector type. */
179 #define SCM_BYTEVECTOR_HEADER_BYTES \
180 (SCM_BYTEVECTOR_HEADER_SIZE * sizeof (scm_t_bits))
182 #define SCM_BYTEVECTOR_SET_LENGTH(_bv, _len) \
183 SCM_SET_CELL_WORD_1 ((_bv), (scm_t_bits) (_len))
184 #define SCM_BYTEVECTOR_SET_CONTENTS(_bv, _contents) \
185 SCM_SET_CELL_WORD_2 ((_bv), (scm_t_bits) (_contents))
186 #define SCM_BYTEVECTOR_SET_CONTIGUOUS_P(bv, contiguous_p) \
187 SCM_SET_BYTEVECTOR_FLAGS ((bv), \
188 SCM_BYTEVECTOR_ELEMENT_TYPE (bv) \
189 | ((contiguous_p) << 8UL))
191 #define SCM_BYTEVECTOR_SET_ELEMENT_TYPE(bv, hint) \
192 SCM_SET_BYTEVECTOR_FLAGS ((bv), \
194 | (SCM_BYTEVECTOR_CONTIGUOUS_P (bv) << 8UL))
195 #define SCM_BYTEVECTOR_SET_PARENT(_bv, _parent) \
196 SCM_SET_CELL_OBJECT_3 ((_bv), (_parent))
198 #define SCM_BYTEVECTOR_TYPE_SIZE(var) \
199 (scm_i_array_element_type_sizes[SCM_BYTEVECTOR_ELEMENT_TYPE (var)]/8)
200 #define SCM_BYTEVECTOR_TYPED_LENGTH(var) \
201 (SCM_BYTEVECTOR_LENGTH (var) / SCM_BYTEVECTOR_TYPE_SIZE (var))
203 /* The empty bytevector. */
204 SCM scm_null_bytevector
= SCM_UNSPECIFIED
;
208 make_bytevector (size_t len
, scm_t_array_element_type element_type
)
213 if (SCM_UNLIKELY (element_type
> SCM_ARRAY_ELEMENT_TYPE_LAST
214 || scm_i_array_element_type_sizes
[element_type
] < 8
215 || len
>= (((size_t) -1)
216 / (scm_i_array_element_type_sizes
[element_type
]/8))))
217 /* This would be an internal Guile programming error */
220 if (SCM_UNLIKELY (len
== 0 && element_type
== SCM_ARRAY_ELEMENT_TYPE_VU8
221 && SCM_BYTEVECTOR_P (scm_null_bytevector
)))
222 ret
= scm_null_bytevector
;
225 signed char *contents
;
227 c_len
= len
* (scm_i_array_element_type_sizes
[element_type
] / 8);
229 contents
= scm_gc_malloc_pointerless (SCM_BYTEVECTOR_HEADER_BYTES
+ c_len
,
231 ret
= SCM_PACK_POINTER (contents
);
232 contents
+= SCM_BYTEVECTOR_HEADER_BYTES
;
234 SCM_BYTEVECTOR_SET_LENGTH (ret
, c_len
);
235 SCM_BYTEVECTOR_SET_CONTENTS (ret
, contents
);
236 SCM_BYTEVECTOR_SET_CONTIGUOUS_P (ret
, 1);
237 SCM_BYTEVECTOR_SET_ELEMENT_TYPE (ret
, element_type
);
238 SCM_BYTEVECTOR_SET_PARENT (ret
, SCM_BOOL_F
);
244 /* Return a bytevector of LEN elements of type ELEMENT_TYPE, with element
245 values taken from CONTENTS. Assume that the storage for CONTENTS will be
246 automatically reclaimed when it becomes unreachable. */
248 make_bytevector_from_buffer (size_t len
, void *contents
,
249 scm_t_array_element_type element_type
)
253 if (SCM_UNLIKELY (len
== 0))
254 ret
= make_bytevector (len
, element_type
);
259 ret
= SCM_PACK_POINTER (scm_gc_malloc (SCM_BYTEVECTOR_HEADER_BYTES
,
262 c_len
= len
* (scm_i_array_element_type_sizes
[element_type
] / 8);
264 SCM_BYTEVECTOR_SET_LENGTH (ret
, c_len
);
265 SCM_BYTEVECTOR_SET_CONTENTS (ret
, contents
);
266 SCM_BYTEVECTOR_SET_CONTIGUOUS_P (ret
, 0);
267 SCM_BYTEVECTOR_SET_ELEMENT_TYPE (ret
, element_type
);
268 SCM_BYTEVECTOR_SET_PARENT (ret
, SCM_BOOL_F
);
275 /* Return a new bytevector of size LEN octets. */
277 scm_c_make_bytevector (size_t len
)
279 return make_bytevector (len
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
282 /* Return a new bytevector of size LEN elements. */
284 scm_i_make_typed_bytevector (size_t len
, scm_t_array_element_type element_type
)
286 return make_bytevector (len
, element_type
);
289 /* Return a bytevector of size LEN made up of CONTENTS. The area
290 pointed to by CONTENTS must be protected from GC somehow: either
291 because it was allocated using `scm_gc_malloc ()', or because it is
294 scm_c_take_gc_bytevector (signed char *contents
, size_t len
, SCM parent
)
298 ret
= make_bytevector_from_buffer (len
, contents
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
299 SCM_BYTEVECTOR_SET_PARENT (ret
, parent
);
305 scm_c_take_typed_bytevector (signed char *contents
, size_t len
,
306 scm_t_array_element_type element_type
, SCM parent
)
310 ret
= make_bytevector_from_buffer (len
, contents
, element_type
);
311 SCM_BYTEVECTOR_SET_PARENT (ret
, parent
);
316 /* Shrink BV to C_NEW_LEN (which is assumed to be smaller than its current
317 size) and return the new bytevector (possibly different from BV). */
319 scm_c_shrink_bytevector (SCM bv
, size_t c_new_len
)
324 if (SCM_UNLIKELY (c_new_len
% SCM_BYTEVECTOR_TYPE_SIZE (bv
)))
325 /* This would be an internal Guile programming error */
328 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
329 if (SCM_UNLIKELY (c_new_len
> c_len
))
332 SCM_BYTEVECTOR_SET_LENGTH (bv
, c_new_len
);
334 if (SCM_BYTEVECTOR_CONTIGUOUS_P (bv
))
335 new_bv
= PTR2SCM (scm_gc_realloc (SCM2PTR (bv
),
336 c_len
+ SCM_BYTEVECTOR_HEADER_BYTES
,
337 c_new_len
+ SCM_BYTEVECTOR_HEADER_BYTES
,
343 c_bv
= scm_gc_realloc (SCM_BYTEVECTOR_CONTENTS (bv
),
344 c_len
, c_new_len
, SCM_GC_BYTEVECTOR
);
345 SCM_BYTEVECTOR_SET_CONTENTS (bv
, c_bv
);
354 scm_is_bytevector (SCM obj
)
356 return SCM_BYTEVECTOR_P (obj
);
360 scm_c_bytevector_length (SCM bv
)
361 #define FUNC_NAME "scm_c_bytevector_length"
363 SCM_VALIDATE_BYTEVECTOR (1, bv
);
365 return SCM_BYTEVECTOR_LENGTH (bv
);
370 scm_c_bytevector_ref (SCM bv
, size_t index
)
371 #define FUNC_NAME "scm_c_bytevector_ref"
374 const scm_t_uint8
*c_bv
;
376 SCM_VALIDATE_BYTEVECTOR (1, bv
);
378 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
379 c_bv
= (scm_t_uint8
*) SCM_BYTEVECTOR_CONTENTS (bv
);
381 if (SCM_UNLIKELY (index
>= c_len
))
382 scm_out_of_range (FUNC_NAME
, scm_from_size_t (index
));
389 scm_c_bytevector_set_x (SCM bv
, size_t index
, scm_t_uint8 value
)
390 #define FUNC_NAME "scm_c_bytevector_set_x"
395 SCM_VALIDATE_BYTEVECTOR (1, bv
);
397 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
398 c_bv
= (scm_t_uint8
*) SCM_BYTEVECTOR_CONTENTS (bv
);
400 if (SCM_UNLIKELY (index
>= c_len
))
401 scm_out_of_range (FUNC_NAME
, scm_from_size_t (index
));
410 scm_i_print_bytevector (SCM bv
, SCM port
, scm_print_state
*pstate SCM_UNUSED
)
412 ssize_t ubnd
, inc
, i
;
413 scm_t_array_handle h
;
415 scm_array_get_handle (bv
, &h
);
417 scm_putc_unlocked ('#', port
);
418 scm_write (scm_array_handle_element_type (&h
), port
);
419 scm_putc_unlocked ('(', port
);
420 for (i
= h
.dims
[0].lbnd
, ubnd
= h
.dims
[0].ubnd
, inc
= h
.dims
[0].inc
;
424 scm_putc_unlocked (' ', port
);
425 scm_write (scm_array_handle_ref (&h
, i
), port
);
427 scm_putc_unlocked (')', port
);
433 /* General operations. */
435 SCM_SYMBOL (scm_sym_big
, "big");
436 SCM_SYMBOL (scm_sym_little
, "little");
438 SCM scm_endianness_big
, scm_endianness_little
;
440 /* Host endianness (a symbol). */
441 SCM scm_i_native_endianness
= SCM_UNSPECIFIED
;
445 # define bswap_24(_x) \
446 ((((_x) & 0xff0000) >> 16) | \
447 (((_x) & 0x00ff00)) | \
448 (((_x) & 0x0000ff) << 16))
452 SCM_DEFINE (scm_native_endianness
, "native-endianness", 0, 0, 0,
454 "Return a symbol denoting the machine's native endianness.")
455 #define FUNC_NAME s_scm_native_endianness
457 return scm_i_native_endianness
;
461 SCM_DEFINE (scm_bytevector_p
, "bytevector?", 1, 0, 0,
463 "Return true if @var{obj} is a bytevector.")
464 #define FUNC_NAME s_scm_bytevector_p
466 return scm_from_bool (scm_is_bytevector (obj
));
470 SCM_DEFINE (scm_make_bytevector
, "make-bytevector", 1, 1, 0,
472 "Return a newly allocated bytevector of @var{len} bytes, "
473 "optionally filled with @var{fill}.")
474 #define FUNC_NAME s_scm_make_bytevector
478 signed char c_fill
= '\0';
480 SCM_VALIDATE_UINT_COPY (1, len
, c_len
);
481 if (!scm_is_eq (fill
, SCM_UNDEFINED
))
485 value
= scm_to_int (fill
);
486 if (SCM_UNLIKELY ((value
< -128) || (value
> 255)))
487 scm_out_of_range (FUNC_NAME
, fill
);
488 c_fill
= (signed char) value
;
491 bv
= make_bytevector (c_len
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
492 if (!scm_is_eq (fill
, SCM_UNDEFINED
))
495 signed char *contents
;
497 contents
= SCM_BYTEVECTOR_CONTENTS (bv
);
498 for (i
= 0; i
< c_len
; i
++)
499 contents
[i
] = c_fill
;
502 memset (SCM_BYTEVECTOR_CONTENTS (bv
), 0, c_len
);
508 SCM_DEFINE (scm_bytevector_length
, "bytevector-length", 1, 0, 0,
510 "Return the length (in bytes) of @var{bv}.")
511 #define FUNC_NAME s_scm_bytevector_length
513 return scm_from_uint (scm_c_bytevector_length (bv
));
517 SCM_DEFINE (scm_bytevector_eq_p
, "bytevector=?", 2, 0, 0,
519 "Return is @var{bv1} equals to @var{bv2}---i.e., if they "
520 "have the same length and contents.")
521 #define FUNC_NAME s_scm_bytevector_eq_p
523 SCM result
= SCM_BOOL_F
;
524 unsigned c_len1
, c_len2
;
526 SCM_VALIDATE_BYTEVECTOR (1, bv1
);
527 SCM_VALIDATE_BYTEVECTOR (2, bv2
);
529 c_len1
= SCM_BYTEVECTOR_LENGTH (bv1
);
530 c_len2
= SCM_BYTEVECTOR_LENGTH (bv2
);
532 if (c_len1
== c_len2
&& (SCM_BYTEVECTOR_ELEMENT_TYPE (bv1
)
533 == SCM_BYTEVECTOR_ELEMENT_TYPE (bv2
)))
535 signed char *c_bv1
, *c_bv2
;
537 c_bv1
= SCM_BYTEVECTOR_CONTENTS (bv1
);
538 c_bv2
= SCM_BYTEVECTOR_CONTENTS (bv2
);
540 result
= scm_from_bool (!memcmp (c_bv1
, c_bv2
, c_len1
));
547 SCM_DEFINE (scm_bytevector_fill_x
, "bytevector-fill!", 2, 0, 0,
549 "Fill bytevector @var{bv} with @var{fill}, a byte.")
550 #define FUNC_NAME s_scm_bytevector_fill_x
553 signed char *c_bv
, c_fill
;
555 SCM_VALIDATE_BYTEVECTOR (1, bv
);
556 c_fill
= scm_to_int8 (fill
);
558 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
559 c_bv
= SCM_BYTEVECTOR_CONTENTS (bv
);
561 for (i
= 0; i
< c_len
; i
++)
564 return SCM_UNSPECIFIED
;
568 SCM_DEFINE (scm_bytevector_copy_x
, "bytevector-copy!", 5, 0, 0,
569 (SCM source
, SCM source_start
, SCM target
, SCM target_start
,
571 "Copy @var{len} bytes from @var{source} into @var{target}, "
572 "starting reading from @var{source_start} (a positive index "
573 "within @var{source}) and start writing at "
574 "@var{target_start}.")
575 #define FUNC_NAME s_scm_bytevector_copy_x
577 unsigned c_len
, c_source_len
, c_target_len
;
578 unsigned c_source_start
, c_target_start
;
579 signed char *c_source
, *c_target
;
581 SCM_VALIDATE_BYTEVECTOR (1, source
);
582 SCM_VALIDATE_BYTEVECTOR (3, target
);
584 c_len
= scm_to_uint (len
);
585 c_source_start
= scm_to_uint (source_start
);
586 c_target_start
= scm_to_uint (target_start
);
588 c_source
= SCM_BYTEVECTOR_CONTENTS (source
);
589 c_target
= SCM_BYTEVECTOR_CONTENTS (target
);
590 c_source_len
= SCM_BYTEVECTOR_LENGTH (source
);
591 c_target_len
= SCM_BYTEVECTOR_LENGTH (target
);
593 if (SCM_UNLIKELY (c_source_start
+ c_len
> c_source_len
))
594 scm_out_of_range (FUNC_NAME
, source_start
);
595 if (SCM_UNLIKELY (c_target_start
+ c_len
> c_target_len
))
596 scm_out_of_range (FUNC_NAME
, target_start
);
598 memmove (c_target
+ c_target_start
,
599 c_source
+ c_source_start
,
602 return SCM_UNSPECIFIED
;
606 SCM_DEFINE (scm_bytevector_copy
, "bytevector-copy", 1, 0, 0,
608 "Return a newly allocated copy of @var{bv}.")
609 #define FUNC_NAME s_scm_bytevector_copy
613 signed char *c_bv
, *c_copy
;
615 SCM_VALIDATE_BYTEVECTOR (1, bv
);
617 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
618 c_bv
= SCM_BYTEVECTOR_CONTENTS (bv
);
620 copy
= make_bytevector (c_len
, SCM_BYTEVECTOR_ELEMENT_TYPE (bv
));
621 c_copy
= SCM_BYTEVECTOR_CONTENTS (copy
);
622 memcpy (c_copy
, c_bv
, c_len
);
628 SCM_DEFINE (scm_uniform_array_to_bytevector
, "uniform-array->bytevector",
629 1, 0, 0, (SCM array
),
630 "Return a newly allocated bytevector whose contents\n"
631 "will be copied from the uniform array @var{array}.")
632 #define FUNC_NAME s_scm_uniform_array_to_bytevector
635 size_t len
, sz
, byte_len
;
636 scm_t_array_handle h
;
639 contents
= scm_array_contents (array
, SCM_BOOL_T
);
640 if (scm_is_false (contents
))
641 scm_wrong_type_arg_msg (FUNC_NAME
, 0, array
, "uniform contiguous array");
643 scm_array_get_handle (contents
, &h
);
644 assert (h
.base
== 0);
647 len
= h
.dims
->inc
* (h
.dims
->ubnd
- h
.dims
->lbnd
+ 1);
648 sz
= scm_array_handle_uniform_element_bit_size (&h
);
649 if (sz
>= 8 && ((sz
% 8) == 0))
650 byte_len
= len
* (sz
/ 8);
652 /* byte_len = ceil (len * sz / 8) */
653 byte_len
= (len
* sz
+ 7) / 8;
655 /* an internal guile error, really */
656 SCM_MISC_ERROR ("uniform elements larger than 8 bits must fill whole bytes", SCM_EOL
);
658 ret
= make_bytevector (byte_len
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
659 memcpy (SCM_BYTEVECTOR_CONTENTS (ret
), elts
, byte_len
);
661 scm_array_handle_release (&h
);
668 /* Operations on bytes and octets. */
670 SCM_DEFINE (scm_bytevector_u8_ref
, "bytevector-u8-ref", 2, 0, 0,
672 "Return the octet located at @var{index} in @var{bv}.")
673 #define FUNC_NAME s_scm_bytevector_u8_ref
675 INTEGER_NATIVE_REF (8, unsigned);
679 SCM_DEFINE (scm_bytevector_s8_ref
, "bytevector-s8-ref", 2, 0, 0,
681 "Return the byte located at @var{index} in @var{bv}.")
682 #define FUNC_NAME s_scm_bytevector_s8_ref
684 INTEGER_NATIVE_REF (8, signed);
688 SCM_DEFINE (scm_bytevector_u8_set_x
, "bytevector-u8-set!", 3, 0, 0,
689 (SCM bv
, SCM index
, SCM value
),
690 "Return the octet located at @var{index} in @var{bv}.")
691 #define FUNC_NAME s_scm_bytevector_u8_set_x
693 INTEGER_NATIVE_SET (8, unsigned);
697 SCM_DEFINE (scm_bytevector_s8_set_x
, "bytevector-s8-set!", 3, 0, 0,
698 (SCM bv
, SCM index
, SCM value
),
699 "Return the octet located at @var{index} in @var{bv}.")
700 #define FUNC_NAME s_scm_bytevector_s8_set_x
702 INTEGER_NATIVE_SET (8, signed);
706 #undef OCTET_ACCESSOR_PROLOGUE
709 SCM_DEFINE (scm_bytevector_to_u8_list
, "bytevector->u8-list", 1, 0, 0,
711 "Return a newly allocated list of octets containing the "
712 "contents of @var{bv}.")
713 #define FUNC_NAME s_scm_bytevector_to_u8_list
719 SCM_VALIDATE_BYTEVECTOR (1, bv
);
721 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
722 c_bv
= (unsigned char *) SCM_BYTEVECTOR_CONTENTS (bv
);
724 lst
= scm_make_list (scm_from_uint (c_len
), SCM_UNSPECIFIED
);
725 for (i
= 0, pair
= lst
;
727 i
++, pair
= SCM_CDR (pair
))
729 SCM_SETCAR (pair
, SCM_I_MAKINUM (c_bv
[i
]));
736 SCM_DEFINE (scm_u8_list_to_bytevector
, "u8-list->bytevector", 1, 0, 0,
738 "Turn @var{lst}, a list of octets, into a bytevector.")
739 #define FUNC_NAME s_scm_u8_list_to_bytevector
745 SCM_VALIDATE_LIST_COPYLEN (1, lst
, c_len
);
747 bv
= make_bytevector (c_len
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
748 c_bv
= (unsigned char *) SCM_BYTEVECTOR_CONTENTS (bv
);
750 for (i
= 0; i
< c_len
; lst
= SCM_CDR (lst
), i
++)
752 item
= SCM_CAR (lst
);
754 if (SCM_LIKELY (SCM_I_INUMP (item
)))
756 scm_t_signed_bits c_item
;
758 c_item
= SCM_I_INUM (item
);
759 if (SCM_LIKELY ((c_item
>= 0) && (c_item
< 256)))
760 c_bv
[i
] = (unsigned char) c_item
;
771 scm_wrong_type_arg (FUNC_NAME
, 1, item
);
777 /* Compute the two's complement of VALUE (a positive integer) on SIZE octets
778 using (2^(SIZE * 8) - VALUE). */
780 twos_complement (mpz_t value
, size_t size
)
782 unsigned long bit_count
;
784 /* We expect BIT_COUNT to fit in a unsigned long thanks to the range
785 checking on SIZE performed earlier. */
786 bit_count
= (unsigned long) size
<< 3UL;
788 if (SCM_LIKELY (bit_count
< sizeof (unsigned long)))
789 mpz_ui_sub (value
, 1UL << bit_count
, value
);
795 mpz_ui_pow_ui (max
, 2, bit_count
);
796 mpz_sub (value
, max
, value
);
802 bytevector_large_ref (const char *c_bv
, size_t c_size
, int signed_p
,
807 int c_endianness
, negative_p
= 0;
811 if (scm_is_eq (endianness
, scm_sym_big
))
812 negative_p
= c_bv
[0] & 0x80;
814 negative_p
= c_bv
[c_size
- 1] & 0x80;
817 c_endianness
= scm_is_eq (endianness
, scm_sym_big
) ? 1 : -1;
820 mpz_import (c_mpz
, 1 /* 1 word */, 1 /* word order doesn't matter */,
821 c_size
/* word is C_SIZE-byte long */,
823 0 /* nails */, c_bv
);
825 if (signed_p
&& negative_p
)
827 twos_complement (c_mpz
, c_size
);
828 mpz_neg (c_mpz
, c_mpz
);
831 result
= scm_from_mpz (c_mpz
);
832 mpz_clear (c_mpz
); /* FIXME: Needed? */
838 bytevector_large_set (char *c_bv
, size_t c_size
, int signed_p
,
839 SCM value
, SCM endianness
)
842 int c_endianness
, c_sign
, err
= 0;
844 c_endianness
= scm_is_eq (endianness
, scm_sym_big
) ? 1 : -1;
847 scm_to_mpz (value
, c_mpz
);
849 c_sign
= mpz_sgn (c_mpz
);
852 if (SCM_LIKELY (signed_p
))
854 mpz_neg (c_mpz
, c_mpz
);
855 twos_complement (c_mpz
, c_size
);
866 memset (c_bv
, 0, c_size
);
869 size_t word_count
, value_size
;
871 value_size
= (mpz_sizeinbase (c_mpz
, 2) + (8 * c_size
)) / (8 * c_size
);
872 if (SCM_UNLIKELY (value_size
> c_size
))
879 mpz_export (c_bv
, &word_count
, 1 /* word order doesn't matter */,
880 c_size
, c_endianness
,
881 0 /* nails */, c_mpz
);
882 if (SCM_UNLIKELY (word_count
!= 1))
883 /* Shouldn't happen since we already checked with VALUE_SIZE. */
893 #define GENERIC_INTEGER_ACCESSOR_PROLOGUE(_sign) \
894 unsigned long c_len, c_index, c_size; \
897 SCM_VALIDATE_BYTEVECTOR (1, bv); \
898 c_index = scm_to_ulong (index); \
899 c_size = scm_to_ulong (size); \
901 c_len = SCM_BYTEVECTOR_LENGTH (bv); \
902 c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
904 /* C_SIZE must have its 3 higher bits set to zero so that \
905 multiplying it by 8 yields a number that fits in an \
907 if (SCM_UNLIKELY ((c_size == 0) || (c_size >= (ULONG_MAX >> 3L)))) \
908 scm_out_of_range (FUNC_NAME, size); \
909 if (SCM_UNLIKELY (c_index + c_size > c_len)) \
910 scm_out_of_range (FUNC_NAME, index);
913 /* Template of an integer reference function. */
914 #define GENERIC_INTEGER_REF(_sign) \
922 swap = !scm_is_eq (endianness, scm_i_native_endianness); \
927 _sign char c_value8; \
928 memcpy (&c_value8, c_bv, 1); \
934 INT_TYPE (16, _sign) c_value16; \
935 memcpy (&c_value16, c_bv, 2); \
937 value = (INT_TYPE (16, _sign)) bswap_16 (c_value16); \
946 result = SCM_I_MAKINUM ((_sign int) value); \
949 result = bytevector_large_ref ((char *) c_bv, \
950 c_size, SIGNEDNESS (_sign), \
956 bytevector_signed_ref (const char *c_bv
, size_t c_size
, SCM endianness
)
958 GENERIC_INTEGER_REF (signed);
962 bytevector_unsigned_ref (const char *c_bv
, size_t c_size
, SCM endianness
)
964 GENERIC_INTEGER_REF (unsigned);
968 /* Template of an integer assignment function. */
969 #define GENERIC_INTEGER_SET(_sign) \
972 scm_t_signed_bits c_value; \
974 if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
977 c_value = SCM_I_INUM (value); \
981 if (SCM_LIKELY (INT_VALID_P (8, _sign) (c_value))) \
983 _sign char c_value8; \
984 c_value8 = (_sign char) c_value; \
985 memcpy (c_bv, &c_value8, 1); \
992 if (SCM_LIKELY (INT_VALID_P (16, _sign) (c_value))) \
995 INT_TYPE (16, _sign) c_value16; \
997 swap = !scm_is_eq (endianness, scm_i_native_endianness); \
1000 c_value16 = (INT_TYPE (16, _sign)) bswap_16 (c_value); \
1002 c_value16 = c_value; \
1004 memcpy (c_bv, &c_value16, 2); \
1018 err = bytevector_large_set (c_bv, c_size, \
1019 SIGNEDNESS (_sign), \
1020 value, endianness); \
1028 scm_out_of_range (FUNC_NAME, value); \
1032 bytevector_signed_set (char *c_bv
, size_t c_size
,
1033 SCM value
, SCM endianness
,
1034 const char *func_name
)
1035 #define FUNC_NAME func_name
1037 GENERIC_INTEGER_SET (signed);
1042 bytevector_unsigned_set (char *c_bv
, size_t c_size
,
1043 SCM value
, SCM endianness
,
1044 const char *func_name
)
1045 #define FUNC_NAME func_name
1047 GENERIC_INTEGER_SET (unsigned);
1051 #undef GENERIC_INTEGER_SET
1052 #undef GENERIC_INTEGER_REF
1055 SCM_DEFINE (scm_bytevector_uint_ref
, "bytevector-uint-ref", 4, 0, 0,
1056 (SCM bv
, SCM index
, SCM endianness
, SCM size
),
1057 "Return the @var{size}-octet long unsigned integer at index "
1058 "@var{index} in @var{bv}.")
1059 #define FUNC_NAME s_scm_bytevector_uint_ref
1061 GENERIC_INTEGER_ACCESSOR_PROLOGUE (unsigned);
1063 return (bytevector_unsigned_ref (&c_bv
[c_index
], c_size
, endianness
));
1067 SCM_DEFINE (scm_bytevector_sint_ref
, "bytevector-sint-ref", 4, 0, 0,
1068 (SCM bv
, SCM index
, SCM endianness
, SCM size
),
1069 "Return the @var{size}-octet long unsigned integer at index "
1070 "@var{index} in @var{bv}.")
1071 #define FUNC_NAME s_scm_bytevector_sint_ref
1073 GENERIC_INTEGER_ACCESSOR_PROLOGUE (signed);
1075 return (bytevector_signed_ref (&c_bv
[c_index
], c_size
, endianness
));
1079 SCM_DEFINE (scm_bytevector_uint_set_x
, "bytevector-uint-set!", 5, 0, 0,
1080 (SCM bv
, SCM index
, SCM value
, SCM endianness
, SCM size
),
1081 "Set the @var{size}-octet long unsigned integer at @var{index} "
1083 #define FUNC_NAME s_scm_bytevector_uint_set_x
1085 GENERIC_INTEGER_ACCESSOR_PROLOGUE (unsigned);
1087 bytevector_unsigned_set (&c_bv
[c_index
], c_size
, value
, endianness
,
1090 return SCM_UNSPECIFIED
;
1094 SCM_DEFINE (scm_bytevector_sint_set_x
, "bytevector-sint-set!", 5, 0, 0,
1095 (SCM bv
, SCM index
, SCM value
, SCM endianness
, SCM size
),
1096 "Set the @var{size}-octet long signed integer at @var{index} "
1098 #define FUNC_NAME s_scm_bytevector_sint_set_x
1100 GENERIC_INTEGER_ACCESSOR_PROLOGUE (signed);
1102 bytevector_signed_set (&c_bv
[c_index
], c_size
, value
, endianness
,
1105 return SCM_UNSPECIFIED
;
1111 /* Operations on integers of arbitrary size. */
1113 #define INTEGERS_TO_LIST(_sign) \
1115 size_t i, c_len, c_size; \
1117 SCM_VALIDATE_BYTEVECTOR (1, bv); \
1118 SCM_VALIDATE_SYMBOL (2, endianness); \
1119 c_size = scm_to_unsigned_integer (size, 1, (size_t) -1); \
1121 c_len = SCM_BYTEVECTOR_LENGTH (bv); \
1122 if (SCM_UNLIKELY (c_len % c_size != 0)) \
1123 scm_wrong_type_arg_msg \
1124 (FUNC_NAME, 0, size, \
1125 "an exact positive integer that divides the bytevector length"); \
1126 else if (SCM_UNLIKELY (c_len == 0)) \
1132 c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
1134 lst = scm_make_list (scm_from_size_t (c_len / c_size), \
1136 for (i = 0, pair = lst; \
1137 i <= c_len - c_size; \
1138 i += c_size, c_bv += c_size, pair = SCM_CDR (pair)) \
1141 bytevector_ ## _sign ## _ref (c_bv, c_size, \
1148 SCM_DEFINE (scm_bytevector_to_sint_list
, "bytevector->sint-list",
1150 (SCM bv
, SCM endianness
, SCM size
),
1151 "Return a list of signed integers of @var{size} octets "
1152 "representing the contents of @var{bv}.")
1153 #define FUNC_NAME s_scm_bytevector_to_sint_list
1155 INTEGERS_TO_LIST (signed);
1159 SCM_DEFINE (scm_bytevector_to_uint_list
, "bytevector->uint-list",
1161 (SCM bv
, SCM endianness
, SCM size
),
1162 "Return a list of unsigned integers of @var{size} octets "
1163 "representing the contents of @var{bv}.")
1164 #define FUNC_NAME s_scm_bytevector_to_uint_list
1166 INTEGERS_TO_LIST (unsigned);
1170 #undef INTEGER_TO_LIST
1173 #define INTEGER_LIST_TO_BYTEVECTOR(_sign) \
1177 char *c_bv, *c_bv_ptr; \
1179 SCM_VALIDATE_LIST_COPYLEN (1, lst, c_len); \
1180 SCM_VALIDATE_SYMBOL (2, endianness); \
1181 c_size = scm_to_uint (size); \
1183 if (SCM_UNLIKELY ((c_size == 0) || (c_size >= (ULONG_MAX >> 3L)))) \
1184 scm_out_of_range (FUNC_NAME, size); \
1186 bv = make_bytevector (c_len * c_size, SCM_ARRAY_ELEMENT_TYPE_VU8); \
1187 c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
1189 for (c_bv_ptr = c_bv; \
1190 !scm_is_null (lst); \
1191 lst = SCM_CDR (lst), c_bv_ptr += c_size) \
1193 bytevector_ ## _sign ## _set (c_bv_ptr, c_size, \
1194 SCM_CAR (lst), endianness, \
1201 SCM_DEFINE (scm_uint_list_to_bytevector
, "uint-list->bytevector",
1203 (SCM lst
, SCM endianness
, SCM size
),
1204 "Return a bytevector containing the unsigned integers "
1205 "listed in @var{lst} and encoded on @var{size} octets "
1206 "according to @var{endianness}.")
1207 #define FUNC_NAME s_scm_uint_list_to_bytevector
1209 INTEGER_LIST_TO_BYTEVECTOR (unsigned);
1213 SCM_DEFINE (scm_sint_list_to_bytevector
, "sint-list->bytevector",
1215 (SCM lst
, SCM endianness
, SCM size
),
1216 "Return a bytevector containing the signed integers "
1217 "listed in @var{lst} and encoded on @var{size} octets "
1218 "according to @var{endianness}.")
1219 #define FUNC_NAME s_scm_sint_list_to_bytevector
1221 INTEGER_LIST_TO_BYTEVECTOR (signed);
1225 #undef INTEGER_LIST_TO_BYTEVECTOR
1229 /* Operations on 16-bit integers. */
1231 SCM_DEFINE (scm_bytevector_u16_ref
, "bytevector-u16-ref",
1233 (SCM bv
, SCM index
, SCM endianness
),
1234 "Return the unsigned 16-bit integer from @var{bv} at "
1236 #define FUNC_NAME s_scm_bytevector_u16_ref
1238 INTEGER_REF (16, unsigned);
1242 SCM_DEFINE (scm_bytevector_s16_ref
, "bytevector-s16-ref",
1244 (SCM bv
, SCM index
, SCM endianness
),
1245 "Return the signed 16-bit integer from @var{bv} at "
1247 #define FUNC_NAME s_scm_bytevector_s16_ref
1249 INTEGER_REF (16, signed);
1253 SCM_DEFINE (scm_bytevector_u16_native_ref
, "bytevector-u16-native-ref",
1255 (SCM bv
, SCM index
),
1256 "Return the unsigned 16-bit integer from @var{bv} at "
1257 "@var{index} using the native endianness.")
1258 #define FUNC_NAME s_scm_bytevector_u16_native_ref
1260 INTEGER_NATIVE_REF (16, unsigned);
1264 SCM_DEFINE (scm_bytevector_s16_native_ref
, "bytevector-s16-native-ref",
1266 (SCM bv
, SCM index
),
1267 "Return the unsigned 16-bit integer from @var{bv} at "
1268 "@var{index} using the native endianness.")
1269 #define FUNC_NAME s_scm_bytevector_s16_native_ref
1271 INTEGER_NATIVE_REF (16, signed);
1275 SCM_DEFINE (scm_bytevector_u16_set_x
, "bytevector-u16-set!",
1277 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1278 "Store @var{value} in @var{bv} at @var{index} according to "
1279 "@var{endianness}.")
1280 #define FUNC_NAME s_scm_bytevector_u16_set_x
1282 INTEGER_SET (16, unsigned);
1286 SCM_DEFINE (scm_bytevector_s16_set_x
, "bytevector-s16-set!",
1288 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1289 "Store @var{value} in @var{bv} at @var{index} according to "
1290 "@var{endianness}.")
1291 #define FUNC_NAME s_scm_bytevector_s16_set_x
1293 INTEGER_SET (16, signed);
1297 SCM_DEFINE (scm_bytevector_u16_native_set_x
, "bytevector-u16-native-set!",
1299 (SCM bv
, SCM index
, SCM value
),
1300 "Store the unsigned integer @var{value} at index @var{index} "
1301 "of @var{bv} using the native endianness.")
1302 #define FUNC_NAME s_scm_bytevector_u16_native_set_x
1304 INTEGER_NATIVE_SET (16, unsigned);
1308 SCM_DEFINE (scm_bytevector_s16_native_set_x
, "bytevector-s16-native-set!",
1310 (SCM bv
, SCM index
, SCM value
),
1311 "Store the signed integer @var{value} at index @var{index} "
1312 "of @var{bv} using the native endianness.")
1313 #define FUNC_NAME s_scm_bytevector_s16_native_set_x
1315 INTEGER_NATIVE_SET (16, signed);
1321 /* Operations on 32-bit integers. */
1323 /* Unfortunately, on 32-bit machines `SCM' is not large enough to hold
1324 arbitrary 32-bit integers. Thus we fall back to using the
1325 `large_{ref,set}' variants on 32-bit machines. */
1327 #define LARGE_INTEGER_REF(_len, _sign) \
1328 INTEGER_ACCESSOR_PROLOGUE(_len, _sign); \
1329 SCM_VALIDATE_SYMBOL (3, endianness); \
1331 return (bytevector_large_ref ((char *) c_bv + c_index, _len / 8, \
1332 SIGNEDNESS (_sign), endianness));
1334 #define LARGE_INTEGER_SET(_len, _sign) \
1336 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
1337 SCM_VALIDATE_SYMBOL (4, endianness); \
1339 err = bytevector_large_set ((char *) c_bv + c_index, _len / 8, \
1340 SIGNEDNESS (_sign), value, endianness); \
1341 if (SCM_UNLIKELY (err)) \
1342 scm_out_of_range (FUNC_NAME, value); \
1344 return SCM_UNSPECIFIED;
1346 #define LARGE_INTEGER_NATIVE_REF(_len, _sign) \
1347 INTEGER_ACCESSOR_PROLOGUE(_len, _sign); \
1348 return (bytevector_large_ref ((char *) c_bv + c_index, _len / 8, \
1349 SIGNEDNESS (_sign), scm_i_native_endianness));
1351 #define LARGE_INTEGER_NATIVE_SET(_len, _sign) \
1353 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
1355 err = bytevector_large_set ((char *) c_bv + c_index, _len / 8, \
1356 SIGNEDNESS (_sign), value, \
1357 scm_i_native_endianness); \
1358 if (SCM_UNLIKELY (err)) \
1359 scm_out_of_range (FUNC_NAME, value); \
1361 return SCM_UNSPECIFIED;
1364 SCM_DEFINE (scm_bytevector_u32_ref
, "bytevector-u32-ref",
1366 (SCM bv
, SCM index
, SCM endianness
),
1367 "Return the unsigned 32-bit integer from @var{bv} at "
1369 #define FUNC_NAME s_scm_bytevector_u32_ref
1371 #if SIZEOF_VOID_P > 4
1372 INTEGER_REF (32, unsigned);
1374 LARGE_INTEGER_REF (32, unsigned);
1379 SCM_DEFINE (scm_bytevector_s32_ref
, "bytevector-s32-ref",
1381 (SCM bv
, SCM index
, SCM endianness
),
1382 "Return the signed 32-bit integer from @var{bv} at "
1384 #define FUNC_NAME s_scm_bytevector_s32_ref
1386 #if SIZEOF_VOID_P > 4
1387 INTEGER_REF (32, signed);
1389 LARGE_INTEGER_REF (32, signed);
1394 SCM_DEFINE (scm_bytevector_u32_native_ref
, "bytevector-u32-native-ref",
1396 (SCM bv
, SCM index
),
1397 "Return the unsigned 32-bit integer from @var{bv} at "
1398 "@var{index} using the native endianness.")
1399 #define FUNC_NAME s_scm_bytevector_u32_native_ref
1401 #if SIZEOF_VOID_P > 4
1402 INTEGER_NATIVE_REF (32, unsigned);
1404 LARGE_INTEGER_NATIVE_REF (32, unsigned);
1409 SCM_DEFINE (scm_bytevector_s32_native_ref
, "bytevector-s32-native-ref",
1411 (SCM bv
, SCM index
),
1412 "Return the unsigned 32-bit integer from @var{bv} at "
1413 "@var{index} using the native endianness.")
1414 #define FUNC_NAME s_scm_bytevector_s32_native_ref
1416 #if SIZEOF_VOID_P > 4
1417 INTEGER_NATIVE_REF (32, signed);
1419 LARGE_INTEGER_NATIVE_REF (32, signed);
1424 SCM_DEFINE (scm_bytevector_u32_set_x
, "bytevector-u32-set!",
1426 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1427 "Store @var{value} in @var{bv} at @var{index} according to "
1428 "@var{endianness}.")
1429 #define FUNC_NAME s_scm_bytevector_u32_set_x
1431 #if SIZEOF_VOID_P > 4
1432 INTEGER_SET (32, unsigned);
1434 LARGE_INTEGER_SET (32, unsigned);
1439 SCM_DEFINE (scm_bytevector_s32_set_x
, "bytevector-s32-set!",
1441 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1442 "Store @var{value} in @var{bv} at @var{index} according to "
1443 "@var{endianness}.")
1444 #define FUNC_NAME s_scm_bytevector_s32_set_x
1446 #if SIZEOF_VOID_P > 4
1447 INTEGER_SET (32, signed);
1449 LARGE_INTEGER_SET (32, signed);
1454 SCM_DEFINE (scm_bytevector_u32_native_set_x
, "bytevector-u32-native-set!",
1456 (SCM bv
, SCM index
, SCM value
),
1457 "Store the unsigned integer @var{value} at index @var{index} "
1458 "of @var{bv} using the native endianness.")
1459 #define FUNC_NAME s_scm_bytevector_u32_native_set_x
1461 #if SIZEOF_VOID_P > 4
1462 INTEGER_NATIVE_SET (32, unsigned);
1464 LARGE_INTEGER_NATIVE_SET (32, unsigned);
1469 SCM_DEFINE (scm_bytevector_s32_native_set_x
, "bytevector-s32-native-set!",
1471 (SCM bv
, SCM index
, SCM value
),
1472 "Store the signed integer @var{value} at index @var{index} "
1473 "of @var{bv} using the native endianness.")
1474 #define FUNC_NAME s_scm_bytevector_s32_native_set_x
1476 #if SIZEOF_VOID_P > 4
1477 INTEGER_NATIVE_SET (32, signed);
1479 LARGE_INTEGER_NATIVE_SET (32, signed);
1486 /* Operations on 64-bit integers. */
1488 /* For 64-bit integers, we use only the `large_{ref,set}' variant. */
1490 SCM_DEFINE (scm_bytevector_u64_ref
, "bytevector-u64-ref",
1492 (SCM bv
, SCM index
, SCM endianness
),
1493 "Return the unsigned 64-bit integer from @var{bv} at "
1495 #define FUNC_NAME s_scm_bytevector_u64_ref
1497 LARGE_INTEGER_REF (64, unsigned);
1501 SCM_DEFINE (scm_bytevector_s64_ref
, "bytevector-s64-ref",
1503 (SCM bv
, SCM index
, SCM endianness
),
1504 "Return the signed 64-bit integer from @var{bv} at "
1506 #define FUNC_NAME s_scm_bytevector_s64_ref
1508 LARGE_INTEGER_REF (64, signed);
1512 SCM_DEFINE (scm_bytevector_u64_native_ref
, "bytevector-u64-native-ref",
1514 (SCM bv
, SCM index
),
1515 "Return the unsigned 64-bit integer from @var{bv} at "
1516 "@var{index} using the native endianness.")
1517 #define FUNC_NAME s_scm_bytevector_u64_native_ref
1519 LARGE_INTEGER_NATIVE_REF (64, unsigned);
1523 SCM_DEFINE (scm_bytevector_s64_native_ref
, "bytevector-s64-native-ref",
1525 (SCM bv
, SCM index
),
1526 "Return the unsigned 64-bit integer from @var{bv} at "
1527 "@var{index} using the native endianness.")
1528 #define FUNC_NAME s_scm_bytevector_s64_native_ref
1530 LARGE_INTEGER_NATIVE_REF (64, signed);
1534 SCM_DEFINE (scm_bytevector_u64_set_x
, "bytevector-u64-set!",
1536 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1537 "Store @var{value} in @var{bv} at @var{index} according to "
1538 "@var{endianness}.")
1539 #define FUNC_NAME s_scm_bytevector_u64_set_x
1541 LARGE_INTEGER_SET (64, unsigned);
1545 SCM_DEFINE (scm_bytevector_s64_set_x
, "bytevector-s64-set!",
1547 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1548 "Store @var{value} in @var{bv} at @var{index} according to "
1549 "@var{endianness}.")
1550 #define FUNC_NAME s_scm_bytevector_s64_set_x
1552 LARGE_INTEGER_SET (64, signed);
1556 SCM_DEFINE (scm_bytevector_u64_native_set_x
, "bytevector-u64-native-set!",
1558 (SCM bv
, SCM index
, SCM value
),
1559 "Store the unsigned integer @var{value} at index @var{index} "
1560 "of @var{bv} using the native endianness.")
1561 #define FUNC_NAME s_scm_bytevector_u64_native_set_x
1563 LARGE_INTEGER_NATIVE_SET (64, unsigned);
1567 SCM_DEFINE (scm_bytevector_s64_native_set_x
, "bytevector-s64-native-set!",
1569 (SCM bv
, SCM index
, SCM value
),
1570 "Store the signed integer @var{value} at index @var{index} "
1571 "of @var{bv} using the native endianness.")
1572 #define FUNC_NAME s_scm_bytevector_s64_native_set_x
1574 LARGE_INTEGER_NATIVE_SET (64, signed);
1580 /* Operations on IEEE-754 numbers. */
1582 /* There are two possible word endians, visible in glibc's <ieee754.h>.
1583 However, in R6RS, when the endianness is `little', little endian is
1584 assumed for both the byte order and the word order. This is clear from
1585 Section 2.1 of R6RS-lib (in response to
1586 http://www.r6rs.org/formal-comments/comment-187.txt). */
1588 union scm_ieee754_float
1594 union scm_ieee754_double
1601 /* Convert to/from a floating-point number with different endianness. This
1602 method is probably not the most efficient but it should be portable. */
1605 float_to_foreign_endianness (union scm_ieee754_float
*target
,
1608 union scm_ieee754_float input
;
1611 target
->i
= bswap_32 (input
.i
);
1615 float_from_foreign_endianness (const union scm_ieee754_float
*source
)
1617 union scm_ieee754_float result
;
1619 result
.i
= bswap_32 (source
->i
);
1625 double_to_foreign_endianness (union scm_ieee754_double
*target
,
1628 union scm_ieee754_double input
;
1631 target
->i
= bswap_64 (input
.i
);
1634 static inline double
1635 double_from_foreign_endianness (const union scm_ieee754_double
*source
)
1637 union scm_ieee754_double result
;
1639 result
.i
= bswap_64 (source
->i
);
1644 /* Template macros to abstract over doubles and floats.
1645 XXX: Guile can only convert to/from doubles. */
1646 #define IEEE754_UNION(_c_type) union scm_ieee754_ ## _c_type
1647 #define IEEE754_TO_SCM(_c_type) scm_from_double
1648 #define IEEE754_FROM_SCM(_c_type) scm_to_double
1649 #define IEEE754_FROM_FOREIGN_ENDIANNESS(_c_type) \
1650 _c_type ## _from_foreign_endianness
1651 #define IEEE754_TO_FOREIGN_ENDIANNESS(_c_type) \
1652 _c_type ## _to_foreign_endianness
1655 /* FIXME: SCM_VALIDATE_REAL rejects integers, etc. grrr */
1656 #define VALIDATE_REAL(pos, v) \
1658 SCM_ASSERT_TYPE (scm_is_real (v), v, pos, FUNC_NAME, "real"); \
1661 /* Templace getters and setters. */
1663 #define IEEE754_ACCESSOR_PROLOGUE(_type) \
1664 INTEGER_ACCESSOR_PROLOGUE (sizeof (_type) << 3UL, signed);
1666 #define IEEE754_REF(_type) \
1669 IEEE754_ACCESSOR_PROLOGUE (_type); \
1670 SCM_VALIDATE_SYMBOL (3, endianness); \
1672 if (scm_is_eq (endianness, scm_i_native_endianness)) \
1673 memcpy (&c_result, &c_bv[c_index], sizeof (c_result)); \
1676 IEEE754_UNION (_type) c_raw; \
1678 memcpy (&c_raw, &c_bv[c_index], sizeof (c_raw)); \
1680 IEEE754_FROM_FOREIGN_ENDIANNESS (_type) (&c_raw); \
1683 return (IEEE754_TO_SCM (_type) (c_result));
1685 #define IEEE754_NATIVE_REF(_type) \
1688 IEEE754_ACCESSOR_PROLOGUE (_type); \
1690 memcpy (&c_result, &c_bv[c_index], sizeof (c_result)); \
1691 return (IEEE754_TO_SCM (_type) (c_result));
1693 #define IEEE754_SET(_type) \
1696 IEEE754_ACCESSOR_PROLOGUE (_type); \
1697 VALIDATE_REAL (3, value); \
1698 SCM_VALIDATE_SYMBOL (4, endianness); \
1699 c_value = IEEE754_FROM_SCM (_type) (value); \
1701 if (scm_is_eq (endianness, scm_i_native_endianness)) \
1702 memcpy (&c_bv[c_index], &c_value, sizeof (c_value)); \
1705 IEEE754_UNION (_type) c_raw; \
1707 IEEE754_TO_FOREIGN_ENDIANNESS (_type) (&c_raw, c_value); \
1708 memcpy (&c_bv[c_index], &c_raw, sizeof (c_raw)); \
1711 return SCM_UNSPECIFIED;
1713 #define IEEE754_NATIVE_SET(_type) \
1716 IEEE754_ACCESSOR_PROLOGUE (_type); \
1717 VALIDATE_REAL (3, value); \
1718 c_value = IEEE754_FROM_SCM (_type) (value); \
1720 memcpy (&c_bv[c_index], &c_value, sizeof (c_value)); \
1721 return SCM_UNSPECIFIED;
1724 /* Single precision. */
1726 SCM_DEFINE (scm_bytevector_ieee_single_ref
,
1727 "bytevector-ieee-single-ref",
1729 (SCM bv
, SCM index
, SCM endianness
),
1730 "Return the IEEE-754 single from @var{bv} at "
1732 #define FUNC_NAME s_scm_bytevector_ieee_single_ref
1734 IEEE754_REF (float);
1738 SCM_DEFINE (scm_bytevector_ieee_single_native_ref
,
1739 "bytevector-ieee-single-native-ref",
1741 (SCM bv
, SCM index
),
1742 "Return the IEEE-754 single from @var{bv} at "
1743 "@var{index} using the native endianness.")
1744 #define FUNC_NAME s_scm_bytevector_ieee_single_native_ref
1746 IEEE754_NATIVE_REF (float);
1750 SCM_DEFINE (scm_bytevector_ieee_single_set_x
,
1751 "bytevector-ieee-single-set!",
1753 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1754 "Store real @var{value} in @var{bv} at @var{index} according to "
1755 "@var{endianness}.")
1756 #define FUNC_NAME s_scm_bytevector_ieee_single_set_x
1758 IEEE754_SET (float);
1762 SCM_DEFINE (scm_bytevector_ieee_single_native_set_x
,
1763 "bytevector-ieee-single-native-set!",
1765 (SCM bv
, SCM index
, SCM value
),
1766 "Store the real @var{value} at index @var{index} "
1767 "of @var{bv} using the native endianness.")
1768 #define FUNC_NAME s_scm_bytevector_ieee_single_native_set_x
1770 IEEE754_NATIVE_SET (float);
1775 /* Double precision. */
1777 SCM_DEFINE (scm_bytevector_ieee_double_ref
,
1778 "bytevector-ieee-double-ref",
1780 (SCM bv
, SCM index
, SCM endianness
),
1781 "Return the IEEE-754 double from @var{bv} at "
1783 #define FUNC_NAME s_scm_bytevector_ieee_double_ref
1785 IEEE754_REF (double);
1789 SCM_DEFINE (scm_bytevector_ieee_double_native_ref
,
1790 "bytevector-ieee-double-native-ref",
1792 (SCM bv
, SCM index
),
1793 "Return the IEEE-754 double from @var{bv} at "
1794 "@var{index} using the native endianness.")
1795 #define FUNC_NAME s_scm_bytevector_ieee_double_native_ref
1797 IEEE754_NATIVE_REF (double);
1801 SCM_DEFINE (scm_bytevector_ieee_double_set_x
,
1802 "bytevector-ieee-double-set!",
1804 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1805 "Store real @var{value} in @var{bv} at @var{index} according to "
1806 "@var{endianness}.")
1807 #define FUNC_NAME s_scm_bytevector_ieee_double_set_x
1809 IEEE754_SET (double);
1813 SCM_DEFINE (scm_bytevector_ieee_double_native_set_x
,
1814 "bytevector-ieee-double-native-set!",
1816 (SCM bv
, SCM index
, SCM value
),
1817 "Store the real @var{value} at index @var{index} "
1818 "of @var{bv} using the native endianness.")
1819 #define FUNC_NAME s_scm_bytevector_ieee_double_native_set_x
1821 IEEE754_NATIVE_SET (double);
1826 #undef IEEE754_UNION
1827 #undef IEEE754_TO_SCM
1828 #undef IEEE754_FROM_SCM
1829 #undef IEEE754_FROM_FOREIGN_ENDIANNESS
1830 #undef IEEE754_TO_FOREIGN_ENDIANNESS
1832 #undef IEEE754_NATIVE_REF
1834 #undef IEEE754_NATIVE_SET
1837 /* Operations on strings. */
1840 /* Produce a function that returns the length of a UTF-encoded string. */
1841 #define UTF_STRLEN_FUNCTION(_utf_width) \
1842 static inline size_t \
1843 utf ## _utf_width ## _strlen (const uint ## _utf_width ## _t *str) \
1846 const uint ## _utf_width ## _t *ptr; \
1854 return (len * ((_utf_width) / 8)); \
1857 UTF_STRLEN_FUNCTION (8)
1860 /* Return the length (in bytes) of STR, a UTF-(UTF_WIDTH) encoded string. */
1861 #define UTF_STRLEN(_utf_width, _str) \
1862 utf ## _utf_width ## _strlen (_str)
1864 /* Return the "portable" name of the UTF encoding of size UTF_WIDTH and
1865 ENDIANNESS (Gnulib's `iconv_open' module guarantees the portability of the
1868 utf_encoding_name (char *name
, size_t utf_width
, SCM endianness
)
1870 strcpy (name
, "UTF-");
1871 strcat (name
, ((utf_width
== 8)
1873 : ((utf_width
== 16)
1875 : ((utf_width
== 32)
1879 ((scm_is_eq (endianness
, scm_sym_big
))
1881 : ((scm_is_eq (endianness
, scm_sym_little
))
1886 /* Maximum length of a UTF encoding name. */
1887 #define MAX_UTF_ENCODING_NAME_LEN 16
1889 /* Produce the body of a `string->utf' function. */
1890 #define STRING_TO_UTF(_utf_width) \
1893 char c_utf_name[MAX_UTF_ENCODING_NAME_LEN]; \
1894 char *c_utf = NULL; \
1895 size_t c_strlen, c_utf_len = 0; \
1897 SCM_VALIDATE_STRING (1, str); \
1898 if (scm_is_eq (endianness, SCM_UNDEFINED)) \
1899 endianness = scm_sym_big; \
1901 SCM_VALIDATE_SYMBOL (2, endianness); \
1903 utf_encoding_name (c_utf_name, (_utf_width), endianness); \
1905 c_strlen = scm_i_string_length (str); \
1906 if (scm_i_is_narrow_string (str)) \
1908 err = mem_iconveh (scm_i_string_chars (str), c_strlen, \
1909 "ISO-8859-1", c_utf_name, \
1910 iconveh_question_mark, NULL, \
1911 &c_utf, &c_utf_len); \
1912 if (SCM_UNLIKELY (err)) \
1913 scm_syserror_msg (FUNC_NAME, "failed to convert string: ~A", \
1914 scm_list_1 (str), err); \
1918 const scm_t_wchar *wbuf = scm_i_string_wide_chars (str); \
1919 c_utf = u32_conv_to_encoding (c_utf_name, \
1920 iconveh_question_mark, \
1921 (scm_t_uint32 *) wbuf, \
1922 c_strlen, NULL, NULL, &c_utf_len); \
1923 if (SCM_UNLIKELY (c_utf == NULL)) \
1924 scm_syserror_msg (FUNC_NAME, "failed to convert string: ~A", \
1925 scm_list_1 (str), errno); \
1927 scm_dynwind_begin (0); \
1928 scm_dynwind_free (c_utf); \
1929 utf = make_bytevector (c_utf_len, SCM_ARRAY_ELEMENT_TYPE_VU8); \
1930 memcpy (SCM_BYTEVECTOR_CONTENTS (utf), c_utf, c_utf_len); \
1931 scm_dynwind_end (); \
1937 SCM_DEFINE (scm_string_to_utf8
, "string->utf8",
1940 "Return a newly allocated bytevector that contains the UTF-8 "
1941 "encoding of @var{str}.")
1942 #define FUNC_NAME s_scm_string_to_utf8
1946 size_t c_utf_len
= 0;
1948 SCM_VALIDATE_STRING (1, str
);
1950 c_utf
= (scm_t_uint8
*) scm_to_utf8_stringn (str
, &c_utf_len
);
1951 utf
= make_bytevector (c_utf_len
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
1952 memcpy (SCM_BYTEVECTOR_CONTENTS (utf
), c_utf
, c_utf_len
);
1959 SCM_DEFINE (scm_string_to_utf16
, "string->utf16",
1961 (SCM str
, SCM endianness
),
1962 "Return a newly allocated bytevector that contains the UTF-16 "
1963 "encoding of @var{str}.")
1964 #define FUNC_NAME s_scm_string_to_utf16
1971 swap_u32 (scm_t_wchar
*vals
, size_t len
)
1974 for (n
= 0; n
< len
; n
++)
1975 vals
[n
] = bswap_32 (vals
[n
]);
1978 SCM_DEFINE (scm_string_to_utf32
, "string->utf32",
1980 (SCM str
, SCM endianness
),
1981 "Return a newly allocated bytevector that contains the UTF-32 "
1982 "encoding of @var{str}.")
1983 #define FUNC_NAME s_scm_string_to_utf32
1986 scm_t_wchar
*wchars
;
1987 size_t wchar_len
, bytes_len
;
1989 wchars
= scm_to_utf32_stringn (str
, &wchar_len
);
1990 bytes_len
= wchar_len
* sizeof (scm_t_wchar
);
1991 if (!scm_is_eq (SCM_UNBNDP (endianness
) ? scm_endianness_big
: endianness
,
1992 scm_i_native_endianness
))
1993 swap_u32 (wchars
, wchar_len
);
1995 bv
= make_bytevector (bytes_len
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
1996 memcpy (SCM_BYTEVECTOR_CONTENTS (bv
), wchars
, bytes_len
);
2004 /* Produce the body of a function that converts a UTF-encoded bytevector to a
2006 #define UTF_TO_STRING(_utf_width) \
2007 SCM str = SCM_BOOL_F; \
2009 char *c_str = NULL; \
2010 char c_utf_name[MAX_UTF_ENCODING_NAME_LEN]; \
2012 size_t c_strlen = 0, c_utf_len = 0; \
2014 SCM_VALIDATE_BYTEVECTOR (1, utf); \
2015 if (scm_is_eq (endianness, SCM_UNDEFINED)) \
2016 endianness = scm_sym_big; \
2018 SCM_VALIDATE_SYMBOL (2, endianness); \
2020 c_utf_len = SCM_BYTEVECTOR_LENGTH (utf); \
2021 c_utf = (char *) SCM_BYTEVECTOR_CONTENTS (utf); \
2022 utf_encoding_name (c_utf_name, (_utf_width), endianness); \
2024 err = mem_iconveh (c_utf, c_utf_len, \
2025 c_utf_name, "UTF-8", \
2026 iconveh_question_mark, NULL, \
2027 &c_str, &c_strlen); \
2028 if (SCM_UNLIKELY (err)) \
2029 scm_syserror_msg (FUNC_NAME, "failed to convert to string: ~A", \
2030 scm_list_1 (utf), err); \
2033 str = scm_from_utf8_stringn (c_str, c_strlen); \
2039 SCM_DEFINE (scm_utf8_to_string
, "utf8->string",
2042 "Return a newly allocate string that contains from the UTF-8-"
2043 "encoded contents of bytevector @var{utf}.")
2044 #define FUNC_NAME s_scm_utf8_to_string
2048 size_t c_utf_len
= 0;
2050 SCM_VALIDATE_BYTEVECTOR (1, utf
);
2052 c_utf_len
= SCM_BYTEVECTOR_LENGTH (utf
);
2053 c_utf
= (char *) SCM_BYTEVECTOR_CONTENTS (utf
);
2054 str
= scm_from_utf8_stringn (c_utf
, c_utf_len
);
2060 SCM_DEFINE (scm_utf16_to_string
, "utf16->string",
2062 (SCM utf
, SCM endianness
),
2063 "Return a newly allocate string that contains from the UTF-16-"
2064 "encoded contents of bytevector @var{utf}.")
2065 #define FUNC_NAME s_scm_utf16_to_string
2071 SCM_DEFINE (scm_utf32_to_string
, "utf32->string",
2073 (SCM utf
, SCM endianness
),
2074 "Return a newly allocate string that contains from the UTF-32-"
2075 "encoded contents of bytevector @var{utf}.")
2076 #define FUNC_NAME s_scm_utf32_to_string
2083 /* Bytevectors as generalized vectors & arrays. */
2085 #define COMPLEX_ACCESSOR_PROLOGUE(_type) \
2086 size_t c_len, c_index; \
2089 SCM_VALIDATE_BYTEVECTOR (1, bv); \
2090 c_index = scm_to_size_t (index); \
2092 c_len = SCM_BYTEVECTOR_LENGTH (bv); \
2093 c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
2095 if (SCM_UNLIKELY (c_index + 2 * sizeof (_type) - 1 >= c_len)) \
2096 scm_out_of_range (FUNC_NAME, index);
2098 /* Template for native access to complex numbers of type TYPE. */
2099 #define COMPLEX_NATIVE_REF(_type) \
2102 COMPLEX_ACCESSOR_PROLOGUE (_type); \
2107 memcpy (&real, &c_bv[c_index], sizeof (_type)); \
2108 memcpy (&imag, &c_bv[c_index + sizeof (_type)], sizeof (_type)); \
2110 result = scm_c_make_rectangular (real, imag); \
2116 bytevector_ref_c32 (SCM bv
, SCM index
)
2117 #define FUNC_NAME "bytevector_ref_c32"
2119 COMPLEX_NATIVE_REF (float);
2124 bytevector_ref_c64 (SCM bv
, SCM index
)
2125 #define FUNC_NAME "bytevector_ref_c64"
2127 COMPLEX_NATIVE_REF (double);
2131 typedef SCM (*scm_t_bytevector_ref_fn
)(SCM
, SCM
);
2133 static const scm_t_bytevector_ref_fn
2134 bytevector_ref_fns
[SCM_ARRAY_ELEMENT_TYPE_LAST
+ 1] =
2139 scm_bytevector_u8_ref
, /* VU8 */
2140 scm_bytevector_u8_ref
, /* U8 */
2141 scm_bytevector_s8_ref
,
2142 scm_bytevector_u16_native_ref
,
2143 scm_bytevector_s16_native_ref
,
2144 scm_bytevector_u32_native_ref
,
2145 scm_bytevector_s32_native_ref
,
2146 scm_bytevector_u64_native_ref
,
2147 scm_bytevector_s64_native_ref
,
2148 scm_bytevector_ieee_single_native_ref
,
2149 scm_bytevector_ieee_double_native_ref
,
2155 bv_handle_ref (scm_t_array_handle
*h
, size_t index
)
2158 scm_t_bytevector_ref_fn ref_fn
;
2160 ref_fn
= bytevector_ref_fns
[h
->element_type
];
2162 scm_from_size_t (index
* scm_array_handle_uniform_element_size (h
));
2163 return ref_fn (h
->array
, byte_index
);
2166 /* Template for native modification of complex numbers of type TYPE. */
2167 #define COMPLEX_NATIVE_SET(_type) \
2168 COMPLEX_ACCESSOR_PROLOGUE (_type); \
2172 real = scm_c_real_part (value); \
2173 imag = scm_c_imag_part (value); \
2175 memcpy (&c_bv[c_index], &real, sizeof (_type)); \
2176 memcpy (&c_bv[c_index + sizeof (_type)], &imag, sizeof (_type)); \
2179 return SCM_UNSPECIFIED;
2182 bytevector_set_c32 (SCM bv
, SCM index
, SCM value
)
2183 #define FUNC_NAME "bytevector_set_c32"
2185 COMPLEX_NATIVE_SET (float);
2190 bytevector_set_c64 (SCM bv
, SCM index
, SCM value
)
2191 #define FUNC_NAME "bytevector_set_c64"
2193 COMPLEX_NATIVE_SET (double);
2197 typedef SCM (*scm_t_bytevector_set_fn
)(SCM
, SCM
, SCM
);
2199 const scm_t_bytevector_set_fn bytevector_set_fns
[SCM_ARRAY_ELEMENT_TYPE_LAST
+ 1] =
2204 scm_bytevector_u8_set_x
, /* VU8 */
2205 scm_bytevector_u8_set_x
, /* U8 */
2206 scm_bytevector_s8_set_x
,
2207 scm_bytevector_u16_native_set_x
,
2208 scm_bytevector_s16_native_set_x
,
2209 scm_bytevector_u32_native_set_x
,
2210 scm_bytevector_s32_native_set_x
,
2211 scm_bytevector_u64_native_set_x
,
2212 scm_bytevector_s64_native_set_x
,
2213 scm_bytevector_ieee_single_native_set_x
,
2214 scm_bytevector_ieee_double_native_set_x
,
2220 bv_handle_set_x (scm_t_array_handle
*h
, size_t index
, SCM val
)
2223 scm_t_bytevector_set_fn set_fn
;
2225 set_fn
= bytevector_set_fns
[h
->element_type
];
2227 scm_from_size_t (index
* scm_array_handle_uniform_element_size (h
));
2228 set_fn (h
->array
, byte_index
, val
);
2232 bytevector_get_handle (SCM v
, scm_t_array_handle
*h
)
2238 h
->dim0
.ubnd
= SCM_BYTEVECTOR_TYPED_LENGTH (v
) - 1;
2240 h
->element_type
= SCM_BYTEVECTOR_ELEMENT_TYPE (v
);
2241 h
->elements
= h
->writable_elements
= SCM_BYTEVECTOR_CONTENTS (v
);
2245 /* Initialization. */
2248 scm_bootstrap_bytevectors (void)
2250 /* This must be instantiated here because the generalized-vector API may
2251 want to access bytevectors even though `(rnrs bytevectors)' hasn't been
2253 scm_null_bytevector
= make_bytevector (0, SCM_ARRAY_ELEMENT_TYPE_VU8
);
2255 #ifdef WORDS_BIGENDIAN
2256 scm_i_native_endianness
= scm_from_latin1_symbol ("big");
2258 scm_i_native_endianness
= scm_from_latin1_symbol ("little");
2261 scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION
,
2262 "scm_init_bytevectors",
2263 (scm_t_extension_init_func
) scm_init_bytevectors
,
2267 scm_t_array_implementation impl
;
2269 impl
.tag
= scm_tc7_bytevector
;
2271 impl
.vref
= bv_handle_ref
;
2272 impl
.vset
= bv_handle_set_x
;
2273 impl
.get_handle
= bytevector_get_handle
;
2274 scm_i_register_array_implementation (&impl
);
2275 scm_i_register_vector_constructor
2276 (scm_i_array_element_types
[SCM_ARRAY_ELEMENT_TYPE_VU8
],
2277 scm_make_bytevector
);
2282 scm_init_bytevectors (void)
2284 #include "libguile/bytevectors.x"
2286 scm_endianness_big
= scm_sym_big
;
2287 scm_endianness_little
= scm_sym_little
;