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
28 #include "libguile/_scm.h"
29 #include "libguile/extensions.h"
30 #include "libguile/bytevectors.h"
31 #include "libguile/strings.h"
32 #include "libguile/validate.h"
33 #include "libguile/ieee-754.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>
46 /* Assuming 32-bit longs. */
47 # define ULONG_MAX 4294967295UL
56 /* Convenience macros. These are used by the various templates (macros) that
57 are parameterized by integer signedness. */
58 #define INT8_T_signed scm_t_int8
59 #define INT8_T_unsigned scm_t_uint8
60 #define INT16_T_signed scm_t_int16
61 #define INT16_T_unsigned scm_t_uint16
62 #define INT32_T_signed scm_t_int32
63 #define INT32_T_unsigned scm_t_uint32
64 #define is_signed_int8(_x) (((_x) >= -128L) && ((_x) <= 127L))
65 #define is_unsigned_int8(_x) ((_x) <= 255UL)
66 #define is_signed_int16(_x) (((_x) >= -32768L) && ((_x) <= 32767L))
67 #define is_unsigned_int16(_x) ((_x) <= 65535UL)
68 #define is_signed_int32(_x) (((_x) >= -2147483648L) && ((_x) <= 2147483647L))
69 #define is_unsigned_int32(_x) ((_x) <= 4294967295UL)
70 #define SIGNEDNESS_signed 1
71 #define SIGNEDNESS_unsigned 0
73 #define INT_TYPE(_size, _sign) INT ## _size ## _T_ ## _sign
74 #define INT_SWAP(_size) bswap_ ## _size
75 #define INT_VALID_P(_size, _sign) is_ ## _sign ## _int ## _size
76 #define SIGNEDNESS(_sign) SIGNEDNESS_ ## _sign
79 #define INTEGER_ACCESSOR_PROLOGUE(_len, _sign) \
80 size_t c_len, c_index; \
83 SCM_VALIDATE_BYTEVECTOR (1, bv); \
84 c_index = scm_to_uint (index); \
86 c_len = SCM_BYTEVECTOR_LENGTH (bv); \
87 c_bv = (_sign char *) SCM_BYTEVECTOR_CONTENTS (bv); \
89 if (SCM_UNLIKELY (c_index + ((_len) >> 3UL) - 1 >= c_len)) \
90 scm_out_of_range (FUNC_NAME, index);
92 /* Template for fixed-size integer access (only 8, 16 or 32-bit). */
93 #define INTEGER_REF(_len, _sign) \
96 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
97 SCM_VALIDATE_SYMBOL (3, endianness); \
100 INT_TYPE (_len, _sign) c_result; \
102 memcpy (&c_result, &c_bv[c_index], (_len) / 8); \
103 if (!scm_is_eq (endianness, scm_i_native_endianness)) \
104 c_result = INT_SWAP (_len) (c_result); \
106 result = SCM_I_MAKINUM (c_result); \
111 /* Template for fixed-size integer access using the native endianness. */
112 #define INTEGER_NATIVE_REF(_len, _sign) \
115 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
118 INT_TYPE (_len, _sign) c_result; \
120 memcpy (&c_result, &c_bv[c_index], (_len) / 8); \
121 result = SCM_I_MAKINUM (c_result); \
126 /* Template for fixed-size integer modification (only 8, 16 or 32-bit). */
127 #define INTEGER_SET(_len, _sign) \
128 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
129 SCM_VALIDATE_SYMBOL (3, endianness); \
132 _sign long c_value; \
133 INT_TYPE (_len, _sign) c_value_short; \
135 if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
136 scm_wrong_type_arg (FUNC_NAME, 3, value); \
138 c_value = SCM_I_INUM (value); \
139 if (SCM_UNLIKELY (!INT_VALID_P (_len, _sign) (c_value))) \
140 scm_out_of_range (FUNC_NAME, value); \
142 c_value_short = (INT_TYPE (_len, _sign)) c_value; \
143 if (!scm_is_eq (endianness, scm_i_native_endianness)) \
144 c_value_short = INT_SWAP (_len) (c_value_short); \
146 memcpy (&c_bv[c_index], &c_value_short, (_len) / 8); \
149 return SCM_UNSPECIFIED;
151 /* Template for fixed-size integer modification using the native
153 #define INTEGER_NATIVE_SET(_len, _sign) \
154 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
157 _sign long c_value; \
158 INT_TYPE (_len, _sign) c_value_short; \
160 if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
161 scm_wrong_type_arg (FUNC_NAME, 3, value); \
163 c_value = SCM_I_INUM (value); \
164 if (SCM_UNLIKELY (!INT_VALID_P (_len, _sign) (c_value))) \
165 scm_out_of_range (FUNC_NAME, value); \
167 c_value_short = (INT_TYPE (_len, _sign)) c_value; \
169 memcpy (&c_bv[c_index], &c_value_short, (_len) / 8); \
172 return SCM_UNSPECIFIED;
176 /* Bytevector type. */
178 #define SCM_BYTEVECTOR_HEADER_BYTES \
179 (SCM_BYTEVECTOR_HEADER_SIZE * sizeof (SCM))
181 #define SCM_BYTEVECTOR_SET_LENGTH(_bv, _len) \
182 SCM_SET_CELL_WORD_1 ((_bv), (scm_t_bits) (_len))
184 #define SCM_BYTEVECTOR_SET_ELEMENT_TYPE(bv, hint) \
185 SCM_SET_BYTEVECTOR_FLAGS ((bv), (hint))
186 #define SCM_BYTEVECTOR_TYPE_SIZE(var) \
187 (scm_i_array_element_type_sizes[SCM_BYTEVECTOR_ELEMENT_TYPE (var)]/8)
188 #define SCM_BYTEVECTOR_TYPED_LENGTH(var) \
189 SCM_BYTEVECTOR_LENGTH (var) / SCM_BYTEVECTOR_TYPE_SIZE (var)
191 /* The empty bytevector. */
192 SCM scm_null_bytevector
= SCM_UNSPECIFIED
;
196 make_bytevector (size_t len
, scm_t_array_element_type element_type
)
201 if (SCM_UNLIKELY (element_type
> SCM_ARRAY_ELEMENT_TYPE_LAST
202 || scm_i_array_element_type_sizes
[element_type
] < 8
203 || len
>= (SCM_I_SIZE_MAX
204 / (scm_i_array_element_type_sizes
[element_type
]/8))))
205 /* This would be an internal Guile programming error */
208 if (SCM_UNLIKELY (len
== 0 && element_type
== SCM_ARRAY_ELEMENT_TYPE_VU8
209 && SCM_BYTEVECTOR_P (scm_null_bytevector
)))
210 ret
= scm_null_bytevector
;
213 c_len
= len
* (scm_i_array_element_type_sizes
[element_type
] / 8);
215 ret
= PTR2SCM (scm_gc_malloc_pointerless (SCM_BYTEVECTOR_HEADER_BYTES
+ c_len
,
218 SCM_SET_CELL_TYPE (ret
, scm_tc7_bytevector
);
219 SCM_BYTEVECTOR_SET_LENGTH (ret
, c_len
);
220 SCM_BYTEVECTOR_SET_ELEMENT_TYPE (ret
, element_type
);
226 /* Return a bytevector of LEN elements of type ELEMENT_TYPE, with element
227 values taken from CONTENTS. */
229 make_bytevector_from_buffer (size_t len
, void *contents
,
230 scm_t_array_element_type element_type
)
234 /* We actually never reuse storage from CONTENTS. Hans Boehm says in
235 <gc/gc.h> that realloc(3) "shouldn't have been invented" and he may well
237 ret
= make_bytevector (len
, element_type
);
243 c_len
= len
* (scm_i_array_element_type_sizes
[element_type
] / 8);
244 memcpy (SCM_BYTEVECTOR_CONTENTS (ret
),
248 scm_gc_free (contents
, c_len
, SCM_GC_BYTEVECTOR
);
255 /* Return a new bytevector of size LEN octets. */
257 scm_c_make_bytevector (size_t len
)
259 return make_bytevector (len
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
262 /* Return a new bytevector of size LEN elements. */
264 scm_i_make_typed_bytevector (size_t len
, scm_t_array_element_type element_type
)
266 return make_bytevector (len
, element_type
);
269 /* Return a bytevector of size LEN made up of CONTENTS. The area pointed to
270 by CONTENTS must have been allocated using `scm_gc_malloc ()'. */
272 scm_c_take_bytevector (signed char *contents
, size_t len
)
274 return make_bytevector_from_buffer (len
, contents
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
278 scm_c_take_typed_bytevector (signed char *contents
, size_t len
,
279 scm_t_array_element_type element_type
)
281 return make_bytevector_from_buffer (len
, contents
, element_type
);
284 /* Shrink BV to C_NEW_LEN (which is assumed to be smaller than its current
285 size) and return the new bytevector (possibly different from BV). */
287 scm_c_shrink_bytevector (SCM bv
, size_t c_new_len
)
292 if (SCM_UNLIKELY (c_new_len
% SCM_BYTEVECTOR_TYPE_SIZE (bv
)))
293 /* This would be an internal Guile programming error */
296 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
297 if (SCM_UNLIKELY (c_new_len
> c_len
))
300 SCM_BYTEVECTOR_SET_LENGTH (bv
, c_new_len
);
302 /* Resize the existing buffer. */
303 new_bv
= PTR2SCM (scm_gc_realloc (SCM2PTR (bv
),
304 c_len
+ SCM_BYTEVECTOR_HEADER_BYTES
,
305 c_new_len
+ SCM_BYTEVECTOR_HEADER_BYTES
,
312 scm_is_bytevector (SCM obj
)
314 return SCM_BYTEVECTOR_P (obj
);
318 scm_c_bytevector_length (SCM bv
)
319 #define FUNC_NAME "scm_c_bytevector_length"
321 SCM_VALIDATE_BYTEVECTOR (1, bv
);
323 return SCM_BYTEVECTOR_LENGTH (bv
);
328 scm_c_bytevector_ref (SCM bv
, size_t index
)
329 #define FUNC_NAME "scm_c_bytevector_ref"
332 const scm_t_uint8
*c_bv
;
334 SCM_VALIDATE_BYTEVECTOR (1, bv
);
336 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
337 c_bv
= (scm_t_uint8
*) SCM_BYTEVECTOR_CONTENTS (bv
);
339 if (SCM_UNLIKELY (index
>= c_len
))
340 scm_out_of_range (FUNC_NAME
, scm_from_size_t (index
));
347 scm_c_bytevector_set_x (SCM bv
, size_t index
, scm_t_uint8 value
)
348 #define FUNC_NAME "scm_c_bytevector_set_x"
353 SCM_VALIDATE_BYTEVECTOR (1, bv
);
355 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
356 c_bv
= (scm_t_uint8
*) SCM_BYTEVECTOR_CONTENTS (bv
);
358 if (SCM_UNLIKELY (index
>= c_len
))
359 scm_out_of_range (FUNC_NAME
, scm_from_size_t (index
));
368 scm_i_print_bytevector (SCM bv
, SCM port
, scm_print_state
*pstate SCM_UNUSED
)
370 ssize_t ubnd
, inc
, i
;
371 scm_t_array_handle h
;
373 scm_array_get_handle (bv
, &h
);
375 scm_putc ('#', port
);
376 scm_write (scm_array_handle_element_type (&h
), port
);
377 scm_putc ('(', port
);
378 for (i
= h
.dims
[0].lbnd
, ubnd
= h
.dims
[0].ubnd
, inc
= h
.dims
[0].inc
;
382 scm_putc (' ', port
);
383 scm_write (scm_array_handle_ref (&h
, i
), port
);
385 scm_putc (')', port
);
391 /* General operations. */
393 SCM_SYMBOL (scm_sym_big
, "big");
394 SCM_SYMBOL (scm_sym_little
, "little");
396 SCM scm_endianness_big
, scm_endianness_little
;
398 /* Host endianness (a symbol). */
399 SCM scm_i_native_endianness
= SCM_UNSPECIFIED
;
403 # define bswap_24(_x) \
404 ((((_x) & 0xff0000) >> 16) | \
405 (((_x) & 0x00ff00)) | \
406 (((_x) & 0x0000ff) << 16))
410 SCM_DEFINE (scm_native_endianness
, "native-endianness", 0, 0, 0,
412 "Return a symbol denoting the machine's native endianness.")
413 #define FUNC_NAME s_scm_native_endianness
415 return scm_i_native_endianness
;
419 SCM_DEFINE (scm_bytevector_p
, "bytevector?", 1, 0, 0,
421 "Return true if @var{obj} is a bytevector.")
422 #define FUNC_NAME s_scm_bytevector_p
424 return scm_from_bool (scm_is_bytevector (obj
));
428 SCM_DEFINE (scm_make_bytevector
, "make-bytevector", 1, 1, 0,
430 "Return a newly allocated bytevector of @var{len} bytes, "
431 "optionally filled with @var{fill}.")
432 #define FUNC_NAME s_scm_make_bytevector
436 signed char c_fill
= '\0';
438 SCM_VALIDATE_UINT_COPY (1, len
, c_len
);
439 if (fill
!= SCM_UNDEFINED
)
443 value
= scm_to_int (fill
);
444 if (SCM_UNLIKELY ((value
< -128) || (value
> 255)))
445 scm_out_of_range (FUNC_NAME
, fill
);
446 c_fill
= (signed char) value
;
449 bv
= make_bytevector (c_len
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
450 if (fill
!= SCM_UNDEFINED
)
453 signed char *contents
;
455 contents
= SCM_BYTEVECTOR_CONTENTS (bv
);
456 for (i
= 0; i
< c_len
; i
++)
457 contents
[i
] = c_fill
;
464 SCM_DEFINE (scm_bytevector_length
, "bytevector-length", 1, 0, 0,
466 "Return the length (in bytes) of @var{bv}.")
467 #define FUNC_NAME s_scm_bytevector_length
469 return scm_from_uint (scm_c_bytevector_length (bv
));
473 SCM_DEFINE (scm_bytevector_eq_p
, "bytevector=?", 2, 0, 0,
475 "Return is @var{bv1} equals to @var{bv2}---i.e., if they "
476 "have the same length and contents.")
477 #define FUNC_NAME s_scm_bytevector_eq_p
479 SCM result
= SCM_BOOL_F
;
480 unsigned c_len1
, c_len2
;
482 SCM_VALIDATE_BYTEVECTOR (1, bv1
);
483 SCM_VALIDATE_BYTEVECTOR (2, bv2
);
485 c_len1
= SCM_BYTEVECTOR_LENGTH (bv1
);
486 c_len2
= SCM_BYTEVECTOR_LENGTH (bv2
);
488 if (c_len1
== c_len2
)
490 signed char *c_bv1
, *c_bv2
;
492 c_bv1
= SCM_BYTEVECTOR_CONTENTS (bv1
);
493 c_bv2
= SCM_BYTEVECTOR_CONTENTS (bv2
);
495 result
= scm_from_bool (!memcmp (c_bv1
, c_bv2
, c_len1
));
502 SCM_DEFINE (scm_bytevector_fill_x
, "bytevector-fill!", 2, 0, 0,
504 "Fill bytevector @var{bv} with @var{fill}, a byte.")
505 #define FUNC_NAME s_scm_bytevector_fill_x
508 signed char *c_bv
, c_fill
;
510 SCM_VALIDATE_BYTEVECTOR (1, bv
);
511 c_fill
= scm_to_int8 (fill
);
513 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
514 c_bv
= SCM_BYTEVECTOR_CONTENTS (bv
);
516 for (i
= 0; i
< c_len
; i
++)
519 return SCM_UNSPECIFIED
;
523 SCM_DEFINE (scm_bytevector_copy_x
, "bytevector-copy!", 5, 0, 0,
524 (SCM source
, SCM source_start
, SCM target
, SCM target_start
,
526 "Copy @var{len} bytes from @var{source} into @var{target}, "
527 "starting reading from @var{source_start} (a positive index "
528 "within @var{source}) and start writing at "
529 "@var{target_start}.")
530 #define FUNC_NAME s_scm_bytevector_copy_x
532 unsigned c_len
, c_source_len
, c_target_len
;
533 unsigned c_source_start
, c_target_start
;
534 signed char *c_source
, *c_target
;
536 SCM_VALIDATE_BYTEVECTOR (1, source
);
537 SCM_VALIDATE_BYTEVECTOR (3, target
);
539 c_len
= scm_to_uint (len
);
540 c_source_start
= scm_to_uint (source_start
);
541 c_target_start
= scm_to_uint (target_start
);
543 c_source
= SCM_BYTEVECTOR_CONTENTS (source
);
544 c_target
= SCM_BYTEVECTOR_CONTENTS (target
);
545 c_source_len
= SCM_BYTEVECTOR_LENGTH (source
);
546 c_target_len
= SCM_BYTEVECTOR_LENGTH (target
);
548 if (SCM_UNLIKELY (c_source_start
+ c_len
> c_source_len
))
549 scm_out_of_range (FUNC_NAME
, source_start
);
550 if (SCM_UNLIKELY (c_target_start
+ c_len
> c_target_len
))
551 scm_out_of_range (FUNC_NAME
, target_start
);
553 memcpy (c_target
+ c_target_start
,
554 c_source
+ c_source_start
,
557 return SCM_UNSPECIFIED
;
561 SCM_DEFINE (scm_bytevector_copy
, "bytevector-copy", 1, 0, 0,
563 "Return a newly allocated copy of @var{bv}.")
564 #define FUNC_NAME s_scm_bytevector_copy
568 signed char *c_bv
, *c_copy
;
570 SCM_VALIDATE_BYTEVECTOR (1, bv
);
572 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
573 c_bv
= SCM_BYTEVECTOR_CONTENTS (bv
);
575 copy
= make_bytevector (c_len
, SCM_BYTEVECTOR_ELEMENT_TYPE (bv
));
576 c_copy
= SCM_BYTEVECTOR_CONTENTS (copy
);
577 memcpy (c_copy
, c_bv
, c_len
);
583 SCM_DEFINE (scm_uniform_array_to_bytevector
, "uniform-array->bytevector",
584 1, 0, 0, (SCM array
),
585 "Return a newly allocated bytevector whose contents\n"
586 "will be copied from the uniform array @var{array}.")
587 #define FUNC_NAME s_scm_uniform_array_to_bytevector
591 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
);
601 base
= scm_array_handle_uniform_elements (&h
);
602 len
= h
.dims
->inc
* (h
.dims
->ubnd
- h
.dims
->lbnd
+ 1);
603 sz
= scm_array_handle_uniform_element_size (&h
);
605 ret
= make_bytevector (len
* sz
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
606 memcpy (SCM_BYTEVECTOR_CONTENTS (ret
), base
, len
* sz
);
608 scm_array_handle_release (&h
);
615 /* Operations on bytes and octets. */
617 SCM_DEFINE (scm_bytevector_u8_ref
, "bytevector-u8-ref", 2, 0, 0,
619 "Return the octet located at @var{index} in @var{bv}.")
620 #define FUNC_NAME s_scm_bytevector_u8_ref
622 INTEGER_NATIVE_REF (8, unsigned);
626 SCM_DEFINE (scm_bytevector_s8_ref
, "bytevector-s8-ref", 2, 0, 0,
628 "Return the byte located at @var{index} in @var{bv}.")
629 #define FUNC_NAME s_scm_bytevector_s8_ref
631 INTEGER_NATIVE_REF (8, signed);
635 SCM_DEFINE (scm_bytevector_u8_set_x
, "bytevector-u8-set!", 3, 0, 0,
636 (SCM bv
, SCM index
, SCM value
),
637 "Return the octet located at @var{index} in @var{bv}.")
638 #define FUNC_NAME s_scm_bytevector_u8_set_x
640 INTEGER_NATIVE_SET (8, unsigned);
644 SCM_DEFINE (scm_bytevector_s8_set_x
, "bytevector-s8-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_s8_set_x
649 INTEGER_NATIVE_SET (8, signed);
653 #undef OCTET_ACCESSOR_PROLOGUE
656 SCM_DEFINE (scm_bytevector_to_u8_list
, "bytevector->u8-list", 1, 0, 0,
658 "Return a newly allocated list of octets containing the "
659 "contents of @var{bv}.")
660 #define FUNC_NAME s_scm_bytevector_to_u8_list
666 SCM_VALIDATE_BYTEVECTOR (1, bv
);
668 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
669 c_bv
= (unsigned char *) SCM_BYTEVECTOR_CONTENTS (bv
);
671 lst
= scm_make_list (scm_from_uint (c_len
), SCM_UNSPECIFIED
);
672 for (i
= 0, pair
= lst
;
674 i
++, pair
= SCM_CDR (pair
))
676 SCM_SETCAR (pair
, SCM_I_MAKINUM (c_bv
[i
]));
683 SCM_DEFINE (scm_u8_list_to_bytevector
, "u8-list->bytevector", 1, 0, 0,
685 "Turn @var{lst}, a list of octets, into a bytevector.")
686 #define FUNC_NAME s_scm_u8_list_to_bytevector
692 SCM_VALIDATE_LIST_COPYLEN (1, lst
, c_len
);
694 bv
= make_bytevector (c_len
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
695 c_bv
= (unsigned char *) SCM_BYTEVECTOR_CONTENTS (bv
);
697 for (i
= 0; i
< c_len
; lst
= SCM_CDR (lst
), i
++)
699 item
= SCM_CAR (lst
);
701 if (SCM_LIKELY (SCM_I_INUMP (item
)))
705 c_item
= SCM_I_INUM (item
);
706 if (SCM_LIKELY ((c_item
>= 0) && (c_item
< 256)))
707 c_bv
[i
] = (unsigned char) c_item
;
718 scm_wrong_type_arg (FUNC_NAME
, 1, item
);
724 /* Compute the two's complement of VALUE (a positive integer) on SIZE octets
725 using (2^(SIZE * 8) - VALUE). */
727 twos_complement (mpz_t value
, size_t size
)
729 unsigned long bit_count
;
731 /* We expect BIT_COUNT to fit in a unsigned long thanks to the range
732 checking on SIZE performed earlier. */
733 bit_count
= (unsigned long) size
<< 3UL;
735 if (SCM_LIKELY (bit_count
< sizeof (unsigned long)))
736 mpz_ui_sub (value
, 1UL << bit_count
, value
);
742 mpz_ui_pow_ui (max
, 2, bit_count
);
743 mpz_sub (value
, max
, value
);
749 bytevector_large_ref (const char *c_bv
, size_t c_size
, int signed_p
,
754 int c_endianness
, negative_p
= 0;
758 if (scm_is_eq (endianness
, scm_sym_big
))
759 negative_p
= c_bv
[0] & 0x80;
761 negative_p
= c_bv
[c_size
- 1] & 0x80;
764 c_endianness
= scm_is_eq (endianness
, scm_sym_big
) ? 1 : -1;
767 mpz_import (c_mpz
, 1 /* 1 word */, 1 /* word order doesn't matter */,
768 c_size
/* word is C_SIZE-byte long */,
770 0 /* nails */, c_bv
);
772 if (signed_p
&& negative_p
)
774 twos_complement (c_mpz
, c_size
);
775 mpz_neg (c_mpz
, c_mpz
);
778 result
= scm_from_mpz (c_mpz
);
779 mpz_clear (c_mpz
); /* FIXME: Needed? */
785 bytevector_large_set (char *c_bv
, size_t c_size
, int signed_p
,
786 SCM value
, SCM endianness
)
789 int c_endianness
, c_sign
, err
= 0;
791 c_endianness
= scm_is_eq (endianness
, scm_sym_big
) ? 1 : -1;
794 scm_to_mpz (value
, c_mpz
);
796 c_sign
= mpz_sgn (c_mpz
);
799 if (SCM_LIKELY (signed_p
))
801 mpz_neg (c_mpz
, c_mpz
);
802 twos_complement (c_mpz
, c_size
);
813 memset (c_bv
, 0, c_size
);
816 size_t word_count
, value_size
;
818 value_size
= (mpz_sizeinbase (c_mpz
, 2) + (8 * c_size
)) / (8 * c_size
);
819 if (SCM_UNLIKELY (value_size
> c_size
))
826 mpz_export (c_bv
, &word_count
, 1 /* word order doesn't matter */,
827 c_size
, c_endianness
,
828 0 /* nails */, c_mpz
);
829 if (SCM_UNLIKELY (word_count
!= 1))
830 /* Shouldn't happen since we already checked with VALUE_SIZE. */
840 #define GENERIC_INTEGER_ACCESSOR_PROLOGUE(_sign) \
841 unsigned long c_len, c_index, c_size; \
844 SCM_VALIDATE_BYTEVECTOR (1, bv); \
845 c_index = scm_to_ulong (index); \
846 c_size = scm_to_ulong (size); \
848 c_len = SCM_BYTEVECTOR_LENGTH (bv); \
849 c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
851 /* C_SIZE must have its 3 higher bits set to zero so that \
852 multiplying it by 8 yields a number that fits in an \
854 if (SCM_UNLIKELY ((c_size == 0) || (c_size >= (ULONG_MAX >> 3L)))) \
855 scm_out_of_range (FUNC_NAME, size); \
856 if (SCM_UNLIKELY (c_index + c_size > c_len)) \
857 scm_out_of_range (FUNC_NAME, index);
860 /* Template of an integer reference function. */
861 #define GENERIC_INTEGER_REF(_sign) \
869 swap = !scm_is_eq (endianness, scm_i_native_endianness); \
874 _sign char c_value8; \
875 memcpy (&c_value8, c_bv, 1); \
881 INT_TYPE (16, _sign) c_value16; \
882 memcpy (&c_value16, c_bv, 2); \
884 value = (INT_TYPE (16, _sign)) bswap_16 (c_value16); \
893 result = SCM_I_MAKINUM ((_sign int) value); \
896 result = bytevector_large_ref ((char *) c_bv, \
897 c_size, SIGNEDNESS (_sign), \
903 bytevector_signed_ref (const char *c_bv
, size_t c_size
, SCM endianness
)
905 GENERIC_INTEGER_REF (signed);
909 bytevector_unsigned_ref (const char *c_bv
, size_t c_size
, SCM endianness
)
911 GENERIC_INTEGER_REF (unsigned);
915 /* Template of an integer assignment function. */
916 #define GENERIC_INTEGER_SET(_sign) \
921 if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
924 c_value = SCM_I_INUM (value); \
928 if (SCM_LIKELY (INT_VALID_P (8, _sign) (c_value))) \
930 _sign char c_value8; \
931 c_value8 = (_sign char) c_value; \
932 memcpy (c_bv, &c_value8, 1); \
939 if (SCM_LIKELY (INT_VALID_P (16, _sign) (c_value))) \
942 INT_TYPE (16, _sign) c_value16; \
944 swap = !scm_is_eq (endianness, scm_i_native_endianness); \
947 c_value16 = (INT_TYPE (16, _sign)) bswap_16 (c_value); \
949 c_value16 = c_value; \
951 memcpy (c_bv, &c_value16, 2); \
965 err = bytevector_large_set (c_bv, c_size, \
966 SIGNEDNESS (_sign), \
967 value, endianness); \
975 scm_out_of_range (FUNC_NAME, value); \
979 bytevector_signed_set (char *c_bv
, size_t c_size
,
980 SCM value
, SCM endianness
,
981 const char *func_name
)
982 #define FUNC_NAME func_name
984 GENERIC_INTEGER_SET (signed);
989 bytevector_unsigned_set (char *c_bv
, size_t c_size
,
990 SCM value
, SCM endianness
,
991 const char *func_name
)
992 #define FUNC_NAME func_name
994 GENERIC_INTEGER_SET (unsigned);
998 #undef GENERIC_INTEGER_SET
999 #undef GENERIC_INTEGER_REF
1002 SCM_DEFINE (scm_bytevector_uint_ref
, "bytevector-uint-ref", 4, 0, 0,
1003 (SCM bv
, SCM index
, SCM endianness
, SCM size
),
1004 "Return the @var{size}-octet long unsigned integer at index "
1005 "@var{index} in @var{bv}.")
1006 #define FUNC_NAME s_scm_bytevector_uint_ref
1008 GENERIC_INTEGER_ACCESSOR_PROLOGUE (unsigned);
1010 return (bytevector_unsigned_ref (&c_bv
[c_index
], c_size
, endianness
));
1014 SCM_DEFINE (scm_bytevector_sint_ref
, "bytevector-sint-ref", 4, 0, 0,
1015 (SCM bv
, SCM index
, SCM endianness
, SCM size
),
1016 "Return the @var{size}-octet long unsigned integer at index "
1017 "@var{index} in @var{bv}.")
1018 #define FUNC_NAME s_scm_bytevector_sint_ref
1020 GENERIC_INTEGER_ACCESSOR_PROLOGUE (signed);
1022 return (bytevector_signed_ref (&c_bv
[c_index
], c_size
, endianness
));
1026 SCM_DEFINE (scm_bytevector_uint_set_x
, "bytevector-uint-set!", 5, 0, 0,
1027 (SCM bv
, SCM index
, SCM value
, SCM endianness
, SCM size
),
1028 "Set the @var{size}-octet long unsigned integer at @var{index} "
1030 #define FUNC_NAME s_scm_bytevector_uint_set_x
1032 GENERIC_INTEGER_ACCESSOR_PROLOGUE (unsigned);
1034 bytevector_unsigned_set (&c_bv
[c_index
], c_size
, value
, endianness
,
1037 return SCM_UNSPECIFIED
;
1041 SCM_DEFINE (scm_bytevector_sint_set_x
, "bytevector-sint-set!", 5, 0, 0,
1042 (SCM bv
, SCM index
, SCM value
, SCM endianness
, SCM size
),
1043 "Set the @var{size}-octet long signed integer at @var{index} "
1045 #define FUNC_NAME s_scm_bytevector_sint_set_x
1047 GENERIC_INTEGER_ACCESSOR_PROLOGUE (signed);
1049 bytevector_signed_set (&c_bv
[c_index
], c_size
, value
, endianness
,
1052 return SCM_UNSPECIFIED
;
1058 /* Operations on integers of arbitrary size. */
1060 #define INTEGERS_TO_LIST(_sign) \
1062 size_t i, c_len, c_size; \
1064 SCM_VALIDATE_BYTEVECTOR (1, bv); \
1065 SCM_VALIDATE_SYMBOL (2, endianness); \
1066 c_size = scm_to_uint (size); \
1068 c_len = SCM_BYTEVECTOR_LENGTH (bv); \
1069 if (SCM_UNLIKELY (c_len == 0)) \
1071 else if (SCM_UNLIKELY (c_len < c_size)) \
1072 scm_out_of_range (FUNC_NAME, size); \
1077 c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
1079 lst = scm_make_list (scm_from_uint (c_len / c_size), \
1081 for (i = 0, pair = lst; \
1082 i <= c_len - c_size; \
1083 i += c_size, c_bv += c_size, pair = SCM_CDR (pair)) \
1086 bytevector_ ## _sign ## _ref (c_bv, c_size, \
1093 SCM_DEFINE (scm_bytevector_to_sint_list
, "bytevector->sint-list",
1095 (SCM bv
, SCM endianness
, SCM size
),
1096 "Return a list of signed integers of @var{size} octets "
1097 "representing the contents of @var{bv}.")
1098 #define FUNC_NAME s_scm_bytevector_to_sint_list
1100 INTEGERS_TO_LIST (signed);
1104 SCM_DEFINE (scm_bytevector_to_uint_list
, "bytevector->uint-list",
1106 (SCM bv
, SCM endianness
, SCM size
),
1107 "Return a list of unsigned integers of @var{size} octets "
1108 "representing the contents of @var{bv}.")
1109 #define FUNC_NAME s_scm_bytevector_to_uint_list
1111 INTEGERS_TO_LIST (unsigned);
1115 #undef INTEGER_TO_LIST
1118 #define INTEGER_LIST_TO_BYTEVECTOR(_sign) \
1122 char *c_bv, *c_bv_ptr; \
1124 SCM_VALIDATE_LIST_COPYLEN (1, lst, c_len); \
1125 SCM_VALIDATE_SYMBOL (2, endianness); \
1126 c_size = scm_to_uint (size); \
1128 if (SCM_UNLIKELY ((c_size == 0) || (c_size >= (ULONG_MAX >> 3L)))) \
1129 scm_out_of_range (FUNC_NAME, size); \
1131 bv = make_bytevector (c_len * c_size, SCM_ARRAY_ELEMENT_TYPE_VU8); \
1132 c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
1134 for (c_bv_ptr = c_bv; \
1135 !scm_is_null (lst); \
1136 lst = SCM_CDR (lst), c_bv_ptr += c_size) \
1138 bytevector_ ## _sign ## _set (c_bv_ptr, c_size, \
1139 SCM_CAR (lst), endianness, \
1146 SCM_DEFINE (scm_uint_list_to_bytevector
, "uint-list->bytevector",
1148 (SCM lst
, SCM endianness
, SCM size
),
1149 "Return a bytevector containing the unsigned integers "
1150 "listed in @var{lst} and encoded on @var{size} octets "
1151 "according to @var{endianness}.")
1152 #define FUNC_NAME s_scm_uint_list_to_bytevector
1154 INTEGER_LIST_TO_BYTEVECTOR (unsigned);
1158 SCM_DEFINE (scm_sint_list_to_bytevector
, "sint-list->bytevector",
1160 (SCM lst
, SCM endianness
, SCM size
),
1161 "Return a bytevector containing the signed integers "
1162 "listed in @var{lst} and encoded on @var{size} octets "
1163 "according to @var{endianness}.")
1164 #define FUNC_NAME s_scm_sint_list_to_bytevector
1166 INTEGER_LIST_TO_BYTEVECTOR (signed);
1170 #undef INTEGER_LIST_TO_BYTEVECTOR
1174 /* Operations on 16-bit integers. */
1176 SCM_DEFINE (scm_bytevector_u16_ref
, "bytevector-u16-ref",
1178 (SCM bv
, SCM index
, SCM endianness
),
1179 "Return the unsigned 16-bit integer from @var{bv} at "
1181 #define FUNC_NAME s_scm_bytevector_u16_ref
1183 INTEGER_REF (16, unsigned);
1187 SCM_DEFINE (scm_bytevector_s16_ref
, "bytevector-s16-ref",
1189 (SCM bv
, SCM index
, SCM endianness
),
1190 "Return the signed 16-bit integer from @var{bv} at "
1192 #define FUNC_NAME s_scm_bytevector_s16_ref
1194 INTEGER_REF (16, signed);
1198 SCM_DEFINE (scm_bytevector_u16_native_ref
, "bytevector-u16-native-ref",
1200 (SCM bv
, SCM index
),
1201 "Return the unsigned 16-bit integer from @var{bv} at "
1202 "@var{index} using the native endianness.")
1203 #define FUNC_NAME s_scm_bytevector_u16_native_ref
1205 INTEGER_NATIVE_REF (16, unsigned);
1209 SCM_DEFINE (scm_bytevector_s16_native_ref
, "bytevector-s16-native-ref",
1211 (SCM bv
, SCM index
),
1212 "Return the unsigned 16-bit integer from @var{bv} at "
1213 "@var{index} using the native endianness.")
1214 #define FUNC_NAME s_scm_bytevector_s16_native_ref
1216 INTEGER_NATIVE_REF (16, signed);
1220 SCM_DEFINE (scm_bytevector_u16_set_x
, "bytevector-u16-set!",
1222 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1223 "Store @var{value} in @var{bv} at @var{index} according to "
1224 "@var{endianness}.")
1225 #define FUNC_NAME s_scm_bytevector_u16_set_x
1227 INTEGER_SET (16, unsigned);
1231 SCM_DEFINE (scm_bytevector_s16_set_x
, "bytevector-s16-set!",
1233 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1234 "Store @var{value} in @var{bv} at @var{index} according to "
1235 "@var{endianness}.")
1236 #define FUNC_NAME s_scm_bytevector_s16_set_x
1238 INTEGER_SET (16, signed);
1242 SCM_DEFINE (scm_bytevector_u16_native_set_x
, "bytevector-u16-native-set!",
1244 (SCM bv
, SCM index
, SCM value
),
1245 "Store the unsigned integer @var{value} at index @var{index} "
1246 "of @var{bv} using the native endianness.")
1247 #define FUNC_NAME s_scm_bytevector_u16_native_set_x
1249 INTEGER_NATIVE_SET (16, unsigned);
1253 SCM_DEFINE (scm_bytevector_s16_native_set_x
, "bytevector-s16-native-set!",
1255 (SCM bv
, SCM index
, SCM value
),
1256 "Store the signed integer @var{value} at index @var{index} "
1257 "of @var{bv} using the native endianness.")
1258 #define FUNC_NAME s_scm_bytevector_s16_native_set_x
1260 INTEGER_NATIVE_SET (16, signed);
1266 /* Operations on 32-bit integers. */
1268 /* Unfortunately, on 32-bit machines `SCM' is not large enough to hold
1269 arbitrary 32-bit integers. Thus we fall back to using the
1270 `large_{ref,set}' variants on 32-bit machines. */
1272 #define LARGE_INTEGER_REF(_len, _sign) \
1273 INTEGER_ACCESSOR_PROLOGUE(_len, _sign); \
1274 SCM_VALIDATE_SYMBOL (3, endianness); \
1276 return (bytevector_large_ref ((char *) c_bv + c_index, _len / 8, \
1277 SIGNEDNESS (_sign), endianness));
1279 #define LARGE_INTEGER_SET(_len, _sign) \
1281 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
1282 SCM_VALIDATE_SYMBOL (4, endianness); \
1284 err = bytevector_large_set ((char *) c_bv + c_index, _len / 8, \
1285 SIGNEDNESS (_sign), value, endianness); \
1286 if (SCM_UNLIKELY (err)) \
1287 scm_out_of_range (FUNC_NAME, value); \
1289 return SCM_UNSPECIFIED;
1291 #define LARGE_INTEGER_NATIVE_REF(_len, _sign) \
1292 INTEGER_ACCESSOR_PROLOGUE(_len, _sign); \
1293 return (bytevector_large_ref ((char *) c_bv + c_index, _len / 8, \
1294 SIGNEDNESS (_sign), scm_i_native_endianness));
1296 #define LARGE_INTEGER_NATIVE_SET(_len, _sign) \
1298 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
1300 err = bytevector_large_set ((char *) c_bv + c_index, _len / 8, \
1301 SIGNEDNESS (_sign), value, \
1302 scm_i_native_endianness); \
1303 if (SCM_UNLIKELY (err)) \
1304 scm_out_of_range (FUNC_NAME, value); \
1306 return SCM_UNSPECIFIED;
1309 SCM_DEFINE (scm_bytevector_u32_ref
, "bytevector-u32-ref",
1311 (SCM bv
, SCM index
, SCM endianness
),
1312 "Return the unsigned 32-bit integer from @var{bv} at "
1314 #define FUNC_NAME s_scm_bytevector_u32_ref
1316 #if SIZEOF_VOID_P > 4
1317 INTEGER_REF (32, unsigned);
1319 LARGE_INTEGER_REF (32, unsigned);
1324 SCM_DEFINE (scm_bytevector_s32_ref
, "bytevector-s32-ref",
1326 (SCM bv
, SCM index
, SCM endianness
),
1327 "Return the signed 32-bit integer from @var{bv} at "
1329 #define FUNC_NAME s_scm_bytevector_s32_ref
1331 #if SIZEOF_VOID_P > 4
1332 INTEGER_REF (32, signed);
1334 LARGE_INTEGER_REF (32, signed);
1339 SCM_DEFINE (scm_bytevector_u32_native_ref
, "bytevector-u32-native-ref",
1341 (SCM bv
, SCM index
),
1342 "Return the unsigned 32-bit integer from @var{bv} at "
1343 "@var{index} using the native endianness.")
1344 #define FUNC_NAME s_scm_bytevector_u32_native_ref
1346 #if SIZEOF_VOID_P > 4
1347 INTEGER_NATIVE_REF (32, unsigned);
1349 LARGE_INTEGER_NATIVE_REF (32, unsigned);
1354 SCM_DEFINE (scm_bytevector_s32_native_ref
, "bytevector-s32-native-ref",
1356 (SCM bv
, SCM index
),
1357 "Return the unsigned 32-bit integer from @var{bv} at "
1358 "@var{index} using the native endianness.")
1359 #define FUNC_NAME s_scm_bytevector_s32_native_ref
1361 #if SIZEOF_VOID_P > 4
1362 INTEGER_NATIVE_REF (32, signed);
1364 LARGE_INTEGER_NATIVE_REF (32, signed);
1369 SCM_DEFINE (scm_bytevector_u32_set_x
, "bytevector-u32-set!",
1371 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1372 "Store @var{value} in @var{bv} at @var{index} according to "
1373 "@var{endianness}.")
1374 #define FUNC_NAME s_scm_bytevector_u32_set_x
1376 #if SIZEOF_VOID_P > 4
1377 INTEGER_SET (32, unsigned);
1379 LARGE_INTEGER_SET (32, unsigned);
1384 SCM_DEFINE (scm_bytevector_s32_set_x
, "bytevector-s32-set!",
1386 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1387 "Store @var{value} in @var{bv} at @var{index} according to "
1388 "@var{endianness}.")
1389 #define FUNC_NAME s_scm_bytevector_s32_set_x
1391 #if SIZEOF_VOID_P > 4
1392 INTEGER_SET (32, signed);
1394 LARGE_INTEGER_SET (32, signed);
1399 SCM_DEFINE (scm_bytevector_u32_native_set_x
, "bytevector-u32-native-set!",
1401 (SCM bv
, SCM index
, SCM value
),
1402 "Store the unsigned integer @var{value} at index @var{index} "
1403 "of @var{bv} using the native endianness.")
1404 #define FUNC_NAME s_scm_bytevector_u32_native_set_x
1406 #if SIZEOF_VOID_P > 4
1407 INTEGER_NATIVE_SET (32, unsigned);
1409 LARGE_INTEGER_NATIVE_SET (32, unsigned);
1414 SCM_DEFINE (scm_bytevector_s32_native_set_x
, "bytevector-s32-native-set!",
1416 (SCM bv
, SCM index
, SCM value
),
1417 "Store the signed integer @var{value} at index @var{index} "
1418 "of @var{bv} using the native endianness.")
1419 #define FUNC_NAME s_scm_bytevector_s32_native_set_x
1421 #if SIZEOF_VOID_P > 4
1422 INTEGER_NATIVE_SET (32, signed);
1424 LARGE_INTEGER_NATIVE_SET (32, signed);
1431 /* Operations on 64-bit integers. */
1433 /* For 64-bit integers, we use only the `large_{ref,set}' variant. */
1435 SCM_DEFINE (scm_bytevector_u64_ref
, "bytevector-u64-ref",
1437 (SCM bv
, SCM index
, SCM endianness
),
1438 "Return the unsigned 64-bit integer from @var{bv} at "
1440 #define FUNC_NAME s_scm_bytevector_u64_ref
1442 LARGE_INTEGER_REF (64, unsigned);
1446 SCM_DEFINE (scm_bytevector_s64_ref
, "bytevector-s64-ref",
1448 (SCM bv
, SCM index
, SCM endianness
),
1449 "Return the signed 64-bit integer from @var{bv} at "
1451 #define FUNC_NAME s_scm_bytevector_s64_ref
1453 LARGE_INTEGER_REF (64, signed);
1457 SCM_DEFINE (scm_bytevector_u64_native_ref
, "bytevector-u64-native-ref",
1459 (SCM bv
, SCM index
),
1460 "Return the unsigned 64-bit integer from @var{bv} at "
1461 "@var{index} using the native endianness.")
1462 #define FUNC_NAME s_scm_bytevector_u64_native_ref
1464 LARGE_INTEGER_NATIVE_REF (64, unsigned);
1468 SCM_DEFINE (scm_bytevector_s64_native_ref
, "bytevector-s64-native-ref",
1470 (SCM bv
, SCM index
),
1471 "Return the unsigned 64-bit integer from @var{bv} at "
1472 "@var{index} using the native endianness.")
1473 #define FUNC_NAME s_scm_bytevector_s64_native_ref
1475 LARGE_INTEGER_NATIVE_REF (64, signed);
1479 SCM_DEFINE (scm_bytevector_u64_set_x
, "bytevector-u64-set!",
1481 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1482 "Store @var{value} in @var{bv} at @var{index} according to "
1483 "@var{endianness}.")
1484 #define FUNC_NAME s_scm_bytevector_u64_set_x
1486 LARGE_INTEGER_SET (64, unsigned);
1490 SCM_DEFINE (scm_bytevector_s64_set_x
, "bytevector-s64-set!",
1492 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1493 "Store @var{value} in @var{bv} at @var{index} according to "
1494 "@var{endianness}.")
1495 #define FUNC_NAME s_scm_bytevector_s64_set_x
1497 LARGE_INTEGER_SET (64, signed);
1501 SCM_DEFINE (scm_bytevector_u64_native_set_x
, "bytevector-u64-native-set!",
1503 (SCM bv
, SCM index
, SCM value
),
1504 "Store the unsigned integer @var{value} at index @var{index} "
1505 "of @var{bv} using the native endianness.")
1506 #define FUNC_NAME s_scm_bytevector_u64_native_set_x
1508 LARGE_INTEGER_NATIVE_SET (64, unsigned);
1512 SCM_DEFINE (scm_bytevector_s64_native_set_x
, "bytevector-s64-native-set!",
1514 (SCM bv
, SCM index
, SCM value
),
1515 "Store the signed integer @var{value} at index @var{index} "
1516 "of @var{bv} using the native endianness.")
1517 #define FUNC_NAME s_scm_bytevector_s64_native_set_x
1519 LARGE_INTEGER_NATIVE_SET (64, signed);
1525 /* Operations on IEEE-754 numbers. */
1527 /* There are two possible word endians, visible in glibc's <ieee754.h>.
1528 However, in R6RS, when the endianness is `little', little endian is
1529 assumed for both the byte order and the word order. This is clear from
1530 Section 2.1 of R6RS-lib (in response to
1531 http://www.r6rs.org/formal-comments/comment-187.txt). */
1534 /* Convert to/from a floating-point number with different endianness. This
1535 method is probably not the most efficient but it should be portable. */
1538 float_to_foreign_endianness (union scm_ieee754_float
*target
,
1541 union scm_ieee754_float src
;
1545 #ifdef WORDS_BIGENDIAN
1546 /* Assuming little endian for both byte and word order. */
1547 target
->little_endian
.negative
= src
.big_endian
.negative
;
1548 target
->little_endian
.exponent
= src
.big_endian
.exponent
;
1549 target
->little_endian
.mantissa
= src
.big_endian
.mantissa
;
1551 target
->big_endian
.negative
= src
.little_endian
.negative
;
1552 target
->big_endian
.exponent
= src
.little_endian
.exponent
;
1553 target
->big_endian
.mantissa
= src
.little_endian
.mantissa
;
1558 float_from_foreign_endianness (const union scm_ieee754_float
*source
)
1560 union scm_ieee754_float result
;
1562 #ifdef WORDS_BIGENDIAN
1563 /* Assuming little endian for both byte and word order. */
1564 result
.big_endian
.negative
= source
->little_endian
.negative
;
1565 result
.big_endian
.exponent
= source
->little_endian
.exponent
;
1566 result
.big_endian
.mantissa
= source
->little_endian
.mantissa
;
1568 result
.little_endian
.negative
= source
->big_endian
.negative
;
1569 result
.little_endian
.exponent
= source
->big_endian
.exponent
;
1570 result
.little_endian
.mantissa
= source
->big_endian
.mantissa
;
1577 double_to_foreign_endianness (union scm_ieee754_double
*target
,
1580 union scm_ieee754_double src
;
1584 #ifdef WORDS_BIGENDIAN
1585 /* Assuming little endian for both byte and word order. */
1586 target
->little_little_endian
.negative
= src
.big_endian
.negative
;
1587 target
->little_little_endian
.exponent
= src
.big_endian
.exponent
;
1588 target
->little_little_endian
.mantissa0
= src
.big_endian
.mantissa0
;
1589 target
->little_little_endian
.mantissa1
= src
.big_endian
.mantissa1
;
1591 target
->big_endian
.negative
= src
.little_little_endian
.negative
;
1592 target
->big_endian
.exponent
= src
.little_little_endian
.exponent
;
1593 target
->big_endian
.mantissa0
= src
.little_little_endian
.mantissa0
;
1594 target
->big_endian
.mantissa1
= src
.little_little_endian
.mantissa1
;
1598 static inline double
1599 double_from_foreign_endianness (const union scm_ieee754_double
*source
)
1601 union scm_ieee754_double result
;
1603 #ifdef WORDS_BIGENDIAN
1604 /* Assuming little endian for both byte and word order. */
1605 result
.big_endian
.negative
= source
->little_little_endian
.negative
;
1606 result
.big_endian
.exponent
= source
->little_little_endian
.exponent
;
1607 result
.big_endian
.mantissa0
= source
->little_little_endian
.mantissa0
;
1608 result
.big_endian
.mantissa1
= source
->little_little_endian
.mantissa1
;
1610 result
.little_little_endian
.negative
= source
->big_endian
.negative
;
1611 result
.little_little_endian
.exponent
= source
->big_endian
.exponent
;
1612 result
.little_little_endian
.mantissa0
= source
->big_endian
.mantissa0
;
1613 result
.little_little_endian
.mantissa1
= source
->big_endian
.mantissa1
;
1619 /* Template macros to abstract over doubles and floats.
1620 XXX: Guile can only convert to/from doubles. */
1621 #define IEEE754_UNION(_c_type) union scm_ieee754_ ## _c_type
1622 #define IEEE754_TO_SCM(_c_type) scm_from_double
1623 #define IEEE754_FROM_SCM(_c_type) scm_to_double
1624 #define IEEE754_FROM_FOREIGN_ENDIANNESS(_c_type) \
1625 _c_type ## _from_foreign_endianness
1626 #define IEEE754_TO_FOREIGN_ENDIANNESS(_c_type) \
1627 _c_type ## _to_foreign_endianness
1630 /* FIXME: SCM_VALIDATE_REAL rejects integers, etc. grrr */
1631 #define VALIDATE_REAL(pos, v) \
1633 SCM_ASSERT_TYPE (scm_is_true (scm_rational_p (v)), v, pos, FUNC_NAME, "real"); \
1636 /* Templace getters and setters. */
1638 #define IEEE754_ACCESSOR_PROLOGUE(_type) \
1639 INTEGER_ACCESSOR_PROLOGUE (sizeof (_type) << 3UL, signed);
1641 #define IEEE754_REF(_type) \
1644 IEEE754_ACCESSOR_PROLOGUE (_type); \
1645 SCM_VALIDATE_SYMBOL (3, endianness); \
1647 if (scm_is_eq (endianness, scm_i_native_endianness)) \
1648 memcpy (&c_result, &c_bv[c_index], sizeof (c_result)); \
1651 IEEE754_UNION (_type) c_raw; \
1653 memcpy (&c_raw, &c_bv[c_index], sizeof (c_raw)); \
1655 IEEE754_FROM_FOREIGN_ENDIANNESS (_type) (&c_raw); \
1658 return (IEEE754_TO_SCM (_type) (c_result));
1660 #define IEEE754_NATIVE_REF(_type) \
1663 IEEE754_ACCESSOR_PROLOGUE (_type); \
1665 memcpy (&c_result, &c_bv[c_index], sizeof (c_result)); \
1666 return (IEEE754_TO_SCM (_type) (c_result));
1668 #define IEEE754_SET(_type) \
1671 IEEE754_ACCESSOR_PROLOGUE (_type); \
1672 VALIDATE_REAL (3, value); \
1673 SCM_VALIDATE_SYMBOL (4, endianness); \
1674 c_value = IEEE754_FROM_SCM (_type) (value); \
1676 if (scm_is_eq (endianness, scm_i_native_endianness)) \
1677 memcpy (&c_bv[c_index], &c_value, sizeof (c_value)); \
1680 IEEE754_UNION (_type) c_raw; \
1682 IEEE754_TO_FOREIGN_ENDIANNESS (_type) (&c_raw, c_value); \
1683 memcpy (&c_bv[c_index], &c_raw, sizeof (c_raw)); \
1686 return SCM_UNSPECIFIED;
1688 #define IEEE754_NATIVE_SET(_type) \
1691 IEEE754_ACCESSOR_PROLOGUE (_type); \
1692 VALIDATE_REAL (3, value); \
1693 c_value = IEEE754_FROM_SCM (_type) (value); \
1695 memcpy (&c_bv[c_index], &c_value, sizeof (c_value)); \
1696 return SCM_UNSPECIFIED;
1699 /* Single precision. */
1701 SCM_DEFINE (scm_bytevector_ieee_single_ref
,
1702 "bytevector-ieee-single-ref",
1704 (SCM bv
, SCM index
, SCM endianness
),
1705 "Return the IEEE-754 single from @var{bv} at "
1707 #define FUNC_NAME s_scm_bytevector_ieee_single_ref
1709 IEEE754_REF (float);
1713 SCM_DEFINE (scm_bytevector_ieee_single_native_ref
,
1714 "bytevector-ieee-single-native-ref",
1716 (SCM bv
, SCM index
),
1717 "Return the IEEE-754 single from @var{bv} at "
1718 "@var{index} using the native endianness.")
1719 #define FUNC_NAME s_scm_bytevector_ieee_single_native_ref
1721 IEEE754_NATIVE_REF (float);
1725 SCM_DEFINE (scm_bytevector_ieee_single_set_x
,
1726 "bytevector-ieee-single-set!",
1728 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1729 "Store real @var{value} in @var{bv} at @var{index} according to "
1730 "@var{endianness}.")
1731 #define FUNC_NAME s_scm_bytevector_ieee_single_set_x
1733 IEEE754_SET (float);
1737 SCM_DEFINE (scm_bytevector_ieee_single_native_set_x
,
1738 "bytevector-ieee-single-native-set!",
1740 (SCM bv
, SCM index
, SCM value
),
1741 "Store the real @var{value} at index @var{index} "
1742 "of @var{bv} using the native endianness.")
1743 #define FUNC_NAME s_scm_bytevector_ieee_single_native_set_x
1745 IEEE754_NATIVE_SET (float);
1750 /* Double precision. */
1752 SCM_DEFINE (scm_bytevector_ieee_double_ref
,
1753 "bytevector-ieee-double-ref",
1755 (SCM bv
, SCM index
, SCM endianness
),
1756 "Return the IEEE-754 double from @var{bv} at "
1758 #define FUNC_NAME s_scm_bytevector_ieee_double_ref
1760 IEEE754_REF (double);
1764 SCM_DEFINE (scm_bytevector_ieee_double_native_ref
,
1765 "bytevector-ieee-double-native-ref",
1767 (SCM bv
, SCM index
),
1768 "Return the IEEE-754 double from @var{bv} at "
1769 "@var{index} using the native endianness.")
1770 #define FUNC_NAME s_scm_bytevector_ieee_double_native_ref
1772 IEEE754_NATIVE_REF (double);
1776 SCM_DEFINE (scm_bytevector_ieee_double_set_x
,
1777 "bytevector-ieee-double-set!",
1779 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1780 "Store real @var{value} in @var{bv} at @var{index} according to "
1781 "@var{endianness}.")
1782 #define FUNC_NAME s_scm_bytevector_ieee_double_set_x
1784 IEEE754_SET (double);
1788 SCM_DEFINE (scm_bytevector_ieee_double_native_set_x
,
1789 "bytevector-ieee-double-native-set!",
1791 (SCM bv
, SCM index
, SCM value
),
1792 "Store the real @var{value} at index @var{index} "
1793 "of @var{bv} using the native endianness.")
1794 #define FUNC_NAME s_scm_bytevector_ieee_double_native_set_x
1796 IEEE754_NATIVE_SET (double);
1801 #undef IEEE754_UNION
1802 #undef IEEE754_TO_SCM
1803 #undef IEEE754_FROM_SCM
1804 #undef IEEE754_FROM_FOREIGN_ENDIANNESS
1805 #undef IEEE754_TO_FOREIGN_ENDIANNESS
1807 #undef IEEE754_NATIVE_REF
1809 #undef IEEE754_NATIVE_SET
1812 /* Operations on strings. */
1815 /* Produce a function that returns the length of a UTF-encoded string. */
1816 #define UTF_STRLEN_FUNCTION(_utf_width) \
1817 static inline size_t \
1818 utf ## _utf_width ## _strlen (const uint ## _utf_width ## _t *str) \
1821 const uint ## _utf_width ## _t *ptr; \
1829 return (len * ((_utf_width) / 8)); \
1832 UTF_STRLEN_FUNCTION (8)
1835 /* Return the length (in bytes) of STR, a UTF-(UTF_WIDTH) encoded string. */
1836 #define UTF_STRLEN(_utf_width, _str) \
1837 utf ## _utf_width ## _strlen (_str)
1839 /* Return the "portable" name of the UTF encoding of size UTF_WIDTH and
1840 ENDIANNESS (Gnulib's `iconv_open' module guarantees the portability of the
1843 utf_encoding_name (char *name
, size_t utf_width
, SCM endianness
)
1845 strcpy (name
, "UTF-");
1846 strcat (name
, ((utf_width
== 8)
1848 : ((utf_width
== 16)
1850 : ((utf_width
== 32)
1854 ((scm_is_eq (endianness
, scm_sym_big
))
1856 : ((scm_is_eq (endianness
, scm_sym_little
))
1861 /* Maximum length of a UTF encoding name. */
1862 #define MAX_UTF_ENCODING_NAME_LEN 16
1864 /* Produce the body of a `string->utf' function. */
1865 #define STRING_TO_UTF(_utf_width) \
1869 char c_utf_name[MAX_UTF_ENCODING_NAME_LEN]; \
1870 char *c_utf = NULL, *c_locale; \
1871 size_t c_strlen, c_raw_strlen, c_utf_len = 0; \
1873 SCM_VALIDATE_STRING (1, str); \
1874 if (endianness == SCM_UNDEFINED) \
1875 endianness = scm_sym_big; \
1877 SCM_VALIDATE_SYMBOL (2, endianness); \
1879 c_strlen = scm_c_string_length (str); \
1880 c_raw_strlen = c_strlen * ((_utf_width) / 8); \
1883 c_str = (char *) alloca (c_raw_strlen + 1); \
1884 c_raw_strlen = scm_to_locale_stringbuf (str, c_str, c_strlen); \
1886 while (c_raw_strlen > c_strlen); \
1887 c_str[c_raw_strlen] = '\0'; \
1889 utf_encoding_name (c_utf_name, (_utf_width), endianness); \
1891 c_locale = (char *) alloca (strlen (locale_charset ()) + 1); \
1892 strcpy (c_locale, locale_charset ()); \
1894 err = mem_iconveh (c_str, c_raw_strlen, \
1895 c_locale, c_utf_name, \
1896 iconveh_question_mark, NULL, \
1897 &c_utf, &c_utf_len); \
1898 if (SCM_UNLIKELY (err)) \
1899 scm_syserror_msg (FUNC_NAME, "failed to convert string: ~A", \
1900 scm_list_1 (str), err); \
1903 /* C_UTF is null-terminated. It is malloc(3)-allocated, so we cannot \
1904 use `scm_c_take_bytevector ()'. */ \
1905 scm_dynwind_begin (0); \
1906 scm_dynwind_free (c_utf); \
1908 utf = make_bytevector (c_utf_len, \
1909 SCM_ARRAY_ELEMENT_TYPE_VU8); \
1910 memcpy (SCM_BYTEVECTOR_CONTENTS (utf), c_utf, \
1913 scm_dynwind_end (); \
1920 SCM_DEFINE (scm_string_to_utf8
, "string->utf8",
1923 "Return a newly allocated bytevector that contains the UTF-8 "
1924 "encoding of @var{str}.")
1925 #define FUNC_NAME s_scm_string_to_utf8
1930 size_t c_strlen
, c_raw_strlen
;
1932 SCM_VALIDATE_STRING (1, str
);
1934 c_strlen
= scm_c_string_length (str
);
1935 c_raw_strlen
= c_strlen
;
1938 c_str
= (char *) alloca (c_raw_strlen
+ 1);
1939 c_raw_strlen
= scm_to_locale_stringbuf (str
, c_str
, c_strlen
);
1941 while (c_raw_strlen
> c_strlen
);
1942 c_str
[c_raw_strlen
] = '\0';
1944 c_utf
= u8_strconv_from_locale (c_str
);
1945 if (SCM_UNLIKELY (c_utf
== NULL
))
1946 scm_syserror (FUNC_NAME
);
1949 /* C_UTF is null-terminated. It is malloc(3)-allocated, so we cannot
1950 use `scm_c_take_bytevector ()'. */
1951 scm_dynwind_begin (0);
1952 scm_dynwind_free (c_utf
);
1954 utf
= make_bytevector (UTF_STRLEN (8, c_utf
),
1955 SCM_ARRAY_ELEMENT_TYPE_VU8
);
1956 memcpy (SCM_BYTEVECTOR_CONTENTS (utf
), c_utf
,
1957 UTF_STRLEN (8, c_utf
));
1966 SCM_DEFINE (scm_string_to_utf16
, "string->utf16",
1968 (SCM str
, SCM endianness
),
1969 "Return a newly allocated bytevector that contains the UTF-16 "
1970 "encoding of @var{str}.")
1971 #define FUNC_NAME s_scm_string_to_utf16
1977 SCM_DEFINE (scm_string_to_utf32
, "string->utf32",
1979 (SCM str
, SCM endianness
),
1980 "Return a newly allocated bytevector that contains the UTF-32 "
1981 "encoding of @var{str}.")
1982 #define FUNC_NAME s_scm_string_to_utf32
1989 /* Produce the body of a function that converts a UTF-encoded bytevector to a
1991 #define UTF_TO_STRING(_utf_width) \
1992 SCM str = SCM_BOOL_F; \
1994 char *c_str = NULL, *c_locale; \
1995 char c_utf_name[MAX_UTF_ENCODING_NAME_LEN]; \
1996 const char *c_utf; \
1997 size_t c_strlen = 0, c_utf_len; \
1999 SCM_VALIDATE_BYTEVECTOR (1, utf); \
2000 if (endianness == SCM_UNDEFINED) \
2001 endianness = scm_sym_big; \
2003 SCM_VALIDATE_SYMBOL (2, endianness); \
2005 c_utf_len = SCM_BYTEVECTOR_LENGTH (utf); \
2006 c_utf = (char *) SCM_BYTEVECTOR_CONTENTS (utf); \
2007 utf_encoding_name (c_utf_name, (_utf_width), endianness); \
2009 c_locale = (char *) alloca (strlen (locale_charset ()) + 1); \
2010 strcpy (c_locale, locale_charset ()); \
2012 err = mem_iconveh (c_utf, c_utf_len, \
2013 c_utf_name, c_locale, \
2014 iconveh_question_mark, NULL, \
2015 &c_str, &c_strlen); \
2016 if (SCM_UNLIKELY (err)) \
2017 scm_syserror_msg (FUNC_NAME, "failed to convert to string: ~A", \
2018 scm_list_1 (utf), err); \
2020 /* C_STR is null-terminated. */ \
2021 str = scm_take_locale_stringn (c_str, c_strlen); \
2026 SCM_DEFINE (scm_utf8_to_string
, "utf8->string",
2029 "Return a newly allocate string that contains from the UTF-8-"
2030 "encoded contents of bytevector @var{utf}.")
2031 #define FUNC_NAME s_scm_utf8_to_string
2035 char *c_str
= NULL
, *c_locale
;
2037 size_t c_utf_len
, c_strlen
= 0;
2039 SCM_VALIDATE_BYTEVECTOR (1, utf
);
2041 c_utf_len
= SCM_BYTEVECTOR_LENGTH (utf
);
2043 c_locale
= (char *) alloca (strlen (locale_charset ()) + 1);
2044 strcpy (c_locale
, locale_charset ());
2046 c_utf
= (char *) SCM_BYTEVECTOR_CONTENTS (utf
);
2047 err
= mem_iconveh (c_utf
, c_utf_len
,
2049 iconveh_question_mark
, NULL
,
2051 if (SCM_UNLIKELY (err
))
2052 scm_syserror_msg (FUNC_NAME
, "failed to convert to string: ~A",
2053 scm_list_1 (utf
), err
);
2055 /* C_STR is null-terminated. */
2056 str
= scm_take_locale_stringn (c_str
, c_strlen
);
2062 SCM_DEFINE (scm_utf16_to_string
, "utf16->string",
2064 (SCM utf
, SCM endianness
),
2065 "Return a newly allocate string that contains from the UTF-16-"
2066 "encoded contents of bytevector @var{utf}.")
2067 #define FUNC_NAME s_scm_utf16_to_string
2073 SCM_DEFINE (scm_utf32_to_string
, "utf32->string",
2075 (SCM utf
, SCM endianness
),
2076 "Return a newly allocate string that contains from the UTF-32-"
2077 "encoded contents of bytevector @var{utf}.")
2078 #define FUNC_NAME s_scm_utf32_to_string
2086 /* Bytevectors as generalized vectors & arrays. */
2090 bytevector_ref_c32 (SCM bv
, SCM idx
)
2091 { /* FIXME add some checks */
2092 const float *contents
= (const float*)SCM_BYTEVECTOR_CONTENTS (bv
);
2093 size_t i
= scm_to_size_t (idx
);
2094 return scm_c_make_rectangular (contents
[i
/8], contents
[i
/8 + 1]);
2098 bytevector_ref_c64 (SCM bv
, SCM idx
)
2099 { /* FIXME add some checks */
2100 const double *contents
= (const double*)SCM_BYTEVECTOR_CONTENTS (bv
);
2101 size_t i
= scm_to_size_t (idx
);
2102 return scm_c_make_rectangular (contents
[i
/16], contents
[i
/16 + 1]);
2105 typedef SCM (*scm_t_bytevector_ref_fn
)(SCM
, SCM
);
2107 const scm_t_bytevector_ref_fn bytevector_ref_fns
[SCM_ARRAY_ELEMENT_TYPE_LAST
+ 1] =
2112 scm_bytevector_u8_ref
, /* VU8 */
2113 scm_bytevector_u8_ref
, /* U8 */
2114 scm_bytevector_s8_ref
,
2115 scm_bytevector_u16_native_ref
,
2116 scm_bytevector_s16_native_ref
,
2117 scm_bytevector_u32_native_ref
,
2118 scm_bytevector_s32_native_ref
,
2119 scm_bytevector_u64_native_ref
,
2120 scm_bytevector_s64_native_ref
,
2121 scm_bytevector_ieee_single_native_ref
,
2122 scm_bytevector_ieee_double_native_ref
,
2128 bv_handle_ref (scm_t_array_handle
*h
, size_t index
)
2131 scm_t_bytevector_ref_fn ref_fn
;
2133 ref_fn
= bytevector_ref_fns
[h
->element_type
];
2135 scm_from_size_t (index
* scm_array_handle_uniform_element_size (h
));
2136 return ref_fn (h
->array
, byte_index
);
2140 bytevector_set_c32 (SCM bv
, SCM idx
, SCM val
)
2141 { /* checks are unnecessary here */
2142 float *contents
= (float*)SCM_BYTEVECTOR_CONTENTS (bv
);
2143 size_t i
= scm_to_size_t (idx
);
2144 contents
[i
/8] = scm_c_real_part (val
);
2145 contents
[i
/8 + 1] = scm_c_imag_part (val
);
2146 return SCM_UNSPECIFIED
;
2150 bytevector_set_c64 (SCM bv
, SCM idx
, SCM val
)
2151 { /* checks are unnecessary here */
2152 double *contents
= (double*)SCM_BYTEVECTOR_CONTENTS (bv
);
2153 size_t i
= scm_to_size_t (idx
);
2154 contents
[i
/16] = scm_c_real_part (val
);
2155 contents
[i
/16 + 1] = scm_c_imag_part (val
);
2156 return SCM_UNSPECIFIED
;
2159 typedef SCM (*scm_t_bytevector_set_fn
)(SCM
, SCM
, SCM
);
2161 const scm_t_bytevector_set_fn bytevector_set_fns
[SCM_ARRAY_ELEMENT_TYPE_LAST
+ 1] =
2166 scm_bytevector_u8_set_x
, /* VU8 */
2167 scm_bytevector_u8_set_x
, /* U8 */
2168 scm_bytevector_s8_set_x
,
2169 scm_bytevector_u16_native_set_x
,
2170 scm_bytevector_s16_native_set_x
,
2171 scm_bytevector_u32_native_set_x
,
2172 scm_bytevector_s32_native_set_x
,
2173 scm_bytevector_u64_native_set_x
,
2174 scm_bytevector_s64_native_set_x
,
2175 scm_bytevector_ieee_single_native_set_x
,
2176 scm_bytevector_ieee_double_native_set_x
,
2182 bv_handle_set_x (scm_t_array_handle
*h
, size_t index
, SCM val
)
2185 scm_t_bytevector_set_fn set_fn
;
2187 set_fn
= bytevector_set_fns
[h
->element_type
];
2189 scm_from_size_t (index
* scm_array_handle_uniform_element_size (h
));
2190 set_fn (h
->array
, byte_index
, val
);
2194 bytevector_get_handle (SCM v
, scm_t_array_handle
*h
)
2200 h
->dim0
.ubnd
= SCM_BYTEVECTOR_TYPED_LENGTH (v
) - 1;
2202 h
->element_type
= SCM_BYTEVECTOR_ELEMENT_TYPE (v
);
2203 h
->elements
= h
->writable_elements
= SCM_BYTEVECTOR_CONTENTS (v
);
2207 /* Initialization. */
2210 scm_bootstrap_bytevectors (void)
2212 /* This must be instantiated here because the generalized-vector API may
2213 want to access bytevectors even though `(rnrs bytevector)' hasn't been
2215 scm_null_bytevector
=
2216 scm_gc_protect_object (make_bytevector (0, SCM_ARRAY_ELEMENT_TYPE_VU8
));
2218 #ifdef WORDS_BIGENDIAN
2219 scm_i_native_endianness
= scm_permanent_object (scm_from_locale_symbol ("big"));
2221 scm_i_native_endianness
= scm_permanent_object (scm_from_locale_symbol ("little"));
2224 scm_c_register_extension ("libguile", "scm_init_bytevectors",
2225 (scm_t_extension_init_func
) scm_init_bytevectors
,
2229 scm_t_array_implementation impl
;
2231 impl
.tag
= scm_tc7_bytevector
;
2233 impl
.vref
= bv_handle_ref
;
2234 impl
.vset
= bv_handle_set_x
;
2235 impl
.get_handle
= bytevector_get_handle
;
2236 scm_i_register_array_implementation (&impl
);
2237 scm_i_register_vector_constructor
2238 (scm_i_array_element_types
[SCM_ARRAY_ELEMENT_TYPE_VU8
],
2239 scm_make_bytevector
);
2244 scm_init_bytevectors (void)
2246 #include "libguile/bytevectors.x"
2248 scm_endianness_big
= scm_sym_big
;
2249 scm_endianness_little
= scm_sym_little
;