1 /* Copyright (C) 2009 Free Software Foundation, Inc.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
28 #include "libguile/_scm.h"
29 #include "libguile/extensions.h"
30 #include "libguile/bytevectors.h"
31 #include "libguile/strings.h"
32 #include "libguile/validate.h"
33 #include "libguile/ieee-754.h"
34 #include "libguile/arrays.h"
35 #include "libguile/array-handle.h"
36 #include "libguile/uniform.h"
37 #include "libguile/srfi-4.h"
40 #include <striconveh.h>
46 /* Assuming 32-bit longs. */
47 # define ULONG_MAX 4294967295UL
56 /* Convenience macros. These are used by the various templates (macros) that
57 are parameterized by integer signedness. */
58 #define INT8_T_signed scm_t_int8
59 #define INT8_T_unsigned scm_t_uint8
60 #define INT16_T_signed scm_t_int16
61 #define INT16_T_unsigned scm_t_uint16
62 #define INT32_T_signed scm_t_int32
63 #define INT32_T_unsigned scm_t_uint32
64 #define is_signed_int8(_x) (((_x) >= -128L) && ((_x) <= 127L))
65 #define is_unsigned_int8(_x) ((_x) <= 255UL)
66 #define is_signed_int16(_x) (((_x) >= -32768L) && ((_x) <= 32767L))
67 #define is_unsigned_int16(_x) ((_x) <= 65535UL)
68 #define is_signed_int32(_x) (((_x) >= -2147483648L) && ((_x) <= 2147483647L))
69 #define is_unsigned_int32(_x) ((_x) <= 4294967295UL)
70 #define SIGNEDNESS_signed 1
71 #define SIGNEDNESS_unsigned 0
73 #define INT_TYPE(_size, _sign) INT ## _size ## _T_ ## _sign
74 #define INT_SWAP(_size) bswap_ ## _size
75 #define INT_VALID_P(_size, _sign) is_ ## _sign ## _int ## _size
76 #define SIGNEDNESS(_sign) SIGNEDNESS_ ## _sign
79 #define INTEGER_ACCESSOR_PROLOGUE(_len, _sign) \
80 size_t c_len, c_index; \
83 SCM_VALIDATE_BYTEVECTOR (1, bv); \
84 c_index = scm_to_uint (index); \
86 c_len = SCM_BYTEVECTOR_LENGTH (bv); \
87 c_bv = (_sign char *) SCM_BYTEVECTOR_CONTENTS (bv); \
89 if (SCM_UNLIKELY (c_index + ((_len) >> 3UL) - 1 >= c_len)) \
90 scm_out_of_range (FUNC_NAME, index);
92 /* Template for fixed-size integer access (only 8, 16 or 32-bit). */
93 #define INTEGER_REF(_len, _sign) \
96 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
97 SCM_VALIDATE_SYMBOL (3, endianness); \
100 INT_TYPE (_len, _sign) c_result; \
102 memcpy (&c_result, &c_bv[c_index], (_len) / 8); \
103 if (!scm_is_eq (endianness, scm_i_native_endianness)) \
104 c_result = INT_SWAP (_len) (c_result); \
106 result = SCM_I_MAKINUM (c_result); \
111 /* Template for fixed-size integer access using the native endianness. */
112 #define INTEGER_NATIVE_REF(_len, _sign) \
115 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
118 INT_TYPE (_len, _sign) c_result; \
120 memcpy (&c_result, &c_bv[c_index], (_len) / 8); \
121 result = SCM_I_MAKINUM (c_result); \
126 /* Template for fixed-size integer modification (only 8, 16 or 32-bit). */
127 #define INTEGER_SET(_len, _sign) \
128 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
129 SCM_VALIDATE_SYMBOL (3, endianness); \
132 _sign long c_value; \
133 INT_TYPE (_len, _sign) c_value_short; \
135 if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
136 scm_wrong_type_arg (FUNC_NAME, 3, value); \
138 c_value = SCM_I_INUM (value); \
139 if (SCM_UNLIKELY (!INT_VALID_P (_len, _sign) (c_value))) \
140 scm_out_of_range (FUNC_NAME, value); \
142 c_value_short = (INT_TYPE (_len, _sign)) c_value; \
143 if (!scm_is_eq (endianness, scm_i_native_endianness)) \
144 c_value_short = INT_SWAP (_len) (c_value_short); \
146 memcpy (&c_bv[c_index], &c_value_short, (_len) / 8); \
149 return SCM_UNSPECIFIED;
151 /* Template for fixed-size integer modification using the native
153 #define INTEGER_NATIVE_SET(_len, _sign) \
154 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
157 _sign long c_value; \
158 INT_TYPE (_len, _sign) c_value_short; \
160 if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
161 scm_wrong_type_arg (FUNC_NAME, 3, value); \
163 c_value = SCM_I_INUM (value); \
164 if (SCM_UNLIKELY (!INT_VALID_P (_len, _sign) (c_value))) \
165 scm_out_of_range (FUNC_NAME, value); \
167 c_value_short = (INT_TYPE (_len, _sign)) c_value; \
169 memcpy (&c_bv[c_index], &c_value_short, (_len) / 8); \
172 return SCM_UNSPECIFIED;
176 /* Bytevector type. */
178 /* The threshold (in octets) under which bytevectors are stored "in-line",
179 i.e., without allocating memory beside the double cell itself.
180 This optimization is necessary since small bytevectors are expected to be
182 #define SCM_BYTEVECTOR_INLINE_THRESHOLD (2 * sizeof (SCM))
184 #define SCM_BYTEVECTOR_INLINEABLE_SIZE_P(_size) \
185 ((_size) <= SCM_BYTEVECTOR_INLINE_THRESHOLD)
186 #define SCM_BYTEVECTOR_SET_LENGTH(_bv, _len) \
187 SCM_SET_CELL_WORD_1 ((_bv), (scm_t_bits) (_len))
188 #define SCM_BYTEVECTOR_SET_CONTENTS(_bv, _buf) \
189 SCM_SET_CELL_WORD_2 ((_bv), (scm_t_bits) (_buf))
190 #define SCM_BYTEVECTOR_SET_INLINE(bv) \
191 SCM_SET_BYTEVECTOR_FLAGS (bv, \
192 SCM_BYTEVECTOR_FLAGS (bv) \
193 | SCM_F_BYTEVECTOR_INLINE)
195 #define SCM_BYTEVECTOR_SET_ELEMENT_TYPE(bv, hint) \
196 SCM_SET_BYTEVECTOR_FLAGS (bv, \
197 (SCM_BYTEVECTOR_FLAGS (bv) & SCM_F_BYTEVECTOR_INLINE) \
199 #define SCM_BYTEVECTOR_TYPE_SIZE(var) \
200 (scm_i_array_element_type_sizes[SCM_BYTEVECTOR_ELEMENT_TYPE (var)]/8)
201 #define SCM_BYTEVECTOR_TYPED_LENGTH(var) \
202 SCM_BYTEVECTOR_LENGTH (var) / SCM_BYTEVECTOR_TYPE_SIZE (var)
204 /* The empty bytevector. */
205 SCM scm_null_bytevector
= SCM_UNSPECIFIED
;
209 make_bytevector_from_buffer (size_t len
, void *contents
,
210 scm_t_array_element_type element_type
)
215 if (SCM_UNLIKELY (element_type
> SCM_ARRAY_ELEMENT_TYPE_LAST
216 || scm_i_array_element_type_sizes
[element_type
] < 8
217 || len
>= (SCM_I_SIZE_MAX
218 / (scm_i_array_element_type_sizes
[element_type
]/8))))
219 /* This would be an internal Guile programming error */
222 c_len
= len
* (scm_i_array_element_type_sizes
[element_type
] / 8);
223 if (!SCM_BYTEVECTOR_INLINEABLE_SIZE_P (c_len
))
224 ret
= scm_double_cell (scm_tc7_bytevector
, (scm_t_bits
) c_len
,
225 (scm_t_bits
) contents
, 0);
228 ret
= scm_double_cell (scm_tc7_bytevector
, (scm_t_bits
) c_len
, 0, 0);
229 SCM_BYTEVECTOR_SET_INLINE (ret
);
232 memcpy (SCM_BYTEVECTOR_CONTENTS (ret
), contents
, c_len
);
233 scm_gc_free (contents
, c_len
, SCM_GC_BYTEVECTOR
);
236 SCM_BYTEVECTOR_SET_ELEMENT_TYPE (ret
, element_type
);
241 make_bytevector (size_t len
, scm_t_array_element_type element_type
)
245 if (SCM_UNLIKELY (len
== 0 && element_type
== 0))
246 return scm_null_bytevector
;
247 else if (SCM_UNLIKELY (element_type
> SCM_ARRAY_ELEMENT_TYPE_LAST
248 || scm_i_array_element_type_sizes
[element_type
] < 8
249 || len
>= (SCM_I_SIZE_MAX
250 / (scm_i_array_element_type_sizes
[element_type
]/8))))
251 /* This would be an internal Guile programming error */
254 c_len
= len
* (scm_i_array_element_type_sizes
[element_type
]/8);
255 if (SCM_BYTEVECTOR_INLINEABLE_SIZE_P (c_len
))
258 ret
= scm_double_cell (scm_tc7_bytevector
, (scm_t_bits
) c_len
, 0, 0);
259 SCM_BYTEVECTOR_SET_INLINE (ret
);
260 SCM_BYTEVECTOR_SET_ELEMENT_TYPE (ret
, element_type
);
265 void *buf
= scm_gc_malloc_pointerless (c_len
, SCM_GC_BYTEVECTOR
);
266 return make_bytevector_from_buffer (len
, buf
, element_type
);
270 /* Return a new bytevector of size LEN octets. */
272 scm_c_make_bytevector (size_t len
)
274 return make_bytevector (len
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
277 /* Return a new bytevector of size LEN elements. */
279 scm_i_make_typed_bytevector (size_t len
, scm_t_array_element_type element_type
)
281 return make_bytevector (len
, element_type
);
284 /* Return a bytevector of size LEN made up of CONTENTS. The area pointed to
285 by CONTENTS must have been allocated using `scm_gc_malloc ()'. */
287 scm_c_take_bytevector (signed char *contents
, size_t len
)
289 return make_bytevector_from_buffer (len
, contents
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
293 scm_c_take_typed_bytevector (signed char *contents
, size_t len
,
294 scm_t_array_element_type element_type
)
296 return make_bytevector_from_buffer (len
, contents
, element_type
);
299 /* Shrink BV to C_NEW_LEN (which is assumed to be smaller than its current
300 size) and return BV. */
302 scm_i_shrink_bytevector (SCM bv
, size_t c_new_len
)
304 if (SCM_UNLIKELY (c_new_len
% SCM_BYTEVECTOR_TYPE_SIZE (bv
)))
305 /* This would be an internal Guile programming error */
308 if (!SCM_BYTEVECTOR_INLINE_P (bv
))
311 signed char *c_bv
, *c_new_bv
;
313 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
314 c_bv
= SCM_BYTEVECTOR_CONTENTS (bv
);
316 SCM_BYTEVECTOR_SET_LENGTH (bv
, c_new_len
);
318 if (SCM_BYTEVECTOR_INLINEABLE_SIZE_P (c_new_len
))
320 /* Copy to the in-line buffer and free the current buffer. */
321 SCM_BYTEVECTOR_SET_INLINE (bv
);
322 c_new_bv
= SCM_BYTEVECTOR_CONTENTS (bv
);
323 memcpy (c_new_bv
, c_bv
, c_new_len
);
324 scm_gc_free (c_bv
, c_len
, SCM_GC_BYTEVECTOR
);
328 /* Resize the existing buffer. */
329 c_new_bv
= scm_gc_realloc (c_bv
, c_len
, c_new_len
,
331 SCM_BYTEVECTOR_SET_CONTENTS (bv
, c_new_bv
);
335 SCM_BYTEVECTOR_SET_LENGTH (bv
, c_new_len
);
341 scm_is_bytevector (SCM obj
)
343 return SCM_BYTEVECTOR_P (obj
);
347 scm_c_bytevector_length (SCM bv
)
348 #define FUNC_NAME "scm_c_bytevector_length"
350 SCM_VALIDATE_BYTEVECTOR (1, bv
);
352 return SCM_BYTEVECTOR_LENGTH (bv
);
357 scm_c_bytevector_ref (SCM bv
, size_t index
)
358 #define FUNC_NAME "scm_c_bytevector_ref"
361 const scm_t_uint8
*c_bv
;
363 SCM_VALIDATE_BYTEVECTOR (1, bv
);
365 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
366 c_bv
= (scm_t_uint8
*) SCM_BYTEVECTOR_CONTENTS (bv
);
368 if (SCM_UNLIKELY (index
>= c_len
))
369 scm_out_of_range (FUNC_NAME
, scm_from_size_t (index
));
376 scm_c_bytevector_set_x (SCM bv
, size_t index
, scm_t_uint8 value
)
377 #define FUNC_NAME "scm_c_bytevector_set_x"
382 SCM_VALIDATE_BYTEVECTOR (1, bv
);
384 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
385 c_bv
= (scm_t_uint8
*) SCM_BYTEVECTOR_CONTENTS (bv
);
387 if (SCM_UNLIKELY (index
>= c_len
))
388 scm_out_of_range (FUNC_NAME
, scm_from_size_t (index
));
397 scm_i_print_bytevector (SCM bv
, SCM port
, scm_print_state
*pstate SCM_UNUSED
)
399 ssize_t ubnd
, inc
, i
;
400 scm_t_array_handle h
;
402 scm_array_get_handle (bv
, &h
);
404 scm_putc ('#', port
);
405 scm_write (scm_array_handle_element_type (&h
), port
);
406 scm_putc ('(', port
);
407 for (i
= h
.dims
[0].lbnd
, ubnd
= h
.dims
[0].ubnd
, inc
= h
.dims
[0].inc
;
411 scm_putc (' ', port
);
412 scm_write (scm_array_handle_ref (&h
, i
), port
);
414 scm_putc (')', port
);
420 /* General operations. */
422 SCM_SYMBOL (scm_sym_big
, "big");
423 SCM_SYMBOL (scm_sym_little
, "little");
425 SCM scm_endianness_big
, scm_endianness_little
;
427 /* Host endianness (a symbol). */
428 SCM scm_i_native_endianness
= SCM_UNSPECIFIED
;
432 # define bswap_24(_x) \
433 ((((_x) & 0xff0000) >> 16) | \
434 (((_x) & 0x00ff00)) | \
435 (((_x) & 0x0000ff) << 16))
439 SCM_DEFINE (scm_native_endianness
, "native-endianness", 0, 0, 0,
441 "Return a symbol denoting the machine's native endianness.")
442 #define FUNC_NAME s_scm_native_endianness
444 return scm_i_native_endianness
;
448 SCM_DEFINE (scm_bytevector_p
, "bytevector?", 1, 0, 0,
450 "Return true if @var{obj} is a bytevector.")
451 #define FUNC_NAME s_scm_bytevector_p
453 return scm_from_bool (scm_is_bytevector (obj
));
457 SCM_DEFINE (scm_make_bytevector
, "make-bytevector", 1, 1, 0,
459 "Return a newly allocated bytevector of @var{len} bytes, "
460 "optionally filled with @var{fill}.")
461 #define FUNC_NAME s_scm_make_bytevector
465 signed char c_fill
= '\0';
467 SCM_VALIDATE_UINT_COPY (1, len
, c_len
);
468 if (fill
!= SCM_UNDEFINED
)
472 value
= scm_to_int (fill
);
473 if (SCM_UNLIKELY ((value
< -128) || (value
> 255)))
474 scm_out_of_range (FUNC_NAME
, fill
);
475 c_fill
= (signed char) value
;
478 bv
= make_bytevector (c_len
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
479 if (fill
!= SCM_UNDEFINED
)
482 signed char *contents
;
484 contents
= SCM_BYTEVECTOR_CONTENTS (bv
);
485 for (i
= 0; i
< c_len
; i
++)
486 contents
[i
] = c_fill
;
493 SCM_DEFINE (scm_bytevector_length
, "bytevector-length", 1, 0, 0,
495 "Return the length (in bytes) of @var{bv}.")
496 #define FUNC_NAME s_scm_bytevector_length
498 return scm_from_uint (scm_c_bytevector_length (bv
));
502 SCM_DEFINE (scm_bytevector_eq_p
, "bytevector=?", 2, 0, 0,
504 "Return is @var{bv1} equals to @var{bv2}---i.e., if they "
505 "have the same length and contents.")
506 #define FUNC_NAME s_scm_bytevector_eq_p
508 SCM result
= SCM_BOOL_F
;
509 unsigned c_len1
, c_len2
;
511 SCM_VALIDATE_BYTEVECTOR (1, bv1
);
512 SCM_VALIDATE_BYTEVECTOR (2, bv2
);
514 c_len1
= SCM_BYTEVECTOR_LENGTH (bv1
);
515 c_len2
= SCM_BYTEVECTOR_LENGTH (bv2
);
517 if (c_len1
== c_len2
)
519 signed char *c_bv1
, *c_bv2
;
521 c_bv1
= SCM_BYTEVECTOR_CONTENTS (bv1
);
522 c_bv2
= SCM_BYTEVECTOR_CONTENTS (bv2
);
524 result
= scm_from_bool (!memcmp (c_bv1
, c_bv2
, c_len1
));
531 SCM_DEFINE (scm_bytevector_fill_x
, "bytevector-fill!", 2, 0, 0,
533 "Fill bytevector @var{bv} with @var{fill}, a byte.")
534 #define FUNC_NAME s_scm_bytevector_fill_x
537 signed char *c_bv
, c_fill
;
539 SCM_VALIDATE_BYTEVECTOR (1, bv
);
540 c_fill
= scm_to_int8 (fill
);
542 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
543 c_bv
= SCM_BYTEVECTOR_CONTENTS (bv
);
545 for (i
= 0; i
< c_len
; i
++)
548 return SCM_UNSPECIFIED
;
552 SCM_DEFINE (scm_bytevector_copy_x
, "bytevector-copy!", 5, 0, 0,
553 (SCM source
, SCM source_start
, SCM target
, SCM target_start
,
555 "Copy @var{len} bytes from @var{source} into @var{target}, "
556 "starting reading from @var{source_start} (a positive index "
557 "within @var{source}) and start writing at "
558 "@var{target_start}.")
559 #define FUNC_NAME s_scm_bytevector_copy_x
561 unsigned c_len
, c_source_len
, c_target_len
;
562 unsigned c_source_start
, c_target_start
;
563 signed char *c_source
, *c_target
;
565 SCM_VALIDATE_BYTEVECTOR (1, source
);
566 SCM_VALIDATE_BYTEVECTOR (3, target
);
568 c_len
= scm_to_uint (len
);
569 c_source_start
= scm_to_uint (source_start
);
570 c_target_start
= scm_to_uint (target_start
);
572 c_source
= SCM_BYTEVECTOR_CONTENTS (source
);
573 c_target
= SCM_BYTEVECTOR_CONTENTS (target
);
574 c_source_len
= SCM_BYTEVECTOR_LENGTH (source
);
575 c_target_len
= SCM_BYTEVECTOR_LENGTH (target
);
577 if (SCM_UNLIKELY (c_source_start
+ c_len
> c_source_len
))
578 scm_out_of_range (FUNC_NAME
, source_start
);
579 if (SCM_UNLIKELY (c_target_start
+ c_len
> c_target_len
))
580 scm_out_of_range (FUNC_NAME
, target_start
);
582 memcpy (c_target
+ c_target_start
,
583 c_source
+ c_source_start
,
586 return SCM_UNSPECIFIED
;
590 SCM_DEFINE (scm_bytevector_copy
, "bytevector-copy", 1, 0, 0,
592 "Return a newly allocated copy of @var{bv}.")
593 #define FUNC_NAME s_scm_bytevector_copy
597 signed char *c_bv
, *c_copy
;
599 SCM_VALIDATE_BYTEVECTOR (1, bv
);
601 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
602 c_bv
= SCM_BYTEVECTOR_CONTENTS (bv
);
604 copy
= make_bytevector (c_len
, SCM_BYTEVECTOR_ELEMENT_TYPE (bv
));
605 c_copy
= SCM_BYTEVECTOR_CONTENTS (copy
);
606 memcpy (c_copy
, c_bv
, c_len
);
612 SCM_DEFINE (scm_uniform_array_to_bytevector
, "uniform-array->bytevector",
613 1, 0, 0, (SCM array
),
614 "Return a newly allocated bytevector whose contents\n"
615 "will be copied from the uniform array @var{array}.")
616 #define FUNC_NAME s_scm_uniform_array_to_bytevector
620 scm_t_array_handle h
;
624 contents
= scm_array_contents (array
, SCM_BOOL_T
);
625 if (scm_is_false (contents
))
626 scm_wrong_type_arg_msg (FUNC_NAME
, 0, array
, "uniform contiguous array");
628 scm_array_get_handle (contents
, &h
);
630 base
= scm_array_handle_uniform_elements (&h
);
631 len
= h
.dims
->inc
* (h
.dims
->ubnd
- h
.dims
->lbnd
+ 1);
632 sz
= scm_array_handle_uniform_element_size (&h
);
634 ret
= make_bytevector (len
* sz
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
635 memcpy (SCM_BYTEVECTOR_CONTENTS (ret
), base
, len
* sz
);
637 scm_array_handle_release (&h
);
644 /* Operations on bytes and octets. */
646 SCM_DEFINE (scm_bytevector_u8_ref
, "bytevector-u8-ref", 2, 0, 0,
648 "Return the octet located at @var{index} in @var{bv}.")
649 #define FUNC_NAME s_scm_bytevector_u8_ref
651 INTEGER_NATIVE_REF (8, unsigned);
655 SCM_DEFINE (scm_bytevector_s8_ref
, "bytevector-s8-ref", 2, 0, 0,
657 "Return the byte located at @var{index} in @var{bv}.")
658 #define FUNC_NAME s_scm_bytevector_s8_ref
660 INTEGER_NATIVE_REF (8, signed);
664 SCM_DEFINE (scm_bytevector_u8_set_x
, "bytevector-u8-set!", 3, 0, 0,
665 (SCM bv
, SCM index
, SCM value
),
666 "Return the octet located at @var{index} in @var{bv}.")
667 #define FUNC_NAME s_scm_bytevector_u8_set_x
669 INTEGER_NATIVE_SET (8, unsigned);
673 SCM_DEFINE (scm_bytevector_s8_set_x
, "bytevector-s8-set!", 3, 0, 0,
674 (SCM bv
, SCM index
, SCM value
),
675 "Return the octet located at @var{index} in @var{bv}.")
676 #define FUNC_NAME s_scm_bytevector_s8_set_x
678 INTEGER_NATIVE_SET (8, signed);
682 #undef OCTET_ACCESSOR_PROLOGUE
685 SCM_DEFINE (scm_bytevector_to_u8_list
, "bytevector->u8-list", 1, 0, 0,
687 "Return a newly allocated list of octets containing the "
688 "contents of @var{bv}.")
689 #define FUNC_NAME s_scm_bytevector_to_u8_list
695 SCM_VALIDATE_BYTEVECTOR (1, bv
);
697 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
698 c_bv
= (unsigned char *) SCM_BYTEVECTOR_CONTENTS (bv
);
700 lst
= scm_make_list (scm_from_uint (c_len
), SCM_UNSPECIFIED
);
701 for (i
= 0, pair
= lst
;
703 i
++, pair
= SCM_CDR (pair
))
705 SCM_SETCAR (pair
, SCM_I_MAKINUM (c_bv
[i
]));
712 SCM_DEFINE (scm_u8_list_to_bytevector
, "u8-list->bytevector", 1, 0, 0,
714 "Turn @var{lst}, a list of octets, into a bytevector.")
715 #define FUNC_NAME s_scm_u8_list_to_bytevector
721 SCM_VALIDATE_LIST_COPYLEN (1, lst
, c_len
);
723 bv
= make_bytevector (c_len
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
724 c_bv
= (unsigned char *) SCM_BYTEVECTOR_CONTENTS (bv
);
726 for (i
= 0; i
< c_len
; lst
= SCM_CDR (lst
), i
++)
728 item
= SCM_CAR (lst
);
730 if (SCM_LIKELY (SCM_I_INUMP (item
)))
734 c_item
= SCM_I_INUM (item
);
735 if (SCM_LIKELY ((c_item
>= 0) && (c_item
< 256)))
736 c_bv
[i
] = (unsigned char) c_item
;
747 scm_wrong_type_arg (FUNC_NAME
, 1, item
);
753 /* Compute the two's complement of VALUE (a positive integer) on SIZE octets
754 using (2^(SIZE * 8) - VALUE). */
756 twos_complement (mpz_t value
, size_t size
)
758 unsigned long bit_count
;
760 /* We expect BIT_COUNT to fit in a unsigned long thanks to the range
761 checking on SIZE performed earlier. */
762 bit_count
= (unsigned long) size
<< 3UL;
764 if (SCM_LIKELY (bit_count
< sizeof (unsigned long)))
765 mpz_ui_sub (value
, 1UL << bit_count
, value
);
771 mpz_ui_pow_ui (max
, 2, bit_count
);
772 mpz_sub (value
, max
, value
);
778 bytevector_large_ref (const char *c_bv
, size_t c_size
, int signed_p
,
783 int c_endianness
, negative_p
= 0;
787 if (scm_is_eq (endianness
, scm_sym_big
))
788 negative_p
= c_bv
[0] & 0x80;
790 negative_p
= c_bv
[c_size
- 1] & 0x80;
793 c_endianness
= scm_is_eq (endianness
, scm_sym_big
) ? 1 : -1;
796 mpz_import (c_mpz
, 1 /* 1 word */, 1 /* word order doesn't matter */,
797 c_size
/* word is C_SIZE-byte long */,
799 0 /* nails */, c_bv
);
801 if (signed_p
&& negative_p
)
803 twos_complement (c_mpz
, c_size
);
804 mpz_neg (c_mpz
, c_mpz
);
807 result
= scm_from_mpz (c_mpz
);
808 mpz_clear (c_mpz
); /* FIXME: Needed? */
814 bytevector_large_set (char *c_bv
, size_t c_size
, int signed_p
,
815 SCM value
, SCM endianness
)
818 int c_endianness
, c_sign
, err
= 0;
820 c_endianness
= scm_is_eq (endianness
, scm_sym_big
) ? 1 : -1;
823 scm_to_mpz (value
, c_mpz
);
825 c_sign
= mpz_sgn (c_mpz
);
828 if (SCM_LIKELY (signed_p
))
830 mpz_neg (c_mpz
, c_mpz
);
831 twos_complement (c_mpz
, c_size
);
842 memset (c_bv
, 0, c_size
);
845 size_t word_count
, value_size
;
847 value_size
= (mpz_sizeinbase (c_mpz
, 2) + (8 * c_size
)) / (8 * c_size
);
848 if (SCM_UNLIKELY (value_size
> c_size
))
855 mpz_export (c_bv
, &word_count
, 1 /* word order doesn't matter */,
856 c_size
, c_endianness
,
857 0 /* nails */, c_mpz
);
858 if (SCM_UNLIKELY (word_count
!= 1))
859 /* Shouldn't happen since we already checked with VALUE_SIZE. */
869 #define GENERIC_INTEGER_ACCESSOR_PROLOGUE(_sign) \
870 unsigned long c_len, c_index, c_size; \
873 SCM_VALIDATE_BYTEVECTOR (1, bv); \
874 c_index = scm_to_ulong (index); \
875 c_size = scm_to_ulong (size); \
877 c_len = SCM_BYTEVECTOR_LENGTH (bv); \
878 c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
880 /* C_SIZE must have its 3 higher bits set to zero so that \
881 multiplying it by 8 yields a number that fits in an \
883 if (SCM_UNLIKELY ((c_size == 0) || (c_size >= (ULONG_MAX >> 3L)))) \
884 scm_out_of_range (FUNC_NAME, size); \
885 if (SCM_UNLIKELY (c_index + c_size > c_len)) \
886 scm_out_of_range (FUNC_NAME, index);
889 /* Template of an integer reference function. */
890 #define GENERIC_INTEGER_REF(_sign) \
898 swap = !scm_is_eq (endianness, scm_i_native_endianness); \
903 _sign char c_value8; \
904 memcpy (&c_value8, c_bv, 1); \
910 INT_TYPE (16, _sign) c_value16; \
911 memcpy (&c_value16, c_bv, 2); \
913 value = (INT_TYPE (16, _sign)) bswap_16 (c_value16); \
922 result = SCM_I_MAKINUM ((_sign int) value); \
925 result = bytevector_large_ref ((char *) c_bv, \
926 c_size, SIGNEDNESS (_sign), \
932 bytevector_signed_ref (const char *c_bv
, size_t c_size
, SCM endianness
)
934 GENERIC_INTEGER_REF (signed);
938 bytevector_unsigned_ref (const char *c_bv
, size_t c_size
, SCM endianness
)
940 GENERIC_INTEGER_REF (unsigned);
944 /* Template of an integer assignment function. */
945 #define GENERIC_INTEGER_SET(_sign) \
950 if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
953 c_value = SCM_I_INUM (value); \
957 if (SCM_LIKELY (INT_VALID_P (8, _sign) (c_value))) \
959 _sign char c_value8; \
960 c_value8 = (_sign char) c_value; \
961 memcpy (c_bv, &c_value8, 1); \
968 if (SCM_LIKELY (INT_VALID_P (16, _sign) (c_value))) \
971 INT_TYPE (16, _sign) c_value16; \
973 swap = !scm_is_eq (endianness, scm_i_native_endianness); \
976 c_value16 = (INT_TYPE (16, _sign)) bswap_16 (c_value); \
978 c_value16 = c_value; \
980 memcpy (c_bv, &c_value16, 2); \
994 err = bytevector_large_set (c_bv, c_size, \
995 SIGNEDNESS (_sign), \
996 value, endianness); \
1004 scm_out_of_range (FUNC_NAME, value); \
1008 bytevector_signed_set (char *c_bv
, size_t c_size
,
1009 SCM value
, SCM endianness
,
1010 const char *func_name
)
1011 #define FUNC_NAME func_name
1013 GENERIC_INTEGER_SET (signed);
1018 bytevector_unsigned_set (char *c_bv
, size_t c_size
,
1019 SCM value
, SCM endianness
,
1020 const char *func_name
)
1021 #define FUNC_NAME func_name
1023 GENERIC_INTEGER_SET (unsigned);
1027 #undef GENERIC_INTEGER_SET
1028 #undef GENERIC_INTEGER_REF
1031 SCM_DEFINE (scm_bytevector_uint_ref
, "bytevector-uint-ref", 4, 0, 0,
1032 (SCM bv
, SCM index
, SCM endianness
, SCM size
),
1033 "Return the @var{size}-octet long unsigned integer at index "
1034 "@var{index} in @var{bv}.")
1035 #define FUNC_NAME s_scm_bytevector_uint_ref
1037 GENERIC_INTEGER_ACCESSOR_PROLOGUE (unsigned);
1039 return (bytevector_unsigned_ref (&c_bv
[c_index
], c_size
, endianness
));
1043 SCM_DEFINE (scm_bytevector_sint_ref
, "bytevector-sint-ref", 4, 0, 0,
1044 (SCM bv
, SCM index
, SCM endianness
, SCM size
),
1045 "Return the @var{size}-octet long unsigned integer at index "
1046 "@var{index} in @var{bv}.")
1047 #define FUNC_NAME s_scm_bytevector_sint_ref
1049 GENERIC_INTEGER_ACCESSOR_PROLOGUE (signed);
1051 return (bytevector_signed_ref (&c_bv
[c_index
], c_size
, endianness
));
1055 SCM_DEFINE (scm_bytevector_uint_set_x
, "bytevector-uint-set!", 5, 0, 0,
1056 (SCM bv
, SCM index
, SCM value
, SCM endianness
, SCM size
),
1057 "Set the @var{size}-octet long unsigned integer at @var{index} "
1059 #define FUNC_NAME s_scm_bytevector_uint_set_x
1061 GENERIC_INTEGER_ACCESSOR_PROLOGUE (unsigned);
1063 bytevector_unsigned_set (&c_bv
[c_index
], c_size
, value
, endianness
,
1066 return SCM_UNSPECIFIED
;
1070 SCM_DEFINE (scm_bytevector_sint_set_x
, "bytevector-sint-set!", 5, 0, 0,
1071 (SCM bv
, SCM index
, SCM value
, SCM endianness
, SCM size
),
1072 "Set the @var{size}-octet long signed integer at @var{index} "
1074 #define FUNC_NAME s_scm_bytevector_sint_set_x
1076 GENERIC_INTEGER_ACCESSOR_PROLOGUE (signed);
1078 bytevector_signed_set (&c_bv
[c_index
], c_size
, value
, endianness
,
1081 return SCM_UNSPECIFIED
;
1087 /* Operations on integers of arbitrary size. */
1089 #define INTEGERS_TO_LIST(_sign) \
1091 size_t i, c_len, c_size; \
1093 SCM_VALIDATE_BYTEVECTOR (1, bv); \
1094 SCM_VALIDATE_SYMBOL (2, endianness); \
1095 c_size = scm_to_uint (size); \
1097 c_len = SCM_BYTEVECTOR_LENGTH (bv); \
1098 if (SCM_UNLIKELY (c_len == 0)) \
1100 else if (SCM_UNLIKELY (c_len < c_size)) \
1101 scm_out_of_range (FUNC_NAME, size); \
1106 c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
1108 lst = scm_make_list (scm_from_uint (c_len / c_size), \
1110 for (i = 0, pair = lst; \
1111 i <= c_len - c_size; \
1112 i += c_size, c_bv += c_size, pair = SCM_CDR (pair)) \
1115 bytevector_ ## _sign ## _ref (c_bv, c_size, \
1122 SCM_DEFINE (scm_bytevector_to_sint_list
, "bytevector->sint-list",
1124 (SCM bv
, SCM endianness
, SCM size
),
1125 "Return a list of signed integers of @var{size} octets "
1126 "representing the contents of @var{bv}.")
1127 #define FUNC_NAME s_scm_bytevector_to_sint_list
1129 INTEGERS_TO_LIST (signed);
1133 SCM_DEFINE (scm_bytevector_to_uint_list
, "bytevector->uint-list",
1135 (SCM bv
, SCM endianness
, SCM size
),
1136 "Return a list of unsigned integers of @var{size} octets "
1137 "representing the contents of @var{bv}.")
1138 #define FUNC_NAME s_scm_bytevector_to_uint_list
1140 INTEGERS_TO_LIST (unsigned);
1144 #undef INTEGER_TO_LIST
1147 #define INTEGER_LIST_TO_BYTEVECTOR(_sign) \
1151 char *c_bv, *c_bv_ptr; \
1153 SCM_VALIDATE_LIST_COPYLEN (1, lst, c_len); \
1154 SCM_VALIDATE_SYMBOL (2, endianness); \
1155 c_size = scm_to_uint (size); \
1157 if (SCM_UNLIKELY ((c_size == 0) || (c_size >= (ULONG_MAX >> 3L)))) \
1158 scm_out_of_range (FUNC_NAME, size); \
1160 bv = make_bytevector (c_len * c_size, SCM_ARRAY_ELEMENT_TYPE_VU8); \
1161 c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
1163 for (c_bv_ptr = c_bv; \
1164 !scm_is_null (lst); \
1165 lst = SCM_CDR (lst), c_bv_ptr += c_size) \
1167 bytevector_ ## _sign ## _set (c_bv_ptr, c_size, \
1168 SCM_CAR (lst), endianness, \
1175 SCM_DEFINE (scm_uint_list_to_bytevector
, "uint-list->bytevector",
1177 (SCM lst
, SCM endianness
, SCM size
),
1178 "Return a bytevector containing the unsigned integers "
1179 "listed in @var{lst} and encoded on @var{size} octets "
1180 "according to @var{endianness}.")
1181 #define FUNC_NAME s_scm_uint_list_to_bytevector
1183 INTEGER_LIST_TO_BYTEVECTOR (unsigned);
1187 SCM_DEFINE (scm_sint_list_to_bytevector
, "sint-list->bytevector",
1189 (SCM lst
, SCM endianness
, SCM size
),
1190 "Return a bytevector containing the signed integers "
1191 "listed in @var{lst} and encoded on @var{size} octets "
1192 "according to @var{endianness}.")
1193 #define FUNC_NAME s_scm_sint_list_to_bytevector
1195 INTEGER_LIST_TO_BYTEVECTOR (signed);
1199 #undef INTEGER_LIST_TO_BYTEVECTOR
1203 /* Operations on 16-bit integers. */
1205 SCM_DEFINE (scm_bytevector_u16_ref
, "bytevector-u16-ref",
1207 (SCM bv
, SCM index
, SCM endianness
),
1208 "Return the unsigned 16-bit integer from @var{bv} at "
1210 #define FUNC_NAME s_scm_bytevector_u16_ref
1212 INTEGER_REF (16, unsigned);
1216 SCM_DEFINE (scm_bytevector_s16_ref
, "bytevector-s16-ref",
1218 (SCM bv
, SCM index
, SCM endianness
),
1219 "Return the signed 16-bit integer from @var{bv} at "
1221 #define FUNC_NAME s_scm_bytevector_s16_ref
1223 INTEGER_REF (16, signed);
1227 SCM_DEFINE (scm_bytevector_u16_native_ref
, "bytevector-u16-native-ref",
1229 (SCM bv
, SCM index
),
1230 "Return the unsigned 16-bit integer from @var{bv} at "
1231 "@var{index} using the native endianness.")
1232 #define FUNC_NAME s_scm_bytevector_u16_native_ref
1234 INTEGER_NATIVE_REF (16, unsigned);
1238 SCM_DEFINE (scm_bytevector_s16_native_ref
, "bytevector-s16-native-ref",
1240 (SCM bv
, SCM index
),
1241 "Return the unsigned 16-bit integer from @var{bv} at "
1242 "@var{index} using the native endianness.")
1243 #define FUNC_NAME s_scm_bytevector_s16_native_ref
1245 INTEGER_NATIVE_REF (16, signed);
1249 SCM_DEFINE (scm_bytevector_u16_set_x
, "bytevector-u16-set!",
1251 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1252 "Store @var{value} in @var{bv} at @var{index} according to "
1253 "@var{endianness}.")
1254 #define FUNC_NAME s_scm_bytevector_u16_set_x
1256 INTEGER_SET (16, unsigned);
1260 SCM_DEFINE (scm_bytevector_s16_set_x
, "bytevector-s16-set!",
1262 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1263 "Store @var{value} in @var{bv} at @var{index} according to "
1264 "@var{endianness}.")
1265 #define FUNC_NAME s_scm_bytevector_s16_set_x
1267 INTEGER_SET (16, signed);
1271 SCM_DEFINE (scm_bytevector_u16_native_set_x
, "bytevector-u16-native-set!",
1273 (SCM bv
, SCM index
, SCM value
),
1274 "Store the unsigned integer @var{value} at index @var{index} "
1275 "of @var{bv} using the native endianness.")
1276 #define FUNC_NAME s_scm_bytevector_u16_native_set_x
1278 INTEGER_NATIVE_SET (16, unsigned);
1282 SCM_DEFINE (scm_bytevector_s16_native_set_x
, "bytevector-s16-native-set!",
1284 (SCM bv
, SCM index
, SCM value
),
1285 "Store the signed integer @var{value} at index @var{index} "
1286 "of @var{bv} using the native endianness.")
1287 #define FUNC_NAME s_scm_bytevector_s16_native_set_x
1289 INTEGER_NATIVE_SET (16, signed);
1295 /* Operations on 32-bit integers. */
1297 /* Unfortunately, on 32-bit machines `SCM' is not large enough to hold
1298 arbitrary 32-bit integers. Thus we fall back to using the
1299 `large_{ref,set}' variants on 32-bit machines. */
1301 #define LARGE_INTEGER_REF(_len, _sign) \
1302 INTEGER_ACCESSOR_PROLOGUE(_len, _sign); \
1303 SCM_VALIDATE_SYMBOL (3, endianness); \
1305 return (bytevector_large_ref ((char *) c_bv + c_index, _len / 8, \
1306 SIGNEDNESS (_sign), endianness));
1308 #define LARGE_INTEGER_SET(_len, _sign) \
1310 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
1311 SCM_VALIDATE_SYMBOL (4, endianness); \
1313 err = bytevector_large_set ((char *) c_bv + c_index, _len / 8, \
1314 SIGNEDNESS (_sign), value, endianness); \
1315 if (SCM_UNLIKELY (err)) \
1316 scm_out_of_range (FUNC_NAME, value); \
1318 return SCM_UNSPECIFIED;
1320 #define LARGE_INTEGER_NATIVE_REF(_len, _sign) \
1321 INTEGER_ACCESSOR_PROLOGUE(_len, _sign); \
1322 return (bytevector_large_ref ((char *) c_bv + c_index, _len / 8, \
1323 SIGNEDNESS (_sign), scm_i_native_endianness));
1325 #define LARGE_INTEGER_NATIVE_SET(_len, _sign) \
1327 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
1329 err = bytevector_large_set ((char *) c_bv + c_index, _len / 8, \
1330 SIGNEDNESS (_sign), value, \
1331 scm_i_native_endianness); \
1332 if (SCM_UNLIKELY (err)) \
1333 scm_out_of_range (FUNC_NAME, value); \
1335 return SCM_UNSPECIFIED;
1338 SCM_DEFINE (scm_bytevector_u32_ref
, "bytevector-u32-ref",
1340 (SCM bv
, SCM index
, SCM endianness
),
1341 "Return the unsigned 32-bit integer from @var{bv} at "
1343 #define FUNC_NAME s_scm_bytevector_u32_ref
1345 #if SIZEOF_VOID_P > 4
1346 INTEGER_REF (32, unsigned);
1348 LARGE_INTEGER_REF (32, unsigned);
1353 SCM_DEFINE (scm_bytevector_s32_ref
, "bytevector-s32-ref",
1355 (SCM bv
, SCM index
, SCM endianness
),
1356 "Return the signed 32-bit integer from @var{bv} at "
1358 #define FUNC_NAME s_scm_bytevector_s32_ref
1360 #if SIZEOF_VOID_P > 4
1361 INTEGER_REF (32, signed);
1363 LARGE_INTEGER_REF (32, signed);
1368 SCM_DEFINE (scm_bytevector_u32_native_ref
, "bytevector-u32-native-ref",
1370 (SCM bv
, SCM index
),
1371 "Return the unsigned 32-bit integer from @var{bv} at "
1372 "@var{index} using the native endianness.")
1373 #define FUNC_NAME s_scm_bytevector_u32_native_ref
1375 #if SIZEOF_VOID_P > 4
1376 INTEGER_NATIVE_REF (32, unsigned);
1378 LARGE_INTEGER_NATIVE_REF (32, unsigned);
1383 SCM_DEFINE (scm_bytevector_s32_native_ref
, "bytevector-s32-native-ref",
1385 (SCM bv
, SCM index
),
1386 "Return the unsigned 32-bit integer from @var{bv} at "
1387 "@var{index} using the native endianness.")
1388 #define FUNC_NAME s_scm_bytevector_s32_native_ref
1390 #if SIZEOF_VOID_P > 4
1391 INTEGER_NATIVE_REF (32, signed);
1393 LARGE_INTEGER_NATIVE_REF (32, signed);
1398 SCM_DEFINE (scm_bytevector_u32_set_x
, "bytevector-u32-set!",
1400 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1401 "Store @var{value} in @var{bv} at @var{index} according to "
1402 "@var{endianness}.")
1403 #define FUNC_NAME s_scm_bytevector_u32_set_x
1405 #if SIZEOF_VOID_P > 4
1406 INTEGER_SET (32, unsigned);
1408 LARGE_INTEGER_SET (32, unsigned);
1413 SCM_DEFINE (scm_bytevector_s32_set_x
, "bytevector-s32-set!",
1415 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1416 "Store @var{value} in @var{bv} at @var{index} according to "
1417 "@var{endianness}.")
1418 #define FUNC_NAME s_scm_bytevector_s32_set_x
1420 #if SIZEOF_VOID_P > 4
1421 INTEGER_SET (32, signed);
1423 LARGE_INTEGER_SET (32, signed);
1428 SCM_DEFINE (scm_bytevector_u32_native_set_x
, "bytevector-u32-native-set!",
1430 (SCM bv
, SCM index
, SCM value
),
1431 "Store the unsigned integer @var{value} at index @var{index} "
1432 "of @var{bv} using the native endianness.")
1433 #define FUNC_NAME s_scm_bytevector_u32_native_set_x
1435 #if SIZEOF_VOID_P > 4
1436 INTEGER_NATIVE_SET (32, unsigned);
1438 LARGE_INTEGER_NATIVE_SET (32, unsigned);
1443 SCM_DEFINE (scm_bytevector_s32_native_set_x
, "bytevector-s32-native-set!",
1445 (SCM bv
, SCM index
, SCM value
),
1446 "Store the signed integer @var{value} at index @var{index} "
1447 "of @var{bv} using the native endianness.")
1448 #define FUNC_NAME s_scm_bytevector_s32_native_set_x
1450 #if SIZEOF_VOID_P > 4
1451 INTEGER_NATIVE_SET (32, signed);
1453 LARGE_INTEGER_NATIVE_SET (32, signed);
1460 /* Operations on 64-bit integers. */
1462 /* For 64-bit integers, we use only the `large_{ref,set}' variant. */
1464 SCM_DEFINE (scm_bytevector_u64_ref
, "bytevector-u64-ref",
1466 (SCM bv
, SCM index
, SCM endianness
),
1467 "Return the unsigned 64-bit integer from @var{bv} at "
1469 #define FUNC_NAME s_scm_bytevector_u64_ref
1471 LARGE_INTEGER_REF (64, unsigned);
1475 SCM_DEFINE (scm_bytevector_s64_ref
, "bytevector-s64-ref",
1477 (SCM bv
, SCM index
, SCM endianness
),
1478 "Return the signed 64-bit integer from @var{bv} at "
1480 #define FUNC_NAME s_scm_bytevector_s64_ref
1482 LARGE_INTEGER_REF (64, signed);
1486 SCM_DEFINE (scm_bytevector_u64_native_ref
, "bytevector-u64-native-ref",
1488 (SCM bv
, SCM index
),
1489 "Return the unsigned 64-bit integer from @var{bv} at "
1490 "@var{index} using the native endianness.")
1491 #define FUNC_NAME s_scm_bytevector_u64_native_ref
1493 LARGE_INTEGER_NATIVE_REF (64, unsigned);
1497 SCM_DEFINE (scm_bytevector_s64_native_ref
, "bytevector-s64-native-ref",
1499 (SCM bv
, SCM index
),
1500 "Return the unsigned 64-bit integer from @var{bv} at "
1501 "@var{index} using the native endianness.")
1502 #define FUNC_NAME s_scm_bytevector_s64_native_ref
1504 LARGE_INTEGER_NATIVE_REF (64, signed);
1508 SCM_DEFINE (scm_bytevector_u64_set_x
, "bytevector-u64-set!",
1510 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1511 "Store @var{value} in @var{bv} at @var{index} according to "
1512 "@var{endianness}.")
1513 #define FUNC_NAME s_scm_bytevector_u64_set_x
1515 LARGE_INTEGER_SET (64, unsigned);
1519 SCM_DEFINE (scm_bytevector_s64_set_x
, "bytevector-s64-set!",
1521 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1522 "Store @var{value} in @var{bv} at @var{index} according to "
1523 "@var{endianness}.")
1524 #define FUNC_NAME s_scm_bytevector_s64_set_x
1526 LARGE_INTEGER_SET (64, signed);
1530 SCM_DEFINE (scm_bytevector_u64_native_set_x
, "bytevector-u64-native-set!",
1532 (SCM bv
, SCM index
, SCM value
),
1533 "Store the unsigned integer @var{value} at index @var{index} "
1534 "of @var{bv} using the native endianness.")
1535 #define FUNC_NAME s_scm_bytevector_u64_native_set_x
1537 LARGE_INTEGER_NATIVE_SET (64, unsigned);
1541 SCM_DEFINE (scm_bytevector_s64_native_set_x
, "bytevector-s64-native-set!",
1543 (SCM bv
, SCM index
, SCM value
),
1544 "Store the signed integer @var{value} at index @var{index} "
1545 "of @var{bv} using the native endianness.")
1546 #define FUNC_NAME s_scm_bytevector_s64_native_set_x
1548 LARGE_INTEGER_NATIVE_SET (64, signed);
1554 /* Operations on IEEE-754 numbers. */
1556 /* There are two possible word endians, visible in glibc's <ieee754.h>.
1557 However, in R6RS, when the endianness is `little', little endian is
1558 assumed for both the byte order and the word order. This is clear from
1559 Section 2.1 of R6RS-lib (in response to
1560 http://www.r6rs.org/formal-comments/comment-187.txt). */
1563 /* Convert to/from a floating-point number with different endianness. This
1564 method is probably not the most efficient but it should be portable. */
1567 float_to_foreign_endianness (union scm_ieee754_float
*target
,
1570 union scm_ieee754_float src
;
1574 #ifdef WORDS_BIGENDIAN
1575 /* Assuming little endian for both byte and word order. */
1576 target
->little_endian
.negative
= src
.big_endian
.negative
;
1577 target
->little_endian
.exponent
= src
.big_endian
.exponent
;
1578 target
->little_endian
.mantissa
= src
.big_endian
.mantissa
;
1580 target
->big_endian
.negative
= src
.little_endian
.negative
;
1581 target
->big_endian
.exponent
= src
.little_endian
.exponent
;
1582 target
->big_endian
.mantissa
= src
.little_endian
.mantissa
;
1587 float_from_foreign_endianness (const union scm_ieee754_float
*source
)
1589 union scm_ieee754_float result
;
1591 #ifdef WORDS_BIGENDIAN
1592 /* Assuming little endian for both byte and word order. */
1593 result
.big_endian
.negative
= source
->little_endian
.negative
;
1594 result
.big_endian
.exponent
= source
->little_endian
.exponent
;
1595 result
.big_endian
.mantissa
= source
->little_endian
.mantissa
;
1597 result
.little_endian
.negative
= source
->big_endian
.negative
;
1598 result
.little_endian
.exponent
= source
->big_endian
.exponent
;
1599 result
.little_endian
.mantissa
= source
->big_endian
.mantissa
;
1606 double_to_foreign_endianness (union scm_ieee754_double
*target
,
1609 union scm_ieee754_double src
;
1613 #ifdef WORDS_BIGENDIAN
1614 /* Assuming little endian for both byte and word order. */
1615 target
->little_little_endian
.negative
= src
.big_endian
.negative
;
1616 target
->little_little_endian
.exponent
= src
.big_endian
.exponent
;
1617 target
->little_little_endian
.mantissa0
= src
.big_endian
.mantissa0
;
1618 target
->little_little_endian
.mantissa1
= src
.big_endian
.mantissa1
;
1620 target
->big_endian
.negative
= src
.little_little_endian
.negative
;
1621 target
->big_endian
.exponent
= src
.little_little_endian
.exponent
;
1622 target
->big_endian
.mantissa0
= src
.little_little_endian
.mantissa0
;
1623 target
->big_endian
.mantissa1
= src
.little_little_endian
.mantissa1
;
1627 static inline double
1628 double_from_foreign_endianness (const union scm_ieee754_double
*source
)
1630 union scm_ieee754_double result
;
1632 #ifdef WORDS_BIGENDIAN
1633 /* Assuming little endian for both byte and word order. */
1634 result
.big_endian
.negative
= source
->little_little_endian
.negative
;
1635 result
.big_endian
.exponent
= source
->little_little_endian
.exponent
;
1636 result
.big_endian
.mantissa0
= source
->little_little_endian
.mantissa0
;
1637 result
.big_endian
.mantissa1
= source
->little_little_endian
.mantissa1
;
1639 result
.little_little_endian
.negative
= source
->big_endian
.negative
;
1640 result
.little_little_endian
.exponent
= source
->big_endian
.exponent
;
1641 result
.little_little_endian
.mantissa0
= source
->big_endian
.mantissa0
;
1642 result
.little_little_endian
.mantissa1
= source
->big_endian
.mantissa1
;
1648 /* Template macros to abstract over doubles and floats.
1649 XXX: Guile can only convert to/from doubles. */
1650 #define IEEE754_UNION(_c_type) union scm_ieee754_ ## _c_type
1651 #define IEEE754_TO_SCM(_c_type) scm_from_double
1652 #define IEEE754_FROM_SCM(_c_type) scm_to_double
1653 #define IEEE754_FROM_FOREIGN_ENDIANNESS(_c_type) \
1654 _c_type ## _from_foreign_endianness
1655 #define IEEE754_TO_FOREIGN_ENDIANNESS(_c_type) \
1656 _c_type ## _to_foreign_endianness
1659 /* FIXME: SCM_VALIDATE_REAL rejects integers, etc. grrr */
1660 #define VALIDATE_REAL(pos, v) \
1662 SCM_ASSERT_TYPE (scm_is_true (scm_rational_p (v)), v, pos, FUNC_NAME, "real"); \
1665 /* Templace getters and setters. */
1667 #define IEEE754_ACCESSOR_PROLOGUE(_type) \
1668 INTEGER_ACCESSOR_PROLOGUE (sizeof (_type) << 3UL, signed);
1670 #define IEEE754_REF(_type) \
1673 IEEE754_ACCESSOR_PROLOGUE (_type); \
1674 SCM_VALIDATE_SYMBOL (3, endianness); \
1676 if (scm_is_eq (endianness, scm_i_native_endianness)) \
1677 memcpy (&c_result, &c_bv[c_index], sizeof (c_result)); \
1680 IEEE754_UNION (_type) c_raw; \
1682 memcpy (&c_raw, &c_bv[c_index], sizeof (c_raw)); \
1684 IEEE754_FROM_FOREIGN_ENDIANNESS (_type) (&c_raw); \
1687 return (IEEE754_TO_SCM (_type) (c_result));
1689 #define IEEE754_NATIVE_REF(_type) \
1692 IEEE754_ACCESSOR_PROLOGUE (_type); \
1694 memcpy (&c_result, &c_bv[c_index], sizeof (c_result)); \
1695 return (IEEE754_TO_SCM (_type) (c_result));
1697 #define IEEE754_SET(_type) \
1700 IEEE754_ACCESSOR_PROLOGUE (_type); \
1701 VALIDATE_REAL (3, value); \
1702 SCM_VALIDATE_SYMBOL (4, endianness); \
1703 c_value = IEEE754_FROM_SCM (_type) (value); \
1705 if (scm_is_eq (endianness, scm_i_native_endianness)) \
1706 memcpy (&c_bv[c_index], &c_value, sizeof (c_value)); \
1709 IEEE754_UNION (_type) c_raw; \
1711 IEEE754_TO_FOREIGN_ENDIANNESS (_type) (&c_raw, c_value); \
1712 memcpy (&c_bv[c_index], &c_raw, sizeof (c_raw)); \
1715 return SCM_UNSPECIFIED;
1717 #define IEEE754_NATIVE_SET(_type) \
1720 IEEE754_ACCESSOR_PROLOGUE (_type); \
1721 VALIDATE_REAL (3, value); \
1722 c_value = IEEE754_FROM_SCM (_type) (value); \
1724 memcpy (&c_bv[c_index], &c_value, sizeof (c_value)); \
1725 return SCM_UNSPECIFIED;
1728 /* Single precision. */
1730 SCM_DEFINE (scm_bytevector_ieee_single_ref
,
1731 "bytevector-ieee-single-ref",
1733 (SCM bv
, SCM index
, SCM endianness
),
1734 "Return the IEEE-754 single from @var{bv} at "
1736 #define FUNC_NAME s_scm_bytevector_ieee_single_ref
1738 IEEE754_REF (float);
1742 SCM_DEFINE (scm_bytevector_ieee_single_native_ref
,
1743 "bytevector-ieee-single-native-ref",
1745 (SCM bv
, SCM index
),
1746 "Return the IEEE-754 single from @var{bv} at "
1747 "@var{index} using the native endianness.")
1748 #define FUNC_NAME s_scm_bytevector_ieee_single_native_ref
1750 IEEE754_NATIVE_REF (float);
1754 SCM_DEFINE (scm_bytevector_ieee_single_set_x
,
1755 "bytevector-ieee-single-set!",
1757 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1758 "Store real @var{value} in @var{bv} at @var{index} according to "
1759 "@var{endianness}.")
1760 #define FUNC_NAME s_scm_bytevector_ieee_single_set_x
1762 IEEE754_SET (float);
1766 SCM_DEFINE (scm_bytevector_ieee_single_native_set_x
,
1767 "bytevector-ieee-single-native-set!",
1769 (SCM bv
, SCM index
, SCM value
),
1770 "Store the real @var{value} at index @var{index} "
1771 "of @var{bv} using the native endianness.")
1772 #define FUNC_NAME s_scm_bytevector_ieee_single_native_set_x
1774 IEEE754_NATIVE_SET (float);
1779 /* Double precision. */
1781 SCM_DEFINE (scm_bytevector_ieee_double_ref
,
1782 "bytevector-ieee-double-ref",
1784 (SCM bv
, SCM index
, SCM endianness
),
1785 "Return the IEEE-754 double from @var{bv} at "
1787 #define FUNC_NAME s_scm_bytevector_ieee_double_ref
1789 IEEE754_REF (double);
1793 SCM_DEFINE (scm_bytevector_ieee_double_native_ref
,
1794 "bytevector-ieee-double-native-ref",
1796 (SCM bv
, SCM index
),
1797 "Return the IEEE-754 double from @var{bv} at "
1798 "@var{index} using the native endianness.")
1799 #define FUNC_NAME s_scm_bytevector_ieee_double_native_ref
1801 IEEE754_NATIVE_REF (double);
1805 SCM_DEFINE (scm_bytevector_ieee_double_set_x
,
1806 "bytevector-ieee-double-set!",
1808 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1809 "Store real @var{value} in @var{bv} at @var{index} according to "
1810 "@var{endianness}.")
1811 #define FUNC_NAME s_scm_bytevector_ieee_double_set_x
1813 IEEE754_SET (double);
1817 SCM_DEFINE (scm_bytevector_ieee_double_native_set_x
,
1818 "bytevector-ieee-double-native-set!",
1820 (SCM bv
, SCM index
, SCM value
),
1821 "Store the real @var{value} at index @var{index} "
1822 "of @var{bv} using the native endianness.")
1823 #define FUNC_NAME s_scm_bytevector_ieee_double_native_set_x
1825 IEEE754_NATIVE_SET (double);
1830 #undef IEEE754_UNION
1831 #undef IEEE754_TO_SCM
1832 #undef IEEE754_FROM_SCM
1833 #undef IEEE754_FROM_FOREIGN_ENDIANNESS
1834 #undef IEEE754_TO_FOREIGN_ENDIANNESS
1836 #undef IEEE754_NATIVE_REF
1838 #undef IEEE754_NATIVE_SET
1841 /* Operations on strings. */
1844 /* Produce a function that returns the length of a UTF-encoded string. */
1845 #define UTF_STRLEN_FUNCTION(_utf_width) \
1846 static inline size_t \
1847 utf ## _utf_width ## _strlen (const uint ## _utf_width ## _t *str) \
1850 const uint ## _utf_width ## _t *ptr; \
1858 return (len * ((_utf_width) / 8)); \
1861 UTF_STRLEN_FUNCTION (8)
1864 /* Return the length (in bytes) of STR, a UTF-(UTF_WIDTH) encoded string. */
1865 #define UTF_STRLEN(_utf_width, _str) \
1866 utf ## _utf_width ## _strlen (_str)
1868 /* Return the "portable" name of the UTF encoding of size UTF_WIDTH and
1869 ENDIANNESS (Gnulib's `iconv_open' module guarantees the portability of the
1872 utf_encoding_name (char *name
, size_t utf_width
, SCM endianness
)
1874 strcpy (name
, "UTF-");
1875 strcat (name
, ((utf_width
== 8)
1877 : ((utf_width
== 16)
1879 : ((utf_width
== 32)
1883 ((scm_is_eq (endianness
, scm_sym_big
))
1885 : ((scm_is_eq (endianness
, scm_sym_little
))
1890 /* Maximum length of a UTF encoding name. */
1891 #define MAX_UTF_ENCODING_NAME_LEN 16
1893 /* Produce the body of a `string->utf' function. */
1894 #define STRING_TO_UTF(_utf_width) \
1898 char c_utf_name[MAX_UTF_ENCODING_NAME_LEN]; \
1899 char *c_utf = NULL, *c_locale; \
1900 size_t c_strlen, c_raw_strlen, c_utf_len = 0; \
1902 SCM_VALIDATE_STRING (1, str); \
1903 if (endianness == SCM_UNDEFINED) \
1904 endianness = scm_sym_big; \
1906 SCM_VALIDATE_SYMBOL (2, endianness); \
1908 c_strlen = scm_c_string_length (str); \
1909 c_raw_strlen = c_strlen * ((_utf_width) / 8); \
1912 c_str = (char *) alloca (c_raw_strlen + 1); \
1913 c_raw_strlen = scm_to_locale_stringbuf (str, c_str, c_strlen); \
1915 while (c_raw_strlen > c_strlen); \
1916 c_str[c_raw_strlen] = '\0'; \
1918 utf_encoding_name (c_utf_name, (_utf_width), endianness); \
1920 c_locale = (char *) alloca (strlen (locale_charset ()) + 1); \
1921 strcpy (c_locale, locale_charset ()); \
1923 err = mem_iconveh (c_str, c_raw_strlen, \
1924 c_locale, c_utf_name, \
1925 iconveh_question_mark, NULL, \
1926 &c_utf, &c_utf_len); \
1927 if (SCM_UNLIKELY (err)) \
1928 scm_syserror_msg (FUNC_NAME, "failed to convert string: ~A", \
1929 scm_list_1 (str), err); \
1932 /* C_UTF is null-terminated. It is malloc(3)-allocated, so we cannot \
1933 use `scm_c_take_bytevector ()'. */ \
1934 scm_dynwind_begin (0); \
1935 scm_dynwind_free (c_utf); \
1937 utf = make_bytevector (c_utf_len, \
1938 SCM_ARRAY_ELEMENT_TYPE_VU8); \
1939 memcpy (SCM_BYTEVECTOR_CONTENTS (utf), c_utf, \
1942 scm_dynwind_end (); \
1949 SCM_DEFINE (scm_string_to_utf8
, "string->utf8",
1952 "Return a newly allocated bytevector that contains the UTF-8 "
1953 "encoding of @var{str}.")
1954 #define FUNC_NAME s_scm_string_to_utf8
1959 size_t c_strlen
, c_raw_strlen
;
1961 SCM_VALIDATE_STRING (1, str
);
1963 c_strlen
= scm_c_string_length (str
);
1964 c_raw_strlen
= c_strlen
;
1967 c_str
= (char *) alloca (c_raw_strlen
+ 1);
1968 c_raw_strlen
= scm_to_locale_stringbuf (str
, c_str
, c_strlen
);
1970 while (c_raw_strlen
> c_strlen
);
1971 c_str
[c_raw_strlen
] = '\0';
1973 c_utf
= u8_strconv_from_locale (c_str
);
1974 if (SCM_UNLIKELY (c_utf
== NULL
))
1975 scm_syserror (FUNC_NAME
);
1978 /* C_UTF is null-terminated. It is malloc(3)-allocated, so we cannot
1979 use `scm_c_take_bytevector ()'. */
1980 scm_dynwind_begin (0);
1981 scm_dynwind_free (c_utf
);
1983 utf
= make_bytevector (UTF_STRLEN (8, c_utf
),
1984 SCM_ARRAY_ELEMENT_TYPE_VU8
);
1985 memcpy (SCM_BYTEVECTOR_CONTENTS (utf
), c_utf
,
1986 UTF_STRLEN (8, c_utf
));
1995 SCM_DEFINE (scm_string_to_utf16
, "string->utf16",
1997 (SCM str
, SCM endianness
),
1998 "Return a newly allocated bytevector that contains the UTF-16 "
1999 "encoding of @var{str}.")
2000 #define FUNC_NAME s_scm_string_to_utf16
2006 SCM_DEFINE (scm_string_to_utf32
, "string->utf32",
2008 (SCM str
, SCM endianness
),
2009 "Return a newly allocated bytevector that contains the UTF-32 "
2010 "encoding of @var{str}.")
2011 #define FUNC_NAME s_scm_string_to_utf32
2018 /* Produce the body of a function that converts a UTF-encoded bytevector to a
2020 #define UTF_TO_STRING(_utf_width) \
2021 SCM str = SCM_BOOL_F; \
2023 char *c_str = NULL, *c_locale; \
2024 char c_utf_name[MAX_UTF_ENCODING_NAME_LEN]; \
2025 const char *c_utf; \
2026 size_t c_strlen = 0, c_utf_len; \
2028 SCM_VALIDATE_BYTEVECTOR (1, utf); \
2029 if (endianness == SCM_UNDEFINED) \
2030 endianness = scm_sym_big; \
2032 SCM_VALIDATE_SYMBOL (2, endianness); \
2034 c_utf_len = SCM_BYTEVECTOR_LENGTH (utf); \
2035 c_utf = (char *) SCM_BYTEVECTOR_CONTENTS (utf); \
2036 utf_encoding_name (c_utf_name, (_utf_width), endianness); \
2038 c_locale = (char *) alloca (strlen (locale_charset ()) + 1); \
2039 strcpy (c_locale, locale_charset ()); \
2041 err = mem_iconveh (c_utf, c_utf_len, \
2042 c_utf_name, c_locale, \
2043 iconveh_question_mark, NULL, \
2044 &c_str, &c_strlen); \
2045 if (SCM_UNLIKELY (err)) \
2046 scm_syserror_msg (FUNC_NAME, "failed to convert to string: ~A", \
2047 scm_list_1 (utf), err); \
2049 /* C_STR is null-terminated. */ \
2050 str = scm_take_locale_stringn (c_str, c_strlen); \
2055 SCM_DEFINE (scm_utf8_to_string
, "utf8->string",
2058 "Return a newly allocate string that contains from the UTF-8-"
2059 "encoded contents of bytevector @var{utf}.")
2060 #define FUNC_NAME s_scm_utf8_to_string
2064 char *c_str
= NULL
, *c_locale
;
2066 size_t c_utf_len
, c_strlen
= 0;
2068 SCM_VALIDATE_BYTEVECTOR (1, utf
);
2070 c_utf_len
= SCM_BYTEVECTOR_LENGTH (utf
);
2072 c_locale
= (char *) alloca (strlen (locale_charset ()) + 1);
2073 strcpy (c_locale
, locale_charset ());
2075 c_utf
= (char *) SCM_BYTEVECTOR_CONTENTS (utf
);
2076 err
= mem_iconveh (c_utf
, c_utf_len
,
2078 iconveh_question_mark
, NULL
,
2080 if (SCM_UNLIKELY (err
))
2081 scm_syserror_msg (FUNC_NAME
, "failed to convert to string: ~A",
2082 scm_list_1 (utf
), err
);
2084 /* C_STR is null-terminated. */
2085 str
= scm_take_locale_stringn (c_str
, c_strlen
);
2091 SCM_DEFINE (scm_utf16_to_string
, "utf16->string",
2093 (SCM utf
, SCM endianness
),
2094 "Return a newly allocate string that contains from the UTF-16-"
2095 "encoded contents of bytevector @var{utf}.")
2096 #define FUNC_NAME s_scm_utf16_to_string
2102 SCM_DEFINE (scm_utf32_to_string
, "utf32->string",
2104 (SCM utf
, SCM endianness
),
2105 "Return a newly allocate string that contains from the UTF-32-"
2106 "encoded contents of bytevector @var{utf}.")
2107 #define FUNC_NAME s_scm_utf32_to_string
2115 /* Bytevectors as generalized vectors & arrays. */
2119 bytevector_ref_c32 (SCM bv
, SCM idx
)
2120 { /* FIXME add some checks */
2121 const float *contents
= (const float*)SCM_BYTEVECTOR_CONTENTS (bv
);
2122 size_t i
= scm_to_size_t (idx
);
2123 return scm_c_make_rectangular (contents
[i
/8], contents
[i
/8 + 1]);
2127 bytevector_ref_c64 (SCM bv
, SCM idx
)
2128 { /* FIXME add some checks */
2129 const double *contents
= (const double*)SCM_BYTEVECTOR_CONTENTS (bv
);
2130 size_t i
= scm_to_size_t (idx
);
2131 return scm_c_make_rectangular (contents
[i
/16], contents
[i
/16 + 1]);
2134 typedef SCM (*scm_t_bytevector_ref_fn
)(SCM
, SCM
);
2136 const scm_t_bytevector_ref_fn bytevector_ref_fns
[SCM_ARRAY_ELEMENT_TYPE_LAST
+ 1] =
2141 scm_bytevector_u8_ref
, /* VU8 */
2142 scm_bytevector_u8_ref
, /* U8 */
2143 scm_bytevector_s8_ref
,
2144 scm_bytevector_u16_native_ref
,
2145 scm_bytevector_s16_native_ref
,
2146 scm_bytevector_u32_native_ref
,
2147 scm_bytevector_s32_native_ref
,
2148 scm_bytevector_u64_native_ref
,
2149 scm_bytevector_s64_native_ref
,
2150 scm_bytevector_ieee_single_native_ref
,
2151 scm_bytevector_ieee_double_native_ref
,
2157 bv_handle_ref (scm_t_array_handle
*h
, size_t index
)
2160 scm_t_bytevector_ref_fn ref_fn
;
2162 ref_fn
= bytevector_ref_fns
[h
->element_type
];
2164 scm_from_size_t (index
* scm_array_handle_uniform_element_size (h
));
2165 return ref_fn (h
->array
, byte_index
);
2169 bytevector_set_c32 (SCM bv
, SCM idx
, SCM val
)
2170 { /* checks are unnecessary here */
2171 float *contents
= (float*)SCM_BYTEVECTOR_CONTENTS (bv
);
2172 size_t i
= scm_to_size_t (idx
);
2173 contents
[i
/8] = scm_c_real_part (val
);
2174 contents
[i
/8 + 1] = scm_c_imag_part (val
);
2175 return SCM_UNSPECIFIED
;
2179 bytevector_set_c64 (SCM bv
, SCM idx
, SCM val
)
2180 { /* checks are unnecessary here */
2181 double *contents
= (double*)SCM_BYTEVECTOR_CONTENTS (bv
);
2182 size_t i
= scm_to_size_t (idx
);
2183 contents
[i
/16] = scm_c_real_part (val
);
2184 contents
[i
/16 + 1] = scm_c_imag_part (val
);
2185 return SCM_UNSPECIFIED
;
2188 typedef SCM (*scm_t_bytevector_set_fn
)(SCM
, SCM
, SCM
);
2190 const scm_t_bytevector_set_fn bytevector_set_fns
[SCM_ARRAY_ELEMENT_TYPE_LAST
+ 1] =
2195 scm_bytevector_u8_set_x
, /* VU8 */
2196 scm_bytevector_u8_set_x
, /* U8 */
2197 scm_bytevector_s8_set_x
,
2198 scm_bytevector_u16_native_set_x
,
2199 scm_bytevector_s16_native_set_x
,
2200 scm_bytevector_u32_native_set_x
,
2201 scm_bytevector_s32_native_set_x
,
2202 scm_bytevector_u64_native_set_x
,
2203 scm_bytevector_s64_native_set_x
,
2204 scm_bytevector_ieee_single_native_set_x
,
2205 scm_bytevector_ieee_double_native_set_x
,
2211 bv_handle_set_x (scm_t_array_handle
*h
, size_t index
, SCM val
)
2214 scm_t_bytevector_set_fn set_fn
;
2216 set_fn
= bytevector_set_fns
[h
->element_type
];
2218 scm_from_size_t (index
* scm_array_handle_uniform_element_size (h
));
2219 set_fn (h
->array
, byte_index
, val
);
2223 bytevector_get_handle (SCM v
, scm_t_array_handle
*h
)
2229 h
->dim0
.ubnd
= SCM_BYTEVECTOR_TYPED_LENGTH (v
) - 1;
2231 h
->element_type
= SCM_BYTEVECTOR_ELEMENT_TYPE (v
);
2232 h
->elements
= h
->writable_elements
= SCM_BYTEVECTOR_CONTENTS (v
);
2236 /* Initialization. */
2239 scm_bootstrap_bytevectors (void)
2241 /* This must be instantiated here because the generalized-vector API may
2242 want to access bytevectors even though `(rnrs bytevector)' hasn't been
2244 scm_null_bytevector
=
2245 scm_gc_protect_object
2246 (make_bytevector_from_buffer (0, NULL
, SCM_ARRAY_ELEMENT_TYPE_VU8
));
2248 #ifdef WORDS_BIGENDIAN
2249 scm_i_native_endianness
= scm_permanent_object (scm_from_locale_symbol ("big"));
2251 scm_i_native_endianness
= scm_permanent_object (scm_from_locale_symbol ("little"));
2254 scm_c_register_extension ("libguile", "scm_init_bytevectors",
2255 (scm_t_extension_init_func
) scm_init_bytevectors
,
2259 scm_t_array_implementation impl
;
2261 impl
.tag
= scm_tc7_bytevector
;
2263 impl
.vref
= bv_handle_ref
;
2264 impl
.vset
= bv_handle_set_x
;
2265 impl
.get_handle
= bytevector_get_handle
;
2266 scm_i_register_array_implementation (&impl
);
2267 scm_i_register_vector_constructor
2268 (scm_i_array_element_types
[SCM_ARRAY_ELEMENT_TYPE_VU8
],
2269 scm_make_bytevector
);
2274 scm_init_bytevectors (void)
2276 #include "libguile/bytevectors.x"
2278 scm_endianness_big
= scm_sym_big
;
2279 scm_endianness_little
= scm_sym_little
;