1 /* Copyright (C) 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
29 #include "libguile/_scm.h"
30 #include "libguile/extensions.h"
31 #include "libguile/bytevectors.h"
32 #include "libguile/strings.h"
33 #include "libguile/validate.h"
34 #include "libguile/arrays.h"
35 #include "libguile/array-handle.h"
36 #include "libguile/uniform.h"
37 #include "libguile/srfi-4.h"
40 #include <striconveh.h>
47 /* Assuming 32-bit longs. */
48 # define ULONG_MAX 4294967295UL
57 /* Convenience macros. These are used by the various templates (macros) that
58 are parameterized by integer signedness. */
59 #define INT8_T_signed scm_t_int8
60 #define INT8_T_unsigned scm_t_uint8
61 #define INT16_T_signed scm_t_int16
62 #define INT16_T_unsigned scm_t_uint16
63 #define INT32_T_signed scm_t_int32
64 #define INT32_T_unsigned scm_t_uint32
65 #define is_signed_int8(_x) (((_x) >= -128L) && ((_x) <= 127L))
66 #define is_unsigned_int8(_x) ((_x) <= 255UL)
67 #define is_signed_int16(_x) (((_x) >= -32768L) && ((_x) <= 32767L))
68 #define is_unsigned_int16(_x) ((_x) <= 65535UL)
69 #define is_signed_int32(_x) (((_x) >= -2147483648L) && ((_x) <= 2147483647L))
70 #define is_unsigned_int32(_x) ((_x) <= 4294967295UL)
71 #define SIGNEDNESS_signed 1
72 #define SIGNEDNESS_unsigned 0
74 #define INT_TYPE(_size, _sign) INT ## _size ## _T_ ## _sign
75 #define INT_SWAP(_size) bswap_ ## _size
76 #define INT_VALID_P(_size, _sign) is_ ## _sign ## _int ## _size
77 #define SIGNEDNESS(_sign) SIGNEDNESS_ ## _sign
80 #define INTEGER_ACCESSOR_PROLOGUE(_len, _sign) \
81 size_t c_len, c_index; \
84 SCM_VALIDATE_BYTEVECTOR (1, bv); \
85 c_index = scm_to_uint (index); \
87 c_len = SCM_BYTEVECTOR_LENGTH (bv); \
88 c_bv = (_sign char *) SCM_BYTEVECTOR_CONTENTS (bv); \
90 if (SCM_UNLIKELY (c_index + ((_len) >> 3UL) - 1 >= c_len)) \
91 scm_out_of_range (FUNC_NAME, index);
93 /* Template for fixed-size integer access (only 8, 16 or 32-bit). */
94 #define INTEGER_REF(_len, _sign) \
97 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
98 SCM_VALIDATE_SYMBOL (3, endianness); \
101 INT_TYPE (_len, _sign) c_result; \
103 memcpy (&c_result, &c_bv[c_index], (_len) / 8); \
104 if (!scm_is_eq (endianness, scm_i_native_endianness)) \
105 c_result = INT_SWAP (_len) (c_result); \
107 result = SCM_I_MAKINUM (c_result); \
112 /* Template for fixed-size integer access using the native endianness. */
113 #define INTEGER_NATIVE_REF(_len, _sign) \
116 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
119 INT_TYPE (_len, _sign) c_result; \
121 memcpy (&c_result, &c_bv[c_index], (_len) / 8); \
122 result = SCM_I_MAKINUM (c_result); \
127 /* Template for fixed-size integer modification (only 8, 16 or 32-bit). */
128 #define INTEGER_SET(_len, _sign) \
129 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
130 SCM_VALIDATE_SYMBOL (3, endianness); \
133 scm_t_signed_bits c_value; \
134 INT_TYPE (_len, _sign) c_value_short; \
136 if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
137 scm_wrong_type_arg (FUNC_NAME, 3, value); \
139 c_value = SCM_I_INUM (value); \
140 if (SCM_UNLIKELY (!INT_VALID_P (_len, _sign) (c_value))) \
141 scm_out_of_range (FUNC_NAME, value); \
143 c_value_short = (INT_TYPE (_len, _sign)) c_value; \
144 if (!scm_is_eq (endianness, scm_i_native_endianness)) \
145 c_value_short = INT_SWAP (_len) (c_value_short); \
147 memcpy (&c_bv[c_index], &c_value_short, (_len) / 8); \
150 return SCM_UNSPECIFIED;
152 /* Template for fixed-size integer modification using the native
154 #define INTEGER_NATIVE_SET(_len, _sign) \
155 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
158 scm_t_signed_bits c_value; \
159 INT_TYPE (_len, _sign) c_value_short; \
161 if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
162 scm_wrong_type_arg (FUNC_NAME, 3, value); \
164 c_value = SCM_I_INUM (value); \
165 if (SCM_UNLIKELY (!INT_VALID_P (_len, _sign) (c_value))) \
166 scm_out_of_range (FUNC_NAME, value); \
168 c_value_short = (INT_TYPE (_len, _sign)) c_value; \
170 memcpy (&c_bv[c_index], &c_value_short, (_len) / 8); \
173 return SCM_UNSPECIFIED;
177 /* Bytevector type. */
179 #define SCM_BYTEVECTOR_HEADER_BYTES \
180 (SCM_BYTEVECTOR_HEADER_SIZE * sizeof (scm_t_bits))
182 #define SCM_BYTEVECTOR_SET_LENGTH(_bv, _len) \
183 SCM_SET_CELL_WORD_1 ((_bv), (scm_t_bits) (_len))
184 #define SCM_BYTEVECTOR_SET_CONTENTS(_bv, _contents) \
185 SCM_SET_CELL_WORD_2 ((_bv), (scm_t_bits) (_contents))
186 #define SCM_BYTEVECTOR_SET_CONTIGUOUS_P(bv, contiguous_p) \
187 SCM_SET_BYTEVECTOR_FLAGS ((bv), \
188 SCM_BYTEVECTOR_ELEMENT_TYPE (bv) \
189 | ((contiguous_p) << 8UL))
191 #define SCM_BYTEVECTOR_SET_ELEMENT_TYPE(bv, hint) \
192 SCM_SET_BYTEVECTOR_FLAGS ((bv), \
194 | (SCM_BYTEVECTOR_CONTIGUOUS_P (bv) << 8UL))
195 #define SCM_BYTEVECTOR_TYPE_SIZE(var) \
196 (scm_i_array_element_type_sizes[SCM_BYTEVECTOR_ELEMENT_TYPE (var)]/8)
197 #define SCM_BYTEVECTOR_TYPED_LENGTH(var) \
198 (SCM_BYTEVECTOR_LENGTH (var) / SCM_BYTEVECTOR_TYPE_SIZE (var))
200 /* The empty bytevector. */
201 SCM scm_null_bytevector
= SCM_UNSPECIFIED
;
205 make_bytevector (size_t len
, scm_t_array_element_type element_type
)
210 if (SCM_UNLIKELY (element_type
> SCM_ARRAY_ELEMENT_TYPE_LAST
211 || scm_i_array_element_type_sizes
[element_type
] < 8
212 || len
>= (SCM_I_SIZE_MAX
213 / (scm_i_array_element_type_sizes
[element_type
]/8))))
214 /* This would be an internal Guile programming error */
217 if (SCM_UNLIKELY (len
== 0 && element_type
== SCM_ARRAY_ELEMENT_TYPE_VU8
218 && SCM_BYTEVECTOR_P (scm_null_bytevector
)))
219 ret
= scm_null_bytevector
;
222 signed char *contents
;
224 c_len
= len
* (scm_i_array_element_type_sizes
[element_type
] / 8);
226 contents
= scm_gc_malloc_pointerless (SCM_BYTEVECTOR_HEADER_BYTES
+ c_len
,
228 ret
= PTR2SCM (contents
);
229 contents
+= SCM_BYTEVECTOR_HEADER_BYTES
;
231 SCM_BYTEVECTOR_SET_LENGTH (ret
, c_len
);
232 SCM_BYTEVECTOR_SET_CONTENTS (ret
, contents
);
233 SCM_BYTEVECTOR_SET_CONTIGUOUS_P (ret
, 1);
234 SCM_BYTEVECTOR_SET_ELEMENT_TYPE (ret
, element_type
);
240 /* Return a bytevector of LEN elements of type ELEMENT_TYPE, with element
241 values taken from CONTENTS. Assume that the storage for CONTENTS will be
242 automatically reclaimed when it becomes unreachable. */
244 make_bytevector_from_buffer (size_t len
, void *contents
,
245 scm_t_array_element_type element_type
)
249 if (SCM_UNLIKELY (len
== 0))
250 ret
= make_bytevector (len
, element_type
);
255 ret
= PTR2SCM (scm_gc_malloc (SCM_BYTEVECTOR_HEADER_BYTES
,
258 c_len
= len
* (scm_i_array_element_type_sizes
[element_type
] / 8);
260 SCM_BYTEVECTOR_SET_LENGTH (ret
, c_len
);
261 SCM_BYTEVECTOR_SET_CONTENTS (ret
, contents
);
262 SCM_BYTEVECTOR_SET_CONTIGUOUS_P (ret
, 0);
263 SCM_BYTEVECTOR_SET_ELEMENT_TYPE (ret
, element_type
);
270 /* Return a new bytevector of size LEN octets. */
272 scm_c_make_bytevector (size_t len
)
274 return make_bytevector (len
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
277 /* Return a new bytevector of size LEN elements. */
279 scm_i_make_typed_bytevector (size_t len
, scm_t_array_element_type element_type
)
281 return make_bytevector (len
, element_type
);
284 /* Return a bytevector of size LEN made up of CONTENTS. The area pointed to
285 by CONTENTS must have been allocated using `scm_gc_malloc ()'. */
287 scm_c_take_gc_bytevector (signed char *contents
, size_t len
)
289 return make_bytevector_from_buffer (len
, contents
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
293 scm_c_take_typed_bytevector (signed char *contents
, size_t len
,
294 scm_t_array_element_type element_type
)
296 return make_bytevector_from_buffer (len
, contents
, element_type
);
299 /* Shrink BV to C_NEW_LEN (which is assumed to be smaller than its current
300 size) and return the new bytevector (possibly different from BV). */
302 scm_c_shrink_bytevector (SCM bv
, size_t c_new_len
)
307 if (SCM_UNLIKELY (c_new_len
% SCM_BYTEVECTOR_TYPE_SIZE (bv
)))
308 /* This would be an internal Guile programming error */
311 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
312 if (SCM_UNLIKELY (c_new_len
> c_len
))
315 SCM_BYTEVECTOR_SET_LENGTH (bv
, c_new_len
);
317 if (SCM_BYTEVECTOR_CONTIGUOUS_P (bv
))
318 new_bv
= PTR2SCM (scm_gc_realloc (SCM2PTR (bv
),
319 c_len
+ SCM_BYTEVECTOR_HEADER_BYTES
,
320 c_new_len
+ SCM_BYTEVECTOR_HEADER_BYTES
,
326 c_bv
= scm_gc_realloc (SCM_BYTEVECTOR_CONTENTS (bv
),
327 c_len
, c_new_len
, SCM_GC_BYTEVECTOR
);
328 SCM_BYTEVECTOR_SET_CONTENTS (bv
, c_bv
);
337 scm_is_bytevector (SCM obj
)
339 return SCM_BYTEVECTOR_P (obj
);
343 scm_c_bytevector_length (SCM bv
)
344 #define FUNC_NAME "scm_c_bytevector_length"
346 SCM_VALIDATE_BYTEVECTOR (1, bv
);
348 return SCM_BYTEVECTOR_LENGTH (bv
);
353 scm_c_bytevector_ref (SCM bv
, size_t index
)
354 #define FUNC_NAME "scm_c_bytevector_ref"
357 const scm_t_uint8
*c_bv
;
359 SCM_VALIDATE_BYTEVECTOR (1, bv
);
361 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
362 c_bv
= (scm_t_uint8
*) SCM_BYTEVECTOR_CONTENTS (bv
);
364 if (SCM_UNLIKELY (index
>= c_len
))
365 scm_out_of_range (FUNC_NAME
, scm_from_size_t (index
));
372 scm_c_bytevector_set_x (SCM bv
, size_t index
, scm_t_uint8 value
)
373 #define FUNC_NAME "scm_c_bytevector_set_x"
378 SCM_VALIDATE_BYTEVECTOR (1, bv
);
380 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
381 c_bv
= (scm_t_uint8
*) SCM_BYTEVECTOR_CONTENTS (bv
);
383 if (SCM_UNLIKELY (index
>= c_len
))
384 scm_out_of_range (FUNC_NAME
, scm_from_size_t (index
));
393 scm_i_print_bytevector (SCM bv
, SCM port
, scm_print_state
*pstate SCM_UNUSED
)
395 ssize_t ubnd
, inc
, i
;
396 scm_t_array_handle h
;
398 scm_array_get_handle (bv
, &h
);
400 scm_putc ('#', port
);
401 scm_write (scm_array_handle_element_type (&h
), port
);
402 scm_putc ('(', port
);
403 for (i
= h
.dims
[0].lbnd
, ubnd
= h
.dims
[0].ubnd
, inc
= h
.dims
[0].inc
;
407 scm_putc (' ', port
);
408 scm_write (scm_array_handle_ref (&h
, i
), port
);
410 scm_putc (')', port
);
416 /* General operations. */
418 SCM_SYMBOL (scm_sym_big
, "big");
419 SCM_SYMBOL (scm_sym_little
, "little");
421 SCM scm_endianness_big
, scm_endianness_little
;
423 /* Host endianness (a symbol). */
424 SCM scm_i_native_endianness
= SCM_UNSPECIFIED
;
428 # define bswap_24(_x) \
429 ((((_x) & 0xff0000) >> 16) | \
430 (((_x) & 0x00ff00)) | \
431 (((_x) & 0x0000ff) << 16))
435 SCM_DEFINE (scm_native_endianness
, "native-endianness", 0, 0, 0,
437 "Return a symbol denoting the machine's native endianness.")
438 #define FUNC_NAME s_scm_native_endianness
440 return scm_i_native_endianness
;
444 SCM_DEFINE (scm_bytevector_p
, "bytevector?", 1, 0, 0,
446 "Return true if @var{obj} is a bytevector.")
447 #define FUNC_NAME s_scm_bytevector_p
449 return scm_from_bool (scm_is_bytevector (obj
));
453 SCM_DEFINE (scm_make_bytevector
, "make-bytevector", 1, 1, 0,
455 "Return a newly allocated bytevector of @var{len} bytes, "
456 "optionally filled with @var{fill}.")
457 #define FUNC_NAME s_scm_make_bytevector
461 signed char c_fill
= '\0';
463 SCM_VALIDATE_UINT_COPY (1, len
, c_len
);
464 if (!scm_is_eq (fill
, SCM_UNDEFINED
))
468 value
= scm_to_int (fill
);
469 if (SCM_UNLIKELY ((value
< -128) || (value
> 255)))
470 scm_out_of_range (FUNC_NAME
, fill
);
471 c_fill
= (signed char) value
;
474 bv
= make_bytevector (c_len
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
475 if (!scm_is_eq (fill
, SCM_UNDEFINED
))
478 signed char *contents
;
480 contents
= SCM_BYTEVECTOR_CONTENTS (bv
);
481 for (i
= 0; i
< c_len
; i
++)
482 contents
[i
] = c_fill
;
485 memset (SCM_BYTEVECTOR_CONTENTS (bv
), 0, c_len
);
491 SCM_DEFINE (scm_bytevector_length
, "bytevector-length", 1, 0, 0,
493 "Return the length (in bytes) of @var{bv}.")
494 #define FUNC_NAME s_scm_bytevector_length
496 return scm_from_uint (scm_c_bytevector_length (bv
));
500 SCM_DEFINE (scm_bytevector_eq_p
, "bytevector=?", 2, 0, 0,
502 "Return is @var{bv1} equals to @var{bv2}---i.e., if they "
503 "have the same length and contents.")
504 #define FUNC_NAME s_scm_bytevector_eq_p
506 SCM result
= SCM_BOOL_F
;
507 unsigned c_len1
, c_len2
;
509 SCM_VALIDATE_BYTEVECTOR (1, bv1
);
510 SCM_VALIDATE_BYTEVECTOR (2, bv2
);
512 c_len1
= SCM_BYTEVECTOR_LENGTH (bv1
);
513 c_len2
= SCM_BYTEVECTOR_LENGTH (bv2
);
515 if (c_len1
== c_len2
&& (SCM_BYTEVECTOR_ELEMENT_TYPE (bv1
)
516 == SCM_BYTEVECTOR_ELEMENT_TYPE (bv2
)))
518 signed char *c_bv1
, *c_bv2
;
520 c_bv1
= SCM_BYTEVECTOR_CONTENTS (bv1
);
521 c_bv2
= SCM_BYTEVECTOR_CONTENTS (bv2
);
523 result
= scm_from_bool (!memcmp (c_bv1
, c_bv2
, c_len1
));
530 SCM_DEFINE (scm_bytevector_fill_x
, "bytevector-fill!", 2, 0, 0,
532 "Fill bytevector @var{bv} with @var{fill}, a byte.")
533 #define FUNC_NAME s_scm_bytevector_fill_x
536 signed char *c_bv
, c_fill
;
538 SCM_VALIDATE_BYTEVECTOR (1, bv
);
539 c_fill
= scm_to_int8 (fill
);
541 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
542 c_bv
= SCM_BYTEVECTOR_CONTENTS (bv
);
544 for (i
= 0; i
< c_len
; i
++)
547 return SCM_UNSPECIFIED
;
551 SCM_DEFINE (scm_bytevector_copy_x
, "bytevector-copy!", 5, 0, 0,
552 (SCM source
, SCM source_start
, SCM target
, SCM target_start
,
554 "Copy @var{len} bytes from @var{source} into @var{target}, "
555 "starting reading from @var{source_start} (a positive index "
556 "within @var{source}) and start writing at "
557 "@var{target_start}.")
558 #define FUNC_NAME s_scm_bytevector_copy_x
560 unsigned c_len
, c_source_len
, c_target_len
;
561 unsigned c_source_start
, c_target_start
;
562 signed char *c_source
, *c_target
;
564 SCM_VALIDATE_BYTEVECTOR (1, source
);
565 SCM_VALIDATE_BYTEVECTOR (3, target
);
567 c_len
= scm_to_uint (len
);
568 c_source_start
= scm_to_uint (source_start
);
569 c_target_start
= scm_to_uint (target_start
);
571 c_source
= SCM_BYTEVECTOR_CONTENTS (source
);
572 c_target
= SCM_BYTEVECTOR_CONTENTS (target
);
573 c_source_len
= SCM_BYTEVECTOR_LENGTH (source
);
574 c_target_len
= SCM_BYTEVECTOR_LENGTH (target
);
576 if (SCM_UNLIKELY (c_source_start
+ c_len
> c_source_len
))
577 scm_out_of_range (FUNC_NAME
, source_start
);
578 if (SCM_UNLIKELY (c_target_start
+ c_len
> c_target_len
))
579 scm_out_of_range (FUNC_NAME
, target_start
);
581 memmove (c_target
+ c_target_start
,
582 c_source
+ c_source_start
,
585 return SCM_UNSPECIFIED
;
589 SCM_DEFINE (scm_bytevector_copy
, "bytevector-copy", 1, 0, 0,
591 "Return a newly allocated copy of @var{bv}.")
592 #define FUNC_NAME s_scm_bytevector_copy
596 signed char *c_bv
, *c_copy
;
598 SCM_VALIDATE_BYTEVECTOR (1, bv
);
600 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
601 c_bv
= SCM_BYTEVECTOR_CONTENTS (bv
);
603 copy
= make_bytevector (c_len
, SCM_BYTEVECTOR_ELEMENT_TYPE (bv
));
604 c_copy
= SCM_BYTEVECTOR_CONTENTS (copy
);
605 memcpy (c_copy
, c_bv
, c_len
);
611 SCM_DEFINE (scm_uniform_array_to_bytevector
, "uniform-array->bytevector",
612 1, 0, 0, (SCM array
),
613 "Return a newly allocated bytevector whose contents\n"
614 "will be copied from the uniform array @var{array}.")
615 #define FUNC_NAME s_scm_uniform_array_to_bytevector
618 size_t len
, sz
, byte_len
;
619 scm_t_array_handle h
;
622 contents
= scm_array_contents (array
, SCM_BOOL_T
);
623 if (scm_is_false (contents
))
624 scm_wrong_type_arg_msg (FUNC_NAME
, 0, array
, "uniform contiguous array");
626 scm_array_get_handle (contents
, &h
);
627 assert (h
.base
== 0);
630 len
= h
.dims
->inc
* (h
.dims
->ubnd
- h
.dims
->lbnd
+ 1);
631 sz
= scm_array_handle_uniform_element_bit_size (&h
);
632 if (sz
>= 8 && ((sz
% 8) == 0))
633 byte_len
= len
* (sz
/ 8);
635 /* byte_len = ceil (len * sz / 8) */
636 byte_len
= (len
* sz
+ 7) / 8;
638 /* an internal guile error, really */
639 SCM_MISC_ERROR ("uniform elements larger than 8 bits must fill whole bytes", SCM_EOL
);
641 ret
= make_bytevector (byte_len
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
642 memcpy (SCM_BYTEVECTOR_CONTENTS (ret
), elts
, byte_len
);
644 scm_array_handle_release (&h
);
651 /* Operations on bytes and octets. */
653 SCM_DEFINE (scm_bytevector_u8_ref
, "bytevector-u8-ref", 2, 0, 0,
655 "Return the octet located at @var{index} in @var{bv}.")
656 #define FUNC_NAME s_scm_bytevector_u8_ref
658 INTEGER_NATIVE_REF (8, unsigned);
662 SCM_DEFINE (scm_bytevector_s8_ref
, "bytevector-s8-ref", 2, 0, 0,
664 "Return the byte located at @var{index} in @var{bv}.")
665 #define FUNC_NAME s_scm_bytevector_s8_ref
667 INTEGER_NATIVE_REF (8, signed);
671 SCM_DEFINE (scm_bytevector_u8_set_x
, "bytevector-u8-set!", 3, 0, 0,
672 (SCM bv
, SCM index
, SCM value
),
673 "Return the octet located at @var{index} in @var{bv}.")
674 #define FUNC_NAME s_scm_bytevector_u8_set_x
676 INTEGER_NATIVE_SET (8, unsigned);
680 SCM_DEFINE (scm_bytevector_s8_set_x
, "bytevector-s8-set!", 3, 0, 0,
681 (SCM bv
, SCM index
, SCM value
),
682 "Return the octet located at @var{index} in @var{bv}.")
683 #define FUNC_NAME s_scm_bytevector_s8_set_x
685 INTEGER_NATIVE_SET (8, signed);
689 #undef OCTET_ACCESSOR_PROLOGUE
692 SCM_DEFINE (scm_bytevector_to_u8_list
, "bytevector->u8-list", 1, 0, 0,
694 "Return a newly allocated list of octets containing the "
695 "contents of @var{bv}.")
696 #define FUNC_NAME s_scm_bytevector_to_u8_list
702 SCM_VALIDATE_BYTEVECTOR (1, bv
);
704 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
705 c_bv
= (unsigned char *) SCM_BYTEVECTOR_CONTENTS (bv
);
707 lst
= scm_make_list (scm_from_uint (c_len
), SCM_UNSPECIFIED
);
708 for (i
= 0, pair
= lst
;
710 i
++, pair
= SCM_CDR (pair
))
712 SCM_SETCAR (pair
, SCM_I_MAKINUM (c_bv
[i
]));
719 SCM_DEFINE (scm_u8_list_to_bytevector
, "u8-list->bytevector", 1, 0, 0,
721 "Turn @var{lst}, a list of octets, into a bytevector.")
722 #define FUNC_NAME s_scm_u8_list_to_bytevector
728 SCM_VALIDATE_LIST_COPYLEN (1, lst
, c_len
);
730 bv
= make_bytevector (c_len
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
731 c_bv
= (unsigned char *) SCM_BYTEVECTOR_CONTENTS (bv
);
733 for (i
= 0; i
< c_len
; lst
= SCM_CDR (lst
), i
++)
735 item
= SCM_CAR (lst
);
737 if (SCM_LIKELY (SCM_I_INUMP (item
)))
739 scm_t_signed_bits c_item
;
741 c_item
= SCM_I_INUM (item
);
742 if (SCM_LIKELY ((c_item
>= 0) && (c_item
< 256)))
743 c_bv
[i
] = (unsigned char) c_item
;
754 scm_wrong_type_arg (FUNC_NAME
, 1, item
);
760 /* Compute the two's complement of VALUE (a positive integer) on SIZE octets
761 using (2^(SIZE * 8) - VALUE). */
763 twos_complement (mpz_t value
, size_t size
)
765 unsigned long bit_count
;
767 /* We expect BIT_COUNT to fit in a unsigned long thanks to the range
768 checking on SIZE performed earlier. */
769 bit_count
= (unsigned long) size
<< 3UL;
771 if (SCM_LIKELY (bit_count
< sizeof (unsigned long)))
772 mpz_ui_sub (value
, 1UL << bit_count
, value
);
778 mpz_ui_pow_ui (max
, 2, bit_count
);
779 mpz_sub (value
, max
, value
);
785 bytevector_large_ref (const char *c_bv
, size_t c_size
, int signed_p
,
790 int c_endianness
, negative_p
= 0;
794 if (scm_is_eq (endianness
, scm_sym_big
))
795 negative_p
= c_bv
[0] & 0x80;
797 negative_p
= c_bv
[c_size
- 1] & 0x80;
800 c_endianness
= scm_is_eq (endianness
, scm_sym_big
) ? 1 : -1;
803 mpz_import (c_mpz
, 1 /* 1 word */, 1 /* word order doesn't matter */,
804 c_size
/* word is C_SIZE-byte long */,
806 0 /* nails */, c_bv
);
808 if (signed_p
&& negative_p
)
810 twos_complement (c_mpz
, c_size
);
811 mpz_neg (c_mpz
, c_mpz
);
814 result
= scm_from_mpz (c_mpz
);
815 mpz_clear (c_mpz
); /* FIXME: Needed? */
821 bytevector_large_set (char *c_bv
, size_t c_size
, int signed_p
,
822 SCM value
, SCM endianness
)
825 int c_endianness
, c_sign
, err
= 0;
827 c_endianness
= scm_is_eq (endianness
, scm_sym_big
) ? 1 : -1;
830 scm_to_mpz (value
, c_mpz
);
832 c_sign
= mpz_sgn (c_mpz
);
835 if (SCM_LIKELY (signed_p
))
837 mpz_neg (c_mpz
, c_mpz
);
838 twos_complement (c_mpz
, c_size
);
849 memset (c_bv
, 0, c_size
);
852 size_t word_count
, value_size
;
854 value_size
= (mpz_sizeinbase (c_mpz
, 2) + (8 * c_size
)) / (8 * c_size
);
855 if (SCM_UNLIKELY (value_size
> c_size
))
862 mpz_export (c_bv
, &word_count
, 1 /* word order doesn't matter */,
863 c_size
, c_endianness
,
864 0 /* nails */, c_mpz
);
865 if (SCM_UNLIKELY (word_count
!= 1))
866 /* Shouldn't happen since we already checked with VALUE_SIZE. */
876 #define GENERIC_INTEGER_ACCESSOR_PROLOGUE(_sign) \
877 unsigned long c_len, c_index, c_size; \
880 SCM_VALIDATE_BYTEVECTOR (1, bv); \
881 c_index = scm_to_ulong (index); \
882 c_size = scm_to_ulong (size); \
884 c_len = SCM_BYTEVECTOR_LENGTH (bv); \
885 c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
887 /* C_SIZE must have its 3 higher bits set to zero so that \
888 multiplying it by 8 yields a number that fits in an \
890 if (SCM_UNLIKELY ((c_size == 0) || (c_size >= (ULONG_MAX >> 3L)))) \
891 scm_out_of_range (FUNC_NAME, size); \
892 if (SCM_UNLIKELY (c_index + c_size > c_len)) \
893 scm_out_of_range (FUNC_NAME, index);
896 /* Template of an integer reference function. */
897 #define GENERIC_INTEGER_REF(_sign) \
905 swap = !scm_is_eq (endianness, scm_i_native_endianness); \
910 _sign char c_value8; \
911 memcpy (&c_value8, c_bv, 1); \
917 INT_TYPE (16, _sign) c_value16; \
918 memcpy (&c_value16, c_bv, 2); \
920 value = (INT_TYPE (16, _sign)) bswap_16 (c_value16); \
929 result = SCM_I_MAKINUM ((_sign int) value); \
932 result = bytevector_large_ref ((char *) c_bv, \
933 c_size, SIGNEDNESS (_sign), \
939 bytevector_signed_ref (const char *c_bv
, size_t c_size
, SCM endianness
)
941 GENERIC_INTEGER_REF (signed);
945 bytevector_unsigned_ref (const char *c_bv
, size_t c_size
, SCM endianness
)
947 GENERIC_INTEGER_REF (unsigned);
951 /* Template of an integer assignment function. */
952 #define GENERIC_INTEGER_SET(_sign) \
955 scm_t_signed_bits c_value; \
957 if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
960 c_value = SCM_I_INUM (value); \
964 if (SCM_LIKELY (INT_VALID_P (8, _sign) (c_value))) \
966 _sign char c_value8; \
967 c_value8 = (_sign char) c_value; \
968 memcpy (c_bv, &c_value8, 1); \
975 if (SCM_LIKELY (INT_VALID_P (16, _sign) (c_value))) \
978 INT_TYPE (16, _sign) c_value16; \
980 swap = !scm_is_eq (endianness, scm_i_native_endianness); \
983 c_value16 = (INT_TYPE (16, _sign)) bswap_16 (c_value); \
985 c_value16 = c_value; \
987 memcpy (c_bv, &c_value16, 2); \
1001 err = bytevector_large_set (c_bv, c_size, \
1002 SIGNEDNESS (_sign), \
1003 value, endianness); \
1011 scm_out_of_range (FUNC_NAME, value); \
1015 bytevector_signed_set (char *c_bv
, size_t c_size
,
1016 SCM value
, SCM endianness
,
1017 const char *func_name
)
1018 #define FUNC_NAME func_name
1020 GENERIC_INTEGER_SET (signed);
1025 bytevector_unsigned_set (char *c_bv
, size_t c_size
,
1026 SCM value
, SCM endianness
,
1027 const char *func_name
)
1028 #define FUNC_NAME func_name
1030 GENERIC_INTEGER_SET (unsigned);
1034 #undef GENERIC_INTEGER_SET
1035 #undef GENERIC_INTEGER_REF
1038 SCM_DEFINE (scm_bytevector_uint_ref
, "bytevector-uint-ref", 4, 0, 0,
1039 (SCM bv
, SCM index
, SCM endianness
, SCM size
),
1040 "Return the @var{size}-octet long unsigned integer at index "
1041 "@var{index} in @var{bv}.")
1042 #define FUNC_NAME s_scm_bytevector_uint_ref
1044 GENERIC_INTEGER_ACCESSOR_PROLOGUE (unsigned);
1046 return (bytevector_unsigned_ref (&c_bv
[c_index
], c_size
, endianness
));
1050 SCM_DEFINE (scm_bytevector_sint_ref
, "bytevector-sint-ref", 4, 0, 0,
1051 (SCM bv
, SCM index
, SCM endianness
, SCM size
),
1052 "Return the @var{size}-octet long unsigned integer at index "
1053 "@var{index} in @var{bv}.")
1054 #define FUNC_NAME s_scm_bytevector_sint_ref
1056 GENERIC_INTEGER_ACCESSOR_PROLOGUE (signed);
1058 return (bytevector_signed_ref (&c_bv
[c_index
], c_size
, endianness
));
1062 SCM_DEFINE (scm_bytevector_uint_set_x
, "bytevector-uint-set!", 5, 0, 0,
1063 (SCM bv
, SCM index
, SCM value
, SCM endianness
, SCM size
),
1064 "Set the @var{size}-octet long unsigned integer at @var{index} "
1066 #define FUNC_NAME s_scm_bytevector_uint_set_x
1068 GENERIC_INTEGER_ACCESSOR_PROLOGUE (unsigned);
1070 bytevector_unsigned_set (&c_bv
[c_index
], c_size
, value
, endianness
,
1073 return SCM_UNSPECIFIED
;
1077 SCM_DEFINE (scm_bytevector_sint_set_x
, "bytevector-sint-set!", 5, 0, 0,
1078 (SCM bv
, SCM index
, SCM value
, SCM endianness
, SCM size
),
1079 "Set the @var{size}-octet long signed integer at @var{index} "
1081 #define FUNC_NAME s_scm_bytevector_sint_set_x
1083 GENERIC_INTEGER_ACCESSOR_PROLOGUE (signed);
1085 bytevector_signed_set (&c_bv
[c_index
], c_size
, value
, endianness
,
1088 return SCM_UNSPECIFIED
;
1094 /* Operations on integers of arbitrary size. */
1096 #define INTEGERS_TO_LIST(_sign) \
1098 size_t i, c_len, c_size; \
1100 SCM_VALIDATE_BYTEVECTOR (1, bv); \
1101 SCM_VALIDATE_SYMBOL (2, endianness); \
1102 c_size = scm_to_uint (size); \
1104 c_len = SCM_BYTEVECTOR_LENGTH (bv); \
1105 if (SCM_UNLIKELY (c_len == 0)) \
1107 else if (SCM_UNLIKELY (c_len < c_size)) \
1108 scm_out_of_range (FUNC_NAME, size); \
1113 c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
1115 lst = scm_make_list (scm_from_uint (c_len / c_size), \
1117 for (i = 0, pair = lst; \
1118 i <= c_len - c_size; \
1119 i += c_size, c_bv += c_size, pair = SCM_CDR (pair)) \
1122 bytevector_ ## _sign ## _ref (c_bv, c_size, \
1129 SCM_DEFINE (scm_bytevector_to_sint_list
, "bytevector->sint-list",
1131 (SCM bv
, SCM endianness
, SCM size
),
1132 "Return a list of signed integers of @var{size} octets "
1133 "representing the contents of @var{bv}.")
1134 #define FUNC_NAME s_scm_bytevector_to_sint_list
1136 INTEGERS_TO_LIST (signed);
1140 SCM_DEFINE (scm_bytevector_to_uint_list
, "bytevector->uint-list",
1142 (SCM bv
, SCM endianness
, SCM size
),
1143 "Return a list of unsigned integers of @var{size} octets "
1144 "representing the contents of @var{bv}.")
1145 #define FUNC_NAME s_scm_bytevector_to_uint_list
1147 INTEGERS_TO_LIST (unsigned);
1151 #undef INTEGER_TO_LIST
1154 #define INTEGER_LIST_TO_BYTEVECTOR(_sign) \
1158 char *c_bv, *c_bv_ptr; \
1160 SCM_VALIDATE_LIST_COPYLEN (1, lst, c_len); \
1161 SCM_VALIDATE_SYMBOL (2, endianness); \
1162 c_size = scm_to_uint (size); \
1164 if (SCM_UNLIKELY ((c_size == 0) || (c_size >= (ULONG_MAX >> 3L)))) \
1165 scm_out_of_range (FUNC_NAME, size); \
1167 bv = make_bytevector (c_len * c_size, SCM_ARRAY_ELEMENT_TYPE_VU8); \
1168 c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
1170 for (c_bv_ptr = c_bv; \
1171 !scm_is_null (lst); \
1172 lst = SCM_CDR (lst), c_bv_ptr += c_size) \
1174 bytevector_ ## _sign ## _set (c_bv_ptr, c_size, \
1175 SCM_CAR (lst), endianness, \
1182 SCM_DEFINE (scm_uint_list_to_bytevector
, "uint-list->bytevector",
1184 (SCM lst
, SCM endianness
, SCM size
),
1185 "Return a bytevector containing the unsigned integers "
1186 "listed in @var{lst} and encoded on @var{size} octets "
1187 "according to @var{endianness}.")
1188 #define FUNC_NAME s_scm_uint_list_to_bytevector
1190 INTEGER_LIST_TO_BYTEVECTOR (unsigned);
1194 SCM_DEFINE (scm_sint_list_to_bytevector
, "sint-list->bytevector",
1196 (SCM lst
, SCM endianness
, SCM size
),
1197 "Return a bytevector containing the signed integers "
1198 "listed in @var{lst} and encoded on @var{size} octets "
1199 "according to @var{endianness}.")
1200 #define FUNC_NAME s_scm_sint_list_to_bytevector
1202 INTEGER_LIST_TO_BYTEVECTOR (signed);
1206 #undef INTEGER_LIST_TO_BYTEVECTOR
1210 /* Operations on 16-bit integers. */
1212 SCM_DEFINE (scm_bytevector_u16_ref
, "bytevector-u16-ref",
1214 (SCM bv
, SCM index
, SCM endianness
),
1215 "Return the unsigned 16-bit integer from @var{bv} at "
1217 #define FUNC_NAME s_scm_bytevector_u16_ref
1219 INTEGER_REF (16, unsigned);
1223 SCM_DEFINE (scm_bytevector_s16_ref
, "bytevector-s16-ref",
1225 (SCM bv
, SCM index
, SCM endianness
),
1226 "Return the signed 16-bit integer from @var{bv} at "
1228 #define FUNC_NAME s_scm_bytevector_s16_ref
1230 INTEGER_REF (16, signed);
1234 SCM_DEFINE (scm_bytevector_u16_native_ref
, "bytevector-u16-native-ref",
1236 (SCM bv
, SCM index
),
1237 "Return the unsigned 16-bit integer from @var{bv} at "
1238 "@var{index} using the native endianness.")
1239 #define FUNC_NAME s_scm_bytevector_u16_native_ref
1241 INTEGER_NATIVE_REF (16, unsigned);
1245 SCM_DEFINE (scm_bytevector_s16_native_ref
, "bytevector-s16-native-ref",
1247 (SCM bv
, SCM index
),
1248 "Return the unsigned 16-bit integer from @var{bv} at "
1249 "@var{index} using the native endianness.")
1250 #define FUNC_NAME s_scm_bytevector_s16_native_ref
1252 INTEGER_NATIVE_REF (16, signed);
1256 SCM_DEFINE (scm_bytevector_u16_set_x
, "bytevector-u16-set!",
1258 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1259 "Store @var{value} in @var{bv} at @var{index} according to "
1260 "@var{endianness}.")
1261 #define FUNC_NAME s_scm_bytevector_u16_set_x
1263 INTEGER_SET (16, unsigned);
1267 SCM_DEFINE (scm_bytevector_s16_set_x
, "bytevector-s16-set!",
1269 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1270 "Store @var{value} in @var{bv} at @var{index} according to "
1271 "@var{endianness}.")
1272 #define FUNC_NAME s_scm_bytevector_s16_set_x
1274 INTEGER_SET (16, signed);
1278 SCM_DEFINE (scm_bytevector_u16_native_set_x
, "bytevector-u16-native-set!",
1280 (SCM bv
, SCM index
, SCM value
),
1281 "Store the unsigned integer @var{value} at index @var{index} "
1282 "of @var{bv} using the native endianness.")
1283 #define FUNC_NAME s_scm_bytevector_u16_native_set_x
1285 INTEGER_NATIVE_SET (16, unsigned);
1289 SCM_DEFINE (scm_bytevector_s16_native_set_x
, "bytevector-s16-native-set!",
1291 (SCM bv
, SCM index
, SCM value
),
1292 "Store the signed integer @var{value} at index @var{index} "
1293 "of @var{bv} using the native endianness.")
1294 #define FUNC_NAME s_scm_bytevector_s16_native_set_x
1296 INTEGER_NATIVE_SET (16, signed);
1302 /* Operations on 32-bit integers. */
1304 /* Unfortunately, on 32-bit machines `SCM' is not large enough to hold
1305 arbitrary 32-bit integers. Thus we fall back to using the
1306 `large_{ref,set}' variants on 32-bit machines. */
1308 #define LARGE_INTEGER_REF(_len, _sign) \
1309 INTEGER_ACCESSOR_PROLOGUE(_len, _sign); \
1310 SCM_VALIDATE_SYMBOL (3, endianness); \
1312 return (bytevector_large_ref ((char *) c_bv + c_index, _len / 8, \
1313 SIGNEDNESS (_sign), endianness));
1315 #define LARGE_INTEGER_SET(_len, _sign) \
1317 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
1318 SCM_VALIDATE_SYMBOL (4, endianness); \
1320 err = bytevector_large_set ((char *) c_bv + c_index, _len / 8, \
1321 SIGNEDNESS (_sign), value, endianness); \
1322 if (SCM_UNLIKELY (err)) \
1323 scm_out_of_range (FUNC_NAME, value); \
1325 return SCM_UNSPECIFIED;
1327 #define LARGE_INTEGER_NATIVE_REF(_len, _sign) \
1328 INTEGER_ACCESSOR_PROLOGUE(_len, _sign); \
1329 return (bytevector_large_ref ((char *) c_bv + c_index, _len / 8, \
1330 SIGNEDNESS (_sign), scm_i_native_endianness));
1332 #define LARGE_INTEGER_NATIVE_SET(_len, _sign) \
1334 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
1336 err = bytevector_large_set ((char *) c_bv + c_index, _len / 8, \
1337 SIGNEDNESS (_sign), value, \
1338 scm_i_native_endianness); \
1339 if (SCM_UNLIKELY (err)) \
1340 scm_out_of_range (FUNC_NAME, value); \
1342 return SCM_UNSPECIFIED;
1345 SCM_DEFINE (scm_bytevector_u32_ref
, "bytevector-u32-ref",
1347 (SCM bv
, SCM index
, SCM endianness
),
1348 "Return the unsigned 32-bit integer from @var{bv} at "
1350 #define FUNC_NAME s_scm_bytevector_u32_ref
1352 #if SIZEOF_VOID_P > 4
1353 INTEGER_REF (32, unsigned);
1355 LARGE_INTEGER_REF (32, unsigned);
1360 SCM_DEFINE (scm_bytevector_s32_ref
, "bytevector-s32-ref",
1362 (SCM bv
, SCM index
, SCM endianness
),
1363 "Return the signed 32-bit integer from @var{bv} at "
1365 #define FUNC_NAME s_scm_bytevector_s32_ref
1367 #if SIZEOF_VOID_P > 4
1368 INTEGER_REF (32, signed);
1370 LARGE_INTEGER_REF (32, signed);
1375 SCM_DEFINE (scm_bytevector_u32_native_ref
, "bytevector-u32-native-ref",
1377 (SCM bv
, SCM index
),
1378 "Return the unsigned 32-bit integer from @var{bv} at "
1379 "@var{index} using the native endianness.")
1380 #define FUNC_NAME s_scm_bytevector_u32_native_ref
1382 #if SIZEOF_VOID_P > 4
1383 INTEGER_NATIVE_REF (32, unsigned);
1385 LARGE_INTEGER_NATIVE_REF (32, unsigned);
1390 SCM_DEFINE (scm_bytevector_s32_native_ref
, "bytevector-s32-native-ref",
1392 (SCM bv
, SCM index
),
1393 "Return the unsigned 32-bit integer from @var{bv} at "
1394 "@var{index} using the native endianness.")
1395 #define FUNC_NAME s_scm_bytevector_s32_native_ref
1397 #if SIZEOF_VOID_P > 4
1398 INTEGER_NATIVE_REF (32, signed);
1400 LARGE_INTEGER_NATIVE_REF (32, signed);
1405 SCM_DEFINE (scm_bytevector_u32_set_x
, "bytevector-u32-set!",
1407 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1408 "Store @var{value} in @var{bv} at @var{index} according to "
1409 "@var{endianness}.")
1410 #define FUNC_NAME s_scm_bytevector_u32_set_x
1412 #if SIZEOF_VOID_P > 4
1413 INTEGER_SET (32, unsigned);
1415 LARGE_INTEGER_SET (32, unsigned);
1420 SCM_DEFINE (scm_bytevector_s32_set_x
, "bytevector-s32-set!",
1422 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1423 "Store @var{value} in @var{bv} at @var{index} according to "
1424 "@var{endianness}.")
1425 #define FUNC_NAME s_scm_bytevector_s32_set_x
1427 #if SIZEOF_VOID_P > 4
1428 INTEGER_SET (32, signed);
1430 LARGE_INTEGER_SET (32, signed);
1435 SCM_DEFINE (scm_bytevector_u32_native_set_x
, "bytevector-u32-native-set!",
1437 (SCM bv
, SCM index
, SCM value
),
1438 "Store the unsigned integer @var{value} at index @var{index} "
1439 "of @var{bv} using the native endianness.")
1440 #define FUNC_NAME s_scm_bytevector_u32_native_set_x
1442 #if SIZEOF_VOID_P > 4
1443 INTEGER_NATIVE_SET (32, unsigned);
1445 LARGE_INTEGER_NATIVE_SET (32, unsigned);
1450 SCM_DEFINE (scm_bytevector_s32_native_set_x
, "bytevector-s32-native-set!",
1452 (SCM bv
, SCM index
, SCM value
),
1453 "Store the signed integer @var{value} at index @var{index} "
1454 "of @var{bv} using the native endianness.")
1455 #define FUNC_NAME s_scm_bytevector_s32_native_set_x
1457 #if SIZEOF_VOID_P > 4
1458 INTEGER_NATIVE_SET (32, signed);
1460 LARGE_INTEGER_NATIVE_SET (32, signed);
1467 /* Operations on 64-bit integers. */
1469 /* For 64-bit integers, we use only the `large_{ref,set}' variant. */
1471 SCM_DEFINE (scm_bytevector_u64_ref
, "bytevector-u64-ref",
1473 (SCM bv
, SCM index
, SCM endianness
),
1474 "Return the unsigned 64-bit integer from @var{bv} at "
1476 #define FUNC_NAME s_scm_bytevector_u64_ref
1478 LARGE_INTEGER_REF (64, unsigned);
1482 SCM_DEFINE (scm_bytevector_s64_ref
, "bytevector-s64-ref",
1484 (SCM bv
, SCM index
, SCM endianness
),
1485 "Return the signed 64-bit integer from @var{bv} at "
1487 #define FUNC_NAME s_scm_bytevector_s64_ref
1489 LARGE_INTEGER_REF (64, signed);
1493 SCM_DEFINE (scm_bytevector_u64_native_ref
, "bytevector-u64-native-ref",
1495 (SCM bv
, SCM index
),
1496 "Return the unsigned 64-bit integer from @var{bv} at "
1497 "@var{index} using the native endianness.")
1498 #define FUNC_NAME s_scm_bytevector_u64_native_ref
1500 LARGE_INTEGER_NATIVE_REF (64, unsigned);
1504 SCM_DEFINE (scm_bytevector_s64_native_ref
, "bytevector-s64-native-ref",
1506 (SCM bv
, SCM index
),
1507 "Return the unsigned 64-bit integer from @var{bv} at "
1508 "@var{index} using the native endianness.")
1509 #define FUNC_NAME s_scm_bytevector_s64_native_ref
1511 LARGE_INTEGER_NATIVE_REF (64, signed);
1515 SCM_DEFINE (scm_bytevector_u64_set_x
, "bytevector-u64-set!",
1517 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1518 "Store @var{value} in @var{bv} at @var{index} according to "
1519 "@var{endianness}.")
1520 #define FUNC_NAME s_scm_bytevector_u64_set_x
1522 LARGE_INTEGER_SET (64, unsigned);
1526 SCM_DEFINE (scm_bytevector_s64_set_x
, "bytevector-s64-set!",
1528 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1529 "Store @var{value} in @var{bv} at @var{index} according to "
1530 "@var{endianness}.")
1531 #define FUNC_NAME s_scm_bytevector_s64_set_x
1533 LARGE_INTEGER_SET (64, signed);
1537 SCM_DEFINE (scm_bytevector_u64_native_set_x
, "bytevector-u64-native-set!",
1539 (SCM bv
, SCM index
, SCM value
),
1540 "Store the unsigned integer @var{value} at index @var{index} "
1541 "of @var{bv} using the native endianness.")
1542 #define FUNC_NAME s_scm_bytevector_u64_native_set_x
1544 LARGE_INTEGER_NATIVE_SET (64, unsigned);
1548 SCM_DEFINE (scm_bytevector_s64_native_set_x
, "bytevector-s64-native-set!",
1550 (SCM bv
, SCM index
, SCM value
),
1551 "Store the signed integer @var{value} at index @var{index} "
1552 "of @var{bv} using the native endianness.")
1553 #define FUNC_NAME s_scm_bytevector_s64_native_set_x
1555 LARGE_INTEGER_NATIVE_SET (64, signed);
1561 /* Operations on IEEE-754 numbers. */
1563 /* There are two possible word endians, visible in glibc's <ieee754.h>.
1564 However, in R6RS, when the endianness is `little', little endian is
1565 assumed for both the byte order and the word order. This is clear from
1566 Section 2.1 of R6RS-lib (in response to
1567 http://www.r6rs.org/formal-comments/comment-187.txt). */
1569 union scm_ieee754_float
1575 union scm_ieee754_double
1582 /* Convert to/from a floating-point number with different endianness. This
1583 method is probably not the most efficient but it should be portable. */
1586 float_to_foreign_endianness (union scm_ieee754_float
*target
,
1589 union scm_ieee754_float input
;
1592 target
->i
= bswap_32 (input
.i
);
1596 float_from_foreign_endianness (const union scm_ieee754_float
*source
)
1598 union scm_ieee754_float result
;
1600 result
.i
= bswap_32 (source
->i
);
1606 double_to_foreign_endianness (union scm_ieee754_double
*target
,
1609 union scm_ieee754_double input
;
1612 target
->i
= bswap_64 (input
.i
);
1615 static inline double
1616 double_from_foreign_endianness (const union scm_ieee754_double
*source
)
1618 union scm_ieee754_double result
;
1620 result
.i
= bswap_64 (source
->i
);
1625 /* Template macros to abstract over doubles and floats.
1626 XXX: Guile can only convert to/from doubles. */
1627 #define IEEE754_UNION(_c_type) union scm_ieee754_ ## _c_type
1628 #define IEEE754_TO_SCM(_c_type) scm_from_double
1629 #define IEEE754_FROM_SCM(_c_type) scm_to_double
1630 #define IEEE754_FROM_FOREIGN_ENDIANNESS(_c_type) \
1631 _c_type ## _from_foreign_endianness
1632 #define IEEE754_TO_FOREIGN_ENDIANNESS(_c_type) \
1633 _c_type ## _to_foreign_endianness
1636 /* FIXME: SCM_VALIDATE_REAL rejects integers, etc. grrr */
1637 #define VALIDATE_REAL(pos, v) \
1639 SCM_ASSERT_TYPE (scm_is_real (v), v, pos, FUNC_NAME, "real"); \
1642 /* Templace getters and setters. */
1644 #define IEEE754_ACCESSOR_PROLOGUE(_type) \
1645 INTEGER_ACCESSOR_PROLOGUE (sizeof (_type) << 3UL, signed);
1647 #define IEEE754_REF(_type) \
1650 IEEE754_ACCESSOR_PROLOGUE (_type); \
1651 SCM_VALIDATE_SYMBOL (3, endianness); \
1653 if (scm_is_eq (endianness, scm_i_native_endianness)) \
1654 memcpy (&c_result, &c_bv[c_index], sizeof (c_result)); \
1657 IEEE754_UNION (_type) c_raw; \
1659 memcpy (&c_raw, &c_bv[c_index], sizeof (c_raw)); \
1661 IEEE754_FROM_FOREIGN_ENDIANNESS (_type) (&c_raw); \
1664 return (IEEE754_TO_SCM (_type) (c_result));
1666 #define IEEE754_NATIVE_REF(_type) \
1669 IEEE754_ACCESSOR_PROLOGUE (_type); \
1671 memcpy (&c_result, &c_bv[c_index], sizeof (c_result)); \
1672 return (IEEE754_TO_SCM (_type) (c_result));
1674 #define IEEE754_SET(_type) \
1677 IEEE754_ACCESSOR_PROLOGUE (_type); \
1678 VALIDATE_REAL (3, value); \
1679 SCM_VALIDATE_SYMBOL (4, endianness); \
1680 c_value = IEEE754_FROM_SCM (_type) (value); \
1682 if (scm_is_eq (endianness, scm_i_native_endianness)) \
1683 memcpy (&c_bv[c_index], &c_value, sizeof (c_value)); \
1686 IEEE754_UNION (_type) c_raw; \
1688 IEEE754_TO_FOREIGN_ENDIANNESS (_type) (&c_raw, c_value); \
1689 memcpy (&c_bv[c_index], &c_raw, sizeof (c_raw)); \
1692 return SCM_UNSPECIFIED;
1694 #define IEEE754_NATIVE_SET(_type) \
1697 IEEE754_ACCESSOR_PROLOGUE (_type); \
1698 VALIDATE_REAL (3, value); \
1699 c_value = IEEE754_FROM_SCM (_type) (value); \
1701 memcpy (&c_bv[c_index], &c_value, sizeof (c_value)); \
1702 return SCM_UNSPECIFIED;
1705 /* Single precision. */
1707 SCM_DEFINE (scm_bytevector_ieee_single_ref
,
1708 "bytevector-ieee-single-ref",
1710 (SCM bv
, SCM index
, SCM endianness
),
1711 "Return the IEEE-754 single from @var{bv} at "
1713 #define FUNC_NAME s_scm_bytevector_ieee_single_ref
1715 IEEE754_REF (float);
1719 SCM_DEFINE (scm_bytevector_ieee_single_native_ref
,
1720 "bytevector-ieee-single-native-ref",
1722 (SCM bv
, SCM index
),
1723 "Return the IEEE-754 single from @var{bv} at "
1724 "@var{index} using the native endianness.")
1725 #define FUNC_NAME s_scm_bytevector_ieee_single_native_ref
1727 IEEE754_NATIVE_REF (float);
1731 SCM_DEFINE (scm_bytevector_ieee_single_set_x
,
1732 "bytevector-ieee-single-set!",
1734 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1735 "Store real @var{value} in @var{bv} at @var{index} according to "
1736 "@var{endianness}.")
1737 #define FUNC_NAME s_scm_bytevector_ieee_single_set_x
1739 IEEE754_SET (float);
1743 SCM_DEFINE (scm_bytevector_ieee_single_native_set_x
,
1744 "bytevector-ieee-single-native-set!",
1746 (SCM bv
, SCM index
, SCM value
),
1747 "Store the real @var{value} at index @var{index} "
1748 "of @var{bv} using the native endianness.")
1749 #define FUNC_NAME s_scm_bytevector_ieee_single_native_set_x
1751 IEEE754_NATIVE_SET (float);
1756 /* Double precision. */
1758 SCM_DEFINE (scm_bytevector_ieee_double_ref
,
1759 "bytevector-ieee-double-ref",
1761 (SCM bv
, SCM index
, SCM endianness
),
1762 "Return the IEEE-754 double from @var{bv} at "
1764 #define FUNC_NAME s_scm_bytevector_ieee_double_ref
1766 IEEE754_REF (double);
1770 SCM_DEFINE (scm_bytevector_ieee_double_native_ref
,
1771 "bytevector-ieee-double-native-ref",
1773 (SCM bv
, SCM index
),
1774 "Return the IEEE-754 double from @var{bv} at "
1775 "@var{index} using the native endianness.")
1776 #define FUNC_NAME s_scm_bytevector_ieee_double_native_ref
1778 IEEE754_NATIVE_REF (double);
1782 SCM_DEFINE (scm_bytevector_ieee_double_set_x
,
1783 "bytevector-ieee-double-set!",
1785 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1786 "Store real @var{value} in @var{bv} at @var{index} according to "
1787 "@var{endianness}.")
1788 #define FUNC_NAME s_scm_bytevector_ieee_double_set_x
1790 IEEE754_SET (double);
1794 SCM_DEFINE (scm_bytevector_ieee_double_native_set_x
,
1795 "bytevector-ieee-double-native-set!",
1797 (SCM bv
, SCM index
, SCM value
),
1798 "Store the real @var{value} at index @var{index} "
1799 "of @var{bv} using the native endianness.")
1800 #define FUNC_NAME s_scm_bytevector_ieee_double_native_set_x
1802 IEEE754_NATIVE_SET (double);
1807 #undef IEEE754_UNION
1808 #undef IEEE754_TO_SCM
1809 #undef IEEE754_FROM_SCM
1810 #undef IEEE754_FROM_FOREIGN_ENDIANNESS
1811 #undef IEEE754_TO_FOREIGN_ENDIANNESS
1813 #undef IEEE754_NATIVE_REF
1815 #undef IEEE754_NATIVE_SET
1818 /* Operations on strings. */
1821 /* Produce a function that returns the length of a UTF-encoded string. */
1822 #define UTF_STRLEN_FUNCTION(_utf_width) \
1823 static inline size_t \
1824 utf ## _utf_width ## _strlen (const uint ## _utf_width ## _t *str) \
1827 const uint ## _utf_width ## _t *ptr; \
1835 return (len * ((_utf_width) / 8)); \
1838 UTF_STRLEN_FUNCTION (8)
1841 /* Return the length (in bytes) of STR, a UTF-(UTF_WIDTH) encoded string. */
1842 #define UTF_STRLEN(_utf_width, _str) \
1843 utf ## _utf_width ## _strlen (_str)
1845 /* Return the "portable" name of the UTF encoding of size UTF_WIDTH and
1846 ENDIANNESS (Gnulib's `iconv_open' module guarantees the portability of the
1849 utf_encoding_name (char *name
, size_t utf_width
, SCM endianness
)
1851 strcpy (name
, "UTF-");
1852 strcat (name
, ((utf_width
== 8)
1854 : ((utf_width
== 16)
1856 : ((utf_width
== 32)
1860 ((scm_is_eq (endianness
, scm_sym_big
))
1862 : ((scm_is_eq (endianness
, scm_sym_little
))
1867 /* Maximum length of a UTF encoding name. */
1868 #define MAX_UTF_ENCODING_NAME_LEN 16
1870 /* Produce the body of a `string->utf' function. */
1871 #define STRING_TO_UTF(_utf_width) \
1874 char c_utf_name[MAX_UTF_ENCODING_NAME_LEN]; \
1875 char *c_utf = NULL; \
1876 size_t c_strlen, c_utf_len = 0; \
1878 SCM_VALIDATE_STRING (1, str); \
1879 if (scm_is_eq (endianness, SCM_UNDEFINED)) \
1880 endianness = scm_sym_big; \
1882 SCM_VALIDATE_SYMBOL (2, endianness); \
1884 utf_encoding_name (c_utf_name, (_utf_width), endianness); \
1886 c_strlen = scm_i_string_length (str); \
1887 if (scm_i_is_narrow_string (str)) \
1889 err = mem_iconveh (scm_i_string_chars (str), c_strlen, \
1890 "ISO-8859-1", c_utf_name, \
1891 iconveh_question_mark, NULL, \
1892 &c_utf, &c_utf_len); \
1893 if (SCM_UNLIKELY (err)) \
1894 scm_syserror_msg (FUNC_NAME, "failed to convert string: ~A", \
1895 scm_list_1 (str), err); \
1899 const scm_t_wchar *wbuf = scm_i_string_wide_chars (str); \
1900 c_utf = u32_conv_to_encoding (c_utf_name, \
1901 iconveh_question_mark, \
1902 (scm_t_uint32 *) wbuf, \
1903 c_strlen, NULL, NULL, &c_utf_len); \
1904 if (SCM_UNLIKELY (c_utf == NULL)) \
1905 scm_syserror_msg (FUNC_NAME, "failed to convert string: ~A", \
1906 scm_list_1 (str), errno); \
1908 scm_dynwind_begin (0); \
1909 scm_dynwind_free (c_utf); \
1910 utf = make_bytevector (c_utf_len, SCM_ARRAY_ELEMENT_TYPE_VU8); \
1911 memcpy (SCM_BYTEVECTOR_CONTENTS (utf), c_utf, c_utf_len); \
1912 scm_dynwind_end (); \
1918 SCM_DEFINE (scm_string_to_utf8
, "string->utf8",
1921 "Return a newly allocated bytevector that contains the UTF-8 "
1922 "encoding of @var{str}.")
1923 #define FUNC_NAME s_scm_string_to_utf8
1927 size_t c_utf_len
= 0;
1929 SCM_VALIDATE_STRING (1, str
);
1931 c_utf
= (scm_t_uint8
*) scm_to_utf8_stringn (str
, &c_utf_len
);
1932 utf
= make_bytevector (c_utf_len
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
1933 memcpy (SCM_BYTEVECTOR_CONTENTS (utf
), c_utf
, c_utf_len
);
1940 SCM_DEFINE (scm_string_to_utf16
, "string->utf16",
1942 (SCM str
, SCM endianness
),
1943 "Return a newly allocated bytevector that contains the UTF-16 "
1944 "encoding of @var{str}.")
1945 #define FUNC_NAME s_scm_string_to_utf16
1952 swap_u32 (scm_t_wchar
*vals
, size_t len
)
1955 for (n
= 0; n
< len
; n
++)
1956 vals
[n
] = bswap_32 (vals
[n
]);
1959 SCM_DEFINE (scm_string_to_utf32
, "string->utf32",
1961 (SCM str
, SCM endianness
),
1962 "Return a newly allocated bytevector that contains the UTF-32 "
1963 "encoding of @var{str}.")
1964 #define FUNC_NAME s_scm_string_to_utf32
1967 scm_t_wchar
*wchars
;
1968 size_t wchar_len
, bytes_len
;
1970 wchars
= scm_to_utf32_stringn (str
, &wchar_len
);
1971 bytes_len
= wchar_len
* sizeof (scm_t_wchar
);
1972 if (!scm_is_eq (SCM_UNBNDP (endianness
) ? scm_endianness_big
: endianness
,
1973 scm_i_native_endianness
))
1974 swap_u32 (wchars
, wchar_len
);
1976 bv
= make_bytevector (bytes_len
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
1977 memcpy (SCM_BYTEVECTOR_CONTENTS (bv
), wchars
, bytes_len
);
1985 /* Produce the body of a function that converts a UTF-encoded bytevector to a
1987 #define UTF_TO_STRING(_utf_width) \
1988 SCM str = SCM_BOOL_F; \
1990 char *c_str = NULL; \
1991 char c_utf_name[MAX_UTF_ENCODING_NAME_LEN]; \
1993 size_t c_strlen = 0, c_utf_len = 0; \
1995 SCM_VALIDATE_BYTEVECTOR (1, utf); \
1996 if (scm_is_eq (endianness, SCM_UNDEFINED)) \
1997 endianness = scm_sym_big; \
1999 SCM_VALIDATE_SYMBOL (2, endianness); \
2001 c_utf_len = SCM_BYTEVECTOR_LENGTH (utf); \
2002 c_utf = (char *) SCM_BYTEVECTOR_CONTENTS (utf); \
2003 utf_encoding_name (c_utf_name, (_utf_width), endianness); \
2005 err = mem_iconveh (c_utf, c_utf_len, \
2006 c_utf_name, "UTF-8", \
2007 iconveh_question_mark, NULL, \
2008 &c_str, &c_strlen); \
2009 if (SCM_UNLIKELY (err)) \
2010 scm_syserror_msg (FUNC_NAME, "failed to convert to string: ~A", \
2011 scm_list_1 (utf), err); \
2014 str = scm_from_stringn (c_str, c_strlen, "UTF-8", \
2015 SCM_FAILED_CONVERSION_ERROR); \
2021 SCM_DEFINE (scm_utf8_to_string
, "utf8->string",
2024 "Return a newly allocate string that contains from the UTF-8-"
2025 "encoded contents of bytevector @var{utf}.")
2026 #define FUNC_NAME s_scm_utf8_to_string
2030 size_t c_utf_len
= 0;
2032 SCM_VALIDATE_BYTEVECTOR (1, utf
);
2034 c_utf_len
= SCM_BYTEVECTOR_LENGTH (utf
);
2035 c_utf
= (char *) SCM_BYTEVECTOR_CONTENTS (utf
);
2036 str
= scm_from_stringn (c_utf
, c_utf_len
, "UTF-8",
2037 SCM_FAILED_CONVERSION_ERROR
);
2043 SCM_DEFINE (scm_utf16_to_string
, "utf16->string",
2045 (SCM utf
, SCM endianness
),
2046 "Return a newly allocate string that contains from the UTF-16-"
2047 "encoded contents of bytevector @var{utf}.")
2048 #define FUNC_NAME s_scm_utf16_to_string
2054 SCM_DEFINE (scm_utf32_to_string
, "utf32->string",
2056 (SCM utf
, SCM endianness
),
2057 "Return a newly allocate string that contains from the UTF-32-"
2058 "encoded contents of bytevector @var{utf}.")
2059 #define FUNC_NAME s_scm_utf32_to_string
2066 /* Bytevectors as generalized vectors & arrays. */
2068 #define COMPLEX_ACCESSOR_PROLOGUE(_type) \
2069 size_t c_len, c_index; \
2072 SCM_VALIDATE_BYTEVECTOR (1, bv); \
2073 c_index = scm_to_size_t (index); \
2075 c_len = SCM_BYTEVECTOR_LENGTH (bv); \
2076 c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
2078 if (SCM_UNLIKELY (c_index + 2 * sizeof (_type) - 1 >= c_len)) \
2079 scm_out_of_range (FUNC_NAME, index);
2081 /* Template for native access to complex numbers of type TYPE. */
2082 #define COMPLEX_NATIVE_REF(_type) \
2085 COMPLEX_ACCESSOR_PROLOGUE (_type); \
2090 memcpy (&real, &c_bv[c_index], sizeof (_type)); \
2091 memcpy (&imag, &c_bv[c_index + sizeof (_type)], sizeof (_type)); \
2093 result = scm_c_make_rectangular (real, imag); \
2099 bytevector_ref_c32 (SCM bv
, SCM index
)
2100 #define FUNC_NAME "bytevector_ref_c32"
2102 COMPLEX_NATIVE_REF (float);
2107 bytevector_ref_c64 (SCM bv
, SCM index
)
2108 #define FUNC_NAME "bytevector_ref_c64"
2110 COMPLEX_NATIVE_REF (double);
2114 typedef SCM (*scm_t_bytevector_ref_fn
)(SCM
, SCM
);
2116 static const scm_t_bytevector_ref_fn
2117 bytevector_ref_fns
[SCM_ARRAY_ELEMENT_TYPE_LAST
+ 1] =
2122 scm_bytevector_u8_ref
, /* VU8 */
2123 scm_bytevector_u8_ref
, /* U8 */
2124 scm_bytevector_s8_ref
,
2125 scm_bytevector_u16_native_ref
,
2126 scm_bytevector_s16_native_ref
,
2127 scm_bytevector_u32_native_ref
,
2128 scm_bytevector_s32_native_ref
,
2129 scm_bytevector_u64_native_ref
,
2130 scm_bytevector_s64_native_ref
,
2131 scm_bytevector_ieee_single_native_ref
,
2132 scm_bytevector_ieee_double_native_ref
,
2138 bv_handle_ref (scm_t_array_handle
*h
, size_t index
)
2141 scm_t_bytevector_ref_fn ref_fn
;
2143 ref_fn
= bytevector_ref_fns
[h
->element_type
];
2145 scm_from_size_t (index
* scm_array_handle_uniform_element_size (h
));
2146 return ref_fn (h
->array
, byte_index
);
2149 /* Template for native modification of complex numbers of type TYPE. */
2150 #define COMPLEX_NATIVE_SET(_type) \
2151 COMPLEX_ACCESSOR_PROLOGUE (_type); \
2155 real = scm_c_real_part (value); \
2156 imag = scm_c_imag_part (value); \
2158 memcpy (&c_bv[c_index], &real, sizeof (_type)); \
2159 memcpy (&c_bv[c_index + sizeof (_type)], &imag, sizeof (_type)); \
2162 return SCM_UNSPECIFIED;
2165 bytevector_set_c32 (SCM bv
, SCM index
, SCM value
)
2166 #define FUNC_NAME "bytevector_set_c32"
2168 COMPLEX_NATIVE_SET (float);
2173 bytevector_set_c64 (SCM bv
, SCM index
, SCM value
)
2174 #define FUNC_NAME "bytevector_set_c64"
2176 COMPLEX_NATIVE_SET (double);
2180 typedef SCM (*scm_t_bytevector_set_fn
)(SCM
, SCM
, SCM
);
2182 const scm_t_bytevector_set_fn bytevector_set_fns
[SCM_ARRAY_ELEMENT_TYPE_LAST
+ 1] =
2187 scm_bytevector_u8_set_x
, /* VU8 */
2188 scm_bytevector_u8_set_x
, /* U8 */
2189 scm_bytevector_s8_set_x
,
2190 scm_bytevector_u16_native_set_x
,
2191 scm_bytevector_s16_native_set_x
,
2192 scm_bytevector_u32_native_set_x
,
2193 scm_bytevector_s32_native_set_x
,
2194 scm_bytevector_u64_native_set_x
,
2195 scm_bytevector_s64_native_set_x
,
2196 scm_bytevector_ieee_single_native_set_x
,
2197 scm_bytevector_ieee_double_native_set_x
,
2203 bv_handle_set_x (scm_t_array_handle
*h
, size_t index
, SCM val
)
2206 scm_t_bytevector_set_fn set_fn
;
2208 set_fn
= bytevector_set_fns
[h
->element_type
];
2210 scm_from_size_t (index
* scm_array_handle_uniform_element_size (h
));
2211 set_fn (h
->array
, byte_index
, val
);
2215 bytevector_get_handle (SCM v
, scm_t_array_handle
*h
)
2221 h
->dim0
.ubnd
= SCM_BYTEVECTOR_TYPED_LENGTH (v
) - 1;
2223 h
->element_type
= SCM_BYTEVECTOR_ELEMENT_TYPE (v
);
2224 h
->elements
= h
->writable_elements
= SCM_BYTEVECTOR_CONTENTS (v
);
2228 /* Initialization. */
2231 scm_bootstrap_bytevectors (void)
2233 /* This must be instantiated here because the generalized-vector API may
2234 want to access bytevectors even though `(rnrs bytevectors)' hasn't been
2236 scm_null_bytevector
= make_bytevector (0, SCM_ARRAY_ELEMENT_TYPE_VU8
);
2238 #ifdef WORDS_BIGENDIAN
2239 scm_i_native_endianness
= scm_from_latin1_symbol ("big");
2241 scm_i_native_endianness
= scm_from_latin1_symbol ("little");
2244 scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION
,
2245 "scm_init_bytevectors",
2246 (scm_t_extension_init_func
) scm_init_bytevectors
,
2250 scm_t_array_implementation impl
;
2252 impl
.tag
= scm_tc7_bytevector
;
2254 impl
.vref
= bv_handle_ref
;
2255 impl
.vset
= bv_handle_set_x
;
2256 impl
.get_handle
= bytevector_get_handle
;
2257 scm_i_register_array_implementation (&impl
);
2258 scm_i_register_vector_constructor
2259 (scm_i_array_element_types
[SCM_ARRAY_ELEMENT_TYPE_VU8
],
2260 scm_make_bytevector
);
2265 scm_init_bytevectors (void)
2267 #include "libguile/bytevectors.x"
2269 scm_endianness_big
= scm_sym_big
;
2270 scm_endianness_little
= scm_sym_little
;