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 scm_t_bits scm_tc16_bytevector
;
180 #define SCM_BYTEVECTOR_INLINE_THRESHOLD (2 * sizeof (SCM))
181 #define SCM_BYTEVECTOR_INLINEABLE_SIZE_P(_size) \
182 ((_size) <= SCM_BYTEVECTOR_INLINE_THRESHOLD)
183 #define SCM_BYTEVECTOR_SET_LENGTH(_bv, _len) \
184 SCM_SET_SMOB_DATA ((_bv), (scm_t_bits) (_len))
185 #define SCM_BYTEVECTOR_SET_CONTENTS(_bv, _buf) \
186 SCM_SET_SMOB_DATA_2 ((_bv), (scm_t_bits) (_buf))
187 #define SCM_BYTEVECTOR_SET_INLINE(bv) \
188 SCM_SET_SMOB_FLAGS (bv, SCM_SMOB_FLAGS (bv) | SCM_F_BYTEVECTOR_INLINE)
189 #define SCM_BYTEVECTOR_SET_ELEMENT_TYPE(bv, hint) \
190 SCM_SET_SMOB_FLAGS (bv, (SCM_SMOB_FLAGS (bv) & 0xFF) | (hint << 8))
191 #define SCM_BYTEVECTOR_TYPE_SIZE(var) \
192 (scm_i_array_element_type_sizes[SCM_BYTEVECTOR_ELEMENT_TYPE (var)]/8)
193 #define SCM_BYTEVECTOR_TYPED_LENGTH(var) \
194 SCM_BYTEVECTOR_LENGTH (var) / SCM_BYTEVECTOR_TYPE_SIZE (var)
196 /* The empty bytevector. */
197 SCM scm_null_bytevector
= SCM_UNSPECIFIED
;
201 make_bytevector_from_buffer (size_t len
, void *contents
,
202 scm_t_array_element_type element_type
)
207 if (SCM_UNLIKELY (element_type
> SCM_ARRAY_ELEMENT_TYPE_LAST
208 || scm_i_array_element_type_sizes
[element_type
] < 8
209 || len
>= (SCM_I_SIZE_MAX
210 / (scm_i_array_element_type_sizes
[element_type
]/8))))
211 /* This would be an internal Guile programming error */
214 c_len
= len
* (scm_i_array_element_type_sizes
[element_type
] / 8);
215 if (!SCM_BYTEVECTOR_INLINEABLE_SIZE_P (c_len
))
216 SCM_NEWSMOB2 (ret
, scm_tc16_bytevector
, c_len
, contents
);
219 SCM_NEWSMOB2 (ret
, scm_tc16_bytevector
, c_len
, NULL
);
220 SCM_BYTEVECTOR_SET_INLINE (ret
);
223 memcpy (SCM_BYTEVECTOR_CONTENTS (ret
), contents
, c_len
);
224 scm_gc_free (contents
, c_len
, SCM_GC_BYTEVECTOR
);
227 SCM_BYTEVECTOR_SET_ELEMENT_TYPE (ret
, element_type
);
232 make_bytevector (size_t len
, scm_t_array_element_type element_type
)
236 if (SCM_UNLIKELY (len
== 0 && element_type
== 0))
237 return scm_null_bytevector
;
238 else if (SCM_UNLIKELY (element_type
> SCM_ARRAY_ELEMENT_TYPE_LAST
239 || scm_i_array_element_type_sizes
[element_type
] < 8
240 || len
>= (SCM_I_SIZE_MAX
241 / (scm_i_array_element_type_sizes
[element_type
]/8))))
242 /* This would be an internal Guile programming error */
245 c_len
= len
* (scm_i_array_element_type_sizes
[element_type
]/8);
246 if (SCM_BYTEVECTOR_INLINEABLE_SIZE_P (c_len
))
249 SCM_NEWSMOB2 (ret
, scm_tc16_bytevector
, c_len
, NULL
);
250 SCM_BYTEVECTOR_SET_INLINE (ret
);
251 SCM_BYTEVECTOR_SET_ELEMENT_TYPE (ret
, element_type
);
256 void *buf
= scm_gc_malloc (c_len
, SCM_GC_BYTEVECTOR
);
257 return make_bytevector_from_buffer (len
, buf
, element_type
);
261 /* Return a new bytevector of size LEN octets. */
263 scm_c_make_bytevector (size_t len
)
265 return make_bytevector (len
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
268 /* Return a new bytevector of size LEN elements. */
270 scm_i_make_typed_bytevector (size_t len
, scm_t_array_element_type element_type
)
272 return make_bytevector (len
, element_type
);
275 /* Return a bytevector of size LEN made up of CONTENTS. The area pointed to
276 by CONTENTS must have been allocated using `scm_gc_malloc ()'. */
278 scm_c_take_bytevector (signed char *contents
, size_t len
)
280 return make_bytevector_from_buffer (len
, contents
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
284 scm_c_take_typed_bytevector (signed char *contents
, size_t len
,
285 scm_t_array_element_type element_type
)
287 return make_bytevector_from_buffer (len
, contents
, element_type
);
290 /* Shrink BV to C_NEW_LEN (which is assumed to be smaller than its current
291 size) and return BV. */
293 scm_i_shrink_bytevector (SCM bv
, size_t c_new_len
)
295 if (SCM_UNLIKELY (c_new_len
% SCM_BYTEVECTOR_TYPE_SIZE (bv
)))
296 /* This would be an internal Guile programming error */
299 if (!SCM_BYTEVECTOR_INLINE_P (bv
))
302 signed char *c_bv
, *c_new_bv
;
304 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
305 c_bv
= SCM_BYTEVECTOR_CONTENTS (bv
);
307 SCM_BYTEVECTOR_SET_LENGTH (bv
, c_new_len
);
309 if (SCM_BYTEVECTOR_INLINEABLE_SIZE_P (c_new_len
))
311 /* Copy to the in-line buffer and free the current buffer. */
312 SCM_BYTEVECTOR_SET_INLINE (bv
);
313 c_new_bv
= SCM_BYTEVECTOR_CONTENTS (bv
);
314 memcpy (c_new_bv
, c_bv
, c_new_len
);
315 scm_gc_free (c_bv
, c_len
, SCM_GC_BYTEVECTOR
);
319 /* Resize the existing buffer. */
320 c_new_bv
= scm_gc_realloc (c_bv
, c_len
, c_new_len
,
322 SCM_BYTEVECTOR_SET_CONTENTS (bv
, c_new_bv
);
326 SCM_BYTEVECTOR_SET_LENGTH (bv
, c_new_len
);
332 scm_is_bytevector (SCM obj
)
334 return SCM_SMOB_PREDICATE (scm_tc16_bytevector
, obj
);
338 scm_c_bytevector_length (SCM bv
)
339 #define FUNC_NAME "scm_c_bytevector_length"
341 SCM_VALIDATE_BYTEVECTOR (1, bv
);
343 return SCM_BYTEVECTOR_LENGTH (bv
);
348 scm_c_bytevector_ref (SCM bv
, size_t index
)
349 #define FUNC_NAME "scm_c_bytevector_ref"
352 const scm_t_uint8
*c_bv
;
354 SCM_VALIDATE_BYTEVECTOR (1, bv
);
356 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
357 c_bv
= (scm_t_uint8
*) SCM_BYTEVECTOR_CONTENTS (bv
);
359 if (SCM_UNLIKELY (index
>= c_len
))
360 scm_out_of_range (FUNC_NAME
, scm_from_size_t (index
));
367 scm_c_bytevector_set_x (SCM bv
, size_t index
, scm_t_uint8 value
)
368 #define FUNC_NAME "scm_c_bytevector_set_x"
373 SCM_VALIDATE_BYTEVECTOR (1, bv
);
375 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
376 c_bv
= (scm_t_uint8
*) SCM_BYTEVECTOR_CONTENTS (bv
);
378 if (SCM_UNLIKELY (index
>= c_len
))
379 scm_out_of_range (FUNC_NAME
, scm_from_size_t (index
));
390 print_bytevector (SCM bv
, SCM port
, scm_print_state
*pstate SCM_UNUSED
)
392 ssize_t ubnd
, inc
, i
;
393 scm_t_array_handle h
;
395 scm_array_get_handle (bv
, &h
);
397 scm_putc ('#', port
);
398 scm_write (scm_array_handle_element_type (&h
), port
);
399 scm_putc ('(', port
);
400 for (i
= h
.dims
[0].lbnd
, ubnd
= h
.dims
[0].ubnd
, inc
= h
.dims
[0].inc
;
404 scm_putc (' ', port
);
405 scm_write (scm_array_handle_ref (&h
, i
), port
);
407 scm_putc (')', port
);
413 bytevector_equal_p (SCM bv1
, SCM bv2
)
415 return scm_bytevector_eq_p (bv1
, bv2
);
419 free_bytevector (SCM bv
)
422 if (!SCM_BYTEVECTOR_INLINE_P (bv
))
427 c_bv
= SCM_BYTEVECTOR_CONTENTS (bv
);
428 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
430 scm_gc_free (c_bv
, c_len
, SCM_GC_BYTEVECTOR
);
438 /* General operations. */
440 SCM_SYMBOL (scm_sym_big
, "big");
441 SCM_SYMBOL (scm_sym_little
, "little");
443 SCM scm_endianness_big
, scm_endianness_little
;
445 /* Host endianness (a symbol). */
446 SCM scm_i_native_endianness
= SCM_UNSPECIFIED
;
450 # define bswap_24(_x) \
451 ((((_x) & 0xff0000) >> 16) | \
452 (((_x) & 0x00ff00)) | \
453 (((_x) & 0x0000ff) << 16))
457 SCM_DEFINE (scm_native_endianness
, "native-endianness", 0, 0, 0,
459 "Return a symbol denoting the machine's native endianness.")
460 #define FUNC_NAME s_scm_native_endianness
462 return scm_i_native_endianness
;
466 SCM_DEFINE (scm_bytevector_p
, "bytevector?", 1, 0, 0,
468 "Return true if @var{obj} is a bytevector.")
469 #define FUNC_NAME s_scm_bytevector_p
471 return scm_from_bool (scm_is_bytevector (obj
));
475 SCM_DEFINE (scm_make_bytevector
, "make-bytevector", 1, 1, 0,
477 "Return a newly allocated bytevector of @var{len} bytes, "
478 "optionally filled with @var{fill}.")
479 #define FUNC_NAME s_scm_make_bytevector
483 signed char c_fill
= '\0';
485 SCM_VALIDATE_UINT_COPY (1, len
, c_len
);
486 if (fill
!= SCM_UNDEFINED
)
490 value
= scm_to_int (fill
);
491 if (SCM_UNLIKELY ((value
< -128) || (value
> 255)))
492 scm_out_of_range (FUNC_NAME
, fill
);
493 c_fill
= (signed char) value
;
496 bv
= make_bytevector (c_len
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
497 if (fill
!= SCM_UNDEFINED
)
500 signed char *contents
;
502 contents
= SCM_BYTEVECTOR_CONTENTS (bv
);
503 for (i
= 0; i
< c_len
; i
++)
504 contents
[i
] = c_fill
;
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 unsigned 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
)
537 signed char *c_bv1
, *c_bv2
;
539 c_bv1
= SCM_BYTEVECTOR_CONTENTS (bv1
);
540 c_bv2
= SCM_BYTEVECTOR_CONTENTS (bv2
);
542 result
= scm_from_bool (!memcmp (c_bv1
, c_bv2
, c_len1
));
549 SCM_DEFINE (scm_bytevector_fill_x
, "bytevector-fill!", 2, 0, 0,
551 "Fill bytevector @var{bv} with @var{fill}, a byte.")
552 #define FUNC_NAME s_scm_bytevector_fill_x
555 signed char *c_bv
, c_fill
;
557 SCM_VALIDATE_BYTEVECTOR (1, bv
);
558 c_fill
= scm_to_int8 (fill
);
560 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
561 c_bv
= SCM_BYTEVECTOR_CONTENTS (bv
);
563 for (i
= 0; i
< c_len
; i
++)
566 return SCM_UNSPECIFIED
;
570 SCM_DEFINE (scm_bytevector_copy_x
, "bytevector-copy!", 5, 0, 0,
571 (SCM source
, SCM source_start
, SCM target
, SCM target_start
,
573 "Copy @var{len} bytes from @var{source} into @var{target}, "
574 "starting reading from @var{source_start} (a positive index "
575 "within @var{source}) and start writing at "
576 "@var{target_start}.")
577 #define FUNC_NAME s_scm_bytevector_copy_x
579 unsigned c_len
, c_source_len
, c_target_len
;
580 unsigned c_source_start
, c_target_start
;
581 signed char *c_source
, *c_target
;
583 SCM_VALIDATE_BYTEVECTOR (1, source
);
584 SCM_VALIDATE_BYTEVECTOR (3, target
);
586 c_len
= scm_to_uint (len
);
587 c_source_start
= scm_to_uint (source_start
);
588 c_target_start
= scm_to_uint (target_start
);
590 c_source
= SCM_BYTEVECTOR_CONTENTS (source
);
591 c_target
= SCM_BYTEVECTOR_CONTENTS (target
);
592 c_source_len
= SCM_BYTEVECTOR_LENGTH (source
);
593 c_target_len
= SCM_BYTEVECTOR_LENGTH (target
);
595 if (SCM_UNLIKELY (c_source_start
+ c_len
> c_source_len
))
596 scm_out_of_range (FUNC_NAME
, source_start
);
597 if (SCM_UNLIKELY (c_target_start
+ c_len
> c_target_len
))
598 scm_out_of_range (FUNC_NAME
, target_start
);
600 memcpy (c_target
+ c_target_start
,
601 c_source
+ c_source_start
,
604 return SCM_UNSPECIFIED
;
608 SCM_DEFINE (scm_bytevector_copy
, "bytevector-copy", 1, 0, 0,
610 "Return a newly allocated copy of @var{bv}.")
611 #define FUNC_NAME s_scm_bytevector_copy
615 signed char *c_bv
, *c_copy
;
617 SCM_VALIDATE_BYTEVECTOR (1, bv
);
619 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
620 c_bv
= SCM_BYTEVECTOR_CONTENTS (bv
);
622 copy
= make_bytevector (c_len
, SCM_BYTEVECTOR_ELEMENT_TYPE (bv
));
623 c_copy
= SCM_BYTEVECTOR_CONTENTS (copy
);
624 memcpy (c_copy
, c_bv
, c_len
);
630 SCM_DEFINE (scm_uniform_array_to_bytevector
, "uniform-array->bytevector",
631 1, 0, 0, (SCM array
),
632 "Return a newly allocated bytevector whose contents\n"
633 "will be copied from the uniform array @var{array}.")
634 #define FUNC_NAME s_scm_uniform_array_to_bytevector
638 scm_t_array_handle h
;
642 contents
= scm_array_contents (array
, SCM_BOOL_T
);
643 if (scm_is_false (contents
))
644 scm_wrong_type_arg_msg (FUNC_NAME
, 0, array
, "uniform contiguous array");
646 scm_array_get_handle (contents
, &h
);
648 base
= scm_array_handle_uniform_elements (&h
);
649 len
= h
.dims
->inc
* (h
.dims
->ubnd
- h
.dims
->lbnd
+ 1);
650 sz
= scm_array_handle_uniform_element_size (&h
);
652 ret
= make_bytevector (len
* sz
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
653 memcpy (SCM_BYTEVECTOR_CONTENTS (ret
), base
, len
* sz
);
655 scm_array_handle_release (&h
);
662 /* Operations on bytes and octets. */
664 SCM_DEFINE (scm_bytevector_u8_ref
, "bytevector-u8-ref", 2, 0, 0,
666 "Return the octet located at @var{index} in @var{bv}.")
667 #define FUNC_NAME s_scm_bytevector_u8_ref
669 INTEGER_NATIVE_REF (8, unsigned);
673 SCM_DEFINE (scm_bytevector_s8_ref
, "bytevector-s8-ref", 2, 0, 0,
675 "Return the byte located at @var{index} in @var{bv}.")
676 #define FUNC_NAME s_scm_bytevector_s8_ref
678 INTEGER_NATIVE_REF (8, signed);
682 SCM_DEFINE (scm_bytevector_u8_set_x
, "bytevector-u8-set!", 3, 0, 0,
683 (SCM bv
, SCM index
, SCM value
),
684 "Return the octet located at @var{index} in @var{bv}.")
685 #define FUNC_NAME s_scm_bytevector_u8_set_x
687 INTEGER_NATIVE_SET (8, unsigned);
691 SCM_DEFINE (scm_bytevector_s8_set_x
, "bytevector-s8-set!", 3, 0, 0,
692 (SCM bv
, SCM index
, SCM value
),
693 "Return the octet located at @var{index} in @var{bv}.")
694 #define FUNC_NAME s_scm_bytevector_s8_set_x
696 INTEGER_NATIVE_SET (8, signed);
700 #undef OCTET_ACCESSOR_PROLOGUE
703 SCM_DEFINE (scm_bytevector_to_u8_list
, "bytevector->u8-list", 1, 0, 0,
705 "Return a newly allocated list of octets containing the "
706 "contents of @var{bv}.")
707 #define FUNC_NAME s_scm_bytevector_to_u8_list
713 SCM_VALIDATE_BYTEVECTOR (1, bv
);
715 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
716 c_bv
= (unsigned char *) SCM_BYTEVECTOR_CONTENTS (bv
);
718 lst
= scm_make_list (scm_from_uint (c_len
), SCM_UNSPECIFIED
);
719 for (i
= 0, pair
= lst
;
721 i
++, pair
= SCM_CDR (pair
))
723 SCM_SETCAR (pair
, SCM_I_MAKINUM (c_bv
[i
]));
730 SCM_DEFINE (scm_u8_list_to_bytevector
, "u8-list->bytevector", 1, 0, 0,
732 "Turn @var{lst}, a list of octets, into a bytevector.")
733 #define FUNC_NAME s_scm_u8_list_to_bytevector
739 SCM_VALIDATE_LIST_COPYLEN (1, lst
, c_len
);
741 bv
= make_bytevector (c_len
, SCM_ARRAY_ELEMENT_TYPE_VU8
);
742 c_bv
= (unsigned char *) SCM_BYTEVECTOR_CONTENTS (bv
);
744 for (i
= 0; i
< c_len
; lst
= SCM_CDR (lst
), i
++)
746 item
= SCM_CAR (lst
);
748 if (SCM_LIKELY (SCM_I_INUMP (item
)))
752 c_item
= SCM_I_INUM (item
);
753 if (SCM_LIKELY ((c_item
>= 0) && (c_item
< 256)))
754 c_bv
[i
] = (unsigned char) c_item
;
765 scm_wrong_type_arg (FUNC_NAME
, 1, item
);
771 /* Compute the two's complement of VALUE (a positive integer) on SIZE octets
772 using (2^(SIZE * 8) - VALUE). */
774 twos_complement (mpz_t value
, size_t size
)
776 unsigned long bit_count
;
778 /* We expect BIT_COUNT to fit in a unsigned long thanks to the range
779 checking on SIZE performed earlier. */
780 bit_count
= (unsigned long) size
<< 3UL;
782 if (SCM_LIKELY (bit_count
< sizeof (unsigned long)))
783 mpz_ui_sub (value
, 1UL << bit_count
, value
);
789 mpz_ui_pow_ui (max
, 2, bit_count
);
790 mpz_sub (value
, max
, value
);
796 bytevector_large_ref (const char *c_bv
, size_t c_size
, int signed_p
,
801 int c_endianness
, negative_p
= 0;
805 if (scm_is_eq (endianness
, scm_sym_big
))
806 negative_p
= c_bv
[0] & 0x80;
808 negative_p
= c_bv
[c_size
- 1] & 0x80;
811 c_endianness
= scm_is_eq (endianness
, scm_sym_big
) ? 1 : -1;
814 mpz_import (c_mpz
, 1 /* 1 word */, 1 /* word order doesn't matter */,
815 c_size
/* word is C_SIZE-byte long */,
817 0 /* nails */, c_bv
);
819 if (signed_p
&& negative_p
)
821 twos_complement (c_mpz
, c_size
);
822 mpz_neg (c_mpz
, c_mpz
);
825 result
= scm_from_mpz (c_mpz
);
826 mpz_clear (c_mpz
); /* FIXME: Needed? */
832 bytevector_large_set (char *c_bv
, size_t c_size
, int signed_p
,
833 SCM value
, SCM endianness
)
836 int c_endianness
, c_sign
, err
= 0;
838 c_endianness
= scm_is_eq (endianness
, scm_sym_big
) ? 1 : -1;
841 scm_to_mpz (value
, c_mpz
);
843 c_sign
= mpz_sgn (c_mpz
);
846 if (SCM_LIKELY (signed_p
))
848 mpz_neg (c_mpz
, c_mpz
);
849 twos_complement (c_mpz
, c_size
);
860 memset (c_bv
, 0, c_size
);
863 size_t word_count
, value_size
;
865 value_size
= (mpz_sizeinbase (c_mpz
, 2) + (8 * c_size
)) / (8 * c_size
);
866 if (SCM_UNLIKELY (value_size
> c_size
))
873 mpz_export (c_bv
, &word_count
, 1 /* word order doesn't matter */,
874 c_size
, c_endianness
,
875 0 /* nails */, c_mpz
);
876 if (SCM_UNLIKELY (word_count
!= 1))
877 /* Shouldn't happen since we already checked with VALUE_SIZE. */
887 #define GENERIC_INTEGER_ACCESSOR_PROLOGUE(_sign) \
888 unsigned long c_len, c_index, c_size; \
891 SCM_VALIDATE_BYTEVECTOR (1, bv); \
892 c_index = scm_to_ulong (index); \
893 c_size = scm_to_ulong (size); \
895 c_len = SCM_BYTEVECTOR_LENGTH (bv); \
896 c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
898 /* C_SIZE must have its 3 higher bits set to zero so that \
899 multiplying it by 8 yields a number that fits in an \
901 if (SCM_UNLIKELY ((c_size == 0) || (c_size >= (ULONG_MAX >> 3L)))) \
902 scm_out_of_range (FUNC_NAME, size); \
903 if (SCM_UNLIKELY (c_index + c_size > c_len)) \
904 scm_out_of_range (FUNC_NAME, index);
907 /* Template of an integer reference function. */
908 #define GENERIC_INTEGER_REF(_sign) \
916 swap = !scm_is_eq (endianness, scm_i_native_endianness); \
921 _sign char c_value8; \
922 memcpy (&c_value8, c_bv, 1); \
928 INT_TYPE (16, _sign) c_value16; \
929 memcpy (&c_value16, c_bv, 2); \
931 value = (INT_TYPE (16, _sign)) bswap_16 (c_value16); \
940 result = SCM_I_MAKINUM ((_sign int) value); \
943 result = bytevector_large_ref ((char *) c_bv, \
944 c_size, SIGNEDNESS (_sign), \
950 bytevector_signed_ref (const char *c_bv
, size_t c_size
, SCM endianness
)
952 GENERIC_INTEGER_REF (signed);
956 bytevector_unsigned_ref (const char *c_bv
, size_t c_size
, SCM endianness
)
958 GENERIC_INTEGER_REF (unsigned);
962 /* Template of an integer assignment function. */
963 #define GENERIC_INTEGER_SET(_sign) \
968 if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
971 c_value = SCM_I_INUM (value); \
975 if (SCM_LIKELY (INT_VALID_P (8, _sign) (c_value))) \
977 _sign char c_value8; \
978 c_value8 = (_sign char) c_value; \
979 memcpy (c_bv, &c_value8, 1); \
986 if (SCM_LIKELY (INT_VALID_P (16, _sign) (c_value))) \
989 INT_TYPE (16, _sign) c_value16; \
991 swap = !scm_is_eq (endianness, scm_i_native_endianness); \
994 c_value16 = (INT_TYPE (16, _sign)) bswap_16 (c_value); \
996 c_value16 = c_value; \
998 memcpy (c_bv, &c_value16, 2); \
1012 err = bytevector_large_set (c_bv, c_size, \
1013 SIGNEDNESS (_sign), \
1014 value, endianness); \
1022 scm_out_of_range (FUNC_NAME, value); \
1026 bytevector_signed_set (char *c_bv
, size_t c_size
,
1027 SCM value
, SCM endianness
,
1028 const char *func_name
)
1029 #define FUNC_NAME func_name
1031 GENERIC_INTEGER_SET (signed);
1036 bytevector_unsigned_set (char *c_bv
, size_t c_size
,
1037 SCM value
, SCM endianness
,
1038 const char *func_name
)
1039 #define FUNC_NAME func_name
1041 GENERIC_INTEGER_SET (unsigned);
1045 #undef GENERIC_INTEGER_SET
1046 #undef GENERIC_INTEGER_REF
1049 SCM_DEFINE (scm_bytevector_uint_ref
, "bytevector-uint-ref", 4, 0, 0,
1050 (SCM bv
, SCM index
, SCM endianness
, SCM size
),
1051 "Return the @var{size}-octet long unsigned integer at index "
1052 "@var{index} in @var{bv}.")
1053 #define FUNC_NAME s_scm_bytevector_uint_ref
1055 GENERIC_INTEGER_ACCESSOR_PROLOGUE (unsigned);
1057 return (bytevector_unsigned_ref (&c_bv
[c_index
], c_size
, endianness
));
1061 SCM_DEFINE (scm_bytevector_sint_ref
, "bytevector-sint-ref", 4, 0, 0,
1062 (SCM bv
, SCM index
, SCM endianness
, SCM size
),
1063 "Return the @var{size}-octet long unsigned integer at index "
1064 "@var{index} in @var{bv}.")
1065 #define FUNC_NAME s_scm_bytevector_sint_ref
1067 GENERIC_INTEGER_ACCESSOR_PROLOGUE (signed);
1069 return (bytevector_signed_ref (&c_bv
[c_index
], c_size
, endianness
));
1073 SCM_DEFINE (scm_bytevector_uint_set_x
, "bytevector-uint-set!", 5, 0, 0,
1074 (SCM bv
, SCM index
, SCM value
, SCM endianness
, SCM size
),
1075 "Set the @var{size}-octet long unsigned integer at @var{index} "
1077 #define FUNC_NAME s_scm_bytevector_uint_set_x
1079 GENERIC_INTEGER_ACCESSOR_PROLOGUE (unsigned);
1081 bytevector_unsigned_set (&c_bv
[c_index
], c_size
, value
, endianness
,
1084 return SCM_UNSPECIFIED
;
1088 SCM_DEFINE (scm_bytevector_sint_set_x
, "bytevector-sint-set!", 5, 0, 0,
1089 (SCM bv
, SCM index
, SCM value
, SCM endianness
, SCM size
),
1090 "Set the @var{size}-octet long signed integer at @var{index} "
1092 #define FUNC_NAME s_scm_bytevector_sint_set_x
1094 GENERIC_INTEGER_ACCESSOR_PROLOGUE (signed);
1096 bytevector_signed_set (&c_bv
[c_index
], c_size
, value
, endianness
,
1099 return SCM_UNSPECIFIED
;
1105 /* Operations on integers of arbitrary size. */
1107 #define INTEGERS_TO_LIST(_sign) \
1109 size_t i, c_len, c_size; \
1111 SCM_VALIDATE_BYTEVECTOR (1, bv); \
1112 SCM_VALIDATE_SYMBOL (2, endianness); \
1113 c_size = scm_to_uint (size); \
1115 c_len = SCM_BYTEVECTOR_LENGTH (bv); \
1116 if (SCM_UNLIKELY (c_len == 0)) \
1118 else if (SCM_UNLIKELY (c_len < c_size)) \
1119 scm_out_of_range (FUNC_NAME, size); \
1124 c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
1126 lst = scm_make_list (scm_from_uint (c_len / c_size), \
1128 for (i = 0, pair = lst; \
1129 i <= c_len - c_size; \
1130 i += c_size, c_bv += c_size, pair = SCM_CDR (pair)) \
1133 bytevector_ ## _sign ## _ref (c_bv, c_size, \
1140 SCM_DEFINE (scm_bytevector_to_sint_list
, "bytevector->sint-list",
1142 (SCM bv
, SCM endianness
, SCM size
),
1143 "Return a list of signed integers of @var{size} octets "
1144 "representing the contents of @var{bv}.")
1145 #define FUNC_NAME s_scm_bytevector_to_sint_list
1147 INTEGERS_TO_LIST (signed);
1151 SCM_DEFINE (scm_bytevector_to_uint_list
, "bytevector->uint-list",
1153 (SCM bv
, SCM endianness
, SCM size
),
1154 "Return a list of unsigned integers of @var{size} octets "
1155 "representing the contents of @var{bv}.")
1156 #define FUNC_NAME s_scm_bytevector_to_uint_list
1158 INTEGERS_TO_LIST (unsigned);
1162 #undef INTEGER_TO_LIST
1165 #define INTEGER_LIST_TO_BYTEVECTOR(_sign) \
1169 char *c_bv, *c_bv_ptr; \
1171 SCM_VALIDATE_LIST_COPYLEN (1, lst, c_len); \
1172 SCM_VALIDATE_SYMBOL (2, endianness); \
1173 c_size = scm_to_uint (size); \
1175 if (SCM_UNLIKELY ((c_size == 0) || (c_size >= (ULONG_MAX >> 3L)))) \
1176 scm_out_of_range (FUNC_NAME, size); \
1178 bv = make_bytevector (c_len * c_size, SCM_ARRAY_ELEMENT_TYPE_VU8); \
1179 c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
1181 for (c_bv_ptr = c_bv; \
1182 !scm_is_null (lst); \
1183 lst = SCM_CDR (lst), c_bv_ptr += c_size) \
1185 bytevector_ ## _sign ## _set (c_bv_ptr, c_size, \
1186 SCM_CAR (lst), endianness, \
1193 SCM_DEFINE (scm_uint_list_to_bytevector
, "uint-list->bytevector",
1195 (SCM lst
, SCM endianness
, SCM size
),
1196 "Return a bytevector containing the unsigned integers "
1197 "listed in @var{lst} and encoded on @var{size} octets "
1198 "according to @var{endianness}.")
1199 #define FUNC_NAME s_scm_uint_list_to_bytevector
1201 INTEGER_LIST_TO_BYTEVECTOR (unsigned);
1205 SCM_DEFINE (scm_sint_list_to_bytevector
, "sint-list->bytevector",
1207 (SCM lst
, SCM endianness
, SCM size
),
1208 "Return a bytevector containing the signed integers "
1209 "listed in @var{lst} and encoded on @var{size} octets "
1210 "according to @var{endianness}.")
1211 #define FUNC_NAME s_scm_sint_list_to_bytevector
1213 INTEGER_LIST_TO_BYTEVECTOR (signed);
1217 #undef INTEGER_LIST_TO_BYTEVECTOR
1221 /* Operations on 16-bit integers. */
1223 SCM_DEFINE (scm_bytevector_u16_ref
, "bytevector-u16-ref",
1225 (SCM bv
, SCM index
, SCM endianness
),
1226 "Return the unsigned 16-bit integer from @var{bv} at "
1228 #define FUNC_NAME s_scm_bytevector_u16_ref
1230 INTEGER_REF (16, unsigned);
1234 SCM_DEFINE (scm_bytevector_s16_ref
, "bytevector-s16-ref",
1236 (SCM bv
, SCM index
, SCM endianness
),
1237 "Return the signed 16-bit integer from @var{bv} at "
1239 #define FUNC_NAME s_scm_bytevector_s16_ref
1241 INTEGER_REF (16, signed);
1245 SCM_DEFINE (scm_bytevector_u16_native_ref
, "bytevector-u16-native-ref",
1247 (SCM bv
, SCM index
),
1248 "Return the unsigned 16-bit integer from @var{bv} at "
1249 "@var{index} using the native endianness.")
1250 #define FUNC_NAME s_scm_bytevector_u16_native_ref
1252 INTEGER_NATIVE_REF (16, unsigned);
1256 SCM_DEFINE (scm_bytevector_s16_native_ref
, "bytevector-s16-native-ref",
1258 (SCM bv
, SCM index
),
1259 "Return the unsigned 16-bit integer from @var{bv} at "
1260 "@var{index} using the native endianness.")
1261 #define FUNC_NAME s_scm_bytevector_s16_native_ref
1263 INTEGER_NATIVE_REF (16, signed);
1267 SCM_DEFINE (scm_bytevector_u16_set_x
, "bytevector-u16-set!",
1269 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1270 "Store @var{value} in @var{bv} at @var{index} according to "
1271 "@var{endianness}.")
1272 #define FUNC_NAME s_scm_bytevector_u16_set_x
1274 INTEGER_SET (16, unsigned);
1278 SCM_DEFINE (scm_bytevector_s16_set_x
, "bytevector-s16-set!",
1280 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1281 "Store @var{value} in @var{bv} at @var{index} according to "
1282 "@var{endianness}.")
1283 #define FUNC_NAME s_scm_bytevector_s16_set_x
1285 INTEGER_SET (16, signed);
1289 SCM_DEFINE (scm_bytevector_u16_native_set_x
, "bytevector-u16-native-set!",
1291 (SCM bv
, SCM index
, SCM value
),
1292 "Store the unsigned integer @var{value} at index @var{index} "
1293 "of @var{bv} using the native endianness.")
1294 #define FUNC_NAME s_scm_bytevector_u16_native_set_x
1296 INTEGER_NATIVE_SET (16, unsigned);
1300 SCM_DEFINE (scm_bytevector_s16_native_set_x
, "bytevector-s16-native-set!",
1302 (SCM bv
, SCM index
, SCM value
),
1303 "Store the signed integer @var{value} at index @var{index} "
1304 "of @var{bv} using the native endianness.")
1305 #define FUNC_NAME s_scm_bytevector_s16_native_set_x
1307 INTEGER_NATIVE_SET (16, signed);
1313 /* Operations on 32-bit integers. */
1315 /* Unfortunately, on 32-bit machines `SCM' is not large enough to hold
1316 arbitrary 32-bit integers. Thus we fall back to using the
1317 `large_{ref,set}' variants on 32-bit machines. */
1319 #define LARGE_INTEGER_REF(_len, _sign) \
1320 INTEGER_ACCESSOR_PROLOGUE(_len, _sign); \
1321 SCM_VALIDATE_SYMBOL (3, endianness); \
1323 return (bytevector_large_ref ((char *) c_bv + c_index, _len / 8, \
1324 SIGNEDNESS (_sign), endianness));
1326 #define LARGE_INTEGER_SET(_len, _sign) \
1328 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
1329 SCM_VALIDATE_SYMBOL (4, endianness); \
1331 err = bytevector_large_set ((char *) c_bv + c_index, _len / 8, \
1332 SIGNEDNESS (_sign), value, endianness); \
1333 if (SCM_UNLIKELY (err)) \
1334 scm_out_of_range (FUNC_NAME, value); \
1336 return SCM_UNSPECIFIED;
1338 #define LARGE_INTEGER_NATIVE_REF(_len, _sign) \
1339 INTEGER_ACCESSOR_PROLOGUE(_len, _sign); \
1340 return (bytevector_large_ref ((char *) c_bv + c_index, _len / 8, \
1341 SIGNEDNESS (_sign), scm_i_native_endianness));
1343 #define LARGE_INTEGER_NATIVE_SET(_len, _sign) \
1345 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
1347 err = bytevector_large_set ((char *) c_bv + c_index, _len / 8, \
1348 SIGNEDNESS (_sign), value, \
1349 scm_i_native_endianness); \
1350 if (SCM_UNLIKELY (err)) \
1351 scm_out_of_range (FUNC_NAME, value); \
1353 return SCM_UNSPECIFIED;
1356 SCM_DEFINE (scm_bytevector_u32_ref
, "bytevector-u32-ref",
1358 (SCM bv
, SCM index
, SCM endianness
),
1359 "Return the unsigned 32-bit integer from @var{bv} at "
1361 #define FUNC_NAME s_scm_bytevector_u32_ref
1363 #if SIZEOF_VOID_P > 4
1364 INTEGER_REF (32, unsigned);
1366 LARGE_INTEGER_REF (32, unsigned);
1371 SCM_DEFINE (scm_bytevector_s32_ref
, "bytevector-s32-ref",
1373 (SCM bv
, SCM index
, SCM endianness
),
1374 "Return the signed 32-bit integer from @var{bv} at "
1376 #define FUNC_NAME s_scm_bytevector_s32_ref
1378 #if SIZEOF_VOID_P > 4
1379 INTEGER_REF (32, signed);
1381 LARGE_INTEGER_REF (32, signed);
1386 SCM_DEFINE (scm_bytevector_u32_native_ref
, "bytevector-u32-native-ref",
1388 (SCM bv
, SCM index
),
1389 "Return the unsigned 32-bit integer from @var{bv} at "
1390 "@var{index} using the native endianness.")
1391 #define FUNC_NAME s_scm_bytevector_u32_native_ref
1393 #if SIZEOF_VOID_P > 4
1394 INTEGER_NATIVE_REF (32, unsigned);
1396 LARGE_INTEGER_NATIVE_REF (32, unsigned);
1401 SCM_DEFINE (scm_bytevector_s32_native_ref
, "bytevector-s32-native-ref",
1403 (SCM bv
, SCM index
),
1404 "Return the unsigned 32-bit integer from @var{bv} at "
1405 "@var{index} using the native endianness.")
1406 #define FUNC_NAME s_scm_bytevector_s32_native_ref
1408 #if SIZEOF_VOID_P > 4
1409 INTEGER_NATIVE_REF (32, signed);
1411 LARGE_INTEGER_NATIVE_REF (32, signed);
1416 SCM_DEFINE (scm_bytevector_u32_set_x
, "bytevector-u32-set!",
1418 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1419 "Store @var{value} in @var{bv} at @var{index} according to "
1420 "@var{endianness}.")
1421 #define FUNC_NAME s_scm_bytevector_u32_set_x
1423 #if SIZEOF_VOID_P > 4
1424 INTEGER_SET (32, unsigned);
1426 LARGE_INTEGER_SET (32, unsigned);
1431 SCM_DEFINE (scm_bytevector_s32_set_x
, "bytevector-s32-set!",
1433 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1434 "Store @var{value} in @var{bv} at @var{index} according to "
1435 "@var{endianness}.")
1436 #define FUNC_NAME s_scm_bytevector_s32_set_x
1438 #if SIZEOF_VOID_P > 4
1439 INTEGER_SET (32, signed);
1441 LARGE_INTEGER_SET (32, signed);
1446 SCM_DEFINE (scm_bytevector_u32_native_set_x
, "bytevector-u32-native-set!",
1448 (SCM bv
, SCM index
, SCM value
),
1449 "Store the unsigned integer @var{value} at index @var{index} "
1450 "of @var{bv} using the native endianness.")
1451 #define FUNC_NAME s_scm_bytevector_u32_native_set_x
1453 #if SIZEOF_VOID_P > 4
1454 INTEGER_NATIVE_SET (32, unsigned);
1456 LARGE_INTEGER_NATIVE_SET (32, unsigned);
1461 SCM_DEFINE (scm_bytevector_s32_native_set_x
, "bytevector-s32-native-set!",
1463 (SCM bv
, SCM index
, SCM value
),
1464 "Store the signed integer @var{value} at index @var{index} "
1465 "of @var{bv} using the native endianness.")
1466 #define FUNC_NAME s_scm_bytevector_s32_native_set_x
1468 #if SIZEOF_VOID_P > 4
1469 INTEGER_NATIVE_SET (32, signed);
1471 LARGE_INTEGER_NATIVE_SET (32, signed);
1478 /* Operations on 64-bit integers. */
1480 /* For 64-bit integers, we use only the `large_{ref,set}' variant. */
1482 SCM_DEFINE (scm_bytevector_u64_ref
, "bytevector-u64-ref",
1484 (SCM bv
, SCM index
, SCM endianness
),
1485 "Return the unsigned 64-bit integer from @var{bv} at "
1487 #define FUNC_NAME s_scm_bytevector_u64_ref
1489 LARGE_INTEGER_REF (64, unsigned);
1493 SCM_DEFINE (scm_bytevector_s64_ref
, "bytevector-s64-ref",
1495 (SCM bv
, SCM index
, SCM endianness
),
1496 "Return the signed 64-bit integer from @var{bv} at "
1498 #define FUNC_NAME s_scm_bytevector_s64_ref
1500 LARGE_INTEGER_REF (64, signed);
1504 SCM_DEFINE (scm_bytevector_u64_native_ref
, "bytevector-u64-native-ref",
1506 (SCM bv
, SCM index
),
1507 "Return the unsigned 64-bit integer from @var{bv} at "
1508 "@var{index} using the native endianness.")
1509 #define FUNC_NAME s_scm_bytevector_u64_native_ref
1511 LARGE_INTEGER_NATIVE_REF (64, unsigned);
1515 SCM_DEFINE (scm_bytevector_s64_native_ref
, "bytevector-s64-native-ref",
1517 (SCM bv
, SCM index
),
1518 "Return the unsigned 64-bit integer from @var{bv} at "
1519 "@var{index} using the native endianness.")
1520 #define FUNC_NAME s_scm_bytevector_s64_native_ref
1522 LARGE_INTEGER_NATIVE_REF (64, signed);
1526 SCM_DEFINE (scm_bytevector_u64_set_x
, "bytevector-u64-set!",
1528 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1529 "Store @var{value} in @var{bv} at @var{index} according to "
1530 "@var{endianness}.")
1531 #define FUNC_NAME s_scm_bytevector_u64_set_x
1533 LARGE_INTEGER_SET (64, unsigned);
1537 SCM_DEFINE (scm_bytevector_s64_set_x
, "bytevector-s64-set!",
1539 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1540 "Store @var{value} in @var{bv} at @var{index} according to "
1541 "@var{endianness}.")
1542 #define FUNC_NAME s_scm_bytevector_s64_set_x
1544 LARGE_INTEGER_SET (64, signed);
1548 SCM_DEFINE (scm_bytevector_u64_native_set_x
, "bytevector-u64-native-set!",
1550 (SCM bv
, SCM index
, SCM value
),
1551 "Store the unsigned integer @var{value} at index @var{index} "
1552 "of @var{bv} using the native endianness.")
1553 #define FUNC_NAME s_scm_bytevector_u64_native_set_x
1555 LARGE_INTEGER_NATIVE_SET (64, unsigned);
1559 SCM_DEFINE (scm_bytevector_s64_native_set_x
, "bytevector-s64-native-set!",
1561 (SCM bv
, SCM index
, SCM value
),
1562 "Store the signed integer @var{value} at index @var{index} "
1563 "of @var{bv} using the native endianness.")
1564 #define FUNC_NAME s_scm_bytevector_s64_native_set_x
1566 LARGE_INTEGER_NATIVE_SET (64, signed);
1572 /* Operations on IEEE-754 numbers. */
1574 /* There are two possible word endians, visible in glibc's <ieee754.h>.
1575 However, in R6RS, when the endianness is `little', little endian is
1576 assumed for both the byte order and the word order. This is clear from
1577 Section 2.1 of R6RS-lib (in response to
1578 http://www.r6rs.org/formal-comments/comment-187.txt). */
1581 /* Convert to/from a floating-point number with different endianness. This
1582 method is probably not the most efficient but it should be portable. */
1585 float_to_foreign_endianness (union scm_ieee754_float
*target
,
1588 union scm_ieee754_float src
;
1592 #ifdef WORDS_BIGENDIAN
1593 /* Assuming little endian for both byte and word order. */
1594 target
->little_endian
.negative
= src
.big_endian
.negative
;
1595 target
->little_endian
.exponent
= src
.big_endian
.exponent
;
1596 target
->little_endian
.mantissa
= src
.big_endian
.mantissa
;
1598 target
->big_endian
.negative
= src
.little_endian
.negative
;
1599 target
->big_endian
.exponent
= src
.little_endian
.exponent
;
1600 target
->big_endian
.mantissa
= src
.little_endian
.mantissa
;
1605 float_from_foreign_endianness (const union scm_ieee754_float
*source
)
1607 union scm_ieee754_float result
;
1609 #ifdef WORDS_BIGENDIAN
1610 /* Assuming little endian for both byte and word order. */
1611 result
.big_endian
.negative
= source
->little_endian
.negative
;
1612 result
.big_endian
.exponent
= source
->little_endian
.exponent
;
1613 result
.big_endian
.mantissa
= source
->little_endian
.mantissa
;
1615 result
.little_endian
.negative
= source
->big_endian
.negative
;
1616 result
.little_endian
.exponent
= source
->big_endian
.exponent
;
1617 result
.little_endian
.mantissa
= source
->big_endian
.mantissa
;
1624 double_to_foreign_endianness (union scm_ieee754_double
*target
,
1627 union scm_ieee754_double src
;
1631 #ifdef WORDS_BIGENDIAN
1632 /* Assuming little endian for both byte and word order. */
1633 target
->little_little_endian
.negative
= src
.big_endian
.negative
;
1634 target
->little_little_endian
.exponent
= src
.big_endian
.exponent
;
1635 target
->little_little_endian
.mantissa0
= src
.big_endian
.mantissa0
;
1636 target
->little_little_endian
.mantissa1
= src
.big_endian
.mantissa1
;
1638 target
->big_endian
.negative
= src
.little_little_endian
.negative
;
1639 target
->big_endian
.exponent
= src
.little_little_endian
.exponent
;
1640 target
->big_endian
.mantissa0
= src
.little_little_endian
.mantissa0
;
1641 target
->big_endian
.mantissa1
= src
.little_little_endian
.mantissa1
;
1645 static inline double
1646 double_from_foreign_endianness (const union scm_ieee754_double
*source
)
1648 union scm_ieee754_double result
;
1650 #ifdef WORDS_BIGENDIAN
1651 /* Assuming little endian for both byte and word order. */
1652 result
.big_endian
.negative
= source
->little_little_endian
.negative
;
1653 result
.big_endian
.exponent
= source
->little_little_endian
.exponent
;
1654 result
.big_endian
.mantissa0
= source
->little_little_endian
.mantissa0
;
1655 result
.big_endian
.mantissa1
= source
->little_little_endian
.mantissa1
;
1657 result
.little_little_endian
.negative
= source
->big_endian
.negative
;
1658 result
.little_little_endian
.exponent
= source
->big_endian
.exponent
;
1659 result
.little_little_endian
.mantissa0
= source
->big_endian
.mantissa0
;
1660 result
.little_little_endian
.mantissa1
= source
->big_endian
.mantissa1
;
1666 /* Template macros to abstract over doubles and floats.
1667 XXX: Guile can only convert to/from doubles. */
1668 #define IEEE754_UNION(_c_type) union scm_ieee754_ ## _c_type
1669 #define IEEE754_TO_SCM(_c_type) scm_from_double
1670 #define IEEE754_FROM_SCM(_c_type) scm_to_double
1671 #define IEEE754_FROM_FOREIGN_ENDIANNESS(_c_type) \
1672 _c_type ## _from_foreign_endianness
1673 #define IEEE754_TO_FOREIGN_ENDIANNESS(_c_type) \
1674 _c_type ## _to_foreign_endianness
1677 /* FIXME: SCM_VALIDATE_REAL rejects integers, etc. grrr */
1678 #define VALIDATE_REAL(pos, v) \
1680 SCM_ASSERT_TYPE (scm_is_true (scm_rational_p (v)), v, pos, FUNC_NAME, "real"); \
1683 /* Templace getters and setters. */
1685 #define IEEE754_ACCESSOR_PROLOGUE(_type) \
1686 INTEGER_ACCESSOR_PROLOGUE (sizeof (_type) << 3UL, signed);
1688 #define IEEE754_REF(_type) \
1691 IEEE754_ACCESSOR_PROLOGUE (_type); \
1692 SCM_VALIDATE_SYMBOL (3, endianness); \
1694 if (scm_is_eq (endianness, scm_i_native_endianness)) \
1695 memcpy (&c_result, &c_bv[c_index], sizeof (c_result)); \
1698 IEEE754_UNION (_type) c_raw; \
1700 memcpy (&c_raw, &c_bv[c_index], sizeof (c_raw)); \
1702 IEEE754_FROM_FOREIGN_ENDIANNESS (_type) (&c_raw); \
1705 return (IEEE754_TO_SCM (_type) (c_result));
1707 #define IEEE754_NATIVE_REF(_type) \
1710 IEEE754_ACCESSOR_PROLOGUE (_type); \
1712 memcpy (&c_result, &c_bv[c_index], sizeof (c_result)); \
1713 return (IEEE754_TO_SCM (_type) (c_result));
1715 #define IEEE754_SET(_type) \
1718 IEEE754_ACCESSOR_PROLOGUE (_type); \
1719 VALIDATE_REAL (3, value); \
1720 SCM_VALIDATE_SYMBOL (4, endianness); \
1721 c_value = IEEE754_FROM_SCM (_type) (value); \
1723 if (scm_is_eq (endianness, scm_i_native_endianness)) \
1724 memcpy (&c_bv[c_index], &c_value, sizeof (c_value)); \
1727 IEEE754_UNION (_type) c_raw; \
1729 IEEE754_TO_FOREIGN_ENDIANNESS (_type) (&c_raw, c_value); \
1730 memcpy (&c_bv[c_index], &c_raw, sizeof (c_raw)); \
1733 return SCM_UNSPECIFIED;
1735 #define IEEE754_NATIVE_SET(_type) \
1738 IEEE754_ACCESSOR_PROLOGUE (_type); \
1739 VALIDATE_REAL (3, value); \
1740 c_value = IEEE754_FROM_SCM (_type) (value); \
1742 memcpy (&c_bv[c_index], &c_value, sizeof (c_value)); \
1743 return SCM_UNSPECIFIED;
1746 /* Single precision. */
1748 SCM_DEFINE (scm_bytevector_ieee_single_ref
,
1749 "bytevector-ieee-single-ref",
1751 (SCM bv
, SCM index
, SCM endianness
),
1752 "Return the IEEE-754 single from @var{bv} at "
1754 #define FUNC_NAME s_scm_bytevector_ieee_single_ref
1756 IEEE754_REF (float);
1760 SCM_DEFINE (scm_bytevector_ieee_single_native_ref
,
1761 "bytevector-ieee-single-native-ref",
1763 (SCM bv
, SCM index
),
1764 "Return the IEEE-754 single from @var{bv} at "
1765 "@var{index} using the native endianness.")
1766 #define FUNC_NAME s_scm_bytevector_ieee_single_native_ref
1768 IEEE754_NATIVE_REF (float);
1772 SCM_DEFINE (scm_bytevector_ieee_single_set_x
,
1773 "bytevector-ieee-single-set!",
1775 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1776 "Store real @var{value} in @var{bv} at @var{index} according to "
1777 "@var{endianness}.")
1778 #define FUNC_NAME s_scm_bytevector_ieee_single_set_x
1780 IEEE754_SET (float);
1784 SCM_DEFINE (scm_bytevector_ieee_single_native_set_x
,
1785 "bytevector-ieee-single-native-set!",
1787 (SCM bv
, SCM index
, SCM value
),
1788 "Store the real @var{value} at index @var{index} "
1789 "of @var{bv} using the native endianness.")
1790 #define FUNC_NAME s_scm_bytevector_ieee_single_native_set_x
1792 IEEE754_NATIVE_SET (float);
1797 /* Double precision. */
1799 SCM_DEFINE (scm_bytevector_ieee_double_ref
,
1800 "bytevector-ieee-double-ref",
1802 (SCM bv
, SCM index
, SCM endianness
),
1803 "Return the IEEE-754 double from @var{bv} at "
1805 #define FUNC_NAME s_scm_bytevector_ieee_double_ref
1807 IEEE754_REF (double);
1811 SCM_DEFINE (scm_bytevector_ieee_double_native_ref
,
1812 "bytevector-ieee-double-native-ref",
1814 (SCM bv
, SCM index
),
1815 "Return the IEEE-754 double from @var{bv} at "
1816 "@var{index} using the native endianness.")
1817 #define FUNC_NAME s_scm_bytevector_ieee_double_native_ref
1819 IEEE754_NATIVE_REF (double);
1823 SCM_DEFINE (scm_bytevector_ieee_double_set_x
,
1824 "bytevector-ieee-double-set!",
1826 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1827 "Store real @var{value} in @var{bv} at @var{index} according to "
1828 "@var{endianness}.")
1829 #define FUNC_NAME s_scm_bytevector_ieee_double_set_x
1831 IEEE754_SET (double);
1835 SCM_DEFINE (scm_bytevector_ieee_double_native_set_x
,
1836 "bytevector-ieee-double-native-set!",
1838 (SCM bv
, SCM index
, SCM value
),
1839 "Store the real @var{value} at index @var{index} "
1840 "of @var{bv} using the native endianness.")
1841 #define FUNC_NAME s_scm_bytevector_ieee_double_native_set_x
1843 IEEE754_NATIVE_SET (double);
1848 #undef IEEE754_UNION
1849 #undef IEEE754_TO_SCM
1850 #undef IEEE754_FROM_SCM
1851 #undef IEEE754_FROM_FOREIGN_ENDIANNESS
1852 #undef IEEE754_TO_FOREIGN_ENDIANNESS
1854 #undef IEEE754_NATIVE_REF
1856 #undef IEEE754_NATIVE_SET
1859 /* Operations on strings. */
1862 /* Produce a function that returns the length of a UTF-encoded string. */
1863 #define UTF_STRLEN_FUNCTION(_utf_width) \
1864 static inline size_t \
1865 utf ## _utf_width ## _strlen (const uint ## _utf_width ## _t *str) \
1868 const uint ## _utf_width ## _t *ptr; \
1876 return (len * ((_utf_width) / 8)); \
1879 UTF_STRLEN_FUNCTION (8)
1882 /* Return the length (in bytes) of STR, a UTF-(UTF_WIDTH) encoded string. */
1883 #define UTF_STRLEN(_utf_width, _str) \
1884 utf ## _utf_width ## _strlen (_str)
1886 /* Return the "portable" name of the UTF encoding of size UTF_WIDTH and
1887 ENDIANNESS (Gnulib's `iconv_open' module guarantees the portability of the
1890 utf_encoding_name (char *name
, size_t utf_width
, SCM endianness
)
1892 strcpy (name
, "UTF-");
1893 strcat (name
, ((utf_width
== 8)
1895 : ((utf_width
== 16)
1897 : ((utf_width
== 32)
1901 ((scm_is_eq (endianness
, scm_sym_big
))
1903 : ((scm_is_eq (endianness
, scm_sym_little
))
1908 /* Maximum length of a UTF encoding name. */
1909 #define MAX_UTF_ENCODING_NAME_LEN 16
1911 /* Produce the body of a `string->utf' function. */
1912 #define STRING_TO_UTF(_utf_width) \
1916 char c_utf_name[MAX_UTF_ENCODING_NAME_LEN]; \
1917 char *c_utf = NULL, *c_locale; \
1918 size_t c_strlen, c_raw_strlen, c_utf_len = 0; \
1920 SCM_VALIDATE_STRING (1, str); \
1921 if (endianness == SCM_UNDEFINED) \
1922 endianness = scm_sym_big; \
1924 SCM_VALIDATE_SYMBOL (2, endianness); \
1926 c_strlen = scm_c_string_length (str); \
1927 c_raw_strlen = c_strlen * ((_utf_width) / 8); \
1930 c_str = (char *) alloca (c_raw_strlen + 1); \
1931 c_raw_strlen = scm_to_locale_stringbuf (str, c_str, c_strlen); \
1933 while (c_raw_strlen > c_strlen); \
1934 c_str[c_raw_strlen] = '\0'; \
1936 utf_encoding_name (c_utf_name, (_utf_width), endianness); \
1938 c_locale = (char *) alloca (strlen (locale_charset ()) + 1); \
1939 strcpy (c_locale, locale_charset ()); \
1941 err = mem_iconveh (c_str, c_raw_strlen, \
1942 c_locale, c_utf_name, \
1943 iconveh_question_mark, NULL, \
1944 &c_utf, &c_utf_len); \
1945 if (SCM_UNLIKELY (err)) \
1946 scm_syserror_msg (FUNC_NAME, "failed to convert string: ~A", \
1947 scm_list_1 (str), err); \
1949 /* C_UTF is null-terminated. */ \
1950 utf = scm_c_take_bytevector ((signed char *) c_utf, c_utf_len); \
1956 SCM_DEFINE (scm_string_to_utf8
, "string->utf8",
1959 "Return a newly allocated bytevector that contains the UTF-8 "
1960 "encoding of @var{str}.")
1961 #define FUNC_NAME s_scm_string_to_utf8
1966 size_t c_strlen
, c_raw_strlen
;
1968 SCM_VALIDATE_STRING (1, str
);
1970 c_strlen
= scm_c_string_length (str
);
1971 c_raw_strlen
= c_strlen
;
1974 c_str
= (char *) alloca (c_raw_strlen
+ 1);
1975 c_raw_strlen
= scm_to_locale_stringbuf (str
, c_str
, c_strlen
);
1977 while (c_raw_strlen
> c_strlen
);
1978 c_str
[c_raw_strlen
] = '\0';
1980 c_utf
= u8_strconv_from_locale (c_str
);
1981 if (SCM_UNLIKELY (c_utf
== NULL
))
1982 scm_syserror (FUNC_NAME
);
1984 /* C_UTF is null-terminated. */
1985 utf
= scm_c_take_bytevector ((signed char *) c_utf
,
1986 UTF_STRLEN (8, c_utf
));
1992 SCM_DEFINE (scm_string_to_utf16
, "string->utf16",
1994 (SCM str
, SCM endianness
),
1995 "Return a newly allocated bytevector that contains the UTF-16 "
1996 "encoding of @var{str}.")
1997 #define FUNC_NAME s_scm_string_to_utf16
2003 SCM_DEFINE (scm_string_to_utf32
, "string->utf32",
2005 (SCM str
, SCM endianness
),
2006 "Return a newly allocated bytevector that contains the UTF-32 "
2007 "encoding of @var{str}.")
2008 #define FUNC_NAME s_scm_string_to_utf32
2015 /* Produce the body of a function that converts a UTF-encoded bytevector to a
2017 #define UTF_TO_STRING(_utf_width) \
2018 SCM str = SCM_BOOL_F; \
2020 char *c_str = NULL, *c_locale; \
2021 char c_utf_name[MAX_UTF_ENCODING_NAME_LEN]; \
2022 const char *c_utf; \
2023 size_t c_strlen = 0, c_utf_len; \
2025 SCM_VALIDATE_BYTEVECTOR (1, utf); \
2026 if (endianness == SCM_UNDEFINED) \
2027 endianness = scm_sym_big; \
2029 SCM_VALIDATE_SYMBOL (2, endianness); \
2031 c_utf_len = SCM_BYTEVECTOR_LENGTH (utf); \
2032 c_utf = (char *) SCM_BYTEVECTOR_CONTENTS (utf); \
2033 utf_encoding_name (c_utf_name, (_utf_width), endianness); \
2035 c_locale = (char *) alloca (strlen (locale_charset ()) + 1); \
2036 strcpy (c_locale, locale_charset ()); \
2038 err = mem_iconveh (c_utf, c_utf_len, \
2039 c_utf_name, c_locale, \
2040 iconveh_question_mark, NULL, \
2041 &c_str, &c_strlen); \
2042 if (SCM_UNLIKELY (err)) \
2043 scm_syserror_msg (FUNC_NAME, "failed to convert to string: ~A", \
2044 scm_list_1 (utf), err); \
2046 /* C_STR is null-terminated. */ \
2047 str = scm_take_locale_stringn (c_str, c_strlen); \
2052 SCM_DEFINE (scm_utf8_to_string
, "utf8->string",
2055 "Return a newly allocate string that contains from the UTF-8-"
2056 "encoded contents of bytevector @var{utf}.")
2057 #define FUNC_NAME s_scm_utf8_to_string
2061 char *c_str
= NULL
, *c_locale
;
2063 size_t c_utf_len
, c_strlen
= 0;
2065 SCM_VALIDATE_BYTEVECTOR (1, utf
);
2067 c_utf_len
= SCM_BYTEVECTOR_LENGTH (utf
);
2069 c_locale
= (char *) alloca (strlen (locale_charset ()) + 1);
2070 strcpy (c_locale
, locale_charset ());
2072 c_utf
= (char *) SCM_BYTEVECTOR_CONTENTS (utf
);
2073 err
= mem_iconveh (c_utf
, c_utf_len
,
2075 iconveh_question_mark
, NULL
,
2077 if (SCM_UNLIKELY (err
))
2078 scm_syserror_msg (FUNC_NAME
, "failed to convert to string: ~A",
2079 scm_list_1 (utf
), err
);
2081 /* C_STR is null-terminated. */
2082 str
= scm_take_locale_stringn (c_str
, c_strlen
);
2088 SCM_DEFINE (scm_utf16_to_string
, "utf16->string",
2090 (SCM utf
, SCM endianness
),
2091 "Return a newly allocate string that contains from the UTF-16-"
2092 "encoded contents of bytevector @var{utf}.")
2093 #define FUNC_NAME s_scm_utf16_to_string
2099 SCM_DEFINE (scm_utf32_to_string
, "utf32->string",
2101 (SCM utf
, SCM endianness
),
2102 "Return a newly allocate string that contains from the UTF-32-"
2103 "encoded contents of bytevector @var{utf}.")
2104 #define FUNC_NAME s_scm_utf32_to_string
2112 /* Bytevectors as generalized vectors & arrays. */
2116 bytevector_ref_c32 (SCM bv
, SCM idx
)
2117 { /* FIXME add some checks */
2118 const float *contents
= (const float*)SCM_BYTEVECTOR_CONTENTS (bv
);
2119 size_t i
= scm_to_size_t (idx
);
2120 return scm_c_make_rectangular (contents
[i
/8], contents
[i
/8 + 1]);
2124 bytevector_ref_c64 (SCM bv
, SCM idx
)
2125 { /* FIXME add some checks */
2126 const double *contents
= (const double*)SCM_BYTEVECTOR_CONTENTS (bv
);
2127 size_t i
= scm_to_size_t (idx
);
2128 return scm_c_make_rectangular (contents
[i
/16], contents
[i
/16 + 1]);
2131 typedef SCM (*scm_t_bytevector_ref_fn
)(SCM
, SCM
);
2133 const scm_t_bytevector_ref_fn bytevector_ref_fns
[SCM_ARRAY_ELEMENT_TYPE_LAST
+ 1] =
2138 scm_bytevector_u8_ref
, /* VU8 */
2139 scm_bytevector_u8_ref
, /* U8 */
2140 scm_bytevector_s8_ref
,
2141 scm_bytevector_u16_native_ref
,
2142 scm_bytevector_s16_native_ref
,
2143 scm_bytevector_u32_native_ref
,
2144 scm_bytevector_s32_native_ref
,
2145 scm_bytevector_u64_native_ref
,
2146 scm_bytevector_s64_native_ref
,
2147 scm_bytevector_ieee_single_native_ref
,
2148 scm_bytevector_ieee_double_native_ref
,
2154 bv_handle_ref (scm_t_array_handle
*h
, size_t index
)
2157 scm_t_bytevector_ref_fn ref_fn
;
2159 ref_fn
= bytevector_ref_fns
[h
->element_type
];
2161 scm_from_size_t (index
* scm_array_handle_uniform_element_size (h
));
2162 return ref_fn (h
->array
, byte_index
);
2166 bytevector_set_c32 (SCM bv
, SCM idx
, SCM val
)
2167 { /* checks are unnecessary here */
2168 float *contents
= (float*)SCM_BYTEVECTOR_CONTENTS (bv
);
2169 size_t i
= scm_to_size_t (idx
);
2170 contents
[i
/8] = scm_c_real_part (val
);
2171 contents
[i
/8 + 1] = scm_c_imag_part (val
);
2172 return SCM_UNSPECIFIED
;
2176 bytevector_set_c64 (SCM bv
, SCM idx
, SCM val
)
2177 { /* checks are unnecessary here */
2178 double *contents
= (double*)SCM_BYTEVECTOR_CONTENTS (bv
);
2179 size_t i
= scm_to_size_t (idx
);
2180 contents
[i
/16] = scm_c_real_part (val
);
2181 contents
[i
/16 + 1] = scm_c_imag_part (val
);
2182 return SCM_UNSPECIFIED
;
2185 typedef SCM (*scm_t_bytevector_set_fn
)(SCM
, SCM
, SCM
);
2187 const scm_t_bytevector_set_fn bytevector_set_fns
[SCM_ARRAY_ELEMENT_TYPE_LAST
+ 1] =
2192 scm_bytevector_u8_set_x
, /* VU8 */
2193 scm_bytevector_u8_set_x
, /* U8 */
2194 scm_bytevector_s8_set_x
,
2195 scm_bytevector_u16_native_set_x
,
2196 scm_bytevector_s16_native_set_x
,
2197 scm_bytevector_u32_native_set_x
,
2198 scm_bytevector_s32_native_set_x
,
2199 scm_bytevector_u64_native_set_x
,
2200 scm_bytevector_s64_native_set_x
,
2201 scm_bytevector_ieee_single_native_set_x
,
2202 scm_bytevector_ieee_double_native_set_x
,
2208 bv_handle_set_x (scm_t_array_handle
*h
, size_t index
, SCM val
)
2211 scm_t_bytevector_set_fn set_fn
;
2213 set_fn
= bytevector_set_fns
[h
->element_type
];
2215 scm_from_size_t (index
* scm_array_handle_uniform_element_size (h
));
2216 set_fn (h
->array
, byte_index
, val
);
2220 bytevector_get_handle (SCM v
, scm_t_array_handle
*h
)
2226 h
->dim0
.ubnd
= SCM_BYTEVECTOR_TYPED_LENGTH (v
) - 1;
2228 h
->element_type
= SCM_BYTEVECTOR_ELEMENT_TYPE (v
);
2229 h
->elements
= h
->writable_elements
= SCM_BYTEVECTOR_CONTENTS (v
);
2233 /* Initialization. */
2236 scm_bootstrap_bytevectors (void)
2238 /* The SMOB type must be instantiated here because the
2239 generalized-vector API may want to access bytevectors even though
2240 `(rnrs bytevector)' hasn't been loaded. */
2241 scm_tc16_bytevector
= scm_make_smob_type ("bytevector", 0);
2242 scm_set_smob_free (scm_tc16_bytevector
, free_bytevector
);
2243 scm_set_smob_print (scm_tc16_bytevector
, print_bytevector
);
2244 scm_set_smob_equalp (scm_tc16_bytevector
, bytevector_equal_p
);
2246 scm_null_bytevector
=
2247 scm_gc_protect_object
2248 (make_bytevector_from_buffer (0, NULL
, SCM_ARRAY_ELEMENT_TYPE_VU8
));
2250 #ifdef WORDS_BIGENDIAN
2251 scm_i_native_endianness
= scm_permanent_object (scm_from_locale_symbol ("big"));
2253 scm_i_native_endianness
= scm_permanent_object (scm_from_locale_symbol ("little"));
2256 scm_c_register_extension ("libguile", "scm_init_bytevectors",
2257 (scm_t_extension_init_func
) scm_init_bytevectors
,
2261 scm_t_array_implementation impl
;
2263 impl
.tag
= scm_tc16_bytevector
;
2265 impl
.vref
= bv_handle_ref
;
2266 impl
.vset
= bv_handle_set_x
;
2267 impl
.get_handle
= bytevector_get_handle
;
2268 scm_i_register_array_implementation (&impl
);
2269 scm_i_register_vector_constructor
2270 (scm_i_array_element_types
[SCM_ARRAY_ELEMENT_TYPE_VU8
],
2271 scm_make_bytevector
);
2276 scm_init_bytevectors (void)
2278 #include "libguile/bytevectors.x"
2280 scm_endianness_big
= scm_sym_big
;
2281 scm_endianness_little
= scm_sym_little
;