1 /* Guile-R6RS-Libs --- Implementation of R6RS standard libraries.
2 Copyright (C) 2007, 2008, 2009 Ludovic Courtès <ludo@gnu.org>
4 Guile-R6RS-Libs is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 Guile-R6RS-Libs is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with Guile-R6RS-Libs; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
25 #include "bytevector.h"
28 #include "striconveh.h"
36 /* Assuming 32-bit longs. */
37 # define ULONG_MAX 4294967295UL
46 /* Convenience macros. These are used by the various templates (macros) that
47 are parameterized by integer signedness. */
48 #define INT8_T_signed scm_t_int8
49 #define INT8_T_unsigned scm_t_uint8
50 #define INT16_T_signed scm_t_int16
51 #define INT16_T_unsigned scm_t_uint16
52 #define INT32_T_signed scm_t_int32
53 #define INT32_T_unsigned scm_t_uint32
54 #define is_signed_int8(_x) (((_x) >= -128L) && ((_x) <= 127L))
55 #define is_unsigned_int8(_x) ((_x) <= 255UL)
56 #define is_signed_int16(_x) (((_x) >= -32768L) && ((_x) <= 32767L))
57 #define is_unsigned_int16(_x) ((_x) <= 65535UL)
58 #define is_signed_int32(_x) (((_x) >= -2147483648L) && ((_x) <= 2147483647L))
59 #define is_unsigned_int32(_x) ((_x) <= 4294967295UL)
60 #define SIGNEDNESS_signed 1
61 #define SIGNEDNESS_unsigned 0
63 #define INT_TYPE(_size, _sign) INT ## _size ## _T_ ## _sign
64 #define INT_SWAP(_size) bswap_ ## _size
65 #define INT_VALID_P(_size, _sign) is_ ## _sign ## _int ## _size
66 #define SIGNEDNESS(_sign) SIGNEDNESS_ ## _sign
69 #define INTEGER_ACCESSOR_PROLOGUE(_len, _sign) \
70 unsigned c_len, c_index; \
73 SCM_VALIDATE_R6RS_BYTEVECTOR (1, bv); \
74 c_index = scm_to_uint (index); \
76 c_len = SCM_R6RS_BYTEVECTOR_LENGTH (bv); \
77 c_bv = (_sign char *) SCM_R6RS_BYTEVECTOR_CONTENTS (bv); \
79 if (SCM_UNLIKELY (c_index + ((_len) >> 3UL) - 1 >= c_len)) \
80 scm_out_of_range (FUNC_NAME, index);
82 /* Template for fixed-size integer access (only 8, 16 or 32-bit). */
83 #define INTEGER_REF(_len, _sign) \
86 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
87 SCM_VALIDATE_SYMBOL (3, endianness); \
90 INT_TYPE (_len, _sign) c_result; \
92 memcpy (&c_result, &c_bv[c_index], (_len) / 8); \
93 if (!scm_is_eq (endianness, native_endianness)) \
94 c_result = INT_SWAP (_len) (c_result); \
96 result = SCM_I_MAKINUM (c_result); \
101 /* Template for fixed-size integer access using the native endianness. */
102 #define INTEGER_NATIVE_REF(_len, _sign) \
105 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
108 INT_TYPE (_len, _sign) c_result; \
110 memcpy (&c_result, &c_bv[c_index], (_len) / 8); \
111 result = SCM_I_MAKINUM (c_result); \
116 /* Template for fixed-size integer modification (only 8, 16 or 32-bit). */
117 #define INTEGER_SET(_len, _sign) \
118 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
119 SCM_VALIDATE_SYMBOL (3, endianness); \
122 _sign long c_value; \
123 INT_TYPE (_len, _sign) c_value_short; \
125 if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
126 scm_wrong_type_arg (FUNC_NAME, 3, value); \
128 c_value = SCM_I_INUM (value); \
129 if (SCM_UNLIKELY (!INT_VALID_P (_len, _sign) (c_value))) \
130 scm_out_of_range (FUNC_NAME, value); \
132 c_value_short = (INT_TYPE (_len, _sign)) c_value; \
133 if (!scm_is_eq (endianness, native_endianness)) \
134 c_value_short = INT_SWAP (_len) (c_value_short); \
136 memcpy (&c_bv[c_index], &c_value_short, (_len) / 8); \
139 return SCM_UNSPECIFIED;
141 /* Template for fixed-size integer modification using the native
143 #define INTEGER_NATIVE_SET(_len, _sign) \
144 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
147 _sign long c_value; \
148 INT_TYPE (_len, _sign) c_value_short; \
150 if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
151 scm_wrong_type_arg (FUNC_NAME, 3, value); \
153 c_value = SCM_I_INUM (value); \
154 if (SCM_UNLIKELY (!INT_VALID_P (_len, _sign) (c_value))) \
155 scm_out_of_range (FUNC_NAME, value); \
157 c_value_short = (INT_TYPE (_len, _sign)) c_value; \
159 memcpy (&c_bv[c_index], &c_value_short, (_len) / 8); \
162 return SCM_UNSPECIFIED;
166 /* Bytevector type. */
168 SCM_GLOBAL_SMOB (scm_tc16_r6rs_bytevector
, "r6rs-bytevector", 0);
170 #define SCM_R6RS_BYTEVECTOR_SET_LENGTH(_bv, _len) \
171 SCM_SET_SMOB_DATA ((_bv), (scm_t_bits) (_len))
172 #define SCM_R6RS_BYTEVECTOR_SET_CONTENTS(_bv, _buf) \
173 SCM_SET_SMOB_DATA_2 ((_bv), (scm_t_bits) (_buf))
175 /* The empty bytevector. */
176 SCM scm_r6rs_null_bytevector
= SCM_UNSPECIFIED
;
180 make_bytevector_from_buffer (unsigned len
, signed char *contents
)
182 /* Assuming LEN > SCM_R6RS_BYTEVECTOR_INLINE_THRESHOLD. */
183 SCM_RETURN_NEWSMOB2 (scm_tc16_r6rs_bytevector
, len
, contents
);
187 make_bytevector (unsigned len
)
191 if (SCM_UNLIKELY (len
== 0))
192 bv
= scm_r6rs_null_bytevector
;
195 signed char *contents
= NULL
;
197 if (!SCM_R6RS_BYTEVECTOR_INLINEABLE_SIZE_P (len
))
198 contents
= (signed char *) scm_gc_malloc (len
, SCM_GC_BYTEVECTOR
);
200 bv
= make_bytevector_from_buffer (len
, contents
);
206 /* Return a new bytevector of size LEN octets. */
208 scm_r6rs_c_make_bytevector (unsigned len
)
210 return (make_bytevector (len
));
213 /* Return a bytevector of size LEN made up of CONTENTS. The area pointed to
214 by CONTENTS must have been allocated using `scm_gc_malloc ()'. */
216 scm_r6rs_c_take_bytevector (signed char *contents
, unsigned len
)
220 if (SCM_UNLIKELY (SCM_R6RS_BYTEVECTOR_INLINEABLE_SIZE_P (len
)))
222 /* Copy CONTENTS into an "in-line" buffer, then free CONTENTS. */
225 bv
= make_bytevector (len
);
226 c_bv
= SCM_R6RS_BYTEVECTOR_CONTENTS (bv
);
227 memcpy (c_bv
, contents
, len
);
228 scm_gc_free (contents
, len
, SCM_GC_BYTEVECTOR
);
231 bv
= make_bytevector_from_buffer (len
, contents
);
236 /* Shrink BV to C_NEW_LEN (which is assumed to be smaller than its current
237 size) and return BV. */
239 scm_r6rs_i_shrink_bytevector (SCM bv
, unsigned c_new_len
)
241 if (!SCM_R6RS_BYTEVECTOR_INLINE_P (bv
))
244 signed char *c_bv
, *c_new_bv
;
246 c_len
= SCM_R6RS_BYTEVECTOR_LENGTH (bv
);
247 c_bv
= SCM_R6RS_BYTEVECTOR_CONTENTS (bv
);
249 SCM_R6RS_BYTEVECTOR_SET_LENGTH (bv
, c_new_len
);
251 if (SCM_R6RS_BYTEVECTOR_INLINEABLE_SIZE_P (c_new_len
))
253 /* Copy to the in-line buffer and free the current buffer. */
254 c_new_bv
= SCM_R6RS_BYTEVECTOR_CONTENTS (bv
);
255 memcpy (c_new_bv
, c_bv
, c_new_len
);
256 scm_gc_free (c_bv
, c_len
, SCM_GC_BYTEVECTOR
);
260 /* Resize the existing buffer. */
261 c_new_bv
= scm_gc_realloc (c_bv
, c_len
, c_new_len
,
263 SCM_R6RS_BYTEVECTOR_SET_CONTENTS (bv
, c_new_bv
);
270 SCM_SMOB_PRINT (scm_tc16_r6rs_bytevector
, print_bytevector
,
276 c_len
= SCM_R6RS_BYTEVECTOR_LENGTH (bv
);
277 c_bv
= (unsigned char *) SCM_R6RS_BYTEVECTOR_CONTENTS (bv
);
279 scm_puts ("#vu8(", port
);
280 for (i
= 0; i
< c_len
; i
++)
283 scm_putc (' ', port
);
285 scm_uintprint (c_bv
[i
], 10, port
);
288 scm_putc (')', port
);
290 /* Make GCC think we use it. */
291 scm_remember_upto_here ((SCM
) pstate
);
296 SCM_SMOB_FREE (scm_tc16_r6rs_bytevector
, free_bytevector
, bv
)
299 if (!SCM_R6RS_BYTEVECTOR_INLINE_P (bv
))
304 c_bv
= SCM_R6RS_BYTEVECTOR_CONTENTS (bv
);
305 c_len
= SCM_R6RS_BYTEVECTOR_LENGTH (bv
);
307 scm_gc_free (c_bv
, c_len
, SCM_GC_BYTEVECTOR
);
315 /* General operations. */
317 SCM_SYMBOL (scm_sym_big
, "big");
318 SCM_SYMBOL (scm_sym_little
, "little");
320 SCM scm_r6rs_endianness_big
, scm_r6rs_endianness_little
;
322 /* Host endianness (a symbol). */
323 static SCM native_endianness
= SCM_UNSPECIFIED
;
327 # define bswap_24(_x) \
328 ((((_x) & 0xff0000) >> 16) | \
329 (((_x) & 0x00ff00)) | \
330 (((_x) & 0x0000ff) << 16))
334 SCM_DEFINE (scm_r6rs_native_endianness
, "native-endianness", 0, 0, 0,
336 "Return a symbol denoting the machine's native endianness.")
338 return native_endianness
;
341 SCM_DEFINE (scm_r6rs_bytevector_p
, "bytevector?", 1, 0, 0,
343 "Return true if @var{obj} is a bytevector.")
345 return (scm_from_bool (SCM_SMOB_PREDICATE (scm_tc16_r6rs_bytevector
,
349 SCM_DEFINE (scm_r6rs_make_bytevector
, "make-bytevector", 1, 1, 0,
351 "Return a newly allocated bytevector of @var{len} bytes, "
352 "optionally filled with @var{fill}.")
353 #define FUNC_NAME s_scm_r6rs_make_bytevector
357 signed char c_fill
= '\0';
359 SCM_VALIDATE_UINT_COPY (1, len
, c_len
);
360 if (fill
!= SCM_UNDEFINED
)
364 value
= scm_to_int (fill
);
365 if (SCM_UNLIKELY ((value
< -128) || (value
> 255)))
366 scm_out_of_range (FUNC_NAME
, fill
);
367 c_fill
= (signed char) value
;
370 bv
= make_bytevector (c_len
);
371 if (fill
!= SCM_UNDEFINED
)
374 signed char *contents
;
376 contents
= SCM_R6RS_BYTEVECTOR_CONTENTS (bv
);
377 for (i
= 0; i
< c_len
; i
++)
378 contents
[i
] = c_fill
;
385 SCM_DEFINE (scm_r6rs_bytevector_length
, "bytevector-length", 1, 0, 0,
387 "Return the length (in bytes) of @var{bv}.")
388 #define FUNC_NAME s_scm_r6rs_bytevector_length
390 SCM_VALIDATE_R6RS_BYTEVECTOR (1, bv
);
392 return (scm_from_uint (SCM_R6RS_BYTEVECTOR_LENGTH (bv
)));
396 SCM_DEFINE (scm_r6rs_bytevector_eq_p
, "bytevector=?", 2, 0, 0,
398 "Return is @var{bv1} equals to @var{bv2}---i.e., if they "
399 "have the same length and contents.")
400 #define FUNC_NAME s_scm_r6rs_bytevector_eq_p
402 SCM result
= SCM_BOOL_F
;
403 unsigned c_len1
, c_len2
;
405 SCM_VALIDATE_R6RS_BYTEVECTOR (1, bv1
);
406 SCM_VALIDATE_R6RS_BYTEVECTOR (2, bv2
);
408 c_len1
= SCM_R6RS_BYTEVECTOR_LENGTH (bv1
);
409 c_len2
= SCM_R6RS_BYTEVECTOR_LENGTH (bv2
);
411 if (c_len1
== c_len2
)
413 signed char *c_bv1
, *c_bv2
;
415 c_bv1
= SCM_R6RS_BYTEVECTOR_CONTENTS (bv1
);
416 c_bv2
= SCM_R6RS_BYTEVECTOR_CONTENTS (bv2
);
418 result
= scm_from_bool (!memcmp (c_bv1
, c_bv2
, c_len1
));
425 SCM_DEFINE (scm_r6rs_bytevector_fill_x
, "bytevector-fill!", 2, 0, 0,
427 "Fill bytevector @var{bv} with @var{fill}, a byte.")
428 #define FUNC_NAME s_scm_r6rs_bytevector_fill_x
431 signed char *c_bv
, c_fill
;
433 SCM_VALIDATE_R6RS_BYTEVECTOR (1, bv
);
434 c_fill
= scm_to_int8 (fill
);
436 c_len
= SCM_R6RS_BYTEVECTOR_LENGTH (bv
);
437 c_bv
= SCM_R6RS_BYTEVECTOR_CONTENTS (bv
);
439 for (i
= 0; i
< c_len
; i
++)
442 return SCM_UNSPECIFIED
;
446 SCM_DEFINE (scm_r6rs_bytevector_copy_x
, "bytevector-copy!", 5, 0, 0,
447 (SCM source
, SCM source_start
, SCM target
, SCM target_start
,
449 "Copy @var{len} bytes from @var{source} into @var{target}, "
450 "starting reading from @var{source_start} (a positive index "
451 "within @var{source}) and start writing at "
452 "@var{target_start}.")
453 #define FUNC_NAME s_scm_r6rs_bytevector_copy_x
455 unsigned c_len
, c_source_len
, c_target_len
;
456 unsigned c_source_start
, c_target_start
;
457 signed char *c_source
, *c_target
;
459 SCM_VALIDATE_R6RS_BYTEVECTOR (1, source
);
460 SCM_VALIDATE_R6RS_BYTEVECTOR (3, target
);
462 c_len
= scm_to_uint (len
);
463 c_source_start
= scm_to_uint (source_start
);
464 c_target_start
= scm_to_uint (target_start
);
466 c_source
= SCM_R6RS_BYTEVECTOR_CONTENTS (source
);
467 c_target
= SCM_R6RS_BYTEVECTOR_CONTENTS (target
);
468 c_source_len
= SCM_R6RS_BYTEVECTOR_LENGTH (source
);
469 c_target_len
= SCM_R6RS_BYTEVECTOR_LENGTH (target
);
471 if (SCM_UNLIKELY (c_source_start
+ c_len
> c_source_len
))
472 scm_out_of_range (FUNC_NAME
, source_start
);
473 if (SCM_UNLIKELY (c_target_start
+ c_len
> c_target_len
))
474 scm_out_of_range (FUNC_NAME
, target_start
);
476 memcpy (c_target
+ c_target_start
,
477 c_source
+ c_source_start
,
480 return SCM_UNSPECIFIED
;
484 SCM_DEFINE (scm_r6rs_bytevector_copy
, "bytevector-copy", 1, 0, 0,
486 "Return a newly allocated copy of @var{bv}.")
487 #define FUNC_NAME s_scm_r6rs_bytevector_copy
491 signed char *c_bv
, *c_copy
;
493 SCM_VALIDATE_R6RS_BYTEVECTOR (1, bv
);
495 c_len
= SCM_R6RS_BYTEVECTOR_LENGTH (bv
);
496 c_bv
= SCM_R6RS_BYTEVECTOR_CONTENTS (bv
);
498 copy
= make_bytevector (c_len
);
499 c_copy
= SCM_R6RS_BYTEVECTOR_CONTENTS (copy
);
500 memcpy (c_copy
, c_bv
, c_len
);
507 /* Operations on bytes and octets. */
509 SCM_DEFINE (scm_r6rs_bytevector_u8_ref
, "bytevector-u8-ref", 2, 0, 0,
511 "Return the octet located at @var{index} in @var{bv}.")
512 #define FUNC_NAME s_scm_r6rs_bytevector_u8_ref
514 INTEGER_NATIVE_REF (8, unsigned);
518 SCM_DEFINE (scm_r6rs_bytevector_s8_ref
, "bytevector-s8-ref", 2, 0, 0,
520 "Return the byte located at @var{index} in @var{bv}.")
521 #define FUNC_NAME s_scm_r6rs_bytevector_u8_ref
523 INTEGER_NATIVE_REF (8, signed);
527 SCM_DEFINE (scm_r6rs_bytevector_u8_set_x
, "bytevector-u8-set!", 3, 0, 0,
528 (SCM bv
, SCM index
, SCM value
),
529 "Return the octet located at @var{index} in @var{bv}.")
530 #define FUNC_NAME s_scm_r6rs_bytevector_u8_set_x
532 INTEGER_NATIVE_SET (8, unsigned);
536 SCM_DEFINE (scm_r6rs_bytevector_s8_set_x
, "bytevector-s8-set!", 3, 0, 0,
537 (SCM bv
, SCM index
, SCM value
),
538 "Return the octet located at @var{index} in @var{bv}.")
539 #define FUNC_NAME s_scm_r6rs_bytevector_u8_set_x
541 INTEGER_NATIVE_SET (8, signed);
545 #undef OCTET_ACCESSOR_PROLOGUE
548 SCM_DEFINE (scm_r6rs_bytevector_to_u8_list
, "bytevector->u8-list", 1, 0, 0,
550 "Return a newly allocated list of octets containing the "
551 "contents of @var{bv}.")
552 #define FUNC_NAME s_scm_r6rs_bytevector_to_u8_list
558 SCM_VALIDATE_R6RS_BYTEVECTOR (1, bv
);
560 c_len
= SCM_R6RS_BYTEVECTOR_LENGTH (bv
);
561 c_bv
= (unsigned char *) SCM_R6RS_BYTEVECTOR_CONTENTS (bv
);
563 lst
= scm_make_list (scm_from_uint (c_len
), SCM_UNSPECIFIED
);
564 for (i
= 0, pair
= lst
;
566 i
++, pair
= SCM_CDR (pair
))
568 SCM_SETCAR (pair
, SCM_I_MAKINUM (c_bv
[i
]));
575 SCM_DEFINE (scm_r6rs_u8_list_to_bytevector
, "u8-list->bytevector", 1, 0, 0,
577 "Turn @var{lst}, a list of octets, into a bytevector.")
578 #define FUNC_NAME s_scm_r6rs_u8_list_to_bytevector
584 SCM_VALIDATE_LIST_COPYLEN (1, lst
, c_len
);
586 bv
= make_bytevector (c_len
);
587 c_bv
= (unsigned char *) SCM_R6RS_BYTEVECTOR_CONTENTS (bv
);
589 for (i
= 0; i
< c_len
; lst
= SCM_CDR (lst
), i
++)
591 item
= SCM_CAR (lst
);
593 if (SCM_LIKELY (SCM_I_INUMP (item
)))
597 c_item
= SCM_I_INUM (item
);
598 if (SCM_LIKELY ((c_item
>= 0) && (c_item
< 256)))
599 c_bv
[i
] = (unsigned char) c_item
;
610 scm_wrong_type_arg (FUNC_NAME
, 1, item
);
616 /* Compute the two's complement of VALUE (a positive integer) on SIZE octets
617 using (2^(SIZE * 8) - VALUE). */
619 twos_complement (mpz_t value
, size_t size
)
621 unsigned long bit_count
;
623 /* We expect BIT_COUNT to fit in a unsigned long thanks to the range
624 checking on SIZE performed earlier. */
625 bit_count
= (unsigned long) size
<< 3UL;
627 if (SCM_LIKELY (bit_count
< sizeof (unsigned long)))
628 mpz_ui_sub (value
, 1UL << bit_count
, value
);
634 mpz_ui_pow_ui (max
, 2, bit_count
);
635 mpz_sub (value
, max
, value
);
641 bytevector_large_ref (const char *c_bv
, size_t c_size
, int signed_p
,
646 int c_endianness
, negative_p
= 0;
650 if (scm_is_eq (endianness
, scm_sym_big
))
651 negative_p
= c_bv
[0] & 0x80;
653 negative_p
= c_bv
[c_size
- 1] & 0x80;
656 c_endianness
= scm_is_eq (endianness
, scm_sym_big
) ? 1 : -1;
659 mpz_import (c_mpz
, 1 /* 1 word */, 1 /* word order doesn't matter */,
660 c_size
/* word is C_SIZE-byte long */,
662 0 /* nails */, c_bv
);
664 if (signed_p
&& negative_p
)
666 twos_complement (c_mpz
, c_size
);
667 mpz_neg (c_mpz
, c_mpz
);
670 result
= scm_from_mpz (c_mpz
);
671 mpz_clear (c_mpz
); /* FIXME: Needed? */
677 bytevector_large_set (char *c_bv
, size_t c_size
, int signed_p
,
678 SCM value
, SCM endianness
)
681 int c_endianness
, c_sign
, err
= 0;
683 c_endianness
= scm_is_eq (endianness
, scm_sym_big
) ? 1 : -1;
686 scm_to_mpz (value
, c_mpz
);
688 c_sign
= mpz_sgn (c_mpz
);
691 if (SCM_LIKELY (signed_p
))
693 mpz_neg (c_mpz
, c_mpz
);
694 twos_complement (c_mpz
, c_size
);
705 memset (c_bv
, 0, c_size
);
708 size_t word_count
, value_size
;
710 value_size
= (mpz_sizeinbase (c_mpz
, 2) + (8 * c_size
)) / (8 * c_size
);
711 if (SCM_UNLIKELY (value_size
> c_size
))
718 mpz_export (c_bv
, &word_count
, 1 /* word order doesn't matter */,
719 c_size
, c_endianness
,
720 0 /* nails */, c_mpz
);
721 if (SCM_UNLIKELY (word_count
!= 1))
722 /* Shouldn't happen since we already checked with VALUE_SIZE. */
732 #define GENERIC_INTEGER_ACCESSOR_PROLOGUE(_sign) \
733 unsigned long c_len, c_index, c_size; \
736 SCM_VALIDATE_R6RS_BYTEVECTOR (1, bv); \
737 c_index = scm_to_ulong (index); \
738 c_size = scm_to_ulong (size); \
740 c_len = SCM_R6RS_BYTEVECTOR_LENGTH (bv); \
741 c_bv = (char *) SCM_R6RS_BYTEVECTOR_CONTENTS (bv); \
743 /* C_SIZE must have its 3 higher bits set to zero so that \
744 multiplying it by 8 yields a number that fits in an \
746 if (SCM_UNLIKELY ((c_size == 0) || (c_size >= (ULONG_MAX >> 3L)))) \
747 scm_out_of_range (FUNC_NAME, size); \
748 if (SCM_UNLIKELY (c_index + c_size > c_len)) \
749 scm_out_of_range (FUNC_NAME, index);
752 /* Template of an integer reference function. */
753 #define GENERIC_INTEGER_REF(_sign) \
761 swap = !scm_is_eq (endianness, native_endianness); \
766 _sign char c_value8; \
767 memcpy (&c_value8, c_bv, 1); \
773 INT_TYPE (16, _sign) c_value16; \
774 memcpy (&c_value16, c_bv, 2); \
776 value = (INT_TYPE (16, _sign)) bswap_16 (c_value16); \
785 result = SCM_I_MAKINUM ((_sign int) value); \
788 result = bytevector_large_ref ((char *) c_bv, \
789 c_size, SIGNEDNESS (_sign), \
795 bytevector_signed_ref (const char *c_bv
, size_t c_size
, SCM endianness
)
797 GENERIC_INTEGER_REF (signed);
801 bytevector_unsigned_ref (const char *c_bv
, size_t c_size
, SCM endianness
)
803 GENERIC_INTEGER_REF (unsigned);
807 /* Template of an integer assignment function. */
808 #define GENERIC_INTEGER_SET(_sign) \
813 if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
816 c_value = SCM_I_INUM (value); \
820 if (SCM_LIKELY (INT_VALID_P (8, _sign) (c_value))) \
822 _sign char c_value8; \
823 c_value8 = (_sign char) c_value; \
824 memcpy (c_bv, &c_value8, 1); \
831 if (SCM_LIKELY (INT_VALID_P (16, _sign) (c_value))) \
834 INT_TYPE (16, _sign) c_value16; \
836 swap = !scm_is_eq (endianness, native_endianness); \
839 c_value16 = (INT_TYPE (16, _sign)) bswap_16 (c_value); \
841 c_value16 = c_value; \
843 memcpy (c_bv, &c_value16, 2); \
857 err = bytevector_large_set (c_bv, c_size, \
858 SIGNEDNESS (_sign), \
859 value, endianness); \
867 scm_out_of_range (FUNC_NAME, value); \
871 bytevector_signed_set (char *c_bv
, size_t c_size
,
872 SCM value
, SCM endianness
,
873 const char *func_name
)
874 #define FUNC_NAME func_name
876 GENERIC_INTEGER_SET (signed);
881 bytevector_unsigned_set (char *c_bv
, size_t c_size
,
882 SCM value
, SCM endianness
,
883 const char *func_name
)
884 #define FUNC_NAME func_name
886 GENERIC_INTEGER_SET (unsigned);
890 #undef GENERIC_INTEGER_SET
891 #undef GENERIC_INTEGER_REF
894 SCM_DEFINE (scm_r6rs_bytevector_uint_ref
, "bytevector-uint-ref", 4, 0, 0,
895 (SCM bv
, SCM index
, SCM endianness
, SCM size
),
896 "Return the @var{size}-octet long unsigned integer at index "
897 "@var{index} in @var{bv}.")
898 #define FUNC_NAME s_scm_r6rs_bytevector_uint_ref
900 GENERIC_INTEGER_ACCESSOR_PROLOGUE (unsigned);
902 return (bytevector_unsigned_ref (&c_bv
[c_index
], c_size
, endianness
));
906 SCM_DEFINE (scm_r6rs_bytevector_sint_ref
, "bytevector-sint-ref", 4, 0, 0,
907 (SCM bv
, SCM index
, SCM endianness
, SCM size
),
908 "Return the @var{size}-octet long unsigned integer at index "
909 "@var{index} in @var{bv}.")
910 #define FUNC_NAME s_scm_r6rs_bytevector_sint_ref
912 GENERIC_INTEGER_ACCESSOR_PROLOGUE (signed);
914 return (bytevector_signed_ref (&c_bv
[c_index
], c_size
, endianness
));
918 SCM_DEFINE (scm_r6rs_bytevector_uint_set_x
, "bytevector-uint-set!", 5, 0, 0,
919 (SCM bv
, SCM index
, SCM value
, SCM endianness
, SCM size
),
920 "Set the @var{size}-octet long unsigned integer at @var{index} "
922 #define FUNC_NAME s_scm_r6rs_bytevector_uint_set_x
924 GENERIC_INTEGER_ACCESSOR_PROLOGUE (unsigned);
926 bytevector_unsigned_set (&c_bv
[c_index
], c_size
, value
, endianness
,
929 return SCM_UNSPECIFIED
;
933 SCM_DEFINE (scm_r6rs_bytevector_sint_set_x
, "bytevector-sint-set!", 5, 0, 0,
934 (SCM bv
, SCM index
, SCM value
, SCM endianness
, SCM size
),
935 "Set the @var{size}-octet long signed integer at @var{index} "
937 #define FUNC_NAME s_scm_r6rs_bytevector_sint_set_x
939 GENERIC_INTEGER_ACCESSOR_PROLOGUE (signed);
941 bytevector_signed_set (&c_bv
[c_index
], c_size
, value
, endianness
,
944 return SCM_UNSPECIFIED
;
950 /* Operations on integers of arbitrary size. */
952 #define INTEGERS_TO_LIST(_sign) \
954 size_t i, c_len, c_size; \
956 SCM_VALIDATE_R6RS_BYTEVECTOR (1, bv); \
957 SCM_VALIDATE_SYMBOL (2, endianness); \
958 c_size = scm_to_uint (size); \
960 c_len = SCM_R6RS_BYTEVECTOR_LENGTH (bv); \
961 if (SCM_UNLIKELY (c_len == 0)) \
963 else if (SCM_UNLIKELY (c_len < c_size)) \
964 scm_out_of_range (FUNC_NAME, size); \
969 c_bv = (char *) SCM_R6RS_BYTEVECTOR_CONTENTS (bv); \
971 lst = scm_make_list (scm_from_uint (c_len / c_size), \
973 for (i = 0, pair = lst; \
974 i <= c_len - c_size; \
975 i += c_size, c_bv += c_size, pair = SCM_CDR (pair)) \
978 bytevector_ ## _sign ## _ref (c_bv, c_size, \
985 SCM_DEFINE (scm_r6rs_bytevector_to_sint_list
, "bytevector->sint-list",
987 (SCM bv
, SCM endianness
, SCM size
),
988 "Return a list of signed integers of @var{size} octets "
989 "representing the contents of @var{bv}.")
990 #define FUNC_NAME s_scm_r6rs_bytevector_to_sint_list
992 INTEGERS_TO_LIST (signed);
996 SCM_DEFINE (scm_r6rs_bytevector_to_uint_list
, "bytevector->uint-list",
998 (SCM bv
, SCM endianness
, SCM size
),
999 "Return a list of unsigned integers of @var{size} octets "
1000 "representing the contents of @var{bv}.")
1001 #define FUNC_NAME s_scm_r6rs_bytevector_to_uint_list
1003 INTEGERS_TO_LIST (unsigned);
1007 #undef INTEGER_TO_LIST
1010 #define INTEGER_LIST_TO_BYTEVECTOR(_sign) \
1014 char *c_bv, *c_bv_ptr; \
1016 SCM_VALIDATE_LIST_COPYLEN (1, lst, c_len); \
1017 SCM_VALIDATE_SYMBOL (2, endianness); \
1018 c_size = scm_to_uint (size); \
1020 if (SCM_UNLIKELY ((c_size == 0) || (c_size >= (ULONG_MAX >> 3L)))) \
1021 scm_out_of_range (FUNC_NAME, size); \
1023 bv = make_bytevector (c_len * c_size); \
1024 c_bv = (char *) SCM_R6RS_BYTEVECTOR_CONTENTS (bv); \
1026 for (c_bv_ptr = c_bv; \
1027 !scm_is_null (lst); \
1028 lst = SCM_CDR (lst), c_bv_ptr += c_size) \
1030 bytevector_ ## _sign ## _set (c_bv_ptr, c_size, \
1031 SCM_CAR (lst), endianness, \
1038 SCM_DEFINE (scm_r6rs_uint_list_to_bytevector
, "uint-list->bytevector",
1040 (SCM lst
, SCM endianness
, SCM size
),
1041 "Return a bytevector containing the unsigned integers "
1042 "listed in @var{lst} and encoded on @var{size} octets "
1043 "according to @var{endianness}.")
1044 #define FUNC_NAME s_scm_r6rs_uint_list_to_bytevector
1046 INTEGER_LIST_TO_BYTEVECTOR (unsigned);
1050 SCM_DEFINE (scm_r6rs_sint_list_to_bytevector
, "sint-list->bytevector",
1052 (SCM lst
, SCM endianness
, SCM size
),
1053 "Return a bytevector containing the signed integers "
1054 "listed in @var{lst} and encoded on @var{size} octets "
1055 "according to @var{endianness}.")
1056 #define FUNC_NAME s_scm_r6rs_sint_list_to_bytevector
1058 INTEGER_LIST_TO_BYTEVECTOR (signed);
1062 #undef INTEGER_LIST_TO_BYTEVECTOR
1066 /* Operations on 16-bit integers. */
1068 SCM_DEFINE (scm_r6rs_bytevector_u16_ref
, "bytevector-u16-ref",
1070 (SCM bv
, SCM index
, SCM endianness
),
1071 "Return the unsigned 16-bit integer from @var{bv} at "
1073 #define FUNC_NAME s_scm_r6rs_bytevector_u16_ref
1075 INTEGER_REF (16, unsigned);
1079 SCM_DEFINE (scm_r6rs_bytevector_s16_ref
, "bytevector-s16-ref",
1081 (SCM bv
, SCM index
, SCM endianness
),
1082 "Return the signed 16-bit integer from @var{bv} at "
1084 #define FUNC_NAME s_scm_r6rs_bytevector_s16_ref
1086 INTEGER_REF (16, signed);
1090 SCM_DEFINE (scm_r6rs_bytevector_u16_native_ref
, "bytevector-u16-native-ref",
1092 (SCM bv
, SCM index
),
1093 "Return the unsigned 16-bit integer from @var{bv} at "
1094 "@var{index} using the native endianness.")
1095 #define FUNC_NAME s_scm_r6rs_bytevector_u16_native_ref
1097 INTEGER_NATIVE_REF (16, unsigned);
1101 SCM_DEFINE (scm_r6rs_bytevector_s16_native_ref
, "bytevector-s16-native-ref",
1103 (SCM bv
, SCM index
),
1104 "Return the unsigned 16-bit integer from @var{bv} at "
1105 "@var{index} using the native endianness.")
1106 #define FUNC_NAME s_scm_r6rs_bytevector_u16_native_ref
1108 INTEGER_NATIVE_REF (16, signed);
1112 SCM_DEFINE (scm_r6rs_bytevector_u16_set_x
, "bytevector-u16-set!",
1114 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1115 "Store @var{value} in @var{bv} at @var{index} according to "
1116 "@var{endianness}.")
1117 #define FUNC_NAME s_scm_r6rs_bytevector_u16_set_x
1119 INTEGER_SET (16, unsigned);
1123 SCM_DEFINE (scm_r6rs_bytevector_s16_set_x
, "bytevector-s16-set!",
1125 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1126 "Store @var{value} in @var{bv} at @var{index} according to "
1127 "@var{endianness}.")
1128 #define FUNC_NAME s_scm_r6rs_bytevector_s16_set_x
1130 INTEGER_SET (16, signed);
1134 SCM_DEFINE (scm_r6rs_bytevector_u16_native_set_x
, "bytevector-u16-native-set!",
1136 (SCM bv
, SCM index
, SCM value
),
1137 "Store the unsigned integer @var{value} at index @var{index} "
1138 "of @var{bv} using the native endianness.")
1139 #define FUNC_NAME s_scm_r6rs_bytevector_u16_native_set_x
1141 INTEGER_NATIVE_SET (16, unsigned);
1145 SCM_DEFINE (scm_r6rs_bytevector_s16_native_set_x
, "bytevector-s16-native-set!",
1147 (SCM bv
, SCM index
, SCM value
),
1148 "Store the signed integer @var{value} at index @var{index} "
1149 "of @var{bv} using the native endianness.")
1150 #define FUNC_NAME s_scm_r6rs_bytevector_u16_native_set_x
1152 INTEGER_NATIVE_SET (16, signed);
1158 /* Operations on 32-bit integers. */
1160 /* Unfortunately, on 32-bit machines `SCM' is not large enough to hold
1161 arbitrary 32-bit integers. Thus we fall back to using the
1162 `large_{ref,set}' variants on 32-bit machines. */
1164 #define LARGE_INTEGER_REF(_len, _sign) \
1165 INTEGER_ACCESSOR_PROLOGUE(_len, _sign); \
1166 SCM_VALIDATE_SYMBOL (3, endianness); \
1168 return (bytevector_large_ref ((char *) c_bv + c_index, _len / 8, \
1169 SIGNEDNESS (_sign), endianness));
1171 #define LARGE_INTEGER_SET(_len, _sign) \
1173 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
1174 SCM_VALIDATE_SYMBOL (4, endianness); \
1176 err = bytevector_large_set ((char *) c_bv + c_index, _len / 8, \
1177 SIGNEDNESS (_sign), value, endianness); \
1178 if (SCM_UNLIKELY (err)) \
1179 scm_out_of_range (FUNC_NAME, value); \
1181 return SCM_UNSPECIFIED;
1183 #define LARGE_INTEGER_NATIVE_REF(_len, _sign) \
1184 INTEGER_ACCESSOR_PROLOGUE(_len, _sign); \
1185 return (bytevector_large_ref ((char *) c_bv + c_index, _len / 8, \
1186 SIGNEDNESS (_sign), native_endianness));
1188 #define LARGE_INTEGER_NATIVE_SET(_len, _sign) \
1190 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
1192 err = bytevector_large_set ((char *) c_bv + c_index, _len / 8, \
1193 SIGNEDNESS (_sign), value, \
1194 native_endianness); \
1195 if (SCM_UNLIKELY (err)) \
1196 scm_out_of_range (FUNC_NAME, value); \
1198 return SCM_UNSPECIFIED;
1201 SCM_DEFINE (scm_r6rs_bytevector_u32_ref
, "bytevector-u32-ref",
1203 (SCM bv
, SCM index
, SCM endianness
),
1204 "Return the unsigned 32-bit integer from @var{bv} at "
1206 #define FUNC_NAME s_scm_r6rs_bytevector_u32_ref
1208 #if SIZEOF_VOID_P > 4
1209 INTEGER_REF (32, unsigned);
1211 LARGE_INTEGER_REF (32, unsigned);
1216 SCM_DEFINE (scm_r6rs_bytevector_s32_ref
, "bytevector-s32-ref",
1218 (SCM bv
, SCM index
, SCM endianness
),
1219 "Return the signed 32-bit integer from @var{bv} at "
1221 #define FUNC_NAME s_scm_r6rs_bytevector_s32_ref
1223 #if SIZEOF_VOID_P > 4
1224 INTEGER_REF (32, signed);
1226 LARGE_INTEGER_REF (32, signed);
1231 SCM_DEFINE (scm_r6rs_bytevector_u32_native_ref
, "bytevector-u32-native-ref",
1233 (SCM bv
, SCM index
),
1234 "Return the unsigned 32-bit integer from @var{bv} at "
1235 "@var{index} using the native endianness.")
1236 #define FUNC_NAME s_scm_r6rs_bytevector_u32_native_ref
1238 #if SIZEOF_VOID_P > 4
1239 INTEGER_NATIVE_REF (32, unsigned);
1241 LARGE_INTEGER_NATIVE_REF (32, unsigned);
1246 SCM_DEFINE (scm_r6rs_bytevector_s32_native_ref
, "bytevector-s32-native-ref",
1248 (SCM bv
, SCM index
),
1249 "Return the unsigned 32-bit integer from @var{bv} at "
1250 "@var{index} using the native endianness.")
1251 #define FUNC_NAME s_scm_r6rs_bytevector_u32_native_ref
1253 #if SIZEOF_VOID_P > 4
1254 INTEGER_NATIVE_REF (32, signed);
1256 LARGE_INTEGER_NATIVE_REF (32, signed);
1261 SCM_DEFINE (scm_r6rs_bytevector_u32_set_x
, "bytevector-u32-set!",
1263 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1264 "Store @var{value} in @var{bv} at @var{index} according to "
1265 "@var{endianness}.")
1266 #define FUNC_NAME s_scm_r6rs_bytevector_u32_set_x
1268 #if SIZEOF_VOID_P > 4
1269 INTEGER_SET (32, unsigned);
1271 LARGE_INTEGER_SET (32, unsigned);
1276 SCM_DEFINE (scm_r6rs_bytevector_s32_set_x
, "bytevector-s32-set!",
1278 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1279 "Store @var{value} in @var{bv} at @var{index} according to "
1280 "@var{endianness}.")
1281 #define FUNC_NAME s_scm_r6rs_bytevector_s32_set_x
1283 #if SIZEOF_VOID_P > 4
1284 INTEGER_SET (32, signed);
1286 LARGE_INTEGER_SET (32, signed);
1291 SCM_DEFINE (scm_r6rs_bytevector_u32_native_set_x
, "bytevector-u32-native-set!",
1293 (SCM bv
, SCM index
, SCM value
),
1294 "Store the unsigned integer @var{value} at index @var{index} "
1295 "of @var{bv} using the native endianness.")
1296 #define FUNC_NAME s_scm_r6rs_bytevector_u32_native_set_x
1298 #if SIZEOF_VOID_P > 4
1299 INTEGER_NATIVE_SET (32, unsigned);
1301 LARGE_INTEGER_NATIVE_SET (32, unsigned);
1306 SCM_DEFINE (scm_r6rs_bytevector_s32_native_set_x
, "bytevector-s32-native-set!",
1308 (SCM bv
, SCM index
, SCM value
),
1309 "Store the signed integer @var{value} at index @var{index} "
1310 "of @var{bv} using the native endianness.")
1311 #define FUNC_NAME s_scm_r6rs_bytevector_u32_native_set_x
1313 #if SIZEOF_VOID_P > 4
1314 INTEGER_NATIVE_SET (32, signed);
1316 LARGE_INTEGER_NATIVE_SET (32, signed);
1323 /* Operations on 64-bit integers. */
1325 /* For 64-bit integers, we use only the `large_{ref,set}' variant. */
1327 SCM_DEFINE (scm_r6rs_bytevector_u64_ref
, "bytevector-u64-ref",
1329 (SCM bv
, SCM index
, SCM endianness
),
1330 "Return the unsigned 64-bit integer from @var{bv} at "
1332 #define FUNC_NAME s_scm_r6rs_bytevector_u64_ref
1334 LARGE_INTEGER_REF (64, unsigned);
1338 SCM_DEFINE (scm_r6rs_bytevector_s64_ref
, "bytevector-s64-ref",
1340 (SCM bv
, SCM index
, SCM endianness
),
1341 "Return the signed 64-bit integer from @var{bv} at "
1343 #define FUNC_NAME s_scm_r6rs_bytevector_s64_ref
1345 LARGE_INTEGER_REF (64, signed);
1349 SCM_DEFINE (scm_r6rs_bytevector_u64_native_ref
, "bytevector-u64-native-ref",
1351 (SCM bv
, SCM index
),
1352 "Return the unsigned 64-bit integer from @var{bv} at "
1353 "@var{index} using the native endianness.")
1354 #define FUNC_NAME s_scm_r6rs_bytevector_u64_native_ref
1356 LARGE_INTEGER_NATIVE_REF (64, unsigned);
1360 SCM_DEFINE (scm_r6rs_bytevector_s64_native_ref
, "bytevector-s64-native-ref",
1362 (SCM bv
, SCM index
),
1363 "Return the unsigned 64-bit integer from @var{bv} at "
1364 "@var{index} using the native endianness.")
1365 #define FUNC_NAME s_scm_r6rs_bytevector_u64_native_ref
1367 LARGE_INTEGER_NATIVE_REF (64, signed);
1371 SCM_DEFINE (scm_r6rs_bytevector_u64_set_x
, "bytevector-u64-set!",
1373 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1374 "Store @var{value} in @var{bv} at @var{index} according to "
1375 "@var{endianness}.")
1376 #define FUNC_NAME s_scm_r6rs_bytevector_u64_set_x
1378 LARGE_INTEGER_SET (64, unsigned);
1382 SCM_DEFINE (scm_r6rs_bytevector_s64_set_x
, "bytevector-s64-set!",
1384 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1385 "Store @var{value} in @var{bv} at @var{index} according to "
1386 "@var{endianness}.")
1387 #define FUNC_NAME s_scm_r6rs_bytevector_s64_set_x
1389 LARGE_INTEGER_SET (64, signed);
1393 SCM_DEFINE (scm_r6rs_bytevector_u64_native_set_x
, "bytevector-u64-native-set!",
1395 (SCM bv
, SCM index
, SCM value
),
1396 "Store the unsigned integer @var{value} at index @var{index} "
1397 "of @var{bv} using the native endianness.")
1398 #define FUNC_NAME s_scm_r6rs_bytevector_u64_native_set_x
1400 LARGE_INTEGER_NATIVE_SET (64, unsigned);
1404 SCM_DEFINE (scm_r6rs_bytevector_s64_native_set_x
, "bytevector-s64-native-set!",
1406 (SCM bv
, SCM index
, SCM value
),
1407 "Store the signed integer @var{value} at index @var{index} "
1408 "of @var{bv} using the native endianness.")
1409 #define FUNC_NAME s_scm_r6rs_bytevector_u64_native_set_x
1411 LARGE_INTEGER_NATIVE_SET (64, signed);
1417 /* Operations on IEEE-754 numbers. */
1419 /* There are two possible word endians, visible in glibc's <ieee754.h>.
1420 However, in R6RS, when the endianness is `little', little endian is
1421 assumed for both the byte order and the word order. This is clear from
1422 Section 2.1 of R6RS-lib (in response to
1423 http://www.r6rs.org/formal-comments/comment-187.txt). */
1426 /* Convert to/from a floating-point number with different endianness. This
1427 method is probably not the most efficient but it should be portable. */
1430 float_to_foreign_endianness (union scm_r6rs_ieee754_float
*target
,
1433 union scm_r6rs_ieee754_float src
;
1437 #ifdef WORDS_BIGENDIAN
1438 /* Assuming little endian for both byte and word order. */
1439 target
->little_endian
.negative
= src
.big_endian
.negative
;
1440 target
->little_endian
.exponent
= src
.big_endian
.exponent
;
1441 target
->little_endian
.mantissa
= src
.big_endian
.mantissa
;
1443 target
->big_endian
.negative
= src
.little_endian
.negative
;
1444 target
->big_endian
.exponent
= src
.little_endian
.exponent
;
1445 target
->big_endian
.mantissa
= src
.little_endian
.mantissa
;
1450 float_from_foreign_endianness (const union scm_r6rs_ieee754_float
*source
)
1452 union scm_r6rs_ieee754_float result
;
1454 #ifdef WORDS_BIGENDIAN
1455 /* Assuming little endian for both byte and word order. */
1456 result
.big_endian
.negative
= source
->little_endian
.negative
;
1457 result
.big_endian
.exponent
= source
->little_endian
.exponent
;
1458 result
.big_endian
.mantissa
= source
->little_endian
.mantissa
;
1460 result
.little_endian
.negative
= source
->big_endian
.negative
;
1461 result
.little_endian
.exponent
= source
->big_endian
.exponent
;
1462 result
.little_endian
.mantissa
= source
->big_endian
.mantissa
;
1469 double_to_foreign_endianness (union scm_r6rs_ieee754_double
*target
,
1472 union scm_r6rs_ieee754_double src
;
1476 #ifdef WORDS_BIGENDIAN
1477 /* Assuming little endian for both byte and word order. */
1478 target
->little_little_endian
.negative
= src
.big_endian
.negative
;
1479 target
->little_little_endian
.exponent
= src
.big_endian
.exponent
;
1480 target
->little_little_endian
.mantissa0
= src
.big_endian
.mantissa0
;
1481 target
->little_little_endian
.mantissa1
= src
.big_endian
.mantissa1
;
1483 target
->big_endian
.negative
= src
.little_little_endian
.negative
;
1484 target
->big_endian
.exponent
= src
.little_little_endian
.exponent
;
1485 target
->big_endian
.mantissa0
= src
.little_little_endian
.mantissa0
;
1486 target
->big_endian
.mantissa1
= src
.little_little_endian
.mantissa1
;
1490 static inline double
1491 double_from_foreign_endianness (const union scm_r6rs_ieee754_double
*source
)
1493 union scm_r6rs_ieee754_double result
;
1495 #ifdef WORDS_BIGENDIAN
1496 /* Assuming little endian for both byte and word order. */
1497 result
.big_endian
.negative
= source
->little_little_endian
.negative
;
1498 result
.big_endian
.exponent
= source
->little_little_endian
.exponent
;
1499 result
.big_endian
.mantissa0
= source
->little_little_endian
.mantissa0
;
1500 result
.big_endian
.mantissa1
= source
->little_little_endian
.mantissa1
;
1502 result
.little_little_endian
.negative
= source
->big_endian
.negative
;
1503 result
.little_little_endian
.exponent
= source
->big_endian
.exponent
;
1504 result
.little_little_endian
.mantissa0
= source
->big_endian
.mantissa0
;
1505 result
.little_little_endian
.mantissa1
= source
->big_endian
.mantissa1
;
1511 /* Template macros to abstract over doubles and floats.
1512 XXX: Guile can only convert to/from doubles. */
1513 #define IEEE754_UNION(_c_type) union scm_r6rs_ieee754_ ## _c_type
1514 #define IEEE754_TO_SCM(_c_type) scm_from_double
1515 #define IEEE754_FROM_SCM(_c_type) scm_to_double
1516 #define IEEE754_FROM_FOREIGN_ENDIANNESS(_c_type) \
1517 _c_type ## _from_foreign_endianness
1518 #define IEEE754_TO_FOREIGN_ENDIANNESS(_c_type) \
1519 _c_type ## _to_foreign_endianness
1522 /* Templace getters and setters. */
1524 #define IEEE754_ACCESSOR_PROLOGUE(_type) \
1525 INTEGER_ACCESSOR_PROLOGUE (sizeof (_type) << 3UL, signed);
1527 #define IEEE754_REF(_type) \
1530 IEEE754_ACCESSOR_PROLOGUE (_type); \
1531 SCM_VALIDATE_SYMBOL (3, endianness); \
1533 if (scm_is_eq (endianness, native_endianness)) \
1534 memcpy (&c_result, &c_bv[c_index], sizeof (c_result)); \
1537 IEEE754_UNION (_type) c_raw; \
1539 memcpy (&c_raw, &c_bv[c_index], sizeof (c_raw)); \
1541 IEEE754_FROM_FOREIGN_ENDIANNESS (_type) (&c_raw); \
1544 return (IEEE754_TO_SCM (_type) (c_result));
1546 #define IEEE754_NATIVE_REF(_type) \
1549 IEEE754_ACCESSOR_PROLOGUE (_type); \
1551 memcpy (&c_result, &c_bv[c_index], sizeof (c_result)); \
1552 return (IEEE754_TO_SCM (_type) (c_result));
1554 #define IEEE754_SET(_type) \
1557 IEEE754_ACCESSOR_PROLOGUE (_type); \
1558 SCM_VALIDATE_REAL (3, value); \
1559 SCM_VALIDATE_SYMBOL (4, endianness); \
1560 c_value = IEEE754_FROM_SCM (_type) (value); \
1562 if (scm_is_eq (endianness, native_endianness)) \
1563 memcpy (&c_bv[c_index], &c_value, sizeof (c_value)); \
1566 IEEE754_UNION (_type) c_raw; \
1568 IEEE754_TO_FOREIGN_ENDIANNESS (_type) (&c_raw, c_value); \
1569 memcpy (&c_bv[c_index], &c_raw, sizeof (c_raw)); \
1572 return SCM_UNSPECIFIED;
1574 #define IEEE754_NATIVE_SET(_type) \
1577 IEEE754_ACCESSOR_PROLOGUE (_type); \
1578 SCM_VALIDATE_REAL (3, value); \
1579 c_value = IEEE754_FROM_SCM (_type) (value); \
1581 memcpy (&c_bv[c_index], &c_value, sizeof (c_value)); \
1582 return SCM_UNSPECIFIED;
1585 /* Single precision. */
1587 SCM_DEFINE (scm_r6rs_bytevector_ieee_single_ref
,
1588 "bytevector-ieee-single-ref",
1590 (SCM bv
, SCM index
, SCM endianness
),
1591 "Return the IEEE-754 single from @var{bv} at "
1593 #define FUNC_NAME s_scm_r6rs_bytevector_ieee_single_ref
1595 IEEE754_REF (float);
1599 SCM_DEFINE (scm_r6rs_bytevector_ieee_single_native_ref
,
1600 "bytevector-ieee-single-native-ref",
1602 (SCM bv
, SCM index
),
1603 "Return the IEEE-754 single from @var{bv} at "
1604 "@var{index} using the native endianness.")
1605 #define FUNC_NAME s_scm_r6rs_bytevector_ieee_single_native_ref
1607 IEEE754_NATIVE_REF (float);
1611 SCM_DEFINE (scm_r6rs_bytevector_ieee_single_set_x
,
1612 "bytevector-ieee-single-set!",
1614 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1615 "Store real @var{value} in @var{bv} at @var{index} according to "
1616 "@var{endianness}.")
1617 #define FUNC_NAME s_scm_r6rs_bytevector_ieee_single_set_x
1619 IEEE754_SET (float);
1623 SCM_DEFINE (scm_r6rs_bytevector_ieee_single_native_set_x
,
1624 "bytevector-ieee-single-native-set!",
1626 (SCM bv
, SCM index
, SCM value
),
1627 "Store the real @var{value} at index @var{index} "
1628 "of @var{bv} using the native endianness.")
1629 #define FUNC_NAME s_scm_r6rs_bytevector_ieee_single_native_set_x
1631 IEEE754_NATIVE_SET (float);
1636 /* Double precision. */
1638 SCM_DEFINE (scm_r6rs_bytevector_ieee_double_ref
,
1639 "bytevector-ieee-double-ref",
1641 (SCM bv
, SCM index
, SCM endianness
),
1642 "Return the IEEE-754 double from @var{bv} at "
1644 #define FUNC_NAME s_scm_r6rs_bytevector_ieee_double_ref
1646 IEEE754_REF (double);
1650 SCM_DEFINE (scm_r6rs_bytevector_ieee_double_native_ref
,
1651 "bytevector-ieee-double-native-ref",
1653 (SCM bv
, SCM index
),
1654 "Return the IEEE-754 double from @var{bv} at "
1655 "@var{index} using the native endianness.")
1656 #define FUNC_NAME s_scm_r6rs_bytevector_ieee_double_native_ref
1658 IEEE754_NATIVE_REF (double);
1662 SCM_DEFINE (scm_r6rs_bytevector_ieee_double_set_x
,
1663 "bytevector-ieee-double-set!",
1665 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1666 "Store real @var{value} in @var{bv} at @var{index} according to "
1667 "@var{endianness}.")
1668 #define FUNC_NAME s_scm_r6rs_bytevector_ieee_double_set_x
1670 IEEE754_SET (double);
1674 SCM_DEFINE (scm_r6rs_bytevector_ieee_double_native_set_x
,
1675 "bytevector-ieee-double-native-set!",
1677 (SCM bv
, SCM index
, SCM value
),
1678 "Store the real @var{value} at index @var{index} "
1679 "of @var{bv} using the native endianness.")
1680 #define FUNC_NAME s_scm_r6rs_bytevector_ieee_double_native_set_x
1682 IEEE754_NATIVE_SET (double);
1687 #undef IEEE754_UNION
1688 #undef IEEE754_TO_SCM
1689 #undef IEEE754_FROM_SCM
1690 #undef IEEE754_FROM_FOREIGN_ENDIANNESS
1691 #undef IEEE754_TO_FOREIGN_ENDIANNESS
1693 #undef IEEE754_NATIVE_REF
1695 #undef IEEE754_NATIVE_SET
1698 /* Operations on strings. */
1701 /* Produce a function that returns the length of a UTF-encoded string. */
1702 #define UTF_STRLEN_FUNCTION(_utf_width) \
1703 static inline size_t \
1704 utf ## _utf_width ## _strlen (const uint ## _utf_width ## _t *str) \
1707 const uint ## _utf_width ## _t *ptr; \
1715 return (len * ((_utf_width) / 8)); \
1718 UTF_STRLEN_FUNCTION (8)
1721 /* Return the length (in bytes) of STR, a UTF-(UTF_WIDTH) encoded string. */
1722 #define UTF_STRLEN(_utf_width, _str) \
1723 utf ## _utf_width ## _strlen (_str)
1725 /* Return the "portable" name of the UTF encoding of size UTF_WIDTH and
1726 ENDIANNESS (Gnulib's `iconv_open' module guarantees the portability of the
1729 utf_encoding_name (char *name
, size_t utf_width
, SCM endianness
)
1731 strcpy (name
, "UTF-");
1732 strcat (name
, ((utf_width
== 8)
1734 : ((utf_width
== 16)
1736 : ((utf_width
== 32)
1740 ((scm_is_eq (endianness
, scm_sym_big
))
1742 : ((scm_is_eq (endianness
, scm_sym_little
))
1747 /* Maximum length of a UTF encoding name. */
1748 #define MAX_UTF_ENCODING_NAME_LEN 16
1750 /* Produce the body of a `string->utf' function. */
1751 #define STRING_TO_UTF(_utf_width) \
1755 char c_utf_name[MAX_UTF_ENCODING_NAME_LEN]; \
1756 char *c_utf = NULL, *c_locale; \
1757 size_t c_strlen, c_raw_strlen, c_utf_len = 0; \
1759 SCM_VALIDATE_STRING (1, str); \
1760 if (endianness == SCM_UNDEFINED) \
1761 endianness = scm_sym_big; \
1763 SCM_VALIDATE_SYMBOL (2, endianness); \
1765 c_strlen = scm_c_string_length (str); \
1766 c_raw_strlen = c_strlen * ((_utf_width) / 8); \
1769 c_str = (char *) alloca (c_raw_strlen + 1); \
1770 c_raw_strlen = scm_to_locale_stringbuf (str, c_str, c_strlen); \
1772 while (c_raw_strlen > c_strlen); \
1773 c_str[c_raw_strlen] = '\0'; \
1775 utf_encoding_name (c_utf_name, (_utf_width), endianness); \
1777 c_locale = (char *) alloca (strlen (locale_charset ()) + 1); \
1778 strcpy (c_locale, locale_charset ()); \
1780 err = mem_iconveh (c_str, c_raw_strlen, \
1781 c_locale, c_utf_name, \
1782 iconveh_question_mark, NULL, \
1783 &c_utf, &c_utf_len); \
1784 if (SCM_UNLIKELY (err)) \
1785 scm_syserror_msg (FUNC_NAME, "failed to convert string: ~A", \
1786 scm_list_1 (str), err); \
1788 /* C_UTF is null-terminated. */ \
1789 utf = scm_r6rs_c_take_bytevector ((signed char *) c_utf, \
1796 SCM_DEFINE (scm_r6rs_string_to_utf8
, "string->utf8",
1799 "Return a newly allocated bytevector that contains the UTF-8 "
1800 "encoding of @var{str}.")
1801 #define FUNC_NAME s_scm_r6rs_string_to_utf8
1806 size_t c_strlen
, c_raw_strlen
;
1808 SCM_VALIDATE_STRING (1, str
);
1810 c_strlen
= scm_c_string_length (str
);
1811 c_raw_strlen
= c_strlen
;
1814 c_str
= (char *) alloca (c_raw_strlen
+ 1);
1815 c_raw_strlen
= scm_to_locale_stringbuf (str
, c_str
, c_strlen
);
1817 while (c_raw_strlen
> c_strlen
);
1818 c_str
[c_raw_strlen
] = '\0';
1820 c_utf
= u8_strconv_from_locale (c_str
);
1821 if (SCM_UNLIKELY (c_utf
== NULL
))
1822 scm_syserror (FUNC_NAME
);
1824 /* C_UTF is null-terminated. */
1825 utf
= scm_r6rs_c_take_bytevector ((signed char *) c_utf
,
1826 UTF_STRLEN (8, c_utf
));
1832 SCM_DEFINE (scm_r6rs_string_to_utf16
, "string->utf16",
1834 (SCM str
, SCM endianness
),
1835 "Return a newly allocated bytevector that contains the UTF-16 "
1836 "encoding of @var{str}.")
1837 #define FUNC_NAME s_scm_r6rs_string_to_utf16
1843 SCM_DEFINE (scm_r6rs_string_to_utf32
, "string->utf32",
1845 (SCM str
, SCM endianness
),
1846 "Return a newly allocated bytevector that contains the UTF-32 "
1847 "encoding of @var{str}.")
1848 #define FUNC_NAME s_scm_r6rs_string_to_utf32
1855 /* Produce the body of a function that converts a UTF-encoded bytevector to a
1857 #define UTF_TO_STRING(_utf_width) \
1858 SCM str = SCM_BOOL_F; \
1860 char *c_str = NULL, *c_locale; \
1861 char c_utf_name[MAX_UTF_ENCODING_NAME_LEN]; \
1862 const char *c_utf; \
1863 size_t c_strlen = 0, c_utf_len; \
1865 SCM_VALIDATE_R6RS_BYTEVECTOR (1, utf); \
1866 if (endianness == SCM_UNDEFINED) \
1867 endianness = scm_sym_big; \
1869 SCM_VALIDATE_SYMBOL (2, endianness); \
1871 c_utf_len = SCM_R6RS_BYTEVECTOR_LENGTH (utf); \
1872 c_utf = (char *) SCM_R6RS_BYTEVECTOR_CONTENTS (utf); \
1873 utf_encoding_name (c_utf_name, (_utf_width), endianness); \
1875 c_locale = (char *) alloca (strlen (locale_charset ()) + 1); \
1876 strcpy (c_locale, locale_charset ()); \
1878 err = mem_iconveh (c_utf, c_utf_len, \
1879 c_utf_name, c_locale, \
1880 iconveh_question_mark, NULL, \
1881 &c_str, &c_strlen); \
1882 if (SCM_UNLIKELY (err)) \
1883 scm_syserror_msg (FUNC_NAME, "failed to convert to string: ~A", \
1884 scm_list_1 (utf), err); \
1886 /* C_STR is null-terminated. */ \
1887 str = scm_take_locale_stringn (c_str, c_strlen); \
1892 SCM_DEFINE (scm_r6rs_utf8_to_string
, "utf8->string",
1895 "Return a newly allocate string that contains from the UTF-8-"
1896 "encoded contents of bytevector @var{utf}.")
1897 #define FUNC_NAME s_scm_r6rs_utf8_to_string
1901 char *c_str
= NULL
, *c_locale
;
1903 size_t c_utf_len
, c_strlen
= 0;
1905 SCM_VALIDATE_R6RS_BYTEVECTOR (1, utf
);
1907 c_utf_len
= SCM_R6RS_BYTEVECTOR_LENGTH (utf
);
1909 c_locale
= (char *) alloca (strlen (locale_charset ()) + 1);
1910 strcpy (c_locale
, locale_charset ());
1912 c_utf
= (char *) SCM_R6RS_BYTEVECTOR_CONTENTS (utf
);
1913 err
= mem_iconveh (c_utf
, c_utf_len
,
1915 iconveh_question_mark
, NULL
,
1917 if (SCM_UNLIKELY (err
))
1918 scm_syserror_msg (FUNC_NAME
, "failed to convert to string: ~A",
1919 scm_list_1 (utf
), err
);
1921 /* C_STR is null-terminated. */
1922 str
= scm_take_locale_stringn (c_str
, c_strlen
);
1928 SCM_DEFINE (scm_r6rs_utf16_to_string
, "utf16->string",
1930 (SCM utf
, SCM endianness
),
1931 "Return a newly allocate string that contains from the UTF-17-"
1932 "encoded contents of bytevector @var{utf}.")
1933 #define FUNC_NAME s_scm_r6rs_utf16_to_string
1939 SCM_DEFINE (scm_r6rs_utf32_to_string
, "utf32->string",
1941 (SCM utf
, SCM endianness
),
1942 "Return a newly allocate string that contains from the UTF-17-"
1943 "encoded contents of bytevector @var{utf}.")
1944 #define FUNC_NAME s_scm_r6rs_utf32_to_string
1952 /* Initialization. */
1955 scm_init_r6rs_bytevector (void)
1957 #include "bytevector.x"
1959 #ifdef WORDS_BIGENDIAN
1960 native_endianness
= scm_sym_big
;
1962 native_endianness
= scm_sym_little
;
1965 scm_r6rs_endianness_big
= scm_sym_big
;
1966 scm_r6rs_endianness_little
= scm_sym_little
;
1968 scm_r6rs_null_bytevector
=
1969 scm_gc_protect_object (make_bytevector_from_buffer (0, NULL
));