1 /* Copyright (C) 2009 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/ieee-754.h"
35 #include "libguile/arrays.h"
36 #include "libguile/array-handle.h"
37 #include "libguile/uniform.h"
38 #include "libguile/srfi-4.h"
41 #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 _sign long 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 _sign long 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))
182 #define SCM_BYTEVECTOR_SET_LENGTH(_bv, _len) \
183 SCM_SET_CELL_WORD_1 ((_bv), (scm_t_bits) (_len))
185 #define SCM_BYTEVECTOR_SET_ELEMENT_TYPE(bv, hint) \
186 SCM_SET_BYTEVECTOR_FLAGS ((bv), (hint))
187 #define SCM_BYTEVECTOR_TYPE_SIZE(var) \
188 (scm_i_array_element_type_sizes[SCM_BYTEVECTOR_ELEMENT_TYPE (var)]/8)
189 #define SCM_BYTEVECTOR_TYPED_LENGTH(var) \
190 SCM_BYTEVECTOR_LENGTH (var) / SCM_BYTEVECTOR_TYPE_SIZE (var)
192 /* The empty bytevector. */
193 SCM scm_null_bytevector
= SCM_UNSPECIFIED
;
197 make_bytevector (size_t len
, scm_t_array_element_type element_type
)
202 if (SCM_UNLIKELY (element_type
> SCM_ARRAY_ELEMENT_TYPE_LAST
203 || scm_i_array_element_type_sizes
[element_type
] < 8
204 || len
>= (SCM_I_SIZE_MAX
205 / (scm_i_array_element_type_sizes
[element_type
]/8))))
206 /* This would be an internal Guile programming error */
209 if (SCM_UNLIKELY (len
== 0 && element_type
== SCM_ARRAY_ELEMENT_TYPE_VU8
210 && SCM_BYTEVECTOR_P (scm_null_bytevector
)))
211 ret
= scm_null_bytevector
;
214 c_len
= len
* (scm_i_array_element_type_sizes
[element_type
] / 8);
216 ret
= PTR2SCM (scm_gc_malloc_pointerless (SCM_BYTEVECTOR_HEADER_BYTES
+ c_len
,
219 SCM_SET_CELL_TYPE (ret
, scm_tc7_bytevector
);
220 SCM_BYTEVECTOR_SET_LENGTH (ret
, c_len
);
221 SCM_BYTEVECTOR_SET_ELEMENT_TYPE (ret
, element_type
);
227 /* Return a bytevector of LEN elements of type ELEMENT_TYPE, with element
228 values taken from CONTENTS. */
230 make_bytevector_from_buffer (size_t len
, void *contents
,
231 scm_t_array_element_type element_type
)
235 /* We actually never reuse storage from CONTENTS. Hans Boehm says in
236 <gc/gc.h> that realloc(3) "shouldn't have been invented" and he may well
238 ret
= make_bytevector (len
, element_type
);
244 c_len
= len
* (scm_i_array_element_type_sizes
[element_type
] / 8);
245 memcpy (SCM_BYTEVECTOR_CONTENTS (ret
),
249 scm_gc_free (contents
, c_len
, SCM_GC_BYTEVECTOR
);
256 /* Return a new bytevector of size LEN octets. */
258 scm_c_make_bytevector (size_t len
)
260 return make_bytevector (len
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
263 /* Return a new bytevector of size LEN elements. */
265 scm_i_make_typed_bytevector (size_t len
, scm_t_array_element_type element_type
)
267 return make_bytevector (len
, element_type
);
270 /* Return a bytevector of size LEN made up of CONTENTS. The area pointed to
271 by CONTENTS must have been allocated using `scm_gc_malloc ()'. */
273 scm_c_take_bytevector (signed char *contents
, size_t len
)
275 return make_bytevector_from_buffer (len
, contents
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
279 scm_c_take_typed_bytevector (signed char *contents
, size_t len
,
280 scm_t_array_element_type element_type
)
282 return make_bytevector_from_buffer (len
, contents
, element_type
);
285 /* Shrink BV to C_NEW_LEN (which is assumed to be smaller than its current
286 size) and return the new bytevector (possibly different from BV). */
288 scm_c_shrink_bytevector (SCM bv
, size_t c_new_len
)
293 if (SCM_UNLIKELY (c_new_len
% SCM_BYTEVECTOR_TYPE_SIZE (bv
)))
294 /* This would be an internal Guile programming error */
297 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
298 if (SCM_UNLIKELY (c_new_len
> c_len
))
301 SCM_BYTEVECTOR_SET_LENGTH (bv
, c_new_len
);
303 /* Resize the existing buffer. */
304 new_bv
= PTR2SCM (scm_gc_realloc (SCM2PTR (bv
),
305 c_len
+ SCM_BYTEVECTOR_HEADER_BYTES
,
306 c_new_len
+ SCM_BYTEVECTOR_HEADER_BYTES
,
313 scm_is_bytevector (SCM obj
)
315 return SCM_BYTEVECTOR_P (obj
);
319 scm_c_bytevector_length (SCM bv
)
320 #define FUNC_NAME "scm_c_bytevector_length"
322 SCM_VALIDATE_BYTEVECTOR (1, bv
);
324 return SCM_BYTEVECTOR_LENGTH (bv
);
329 scm_c_bytevector_ref (SCM bv
, size_t index
)
330 #define FUNC_NAME "scm_c_bytevector_ref"
333 const scm_t_uint8
*c_bv
;
335 SCM_VALIDATE_BYTEVECTOR (1, bv
);
337 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
338 c_bv
= (scm_t_uint8
*) SCM_BYTEVECTOR_CONTENTS (bv
);
340 if (SCM_UNLIKELY (index
>= c_len
))
341 scm_out_of_range (FUNC_NAME
, scm_from_size_t (index
));
348 scm_c_bytevector_set_x (SCM bv
, size_t index
, scm_t_uint8 value
)
349 #define FUNC_NAME "scm_c_bytevector_set_x"
354 SCM_VALIDATE_BYTEVECTOR (1, bv
);
356 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
357 c_bv
= (scm_t_uint8
*) SCM_BYTEVECTOR_CONTENTS (bv
);
359 if (SCM_UNLIKELY (index
>= c_len
))
360 scm_out_of_range (FUNC_NAME
, scm_from_size_t (index
));
369 scm_i_print_bytevector (SCM bv
, SCM port
, scm_print_state
*pstate SCM_UNUSED
)
371 ssize_t ubnd
, inc
, i
;
372 scm_t_array_handle h
;
374 scm_array_get_handle (bv
, &h
);
376 scm_putc ('#', port
);
377 scm_write (scm_array_handle_element_type (&h
), port
);
378 scm_putc ('(', port
);
379 for (i
= h
.dims
[0].lbnd
, ubnd
= h
.dims
[0].ubnd
, inc
= h
.dims
[0].inc
;
383 scm_putc (' ', port
);
384 scm_write (scm_array_handle_ref (&h
, i
), port
);
386 scm_putc (')', port
);
392 /* General operations. */
394 SCM_SYMBOL (scm_sym_big
, "big");
395 SCM_SYMBOL (scm_sym_little
, "little");
397 SCM scm_endianness_big
, scm_endianness_little
;
399 /* Host endianness (a symbol). */
400 SCM scm_i_native_endianness
= SCM_UNSPECIFIED
;
404 # define bswap_24(_x) \
405 ((((_x) & 0xff0000) >> 16) | \
406 (((_x) & 0x00ff00)) | \
407 (((_x) & 0x0000ff) << 16))
411 SCM_DEFINE (scm_native_endianness
, "native-endianness", 0, 0, 0,
413 "Return a symbol denoting the machine's native endianness.")
414 #define FUNC_NAME s_scm_native_endianness
416 return scm_i_native_endianness
;
420 SCM_DEFINE (scm_bytevector_p
, "bytevector?", 1, 0, 0,
422 "Return true if @var{obj} is a bytevector.")
423 #define FUNC_NAME s_scm_bytevector_p
425 return scm_from_bool (scm_is_bytevector (obj
));
429 SCM_DEFINE (scm_make_bytevector
, "make-bytevector", 1, 1, 0,
431 "Return a newly allocated bytevector of @var{len} bytes, "
432 "optionally filled with @var{fill}.")
433 #define FUNC_NAME s_scm_make_bytevector
437 signed char c_fill
= '\0';
439 SCM_VALIDATE_UINT_COPY (1, len
, c_len
);
440 if (fill
!= SCM_UNDEFINED
)
444 value
= scm_to_int (fill
);
445 if (SCM_UNLIKELY ((value
< -128) || (value
> 255)))
446 scm_out_of_range (FUNC_NAME
, fill
);
447 c_fill
= (signed char) value
;
450 bv
= make_bytevector (c_len
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
451 if (fill
!= SCM_UNDEFINED
)
454 signed char *contents
;
456 contents
= SCM_BYTEVECTOR_CONTENTS (bv
);
457 for (i
= 0; i
< c_len
; i
++)
458 contents
[i
] = c_fill
;
465 SCM_DEFINE (scm_bytevector_length
, "bytevector-length", 1, 0, 0,
467 "Return the length (in bytes) of @var{bv}.")
468 #define FUNC_NAME s_scm_bytevector_length
470 return scm_from_uint (scm_c_bytevector_length (bv
));
474 SCM_DEFINE (scm_bytevector_eq_p
, "bytevector=?", 2, 0, 0,
476 "Return is @var{bv1} equals to @var{bv2}---i.e., if they "
477 "have the same length and contents.")
478 #define FUNC_NAME s_scm_bytevector_eq_p
480 SCM result
= SCM_BOOL_F
;
481 unsigned c_len1
, c_len2
;
483 SCM_VALIDATE_BYTEVECTOR (1, bv1
);
484 SCM_VALIDATE_BYTEVECTOR (2, bv2
);
486 c_len1
= SCM_BYTEVECTOR_LENGTH (bv1
);
487 c_len2
= SCM_BYTEVECTOR_LENGTH (bv2
);
489 if (c_len1
== c_len2
)
491 signed char *c_bv1
, *c_bv2
;
493 c_bv1
= SCM_BYTEVECTOR_CONTENTS (bv1
);
494 c_bv2
= SCM_BYTEVECTOR_CONTENTS (bv2
);
496 result
= scm_from_bool (!memcmp (c_bv1
, c_bv2
, c_len1
));
503 SCM_DEFINE (scm_bytevector_fill_x
, "bytevector-fill!", 2, 0, 0,
505 "Fill bytevector @var{bv} with @var{fill}, a byte.")
506 #define FUNC_NAME s_scm_bytevector_fill_x
509 signed char *c_bv
, c_fill
;
511 SCM_VALIDATE_BYTEVECTOR (1, bv
);
512 c_fill
= scm_to_int8 (fill
);
514 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
515 c_bv
= SCM_BYTEVECTOR_CONTENTS (bv
);
517 for (i
= 0; i
< c_len
; i
++)
520 return SCM_UNSPECIFIED
;
524 SCM_DEFINE (scm_bytevector_copy_x
, "bytevector-copy!", 5, 0, 0,
525 (SCM source
, SCM source_start
, SCM target
, SCM target_start
,
527 "Copy @var{len} bytes from @var{source} into @var{target}, "
528 "starting reading from @var{source_start} (a positive index "
529 "within @var{source}) and start writing at "
530 "@var{target_start}.")
531 #define FUNC_NAME s_scm_bytevector_copy_x
533 unsigned c_len
, c_source_len
, c_target_len
;
534 unsigned c_source_start
, c_target_start
;
535 signed char *c_source
, *c_target
;
537 SCM_VALIDATE_BYTEVECTOR (1, source
);
538 SCM_VALIDATE_BYTEVECTOR (3, target
);
540 c_len
= scm_to_uint (len
);
541 c_source_start
= scm_to_uint (source_start
);
542 c_target_start
= scm_to_uint (target_start
);
544 c_source
= SCM_BYTEVECTOR_CONTENTS (source
);
545 c_target
= SCM_BYTEVECTOR_CONTENTS (target
);
546 c_source_len
= SCM_BYTEVECTOR_LENGTH (source
);
547 c_target_len
= SCM_BYTEVECTOR_LENGTH (target
);
549 if (SCM_UNLIKELY (c_source_start
+ c_len
> c_source_len
))
550 scm_out_of_range (FUNC_NAME
, source_start
);
551 if (SCM_UNLIKELY (c_target_start
+ c_len
> c_target_len
))
552 scm_out_of_range (FUNC_NAME
, target_start
);
554 memcpy (c_target
+ c_target_start
,
555 c_source
+ c_source_start
,
558 return SCM_UNSPECIFIED
;
562 SCM_DEFINE (scm_bytevector_copy
, "bytevector-copy", 1, 0, 0,
564 "Return a newly allocated copy of @var{bv}.")
565 #define FUNC_NAME s_scm_bytevector_copy
569 signed char *c_bv
, *c_copy
;
571 SCM_VALIDATE_BYTEVECTOR (1, bv
);
573 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
574 c_bv
= SCM_BYTEVECTOR_CONTENTS (bv
);
576 copy
= make_bytevector (c_len
, SCM_BYTEVECTOR_ELEMENT_TYPE (bv
));
577 c_copy
= SCM_BYTEVECTOR_CONTENTS (copy
);
578 memcpy (c_copy
, c_bv
, c_len
);
584 SCM_DEFINE (scm_uniform_array_to_bytevector
, "uniform-array->bytevector",
585 1, 0, 0, (SCM array
),
586 "Return a newly allocated bytevector whose contents\n"
587 "will be copied from the uniform array @var{array}.")
588 #define FUNC_NAME s_scm_uniform_array_to_bytevector
591 size_t len
, sz
, byte_len
;
592 scm_t_array_handle h
;
595 contents
= scm_array_contents (array
, SCM_BOOL_T
);
596 if (scm_is_false (contents
))
597 scm_wrong_type_arg_msg (FUNC_NAME
, 0, array
, "uniform contiguous array");
599 scm_array_get_handle (contents
, &h
);
600 assert (h
.base
== 0);
603 len
= h
.dims
->inc
* (h
.dims
->ubnd
- h
.dims
->lbnd
+ 1);
604 sz
= scm_array_handle_uniform_element_bit_size (&h
);
605 if (sz
>= 8 && ((sz
% 8) == 0))
606 byte_len
= len
* (sz
/ 8);
608 /* byte_len = ceil (len * sz / 8) */
609 byte_len
= (len
* sz
+ 7) / 8;
611 /* an internal guile error, really */
612 SCM_MISC_ERROR ("uniform elements larger than 8 bits must fill whole bytes", SCM_EOL
);
614 ret
= make_bytevector (byte_len
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
615 memcpy (SCM_BYTEVECTOR_CONTENTS (ret
), elts
, byte_len
);
617 scm_array_handle_release (&h
);
624 /* Operations on bytes and octets. */
626 SCM_DEFINE (scm_bytevector_u8_ref
, "bytevector-u8-ref", 2, 0, 0,
628 "Return the octet located at @var{index} in @var{bv}.")
629 #define FUNC_NAME s_scm_bytevector_u8_ref
631 INTEGER_NATIVE_REF (8, unsigned);
635 SCM_DEFINE (scm_bytevector_s8_ref
, "bytevector-s8-ref", 2, 0, 0,
637 "Return the byte located at @var{index} in @var{bv}.")
638 #define FUNC_NAME s_scm_bytevector_s8_ref
640 INTEGER_NATIVE_REF (8, signed);
644 SCM_DEFINE (scm_bytevector_u8_set_x
, "bytevector-u8-set!", 3, 0, 0,
645 (SCM bv
, SCM index
, SCM value
),
646 "Return the octet located at @var{index} in @var{bv}.")
647 #define FUNC_NAME s_scm_bytevector_u8_set_x
649 INTEGER_NATIVE_SET (8, unsigned);
653 SCM_DEFINE (scm_bytevector_s8_set_x
, "bytevector-s8-set!", 3, 0, 0,
654 (SCM bv
, SCM index
, SCM value
),
655 "Return the octet located at @var{index} in @var{bv}.")
656 #define FUNC_NAME s_scm_bytevector_s8_set_x
658 INTEGER_NATIVE_SET (8, signed);
662 #undef OCTET_ACCESSOR_PROLOGUE
665 SCM_DEFINE (scm_bytevector_to_u8_list
, "bytevector->u8-list", 1, 0, 0,
667 "Return a newly allocated list of octets containing the "
668 "contents of @var{bv}.")
669 #define FUNC_NAME s_scm_bytevector_to_u8_list
675 SCM_VALIDATE_BYTEVECTOR (1, bv
);
677 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
678 c_bv
= (unsigned char *) SCM_BYTEVECTOR_CONTENTS (bv
);
680 lst
= scm_make_list (scm_from_uint (c_len
), SCM_UNSPECIFIED
);
681 for (i
= 0, pair
= lst
;
683 i
++, pair
= SCM_CDR (pair
))
685 SCM_SETCAR (pair
, SCM_I_MAKINUM (c_bv
[i
]));
692 SCM_DEFINE (scm_u8_list_to_bytevector
, "u8-list->bytevector", 1, 0, 0,
694 "Turn @var{lst}, a list of octets, into a bytevector.")
695 #define FUNC_NAME s_scm_u8_list_to_bytevector
701 SCM_VALIDATE_LIST_COPYLEN (1, lst
, c_len
);
703 bv
= make_bytevector (c_len
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
704 c_bv
= (unsigned char *) SCM_BYTEVECTOR_CONTENTS (bv
);
706 for (i
= 0; i
< c_len
; lst
= SCM_CDR (lst
), i
++)
708 item
= SCM_CAR (lst
);
710 if (SCM_LIKELY (SCM_I_INUMP (item
)))
714 c_item
= SCM_I_INUM (item
);
715 if (SCM_LIKELY ((c_item
>= 0) && (c_item
< 256)))
716 c_bv
[i
] = (unsigned char) c_item
;
727 scm_wrong_type_arg (FUNC_NAME
, 1, item
);
733 /* Compute the two's complement of VALUE (a positive integer) on SIZE octets
734 using (2^(SIZE * 8) - VALUE). */
736 twos_complement (mpz_t value
, size_t size
)
738 unsigned long bit_count
;
740 /* We expect BIT_COUNT to fit in a unsigned long thanks to the range
741 checking on SIZE performed earlier. */
742 bit_count
= (unsigned long) size
<< 3UL;
744 if (SCM_LIKELY (bit_count
< sizeof (unsigned long)))
745 mpz_ui_sub (value
, 1UL << bit_count
, value
);
751 mpz_ui_pow_ui (max
, 2, bit_count
);
752 mpz_sub (value
, max
, value
);
758 bytevector_large_ref (const char *c_bv
, size_t c_size
, int signed_p
,
763 int c_endianness
, negative_p
= 0;
767 if (scm_is_eq (endianness
, scm_sym_big
))
768 negative_p
= c_bv
[0] & 0x80;
770 negative_p
= c_bv
[c_size
- 1] & 0x80;
773 c_endianness
= scm_is_eq (endianness
, scm_sym_big
) ? 1 : -1;
776 mpz_import (c_mpz
, 1 /* 1 word */, 1 /* word order doesn't matter */,
777 c_size
/* word is C_SIZE-byte long */,
779 0 /* nails */, c_bv
);
781 if (signed_p
&& negative_p
)
783 twos_complement (c_mpz
, c_size
);
784 mpz_neg (c_mpz
, c_mpz
);
787 result
= scm_from_mpz (c_mpz
);
788 mpz_clear (c_mpz
); /* FIXME: Needed? */
794 bytevector_large_set (char *c_bv
, size_t c_size
, int signed_p
,
795 SCM value
, SCM endianness
)
798 int c_endianness
, c_sign
, err
= 0;
800 c_endianness
= scm_is_eq (endianness
, scm_sym_big
) ? 1 : -1;
803 scm_to_mpz (value
, c_mpz
);
805 c_sign
= mpz_sgn (c_mpz
);
808 if (SCM_LIKELY (signed_p
))
810 mpz_neg (c_mpz
, c_mpz
);
811 twos_complement (c_mpz
, c_size
);
822 memset (c_bv
, 0, c_size
);
825 size_t word_count
, value_size
;
827 value_size
= (mpz_sizeinbase (c_mpz
, 2) + (8 * c_size
)) / (8 * c_size
);
828 if (SCM_UNLIKELY (value_size
> c_size
))
835 mpz_export (c_bv
, &word_count
, 1 /* word order doesn't matter */,
836 c_size
, c_endianness
,
837 0 /* nails */, c_mpz
);
838 if (SCM_UNLIKELY (word_count
!= 1))
839 /* Shouldn't happen since we already checked with VALUE_SIZE. */
849 #define GENERIC_INTEGER_ACCESSOR_PROLOGUE(_sign) \
850 unsigned long c_len, c_index, c_size; \
853 SCM_VALIDATE_BYTEVECTOR (1, bv); \
854 c_index = scm_to_ulong (index); \
855 c_size = scm_to_ulong (size); \
857 c_len = SCM_BYTEVECTOR_LENGTH (bv); \
858 c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
860 /* C_SIZE must have its 3 higher bits set to zero so that \
861 multiplying it by 8 yields a number that fits in an \
863 if (SCM_UNLIKELY ((c_size == 0) || (c_size >= (ULONG_MAX >> 3L)))) \
864 scm_out_of_range (FUNC_NAME, size); \
865 if (SCM_UNLIKELY (c_index + c_size > c_len)) \
866 scm_out_of_range (FUNC_NAME, index);
869 /* Template of an integer reference function. */
870 #define GENERIC_INTEGER_REF(_sign) \
878 swap = !scm_is_eq (endianness, scm_i_native_endianness); \
883 _sign char c_value8; \
884 memcpy (&c_value8, c_bv, 1); \
890 INT_TYPE (16, _sign) c_value16; \
891 memcpy (&c_value16, c_bv, 2); \
893 value = (INT_TYPE (16, _sign)) bswap_16 (c_value16); \
902 result = SCM_I_MAKINUM ((_sign int) value); \
905 result = bytevector_large_ref ((char *) c_bv, \
906 c_size, SIGNEDNESS (_sign), \
912 bytevector_signed_ref (const char *c_bv
, size_t c_size
, SCM endianness
)
914 GENERIC_INTEGER_REF (signed);
918 bytevector_unsigned_ref (const char *c_bv
, size_t c_size
, SCM endianness
)
920 GENERIC_INTEGER_REF (unsigned);
924 /* Template of an integer assignment function. */
925 #define GENERIC_INTEGER_SET(_sign) \
930 if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
933 c_value = SCM_I_INUM (value); \
937 if (SCM_LIKELY (INT_VALID_P (8, _sign) (c_value))) \
939 _sign char c_value8; \
940 c_value8 = (_sign char) c_value; \
941 memcpy (c_bv, &c_value8, 1); \
948 if (SCM_LIKELY (INT_VALID_P (16, _sign) (c_value))) \
951 INT_TYPE (16, _sign) c_value16; \
953 swap = !scm_is_eq (endianness, scm_i_native_endianness); \
956 c_value16 = (INT_TYPE (16, _sign)) bswap_16 (c_value); \
958 c_value16 = c_value; \
960 memcpy (c_bv, &c_value16, 2); \
974 err = bytevector_large_set (c_bv, c_size, \
975 SIGNEDNESS (_sign), \
976 value, endianness); \
984 scm_out_of_range (FUNC_NAME, value); \
988 bytevector_signed_set (char *c_bv
, size_t c_size
,
989 SCM value
, SCM endianness
,
990 const char *func_name
)
991 #define FUNC_NAME func_name
993 GENERIC_INTEGER_SET (signed);
998 bytevector_unsigned_set (char *c_bv
, size_t c_size
,
999 SCM value
, SCM endianness
,
1000 const char *func_name
)
1001 #define FUNC_NAME func_name
1003 GENERIC_INTEGER_SET (unsigned);
1007 #undef GENERIC_INTEGER_SET
1008 #undef GENERIC_INTEGER_REF
1011 SCM_DEFINE (scm_bytevector_uint_ref
, "bytevector-uint-ref", 4, 0, 0,
1012 (SCM bv
, SCM index
, SCM endianness
, SCM size
),
1013 "Return the @var{size}-octet long unsigned integer at index "
1014 "@var{index} in @var{bv}.")
1015 #define FUNC_NAME s_scm_bytevector_uint_ref
1017 GENERIC_INTEGER_ACCESSOR_PROLOGUE (unsigned);
1019 return (bytevector_unsigned_ref (&c_bv
[c_index
], c_size
, endianness
));
1023 SCM_DEFINE (scm_bytevector_sint_ref
, "bytevector-sint-ref", 4, 0, 0,
1024 (SCM bv
, SCM index
, SCM endianness
, SCM size
),
1025 "Return the @var{size}-octet long unsigned integer at index "
1026 "@var{index} in @var{bv}.")
1027 #define FUNC_NAME s_scm_bytevector_sint_ref
1029 GENERIC_INTEGER_ACCESSOR_PROLOGUE (signed);
1031 return (bytevector_signed_ref (&c_bv
[c_index
], c_size
, endianness
));
1035 SCM_DEFINE (scm_bytevector_uint_set_x
, "bytevector-uint-set!", 5, 0, 0,
1036 (SCM bv
, SCM index
, SCM value
, SCM endianness
, SCM size
),
1037 "Set the @var{size}-octet long unsigned integer at @var{index} "
1039 #define FUNC_NAME s_scm_bytevector_uint_set_x
1041 GENERIC_INTEGER_ACCESSOR_PROLOGUE (unsigned);
1043 bytevector_unsigned_set (&c_bv
[c_index
], c_size
, value
, endianness
,
1046 return SCM_UNSPECIFIED
;
1050 SCM_DEFINE (scm_bytevector_sint_set_x
, "bytevector-sint-set!", 5, 0, 0,
1051 (SCM bv
, SCM index
, SCM value
, SCM endianness
, SCM size
),
1052 "Set the @var{size}-octet long signed integer at @var{index} "
1054 #define FUNC_NAME s_scm_bytevector_sint_set_x
1056 GENERIC_INTEGER_ACCESSOR_PROLOGUE (signed);
1058 bytevector_signed_set (&c_bv
[c_index
], c_size
, value
, endianness
,
1061 return SCM_UNSPECIFIED
;
1067 /* Operations on integers of arbitrary size. */
1069 #define INTEGERS_TO_LIST(_sign) \
1071 size_t i, c_len, c_size; \
1073 SCM_VALIDATE_BYTEVECTOR (1, bv); \
1074 SCM_VALIDATE_SYMBOL (2, endianness); \
1075 c_size = scm_to_uint (size); \
1077 c_len = SCM_BYTEVECTOR_LENGTH (bv); \
1078 if (SCM_UNLIKELY (c_len == 0)) \
1080 else if (SCM_UNLIKELY (c_len < c_size)) \
1081 scm_out_of_range (FUNC_NAME, size); \
1086 c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
1088 lst = scm_make_list (scm_from_uint (c_len / c_size), \
1090 for (i = 0, pair = lst; \
1091 i <= c_len - c_size; \
1092 i += c_size, c_bv += c_size, pair = SCM_CDR (pair)) \
1095 bytevector_ ## _sign ## _ref (c_bv, c_size, \
1102 SCM_DEFINE (scm_bytevector_to_sint_list
, "bytevector->sint-list",
1104 (SCM bv
, SCM endianness
, SCM size
),
1105 "Return a list of signed integers of @var{size} octets "
1106 "representing the contents of @var{bv}.")
1107 #define FUNC_NAME s_scm_bytevector_to_sint_list
1109 INTEGERS_TO_LIST (signed);
1113 SCM_DEFINE (scm_bytevector_to_uint_list
, "bytevector->uint-list",
1115 (SCM bv
, SCM endianness
, SCM size
),
1116 "Return a list of unsigned integers of @var{size} octets "
1117 "representing the contents of @var{bv}.")
1118 #define FUNC_NAME s_scm_bytevector_to_uint_list
1120 INTEGERS_TO_LIST (unsigned);
1124 #undef INTEGER_TO_LIST
1127 #define INTEGER_LIST_TO_BYTEVECTOR(_sign) \
1131 char *c_bv, *c_bv_ptr; \
1133 SCM_VALIDATE_LIST_COPYLEN (1, lst, c_len); \
1134 SCM_VALIDATE_SYMBOL (2, endianness); \
1135 c_size = scm_to_uint (size); \
1137 if (SCM_UNLIKELY ((c_size == 0) || (c_size >= (ULONG_MAX >> 3L)))) \
1138 scm_out_of_range (FUNC_NAME, size); \
1140 bv = make_bytevector (c_len * c_size, SCM_ARRAY_ELEMENT_TYPE_VU8); \
1141 c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
1143 for (c_bv_ptr = c_bv; \
1144 !scm_is_null (lst); \
1145 lst = SCM_CDR (lst), c_bv_ptr += c_size) \
1147 bytevector_ ## _sign ## _set (c_bv_ptr, c_size, \
1148 SCM_CAR (lst), endianness, \
1155 SCM_DEFINE (scm_uint_list_to_bytevector
, "uint-list->bytevector",
1157 (SCM lst
, SCM endianness
, SCM size
),
1158 "Return a bytevector containing the unsigned integers "
1159 "listed in @var{lst} and encoded on @var{size} octets "
1160 "according to @var{endianness}.")
1161 #define FUNC_NAME s_scm_uint_list_to_bytevector
1163 INTEGER_LIST_TO_BYTEVECTOR (unsigned);
1167 SCM_DEFINE (scm_sint_list_to_bytevector
, "sint-list->bytevector",
1169 (SCM lst
, SCM endianness
, SCM size
),
1170 "Return a bytevector containing the signed integers "
1171 "listed in @var{lst} and encoded on @var{size} octets "
1172 "according to @var{endianness}.")
1173 #define FUNC_NAME s_scm_sint_list_to_bytevector
1175 INTEGER_LIST_TO_BYTEVECTOR (signed);
1179 #undef INTEGER_LIST_TO_BYTEVECTOR
1183 /* Operations on 16-bit integers. */
1185 SCM_DEFINE (scm_bytevector_u16_ref
, "bytevector-u16-ref",
1187 (SCM bv
, SCM index
, SCM endianness
),
1188 "Return the unsigned 16-bit integer from @var{bv} at "
1190 #define FUNC_NAME s_scm_bytevector_u16_ref
1192 INTEGER_REF (16, unsigned);
1196 SCM_DEFINE (scm_bytevector_s16_ref
, "bytevector-s16-ref",
1198 (SCM bv
, SCM index
, SCM endianness
),
1199 "Return the signed 16-bit integer from @var{bv} at "
1201 #define FUNC_NAME s_scm_bytevector_s16_ref
1203 INTEGER_REF (16, signed);
1207 SCM_DEFINE (scm_bytevector_u16_native_ref
, "bytevector-u16-native-ref",
1209 (SCM bv
, SCM index
),
1210 "Return the unsigned 16-bit integer from @var{bv} at "
1211 "@var{index} using the native endianness.")
1212 #define FUNC_NAME s_scm_bytevector_u16_native_ref
1214 INTEGER_NATIVE_REF (16, unsigned);
1218 SCM_DEFINE (scm_bytevector_s16_native_ref
, "bytevector-s16-native-ref",
1220 (SCM bv
, SCM index
),
1221 "Return the unsigned 16-bit integer from @var{bv} at "
1222 "@var{index} using the native endianness.")
1223 #define FUNC_NAME s_scm_bytevector_s16_native_ref
1225 INTEGER_NATIVE_REF (16, signed);
1229 SCM_DEFINE (scm_bytevector_u16_set_x
, "bytevector-u16-set!",
1231 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1232 "Store @var{value} in @var{bv} at @var{index} according to "
1233 "@var{endianness}.")
1234 #define FUNC_NAME s_scm_bytevector_u16_set_x
1236 INTEGER_SET (16, unsigned);
1240 SCM_DEFINE (scm_bytevector_s16_set_x
, "bytevector-s16-set!",
1242 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1243 "Store @var{value} in @var{bv} at @var{index} according to "
1244 "@var{endianness}.")
1245 #define FUNC_NAME s_scm_bytevector_s16_set_x
1247 INTEGER_SET (16, signed);
1251 SCM_DEFINE (scm_bytevector_u16_native_set_x
, "bytevector-u16-native-set!",
1253 (SCM bv
, SCM index
, SCM value
),
1254 "Store the unsigned integer @var{value} at index @var{index} "
1255 "of @var{bv} using the native endianness.")
1256 #define FUNC_NAME s_scm_bytevector_u16_native_set_x
1258 INTEGER_NATIVE_SET (16, unsigned);
1262 SCM_DEFINE (scm_bytevector_s16_native_set_x
, "bytevector-s16-native-set!",
1264 (SCM bv
, SCM index
, SCM value
),
1265 "Store the signed integer @var{value} at index @var{index} "
1266 "of @var{bv} using the native endianness.")
1267 #define FUNC_NAME s_scm_bytevector_s16_native_set_x
1269 INTEGER_NATIVE_SET (16, signed);
1275 /* Operations on 32-bit integers. */
1277 /* Unfortunately, on 32-bit machines `SCM' is not large enough to hold
1278 arbitrary 32-bit integers. Thus we fall back to using the
1279 `large_{ref,set}' variants on 32-bit machines. */
1281 #define LARGE_INTEGER_REF(_len, _sign) \
1282 INTEGER_ACCESSOR_PROLOGUE(_len, _sign); \
1283 SCM_VALIDATE_SYMBOL (3, endianness); \
1285 return (bytevector_large_ref ((char *) c_bv + c_index, _len / 8, \
1286 SIGNEDNESS (_sign), endianness));
1288 #define LARGE_INTEGER_SET(_len, _sign) \
1290 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
1291 SCM_VALIDATE_SYMBOL (4, endianness); \
1293 err = bytevector_large_set ((char *) c_bv + c_index, _len / 8, \
1294 SIGNEDNESS (_sign), value, endianness); \
1295 if (SCM_UNLIKELY (err)) \
1296 scm_out_of_range (FUNC_NAME, value); \
1298 return SCM_UNSPECIFIED;
1300 #define LARGE_INTEGER_NATIVE_REF(_len, _sign) \
1301 INTEGER_ACCESSOR_PROLOGUE(_len, _sign); \
1302 return (bytevector_large_ref ((char *) c_bv + c_index, _len / 8, \
1303 SIGNEDNESS (_sign), scm_i_native_endianness));
1305 #define LARGE_INTEGER_NATIVE_SET(_len, _sign) \
1307 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
1309 err = bytevector_large_set ((char *) c_bv + c_index, _len / 8, \
1310 SIGNEDNESS (_sign), value, \
1311 scm_i_native_endianness); \
1312 if (SCM_UNLIKELY (err)) \
1313 scm_out_of_range (FUNC_NAME, value); \
1315 return SCM_UNSPECIFIED;
1318 SCM_DEFINE (scm_bytevector_u32_ref
, "bytevector-u32-ref",
1320 (SCM bv
, SCM index
, SCM endianness
),
1321 "Return the unsigned 32-bit integer from @var{bv} at "
1323 #define FUNC_NAME s_scm_bytevector_u32_ref
1325 #if SIZEOF_VOID_P > 4
1326 INTEGER_REF (32, unsigned);
1328 LARGE_INTEGER_REF (32, unsigned);
1333 SCM_DEFINE (scm_bytevector_s32_ref
, "bytevector-s32-ref",
1335 (SCM bv
, SCM index
, SCM endianness
),
1336 "Return the signed 32-bit integer from @var{bv} at "
1338 #define FUNC_NAME s_scm_bytevector_s32_ref
1340 #if SIZEOF_VOID_P > 4
1341 INTEGER_REF (32, signed);
1343 LARGE_INTEGER_REF (32, signed);
1348 SCM_DEFINE (scm_bytevector_u32_native_ref
, "bytevector-u32-native-ref",
1350 (SCM bv
, SCM index
),
1351 "Return the unsigned 32-bit integer from @var{bv} at "
1352 "@var{index} using the native endianness.")
1353 #define FUNC_NAME s_scm_bytevector_u32_native_ref
1355 #if SIZEOF_VOID_P > 4
1356 INTEGER_NATIVE_REF (32, unsigned);
1358 LARGE_INTEGER_NATIVE_REF (32, unsigned);
1363 SCM_DEFINE (scm_bytevector_s32_native_ref
, "bytevector-s32-native-ref",
1365 (SCM bv
, SCM index
),
1366 "Return the unsigned 32-bit integer from @var{bv} at "
1367 "@var{index} using the native endianness.")
1368 #define FUNC_NAME s_scm_bytevector_s32_native_ref
1370 #if SIZEOF_VOID_P > 4
1371 INTEGER_NATIVE_REF (32, signed);
1373 LARGE_INTEGER_NATIVE_REF (32, signed);
1378 SCM_DEFINE (scm_bytevector_u32_set_x
, "bytevector-u32-set!",
1380 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1381 "Store @var{value} in @var{bv} at @var{index} according to "
1382 "@var{endianness}.")
1383 #define FUNC_NAME s_scm_bytevector_u32_set_x
1385 #if SIZEOF_VOID_P > 4
1386 INTEGER_SET (32, unsigned);
1388 LARGE_INTEGER_SET (32, unsigned);
1393 SCM_DEFINE (scm_bytevector_s32_set_x
, "bytevector-s32-set!",
1395 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1396 "Store @var{value} in @var{bv} at @var{index} according to "
1397 "@var{endianness}.")
1398 #define FUNC_NAME s_scm_bytevector_s32_set_x
1400 #if SIZEOF_VOID_P > 4
1401 INTEGER_SET (32, signed);
1403 LARGE_INTEGER_SET (32, signed);
1408 SCM_DEFINE (scm_bytevector_u32_native_set_x
, "bytevector-u32-native-set!",
1410 (SCM bv
, SCM index
, SCM value
),
1411 "Store the unsigned integer @var{value} at index @var{index} "
1412 "of @var{bv} using the native endianness.")
1413 #define FUNC_NAME s_scm_bytevector_u32_native_set_x
1415 #if SIZEOF_VOID_P > 4
1416 INTEGER_NATIVE_SET (32, unsigned);
1418 LARGE_INTEGER_NATIVE_SET (32, unsigned);
1423 SCM_DEFINE (scm_bytevector_s32_native_set_x
, "bytevector-s32-native-set!",
1425 (SCM bv
, SCM index
, SCM value
),
1426 "Store the signed integer @var{value} at index @var{index} "
1427 "of @var{bv} using the native endianness.")
1428 #define FUNC_NAME s_scm_bytevector_s32_native_set_x
1430 #if SIZEOF_VOID_P > 4
1431 INTEGER_NATIVE_SET (32, signed);
1433 LARGE_INTEGER_NATIVE_SET (32, signed);
1440 /* Operations on 64-bit integers. */
1442 /* For 64-bit integers, we use only the `large_{ref,set}' variant. */
1444 SCM_DEFINE (scm_bytevector_u64_ref
, "bytevector-u64-ref",
1446 (SCM bv
, SCM index
, SCM endianness
),
1447 "Return the unsigned 64-bit integer from @var{bv} at "
1449 #define FUNC_NAME s_scm_bytevector_u64_ref
1451 LARGE_INTEGER_REF (64, unsigned);
1455 SCM_DEFINE (scm_bytevector_s64_ref
, "bytevector-s64-ref",
1457 (SCM bv
, SCM index
, SCM endianness
),
1458 "Return the signed 64-bit integer from @var{bv} at "
1460 #define FUNC_NAME s_scm_bytevector_s64_ref
1462 LARGE_INTEGER_REF (64, signed);
1466 SCM_DEFINE (scm_bytevector_u64_native_ref
, "bytevector-u64-native-ref",
1468 (SCM bv
, SCM index
),
1469 "Return the unsigned 64-bit integer from @var{bv} at "
1470 "@var{index} using the native endianness.")
1471 #define FUNC_NAME s_scm_bytevector_u64_native_ref
1473 LARGE_INTEGER_NATIVE_REF (64, unsigned);
1477 SCM_DEFINE (scm_bytevector_s64_native_ref
, "bytevector-s64-native-ref",
1479 (SCM bv
, SCM index
),
1480 "Return the unsigned 64-bit integer from @var{bv} at "
1481 "@var{index} using the native endianness.")
1482 #define FUNC_NAME s_scm_bytevector_s64_native_ref
1484 LARGE_INTEGER_NATIVE_REF (64, signed);
1488 SCM_DEFINE (scm_bytevector_u64_set_x
, "bytevector-u64-set!",
1490 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1491 "Store @var{value} in @var{bv} at @var{index} according to "
1492 "@var{endianness}.")
1493 #define FUNC_NAME s_scm_bytevector_u64_set_x
1495 LARGE_INTEGER_SET (64, unsigned);
1499 SCM_DEFINE (scm_bytevector_s64_set_x
, "bytevector-s64-set!",
1501 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1502 "Store @var{value} in @var{bv} at @var{index} according to "
1503 "@var{endianness}.")
1504 #define FUNC_NAME s_scm_bytevector_s64_set_x
1506 LARGE_INTEGER_SET (64, signed);
1510 SCM_DEFINE (scm_bytevector_u64_native_set_x
, "bytevector-u64-native-set!",
1512 (SCM bv
, SCM index
, SCM value
),
1513 "Store the unsigned integer @var{value} at index @var{index} "
1514 "of @var{bv} using the native endianness.")
1515 #define FUNC_NAME s_scm_bytevector_u64_native_set_x
1517 LARGE_INTEGER_NATIVE_SET (64, unsigned);
1521 SCM_DEFINE (scm_bytevector_s64_native_set_x
, "bytevector-s64-native-set!",
1523 (SCM bv
, SCM index
, SCM value
),
1524 "Store the signed integer @var{value} at index @var{index} "
1525 "of @var{bv} using the native endianness.")
1526 #define FUNC_NAME s_scm_bytevector_s64_native_set_x
1528 LARGE_INTEGER_NATIVE_SET (64, signed);
1534 /* Operations on IEEE-754 numbers. */
1536 /* There are two possible word endians, visible in glibc's <ieee754.h>.
1537 However, in R6RS, when the endianness is `little', little endian is
1538 assumed for both the byte order and the word order. This is clear from
1539 Section 2.1 of R6RS-lib (in response to
1540 http://www.r6rs.org/formal-comments/comment-187.txt). */
1543 /* Convert to/from a floating-point number with different endianness. This
1544 method is probably not the most efficient but it should be portable. */
1547 float_to_foreign_endianness (union scm_ieee754_float
*target
,
1550 union scm_ieee754_float src
;
1554 #ifdef WORDS_BIGENDIAN
1555 /* Assuming little endian for both byte and word order. */
1556 target
->little_endian
.negative
= src
.big_endian
.negative
;
1557 target
->little_endian
.exponent
= src
.big_endian
.exponent
;
1558 target
->little_endian
.mantissa
= src
.big_endian
.mantissa
;
1560 target
->big_endian
.negative
= src
.little_endian
.negative
;
1561 target
->big_endian
.exponent
= src
.little_endian
.exponent
;
1562 target
->big_endian
.mantissa
= src
.little_endian
.mantissa
;
1567 float_from_foreign_endianness (const union scm_ieee754_float
*source
)
1569 union scm_ieee754_float result
;
1571 #ifdef WORDS_BIGENDIAN
1572 /* Assuming little endian for both byte and word order. */
1573 result
.big_endian
.negative
= source
->little_endian
.negative
;
1574 result
.big_endian
.exponent
= source
->little_endian
.exponent
;
1575 result
.big_endian
.mantissa
= source
->little_endian
.mantissa
;
1577 result
.little_endian
.negative
= source
->big_endian
.negative
;
1578 result
.little_endian
.exponent
= source
->big_endian
.exponent
;
1579 result
.little_endian
.mantissa
= source
->big_endian
.mantissa
;
1586 double_to_foreign_endianness (union scm_ieee754_double
*target
,
1589 union scm_ieee754_double src
;
1593 #ifdef WORDS_BIGENDIAN
1594 /* Assuming little endian for both byte and word order. */
1595 target
->little_little_endian
.negative
= src
.big_endian
.negative
;
1596 target
->little_little_endian
.exponent
= src
.big_endian
.exponent
;
1597 target
->little_little_endian
.mantissa0
= src
.big_endian
.mantissa0
;
1598 target
->little_little_endian
.mantissa1
= src
.big_endian
.mantissa1
;
1600 target
->big_endian
.negative
= src
.little_little_endian
.negative
;
1601 target
->big_endian
.exponent
= src
.little_little_endian
.exponent
;
1602 target
->big_endian
.mantissa0
= src
.little_little_endian
.mantissa0
;
1603 target
->big_endian
.mantissa1
= src
.little_little_endian
.mantissa1
;
1607 static inline double
1608 double_from_foreign_endianness (const union scm_ieee754_double
*source
)
1610 union scm_ieee754_double result
;
1612 #ifdef WORDS_BIGENDIAN
1613 /* Assuming little endian for both byte and word order. */
1614 result
.big_endian
.negative
= source
->little_little_endian
.negative
;
1615 result
.big_endian
.exponent
= source
->little_little_endian
.exponent
;
1616 result
.big_endian
.mantissa0
= source
->little_little_endian
.mantissa0
;
1617 result
.big_endian
.mantissa1
= source
->little_little_endian
.mantissa1
;
1619 result
.little_little_endian
.negative
= source
->big_endian
.negative
;
1620 result
.little_little_endian
.exponent
= source
->big_endian
.exponent
;
1621 result
.little_little_endian
.mantissa0
= source
->big_endian
.mantissa0
;
1622 result
.little_little_endian
.mantissa1
= source
->big_endian
.mantissa1
;
1628 /* Template macros to abstract over doubles and floats.
1629 XXX: Guile can only convert to/from doubles. */
1630 #define IEEE754_UNION(_c_type) union scm_ieee754_ ## _c_type
1631 #define IEEE754_TO_SCM(_c_type) scm_from_double
1632 #define IEEE754_FROM_SCM(_c_type) scm_to_double
1633 #define IEEE754_FROM_FOREIGN_ENDIANNESS(_c_type) \
1634 _c_type ## _from_foreign_endianness
1635 #define IEEE754_TO_FOREIGN_ENDIANNESS(_c_type) \
1636 _c_type ## _to_foreign_endianness
1639 /* FIXME: SCM_VALIDATE_REAL rejects integers, etc. grrr */
1640 #define VALIDATE_REAL(pos, v) \
1642 SCM_ASSERT_TYPE (scm_is_true (scm_rational_p (v)), v, pos, FUNC_NAME, "real"); \
1645 /* Templace getters and setters. */
1647 #define IEEE754_ACCESSOR_PROLOGUE(_type) \
1648 INTEGER_ACCESSOR_PROLOGUE (sizeof (_type) << 3UL, signed);
1650 #define IEEE754_REF(_type) \
1653 IEEE754_ACCESSOR_PROLOGUE (_type); \
1654 SCM_VALIDATE_SYMBOL (3, endianness); \
1656 if (scm_is_eq (endianness, scm_i_native_endianness)) \
1657 memcpy (&c_result, &c_bv[c_index], sizeof (c_result)); \
1660 IEEE754_UNION (_type) c_raw; \
1662 memcpy (&c_raw, &c_bv[c_index], sizeof (c_raw)); \
1664 IEEE754_FROM_FOREIGN_ENDIANNESS (_type) (&c_raw); \
1667 return (IEEE754_TO_SCM (_type) (c_result));
1669 #define IEEE754_NATIVE_REF(_type) \
1672 IEEE754_ACCESSOR_PROLOGUE (_type); \
1674 memcpy (&c_result, &c_bv[c_index], sizeof (c_result)); \
1675 return (IEEE754_TO_SCM (_type) (c_result));
1677 #define IEEE754_SET(_type) \
1680 IEEE754_ACCESSOR_PROLOGUE (_type); \
1681 VALIDATE_REAL (3, value); \
1682 SCM_VALIDATE_SYMBOL (4, endianness); \
1683 c_value = IEEE754_FROM_SCM (_type) (value); \
1685 if (scm_is_eq (endianness, scm_i_native_endianness)) \
1686 memcpy (&c_bv[c_index], &c_value, sizeof (c_value)); \
1689 IEEE754_UNION (_type) c_raw; \
1691 IEEE754_TO_FOREIGN_ENDIANNESS (_type) (&c_raw, c_value); \
1692 memcpy (&c_bv[c_index], &c_raw, sizeof (c_raw)); \
1695 return SCM_UNSPECIFIED;
1697 #define IEEE754_NATIVE_SET(_type) \
1700 IEEE754_ACCESSOR_PROLOGUE (_type); \
1701 VALIDATE_REAL (3, value); \
1702 c_value = IEEE754_FROM_SCM (_type) (value); \
1704 memcpy (&c_bv[c_index], &c_value, sizeof (c_value)); \
1705 return SCM_UNSPECIFIED;
1708 /* Single precision. */
1710 SCM_DEFINE (scm_bytevector_ieee_single_ref
,
1711 "bytevector-ieee-single-ref",
1713 (SCM bv
, SCM index
, SCM endianness
),
1714 "Return the IEEE-754 single from @var{bv} at "
1716 #define FUNC_NAME s_scm_bytevector_ieee_single_ref
1718 IEEE754_REF (float);
1722 SCM_DEFINE (scm_bytevector_ieee_single_native_ref
,
1723 "bytevector-ieee-single-native-ref",
1725 (SCM bv
, SCM index
),
1726 "Return the IEEE-754 single from @var{bv} at "
1727 "@var{index} using the native endianness.")
1728 #define FUNC_NAME s_scm_bytevector_ieee_single_native_ref
1730 IEEE754_NATIVE_REF (float);
1734 SCM_DEFINE (scm_bytevector_ieee_single_set_x
,
1735 "bytevector-ieee-single-set!",
1737 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1738 "Store real @var{value} in @var{bv} at @var{index} according to "
1739 "@var{endianness}.")
1740 #define FUNC_NAME s_scm_bytevector_ieee_single_set_x
1742 IEEE754_SET (float);
1746 SCM_DEFINE (scm_bytevector_ieee_single_native_set_x
,
1747 "bytevector-ieee-single-native-set!",
1749 (SCM bv
, SCM index
, SCM value
),
1750 "Store the real @var{value} at index @var{index} "
1751 "of @var{bv} using the native endianness.")
1752 #define FUNC_NAME s_scm_bytevector_ieee_single_native_set_x
1754 IEEE754_NATIVE_SET (float);
1759 /* Double precision. */
1761 SCM_DEFINE (scm_bytevector_ieee_double_ref
,
1762 "bytevector-ieee-double-ref",
1764 (SCM bv
, SCM index
, SCM endianness
),
1765 "Return the IEEE-754 double from @var{bv} at "
1767 #define FUNC_NAME s_scm_bytevector_ieee_double_ref
1769 IEEE754_REF (double);
1773 SCM_DEFINE (scm_bytevector_ieee_double_native_ref
,
1774 "bytevector-ieee-double-native-ref",
1776 (SCM bv
, SCM index
),
1777 "Return the IEEE-754 double from @var{bv} at "
1778 "@var{index} using the native endianness.")
1779 #define FUNC_NAME s_scm_bytevector_ieee_double_native_ref
1781 IEEE754_NATIVE_REF (double);
1785 SCM_DEFINE (scm_bytevector_ieee_double_set_x
,
1786 "bytevector-ieee-double-set!",
1788 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1789 "Store real @var{value} in @var{bv} at @var{index} according to "
1790 "@var{endianness}.")
1791 #define FUNC_NAME s_scm_bytevector_ieee_double_set_x
1793 IEEE754_SET (double);
1797 SCM_DEFINE (scm_bytevector_ieee_double_native_set_x
,
1798 "bytevector-ieee-double-native-set!",
1800 (SCM bv
, SCM index
, SCM value
),
1801 "Store the real @var{value} at index @var{index} "
1802 "of @var{bv} using the native endianness.")
1803 #define FUNC_NAME s_scm_bytevector_ieee_double_native_set_x
1805 IEEE754_NATIVE_SET (double);
1810 #undef IEEE754_UNION
1811 #undef IEEE754_TO_SCM
1812 #undef IEEE754_FROM_SCM
1813 #undef IEEE754_FROM_FOREIGN_ENDIANNESS
1814 #undef IEEE754_TO_FOREIGN_ENDIANNESS
1816 #undef IEEE754_NATIVE_REF
1818 #undef IEEE754_NATIVE_SET
1821 /* Operations on strings. */
1824 /* Produce a function that returns the length of a UTF-encoded string. */
1825 #define UTF_STRLEN_FUNCTION(_utf_width) \
1826 static inline size_t \
1827 utf ## _utf_width ## _strlen (const uint ## _utf_width ## _t *str) \
1830 const uint ## _utf_width ## _t *ptr; \
1838 return (len * ((_utf_width) / 8)); \
1841 UTF_STRLEN_FUNCTION (8)
1844 /* Return the length (in bytes) of STR, a UTF-(UTF_WIDTH) encoded string. */
1845 #define UTF_STRLEN(_utf_width, _str) \
1846 utf ## _utf_width ## _strlen (_str)
1848 /* Return the "portable" name of the UTF encoding of size UTF_WIDTH and
1849 ENDIANNESS (Gnulib's `iconv_open' module guarantees the portability of the
1852 utf_encoding_name (char *name
, size_t utf_width
, SCM endianness
)
1854 strcpy (name
, "UTF-");
1855 strcat (name
, ((utf_width
== 8)
1857 : ((utf_width
== 16)
1859 : ((utf_width
== 32)
1863 ((scm_is_eq (endianness
, scm_sym_big
))
1865 : ((scm_is_eq (endianness
, scm_sym_little
))
1870 /* Maximum length of a UTF encoding name. */
1871 #define MAX_UTF_ENCODING_NAME_LEN 16
1873 /* Produce the body of a `string->utf' function. */
1874 #define STRING_TO_UTF(_utf_width) \
1878 char c_utf_name[MAX_UTF_ENCODING_NAME_LEN]; \
1879 char *c_utf = NULL, *c_locale; \
1880 size_t c_strlen, c_raw_strlen, c_utf_len = 0; \
1882 SCM_VALIDATE_STRING (1, str); \
1883 if (endianness == SCM_UNDEFINED) \
1884 endianness = scm_sym_big; \
1886 SCM_VALIDATE_SYMBOL (2, endianness); \
1888 c_strlen = scm_c_string_length (str); \
1889 c_raw_strlen = c_strlen * ((_utf_width) / 8); \
1892 c_str = (char *) alloca (c_raw_strlen + 1); \
1893 c_raw_strlen = scm_to_locale_stringbuf (str, c_str, c_strlen); \
1895 while (c_raw_strlen > c_strlen); \
1896 c_str[c_raw_strlen] = '\0'; \
1898 utf_encoding_name (c_utf_name, (_utf_width), endianness); \
1900 c_locale = (char *) alloca (strlen (locale_charset ()) + 1); \
1901 strcpy (c_locale, locale_charset ()); \
1903 err = mem_iconveh (c_str, c_raw_strlen, \
1904 c_locale, c_utf_name, \
1905 iconveh_question_mark, NULL, \
1906 &c_utf, &c_utf_len); \
1907 if (SCM_UNLIKELY (err)) \
1908 scm_syserror_msg (FUNC_NAME, "failed to convert string: ~A", \
1909 scm_list_1 (str), err); \
1912 /* C_UTF is null-terminated. It is malloc(3)-allocated, so we cannot \
1913 use `scm_c_take_bytevector ()'. */ \
1914 scm_dynwind_begin (0); \
1915 scm_dynwind_free (c_utf); \
1917 utf = make_bytevector (c_utf_len, \
1918 SCM_ARRAY_ELEMENT_TYPE_VU8); \
1919 memcpy (SCM_BYTEVECTOR_CONTENTS (utf), c_utf, \
1922 scm_dynwind_end (); \
1929 SCM_DEFINE (scm_string_to_utf8
, "string->utf8",
1932 "Return a newly allocated bytevector that contains the UTF-8 "
1933 "encoding of @var{str}.")
1934 #define FUNC_NAME s_scm_string_to_utf8
1939 size_t c_strlen
, c_raw_strlen
;
1941 SCM_VALIDATE_STRING (1, str
);
1943 c_strlen
= scm_c_string_length (str
);
1944 c_raw_strlen
= c_strlen
;
1947 c_str
= (char *) alloca (c_raw_strlen
+ 1);
1948 c_raw_strlen
= scm_to_locale_stringbuf (str
, c_str
, c_strlen
);
1950 while (c_raw_strlen
> c_strlen
);
1951 c_str
[c_raw_strlen
] = '\0';
1953 c_utf
= u8_strconv_from_locale (c_str
);
1954 if (SCM_UNLIKELY (c_utf
== NULL
))
1955 scm_syserror (FUNC_NAME
);
1958 /* C_UTF is null-terminated. It is malloc(3)-allocated, so we cannot
1959 use `scm_c_take_bytevector ()'. */
1960 scm_dynwind_begin (0);
1961 scm_dynwind_free (c_utf
);
1963 utf
= make_bytevector (UTF_STRLEN (8, c_utf
),
1964 SCM_ARRAY_ELEMENT_TYPE_VU8
);
1965 memcpy (SCM_BYTEVECTOR_CONTENTS (utf
), c_utf
,
1966 UTF_STRLEN (8, c_utf
));
1975 SCM_DEFINE (scm_string_to_utf16
, "string->utf16",
1977 (SCM str
, SCM endianness
),
1978 "Return a newly allocated bytevector that contains the UTF-16 "
1979 "encoding of @var{str}.")
1980 #define FUNC_NAME s_scm_string_to_utf16
1986 SCM_DEFINE (scm_string_to_utf32
, "string->utf32",
1988 (SCM str
, SCM endianness
),
1989 "Return a newly allocated bytevector that contains the UTF-32 "
1990 "encoding of @var{str}.")
1991 #define FUNC_NAME s_scm_string_to_utf32
1998 /* Produce the body of a function that converts a UTF-encoded bytevector to a
2000 #define UTF_TO_STRING(_utf_width) \
2001 SCM str = SCM_BOOL_F; \
2003 char *c_str = NULL, *c_locale; \
2004 char c_utf_name[MAX_UTF_ENCODING_NAME_LEN]; \
2005 const char *c_utf; \
2006 size_t c_strlen = 0, c_utf_len; \
2008 SCM_VALIDATE_BYTEVECTOR (1, utf); \
2009 if (endianness == SCM_UNDEFINED) \
2010 endianness = scm_sym_big; \
2012 SCM_VALIDATE_SYMBOL (2, endianness); \
2014 c_utf_len = SCM_BYTEVECTOR_LENGTH (utf); \
2015 c_utf = (char *) SCM_BYTEVECTOR_CONTENTS (utf); \
2016 utf_encoding_name (c_utf_name, (_utf_width), endianness); \
2018 c_locale = (char *) alloca (strlen (locale_charset ()) + 1); \
2019 strcpy (c_locale, locale_charset ()); \
2021 err = mem_iconveh (c_utf, c_utf_len, \
2022 c_utf_name, c_locale, \
2023 iconveh_question_mark, NULL, \
2024 &c_str, &c_strlen); \
2025 if (SCM_UNLIKELY (err)) \
2026 scm_syserror_msg (FUNC_NAME, "failed to convert to string: ~A", \
2027 scm_list_1 (utf), err); \
2029 /* C_STR is null-terminated. */ \
2030 str = scm_take_locale_stringn (c_str, c_strlen); \
2035 SCM_DEFINE (scm_utf8_to_string
, "utf8->string",
2038 "Return a newly allocate string that contains from the UTF-8-"
2039 "encoded contents of bytevector @var{utf}.")
2040 #define FUNC_NAME s_scm_utf8_to_string
2044 char *c_str
= NULL
, *c_locale
;
2046 size_t c_utf_len
, c_strlen
= 0;
2048 SCM_VALIDATE_BYTEVECTOR (1, utf
);
2050 c_utf_len
= SCM_BYTEVECTOR_LENGTH (utf
);
2052 c_locale
= (char *) alloca (strlen (locale_charset ()) + 1);
2053 strcpy (c_locale
, locale_charset ());
2055 c_utf
= (char *) SCM_BYTEVECTOR_CONTENTS (utf
);
2056 err
= mem_iconveh (c_utf
, c_utf_len
,
2058 iconveh_question_mark
, NULL
,
2060 if (SCM_UNLIKELY (err
))
2061 scm_syserror_msg (FUNC_NAME
, "failed to convert to string: ~A",
2062 scm_list_1 (utf
), err
);
2064 /* C_STR is null-terminated. */
2065 str
= scm_take_locale_stringn (c_str
, c_strlen
);
2071 SCM_DEFINE (scm_utf16_to_string
, "utf16->string",
2073 (SCM utf
, SCM endianness
),
2074 "Return a newly allocate string that contains from the UTF-16-"
2075 "encoded contents of bytevector @var{utf}.")
2076 #define FUNC_NAME s_scm_utf16_to_string
2082 SCM_DEFINE (scm_utf32_to_string
, "utf32->string",
2084 (SCM utf
, SCM endianness
),
2085 "Return a newly allocate string that contains from the UTF-32-"
2086 "encoded contents of bytevector @var{utf}.")
2087 #define FUNC_NAME s_scm_utf32_to_string
2095 /* Bytevectors as generalized vectors & arrays. */
2099 bytevector_ref_c32 (SCM bv
, SCM idx
)
2100 { /* FIXME add some checks */
2101 const float *contents
= (const float*)SCM_BYTEVECTOR_CONTENTS (bv
);
2102 size_t i
= scm_to_size_t (idx
);
2103 return scm_c_make_rectangular (contents
[i
/8], contents
[i
/8 + 1]);
2107 bytevector_ref_c64 (SCM bv
, SCM idx
)
2108 { /* FIXME add some checks */
2109 const double *contents
= (const double*)SCM_BYTEVECTOR_CONTENTS (bv
);
2110 size_t i
= scm_to_size_t (idx
);
2111 return scm_c_make_rectangular (contents
[i
/16], contents
[i
/16 + 1]);
2114 typedef SCM (*scm_t_bytevector_ref_fn
)(SCM
, SCM
);
2116 const scm_t_bytevector_ref_fn bytevector_ref_fns
[SCM_ARRAY_ELEMENT_TYPE_LAST
+ 1] =
2121 scm_bytevector_u8_ref
, /* VU8 */
2122 scm_bytevector_u8_ref
, /* U8 */
2123 scm_bytevector_s8_ref
,
2124 scm_bytevector_u16_native_ref
,
2125 scm_bytevector_s16_native_ref
,
2126 scm_bytevector_u32_native_ref
,
2127 scm_bytevector_s32_native_ref
,
2128 scm_bytevector_u64_native_ref
,
2129 scm_bytevector_s64_native_ref
,
2130 scm_bytevector_ieee_single_native_ref
,
2131 scm_bytevector_ieee_double_native_ref
,
2137 bv_handle_ref (scm_t_array_handle
*h
, size_t index
)
2140 scm_t_bytevector_ref_fn ref_fn
;
2142 ref_fn
= bytevector_ref_fns
[h
->element_type
];
2144 scm_from_size_t (index
* scm_array_handle_uniform_element_size (h
));
2145 return ref_fn (h
->array
, byte_index
);
2149 bytevector_set_c32 (SCM bv
, SCM idx
, SCM val
)
2150 { /* checks are unnecessary here */
2151 float *contents
= (float*)SCM_BYTEVECTOR_CONTENTS (bv
);
2152 size_t i
= scm_to_size_t (idx
);
2153 contents
[i
/8] = scm_c_real_part (val
);
2154 contents
[i
/8 + 1] = scm_c_imag_part (val
);
2155 return SCM_UNSPECIFIED
;
2159 bytevector_set_c64 (SCM bv
, SCM idx
, SCM val
)
2160 { /* checks are unnecessary here */
2161 double *contents
= (double*)SCM_BYTEVECTOR_CONTENTS (bv
);
2162 size_t i
= scm_to_size_t (idx
);
2163 contents
[i
/16] = scm_c_real_part (val
);
2164 contents
[i
/16 + 1] = scm_c_imag_part (val
);
2165 return SCM_UNSPECIFIED
;
2168 typedef SCM (*scm_t_bytevector_set_fn
)(SCM
, SCM
, SCM
);
2170 const scm_t_bytevector_set_fn bytevector_set_fns
[SCM_ARRAY_ELEMENT_TYPE_LAST
+ 1] =
2175 scm_bytevector_u8_set_x
, /* VU8 */
2176 scm_bytevector_u8_set_x
, /* U8 */
2177 scm_bytevector_s8_set_x
,
2178 scm_bytevector_u16_native_set_x
,
2179 scm_bytevector_s16_native_set_x
,
2180 scm_bytevector_u32_native_set_x
,
2181 scm_bytevector_s32_native_set_x
,
2182 scm_bytevector_u64_native_set_x
,
2183 scm_bytevector_s64_native_set_x
,
2184 scm_bytevector_ieee_single_native_set_x
,
2185 scm_bytevector_ieee_double_native_set_x
,
2191 bv_handle_set_x (scm_t_array_handle
*h
, size_t index
, SCM val
)
2194 scm_t_bytevector_set_fn set_fn
;
2196 set_fn
= bytevector_set_fns
[h
->element_type
];
2198 scm_from_size_t (index
* scm_array_handle_uniform_element_size (h
));
2199 set_fn (h
->array
, byte_index
, val
);
2203 bytevector_get_handle (SCM v
, scm_t_array_handle
*h
)
2209 h
->dim0
.ubnd
= SCM_BYTEVECTOR_TYPED_LENGTH (v
) - 1;
2211 h
->element_type
= SCM_BYTEVECTOR_ELEMENT_TYPE (v
);
2212 h
->elements
= h
->writable_elements
= SCM_BYTEVECTOR_CONTENTS (v
);
2216 /* Initialization. */
2219 scm_bootstrap_bytevectors (void)
2221 /* This must be instantiated here because the generalized-vector API may
2222 want to access bytevectors even though `(rnrs bytevector)' hasn't been
2224 scm_null_bytevector
=
2225 scm_gc_protect_object (make_bytevector (0, SCM_ARRAY_ELEMENT_TYPE_VU8
));
2227 #ifdef WORDS_BIGENDIAN
2228 scm_i_native_endianness
= scm_permanent_object (scm_from_locale_symbol ("big"));
2230 scm_i_native_endianness
= scm_permanent_object (scm_from_locale_symbol ("little"));
2233 scm_c_register_extension ("libguile", "scm_init_bytevectors",
2234 (scm_t_extension_init_func
) scm_init_bytevectors
,
2238 scm_t_array_implementation impl
;
2240 impl
.tag
= scm_tc7_bytevector
;
2242 impl
.vref
= bv_handle_ref
;
2243 impl
.vset
= bv_handle_set_x
;
2244 impl
.get_handle
= bytevector_get_handle
;
2245 scm_i_register_array_implementation (&impl
);
2246 scm_i_register_vector_constructor
2247 (scm_i_array_element_types
[SCM_ARRAY_ELEMENT_TYPE_VU8
],
2248 scm_make_bytevector
);
2253 scm_init_bytevectors (void)
2255 #include "libguile/bytevectors.x"
2257 scm_endianness_big
= scm_sym_big
;
2258 scm_endianness_little
= scm_sym_little
;