1 /* Copyright (C) 2009-2015 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>
54 /* Convenience macros. These are used by the various templates (macros) that
55 are parameterized by integer signedness. */
56 #define INT8_T_signed scm_t_int8
57 #define INT8_T_unsigned scm_t_uint8
58 #define INT16_T_signed scm_t_int16
59 #define INT16_T_unsigned scm_t_uint16
60 #define INT32_T_signed scm_t_int32
61 #define INT32_T_unsigned scm_t_uint32
62 #define is_signed_int8(_x) (((_x) >= -128L) && ((_x) <= 127L))
63 #define is_unsigned_int8(_x) ((_x) <= 255UL)
64 #define is_signed_int16(_x) (((_x) >= -32768L) && ((_x) <= 32767L))
65 #define is_unsigned_int16(_x) ((_x) <= 65535UL)
66 #define is_signed_int32(_x) (((_x) >= -2147483648L) && ((_x) <= 2147483647L))
67 #define is_unsigned_int32(_x) ((_x) <= 4294967295UL)
68 #define SIGNEDNESS_signed 1
69 #define SIGNEDNESS_unsigned 0
71 #define INT_TYPE(_size, _sign) INT ## _size ## _T_ ## _sign
72 #define INT_SWAP(_size) bswap_ ## _size
73 #define INT_VALID_P(_size, _sign) is_ ## _sign ## _int ## _size
74 #define SIGNEDNESS(_sign) SIGNEDNESS_ ## _sign
77 #define INTEGER_ACCESSOR_PROLOGUE(_len, _sign) \
78 size_t c_len, c_index; \
81 SCM_VALIDATE_BYTEVECTOR (1, bv); \
82 c_index = scm_to_uint (index); \
84 c_len = SCM_BYTEVECTOR_LENGTH (bv); \
85 c_bv = (_sign char *) SCM_BYTEVECTOR_CONTENTS (bv); \
87 if (SCM_UNLIKELY (c_index + ((_len) >> 3UL) - 1 >= c_len)) \
88 scm_out_of_range (FUNC_NAME, index);
90 /* Template for fixed-size integer access (only 8, 16 or 32-bit). */
91 #define INTEGER_REF(_len, _sign) \
94 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
95 SCM_VALIDATE_SYMBOL (3, endianness); \
98 INT_TYPE (_len, _sign) c_result; \
100 memcpy (&c_result, &c_bv[c_index], (_len) / 8); \
101 if (!scm_is_eq (endianness, scm_i_native_endianness)) \
102 c_result = INT_SWAP (_len) (c_result); \
104 result = SCM_I_MAKINUM (c_result); \
109 /* Template for fixed-size integer access using the native endianness. */
110 #define INTEGER_NATIVE_REF(_len, _sign) \
113 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
116 INT_TYPE (_len, _sign) c_result; \
118 memcpy (&c_result, &c_bv[c_index], (_len) / 8); \
119 result = SCM_I_MAKINUM (c_result); \
124 /* Template for fixed-size integer modification (only 8, 16 or 32-bit). */
125 #define INTEGER_SET(_len, _sign) \
126 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
127 SCM_VALIDATE_SYMBOL (3, endianness); \
130 scm_t_signed_bits c_value; \
131 INT_TYPE (_len, _sign) c_value_short; \
133 if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
134 scm_wrong_type_arg (FUNC_NAME, 3, value); \
136 c_value = SCM_I_INUM (value); \
137 if (SCM_UNLIKELY (!INT_VALID_P (_len, _sign) (c_value))) \
138 scm_out_of_range (FUNC_NAME, value); \
140 c_value_short = (INT_TYPE (_len, _sign)) c_value; \
141 if (!scm_is_eq (endianness, scm_i_native_endianness)) \
142 c_value_short = INT_SWAP (_len) (c_value_short); \
144 memcpy (&c_bv[c_index], &c_value_short, (_len) / 8); \
147 return SCM_UNSPECIFIED;
149 /* Template for fixed-size integer modification using the native
151 #define INTEGER_NATIVE_SET(_len, _sign) \
152 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
155 scm_t_signed_bits c_value; \
156 INT_TYPE (_len, _sign) c_value_short; \
158 if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
159 scm_wrong_type_arg (FUNC_NAME, 3, value); \
161 c_value = SCM_I_INUM (value); \
162 if (SCM_UNLIKELY (!INT_VALID_P (_len, _sign) (c_value))) \
163 scm_out_of_range (FUNC_NAME, value); \
165 c_value_short = (INT_TYPE (_len, _sign)) c_value; \
167 memcpy (&c_bv[c_index], &c_value_short, (_len) / 8); \
170 return SCM_UNSPECIFIED;
174 /* Bytevector type. */
176 #define SCM_BYTEVECTOR_HEADER_BYTES \
177 (SCM_BYTEVECTOR_HEADER_SIZE * sizeof (scm_t_bits))
179 #define SCM_BYTEVECTOR_SET_LENGTH(_bv, _len) \
180 SCM_SET_CELL_WORD_1 ((_bv), (scm_t_bits) (_len))
181 #define SCM_BYTEVECTOR_SET_CONTENTS(_bv, _contents) \
182 SCM_SET_CELL_WORD_2 ((_bv), (scm_t_bits) (_contents))
183 #define SCM_BYTEVECTOR_SET_CONTIGUOUS_P(bv, contiguous_p) \
184 SCM_SET_BYTEVECTOR_FLAGS ((bv), \
185 SCM_BYTEVECTOR_ELEMENT_TYPE (bv) \
186 | ((contiguous_p) << 8UL))
188 #define SCM_BYTEVECTOR_SET_ELEMENT_TYPE(bv, hint) \
189 SCM_SET_BYTEVECTOR_FLAGS ((bv), \
191 | (SCM_BYTEVECTOR_CONTIGUOUS_P (bv) << 8UL))
192 #define SCM_BYTEVECTOR_SET_PARENT(_bv, _parent) \
193 SCM_SET_CELL_OBJECT_3 ((_bv), (_parent))
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
>= (((size_t) -1)
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
= SCM_PACK_POINTER (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
);
235 SCM_BYTEVECTOR_SET_PARENT (ret
, SCM_BOOL_F
);
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
= SCM_PACK_POINTER (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
);
265 SCM_BYTEVECTOR_SET_PARENT (ret
, SCM_BOOL_F
);
272 /* Return a new bytevector of size LEN octets. */
274 scm_c_make_bytevector (size_t len
)
276 return make_bytevector (len
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
279 /* Return a new bytevector of size LEN elements. */
281 scm_i_make_typed_bytevector (size_t len
, scm_t_array_element_type element_type
)
283 return make_bytevector (len
, element_type
);
286 /* Return a bytevector of size LEN made up of CONTENTS. The area
287 pointed to by CONTENTS must be protected from GC somehow: either
288 because it was allocated using `scm_gc_malloc ()', or because it is
291 scm_c_take_gc_bytevector (signed char *contents
, size_t len
, SCM parent
)
295 ret
= make_bytevector_from_buffer (len
, contents
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
296 SCM_BYTEVECTOR_SET_PARENT (ret
, parent
);
302 scm_c_take_typed_bytevector (signed char *contents
, size_t len
,
303 scm_t_array_element_type element_type
, SCM parent
)
307 ret
= make_bytevector_from_buffer (len
, contents
, element_type
);
308 SCM_BYTEVECTOR_SET_PARENT (ret
, parent
);
313 /* Shrink BV to C_NEW_LEN (which is assumed to be smaller than its current
314 size) and return the new bytevector (possibly different from BV). */
316 scm_c_shrink_bytevector (SCM bv
, size_t c_new_len
)
321 if (SCM_UNLIKELY (c_new_len
% SCM_BYTEVECTOR_TYPE_SIZE (bv
)))
322 /* This would be an internal Guile programming error */
325 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
326 if (SCM_UNLIKELY (c_new_len
> c_len
))
329 SCM_BYTEVECTOR_SET_LENGTH (bv
, c_new_len
);
331 if (SCM_BYTEVECTOR_CONTIGUOUS_P (bv
))
335 c_bv
= scm_gc_realloc (SCM2PTR (bv
),
336 c_len
+ SCM_BYTEVECTOR_HEADER_BYTES
,
337 c_new_len
+ SCM_BYTEVECTOR_HEADER_BYTES
,
339 new_bv
= PTR2SCM (c_bv
);
340 SCM_BYTEVECTOR_SET_CONTENTS (new_bv
, c_bv
+ SCM_BYTEVECTOR_HEADER_BYTES
);
346 c_bv
= scm_gc_realloc (SCM_BYTEVECTOR_CONTENTS (bv
),
347 c_len
, c_new_len
, SCM_GC_BYTEVECTOR
);
348 SCM_BYTEVECTOR_SET_CONTENTS (bv
, c_bv
);
357 scm_is_bytevector (SCM obj
)
359 return SCM_BYTEVECTOR_P (obj
);
363 scm_c_bytevector_length (SCM bv
)
364 #define FUNC_NAME "scm_c_bytevector_length"
366 SCM_VALIDATE_BYTEVECTOR (1, bv
);
368 return SCM_BYTEVECTOR_LENGTH (bv
);
373 scm_c_bytevector_ref (SCM bv
, size_t index
)
374 #define FUNC_NAME "scm_c_bytevector_ref"
377 const scm_t_uint8
*c_bv
;
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
));
392 scm_c_bytevector_set_x (SCM bv
, size_t index
, scm_t_uint8 value
)
393 #define FUNC_NAME "scm_c_bytevector_set_x"
398 SCM_VALIDATE_BYTEVECTOR (1, bv
);
400 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
401 c_bv
= (scm_t_uint8
*) SCM_BYTEVECTOR_CONTENTS (bv
);
403 if (SCM_UNLIKELY (index
>= c_len
))
404 scm_out_of_range (FUNC_NAME
, scm_from_size_t (index
));
413 scm_i_print_bytevector (SCM bv
, SCM port
, scm_print_state
*pstate SCM_UNUSED
)
415 ssize_t ubnd
, inc
, i
;
416 scm_t_array_handle h
;
418 scm_array_get_handle (bv
, &h
);
420 scm_putc_unlocked ('#', port
);
421 scm_write (scm_array_handle_element_type (&h
), port
);
422 scm_putc_unlocked ('(', port
);
423 for (i
= h
.dims
[0].lbnd
, ubnd
= h
.dims
[0].ubnd
, inc
= h
.dims
[0].inc
;
427 scm_putc_unlocked (' ', port
);
428 scm_write (scm_array_handle_ref (&h
, i
), port
);
430 scm_putc_unlocked (')', port
);
436 /* General operations. */
438 SCM_SYMBOL (scm_sym_big
, "big");
439 SCM_SYMBOL (scm_sym_little
, "little");
441 SCM scm_endianness_big
, scm_endianness_little
;
443 /* Host endianness (a symbol). */
444 SCM scm_i_native_endianness
= SCM_UNSPECIFIED
;
448 # define bswap_24(_x) \
449 ((((_x) & 0xff0000) >> 16) | \
450 (((_x) & 0x00ff00)) | \
451 (((_x) & 0x0000ff) << 16))
455 SCM_DEFINE (scm_native_endianness
, "native-endianness", 0, 0, 0,
457 "Return a symbol denoting the machine's native endianness.")
458 #define FUNC_NAME s_scm_native_endianness
460 return scm_i_native_endianness
;
464 SCM_DEFINE (scm_bytevector_p
, "bytevector?", 1, 0, 0,
466 "Return true if @var{obj} is a bytevector.")
467 #define FUNC_NAME s_scm_bytevector_p
469 return scm_from_bool (scm_is_bytevector (obj
));
473 SCM_DEFINE (scm_make_bytevector
, "make-bytevector", 1, 1, 0,
475 "Return a newly allocated bytevector of @var{len} bytes, "
476 "optionally filled with @var{fill}.")
477 #define FUNC_NAME s_scm_make_bytevector
481 scm_t_uint8 c_fill
= 0;
483 SCM_VALIDATE_SIZE_COPY (1, len
, c_len
);
484 if (!scm_is_eq (fill
, SCM_UNDEFINED
))
488 value
= scm_to_int (fill
);
489 if (SCM_UNLIKELY ((value
< -128) || (value
> 255)))
490 scm_out_of_range (FUNC_NAME
, fill
);
491 c_fill
= (scm_t_uint8
) value
;
494 bv
= make_bytevector (c_len
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
495 if (!scm_is_eq (fill
, SCM_UNDEFINED
))
498 scm_t_uint8
*contents
;
500 contents
= (scm_t_uint8
*) SCM_BYTEVECTOR_CONTENTS (bv
);
501 for (i
= 0; i
< c_len
; i
++)
502 contents
[i
] = c_fill
;
505 memset (SCM_BYTEVECTOR_CONTENTS (bv
), 0, c_len
);
511 SCM_DEFINE (scm_bytevector_length
, "bytevector-length", 1, 0, 0,
513 "Return the length (in bytes) of @var{bv}.")
514 #define FUNC_NAME s_scm_bytevector_length
516 return scm_from_uint (scm_c_bytevector_length (bv
));
520 SCM_DEFINE (scm_bytevector_eq_p
, "bytevector=?", 2, 0, 0,
522 "Return is @var{bv1} equals to @var{bv2}---i.e., if they "
523 "have the same length and contents.")
524 #define FUNC_NAME s_scm_bytevector_eq_p
526 SCM result
= SCM_BOOL_F
;
527 size_t c_len1
, c_len2
;
529 SCM_VALIDATE_BYTEVECTOR (1, bv1
);
530 SCM_VALIDATE_BYTEVECTOR (2, bv2
);
532 c_len1
= SCM_BYTEVECTOR_LENGTH (bv1
);
533 c_len2
= SCM_BYTEVECTOR_LENGTH (bv2
);
535 if (c_len1
== c_len2
&& (SCM_BYTEVECTOR_ELEMENT_TYPE (bv1
)
536 == SCM_BYTEVECTOR_ELEMENT_TYPE (bv2
)))
538 signed char *c_bv1
, *c_bv2
;
540 c_bv1
= SCM_BYTEVECTOR_CONTENTS (bv1
);
541 c_bv2
= SCM_BYTEVECTOR_CONTENTS (bv2
);
543 result
= scm_from_bool (!memcmp (c_bv1
, c_bv2
, c_len1
));
550 SCM_DEFINE (scm_bytevector_fill_x
, "bytevector-fill!", 2, 0, 0,
552 "Fill bytevector @var{bv} with @var{fill}, a byte.")
553 #define FUNC_NAME s_scm_bytevector_fill_x
556 scm_t_uint8
*c_bv
, c_fill
;
559 SCM_VALIDATE_BYTEVECTOR (1, bv
);
561 value
= scm_to_int (fill
);
562 if (SCM_UNLIKELY ((value
< -128) || (value
> 255)))
563 scm_out_of_range (FUNC_NAME
, fill
);
564 c_fill
= (scm_t_uint8
) value
;
566 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
567 c_bv
= (scm_t_uint8
*) SCM_BYTEVECTOR_CONTENTS (bv
);
569 for (i
= 0; i
< c_len
; i
++)
572 return SCM_UNSPECIFIED
;
576 SCM_DEFINE (scm_bytevector_copy_x
, "bytevector-copy!", 5, 0, 0,
577 (SCM source
, SCM source_start
, SCM target
, SCM target_start
,
579 "Copy @var{len} bytes from @var{source} into @var{target}, "
580 "starting reading from @var{source_start} (a positive index "
581 "within @var{source}) and start writing at "
582 "@var{target_start}.")
583 #define FUNC_NAME s_scm_bytevector_copy_x
585 size_t c_len
, c_source_len
, c_target_len
;
586 size_t c_source_start
, c_target_start
;
587 signed char *c_source
, *c_target
;
589 SCM_VALIDATE_BYTEVECTOR (1, source
);
590 SCM_VALIDATE_BYTEVECTOR (3, target
);
592 c_len
= scm_to_size_t (len
);
593 c_source_start
= scm_to_size_t (source_start
);
594 c_target_start
= scm_to_size_t (target_start
);
596 c_source
= SCM_BYTEVECTOR_CONTENTS (source
);
597 c_target
= SCM_BYTEVECTOR_CONTENTS (target
);
598 c_source_len
= SCM_BYTEVECTOR_LENGTH (source
);
599 c_target_len
= SCM_BYTEVECTOR_LENGTH (target
);
601 if (SCM_UNLIKELY (c_source_start
+ c_len
> c_source_len
))
602 scm_out_of_range (FUNC_NAME
, source_start
);
603 if (SCM_UNLIKELY (c_target_start
+ c_len
> c_target_len
))
604 scm_out_of_range (FUNC_NAME
, target_start
);
606 memmove (c_target
+ c_target_start
,
607 c_source
+ c_source_start
,
610 return SCM_UNSPECIFIED
;
614 SCM_DEFINE (scm_bytevector_copy
, "bytevector-copy", 1, 0, 0,
616 "Return a newly allocated copy of @var{bv}.")
617 #define FUNC_NAME s_scm_bytevector_copy
621 signed char *c_bv
, *c_copy
;
623 SCM_VALIDATE_BYTEVECTOR (1, bv
);
625 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
626 c_bv
= SCM_BYTEVECTOR_CONTENTS (bv
);
628 copy
= make_bytevector (c_len
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
629 c_copy
= SCM_BYTEVECTOR_CONTENTS (copy
);
630 memcpy (c_copy
, c_bv
, c_len
);
636 SCM_DEFINE (scm_uniform_array_to_bytevector
, "uniform-array->bytevector",
637 1, 0, 0, (SCM array
),
638 "Return a newly allocated bytevector whose contents\n"
639 "will be copied from the uniform array @var{array}.")
640 #define FUNC_NAME s_scm_uniform_array_to_bytevector
643 size_t len
, sz
, byte_len
;
644 scm_t_array_handle h
;
647 contents
= scm_array_contents (array
, SCM_BOOL_T
);
648 if (scm_is_false (contents
))
649 scm_wrong_type_arg_msg (FUNC_NAME
, 0, array
, "uniform contiguous array");
651 scm_array_get_handle (contents
, &h
);
652 assert (h
.base
== 0);
655 len
= h
.dims
->inc
* (h
.dims
->ubnd
- h
.dims
->lbnd
+ 1);
656 sz
= scm_array_handle_uniform_element_bit_size (&h
);
657 if (sz
>= 8 && ((sz
% 8) == 0))
658 byte_len
= len
* (sz
/ 8);
660 /* Elements of sub-byte size (bitvectors) are addressed in 32-bit
662 byte_len
= ((len
* sz
+ 31) / 32) * 4;
664 /* an internal guile error, really */
665 SCM_MISC_ERROR ("uniform elements larger than 8 bits must fill whole bytes", SCM_EOL
);
667 ret
= make_bytevector (byte_len
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
668 memcpy (SCM_BYTEVECTOR_CONTENTS (ret
), elts
, byte_len
);
670 scm_array_handle_release (&h
);
677 /* Operations on bytes and octets. */
679 SCM_DEFINE (scm_bytevector_u8_ref
, "bytevector-u8-ref", 2, 0, 0,
681 "Return the octet located at @var{index} in @var{bv}.")
682 #define FUNC_NAME s_scm_bytevector_u8_ref
684 INTEGER_NATIVE_REF (8, unsigned);
688 SCM_DEFINE (scm_bytevector_s8_ref
, "bytevector-s8-ref", 2, 0, 0,
690 "Return the byte located at @var{index} in @var{bv}.")
691 #define FUNC_NAME s_scm_bytevector_s8_ref
693 INTEGER_NATIVE_REF (8, signed);
697 SCM_DEFINE (scm_bytevector_u8_set_x
, "bytevector-u8-set!", 3, 0, 0,
698 (SCM bv
, SCM index
, SCM value
),
699 "Return the octet located at @var{index} in @var{bv}.")
700 #define FUNC_NAME s_scm_bytevector_u8_set_x
702 INTEGER_NATIVE_SET (8, unsigned);
706 SCM_DEFINE (scm_bytevector_s8_set_x
, "bytevector-s8-set!", 3, 0, 0,
707 (SCM bv
, SCM index
, SCM value
),
708 "Return the octet located at @var{index} in @var{bv}.")
709 #define FUNC_NAME s_scm_bytevector_s8_set_x
711 INTEGER_NATIVE_SET (8, signed);
715 #undef OCTET_ACCESSOR_PROLOGUE
718 SCM_DEFINE (scm_bytevector_to_u8_list
, "bytevector->u8-list", 1, 0, 0,
720 "Return a newly allocated list of octets containing the "
721 "contents of @var{bv}.")
722 #define FUNC_NAME s_scm_bytevector_to_u8_list
728 SCM_VALIDATE_BYTEVECTOR (1, bv
);
730 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
731 c_bv
= (scm_t_uint8
*) SCM_BYTEVECTOR_CONTENTS (bv
);
733 lst
= scm_make_list (scm_from_size_t (c_len
), SCM_UNSPECIFIED
);
734 for (i
= 0, pair
= lst
;
736 i
++, pair
= SCM_CDR (pair
))
738 SCM_SETCAR (pair
, SCM_I_MAKINUM (c_bv
[i
]));
745 SCM_DEFINE (scm_u8_list_to_bytevector
, "u8-list->bytevector", 1, 0, 0,
747 "Turn @var{lst}, a list of octets, into a bytevector.")
748 #define FUNC_NAME s_scm_u8_list_to_bytevector
754 SCM_VALIDATE_LIST_COPYLEN (1, lst
, c_len
);
756 bv
= make_bytevector (c_len
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
757 c_bv
= (scm_t_uint8
*) SCM_BYTEVECTOR_CONTENTS (bv
);
759 for (i
= 0; i
< c_len
; lst
= SCM_CDR (lst
), i
++)
761 item
= SCM_CAR (lst
);
763 if (SCM_LIKELY (SCM_I_INUMP (item
)))
765 scm_t_signed_bits c_item
;
767 c_item
= SCM_I_INUM (item
);
768 if (SCM_LIKELY ((c_item
>= 0) && (c_item
< 256)))
769 c_bv
[i
] = (scm_t_uint8
) c_item
;
780 scm_wrong_type_arg (FUNC_NAME
, 1, item
);
786 /* Compute the two's complement of VALUE (a positive integer) on SIZE octets
787 using (2^(SIZE * 8) - VALUE). */
789 twos_complement (mpz_t value
, size_t size
)
791 unsigned long bit_count
;
793 /* We expect BIT_COUNT to fit in a unsigned long thanks to the range
794 checking on SIZE performed earlier. */
795 bit_count
= (unsigned long) size
<< 3UL;
797 if (SCM_LIKELY (bit_count
< sizeof (unsigned long)))
798 mpz_ui_sub (value
, 1UL << bit_count
, value
);
804 mpz_ui_pow_ui (max
, 2, bit_count
);
805 mpz_sub (value
, max
, value
);
811 bytevector_large_ref (const char *c_bv
, size_t c_size
, int signed_p
,
816 int c_endianness
, negative_p
= 0;
820 if (scm_is_eq (endianness
, scm_sym_big
))
821 negative_p
= c_bv
[0] & 0x80;
823 negative_p
= c_bv
[c_size
- 1] & 0x80;
826 c_endianness
= scm_is_eq (endianness
, scm_sym_big
) ? 1 : -1;
829 mpz_import (c_mpz
, 1 /* 1 word */, 1 /* word order doesn't matter */,
830 c_size
/* word is C_SIZE-byte long */,
832 0 /* nails */, c_bv
);
834 if (signed_p
&& negative_p
)
836 twos_complement (c_mpz
, c_size
);
837 mpz_neg (c_mpz
, c_mpz
);
840 result
= scm_from_mpz (c_mpz
);
841 mpz_clear (c_mpz
); /* FIXME: Needed? */
847 bytevector_large_set (char *c_bv
, size_t c_size
, int signed_p
,
848 SCM value
, SCM endianness
)
851 int c_endianness
, c_sign
, err
= 0;
853 c_endianness
= scm_is_eq (endianness
, scm_sym_big
) ? 1 : -1;
856 scm_to_mpz (value
, c_mpz
);
858 c_sign
= mpz_sgn (c_mpz
);
861 if (SCM_LIKELY (signed_p
))
863 mpz_neg (c_mpz
, c_mpz
);
864 twos_complement (c_mpz
, c_size
);
875 memset (c_bv
, 0, c_size
);
878 size_t word_count
, value_size
;
880 value_size
= (mpz_sizeinbase (c_mpz
, 2) + (8 * c_size
)) / (8 * c_size
);
881 if (SCM_UNLIKELY (value_size
> c_size
))
888 mpz_export (c_bv
, &word_count
, 1 /* word order doesn't matter */,
889 c_size
, c_endianness
,
890 0 /* nails */, c_mpz
);
891 if (SCM_UNLIKELY (word_count
!= 1))
892 /* Shouldn't happen since we already checked with VALUE_SIZE. */
902 #define GENERIC_INTEGER_ACCESSOR_PROLOGUE(_sign) \
903 size_t c_len, c_index, c_size; \
906 SCM_VALIDATE_BYTEVECTOR (1, bv); \
907 c_index = scm_to_size_t (index); \
908 c_size = scm_to_size_t (size); \
910 c_len = SCM_BYTEVECTOR_LENGTH (bv); \
911 c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
913 /* C_SIZE must have its 3 higher bits set to zero so that \
914 multiplying it by 8 yields a number that fits in a \
916 if (SCM_UNLIKELY (c_size == 0 || c_size >= (SIZE_MAX >> 3))) \
917 scm_out_of_range (FUNC_NAME, size); \
918 if (SCM_UNLIKELY (c_index + c_size > c_len)) \
919 scm_out_of_range (FUNC_NAME, index);
922 /* Template of an integer reference function. */
923 #define GENERIC_INTEGER_REF(_sign) \
931 swap = !scm_is_eq (endianness, scm_i_native_endianness); \
936 _sign char c_value8; \
937 memcpy (&c_value8, c_bv, 1); \
943 INT_TYPE (16, _sign) c_value16; \
944 memcpy (&c_value16, c_bv, 2); \
946 value = (INT_TYPE (16, _sign)) bswap_16 (c_value16); \
955 result = SCM_I_MAKINUM ((_sign int) value); \
958 result = bytevector_large_ref ((char *) c_bv, \
959 c_size, SIGNEDNESS (_sign), \
965 bytevector_signed_ref (const char *c_bv
, size_t c_size
, SCM endianness
)
967 GENERIC_INTEGER_REF (signed);
971 bytevector_unsigned_ref (const char *c_bv
, size_t c_size
, SCM endianness
)
973 GENERIC_INTEGER_REF (unsigned);
977 /* Template of an integer assignment function. */
978 #define GENERIC_INTEGER_SET(_sign) \
981 scm_t_signed_bits c_value; \
983 if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
986 c_value = SCM_I_INUM (value); \
990 if (SCM_LIKELY (INT_VALID_P (8, _sign) (c_value))) \
992 _sign char c_value8; \
993 c_value8 = (_sign char) c_value; \
994 memcpy (c_bv, &c_value8, 1); \
1001 if (SCM_LIKELY (INT_VALID_P (16, _sign) (c_value))) \
1004 INT_TYPE (16, _sign) c_value16; \
1006 swap = !scm_is_eq (endianness, scm_i_native_endianness); \
1009 c_value16 = (INT_TYPE (16, _sign)) bswap_16 (c_value); \
1011 c_value16 = c_value; \
1013 memcpy (c_bv, &c_value16, 2); \
1027 err = bytevector_large_set (c_bv, c_size, \
1028 SIGNEDNESS (_sign), \
1029 value, endianness); \
1037 scm_out_of_range (FUNC_NAME, value); \
1041 bytevector_signed_set (char *c_bv
, size_t c_size
,
1042 SCM value
, SCM endianness
,
1043 const char *func_name
)
1044 #define FUNC_NAME func_name
1046 GENERIC_INTEGER_SET (signed);
1051 bytevector_unsigned_set (char *c_bv
, size_t c_size
,
1052 SCM value
, SCM endianness
,
1053 const char *func_name
)
1054 #define FUNC_NAME func_name
1056 GENERIC_INTEGER_SET (unsigned);
1060 #undef GENERIC_INTEGER_SET
1061 #undef GENERIC_INTEGER_REF
1064 SCM_DEFINE (scm_bytevector_uint_ref
, "bytevector-uint-ref", 4, 0, 0,
1065 (SCM bv
, SCM index
, SCM endianness
, SCM size
),
1066 "Return the @var{size}-octet long unsigned integer at index "
1067 "@var{index} in @var{bv}.")
1068 #define FUNC_NAME s_scm_bytevector_uint_ref
1070 GENERIC_INTEGER_ACCESSOR_PROLOGUE (unsigned);
1072 return (bytevector_unsigned_ref (&c_bv
[c_index
], c_size
, endianness
));
1076 SCM_DEFINE (scm_bytevector_sint_ref
, "bytevector-sint-ref", 4, 0, 0,
1077 (SCM bv
, SCM index
, SCM endianness
, SCM size
),
1078 "Return the @var{size}-octet long unsigned integer at index "
1079 "@var{index} in @var{bv}.")
1080 #define FUNC_NAME s_scm_bytevector_sint_ref
1082 GENERIC_INTEGER_ACCESSOR_PROLOGUE (signed);
1084 return (bytevector_signed_ref (&c_bv
[c_index
], c_size
, endianness
));
1088 SCM_DEFINE (scm_bytevector_uint_set_x
, "bytevector-uint-set!", 5, 0, 0,
1089 (SCM bv
, SCM index
, SCM value
, SCM endianness
, SCM size
),
1090 "Set the @var{size}-octet long unsigned integer at @var{index} "
1092 #define FUNC_NAME s_scm_bytevector_uint_set_x
1094 GENERIC_INTEGER_ACCESSOR_PROLOGUE (unsigned);
1096 bytevector_unsigned_set (&c_bv
[c_index
], c_size
, value
, endianness
,
1099 return SCM_UNSPECIFIED
;
1103 SCM_DEFINE (scm_bytevector_sint_set_x
, "bytevector-sint-set!", 5, 0, 0,
1104 (SCM bv
, SCM index
, SCM value
, SCM endianness
, SCM size
),
1105 "Set the @var{size}-octet long signed integer at @var{index} "
1107 #define FUNC_NAME s_scm_bytevector_sint_set_x
1109 GENERIC_INTEGER_ACCESSOR_PROLOGUE (signed);
1111 bytevector_signed_set (&c_bv
[c_index
], c_size
, value
, endianness
,
1114 return SCM_UNSPECIFIED
;
1120 /* Operations on integers of arbitrary size. */
1122 #define INTEGERS_TO_LIST(_sign) \
1124 size_t i, c_len, c_size; \
1126 SCM_VALIDATE_BYTEVECTOR (1, bv); \
1127 SCM_VALIDATE_SYMBOL (2, endianness); \
1128 c_size = scm_to_unsigned_integer (size, 1, (size_t) -1); \
1130 c_len = SCM_BYTEVECTOR_LENGTH (bv); \
1131 if (SCM_UNLIKELY (c_len % c_size != 0)) \
1132 scm_wrong_type_arg_msg \
1133 (FUNC_NAME, 0, size, \
1134 "an exact positive integer that divides the bytevector length"); \
1135 else if (SCM_UNLIKELY (c_len == 0)) \
1141 c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
1143 lst = scm_make_list (scm_from_size_t (c_len / c_size), \
1145 for (i = 0, pair = lst; \
1146 i <= c_len - c_size; \
1147 i += c_size, c_bv += c_size, pair = SCM_CDR (pair)) \
1150 bytevector_ ## _sign ## _ref (c_bv, c_size, \
1157 SCM_DEFINE (scm_bytevector_to_sint_list
, "bytevector->sint-list",
1159 (SCM bv
, SCM endianness
, SCM size
),
1160 "Return a list of signed integers of @var{size} octets "
1161 "representing the contents of @var{bv}.")
1162 #define FUNC_NAME s_scm_bytevector_to_sint_list
1164 INTEGERS_TO_LIST (signed);
1168 SCM_DEFINE (scm_bytevector_to_uint_list
, "bytevector->uint-list",
1170 (SCM bv
, SCM endianness
, SCM size
),
1171 "Return a list of unsigned integers of @var{size} octets "
1172 "representing the contents of @var{bv}.")
1173 #define FUNC_NAME s_scm_bytevector_to_uint_list
1175 INTEGERS_TO_LIST (unsigned);
1179 #undef INTEGER_TO_LIST
1182 #define INTEGER_LIST_TO_BYTEVECTOR(_sign) \
1186 char *c_bv, *c_bv_ptr; \
1188 SCM_VALIDATE_LIST_COPYLEN (1, lst, c_len); \
1189 SCM_VALIDATE_SYMBOL (2, endianness); \
1190 c_size = scm_to_size_t (size); \
1192 if (SCM_UNLIKELY (c_size == 0 || c_size >= (SIZE_MAX >> 3))) \
1193 scm_out_of_range (FUNC_NAME, size); \
1195 bv = make_bytevector (c_len * c_size, SCM_ARRAY_ELEMENT_TYPE_VU8); \
1196 c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
1198 for (c_bv_ptr = c_bv; \
1199 !scm_is_null (lst); \
1200 lst = SCM_CDR (lst), c_bv_ptr += c_size) \
1202 bytevector_ ## _sign ## _set (c_bv_ptr, c_size, \
1203 SCM_CAR (lst), endianness, \
1210 SCM_DEFINE (scm_uint_list_to_bytevector
, "uint-list->bytevector",
1212 (SCM lst
, SCM endianness
, SCM size
),
1213 "Return a bytevector containing the unsigned integers "
1214 "listed in @var{lst} and encoded on @var{size} octets "
1215 "according to @var{endianness}.")
1216 #define FUNC_NAME s_scm_uint_list_to_bytevector
1218 INTEGER_LIST_TO_BYTEVECTOR (unsigned);
1222 SCM_DEFINE (scm_sint_list_to_bytevector
, "sint-list->bytevector",
1224 (SCM lst
, SCM endianness
, SCM size
),
1225 "Return a bytevector containing the signed integers "
1226 "listed in @var{lst} and encoded on @var{size} octets "
1227 "according to @var{endianness}.")
1228 #define FUNC_NAME s_scm_sint_list_to_bytevector
1230 INTEGER_LIST_TO_BYTEVECTOR (signed);
1234 #undef INTEGER_LIST_TO_BYTEVECTOR
1238 /* Operations on 16-bit integers. */
1240 SCM_DEFINE (scm_bytevector_u16_ref
, "bytevector-u16-ref",
1242 (SCM bv
, SCM index
, SCM endianness
),
1243 "Return the unsigned 16-bit integer from @var{bv} at "
1245 #define FUNC_NAME s_scm_bytevector_u16_ref
1247 INTEGER_REF (16, unsigned);
1251 SCM_DEFINE (scm_bytevector_s16_ref
, "bytevector-s16-ref",
1253 (SCM bv
, SCM index
, SCM endianness
),
1254 "Return the signed 16-bit integer from @var{bv} at "
1256 #define FUNC_NAME s_scm_bytevector_s16_ref
1258 INTEGER_REF (16, signed);
1262 SCM_DEFINE (scm_bytevector_u16_native_ref
, "bytevector-u16-native-ref",
1264 (SCM bv
, SCM index
),
1265 "Return the unsigned 16-bit integer from @var{bv} at "
1266 "@var{index} using the native endianness.")
1267 #define FUNC_NAME s_scm_bytevector_u16_native_ref
1269 INTEGER_NATIVE_REF (16, unsigned);
1273 SCM_DEFINE (scm_bytevector_s16_native_ref
, "bytevector-s16-native-ref",
1275 (SCM bv
, SCM index
),
1276 "Return the unsigned 16-bit integer from @var{bv} at "
1277 "@var{index} using the native endianness.")
1278 #define FUNC_NAME s_scm_bytevector_s16_native_ref
1280 INTEGER_NATIVE_REF (16, signed);
1284 SCM_DEFINE (scm_bytevector_u16_set_x
, "bytevector-u16-set!",
1286 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1287 "Store @var{value} in @var{bv} at @var{index} according to "
1288 "@var{endianness}.")
1289 #define FUNC_NAME s_scm_bytevector_u16_set_x
1291 INTEGER_SET (16, unsigned);
1295 SCM_DEFINE (scm_bytevector_s16_set_x
, "bytevector-s16-set!",
1297 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1298 "Store @var{value} in @var{bv} at @var{index} according to "
1299 "@var{endianness}.")
1300 #define FUNC_NAME s_scm_bytevector_s16_set_x
1302 INTEGER_SET (16, signed);
1306 SCM_DEFINE (scm_bytevector_u16_native_set_x
, "bytevector-u16-native-set!",
1308 (SCM bv
, SCM index
, SCM value
),
1309 "Store the unsigned integer @var{value} at index @var{index} "
1310 "of @var{bv} using the native endianness.")
1311 #define FUNC_NAME s_scm_bytevector_u16_native_set_x
1313 INTEGER_NATIVE_SET (16, unsigned);
1317 SCM_DEFINE (scm_bytevector_s16_native_set_x
, "bytevector-s16-native-set!",
1319 (SCM bv
, SCM index
, SCM value
),
1320 "Store the signed integer @var{value} at index @var{index} "
1321 "of @var{bv} using the native endianness.")
1322 #define FUNC_NAME s_scm_bytevector_s16_native_set_x
1324 INTEGER_NATIVE_SET (16, signed);
1330 /* Operations on 32-bit integers. */
1332 /* Unfortunately, on 32-bit machines `SCM' is not large enough to hold
1333 arbitrary 32-bit integers. Thus we fall back to using the
1334 `large_{ref,set}' variants on 32-bit machines. */
1336 #define LARGE_INTEGER_REF(_len, _sign) \
1337 INTEGER_ACCESSOR_PROLOGUE(_len, _sign); \
1338 SCM_VALIDATE_SYMBOL (3, endianness); \
1340 return (bytevector_large_ref ((char *) c_bv + c_index, _len / 8, \
1341 SIGNEDNESS (_sign), endianness));
1343 #define LARGE_INTEGER_SET(_len, _sign) \
1345 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
1346 SCM_VALIDATE_SYMBOL (4, endianness); \
1348 err = bytevector_large_set ((char *) c_bv + c_index, _len / 8, \
1349 SIGNEDNESS (_sign), value, endianness); \
1350 if (SCM_UNLIKELY (err)) \
1351 scm_out_of_range (FUNC_NAME, value); \
1353 return SCM_UNSPECIFIED;
1355 #define LARGE_INTEGER_NATIVE_REF(_len, _sign) \
1356 INTEGER_ACCESSOR_PROLOGUE(_len, _sign); \
1357 return (bytevector_large_ref ((char *) c_bv + c_index, _len / 8, \
1358 SIGNEDNESS (_sign), scm_i_native_endianness));
1360 #define LARGE_INTEGER_NATIVE_SET(_len, _sign) \
1362 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
1364 err = bytevector_large_set ((char *) c_bv + c_index, _len / 8, \
1365 SIGNEDNESS (_sign), value, \
1366 scm_i_native_endianness); \
1367 if (SCM_UNLIKELY (err)) \
1368 scm_out_of_range (FUNC_NAME, value); \
1370 return SCM_UNSPECIFIED;
1373 SCM_DEFINE (scm_bytevector_u32_ref
, "bytevector-u32-ref",
1375 (SCM bv
, SCM index
, SCM endianness
),
1376 "Return the unsigned 32-bit integer from @var{bv} at "
1378 #define FUNC_NAME s_scm_bytevector_u32_ref
1380 #if SIZEOF_VOID_P > 4
1381 INTEGER_REF (32, unsigned);
1383 LARGE_INTEGER_REF (32, unsigned);
1388 SCM_DEFINE (scm_bytevector_s32_ref
, "bytevector-s32-ref",
1390 (SCM bv
, SCM index
, SCM endianness
),
1391 "Return the signed 32-bit integer from @var{bv} at "
1393 #define FUNC_NAME s_scm_bytevector_s32_ref
1395 #if SIZEOF_VOID_P > 4
1396 INTEGER_REF (32, signed);
1398 LARGE_INTEGER_REF (32, signed);
1403 SCM_DEFINE (scm_bytevector_u32_native_ref
, "bytevector-u32-native-ref",
1405 (SCM bv
, SCM index
),
1406 "Return the unsigned 32-bit integer from @var{bv} at "
1407 "@var{index} using the native endianness.")
1408 #define FUNC_NAME s_scm_bytevector_u32_native_ref
1410 #if SIZEOF_VOID_P > 4
1411 INTEGER_NATIVE_REF (32, unsigned);
1413 LARGE_INTEGER_NATIVE_REF (32, unsigned);
1418 SCM_DEFINE (scm_bytevector_s32_native_ref
, "bytevector-s32-native-ref",
1420 (SCM bv
, SCM index
),
1421 "Return the unsigned 32-bit integer from @var{bv} at "
1422 "@var{index} using the native endianness.")
1423 #define FUNC_NAME s_scm_bytevector_s32_native_ref
1425 #if SIZEOF_VOID_P > 4
1426 INTEGER_NATIVE_REF (32, signed);
1428 LARGE_INTEGER_NATIVE_REF (32, signed);
1433 SCM_DEFINE (scm_bytevector_u32_set_x
, "bytevector-u32-set!",
1435 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1436 "Store @var{value} in @var{bv} at @var{index} according to "
1437 "@var{endianness}.")
1438 #define FUNC_NAME s_scm_bytevector_u32_set_x
1440 #if SIZEOF_VOID_P > 4
1441 INTEGER_SET (32, unsigned);
1443 LARGE_INTEGER_SET (32, unsigned);
1448 SCM_DEFINE (scm_bytevector_s32_set_x
, "bytevector-s32-set!",
1450 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1451 "Store @var{value} in @var{bv} at @var{index} according to "
1452 "@var{endianness}.")
1453 #define FUNC_NAME s_scm_bytevector_s32_set_x
1455 #if SIZEOF_VOID_P > 4
1456 INTEGER_SET (32, signed);
1458 LARGE_INTEGER_SET (32, signed);
1463 SCM_DEFINE (scm_bytevector_u32_native_set_x
, "bytevector-u32-native-set!",
1465 (SCM bv
, SCM index
, SCM value
),
1466 "Store the unsigned integer @var{value} at index @var{index} "
1467 "of @var{bv} using the native endianness.")
1468 #define FUNC_NAME s_scm_bytevector_u32_native_set_x
1470 #if SIZEOF_VOID_P > 4
1471 INTEGER_NATIVE_SET (32, unsigned);
1473 LARGE_INTEGER_NATIVE_SET (32, unsigned);
1478 SCM_DEFINE (scm_bytevector_s32_native_set_x
, "bytevector-s32-native-set!",
1480 (SCM bv
, SCM index
, SCM value
),
1481 "Store the signed integer @var{value} at index @var{index} "
1482 "of @var{bv} using the native endianness.")
1483 #define FUNC_NAME s_scm_bytevector_s32_native_set_x
1485 #if SIZEOF_VOID_P > 4
1486 INTEGER_NATIVE_SET (32, signed);
1488 LARGE_INTEGER_NATIVE_SET (32, signed);
1495 /* Operations on 64-bit integers. */
1497 /* For 64-bit integers, we use only the `large_{ref,set}' variant. */
1499 SCM_DEFINE (scm_bytevector_u64_ref
, "bytevector-u64-ref",
1501 (SCM bv
, SCM index
, SCM endianness
),
1502 "Return the unsigned 64-bit integer from @var{bv} at "
1504 #define FUNC_NAME s_scm_bytevector_u64_ref
1506 LARGE_INTEGER_REF (64, unsigned);
1510 SCM_DEFINE (scm_bytevector_s64_ref
, "bytevector-s64-ref",
1512 (SCM bv
, SCM index
, SCM endianness
),
1513 "Return the signed 64-bit integer from @var{bv} at "
1515 #define FUNC_NAME s_scm_bytevector_s64_ref
1517 LARGE_INTEGER_REF (64, signed);
1521 SCM_DEFINE (scm_bytevector_u64_native_ref
, "bytevector-u64-native-ref",
1523 (SCM bv
, SCM index
),
1524 "Return the unsigned 64-bit integer from @var{bv} at "
1525 "@var{index} using the native endianness.")
1526 #define FUNC_NAME s_scm_bytevector_u64_native_ref
1528 LARGE_INTEGER_NATIVE_REF (64, unsigned);
1532 SCM_DEFINE (scm_bytevector_s64_native_ref
, "bytevector-s64-native-ref",
1534 (SCM bv
, SCM index
),
1535 "Return the unsigned 64-bit integer from @var{bv} at "
1536 "@var{index} using the native endianness.")
1537 #define FUNC_NAME s_scm_bytevector_s64_native_ref
1539 LARGE_INTEGER_NATIVE_REF (64, signed);
1543 SCM_DEFINE (scm_bytevector_u64_set_x
, "bytevector-u64-set!",
1545 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1546 "Store @var{value} in @var{bv} at @var{index} according to "
1547 "@var{endianness}.")
1548 #define FUNC_NAME s_scm_bytevector_u64_set_x
1550 LARGE_INTEGER_SET (64, unsigned);
1554 SCM_DEFINE (scm_bytevector_s64_set_x
, "bytevector-s64-set!",
1556 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1557 "Store @var{value} in @var{bv} at @var{index} according to "
1558 "@var{endianness}.")
1559 #define FUNC_NAME s_scm_bytevector_s64_set_x
1561 LARGE_INTEGER_SET (64, signed);
1565 SCM_DEFINE (scm_bytevector_u64_native_set_x
, "bytevector-u64-native-set!",
1567 (SCM bv
, SCM index
, SCM value
),
1568 "Store the unsigned integer @var{value} at index @var{index} "
1569 "of @var{bv} using the native endianness.")
1570 #define FUNC_NAME s_scm_bytevector_u64_native_set_x
1572 LARGE_INTEGER_NATIVE_SET (64, unsigned);
1576 SCM_DEFINE (scm_bytevector_s64_native_set_x
, "bytevector-s64-native-set!",
1578 (SCM bv
, SCM index
, SCM value
),
1579 "Store the signed integer @var{value} at index @var{index} "
1580 "of @var{bv} using the native endianness.")
1581 #define FUNC_NAME s_scm_bytevector_s64_native_set_x
1583 LARGE_INTEGER_NATIVE_SET (64, signed);
1589 /* Operations on IEEE-754 numbers. */
1591 /* There are two possible word endians, visible in glibc's <ieee754.h>.
1592 However, in R6RS, when the endianness is `little', little endian is
1593 assumed for both the byte order and the word order. This is clear from
1594 Section 2.1 of R6RS-lib (in response to
1595 http://www.r6rs.org/formal-comments/comment-187.txt). */
1597 union scm_ieee754_float
1603 union scm_ieee754_double
1610 /* Convert to/from a floating-point number with different endianness. This
1611 method is probably not the most efficient but it should be portable. */
1614 float_to_foreign_endianness (union scm_ieee754_float
*target
,
1617 union scm_ieee754_float input
;
1620 target
->i
= bswap_32 (input
.i
);
1624 float_from_foreign_endianness (const union scm_ieee754_float
*source
)
1626 union scm_ieee754_float result
;
1628 result
.i
= bswap_32 (source
->i
);
1634 double_to_foreign_endianness (union scm_ieee754_double
*target
,
1637 union scm_ieee754_double input
;
1640 target
->i
= bswap_64 (input
.i
);
1643 static inline double
1644 double_from_foreign_endianness (const union scm_ieee754_double
*source
)
1646 union scm_ieee754_double result
;
1648 result
.i
= bswap_64 (source
->i
);
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_real (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 (scm_is_eq (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_utf_len
= 0;
1957 SCM_VALIDATE_STRING (1, str
);
1959 c_utf
= (scm_t_uint8
*) scm_to_utf8_stringn (str
, &c_utf_len
);
1960 utf
= make_bytevector (c_utf_len
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
1961 memcpy (SCM_BYTEVECTOR_CONTENTS (utf
), c_utf
, c_utf_len
);
1968 SCM_DEFINE (scm_string_to_utf16
, "string->utf16",
1970 (SCM str
, SCM endianness
),
1971 "Return a newly allocated bytevector that contains the UTF-16 "
1972 "encoding of @var{str}.")
1973 #define FUNC_NAME s_scm_string_to_utf16
1980 swap_u32 (scm_t_wchar
*vals
, size_t len
)
1983 for (n
= 0; n
< len
; n
++)
1984 vals
[n
] = bswap_32 (vals
[n
]);
1987 SCM_DEFINE (scm_string_to_utf32
, "string->utf32",
1989 (SCM str
, SCM endianness
),
1990 "Return a newly allocated bytevector that contains the UTF-32 "
1991 "encoding of @var{str}.")
1992 #define FUNC_NAME s_scm_string_to_utf32
1995 scm_t_wchar
*wchars
;
1996 size_t wchar_len
, bytes_len
;
1998 wchars
= scm_to_utf32_stringn (str
, &wchar_len
);
1999 bytes_len
= wchar_len
* sizeof (scm_t_wchar
);
2000 if (!scm_is_eq (SCM_UNBNDP (endianness
) ? scm_endianness_big
: endianness
,
2001 scm_i_native_endianness
))
2002 swap_u32 (wchars
, wchar_len
);
2004 bv
= make_bytevector (bytes_len
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
2005 memcpy (SCM_BYTEVECTOR_CONTENTS (bv
), wchars
, bytes_len
);
2013 /* Produce the body of a function that converts a UTF-encoded bytevector to a
2015 #define UTF_TO_STRING(_utf_width) \
2016 SCM str = SCM_BOOL_F; \
2018 char *c_str = NULL; \
2019 char c_utf_name[MAX_UTF_ENCODING_NAME_LEN]; \
2021 size_t c_strlen = 0, c_utf_len = 0; \
2023 SCM_VALIDATE_BYTEVECTOR (1, utf); \
2024 if (scm_is_eq (endianness, SCM_UNDEFINED)) \
2025 endianness = scm_sym_big; \
2027 SCM_VALIDATE_SYMBOL (2, endianness); \
2029 c_utf_len = SCM_BYTEVECTOR_LENGTH (utf); \
2030 c_utf = (char *) SCM_BYTEVECTOR_CONTENTS (utf); \
2031 utf_encoding_name (c_utf_name, (_utf_width), endianness); \
2033 err = mem_iconveh (c_utf, c_utf_len, \
2034 c_utf_name, "UTF-8", \
2035 iconveh_question_mark, NULL, \
2036 &c_str, &c_strlen); \
2037 if (SCM_UNLIKELY (err)) \
2038 scm_syserror_msg (FUNC_NAME, "failed to convert to string: ~A", \
2039 scm_list_1 (utf), err); \
2042 str = scm_from_utf8_stringn (c_str, c_strlen); \
2048 SCM_DEFINE (scm_utf8_to_string
, "utf8->string",
2051 "Return a newly allocate string that contains from the UTF-8-"
2052 "encoded contents of bytevector @var{utf}.")
2053 #define FUNC_NAME s_scm_utf8_to_string
2057 size_t c_utf_len
= 0;
2059 SCM_VALIDATE_BYTEVECTOR (1, utf
);
2061 c_utf_len
= SCM_BYTEVECTOR_LENGTH (utf
);
2062 c_utf
= (char *) SCM_BYTEVECTOR_CONTENTS (utf
);
2063 str
= scm_from_utf8_stringn (c_utf
, c_utf_len
);
2069 SCM_DEFINE (scm_utf16_to_string
, "utf16->string",
2071 (SCM utf
, SCM endianness
),
2072 "Return a newly allocate string that contains from the UTF-16-"
2073 "encoded contents of bytevector @var{utf}.")
2074 #define FUNC_NAME s_scm_utf16_to_string
2080 SCM_DEFINE (scm_utf32_to_string
, "utf32->string",
2082 (SCM utf
, SCM endianness
),
2083 "Return a newly allocate string that contains from the UTF-32-"
2084 "encoded contents of bytevector @var{utf}.")
2085 #define FUNC_NAME s_scm_utf32_to_string
2092 /* Initialization. */
2095 scm_bootstrap_bytevectors (void)
2097 /* This must be instantiated here because the generalized-vector API may
2098 want to access bytevectors even though `(rnrs bytevectors)' hasn't been
2100 scm_null_bytevector
= make_bytevector (0, SCM_ARRAY_ELEMENT_TYPE_VU8
);
2102 #ifdef WORDS_BIGENDIAN
2103 scm_i_native_endianness
= scm_from_latin1_symbol ("big");
2105 scm_i_native_endianness
= scm_from_latin1_symbol ("little");
2108 scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION
,
2109 "scm_init_bytevectors",
2110 (scm_t_extension_init_func
) scm_init_bytevectors
,
2113 scm_i_register_vector_constructor
2114 (scm_i_array_element_types
[SCM_ARRAY_ELEMENT_TYPE_VU8
],
2115 scm_make_bytevector
);
2119 scm_init_bytevectors (void)
2121 #include "libguile/bytevectors.x"
2123 scm_endianness_big
= scm_sym_big
;
2124 scm_endianness_little
= scm_sym_little
;