1 /* Copyright (C) 2009, 2010 Free Software Foundation, Inc.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
29 #include "libguile/_scm.h"
30 #include "libguile/extensions.h"
31 #include "libguile/bytevectors.h"
32 #include "libguile/strings.h"
33 #include "libguile/validate.h"
34 #include "libguile/ieee-754.h"
35 #include "libguile/arrays.h"
36 #include "libguile/array-handle.h"
37 #include "libguile/uniform.h"
38 #include "libguile/srfi-4.h"
41 #include <striconveh.h>
48 /* Assuming 32-bit longs. */
49 # define ULONG_MAX 4294967295UL
58 /* Convenience macros. These are used by the various templates (macros) that
59 are parameterized by integer signedness. */
60 #define INT8_T_signed scm_t_int8
61 #define INT8_T_unsigned scm_t_uint8
62 #define INT16_T_signed scm_t_int16
63 #define INT16_T_unsigned scm_t_uint16
64 #define INT32_T_signed scm_t_int32
65 #define INT32_T_unsigned scm_t_uint32
66 #define is_signed_int8(_x) (((_x) >= -128L) && ((_x) <= 127L))
67 #define is_unsigned_int8(_x) ((_x) <= 255UL)
68 #define is_signed_int16(_x) (((_x) >= -32768L) && ((_x) <= 32767L))
69 #define is_unsigned_int16(_x) ((_x) <= 65535UL)
70 #define is_signed_int32(_x) (((_x) >= -2147483648L) && ((_x) <= 2147483647L))
71 #define is_unsigned_int32(_x) ((_x) <= 4294967295UL)
72 #define SIGNEDNESS_signed 1
73 #define SIGNEDNESS_unsigned 0
75 #define INT_TYPE(_size, _sign) INT ## _size ## _T_ ## _sign
76 #define INT_SWAP(_size) bswap_ ## _size
77 #define INT_VALID_P(_size, _sign) is_ ## _sign ## _int ## _size
78 #define SIGNEDNESS(_sign) SIGNEDNESS_ ## _sign
81 #define INTEGER_ACCESSOR_PROLOGUE(_len, _sign) \
82 size_t c_len, c_index; \
85 SCM_VALIDATE_BYTEVECTOR (1, bv); \
86 c_index = scm_to_uint (index); \
88 c_len = SCM_BYTEVECTOR_LENGTH (bv); \
89 c_bv = (_sign char *) SCM_BYTEVECTOR_CONTENTS (bv); \
91 if (SCM_UNLIKELY (c_index + ((_len) >> 3UL) - 1 >= c_len)) \
92 scm_out_of_range (FUNC_NAME, index);
94 /* Template for fixed-size integer access (only 8, 16 or 32-bit). */
95 #define INTEGER_REF(_len, _sign) \
98 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
99 SCM_VALIDATE_SYMBOL (3, endianness); \
102 INT_TYPE (_len, _sign) c_result; \
104 memcpy (&c_result, &c_bv[c_index], (_len) / 8); \
105 if (!scm_is_eq (endianness, scm_i_native_endianness)) \
106 c_result = INT_SWAP (_len) (c_result); \
108 result = SCM_I_MAKINUM (c_result); \
113 /* Template for fixed-size integer access using the native endianness. */
114 #define INTEGER_NATIVE_REF(_len, _sign) \
117 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
120 INT_TYPE (_len, _sign) c_result; \
122 memcpy (&c_result, &c_bv[c_index], (_len) / 8); \
123 result = SCM_I_MAKINUM (c_result); \
128 /* Template for fixed-size integer modification (only 8, 16 or 32-bit). */
129 #define INTEGER_SET(_len, _sign) \
130 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
131 SCM_VALIDATE_SYMBOL (3, endianness); \
134 _sign long c_value; \
135 INT_TYPE (_len, _sign) c_value_short; \
137 if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
138 scm_wrong_type_arg (FUNC_NAME, 3, value); \
140 c_value = SCM_I_INUM (value); \
141 if (SCM_UNLIKELY (!INT_VALID_P (_len, _sign) (c_value))) \
142 scm_out_of_range (FUNC_NAME, value); \
144 c_value_short = (INT_TYPE (_len, _sign)) c_value; \
145 if (!scm_is_eq (endianness, scm_i_native_endianness)) \
146 c_value_short = INT_SWAP (_len) (c_value_short); \
148 memcpy (&c_bv[c_index], &c_value_short, (_len) / 8); \
151 return SCM_UNSPECIFIED;
153 /* Template for fixed-size integer modification using the native
155 #define INTEGER_NATIVE_SET(_len, _sign) \
156 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
159 _sign long c_value; \
160 INT_TYPE (_len, _sign) c_value_short; \
162 if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
163 scm_wrong_type_arg (FUNC_NAME, 3, value); \
165 c_value = SCM_I_INUM (value); \
166 if (SCM_UNLIKELY (!INT_VALID_P (_len, _sign) (c_value))) \
167 scm_out_of_range (FUNC_NAME, value); \
169 c_value_short = (INT_TYPE (_len, _sign)) c_value; \
171 memcpy (&c_bv[c_index], &c_value_short, (_len) / 8); \
174 return SCM_UNSPECIFIED;
178 /* Bytevector type. */
180 #define SCM_BYTEVECTOR_HEADER_BYTES \
181 (SCM_BYTEVECTOR_HEADER_SIZE * sizeof (SCM))
183 #define SCM_BYTEVECTOR_SET_LENGTH(_bv, _len) \
184 SCM_SET_CELL_WORD_1 ((_bv), (scm_t_bits) (_len))
185 #define SCM_BYTEVECTOR_SET_CONTENTS(_bv, _contents) \
186 SCM_SET_CELL_WORD_2 ((_bv), (scm_t_bits) (_contents))
187 #define SCM_BYTEVECTOR_SET_CONTIGUOUS_P(bv, contiguous_p) \
188 SCM_SET_BYTEVECTOR_FLAGS ((bv), \
189 SCM_BYTEVECTOR_ELEMENT_TYPE (bv) \
190 | ((contiguous_p) << 8UL))
192 #define SCM_BYTEVECTOR_SET_ELEMENT_TYPE(bv, hint) \
193 SCM_SET_BYTEVECTOR_FLAGS ((bv), \
195 | (SCM_BYTEVECTOR_CONTIGUOUS_P (bv) << 8UL))
196 #define SCM_BYTEVECTOR_TYPE_SIZE(var) \
197 (scm_i_array_element_type_sizes[SCM_BYTEVECTOR_ELEMENT_TYPE (var)]/8)
198 #define SCM_BYTEVECTOR_TYPED_LENGTH(var) \
199 (SCM_BYTEVECTOR_LENGTH (var) / SCM_BYTEVECTOR_TYPE_SIZE (var))
201 /* The empty bytevector. */
202 SCM scm_null_bytevector
= SCM_UNSPECIFIED
;
206 make_bytevector (size_t len
, scm_t_array_element_type element_type
)
211 if (SCM_UNLIKELY (element_type
> SCM_ARRAY_ELEMENT_TYPE_LAST
212 || scm_i_array_element_type_sizes
[element_type
] < 8
213 || len
>= (SCM_I_SIZE_MAX
214 / (scm_i_array_element_type_sizes
[element_type
]/8))))
215 /* This would be an internal Guile programming error */
218 if (SCM_UNLIKELY (len
== 0 && element_type
== SCM_ARRAY_ELEMENT_TYPE_VU8
219 && SCM_BYTEVECTOR_P (scm_null_bytevector
)))
220 ret
= scm_null_bytevector
;
223 signed char *contents
;
225 c_len
= len
* (scm_i_array_element_type_sizes
[element_type
] / 8);
227 contents
= scm_gc_malloc_pointerless (SCM_BYTEVECTOR_HEADER_BYTES
+ c_len
,
229 ret
= PTR2SCM (contents
);
230 contents
+= SCM_BYTEVECTOR_HEADER_BYTES
;
232 SCM_BYTEVECTOR_SET_LENGTH (ret
, c_len
);
233 SCM_BYTEVECTOR_SET_CONTENTS (ret
, contents
);
234 SCM_BYTEVECTOR_SET_CONTIGUOUS_P (ret
, 1);
235 SCM_BYTEVECTOR_SET_ELEMENT_TYPE (ret
, element_type
);
241 /* Return a bytevector of LEN elements of type ELEMENT_TYPE, with element
242 values taken from CONTENTS. Assume that the storage for CONTENTS will be
243 automatically reclaimed when it becomes unreachable. */
245 make_bytevector_from_buffer (size_t len
, void *contents
,
246 scm_t_array_element_type element_type
)
250 if (SCM_UNLIKELY (len
== 0))
251 ret
= make_bytevector (len
, element_type
);
256 ret
= PTR2SCM (scm_gc_malloc (SCM_BYTEVECTOR_HEADER_BYTES
,
259 c_len
= len
* (scm_i_array_element_type_sizes
[element_type
] / 8);
261 SCM_BYTEVECTOR_SET_LENGTH (ret
, c_len
);
262 SCM_BYTEVECTOR_SET_CONTENTS (ret
, contents
);
263 SCM_BYTEVECTOR_SET_CONTIGUOUS_P (ret
, 0);
264 SCM_BYTEVECTOR_SET_ELEMENT_TYPE (ret
, element_type
);
271 /* Return a new bytevector of size LEN octets. */
273 scm_c_make_bytevector (size_t len
)
275 return make_bytevector (len
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
278 /* Return a new bytevector of size LEN elements. */
280 scm_i_make_typed_bytevector (size_t len
, scm_t_array_element_type element_type
)
282 return make_bytevector (len
, element_type
);
285 /* Return a bytevector of size LEN made up of CONTENTS. The area pointed to
286 by CONTENTS must have been allocated using `scm_gc_malloc ()'. */
288 scm_c_take_bytevector (signed char *contents
, size_t len
)
290 return make_bytevector_from_buffer (len
, contents
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
294 scm_c_take_typed_bytevector (signed char *contents
, size_t len
,
295 scm_t_array_element_type element_type
)
297 return make_bytevector_from_buffer (len
, contents
, element_type
);
300 /* Shrink BV to C_NEW_LEN (which is assumed to be smaller than its current
301 size) and return the new bytevector (possibly different from BV). */
303 scm_c_shrink_bytevector (SCM bv
, size_t c_new_len
)
308 if (SCM_UNLIKELY (c_new_len
% SCM_BYTEVECTOR_TYPE_SIZE (bv
)))
309 /* This would be an internal Guile programming error */
312 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
313 if (SCM_UNLIKELY (c_new_len
> c_len
))
316 SCM_BYTEVECTOR_SET_LENGTH (bv
, c_new_len
);
318 if (SCM_BYTEVECTOR_CONTIGUOUS_P (bv
))
319 new_bv
= PTR2SCM (scm_gc_realloc (SCM2PTR (bv
),
320 c_len
+ SCM_BYTEVECTOR_HEADER_BYTES
,
321 c_new_len
+ SCM_BYTEVECTOR_HEADER_BYTES
,
327 c_bv
= scm_gc_realloc (SCM_BYTEVECTOR_CONTENTS (bv
),
328 c_len
, c_new_len
, SCM_GC_BYTEVECTOR
);
329 SCM_BYTEVECTOR_SET_CONTENTS (bv
, c_bv
);
338 scm_is_bytevector (SCM obj
)
340 return SCM_BYTEVECTOR_P (obj
);
344 scm_c_bytevector_length (SCM bv
)
345 #define FUNC_NAME "scm_c_bytevector_length"
347 SCM_VALIDATE_BYTEVECTOR (1, bv
);
349 return SCM_BYTEVECTOR_LENGTH (bv
);
354 scm_c_bytevector_ref (SCM bv
, size_t index
)
355 #define FUNC_NAME "scm_c_bytevector_ref"
358 const scm_t_uint8
*c_bv
;
360 SCM_VALIDATE_BYTEVECTOR (1, bv
);
362 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
363 c_bv
= (scm_t_uint8
*) SCM_BYTEVECTOR_CONTENTS (bv
);
365 if (SCM_UNLIKELY (index
>= c_len
))
366 scm_out_of_range (FUNC_NAME
, scm_from_size_t (index
));
373 scm_c_bytevector_set_x (SCM bv
, size_t index
, scm_t_uint8 value
)
374 #define FUNC_NAME "scm_c_bytevector_set_x"
379 SCM_VALIDATE_BYTEVECTOR (1, bv
);
381 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
382 c_bv
= (scm_t_uint8
*) SCM_BYTEVECTOR_CONTENTS (bv
);
384 if (SCM_UNLIKELY (index
>= c_len
))
385 scm_out_of_range (FUNC_NAME
, scm_from_size_t (index
));
394 scm_i_print_bytevector (SCM bv
, SCM port
, scm_print_state
*pstate SCM_UNUSED
)
396 ssize_t ubnd
, inc
, i
;
397 scm_t_array_handle h
;
399 scm_array_get_handle (bv
, &h
);
401 scm_putc ('#', port
);
402 scm_write (scm_array_handle_element_type (&h
), port
);
403 scm_putc ('(', port
);
404 for (i
= h
.dims
[0].lbnd
, ubnd
= h
.dims
[0].ubnd
, inc
= h
.dims
[0].inc
;
408 scm_putc (' ', port
);
409 scm_write (scm_array_handle_ref (&h
, i
), port
);
411 scm_putc (')', port
);
417 /* General operations. */
419 SCM_SYMBOL (scm_sym_big
, "big");
420 SCM_SYMBOL (scm_sym_little
, "little");
422 SCM scm_endianness_big
, scm_endianness_little
;
424 /* Host endianness (a symbol). */
425 SCM scm_i_native_endianness
= SCM_UNSPECIFIED
;
429 # define bswap_24(_x) \
430 ((((_x) & 0xff0000) >> 16) | \
431 (((_x) & 0x00ff00)) | \
432 (((_x) & 0x0000ff) << 16))
436 SCM_DEFINE (scm_native_endianness
, "native-endianness", 0, 0, 0,
438 "Return a symbol denoting the machine's native endianness.")
439 #define FUNC_NAME s_scm_native_endianness
441 return scm_i_native_endianness
;
445 SCM_DEFINE (scm_bytevector_p
, "bytevector?", 1, 0, 0,
447 "Return true if @var{obj} is a bytevector.")
448 #define FUNC_NAME s_scm_bytevector_p
450 return scm_from_bool (scm_is_bytevector (obj
));
454 SCM_DEFINE (scm_make_bytevector
, "make-bytevector", 1, 1, 0,
456 "Return a newly allocated bytevector of @var{len} bytes, "
457 "optionally filled with @var{fill}.")
458 #define FUNC_NAME s_scm_make_bytevector
462 signed char c_fill
= '\0';
464 SCM_VALIDATE_UINT_COPY (1, len
, c_len
);
465 if (fill
!= SCM_UNDEFINED
)
469 value
= scm_to_int (fill
);
470 if (SCM_UNLIKELY ((value
< -128) || (value
> 255)))
471 scm_out_of_range (FUNC_NAME
, fill
);
472 c_fill
= (signed char) value
;
475 bv
= make_bytevector (c_len
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
476 if (fill
!= SCM_UNDEFINED
)
479 signed char *contents
;
481 contents
= SCM_BYTEVECTOR_CONTENTS (bv
);
482 for (i
= 0; i
< c_len
; i
++)
483 contents
[i
] = c_fill
;
490 SCM_DEFINE (scm_bytevector_length
, "bytevector-length", 1, 0, 0,
492 "Return the length (in bytes) of @var{bv}.")
493 #define FUNC_NAME s_scm_bytevector_length
495 return scm_from_uint (scm_c_bytevector_length (bv
));
499 SCM_DEFINE (scm_bytevector_eq_p
, "bytevector=?", 2, 0, 0,
501 "Return is @var{bv1} equals to @var{bv2}---i.e., if they "
502 "have the same length and contents.")
503 #define FUNC_NAME s_scm_bytevector_eq_p
505 SCM result
= SCM_BOOL_F
;
506 unsigned c_len1
, c_len2
;
508 SCM_VALIDATE_BYTEVECTOR (1, bv1
);
509 SCM_VALIDATE_BYTEVECTOR (2, bv2
);
511 c_len1
= SCM_BYTEVECTOR_LENGTH (bv1
);
512 c_len2
= SCM_BYTEVECTOR_LENGTH (bv2
);
514 if (c_len1
== c_len2
)
516 signed char *c_bv1
, *c_bv2
;
518 c_bv1
= SCM_BYTEVECTOR_CONTENTS (bv1
);
519 c_bv2
= SCM_BYTEVECTOR_CONTENTS (bv2
);
521 result
= scm_from_bool (!memcmp (c_bv1
, c_bv2
, c_len1
));
528 SCM_DEFINE (scm_bytevector_fill_x
, "bytevector-fill!", 2, 0, 0,
530 "Fill bytevector @var{bv} with @var{fill}, a byte.")
531 #define FUNC_NAME s_scm_bytevector_fill_x
534 signed char *c_bv
, c_fill
;
536 SCM_VALIDATE_BYTEVECTOR (1, bv
);
537 c_fill
= scm_to_int8 (fill
);
539 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
540 c_bv
= SCM_BYTEVECTOR_CONTENTS (bv
);
542 for (i
= 0; i
< c_len
; i
++)
545 return SCM_UNSPECIFIED
;
549 SCM_DEFINE (scm_bytevector_copy_x
, "bytevector-copy!", 5, 0, 0,
550 (SCM source
, SCM source_start
, SCM target
, SCM target_start
,
552 "Copy @var{len} bytes from @var{source} into @var{target}, "
553 "starting reading from @var{source_start} (a positive index "
554 "within @var{source}) and start writing at "
555 "@var{target_start}.")
556 #define FUNC_NAME s_scm_bytevector_copy_x
558 unsigned c_len
, c_source_len
, c_target_len
;
559 unsigned c_source_start
, c_target_start
;
560 signed char *c_source
, *c_target
;
562 SCM_VALIDATE_BYTEVECTOR (1, source
);
563 SCM_VALIDATE_BYTEVECTOR (3, target
);
565 c_len
= scm_to_uint (len
);
566 c_source_start
= scm_to_uint (source_start
);
567 c_target_start
= scm_to_uint (target_start
);
569 c_source
= SCM_BYTEVECTOR_CONTENTS (source
);
570 c_target
= SCM_BYTEVECTOR_CONTENTS (target
);
571 c_source_len
= SCM_BYTEVECTOR_LENGTH (source
);
572 c_target_len
= SCM_BYTEVECTOR_LENGTH (target
);
574 if (SCM_UNLIKELY (c_source_start
+ c_len
> c_source_len
))
575 scm_out_of_range (FUNC_NAME
, source_start
);
576 if (SCM_UNLIKELY (c_target_start
+ c_len
> c_target_len
))
577 scm_out_of_range (FUNC_NAME
, target_start
);
579 memcpy (c_target
+ c_target_start
,
580 c_source
+ c_source_start
,
583 return SCM_UNSPECIFIED
;
587 SCM_DEFINE (scm_bytevector_copy
, "bytevector-copy", 1, 0, 0,
589 "Return a newly allocated copy of @var{bv}.")
590 #define FUNC_NAME s_scm_bytevector_copy
594 signed char *c_bv
, *c_copy
;
596 SCM_VALIDATE_BYTEVECTOR (1, bv
);
598 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
599 c_bv
= SCM_BYTEVECTOR_CONTENTS (bv
);
601 copy
= make_bytevector (c_len
, SCM_BYTEVECTOR_ELEMENT_TYPE (bv
));
602 c_copy
= SCM_BYTEVECTOR_CONTENTS (copy
);
603 memcpy (c_copy
, c_bv
, c_len
);
609 SCM_DEFINE (scm_uniform_array_to_bytevector
, "uniform-array->bytevector",
610 1, 0, 0, (SCM array
),
611 "Return a newly allocated bytevector whose contents\n"
612 "will be copied from the uniform array @var{array}.")
613 #define FUNC_NAME s_scm_uniform_array_to_bytevector
616 size_t len
, sz
, byte_len
;
617 scm_t_array_handle h
;
620 contents
= scm_array_contents (array
, SCM_BOOL_T
);
621 if (scm_is_false (contents
))
622 scm_wrong_type_arg_msg (FUNC_NAME
, 0, array
, "uniform contiguous array");
624 scm_array_get_handle (contents
, &h
);
625 assert (h
.base
== 0);
628 len
= h
.dims
->inc
* (h
.dims
->ubnd
- h
.dims
->lbnd
+ 1);
629 sz
= scm_array_handle_uniform_element_bit_size (&h
);
630 if (sz
>= 8 && ((sz
% 8) == 0))
631 byte_len
= len
* (sz
/ 8);
633 /* byte_len = ceil (len * sz / 8) */
634 byte_len
= (len
* sz
+ 7) / 8;
636 /* an internal guile error, really */
637 SCM_MISC_ERROR ("uniform elements larger than 8 bits must fill whole bytes", SCM_EOL
);
639 ret
= make_bytevector (byte_len
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
640 memcpy (SCM_BYTEVECTOR_CONTENTS (ret
), elts
, byte_len
);
642 scm_array_handle_release (&h
);
649 /* Operations on bytes and octets. */
651 SCM_DEFINE (scm_bytevector_u8_ref
, "bytevector-u8-ref", 2, 0, 0,
653 "Return the octet located at @var{index} in @var{bv}.")
654 #define FUNC_NAME s_scm_bytevector_u8_ref
656 INTEGER_NATIVE_REF (8, unsigned);
660 SCM_DEFINE (scm_bytevector_s8_ref
, "bytevector-s8-ref", 2, 0, 0,
662 "Return the byte located at @var{index} in @var{bv}.")
663 #define FUNC_NAME s_scm_bytevector_s8_ref
665 INTEGER_NATIVE_REF (8, signed);
669 SCM_DEFINE (scm_bytevector_u8_set_x
, "bytevector-u8-set!", 3, 0, 0,
670 (SCM bv
, SCM index
, SCM value
),
671 "Return the octet located at @var{index} in @var{bv}.")
672 #define FUNC_NAME s_scm_bytevector_u8_set_x
674 INTEGER_NATIVE_SET (8, unsigned);
678 SCM_DEFINE (scm_bytevector_s8_set_x
, "bytevector-s8-set!", 3, 0, 0,
679 (SCM bv
, SCM index
, SCM value
),
680 "Return the octet located at @var{index} in @var{bv}.")
681 #define FUNC_NAME s_scm_bytevector_s8_set_x
683 INTEGER_NATIVE_SET (8, signed);
687 #undef OCTET_ACCESSOR_PROLOGUE
690 SCM_DEFINE (scm_bytevector_to_u8_list
, "bytevector->u8-list", 1, 0, 0,
692 "Return a newly allocated list of octets containing the "
693 "contents of @var{bv}.")
694 #define FUNC_NAME s_scm_bytevector_to_u8_list
700 SCM_VALIDATE_BYTEVECTOR (1, bv
);
702 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
703 c_bv
= (unsigned char *) SCM_BYTEVECTOR_CONTENTS (bv
);
705 lst
= scm_make_list (scm_from_uint (c_len
), SCM_UNSPECIFIED
);
706 for (i
= 0, pair
= lst
;
708 i
++, pair
= SCM_CDR (pair
))
710 SCM_SETCAR (pair
, SCM_I_MAKINUM (c_bv
[i
]));
717 SCM_DEFINE (scm_u8_list_to_bytevector
, "u8-list->bytevector", 1, 0, 0,
719 "Turn @var{lst}, a list of octets, into a bytevector.")
720 #define FUNC_NAME s_scm_u8_list_to_bytevector
726 SCM_VALIDATE_LIST_COPYLEN (1, lst
, c_len
);
728 bv
= make_bytevector (c_len
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
729 c_bv
= (unsigned char *) SCM_BYTEVECTOR_CONTENTS (bv
);
731 for (i
= 0; i
< c_len
; lst
= SCM_CDR (lst
), i
++)
733 item
= SCM_CAR (lst
);
735 if (SCM_LIKELY (SCM_I_INUMP (item
)))
739 c_item
= SCM_I_INUM (item
);
740 if (SCM_LIKELY ((c_item
>= 0) && (c_item
< 256)))
741 c_bv
[i
] = (unsigned char) c_item
;
752 scm_wrong_type_arg (FUNC_NAME
, 1, item
);
758 /* Compute the two's complement of VALUE (a positive integer) on SIZE octets
759 using (2^(SIZE * 8) - VALUE). */
761 twos_complement (mpz_t value
, size_t size
)
763 unsigned long bit_count
;
765 /* We expect BIT_COUNT to fit in a unsigned long thanks to the range
766 checking on SIZE performed earlier. */
767 bit_count
= (unsigned long) size
<< 3UL;
769 if (SCM_LIKELY (bit_count
< sizeof (unsigned long)))
770 mpz_ui_sub (value
, 1UL << bit_count
, value
);
776 mpz_ui_pow_ui (max
, 2, bit_count
);
777 mpz_sub (value
, max
, value
);
783 bytevector_large_ref (const char *c_bv
, size_t c_size
, int signed_p
,
788 int c_endianness
, negative_p
= 0;
792 if (scm_is_eq (endianness
, scm_sym_big
))
793 negative_p
= c_bv
[0] & 0x80;
795 negative_p
= c_bv
[c_size
- 1] & 0x80;
798 c_endianness
= scm_is_eq (endianness
, scm_sym_big
) ? 1 : -1;
801 mpz_import (c_mpz
, 1 /* 1 word */, 1 /* word order doesn't matter */,
802 c_size
/* word is C_SIZE-byte long */,
804 0 /* nails */, c_bv
);
806 if (signed_p
&& negative_p
)
808 twos_complement (c_mpz
, c_size
);
809 mpz_neg (c_mpz
, c_mpz
);
812 result
= scm_from_mpz (c_mpz
);
813 mpz_clear (c_mpz
); /* FIXME: Needed? */
819 bytevector_large_set (char *c_bv
, size_t c_size
, int signed_p
,
820 SCM value
, SCM endianness
)
823 int c_endianness
, c_sign
, err
= 0;
825 c_endianness
= scm_is_eq (endianness
, scm_sym_big
) ? 1 : -1;
828 scm_to_mpz (value
, c_mpz
);
830 c_sign
= mpz_sgn (c_mpz
);
833 if (SCM_LIKELY (signed_p
))
835 mpz_neg (c_mpz
, c_mpz
);
836 twos_complement (c_mpz
, c_size
);
847 memset (c_bv
, 0, c_size
);
850 size_t word_count
, value_size
;
852 value_size
= (mpz_sizeinbase (c_mpz
, 2) + (8 * c_size
)) / (8 * c_size
);
853 if (SCM_UNLIKELY (value_size
> c_size
))
860 mpz_export (c_bv
, &word_count
, 1 /* word order doesn't matter */,
861 c_size
, c_endianness
,
862 0 /* nails */, c_mpz
);
863 if (SCM_UNLIKELY (word_count
!= 1))
864 /* Shouldn't happen since we already checked with VALUE_SIZE. */
874 #define GENERIC_INTEGER_ACCESSOR_PROLOGUE(_sign) \
875 unsigned long c_len, c_index, c_size; \
878 SCM_VALIDATE_BYTEVECTOR (1, bv); \
879 c_index = scm_to_ulong (index); \
880 c_size = scm_to_ulong (size); \
882 c_len = SCM_BYTEVECTOR_LENGTH (bv); \
883 c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
885 /* C_SIZE must have its 3 higher bits set to zero so that \
886 multiplying it by 8 yields a number that fits in an \
888 if (SCM_UNLIKELY ((c_size == 0) || (c_size >= (ULONG_MAX >> 3L)))) \
889 scm_out_of_range (FUNC_NAME, size); \
890 if (SCM_UNLIKELY (c_index + c_size > c_len)) \
891 scm_out_of_range (FUNC_NAME, index);
894 /* Template of an integer reference function. */
895 #define GENERIC_INTEGER_REF(_sign) \
903 swap = !scm_is_eq (endianness, scm_i_native_endianness); \
908 _sign char c_value8; \
909 memcpy (&c_value8, c_bv, 1); \
915 INT_TYPE (16, _sign) c_value16; \
916 memcpy (&c_value16, c_bv, 2); \
918 value = (INT_TYPE (16, _sign)) bswap_16 (c_value16); \
927 result = SCM_I_MAKINUM ((_sign int) value); \
930 result = bytevector_large_ref ((char *) c_bv, \
931 c_size, SIGNEDNESS (_sign), \
937 bytevector_signed_ref (const char *c_bv
, size_t c_size
, SCM endianness
)
939 GENERIC_INTEGER_REF (signed);
943 bytevector_unsigned_ref (const char *c_bv
, size_t c_size
, SCM endianness
)
945 GENERIC_INTEGER_REF (unsigned);
949 /* Template of an integer assignment function. */
950 #define GENERIC_INTEGER_SET(_sign) \
955 if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
958 c_value = SCM_I_INUM (value); \
962 if (SCM_LIKELY (INT_VALID_P (8, _sign) (c_value))) \
964 _sign char c_value8; \
965 c_value8 = (_sign char) c_value; \
966 memcpy (c_bv, &c_value8, 1); \
973 if (SCM_LIKELY (INT_VALID_P (16, _sign) (c_value))) \
976 INT_TYPE (16, _sign) c_value16; \
978 swap = !scm_is_eq (endianness, scm_i_native_endianness); \
981 c_value16 = (INT_TYPE (16, _sign)) bswap_16 (c_value); \
983 c_value16 = c_value; \
985 memcpy (c_bv, &c_value16, 2); \
999 err = bytevector_large_set (c_bv, c_size, \
1000 SIGNEDNESS (_sign), \
1001 value, endianness); \
1009 scm_out_of_range (FUNC_NAME, value); \
1013 bytevector_signed_set (char *c_bv
, size_t c_size
,
1014 SCM value
, SCM endianness
,
1015 const char *func_name
)
1016 #define FUNC_NAME func_name
1018 GENERIC_INTEGER_SET (signed);
1023 bytevector_unsigned_set (char *c_bv
, size_t c_size
,
1024 SCM value
, SCM endianness
,
1025 const char *func_name
)
1026 #define FUNC_NAME func_name
1028 GENERIC_INTEGER_SET (unsigned);
1032 #undef GENERIC_INTEGER_SET
1033 #undef GENERIC_INTEGER_REF
1036 SCM_DEFINE (scm_bytevector_uint_ref
, "bytevector-uint-ref", 4, 0, 0,
1037 (SCM bv
, SCM index
, SCM endianness
, SCM size
),
1038 "Return the @var{size}-octet long unsigned integer at index "
1039 "@var{index} in @var{bv}.")
1040 #define FUNC_NAME s_scm_bytevector_uint_ref
1042 GENERIC_INTEGER_ACCESSOR_PROLOGUE (unsigned);
1044 return (bytevector_unsigned_ref (&c_bv
[c_index
], c_size
, endianness
));
1048 SCM_DEFINE (scm_bytevector_sint_ref
, "bytevector-sint-ref", 4, 0, 0,
1049 (SCM bv
, SCM index
, SCM endianness
, SCM size
),
1050 "Return the @var{size}-octet long unsigned integer at index "
1051 "@var{index} in @var{bv}.")
1052 #define FUNC_NAME s_scm_bytevector_sint_ref
1054 GENERIC_INTEGER_ACCESSOR_PROLOGUE (signed);
1056 return (bytevector_signed_ref (&c_bv
[c_index
], c_size
, endianness
));
1060 SCM_DEFINE (scm_bytevector_uint_set_x
, "bytevector-uint-set!", 5, 0, 0,
1061 (SCM bv
, SCM index
, SCM value
, SCM endianness
, SCM size
),
1062 "Set the @var{size}-octet long unsigned integer at @var{index} "
1064 #define FUNC_NAME s_scm_bytevector_uint_set_x
1066 GENERIC_INTEGER_ACCESSOR_PROLOGUE (unsigned);
1068 bytevector_unsigned_set (&c_bv
[c_index
], c_size
, value
, endianness
,
1071 return SCM_UNSPECIFIED
;
1075 SCM_DEFINE (scm_bytevector_sint_set_x
, "bytevector-sint-set!", 5, 0, 0,
1076 (SCM bv
, SCM index
, SCM value
, SCM endianness
, SCM size
),
1077 "Set the @var{size}-octet long signed integer at @var{index} "
1079 #define FUNC_NAME s_scm_bytevector_sint_set_x
1081 GENERIC_INTEGER_ACCESSOR_PROLOGUE (signed);
1083 bytevector_signed_set (&c_bv
[c_index
], c_size
, value
, endianness
,
1086 return SCM_UNSPECIFIED
;
1092 /* Operations on integers of arbitrary size. */
1094 #define INTEGERS_TO_LIST(_sign) \
1096 size_t i, c_len, c_size; \
1098 SCM_VALIDATE_BYTEVECTOR (1, bv); \
1099 SCM_VALIDATE_SYMBOL (2, endianness); \
1100 c_size = scm_to_uint (size); \
1102 c_len = SCM_BYTEVECTOR_LENGTH (bv); \
1103 if (SCM_UNLIKELY (c_len == 0)) \
1105 else if (SCM_UNLIKELY (c_len < c_size)) \
1106 scm_out_of_range (FUNC_NAME, size); \
1111 c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
1113 lst = scm_make_list (scm_from_uint (c_len / c_size), \
1115 for (i = 0, pair = lst; \
1116 i <= c_len - c_size; \
1117 i += c_size, c_bv += c_size, pair = SCM_CDR (pair)) \
1120 bytevector_ ## _sign ## _ref (c_bv, c_size, \
1127 SCM_DEFINE (scm_bytevector_to_sint_list
, "bytevector->sint-list",
1129 (SCM bv
, SCM endianness
, SCM size
),
1130 "Return a list of signed integers of @var{size} octets "
1131 "representing the contents of @var{bv}.")
1132 #define FUNC_NAME s_scm_bytevector_to_sint_list
1134 INTEGERS_TO_LIST (signed);
1138 SCM_DEFINE (scm_bytevector_to_uint_list
, "bytevector->uint-list",
1140 (SCM bv
, SCM endianness
, SCM size
),
1141 "Return a list of unsigned integers of @var{size} octets "
1142 "representing the contents of @var{bv}.")
1143 #define FUNC_NAME s_scm_bytevector_to_uint_list
1145 INTEGERS_TO_LIST (unsigned);
1149 #undef INTEGER_TO_LIST
1152 #define INTEGER_LIST_TO_BYTEVECTOR(_sign) \
1156 char *c_bv, *c_bv_ptr; \
1158 SCM_VALIDATE_LIST_COPYLEN (1, lst, c_len); \
1159 SCM_VALIDATE_SYMBOL (2, endianness); \
1160 c_size = scm_to_uint (size); \
1162 if (SCM_UNLIKELY ((c_size == 0) || (c_size >= (ULONG_MAX >> 3L)))) \
1163 scm_out_of_range (FUNC_NAME, size); \
1165 bv = make_bytevector (c_len * c_size, SCM_ARRAY_ELEMENT_TYPE_VU8); \
1166 c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
1168 for (c_bv_ptr = c_bv; \
1169 !scm_is_null (lst); \
1170 lst = SCM_CDR (lst), c_bv_ptr += c_size) \
1172 bytevector_ ## _sign ## _set (c_bv_ptr, c_size, \
1173 SCM_CAR (lst), endianness, \
1180 SCM_DEFINE (scm_uint_list_to_bytevector
, "uint-list->bytevector",
1182 (SCM lst
, SCM endianness
, SCM size
),
1183 "Return a bytevector containing the unsigned integers "
1184 "listed in @var{lst} and encoded on @var{size} octets "
1185 "according to @var{endianness}.")
1186 #define FUNC_NAME s_scm_uint_list_to_bytevector
1188 INTEGER_LIST_TO_BYTEVECTOR (unsigned);
1192 SCM_DEFINE (scm_sint_list_to_bytevector
, "sint-list->bytevector",
1194 (SCM lst
, SCM endianness
, SCM size
),
1195 "Return a bytevector containing the signed integers "
1196 "listed in @var{lst} and encoded on @var{size} octets "
1197 "according to @var{endianness}.")
1198 #define FUNC_NAME s_scm_sint_list_to_bytevector
1200 INTEGER_LIST_TO_BYTEVECTOR (signed);
1204 #undef INTEGER_LIST_TO_BYTEVECTOR
1208 /* Operations on 16-bit integers. */
1210 SCM_DEFINE (scm_bytevector_u16_ref
, "bytevector-u16-ref",
1212 (SCM bv
, SCM index
, SCM endianness
),
1213 "Return the unsigned 16-bit integer from @var{bv} at "
1215 #define FUNC_NAME s_scm_bytevector_u16_ref
1217 INTEGER_REF (16, unsigned);
1221 SCM_DEFINE (scm_bytevector_s16_ref
, "bytevector-s16-ref",
1223 (SCM bv
, SCM index
, SCM endianness
),
1224 "Return the signed 16-bit integer from @var{bv} at "
1226 #define FUNC_NAME s_scm_bytevector_s16_ref
1228 INTEGER_REF (16, signed);
1232 SCM_DEFINE (scm_bytevector_u16_native_ref
, "bytevector-u16-native-ref",
1234 (SCM bv
, SCM index
),
1235 "Return the unsigned 16-bit integer from @var{bv} at "
1236 "@var{index} using the native endianness.")
1237 #define FUNC_NAME s_scm_bytevector_u16_native_ref
1239 INTEGER_NATIVE_REF (16, unsigned);
1243 SCM_DEFINE (scm_bytevector_s16_native_ref
, "bytevector-s16-native-ref",
1245 (SCM bv
, SCM index
),
1246 "Return the unsigned 16-bit integer from @var{bv} at "
1247 "@var{index} using the native endianness.")
1248 #define FUNC_NAME s_scm_bytevector_s16_native_ref
1250 INTEGER_NATIVE_REF (16, signed);
1254 SCM_DEFINE (scm_bytevector_u16_set_x
, "bytevector-u16-set!",
1256 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1257 "Store @var{value} in @var{bv} at @var{index} according to "
1258 "@var{endianness}.")
1259 #define FUNC_NAME s_scm_bytevector_u16_set_x
1261 INTEGER_SET (16, unsigned);
1265 SCM_DEFINE (scm_bytevector_s16_set_x
, "bytevector-s16-set!",
1267 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1268 "Store @var{value} in @var{bv} at @var{index} according to "
1269 "@var{endianness}.")
1270 #define FUNC_NAME s_scm_bytevector_s16_set_x
1272 INTEGER_SET (16, signed);
1276 SCM_DEFINE (scm_bytevector_u16_native_set_x
, "bytevector-u16-native-set!",
1278 (SCM bv
, SCM index
, SCM value
),
1279 "Store the unsigned integer @var{value} at index @var{index} "
1280 "of @var{bv} using the native endianness.")
1281 #define FUNC_NAME s_scm_bytevector_u16_native_set_x
1283 INTEGER_NATIVE_SET (16, unsigned);
1287 SCM_DEFINE (scm_bytevector_s16_native_set_x
, "bytevector-s16-native-set!",
1289 (SCM bv
, SCM index
, SCM value
),
1290 "Store the signed integer @var{value} at index @var{index} "
1291 "of @var{bv} using the native endianness.")
1292 #define FUNC_NAME s_scm_bytevector_s16_native_set_x
1294 INTEGER_NATIVE_SET (16, signed);
1300 /* Operations on 32-bit integers. */
1302 /* Unfortunately, on 32-bit machines `SCM' is not large enough to hold
1303 arbitrary 32-bit integers. Thus we fall back to using the
1304 `large_{ref,set}' variants on 32-bit machines. */
1306 #define LARGE_INTEGER_REF(_len, _sign) \
1307 INTEGER_ACCESSOR_PROLOGUE(_len, _sign); \
1308 SCM_VALIDATE_SYMBOL (3, endianness); \
1310 return (bytevector_large_ref ((char *) c_bv + c_index, _len / 8, \
1311 SIGNEDNESS (_sign), endianness));
1313 #define LARGE_INTEGER_SET(_len, _sign) \
1315 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
1316 SCM_VALIDATE_SYMBOL (4, endianness); \
1318 err = bytevector_large_set ((char *) c_bv + c_index, _len / 8, \
1319 SIGNEDNESS (_sign), value, endianness); \
1320 if (SCM_UNLIKELY (err)) \
1321 scm_out_of_range (FUNC_NAME, value); \
1323 return SCM_UNSPECIFIED;
1325 #define LARGE_INTEGER_NATIVE_REF(_len, _sign) \
1326 INTEGER_ACCESSOR_PROLOGUE(_len, _sign); \
1327 return (bytevector_large_ref ((char *) c_bv + c_index, _len / 8, \
1328 SIGNEDNESS (_sign), scm_i_native_endianness));
1330 #define LARGE_INTEGER_NATIVE_SET(_len, _sign) \
1332 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
1334 err = bytevector_large_set ((char *) c_bv + c_index, _len / 8, \
1335 SIGNEDNESS (_sign), value, \
1336 scm_i_native_endianness); \
1337 if (SCM_UNLIKELY (err)) \
1338 scm_out_of_range (FUNC_NAME, value); \
1340 return SCM_UNSPECIFIED;
1343 SCM_DEFINE (scm_bytevector_u32_ref
, "bytevector-u32-ref",
1345 (SCM bv
, SCM index
, SCM endianness
),
1346 "Return the unsigned 32-bit integer from @var{bv} at "
1348 #define FUNC_NAME s_scm_bytevector_u32_ref
1350 #if SIZEOF_VOID_P > 4
1351 INTEGER_REF (32, unsigned);
1353 LARGE_INTEGER_REF (32, unsigned);
1358 SCM_DEFINE (scm_bytevector_s32_ref
, "bytevector-s32-ref",
1360 (SCM bv
, SCM index
, SCM endianness
),
1361 "Return the signed 32-bit integer from @var{bv} at "
1363 #define FUNC_NAME s_scm_bytevector_s32_ref
1365 #if SIZEOF_VOID_P > 4
1366 INTEGER_REF (32, signed);
1368 LARGE_INTEGER_REF (32, signed);
1373 SCM_DEFINE (scm_bytevector_u32_native_ref
, "bytevector-u32-native-ref",
1375 (SCM bv
, SCM index
),
1376 "Return the unsigned 32-bit integer from @var{bv} at "
1377 "@var{index} using the native endianness.")
1378 #define FUNC_NAME s_scm_bytevector_u32_native_ref
1380 #if SIZEOF_VOID_P > 4
1381 INTEGER_NATIVE_REF (32, unsigned);
1383 LARGE_INTEGER_NATIVE_REF (32, unsigned);
1388 SCM_DEFINE (scm_bytevector_s32_native_ref
, "bytevector-s32-native-ref",
1390 (SCM bv
, SCM index
),
1391 "Return the unsigned 32-bit integer from @var{bv} at "
1392 "@var{index} using the native endianness.")
1393 #define FUNC_NAME s_scm_bytevector_s32_native_ref
1395 #if SIZEOF_VOID_P > 4
1396 INTEGER_NATIVE_REF (32, signed);
1398 LARGE_INTEGER_NATIVE_REF (32, signed);
1403 SCM_DEFINE (scm_bytevector_u32_set_x
, "bytevector-u32-set!",
1405 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1406 "Store @var{value} in @var{bv} at @var{index} according to "
1407 "@var{endianness}.")
1408 #define FUNC_NAME s_scm_bytevector_u32_set_x
1410 #if SIZEOF_VOID_P > 4
1411 INTEGER_SET (32, unsigned);
1413 LARGE_INTEGER_SET (32, unsigned);
1418 SCM_DEFINE (scm_bytevector_s32_set_x
, "bytevector-s32-set!",
1420 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1421 "Store @var{value} in @var{bv} at @var{index} according to "
1422 "@var{endianness}.")
1423 #define FUNC_NAME s_scm_bytevector_s32_set_x
1425 #if SIZEOF_VOID_P > 4
1426 INTEGER_SET (32, signed);
1428 LARGE_INTEGER_SET (32, signed);
1433 SCM_DEFINE (scm_bytevector_u32_native_set_x
, "bytevector-u32-native-set!",
1435 (SCM bv
, SCM index
, SCM value
),
1436 "Store the unsigned integer @var{value} at index @var{index} "
1437 "of @var{bv} using the native endianness.")
1438 #define FUNC_NAME s_scm_bytevector_u32_native_set_x
1440 #if SIZEOF_VOID_P > 4
1441 INTEGER_NATIVE_SET (32, unsigned);
1443 LARGE_INTEGER_NATIVE_SET (32, unsigned);
1448 SCM_DEFINE (scm_bytevector_s32_native_set_x
, "bytevector-s32-native-set!",
1450 (SCM bv
, SCM index
, SCM value
),
1451 "Store the signed integer @var{value} at index @var{index} "
1452 "of @var{bv} using the native endianness.")
1453 #define FUNC_NAME s_scm_bytevector_s32_native_set_x
1455 #if SIZEOF_VOID_P > 4
1456 INTEGER_NATIVE_SET (32, signed);
1458 LARGE_INTEGER_NATIVE_SET (32, signed);
1465 /* Operations on 64-bit integers. */
1467 /* For 64-bit integers, we use only the `large_{ref,set}' variant. */
1469 SCM_DEFINE (scm_bytevector_u64_ref
, "bytevector-u64-ref",
1471 (SCM bv
, SCM index
, SCM endianness
),
1472 "Return the unsigned 64-bit integer from @var{bv} at "
1474 #define FUNC_NAME s_scm_bytevector_u64_ref
1476 LARGE_INTEGER_REF (64, unsigned);
1480 SCM_DEFINE (scm_bytevector_s64_ref
, "bytevector-s64-ref",
1482 (SCM bv
, SCM index
, SCM endianness
),
1483 "Return the signed 64-bit integer from @var{bv} at "
1485 #define FUNC_NAME s_scm_bytevector_s64_ref
1487 LARGE_INTEGER_REF (64, signed);
1491 SCM_DEFINE (scm_bytevector_u64_native_ref
, "bytevector-u64-native-ref",
1493 (SCM bv
, SCM index
),
1494 "Return the unsigned 64-bit integer from @var{bv} at "
1495 "@var{index} using the native endianness.")
1496 #define FUNC_NAME s_scm_bytevector_u64_native_ref
1498 LARGE_INTEGER_NATIVE_REF (64, unsigned);
1502 SCM_DEFINE (scm_bytevector_s64_native_ref
, "bytevector-s64-native-ref",
1504 (SCM bv
, SCM index
),
1505 "Return the unsigned 64-bit integer from @var{bv} at "
1506 "@var{index} using the native endianness.")
1507 #define FUNC_NAME s_scm_bytevector_s64_native_ref
1509 LARGE_INTEGER_NATIVE_REF (64, signed);
1513 SCM_DEFINE (scm_bytevector_u64_set_x
, "bytevector-u64-set!",
1515 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1516 "Store @var{value} in @var{bv} at @var{index} according to "
1517 "@var{endianness}.")
1518 #define FUNC_NAME s_scm_bytevector_u64_set_x
1520 LARGE_INTEGER_SET (64, unsigned);
1524 SCM_DEFINE (scm_bytevector_s64_set_x
, "bytevector-s64-set!",
1526 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1527 "Store @var{value} in @var{bv} at @var{index} according to "
1528 "@var{endianness}.")
1529 #define FUNC_NAME s_scm_bytevector_s64_set_x
1531 LARGE_INTEGER_SET (64, signed);
1535 SCM_DEFINE (scm_bytevector_u64_native_set_x
, "bytevector-u64-native-set!",
1537 (SCM bv
, SCM index
, SCM value
),
1538 "Store the unsigned integer @var{value} at index @var{index} "
1539 "of @var{bv} using the native endianness.")
1540 #define FUNC_NAME s_scm_bytevector_u64_native_set_x
1542 LARGE_INTEGER_NATIVE_SET (64, unsigned);
1546 SCM_DEFINE (scm_bytevector_s64_native_set_x
, "bytevector-s64-native-set!",
1548 (SCM bv
, SCM index
, SCM value
),
1549 "Store the signed integer @var{value} at index @var{index} "
1550 "of @var{bv} using the native endianness.")
1551 #define FUNC_NAME s_scm_bytevector_s64_native_set_x
1553 LARGE_INTEGER_NATIVE_SET (64, signed);
1559 /* Operations on IEEE-754 numbers. */
1561 /* There are two possible word endians, visible in glibc's <ieee754.h>.
1562 However, in R6RS, when the endianness is `little', little endian is
1563 assumed for both the byte order and the word order. This is clear from
1564 Section 2.1 of R6RS-lib (in response to
1565 http://www.r6rs.org/formal-comments/comment-187.txt). */
1568 /* Convert to/from a floating-point number with different endianness. This
1569 method is probably not the most efficient but it should be portable. */
1572 float_to_foreign_endianness (union scm_ieee754_float
*target
,
1575 union scm_ieee754_float src
;
1579 #ifdef WORDS_BIGENDIAN
1580 /* Assuming little endian for both byte and word order. */
1581 target
->little_endian
.negative
= src
.big_endian
.negative
;
1582 target
->little_endian
.exponent
= src
.big_endian
.exponent
;
1583 target
->little_endian
.mantissa
= src
.big_endian
.mantissa
;
1585 target
->big_endian
.negative
= src
.little_endian
.negative
;
1586 target
->big_endian
.exponent
= src
.little_endian
.exponent
;
1587 target
->big_endian
.mantissa
= src
.little_endian
.mantissa
;
1592 float_from_foreign_endianness (const union scm_ieee754_float
*source
)
1594 union scm_ieee754_float result
;
1596 #ifdef WORDS_BIGENDIAN
1597 /* Assuming little endian for both byte and word order. */
1598 result
.big_endian
.negative
= source
->little_endian
.negative
;
1599 result
.big_endian
.exponent
= source
->little_endian
.exponent
;
1600 result
.big_endian
.mantissa
= source
->little_endian
.mantissa
;
1602 result
.little_endian
.negative
= source
->big_endian
.negative
;
1603 result
.little_endian
.exponent
= source
->big_endian
.exponent
;
1604 result
.little_endian
.mantissa
= source
->big_endian
.mantissa
;
1611 double_to_foreign_endianness (union scm_ieee754_double
*target
,
1614 union scm_ieee754_double src
;
1618 #ifdef WORDS_BIGENDIAN
1619 /* Assuming little endian for both byte and word order. */
1620 target
->little_little_endian
.negative
= src
.big_endian
.negative
;
1621 target
->little_little_endian
.exponent
= src
.big_endian
.exponent
;
1622 target
->little_little_endian
.mantissa0
= src
.big_endian
.mantissa0
;
1623 target
->little_little_endian
.mantissa1
= src
.big_endian
.mantissa1
;
1625 target
->big_endian
.negative
= src
.little_little_endian
.negative
;
1626 target
->big_endian
.exponent
= src
.little_little_endian
.exponent
;
1627 target
->big_endian
.mantissa0
= src
.little_little_endian
.mantissa0
;
1628 target
->big_endian
.mantissa1
= src
.little_little_endian
.mantissa1
;
1632 static inline double
1633 double_from_foreign_endianness (const union scm_ieee754_double
*source
)
1635 union scm_ieee754_double result
;
1637 #ifdef WORDS_BIGENDIAN
1638 /* Assuming little endian for both byte and word order. */
1639 result
.big_endian
.negative
= source
->little_little_endian
.negative
;
1640 result
.big_endian
.exponent
= source
->little_little_endian
.exponent
;
1641 result
.big_endian
.mantissa0
= source
->little_little_endian
.mantissa0
;
1642 result
.big_endian
.mantissa1
= source
->little_little_endian
.mantissa1
;
1644 result
.little_little_endian
.negative
= source
->big_endian
.negative
;
1645 result
.little_little_endian
.exponent
= source
->big_endian
.exponent
;
1646 result
.little_little_endian
.mantissa0
= source
->big_endian
.mantissa0
;
1647 result
.little_little_endian
.mantissa1
= source
->big_endian
.mantissa1
;
1653 /* Template macros to abstract over doubles and floats.
1654 XXX: Guile can only convert to/from doubles. */
1655 #define IEEE754_UNION(_c_type) union scm_ieee754_ ## _c_type
1656 #define IEEE754_TO_SCM(_c_type) scm_from_double
1657 #define IEEE754_FROM_SCM(_c_type) scm_to_double
1658 #define IEEE754_FROM_FOREIGN_ENDIANNESS(_c_type) \
1659 _c_type ## _from_foreign_endianness
1660 #define IEEE754_TO_FOREIGN_ENDIANNESS(_c_type) \
1661 _c_type ## _to_foreign_endianness
1664 /* FIXME: SCM_VALIDATE_REAL rejects integers, etc. grrr */
1665 #define VALIDATE_REAL(pos, v) \
1667 SCM_ASSERT_TYPE (scm_is_true (scm_rational_p (v)), v, pos, FUNC_NAME, "real"); \
1670 /* Templace getters and setters. */
1672 #define IEEE754_ACCESSOR_PROLOGUE(_type) \
1673 INTEGER_ACCESSOR_PROLOGUE (sizeof (_type) << 3UL, signed);
1675 #define IEEE754_REF(_type) \
1678 IEEE754_ACCESSOR_PROLOGUE (_type); \
1679 SCM_VALIDATE_SYMBOL (3, endianness); \
1681 if (scm_is_eq (endianness, scm_i_native_endianness)) \
1682 memcpy (&c_result, &c_bv[c_index], sizeof (c_result)); \
1685 IEEE754_UNION (_type) c_raw; \
1687 memcpy (&c_raw, &c_bv[c_index], sizeof (c_raw)); \
1689 IEEE754_FROM_FOREIGN_ENDIANNESS (_type) (&c_raw); \
1692 return (IEEE754_TO_SCM (_type) (c_result));
1694 #define IEEE754_NATIVE_REF(_type) \
1697 IEEE754_ACCESSOR_PROLOGUE (_type); \
1699 memcpy (&c_result, &c_bv[c_index], sizeof (c_result)); \
1700 return (IEEE754_TO_SCM (_type) (c_result));
1702 #define IEEE754_SET(_type) \
1705 IEEE754_ACCESSOR_PROLOGUE (_type); \
1706 VALIDATE_REAL (3, value); \
1707 SCM_VALIDATE_SYMBOL (4, endianness); \
1708 c_value = IEEE754_FROM_SCM (_type) (value); \
1710 if (scm_is_eq (endianness, scm_i_native_endianness)) \
1711 memcpy (&c_bv[c_index], &c_value, sizeof (c_value)); \
1714 IEEE754_UNION (_type) c_raw; \
1716 IEEE754_TO_FOREIGN_ENDIANNESS (_type) (&c_raw, c_value); \
1717 memcpy (&c_bv[c_index], &c_raw, sizeof (c_raw)); \
1720 return SCM_UNSPECIFIED;
1722 #define IEEE754_NATIVE_SET(_type) \
1725 IEEE754_ACCESSOR_PROLOGUE (_type); \
1726 VALIDATE_REAL (3, value); \
1727 c_value = IEEE754_FROM_SCM (_type) (value); \
1729 memcpy (&c_bv[c_index], &c_value, sizeof (c_value)); \
1730 return SCM_UNSPECIFIED;
1733 /* Single precision. */
1735 SCM_DEFINE (scm_bytevector_ieee_single_ref
,
1736 "bytevector-ieee-single-ref",
1738 (SCM bv
, SCM index
, SCM endianness
),
1739 "Return the IEEE-754 single from @var{bv} at "
1741 #define FUNC_NAME s_scm_bytevector_ieee_single_ref
1743 IEEE754_REF (float);
1747 SCM_DEFINE (scm_bytevector_ieee_single_native_ref
,
1748 "bytevector-ieee-single-native-ref",
1750 (SCM bv
, SCM index
),
1751 "Return the IEEE-754 single from @var{bv} at "
1752 "@var{index} using the native endianness.")
1753 #define FUNC_NAME s_scm_bytevector_ieee_single_native_ref
1755 IEEE754_NATIVE_REF (float);
1759 SCM_DEFINE (scm_bytevector_ieee_single_set_x
,
1760 "bytevector-ieee-single-set!",
1762 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1763 "Store real @var{value} in @var{bv} at @var{index} according to "
1764 "@var{endianness}.")
1765 #define FUNC_NAME s_scm_bytevector_ieee_single_set_x
1767 IEEE754_SET (float);
1771 SCM_DEFINE (scm_bytevector_ieee_single_native_set_x
,
1772 "bytevector-ieee-single-native-set!",
1774 (SCM bv
, SCM index
, SCM value
),
1775 "Store the real @var{value} at index @var{index} "
1776 "of @var{bv} using the native endianness.")
1777 #define FUNC_NAME s_scm_bytevector_ieee_single_native_set_x
1779 IEEE754_NATIVE_SET (float);
1784 /* Double precision. */
1786 SCM_DEFINE (scm_bytevector_ieee_double_ref
,
1787 "bytevector-ieee-double-ref",
1789 (SCM bv
, SCM index
, SCM endianness
),
1790 "Return the IEEE-754 double from @var{bv} at "
1792 #define FUNC_NAME s_scm_bytevector_ieee_double_ref
1794 IEEE754_REF (double);
1798 SCM_DEFINE (scm_bytevector_ieee_double_native_ref
,
1799 "bytevector-ieee-double-native-ref",
1801 (SCM bv
, SCM index
),
1802 "Return the IEEE-754 double from @var{bv} at "
1803 "@var{index} using the native endianness.")
1804 #define FUNC_NAME s_scm_bytevector_ieee_double_native_ref
1806 IEEE754_NATIVE_REF (double);
1810 SCM_DEFINE (scm_bytevector_ieee_double_set_x
,
1811 "bytevector-ieee-double-set!",
1813 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1814 "Store real @var{value} in @var{bv} at @var{index} according to "
1815 "@var{endianness}.")
1816 #define FUNC_NAME s_scm_bytevector_ieee_double_set_x
1818 IEEE754_SET (double);
1822 SCM_DEFINE (scm_bytevector_ieee_double_native_set_x
,
1823 "bytevector-ieee-double-native-set!",
1825 (SCM bv
, SCM index
, SCM value
),
1826 "Store the real @var{value} at index @var{index} "
1827 "of @var{bv} using the native endianness.")
1828 #define FUNC_NAME s_scm_bytevector_ieee_double_native_set_x
1830 IEEE754_NATIVE_SET (double);
1835 #undef IEEE754_UNION
1836 #undef IEEE754_TO_SCM
1837 #undef IEEE754_FROM_SCM
1838 #undef IEEE754_FROM_FOREIGN_ENDIANNESS
1839 #undef IEEE754_TO_FOREIGN_ENDIANNESS
1841 #undef IEEE754_NATIVE_REF
1843 #undef IEEE754_NATIVE_SET
1846 /* Operations on strings. */
1849 /* Produce a function that returns the length of a UTF-encoded string. */
1850 #define UTF_STRLEN_FUNCTION(_utf_width) \
1851 static inline size_t \
1852 utf ## _utf_width ## _strlen (const uint ## _utf_width ## _t *str) \
1855 const uint ## _utf_width ## _t *ptr; \
1863 return (len * ((_utf_width) / 8)); \
1866 UTF_STRLEN_FUNCTION (8)
1869 /* Return the length (in bytes) of STR, a UTF-(UTF_WIDTH) encoded string. */
1870 #define UTF_STRLEN(_utf_width, _str) \
1871 utf ## _utf_width ## _strlen (_str)
1873 /* Return the "portable" name of the UTF encoding of size UTF_WIDTH and
1874 ENDIANNESS (Gnulib's `iconv_open' module guarantees the portability of the
1877 utf_encoding_name (char *name
, size_t utf_width
, SCM endianness
)
1879 strcpy (name
, "UTF-");
1880 strcat (name
, ((utf_width
== 8)
1882 : ((utf_width
== 16)
1884 : ((utf_width
== 32)
1888 ((scm_is_eq (endianness
, scm_sym_big
))
1890 : ((scm_is_eq (endianness
, scm_sym_little
))
1895 /* Maximum length of a UTF encoding name. */
1896 #define MAX_UTF_ENCODING_NAME_LEN 16
1898 /* Produce the body of a `string->utf' function. */
1899 #define STRING_TO_UTF(_utf_width) \
1902 char c_utf_name[MAX_UTF_ENCODING_NAME_LEN]; \
1903 char *c_utf = NULL; \
1904 size_t c_strlen, c_utf_len = 0; \
1906 SCM_VALIDATE_STRING (1, str); \
1907 if (endianness == SCM_UNDEFINED) \
1908 endianness = scm_sym_big; \
1910 SCM_VALIDATE_SYMBOL (2, endianness); \
1912 utf_encoding_name (c_utf_name, (_utf_width), endianness); \
1914 c_strlen = scm_i_string_length (str); \
1915 if (scm_i_is_narrow_string (str)) \
1917 err = mem_iconveh (scm_i_string_chars (str), c_strlen, \
1918 "ISO-8859-1", c_utf_name, \
1919 iconveh_question_mark, NULL, \
1920 &c_utf, &c_utf_len); \
1921 if (SCM_UNLIKELY (err)) \
1922 scm_syserror_msg (FUNC_NAME, "failed to convert string: ~A", \
1923 scm_list_1 (str), err); \
1927 const scm_t_wchar *wbuf = scm_i_string_wide_chars (str); \
1928 c_utf = u32_conv_to_encoding (c_utf_name, \
1929 iconveh_question_mark, \
1930 (scm_t_uint32 *) wbuf, \
1931 c_strlen, NULL, NULL, &c_utf_len); \
1932 if (SCM_UNLIKELY (c_utf == NULL)) \
1933 scm_syserror_msg (FUNC_NAME, "failed to convert string: ~A", \
1934 scm_list_1 (str), errno); \
1936 scm_dynwind_begin (0); \
1937 scm_dynwind_free (c_utf); \
1938 utf = make_bytevector (c_utf_len, SCM_ARRAY_ELEMENT_TYPE_VU8); \
1939 memcpy (SCM_BYTEVECTOR_CONTENTS (utf), c_utf, c_utf_len); \
1940 scm_dynwind_end (); \
1946 SCM_DEFINE (scm_string_to_utf8
, "string->utf8",
1949 "Return a newly allocated bytevector that contains the UTF-8 "
1950 "encoding of @var{str}.")
1951 #define FUNC_NAME s_scm_string_to_utf8
1955 size_t c_strlen
, c_utf_len
= 0;
1957 SCM_VALIDATE_STRING (1, str
);
1959 c_strlen
= scm_i_string_length (str
);
1960 if (scm_i_is_narrow_string (str
))
1961 c_utf
= u8_conv_from_encoding ("ISO-8859-1", iconveh_question_mark
,
1962 scm_i_string_chars (str
), c_strlen
,
1963 NULL
, NULL
, &c_utf_len
);
1966 const scm_t_wchar
*wbuf
= scm_i_string_wide_chars (str
);
1967 c_utf
= u32_to_u8 ((const uint32_t *) wbuf
, c_strlen
, NULL
, &c_utf_len
);
1969 if (SCM_UNLIKELY (c_utf
== NULL
))
1970 scm_syserror (FUNC_NAME
);
1973 scm_dynwind_begin (0);
1974 scm_dynwind_free (c_utf
);
1976 utf
= make_bytevector (c_utf_len
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
1977 memcpy (SCM_BYTEVECTOR_CONTENTS (utf
), c_utf
, c_utf_len
);
1986 SCM_DEFINE (scm_string_to_utf16
, "string->utf16",
1988 (SCM str
, SCM endianness
),
1989 "Return a newly allocated bytevector that contains the UTF-16 "
1990 "encoding of @var{str}.")
1991 #define FUNC_NAME s_scm_string_to_utf16
1997 SCM_DEFINE (scm_string_to_utf32
, "string->utf32",
1999 (SCM str
, SCM endianness
),
2000 "Return a newly allocated bytevector that contains the UTF-32 "
2001 "encoding of @var{str}.")
2002 #define FUNC_NAME s_scm_string_to_utf32
2009 /* Produce the body of a function that converts a UTF-encoded bytevector to a
2011 #define UTF_TO_STRING(_utf_width) \
2012 SCM str = SCM_BOOL_F; \
2014 char *c_str = NULL; \
2015 char c_utf_name[MAX_UTF_ENCODING_NAME_LEN]; \
2017 size_t c_strlen = 0, c_utf_len = 0; \
2019 SCM_VALIDATE_BYTEVECTOR (1, utf); \
2020 if (endianness == SCM_UNDEFINED) \
2021 endianness = scm_sym_big; \
2023 SCM_VALIDATE_SYMBOL (2, endianness); \
2025 c_utf_len = SCM_BYTEVECTOR_LENGTH (utf); \
2026 c_utf = (char *) SCM_BYTEVECTOR_CONTENTS (utf); \
2027 utf_encoding_name (c_utf_name, (_utf_width), endianness); \
2029 err = mem_iconveh (c_utf, c_utf_len, \
2030 c_utf_name, "UTF-8", \
2031 iconveh_question_mark, NULL, \
2032 &c_str, &c_strlen); \
2033 if (SCM_UNLIKELY (err)) \
2034 scm_syserror_msg (FUNC_NAME, "failed to convert to string: ~A", \
2035 scm_list_1 (utf), err); \
2038 str = scm_from_stringn (c_str, c_strlen, "UTF-8", \
2039 SCM_FAILED_CONVERSION_ERROR); \
2045 SCM_DEFINE (scm_utf8_to_string
, "utf8->string",
2048 "Return a newly allocate string that contains from the UTF-8-"
2049 "encoded contents of bytevector @var{utf}.")
2050 #define FUNC_NAME s_scm_utf8_to_string
2054 size_t c_utf_len
= 0;
2056 SCM_VALIDATE_BYTEVECTOR (1, utf
);
2058 c_utf_len
= SCM_BYTEVECTOR_LENGTH (utf
);
2059 c_utf
= (char *) SCM_BYTEVECTOR_CONTENTS (utf
);
2060 str
= scm_from_stringn (c_utf
, c_utf_len
, "UTF-8",
2061 SCM_FAILED_CONVERSION_ERROR
);
2067 SCM_DEFINE (scm_utf16_to_string
, "utf16->string",
2069 (SCM utf
, SCM endianness
),
2070 "Return a newly allocate string that contains from the UTF-16-"
2071 "encoded contents of bytevector @var{utf}.")
2072 #define FUNC_NAME s_scm_utf16_to_string
2078 SCM_DEFINE (scm_utf32_to_string
, "utf32->string",
2080 (SCM utf
, SCM endianness
),
2081 "Return a newly allocate string that contains from the UTF-32-"
2082 "encoded contents of bytevector @var{utf}.")
2083 #define FUNC_NAME s_scm_utf32_to_string
2090 /* Bytevectors as generalized vectors & arrays. */
2094 bytevector_ref_c32 (SCM bv
, SCM idx
)
2095 { /* FIXME add some checks */
2096 const float *contents
= (const float*)SCM_BYTEVECTOR_CONTENTS (bv
);
2097 size_t i
= scm_to_size_t (idx
);
2098 return scm_c_make_rectangular (contents
[i
/4], contents
[i
/4 + 1]);
2102 bytevector_ref_c64 (SCM bv
, SCM idx
)
2103 { /* FIXME add some checks */
2104 const double *contents
= (const double*)SCM_BYTEVECTOR_CONTENTS (bv
);
2105 size_t i
= scm_to_size_t (idx
);
2106 return scm_c_make_rectangular (contents
[i
/8], contents
[i
/8 + 1]);
2109 typedef SCM (*scm_t_bytevector_ref_fn
)(SCM
, SCM
);
2111 const scm_t_bytevector_ref_fn bytevector_ref_fns
[SCM_ARRAY_ELEMENT_TYPE_LAST
+ 1] =
2116 scm_bytevector_u8_ref
, /* VU8 */
2117 scm_bytevector_u8_ref
, /* U8 */
2118 scm_bytevector_s8_ref
,
2119 scm_bytevector_u16_native_ref
,
2120 scm_bytevector_s16_native_ref
,
2121 scm_bytevector_u32_native_ref
,
2122 scm_bytevector_s32_native_ref
,
2123 scm_bytevector_u64_native_ref
,
2124 scm_bytevector_s64_native_ref
,
2125 scm_bytevector_ieee_single_native_ref
,
2126 scm_bytevector_ieee_double_native_ref
,
2132 bv_handle_ref (scm_t_array_handle
*h
, size_t index
)
2135 scm_t_bytevector_ref_fn ref_fn
;
2137 ref_fn
= bytevector_ref_fns
[h
->element_type
];
2139 scm_from_size_t (index
* scm_array_handle_uniform_element_size (h
));
2140 return ref_fn (h
->array
, byte_index
);
2143 /* FIXME add checks!!! */
2145 bytevector_set_c32 (SCM bv
, SCM idx
, SCM val
)
2146 { float *contents
= (float*)SCM_BYTEVECTOR_CONTENTS (bv
);
2147 size_t i
= scm_to_size_t (idx
);
2148 contents
[i
/4] = scm_c_real_part (val
);
2149 contents
[i
/4 + 1] = scm_c_imag_part (val
);
2150 return SCM_UNSPECIFIED
;
2154 bytevector_set_c64 (SCM bv
, SCM idx
, SCM val
)
2155 { double *contents
= (double*)SCM_BYTEVECTOR_CONTENTS (bv
);
2156 size_t i
= scm_to_size_t (idx
);
2157 contents
[i
/8] = scm_c_real_part (val
);
2158 contents
[i
/8 + 1] = scm_c_imag_part (val
);
2159 return SCM_UNSPECIFIED
;
2162 typedef SCM (*scm_t_bytevector_set_fn
)(SCM
, SCM
, SCM
);
2164 const scm_t_bytevector_set_fn bytevector_set_fns
[SCM_ARRAY_ELEMENT_TYPE_LAST
+ 1] =
2169 scm_bytevector_u8_set_x
, /* VU8 */
2170 scm_bytevector_u8_set_x
, /* U8 */
2171 scm_bytevector_s8_set_x
,
2172 scm_bytevector_u16_native_set_x
,
2173 scm_bytevector_s16_native_set_x
,
2174 scm_bytevector_u32_native_set_x
,
2175 scm_bytevector_s32_native_set_x
,
2176 scm_bytevector_u64_native_set_x
,
2177 scm_bytevector_s64_native_set_x
,
2178 scm_bytevector_ieee_single_native_set_x
,
2179 scm_bytevector_ieee_double_native_set_x
,
2185 bv_handle_set_x (scm_t_array_handle
*h
, size_t index
, SCM val
)
2188 scm_t_bytevector_set_fn set_fn
;
2190 set_fn
= bytevector_set_fns
[h
->element_type
];
2192 scm_from_size_t (index
* scm_array_handle_uniform_element_size (h
));
2193 set_fn (h
->array
, byte_index
, val
);
2197 bytevector_get_handle (SCM v
, scm_t_array_handle
*h
)
2203 h
->dim0
.ubnd
= SCM_BYTEVECTOR_TYPED_LENGTH (v
) - 1;
2205 h
->element_type
= SCM_BYTEVECTOR_ELEMENT_TYPE (v
);
2206 h
->elements
= h
->writable_elements
= SCM_BYTEVECTOR_CONTENTS (v
);
2210 /* Initialization. */
2213 scm_bootstrap_bytevectors (void)
2215 /* This must be instantiated here because the generalized-vector API may
2216 want to access bytevectors even though `(rnrs bytevector)' hasn't been
2218 scm_null_bytevector
= make_bytevector (0, SCM_ARRAY_ELEMENT_TYPE_VU8
);
2220 #ifdef WORDS_BIGENDIAN
2221 scm_i_native_endianness
= scm_from_locale_symbol ("big");
2223 scm_i_native_endianness
= scm_from_locale_symbol ("little");
2226 scm_c_register_extension ("libguile", "scm_init_bytevectors",
2227 (scm_t_extension_init_func
) scm_init_bytevectors
,
2231 scm_t_array_implementation impl
;
2233 impl
.tag
= scm_tc7_bytevector
;
2235 impl
.vref
= bv_handle_ref
;
2236 impl
.vset
= bv_handle_set_x
;
2237 impl
.get_handle
= bytevector_get_handle
;
2238 scm_i_register_array_implementation (&impl
);
2239 scm_i_register_vector_constructor
2240 (scm_i_array_element_types
[SCM_ARRAY_ELEMENT_TYPE_VU8
],
2241 scm_make_bytevector
);
2246 scm_init_bytevectors (void)
2248 #include "libguile/bytevectors.x"
2250 scm_endianness_big
= scm_sym_big
;
2251 scm_endianness_little
= scm_sym_little
;