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 */
27 #include "bytevector.h"
30 #include "striconveh.h"
38 /* Assuming 32-bit longs. */
39 # define ULONG_MAX 4294967295UL
48 /* Convenience macros. These are used by the various templates (macros) that
49 are parameterized by integer signedness. */
50 #define INT8_T_signed scm_t_int8
51 #define INT8_T_unsigned scm_t_uint8
52 #define INT16_T_signed scm_t_int16
53 #define INT16_T_unsigned scm_t_uint16
54 #define INT32_T_signed scm_t_int32
55 #define INT32_T_unsigned scm_t_uint32
56 #define is_signed_int8(_x) (((_x) >= -128L) && ((_x) <= 127L))
57 #define is_unsigned_int8(_x) ((_x) <= 255UL)
58 #define is_signed_int16(_x) (((_x) >= -32768L) && ((_x) <= 32767L))
59 #define is_unsigned_int16(_x) ((_x) <= 65535UL)
60 #define is_signed_int32(_x) (((_x) >= -2147483648L) && ((_x) <= 2147483647L))
61 #define is_unsigned_int32(_x) ((_x) <= 4294967295UL)
62 #define SIGNEDNESS_signed 1
63 #define SIGNEDNESS_unsigned 0
65 #define INT_TYPE(_size, _sign) INT ## _size ## _T_ ## _sign
66 #define INT_SWAP(_size) bswap_ ## _size
67 #define INT_VALID_P(_size, _sign) is_ ## _sign ## _int ## _size
68 #define SIGNEDNESS(_sign) SIGNEDNESS_ ## _sign
71 #define INTEGER_ACCESSOR_PROLOGUE(_len, _sign) \
72 unsigned c_len, c_index; \
75 SCM_VALIDATE_R6RS_BYTEVECTOR (1, bv); \
76 c_index = scm_to_uint (index); \
78 c_len = SCM_R6RS_BYTEVECTOR_LENGTH (bv); \
79 c_bv = (_sign char *) SCM_R6RS_BYTEVECTOR_CONTENTS (bv); \
81 if (SCM_UNLIKELY (c_index + ((_len) >> 3UL) - 1 >= c_len)) \
82 scm_out_of_range (FUNC_NAME, index);
84 /* Template for fixed-size integer access (only 8, 16 or 32-bit). */
85 #define INTEGER_REF(_len, _sign) \
88 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
89 SCM_VALIDATE_SYMBOL (3, endianness); \
92 INT_TYPE (_len, _sign) c_result; \
94 memcpy (&c_result, &c_bv[c_index], (_len) / 8); \
95 if (!scm_is_eq (endianness, native_endianness)) \
96 c_result = INT_SWAP (_len) (c_result); \
98 result = SCM_I_MAKINUM (c_result); \
103 /* Template for fixed-size integer access using the native endianness. */
104 #define INTEGER_NATIVE_REF(_len, _sign) \
107 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
110 INT_TYPE (_len, _sign) c_result; \
112 memcpy (&c_result, &c_bv[c_index], (_len) / 8); \
113 result = SCM_I_MAKINUM (c_result); \
118 /* Template for fixed-size integer modification (only 8, 16 or 32-bit). */
119 #define INTEGER_SET(_len, _sign) \
120 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
121 SCM_VALIDATE_SYMBOL (3, endianness); \
124 _sign long c_value; \
125 INT_TYPE (_len, _sign) c_value_short; \
127 if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
128 scm_wrong_type_arg (FUNC_NAME, 3, value); \
130 c_value = SCM_I_INUM (value); \
131 if (SCM_UNLIKELY (!INT_VALID_P (_len, _sign) (c_value))) \
132 scm_out_of_range (FUNC_NAME, value); \
134 c_value_short = (INT_TYPE (_len, _sign)) c_value; \
135 if (!scm_is_eq (endianness, native_endianness)) \
136 c_value_short = INT_SWAP (_len) (c_value_short); \
138 memcpy (&c_bv[c_index], &c_value_short, (_len) / 8); \
141 return SCM_UNSPECIFIED;
143 /* Template for fixed-size integer modification using the native
145 #define INTEGER_NATIVE_SET(_len, _sign) \
146 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
149 _sign long c_value; \
150 INT_TYPE (_len, _sign) c_value_short; \
152 if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
153 scm_wrong_type_arg (FUNC_NAME, 3, value); \
155 c_value = SCM_I_INUM (value); \
156 if (SCM_UNLIKELY (!INT_VALID_P (_len, _sign) (c_value))) \
157 scm_out_of_range (FUNC_NAME, value); \
159 c_value_short = (INT_TYPE (_len, _sign)) c_value; \
161 memcpy (&c_bv[c_index], &c_value_short, (_len) / 8); \
164 return SCM_UNSPECIFIED;
168 /* Bytevector type. */
170 SCM_GLOBAL_SMOB (scm_tc16_r6rs_bytevector
, "r6rs-bytevector", 0);
172 #define SCM_R6RS_BYTEVECTOR_SET_LENGTH(_bv, _len) \
173 SCM_SET_SMOB_DATA ((_bv), (scm_t_bits) (_len))
174 #define SCM_R6RS_BYTEVECTOR_SET_CONTENTS(_bv, _buf) \
175 SCM_SET_SMOB_DATA_2 ((_bv), (scm_t_bits) (_buf))
177 /* The empty bytevector. */
178 SCM scm_r6rs_null_bytevector
= SCM_UNSPECIFIED
;
182 make_bytevector_from_buffer (unsigned len
, signed char *contents
)
184 /* Assuming LEN > SCM_R6RS_BYTEVECTOR_INLINE_THRESHOLD. */
185 SCM_RETURN_NEWSMOB2 (scm_tc16_r6rs_bytevector
, len
, contents
);
189 make_bytevector (unsigned len
)
193 if (SCM_UNLIKELY (len
== 0))
194 bv
= scm_r6rs_null_bytevector
;
197 signed char *contents
= NULL
;
199 if (!SCM_R6RS_BYTEVECTOR_INLINEABLE_SIZE_P (len
))
200 contents
= (signed char *) scm_gc_malloc (len
, SCM_R6RS_GC_BYTEVECTOR
);
202 bv
= make_bytevector_from_buffer (len
, contents
);
208 /* Return a new bytevector of size LEN octets. */
210 scm_r6rs_c_make_bytevector (unsigned len
)
212 return (make_bytevector (len
));
215 /* Return a bytevector of size LEN made up of CONTENTS. The area pointed to
216 by CONTENTS must have been allocated using `scm_gc_malloc ()'. */
218 scm_r6rs_c_take_bytevector (signed char *contents
, unsigned len
)
222 if (SCM_UNLIKELY (SCM_R6RS_BYTEVECTOR_INLINEABLE_SIZE_P (len
)))
224 /* Copy CONTENTS into an "in-line" buffer, then free CONTENTS. */
227 bv
= make_bytevector (len
);
228 c_bv
= SCM_R6RS_BYTEVECTOR_CONTENTS (bv
);
229 memcpy (c_bv
, contents
, len
);
230 scm_gc_free (contents
, len
, SCM_R6RS_GC_BYTEVECTOR
);
233 bv
= make_bytevector_from_buffer (len
, contents
);
238 /* Shrink BV to C_NEW_LEN (which is assumed to be smaller than its current
239 size) and return BV. */
241 scm_r6rs_i_shrink_bytevector (SCM bv
, unsigned c_new_len
)
243 if (!SCM_R6RS_BYTEVECTOR_INLINE_P (bv
))
246 signed char *c_bv
, *c_new_bv
;
248 c_len
= SCM_R6RS_BYTEVECTOR_LENGTH (bv
);
249 c_bv
= SCM_R6RS_BYTEVECTOR_CONTENTS (bv
);
251 SCM_R6RS_BYTEVECTOR_SET_LENGTH (bv
, c_new_len
);
253 if (SCM_R6RS_BYTEVECTOR_INLINEABLE_SIZE_P (c_new_len
))
255 /* Copy to the in-line buffer and free the current buffer. */
256 c_new_bv
= SCM_R6RS_BYTEVECTOR_CONTENTS (bv
);
257 memcpy (c_new_bv
, c_bv
, c_new_len
);
258 scm_gc_free (c_bv
, c_len
, SCM_R6RS_GC_BYTEVECTOR
);
262 /* Resize the existing buffer. */
263 c_new_bv
= scm_gc_realloc (c_bv
, c_len
, c_new_len
,
264 SCM_R6RS_GC_BYTEVECTOR
);
265 SCM_R6RS_BYTEVECTOR_SET_CONTENTS (bv
, c_new_bv
);
272 SCM_SMOB_PRINT (scm_tc16_r6rs_bytevector
, print_bytevector
,
278 c_len
= SCM_R6RS_BYTEVECTOR_LENGTH (bv
);
279 c_bv
= (unsigned char *) SCM_R6RS_BYTEVECTOR_CONTENTS (bv
);
281 scm_puts ("#vu8(", port
);
282 for (i
= 0; i
< c_len
; i
++)
285 scm_putc (' ', port
);
287 scm_uintprint (c_bv
[i
], 10, port
);
290 scm_putc (')', port
);
292 /* Make GCC think we use it. */
293 scm_remember_upto_here ((SCM
) pstate
);
298 SCM_SMOB_EQUALP (scm_tc16_r6rs_bytevector
, bytevector_equal_p
, bv1
, bv2
)
300 return scm_r6rs_bytevector_eq_p (bv1
, bv2
);
303 SCM_SMOB_FREE (scm_tc16_r6rs_bytevector
, free_bytevector
, bv
)
306 if (!SCM_R6RS_BYTEVECTOR_INLINE_P (bv
))
311 c_bv
= SCM_R6RS_BYTEVECTOR_CONTENTS (bv
);
312 c_len
= SCM_R6RS_BYTEVECTOR_LENGTH (bv
);
314 scm_gc_free (c_bv
, c_len
, SCM_R6RS_GC_BYTEVECTOR
);
322 /* General operations. */
324 SCM_SYMBOL (scm_sym_big
, "big");
325 SCM_SYMBOL (scm_sym_little
, "little");
327 SCM scm_r6rs_endianness_big
, scm_r6rs_endianness_little
;
329 /* Host endianness (a symbol). */
330 static SCM native_endianness
= SCM_UNSPECIFIED
;
334 # define bswap_24(_x) \
335 ((((_x) & 0xff0000) >> 16) | \
336 (((_x) & 0x00ff00)) | \
337 (((_x) & 0x0000ff) << 16))
341 SCM_DEFINE (scm_r6rs_native_endianness
, "native-endianness", 0, 0, 0,
343 "Return a symbol denoting the machine's native endianness.")
345 return native_endianness
;
348 SCM_DEFINE (scm_r6rs_bytevector_p
, "bytevector?", 1, 0, 0,
350 "Return true if @var{obj} is a bytevector.")
352 return (scm_from_bool (SCM_SMOB_PREDICATE (scm_tc16_r6rs_bytevector
,
356 SCM_DEFINE (scm_r6rs_make_bytevector
, "make-bytevector", 1, 1, 0,
358 "Return a newly allocated bytevector of @var{len} bytes, "
359 "optionally filled with @var{fill}.")
360 #define FUNC_NAME s_scm_r6rs_make_bytevector
364 signed char c_fill
= '\0';
366 SCM_VALIDATE_UINT_COPY (1, len
, c_len
);
367 if (fill
!= SCM_UNDEFINED
)
371 value
= scm_to_int (fill
);
372 if (SCM_UNLIKELY ((value
< -128) || (value
> 255)))
373 scm_out_of_range (FUNC_NAME
, fill
);
374 c_fill
= (signed char) value
;
377 bv
= make_bytevector (c_len
);
378 if (fill
!= SCM_UNDEFINED
)
381 signed char *contents
;
383 contents
= SCM_R6RS_BYTEVECTOR_CONTENTS (bv
);
384 for (i
= 0; i
< c_len
; i
++)
385 contents
[i
] = c_fill
;
392 SCM_DEFINE (scm_r6rs_bytevector_length
, "bytevector-length", 1, 0, 0,
394 "Return the length (in bytes) of @var{bv}.")
395 #define FUNC_NAME s_scm_r6rs_bytevector_length
397 SCM_VALIDATE_R6RS_BYTEVECTOR (1, bv
);
399 return (scm_from_uint (SCM_R6RS_BYTEVECTOR_LENGTH (bv
)));
403 SCM_DEFINE (scm_r6rs_bytevector_eq_p
, "bytevector=?", 2, 0, 0,
405 "Return is @var{bv1} equals to @var{bv2}---i.e., if they "
406 "have the same length and contents.")
407 #define FUNC_NAME s_scm_r6rs_bytevector_eq_p
409 SCM result
= SCM_BOOL_F
;
410 unsigned c_len1
, c_len2
;
412 SCM_VALIDATE_R6RS_BYTEVECTOR (1, bv1
);
413 SCM_VALIDATE_R6RS_BYTEVECTOR (2, bv2
);
415 c_len1
= SCM_R6RS_BYTEVECTOR_LENGTH (bv1
);
416 c_len2
= SCM_R6RS_BYTEVECTOR_LENGTH (bv2
);
418 if (c_len1
== c_len2
)
420 signed char *c_bv1
, *c_bv2
;
422 c_bv1
= SCM_R6RS_BYTEVECTOR_CONTENTS (bv1
);
423 c_bv2
= SCM_R6RS_BYTEVECTOR_CONTENTS (bv2
);
425 result
= scm_from_bool (!memcmp (c_bv1
, c_bv2
, c_len1
));
432 SCM_DEFINE (scm_r6rs_bytevector_fill_x
, "bytevector-fill!", 2, 0, 0,
434 "Fill bytevector @var{bv} with @var{fill}, a byte.")
435 #define FUNC_NAME s_scm_r6rs_bytevector_fill_x
438 signed char *c_bv
, c_fill
;
440 SCM_VALIDATE_R6RS_BYTEVECTOR (1, bv
);
441 c_fill
= scm_to_int8 (fill
);
443 c_len
= SCM_R6RS_BYTEVECTOR_LENGTH (bv
);
444 c_bv
= SCM_R6RS_BYTEVECTOR_CONTENTS (bv
);
446 for (i
= 0; i
< c_len
; i
++)
449 return SCM_UNSPECIFIED
;
453 SCM_DEFINE (scm_r6rs_bytevector_copy_x
, "bytevector-copy!", 5, 0, 0,
454 (SCM source
, SCM source_start
, SCM target
, SCM target_start
,
456 "Copy @var{len} bytes from @var{source} into @var{target}, "
457 "starting reading from @var{source_start} (a positive index "
458 "within @var{source}) and start writing at "
459 "@var{target_start}.")
460 #define FUNC_NAME s_scm_r6rs_bytevector_copy_x
462 unsigned c_len
, c_source_len
, c_target_len
;
463 unsigned c_source_start
, c_target_start
;
464 signed char *c_source
, *c_target
;
466 SCM_VALIDATE_R6RS_BYTEVECTOR (1, source
);
467 SCM_VALIDATE_R6RS_BYTEVECTOR (3, target
);
469 c_len
= scm_to_uint (len
);
470 c_source_start
= scm_to_uint (source_start
);
471 c_target_start
= scm_to_uint (target_start
);
473 c_source
= SCM_R6RS_BYTEVECTOR_CONTENTS (source
);
474 c_target
= SCM_R6RS_BYTEVECTOR_CONTENTS (target
);
475 c_source_len
= SCM_R6RS_BYTEVECTOR_LENGTH (source
);
476 c_target_len
= SCM_R6RS_BYTEVECTOR_LENGTH (target
);
478 if (SCM_UNLIKELY (c_source_start
+ c_len
> c_source_len
))
479 scm_out_of_range (FUNC_NAME
, source_start
);
480 if (SCM_UNLIKELY (c_target_start
+ c_len
> c_target_len
))
481 scm_out_of_range (FUNC_NAME
, target_start
);
483 memcpy (c_target
+ c_target_start
,
484 c_source
+ c_source_start
,
487 return SCM_UNSPECIFIED
;
491 SCM_DEFINE (scm_r6rs_bytevector_copy
, "bytevector-copy", 1, 0, 0,
493 "Return a newly allocated copy of @var{bv}.")
494 #define FUNC_NAME s_scm_r6rs_bytevector_copy
498 signed char *c_bv
, *c_copy
;
500 SCM_VALIDATE_R6RS_BYTEVECTOR (1, bv
);
502 c_len
= SCM_R6RS_BYTEVECTOR_LENGTH (bv
);
503 c_bv
= SCM_R6RS_BYTEVECTOR_CONTENTS (bv
);
505 copy
= make_bytevector (c_len
);
506 c_copy
= SCM_R6RS_BYTEVECTOR_CONTENTS (copy
);
507 memcpy (c_copy
, c_bv
, c_len
);
514 /* Operations on bytes and octets. */
516 SCM_DEFINE (scm_r6rs_bytevector_u8_ref
, "bytevector-u8-ref", 2, 0, 0,
518 "Return the octet located at @var{index} in @var{bv}.")
519 #define FUNC_NAME s_scm_r6rs_bytevector_u8_ref
521 INTEGER_NATIVE_REF (8, unsigned);
525 SCM_DEFINE (scm_r6rs_bytevector_s8_ref
, "bytevector-s8-ref", 2, 0, 0,
527 "Return the byte located at @var{index} in @var{bv}.")
528 #define FUNC_NAME s_scm_r6rs_bytevector_u8_ref
530 INTEGER_NATIVE_REF (8, signed);
534 SCM_DEFINE (scm_r6rs_bytevector_u8_set_x
, "bytevector-u8-set!", 3, 0, 0,
535 (SCM bv
, SCM index
, SCM value
),
536 "Return the octet located at @var{index} in @var{bv}.")
537 #define FUNC_NAME s_scm_r6rs_bytevector_u8_set_x
539 INTEGER_NATIVE_SET (8, unsigned);
543 SCM_DEFINE (scm_r6rs_bytevector_s8_set_x
, "bytevector-s8-set!", 3, 0, 0,
544 (SCM bv
, SCM index
, SCM value
),
545 "Return the octet located at @var{index} in @var{bv}.")
546 #define FUNC_NAME s_scm_r6rs_bytevector_u8_set_x
548 INTEGER_NATIVE_SET (8, signed);
552 #undef OCTET_ACCESSOR_PROLOGUE
555 SCM_DEFINE (scm_r6rs_bytevector_to_u8_list
, "bytevector->u8-list", 1, 0, 0,
557 "Return a newly allocated list of octets containing the "
558 "contents of @var{bv}.")
559 #define FUNC_NAME s_scm_r6rs_bytevector_to_u8_list
565 SCM_VALIDATE_R6RS_BYTEVECTOR (1, bv
);
567 c_len
= SCM_R6RS_BYTEVECTOR_LENGTH (bv
);
568 c_bv
= (unsigned char *) SCM_R6RS_BYTEVECTOR_CONTENTS (bv
);
570 lst
= scm_make_list (scm_from_uint (c_len
), SCM_UNSPECIFIED
);
571 for (i
= 0, pair
= lst
;
573 i
++, pair
= SCM_CDR (pair
))
575 SCM_SETCAR (pair
, SCM_I_MAKINUM (c_bv
[i
]));
582 SCM_DEFINE (scm_r6rs_u8_list_to_bytevector
, "u8-list->bytevector", 1, 0, 0,
584 "Turn @var{lst}, a list of octets, into a bytevector.")
585 #define FUNC_NAME s_scm_r6rs_u8_list_to_bytevector
591 SCM_VALIDATE_LIST_COPYLEN (1, lst
, c_len
);
593 bv
= make_bytevector (c_len
);
594 c_bv
= (unsigned char *) SCM_R6RS_BYTEVECTOR_CONTENTS (bv
);
596 for (i
= 0; i
< c_len
; lst
= SCM_CDR (lst
), i
++)
598 item
= SCM_CAR (lst
);
600 if (SCM_LIKELY (SCM_I_INUMP (item
)))
604 c_item
= SCM_I_INUM (item
);
605 if (SCM_LIKELY ((c_item
>= 0) && (c_item
< 256)))
606 c_bv
[i
] = (unsigned char) c_item
;
617 scm_wrong_type_arg (FUNC_NAME
, 1, item
);
623 /* Compute the two's complement of VALUE (a positive integer) on SIZE octets
624 using (2^(SIZE * 8) - VALUE). */
626 twos_complement (mpz_t value
, size_t size
)
628 unsigned long bit_count
;
630 /* We expect BIT_COUNT to fit in a unsigned long thanks to the range
631 checking on SIZE performed earlier. */
632 bit_count
= (unsigned long) size
<< 3UL;
634 if (SCM_LIKELY (bit_count
< sizeof (unsigned long)))
635 mpz_ui_sub (value
, 1UL << bit_count
, value
);
641 mpz_ui_pow_ui (max
, 2, bit_count
);
642 mpz_sub (value
, max
, value
);
648 bytevector_large_ref (const char *c_bv
, size_t c_size
, int signed_p
,
653 int c_endianness
, negative_p
= 0;
657 if (scm_is_eq (endianness
, scm_sym_big
))
658 negative_p
= c_bv
[0] & 0x80;
660 negative_p
= c_bv
[c_size
- 1] & 0x80;
663 c_endianness
= scm_is_eq (endianness
, scm_sym_big
) ? 1 : -1;
666 mpz_import (c_mpz
, 1 /* 1 word */, 1 /* word order doesn't matter */,
667 c_size
/* word is C_SIZE-byte long */,
669 0 /* nails */, c_bv
);
671 if (signed_p
&& negative_p
)
673 twos_complement (c_mpz
, c_size
);
674 mpz_neg (c_mpz
, c_mpz
);
677 result
= scm_from_mpz (c_mpz
);
678 mpz_clear (c_mpz
); /* FIXME: Needed? */
684 bytevector_large_set (char *c_bv
, size_t c_size
, int signed_p
,
685 SCM value
, SCM endianness
)
688 int c_endianness
, c_sign
, err
= 0;
690 c_endianness
= scm_is_eq (endianness
, scm_sym_big
) ? 1 : -1;
693 scm_to_mpz (value
, c_mpz
);
695 c_sign
= mpz_sgn (c_mpz
);
698 if (SCM_LIKELY (signed_p
))
700 mpz_neg (c_mpz
, c_mpz
);
701 twos_complement (c_mpz
, c_size
);
712 memset (c_bv
, 0, c_size
);
715 size_t word_count
, value_size
;
717 value_size
= (mpz_sizeinbase (c_mpz
, 2) + (8 * c_size
)) / (8 * c_size
);
718 if (SCM_UNLIKELY (value_size
> c_size
))
725 mpz_export (c_bv
, &word_count
, 1 /* word order doesn't matter */,
726 c_size
, c_endianness
,
727 0 /* nails */, c_mpz
);
728 if (SCM_UNLIKELY (word_count
!= 1))
729 /* Shouldn't happen since we already checked with VALUE_SIZE. */
739 #define GENERIC_INTEGER_ACCESSOR_PROLOGUE(_sign) \
740 unsigned long c_len, c_index, c_size; \
743 SCM_VALIDATE_R6RS_BYTEVECTOR (1, bv); \
744 c_index = scm_to_ulong (index); \
745 c_size = scm_to_ulong (size); \
747 c_len = SCM_R6RS_BYTEVECTOR_LENGTH (bv); \
748 c_bv = (char *) SCM_R6RS_BYTEVECTOR_CONTENTS (bv); \
750 /* C_SIZE must have its 3 higher bits set to zero so that \
751 multiplying it by 8 yields a number that fits in an \
753 if (SCM_UNLIKELY ((c_size == 0) || (c_size >= (ULONG_MAX >> 3L)))) \
754 scm_out_of_range (FUNC_NAME, size); \
755 if (SCM_UNLIKELY (c_index + c_size > c_len)) \
756 scm_out_of_range (FUNC_NAME, index);
759 /* Template of an integer reference function. */
760 #define GENERIC_INTEGER_REF(_sign) \
768 swap = !scm_is_eq (endianness, native_endianness); \
773 _sign char c_value8; \
774 memcpy (&c_value8, c_bv, 1); \
780 INT_TYPE (16, _sign) c_value16; \
781 memcpy (&c_value16, c_bv, 2); \
783 value = (INT_TYPE (16, _sign)) bswap_16 (c_value16); \
792 result = SCM_I_MAKINUM ((_sign int) value); \
795 result = bytevector_large_ref ((char *) c_bv, \
796 c_size, SIGNEDNESS (_sign), \
802 bytevector_signed_ref (const char *c_bv
, size_t c_size
, SCM endianness
)
804 GENERIC_INTEGER_REF (signed);
808 bytevector_unsigned_ref (const char *c_bv
, size_t c_size
, SCM endianness
)
810 GENERIC_INTEGER_REF (unsigned);
814 /* Template of an integer assignment function. */
815 #define GENERIC_INTEGER_SET(_sign) \
820 if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
823 c_value = SCM_I_INUM (value); \
827 if (SCM_LIKELY (INT_VALID_P (8, _sign) (c_value))) \
829 _sign char c_value8; \
830 c_value8 = (_sign char) c_value; \
831 memcpy (c_bv, &c_value8, 1); \
838 if (SCM_LIKELY (INT_VALID_P (16, _sign) (c_value))) \
841 INT_TYPE (16, _sign) c_value16; \
843 swap = !scm_is_eq (endianness, native_endianness); \
846 c_value16 = (INT_TYPE (16, _sign)) bswap_16 (c_value); \
848 c_value16 = c_value; \
850 memcpy (c_bv, &c_value16, 2); \
864 err = bytevector_large_set (c_bv, c_size, \
865 SIGNEDNESS (_sign), \
866 value, endianness); \
874 scm_out_of_range (FUNC_NAME, value); \
878 bytevector_signed_set (char *c_bv
, size_t c_size
,
879 SCM value
, SCM endianness
,
880 const char *func_name
)
881 #define FUNC_NAME func_name
883 GENERIC_INTEGER_SET (signed);
888 bytevector_unsigned_set (char *c_bv
, size_t c_size
,
889 SCM value
, SCM endianness
,
890 const char *func_name
)
891 #define FUNC_NAME func_name
893 GENERIC_INTEGER_SET (unsigned);
897 #undef GENERIC_INTEGER_SET
898 #undef GENERIC_INTEGER_REF
901 SCM_DEFINE (scm_r6rs_bytevector_uint_ref
, "bytevector-uint-ref", 4, 0, 0,
902 (SCM bv
, SCM index
, SCM endianness
, SCM size
),
903 "Return the @var{size}-octet long unsigned integer at index "
904 "@var{index} in @var{bv}.")
905 #define FUNC_NAME s_scm_r6rs_bytevector_uint_ref
907 GENERIC_INTEGER_ACCESSOR_PROLOGUE (unsigned);
909 return (bytevector_unsigned_ref (&c_bv
[c_index
], c_size
, endianness
));
913 SCM_DEFINE (scm_r6rs_bytevector_sint_ref
, "bytevector-sint-ref", 4, 0, 0,
914 (SCM bv
, SCM index
, SCM endianness
, SCM size
),
915 "Return the @var{size}-octet long unsigned integer at index "
916 "@var{index} in @var{bv}.")
917 #define FUNC_NAME s_scm_r6rs_bytevector_sint_ref
919 GENERIC_INTEGER_ACCESSOR_PROLOGUE (signed);
921 return (bytevector_signed_ref (&c_bv
[c_index
], c_size
, endianness
));
925 SCM_DEFINE (scm_r6rs_bytevector_uint_set_x
, "bytevector-uint-set!", 5, 0, 0,
926 (SCM bv
, SCM index
, SCM value
, SCM endianness
, SCM size
),
927 "Set the @var{size}-octet long unsigned integer at @var{index} "
929 #define FUNC_NAME s_scm_r6rs_bytevector_uint_set_x
931 GENERIC_INTEGER_ACCESSOR_PROLOGUE (unsigned);
933 bytevector_unsigned_set (&c_bv
[c_index
], c_size
, value
, endianness
,
936 return SCM_UNSPECIFIED
;
940 SCM_DEFINE (scm_r6rs_bytevector_sint_set_x
, "bytevector-sint-set!", 5, 0, 0,
941 (SCM bv
, SCM index
, SCM value
, SCM endianness
, SCM size
),
942 "Set the @var{size}-octet long signed integer at @var{index} "
944 #define FUNC_NAME s_scm_r6rs_bytevector_sint_set_x
946 GENERIC_INTEGER_ACCESSOR_PROLOGUE (signed);
948 bytevector_signed_set (&c_bv
[c_index
], c_size
, value
, endianness
,
951 return SCM_UNSPECIFIED
;
957 /* Operations on integers of arbitrary size. */
959 #define INTEGERS_TO_LIST(_sign) \
961 size_t i, c_len, c_size; \
963 SCM_VALIDATE_R6RS_BYTEVECTOR (1, bv); \
964 SCM_VALIDATE_SYMBOL (2, endianness); \
965 c_size = scm_to_uint (size); \
967 c_len = SCM_R6RS_BYTEVECTOR_LENGTH (bv); \
968 if (SCM_UNLIKELY (c_len == 0)) \
970 else if (SCM_UNLIKELY (c_len < c_size)) \
971 scm_out_of_range (FUNC_NAME, size); \
976 c_bv = (char *) SCM_R6RS_BYTEVECTOR_CONTENTS (bv); \
978 lst = scm_make_list (scm_from_uint (c_len / c_size), \
980 for (i = 0, pair = lst; \
981 i <= c_len - c_size; \
982 i += c_size, c_bv += c_size, pair = SCM_CDR (pair)) \
985 bytevector_ ## _sign ## _ref (c_bv, c_size, \
992 SCM_DEFINE (scm_r6rs_bytevector_to_sint_list
, "bytevector->sint-list",
994 (SCM bv
, SCM endianness
, SCM size
),
995 "Return a list of signed integers of @var{size} octets "
996 "representing the contents of @var{bv}.")
997 #define FUNC_NAME s_scm_r6rs_bytevector_to_sint_list
999 INTEGERS_TO_LIST (signed);
1003 SCM_DEFINE (scm_r6rs_bytevector_to_uint_list
, "bytevector->uint-list",
1005 (SCM bv
, SCM endianness
, SCM size
),
1006 "Return a list of unsigned integers of @var{size} octets "
1007 "representing the contents of @var{bv}.")
1008 #define FUNC_NAME s_scm_r6rs_bytevector_to_uint_list
1010 INTEGERS_TO_LIST (unsigned);
1014 #undef INTEGER_TO_LIST
1017 #define INTEGER_LIST_TO_BYTEVECTOR(_sign) \
1021 char *c_bv, *c_bv_ptr; \
1023 SCM_VALIDATE_LIST_COPYLEN (1, lst, c_len); \
1024 SCM_VALIDATE_SYMBOL (2, endianness); \
1025 c_size = scm_to_uint (size); \
1027 if (SCM_UNLIKELY ((c_size == 0) || (c_size >= (ULONG_MAX >> 3L)))) \
1028 scm_out_of_range (FUNC_NAME, size); \
1030 bv = make_bytevector (c_len * c_size); \
1031 c_bv = (char *) SCM_R6RS_BYTEVECTOR_CONTENTS (bv); \
1033 for (c_bv_ptr = c_bv; \
1034 !scm_is_null (lst); \
1035 lst = SCM_CDR (lst), c_bv_ptr += c_size) \
1037 bytevector_ ## _sign ## _set (c_bv_ptr, c_size, \
1038 SCM_CAR (lst), endianness, \
1045 SCM_DEFINE (scm_r6rs_uint_list_to_bytevector
, "uint-list->bytevector",
1047 (SCM lst
, SCM endianness
, SCM size
),
1048 "Return a bytevector containing the unsigned integers "
1049 "listed in @var{lst} and encoded on @var{size} octets "
1050 "according to @var{endianness}.")
1051 #define FUNC_NAME s_scm_r6rs_uint_list_to_bytevector
1053 INTEGER_LIST_TO_BYTEVECTOR (unsigned);
1057 SCM_DEFINE (scm_r6rs_sint_list_to_bytevector
, "sint-list->bytevector",
1059 (SCM lst
, SCM endianness
, SCM size
),
1060 "Return a bytevector containing the signed integers "
1061 "listed in @var{lst} and encoded on @var{size} octets "
1062 "according to @var{endianness}.")
1063 #define FUNC_NAME s_scm_r6rs_sint_list_to_bytevector
1065 INTEGER_LIST_TO_BYTEVECTOR (signed);
1069 #undef INTEGER_LIST_TO_BYTEVECTOR
1073 /* Operations on 16-bit integers. */
1075 SCM_DEFINE (scm_r6rs_bytevector_u16_ref
, "bytevector-u16-ref",
1077 (SCM bv
, SCM index
, SCM endianness
),
1078 "Return the unsigned 16-bit integer from @var{bv} at "
1080 #define FUNC_NAME s_scm_r6rs_bytevector_u16_ref
1082 INTEGER_REF (16, unsigned);
1086 SCM_DEFINE (scm_r6rs_bytevector_s16_ref
, "bytevector-s16-ref",
1088 (SCM bv
, SCM index
, SCM endianness
),
1089 "Return the signed 16-bit integer from @var{bv} at "
1091 #define FUNC_NAME s_scm_r6rs_bytevector_s16_ref
1093 INTEGER_REF (16, signed);
1097 SCM_DEFINE (scm_r6rs_bytevector_u16_native_ref
, "bytevector-u16-native-ref",
1099 (SCM bv
, SCM index
),
1100 "Return the unsigned 16-bit integer from @var{bv} at "
1101 "@var{index} using the native endianness.")
1102 #define FUNC_NAME s_scm_r6rs_bytevector_u16_native_ref
1104 INTEGER_NATIVE_REF (16, unsigned);
1108 SCM_DEFINE (scm_r6rs_bytevector_s16_native_ref
, "bytevector-s16-native-ref",
1110 (SCM bv
, SCM index
),
1111 "Return the unsigned 16-bit integer from @var{bv} at "
1112 "@var{index} using the native endianness.")
1113 #define FUNC_NAME s_scm_r6rs_bytevector_u16_native_ref
1115 INTEGER_NATIVE_REF (16, signed);
1119 SCM_DEFINE (scm_r6rs_bytevector_u16_set_x
, "bytevector-u16-set!",
1121 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1122 "Store @var{value} in @var{bv} at @var{index} according to "
1123 "@var{endianness}.")
1124 #define FUNC_NAME s_scm_r6rs_bytevector_u16_set_x
1126 INTEGER_SET (16, unsigned);
1130 SCM_DEFINE (scm_r6rs_bytevector_s16_set_x
, "bytevector-s16-set!",
1132 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1133 "Store @var{value} in @var{bv} at @var{index} according to "
1134 "@var{endianness}.")
1135 #define FUNC_NAME s_scm_r6rs_bytevector_s16_set_x
1137 INTEGER_SET (16, signed);
1141 SCM_DEFINE (scm_r6rs_bytevector_u16_native_set_x
, "bytevector-u16-native-set!",
1143 (SCM bv
, SCM index
, SCM value
),
1144 "Store the unsigned integer @var{value} at index @var{index} "
1145 "of @var{bv} using the native endianness.")
1146 #define FUNC_NAME s_scm_r6rs_bytevector_u16_native_set_x
1148 INTEGER_NATIVE_SET (16, unsigned);
1152 SCM_DEFINE (scm_r6rs_bytevector_s16_native_set_x
, "bytevector-s16-native-set!",
1154 (SCM bv
, SCM index
, SCM value
),
1155 "Store the signed integer @var{value} at index @var{index} "
1156 "of @var{bv} using the native endianness.")
1157 #define FUNC_NAME s_scm_r6rs_bytevector_u16_native_set_x
1159 INTEGER_NATIVE_SET (16, signed);
1165 /* Operations on 32-bit integers. */
1167 /* Unfortunately, on 32-bit machines `SCM' is not large enough to hold
1168 arbitrary 32-bit integers. Thus we fall back to using the
1169 `large_{ref,set}' variants on 32-bit machines. */
1171 #define LARGE_INTEGER_REF(_len, _sign) \
1172 INTEGER_ACCESSOR_PROLOGUE(_len, _sign); \
1173 SCM_VALIDATE_SYMBOL (3, endianness); \
1175 return (bytevector_large_ref ((char *) c_bv + c_index, _len / 8, \
1176 SIGNEDNESS (_sign), endianness));
1178 #define LARGE_INTEGER_SET(_len, _sign) \
1180 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
1181 SCM_VALIDATE_SYMBOL (4, endianness); \
1183 err = bytevector_large_set ((char *) c_bv + c_index, _len / 8, \
1184 SIGNEDNESS (_sign), value, endianness); \
1185 if (SCM_UNLIKELY (err)) \
1186 scm_out_of_range (FUNC_NAME, value); \
1188 return SCM_UNSPECIFIED;
1190 #define LARGE_INTEGER_NATIVE_REF(_len, _sign) \
1191 INTEGER_ACCESSOR_PROLOGUE(_len, _sign); \
1192 return (bytevector_large_ref ((char *) c_bv + c_index, _len / 8, \
1193 SIGNEDNESS (_sign), native_endianness));
1195 #define LARGE_INTEGER_NATIVE_SET(_len, _sign) \
1197 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
1199 err = bytevector_large_set ((char *) c_bv + c_index, _len / 8, \
1200 SIGNEDNESS (_sign), value, \
1201 native_endianness); \
1202 if (SCM_UNLIKELY (err)) \
1203 scm_out_of_range (FUNC_NAME, value); \
1205 return SCM_UNSPECIFIED;
1208 SCM_DEFINE (scm_r6rs_bytevector_u32_ref
, "bytevector-u32-ref",
1210 (SCM bv
, SCM index
, SCM endianness
),
1211 "Return the unsigned 32-bit integer from @var{bv} at "
1213 #define FUNC_NAME s_scm_r6rs_bytevector_u32_ref
1215 #if SIZEOF_VOID_P > 4
1216 INTEGER_REF (32, unsigned);
1218 LARGE_INTEGER_REF (32, unsigned);
1223 SCM_DEFINE (scm_r6rs_bytevector_s32_ref
, "bytevector-s32-ref",
1225 (SCM bv
, SCM index
, SCM endianness
),
1226 "Return the signed 32-bit integer from @var{bv} at "
1228 #define FUNC_NAME s_scm_r6rs_bytevector_s32_ref
1230 #if SIZEOF_VOID_P > 4
1231 INTEGER_REF (32, signed);
1233 LARGE_INTEGER_REF (32, signed);
1238 SCM_DEFINE (scm_r6rs_bytevector_u32_native_ref
, "bytevector-u32-native-ref",
1240 (SCM bv
, SCM index
),
1241 "Return the unsigned 32-bit integer from @var{bv} at "
1242 "@var{index} using the native endianness.")
1243 #define FUNC_NAME s_scm_r6rs_bytevector_u32_native_ref
1245 #if SIZEOF_VOID_P > 4
1246 INTEGER_NATIVE_REF (32, unsigned);
1248 LARGE_INTEGER_NATIVE_REF (32, unsigned);
1253 SCM_DEFINE (scm_r6rs_bytevector_s32_native_ref
, "bytevector-s32-native-ref",
1255 (SCM bv
, SCM index
),
1256 "Return the unsigned 32-bit integer from @var{bv} at "
1257 "@var{index} using the native endianness.")
1258 #define FUNC_NAME s_scm_r6rs_bytevector_u32_native_ref
1260 #if SIZEOF_VOID_P > 4
1261 INTEGER_NATIVE_REF (32, signed);
1263 LARGE_INTEGER_NATIVE_REF (32, signed);
1268 SCM_DEFINE (scm_r6rs_bytevector_u32_set_x
, "bytevector-u32-set!",
1270 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1271 "Store @var{value} in @var{bv} at @var{index} according to "
1272 "@var{endianness}.")
1273 #define FUNC_NAME s_scm_r6rs_bytevector_u32_set_x
1275 #if SIZEOF_VOID_P > 4
1276 INTEGER_SET (32, unsigned);
1278 LARGE_INTEGER_SET (32, unsigned);
1283 SCM_DEFINE (scm_r6rs_bytevector_s32_set_x
, "bytevector-s32-set!",
1285 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1286 "Store @var{value} in @var{bv} at @var{index} according to "
1287 "@var{endianness}.")
1288 #define FUNC_NAME s_scm_r6rs_bytevector_s32_set_x
1290 #if SIZEOF_VOID_P > 4
1291 INTEGER_SET (32, signed);
1293 LARGE_INTEGER_SET (32, signed);
1298 SCM_DEFINE (scm_r6rs_bytevector_u32_native_set_x
, "bytevector-u32-native-set!",
1300 (SCM bv
, SCM index
, SCM value
),
1301 "Store the unsigned integer @var{value} at index @var{index} "
1302 "of @var{bv} using the native endianness.")
1303 #define FUNC_NAME s_scm_r6rs_bytevector_u32_native_set_x
1305 #if SIZEOF_VOID_P > 4
1306 INTEGER_NATIVE_SET (32, unsigned);
1308 LARGE_INTEGER_NATIVE_SET (32, unsigned);
1313 SCM_DEFINE (scm_r6rs_bytevector_s32_native_set_x
, "bytevector-s32-native-set!",
1315 (SCM bv
, SCM index
, SCM value
),
1316 "Store the signed integer @var{value} at index @var{index} "
1317 "of @var{bv} using the native endianness.")
1318 #define FUNC_NAME s_scm_r6rs_bytevector_u32_native_set_x
1320 #if SIZEOF_VOID_P > 4
1321 INTEGER_NATIVE_SET (32, signed);
1323 LARGE_INTEGER_NATIVE_SET (32, signed);
1330 /* Operations on 64-bit integers. */
1332 /* For 64-bit integers, we use only the `large_{ref,set}' variant. */
1334 SCM_DEFINE (scm_r6rs_bytevector_u64_ref
, "bytevector-u64-ref",
1336 (SCM bv
, SCM index
, SCM endianness
),
1337 "Return the unsigned 64-bit integer from @var{bv} at "
1339 #define FUNC_NAME s_scm_r6rs_bytevector_u64_ref
1341 LARGE_INTEGER_REF (64, unsigned);
1345 SCM_DEFINE (scm_r6rs_bytevector_s64_ref
, "bytevector-s64-ref",
1347 (SCM bv
, SCM index
, SCM endianness
),
1348 "Return the signed 64-bit integer from @var{bv} at "
1350 #define FUNC_NAME s_scm_r6rs_bytevector_s64_ref
1352 LARGE_INTEGER_REF (64, signed);
1356 SCM_DEFINE (scm_r6rs_bytevector_u64_native_ref
, "bytevector-u64-native-ref",
1358 (SCM bv
, SCM index
),
1359 "Return the unsigned 64-bit integer from @var{bv} at "
1360 "@var{index} using the native endianness.")
1361 #define FUNC_NAME s_scm_r6rs_bytevector_u64_native_ref
1363 LARGE_INTEGER_NATIVE_REF (64, unsigned);
1367 SCM_DEFINE (scm_r6rs_bytevector_s64_native_ref
, "bytevector-s64-native-ref",
1369 (SCM bv
, SCM index
),
1370 "Return the unsigned 64-bit integer from @var{bv} at "
1371 "@var{index} using the native endianness.")
1372 #define FUNC_NAME s_scm_r6rs_bytevector_u64_native_ref
1374 LARGE_INTEGER_NATIVE_REF (64, signed);
1378 SCM_DEFINE (scm_r6rs_bytevector_u64_set_x
, "bytevector-u64-set!",
1380 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1381 "Store @var{value} in @var{bv} at @var{index} according to "
1382 "@var{endianness}.")
1383 #define FUNC_NAME s_scm_r6rs_bytevector_u64_set_x
1385 LARGE_INTEGER_SET (64, unsigned);
1389 SCM_DEFINE (scm_r6rs_bytevector_s64_set_x
, "bytevector-s64-set!",
1391 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1392 "Store @var{value} in @var{bv} at @var{index} according to "
1393 "@var{endianness}.")
1394 #define FUNC_NAME s_scm_r6rs_bytevector_s64_set_x
1396 LARGE_INTEGER_SET (64, signed);
1400 SCM_DEFINE (scm_r6rs_bytevector_u64_native_set_x
, "bytevector-u64-native-set!",
1402 (SCM bv
, SCM index
, SCM value
),
1403 "Store the unsigned integer @var{value} at index @var{index} "
1404 "of @var{bv} using the native endianness.")
1405 #define FUNC_NAME s_scm_r6rs_bytevector_u64_native_set_x
1407 LARGE_INTEGER_NATIVE_SET (64, unsigned);
1411 SCM_DEFINE (scm_r6rs_bytevector_s64_native_set_x
, "bytevector-s64-native-set!",
1413 (SCM bv
, SCM index
, SCM value
),
1414 "Store the signed integer @var{value} at index @var{index} "
1415 "of @var{bv} using the native endianness.")
1416 #define FUNC_NAME s_scm_r6rs_bytevector_u64_native_set_x
1418 LARGE_INTEGER_NATIVE_SET (64, signed);
1424 /* Operations on IEEE-754 numbers. */
1426 /* There are two possible word endians, visible in glibc's <ieee754.h>.
1427 However, in R6RS, when the endianness is `little', little endian is
1428 assumed for both the byte order and the word order. This is clear from
1429 Section 2.1 of R6RS-lib (in response to
1430 http://www.r6rs.org/formal-comments/comment-187.txt). */
1433 /* Convert to/from a floating-point number with different endianness. This
1434 method is probably not the most efficient but it should be portable. */
1437 float_to_foreign_endianness (union scm_r6rs_ieee754_float
*target
,
1440 union scm_r6rs_ieee754_float src
;
1444 #ifdef WORDS_BIGENDIAN
1445 /* Assuming little endian for both byte and word order. */
1446 target
->little_endian
.negative
= src
.big_endian
.negative
;
1447 target
->little_endian
.exponent
= src
.big_endian
.exponent
;
1448 target
->little_endian
.mantissa
= src
.big_endian
.mantissa
;
1450 target
->big_endian
.negative
= src
.little_endian
.negative
;
1451 target
->big_endian
.exponent
= src
.little_endian
.exponent
;
1452 target
->big_endian
.mantissa
= src
.little_endian
.mantissa
;
1457 float_from_foreign_endianness (const union scm_r6rs_ieee754_float
*source
)
1459 union scm_r6rs_ieee754_float result
;
1461 #ifdef WORDS_BIGENDIAN
1462 /* Assuming little endian for both byte and word order. */
1463 result
.big_endian
.negative
= source
->little_endian
.negative
;
1464 result
.big_endian
.exponent
= source
->little_endian
.exponent
;
1465 result
.big_endian
.mantissa
= source
->little_endian
.mantissa
;
1467 result
.little_endian
.negative
= source
->big_endian
.negative
;
1468 result
.little_endian
.exponent
= source
->big_endian
.exponent
;
1469 result
.little_endian
.mantissa
= source
->big_endian
.mantissa
;
1476 double_to_foreign_endianness (union scm_r6rs_ieee754_double
*target
,
1479 union scm_r6rs_ieee754_double src
;
1483 #ifdef WORDS_BIGENDIAN
1484 /* Assuming little endian for both byte and word order. */
1485 target
->little_little_endian
.negative
= src
.big_endian
.negative
;
1486 target
->little_little_endian
.exponent
= src
.big_endian
.exponent
;
1487 target
->little_little_endian
.mantissa0
= src
.big_endian
.mantissa0
;
1488 target
->little_little_endian
.mantissa1
= src
.big_endian
.mantissa1
;
1490 target
->big_endian
.negative
= src
.little_little_endian
.negative
;
1491 target
->big_endian
.exponent
= src
.little_little_endian
.exponent
;
1492 target
->big_endian
.mantissa0
= src
.little_little_endian
.mantissa0
;
1493 target
->big_endian
.mantissa1
= src
.little_little_endian
.mantissa1
;
1497 static inline double
1498 double_from_foreign_endianness (const union scm_r6rs_ieee754_double
*source
)
1500 union scm_r6rs_ieee754_double result
;
1502 #ifdef WORDS_BIGENDIAN
1503 /* Assuming little endian for both byte and word order. */
1504 result
.big_endian
.negative
= source
->little_little_endian
.negative
;
1505 result
.big_endian
.exponent
= source
->little_little_endian
.exponent
;
1506 result
.big_endian
.mantissa0
= source
->little_little_endian
.mantissa0
;
1507 result
.big_endian
.mantissa1
= source
->little_little_endian
.mantissa1
;
1509 result
.little_little_endian
.negative
= source
->big_endian
.negative
;
1510 result
.little_little_endian
.exponent
= source
->big_endian
.exponent
;
1511 result
.little_little_endian
.mantissa0
= source
->big_endian
.mantissa0
;
1512 result
.little_little_endian
.mantissa1
= source
->big_endian
.mantissa1
;
1518 /* Template macros to abstract over doubles and floats.
1519 XXX: Guile can only convert to/from doubles. */
1520 #define IEEE754_UNION(_c_type) union scm_r6rs_ieee754_ ## _c_type
1521 #define IEEE754_TO_SCM(_c_type) scm_from_double
1522 #define IEEE754_FROM_SCM(_c_type) scm_to_double
1523 #define IEEE754_FROM_FOREIGN_ENDIANNESS(_c_type) \
1524 _c_type ## _from_foreign_endianness
1525 #define IEEE754_TO_FOREIGN_ENDIANNESS(_c_type) \
1526 _c_type ## _to_foreign_endianness
1529 /* Templace getters and setters. */
1531 #define IEEE754_ACCESSOR_PROLOGUE(_type) \
1532 INTEGER_ACCESSOR_PROLOGUE (sizeof (_type) << 3UL, signed);
1534 #define IEEE754_REF(_type) \
1537 IEEE754_ACCESSOR_PROLOGUE (_type); \
1538 SCM_VALIDATE_SYMBOL (3, endianness); \
1540 if (scm_is_eq (endianness, native_endianness)) \
1541 memcpy (&c_result, &c_bv[c_index], sizeof (c_result)); \
1544 IEEE754_UNION (_type) c_raw; \
1546 memcpy (&c_raw, &c_bv[c_index], sizeof (c_raw)); \
1548 IEEE754_FROM_FOREIGN_ENDIANNESS (_type) (&c_raw); \
1551 return (IEEE754_TO_SCM (_type) (c_result));
1553 #define IEEE754_NATIVE_REF(_type) \
1556 IEEE754_ACCESSOR_PROLOGUE (_type); \
1558 memcpy (&c_result, &c_bv[c_index], sizeof (c_result)); \
1559 return (IEEE754_TO_SCM (_type) (c_result));
1561 #define IEEE754_SET(_type) \
1564 IEEE754_ACCESSOR_PROLOGUE (_type); \
1565 SCM_VALIDATE_REAL (3, value); \
1566 SCM_VALIDATE_SYMBOL (4, endianness); \
1567 c_value = IEEE754_FROM_SCM (_type) (value); \
1569 if (scm_is_eq (endianness, native_endianness)) \
1570 memcpy (&c_bv[c_index], &c_value, sizeof (c_value)); \
1573 IEEE754_UNION (_type) c_raw; \
1575 IEEE754_TO_FOREIGN_ENDIANNESS (_type) (&c_raw, c_value); \
1576 memcpy (&c_bv[c_index], &c_raw, sizeof (c_raw)); \
1579 return SCM_UNSPECIFIED;
1581 #define IEEE754_NATIVE_SET(_type) \
1584 IEEE754_ACCESSOR_PROLOGUE (_type); \
1585 SCM_VALIDATE_REAL (3, value); \
1586 c_value = IEEE754_FROM_SCM (_type) (value); \
1588 memcpy (&c_bv[c_index], &c_value, sizeof (c_value)); \
1589 return SCM_UNSPECIFIED;
1592 /* Single precision. */
1594 SCM_DEFINE (scm_r6rs_bytevector_ieee_single_ref
,
1595 "bytevector-ieee-single-ref",
1597 (SCM bv
, SCM index
, SCM endianness
),
1598 "Return the IEEE-754 single from @var{bv} at "
1600 #define FUNC_NAME s_scm_r6rs_bytevector_ieee_single_ref
1602 IEEE754_REF (float);
1606 SCM_DEFINE (scm_r6rs_bytevector_ieee_single_native_ref
,
1607 "bytevector-ieee-single-native-ref",
1609 (SCM bv
, SCM index
),
1610 "Return the IEEE-754 single from @var{bv} at "
1611 "@var{index} using the native endianness.")
1612 #define FUNC_NAME s_scm_r6rs_bytevector_ieee_single_native_ref
1614 IEEE754_NATIVE_REF (float);
1618 SCM_DEFINE (scm_r6rs_bytevector_ieee_single_set_x
,
1619 "bytevector-ieee-single-set!",
1621 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1622 "Store real @var{value} in @var{bv} at @var{index} according to "
1623 "@var{endianness}.")
1624 #define FUNC_NAME s_scm_r6rs_bytevector_ieee_single_set_x
1626 IEEE754_SET (float);
1630 SCM_DEFINE (scm_r6rs_bytevector_ieee_single_native_set_x
,
1631 "bytevector-ieee-single-native-set!",
1633 (SCM bv
, SCM index
, SCM value
),
1634 "Store the real @var{value} at index @var{index} "
1635 "of @var{bv} using the native endianness.")
1636 #define FUNC_NAME s_scm_r6rs_bytevector_ieee_single_native_set_x
1638 IEEE754_NATIVE_SET (float);
1643 /* Double precision. */
1645 SCM_DEFINE (scm_r6rs_bytevector_ieee_double_ref
,
1646 "bytevector-ieee-double-ref",
1648 (SCM bv
, SCM index
, SCM endianness
),
1649 "Return the IEEE-754 double from @var{bv} at "
1651 #define FUNC_NAME s_scm_r6rs_bytevector_ieee_double_ref
1653 IEEE754_REF (double);
1657 SCM_DEFINE (scm_r6rs_bytevector_ieee_double_native_ref
,
1658 "bytevector-ieee-double-native-ref",
1660 (SCM bv
, SCM index
),
1661 "Return the IEEE-754 double from @var{bv} at "
1662 "@var{index} using the native endianness.")
1663 #define FUNC_NAME s_scm_r6rs_bytevector_ieee_double_native_ref
1665 IEEE754_NATIVE_REF (double);
1669 SCM_DEFINE (scm_r6rs_bytevector_ieee_double_set_x
,
1670 "bytevector-ieee-double-set!",
1672 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1673 "Store real @var{value} in @var{bv} at @var{index} according to "
1674 "@var{endianness}.")
1675 #define FUNC_NAME s_scm_r6rs_bytevector_ieee_double_set_x
1677 IEEE754_SET (double);
1681 SCM_DEFINE (scm_r6rs_bytevector_ieee_double_native_set_x
,
1682 "bytevector-ieee-double-native-set!",
1684 (SCM bv
, SCM index
, SCM value
),
1685 "Store the real @var{value} at index @var{index} "
1686 "of @var{bv} using the native endianness.")
1687 #define FUNC_NAME s_scm_r6rs_bytevector_ieee_double_native_set_x
1689 IEEE754_NATIVE_SET (double);
1694 #undef IEEE754_UNION
1695 #undef IEEE754_TO_SCM
1696 #undef IEEE754_FROM_SCM
1697 #undef IEEE754_FROM_FOREIGN_ENDIANNESS
1698 #undef IEEE754_TO_FOREIGN_ENDIANNESS
1700 #undef IEEE754_NATIVE_REF
1702 #undef IEEE754_NATIVE_SET
1705 /* Operations on strings. */
1708 /* Produce a function that returns the length of a UTF-encoded string. */
1709 #define UTF_STRLEN_FUNCTION(_utf_width) \
1710 static inline size_t \
1711 utf ## _utf_width ## _strlen (const uint ## _utf_width ## _t *str) \
1714 const uint ## _utf_width ## _t *ptr; \
1722 return (len * ((_utf_width) / 8)); \
1725 UTF_STRLEN_FUNCTION (8)
1728 /* Return the length (in bytes) of STR, a UTF-(UTF_WIDTH) encoded string. */
1729 #define UTF_STRLEN(_utf_width, _str) \
1730 utf ## _utf_width ## _strlen (_str)
1732 /* Return the "portable" name of the UTF encoding of size UTF_WIDTH and
1733 ENDIANNESS (Gnulib's `iconv_open' module guarantees the portability of the
1736 utf_encoding_name (char *name
, size_t utf_width
, SCM endianness
)
1738 strcpy (name
, "UTF-");
1739 strcat (name
, ((utf_width
== 8)
1741 : ((utf_width
== 16)
1743 : ((utf_width
== 32)
1747 ((scm_is_eq (endianness
, scm_sym_big
))
1749 : ((scm_is_eq (endianness
, scm_sym_little
))
1754 /* Maximum length of a UTF encoding name. */
1755 #define MAX_UTF_ENCODING_NAME_LEN 16
1757 /* Produce the body of a `string->utf' function. */
1758 #define STRING_TO_UTF(_utf_width) \
1762 char c_utf_name[MAX_UTF_ENCODING_NAME_LEN]; \
1763 char *c_utf = NULL, *c_locale; \
1764 size_t c_strlen, c_raw_strlen, c_utf_len = 0; \
1766 SCM_VALIDATE_STRING (1, str); \
1767 if (endianness == SCM_UNDEFINED) \
1768 endianness = scm_sym_big; \
1770 SCM_VALIDATE_SYMBOL (2, endianness); \
1772 c_strlen = scm_c_string_length (str); \
1773 c_raw_strlen = c_strlen * ((_utf_width) / 8); \
1776 c_str = (char *) alloca (c_raw_strlen + 1); \
1777 c_raw_strlen = scm_to_locale_stringbuf (str, c_str, c_strlen); \
1779 while (c_raw_strlen > c_strlen); \
1780 c_str[c_raw_strlen] = '\0'; \
1782 utf_encoding_name (c_utf_name, (_utf_width), endianness); \
1784 c_locale = (char *) alloca (strlen (locale_charset ()) + 1); \
1785 strcpy (c_locale, locale_charset ()); \
1787 err = mem_iconveh (c_str, c_raw_strlen, \
1788 c_locale, c_utf_name, \
1789 iconveh_question_mark, NULL, \
1790 &c_utf, &c_utf_len); \
1791 if (SCM_UNLIKELY (err)) \
1792 scm_syserror_msg (FUNC_NAME, "failed to convert string: ~A", \
1793 scm_list_1 (str), err); \
1795 /* C_UTF is null-terminated. */ \
1796 utf = scm_r6rs_c_take_bytevector ((signed char *) c_utf, \
1803 SCM_DEFINE (scm_r6rs_string_to_utf8
, "string->utf8",
1806 "Return a newly allocated bytevector that contains the UTF-8 "
1807 "encoding of @var{str}.")
1808 #define FUNC_NAME s_scm_r6rs_string_to_utf8
1813 size_t c_strlen
, c_raw_strlen
;
1815 SCM_VALIDATE_STRING (1, str
);
1817 c_strlen
= scm_c_string_length (str
);
1818 c_raw_strlen
= c_strlen
;
1821 c_str
= (char *) alloca (c_raw_strlen
+ 1);
1822 c_raw_strlen
= scm_to_locale_stringbuf (str
, c_str
, c_strlen
);
1824 while (c_raw_strlen
> c_strlen
);
1825 c_str
[c_raw_strlen
] = '\0';
1827 c_utf
= u8_strconv_from_locale (c_str
);
1828 if (SCM_UNLIKELY (c_utf
== NULL
))
1829 scm_syserror (FUNC_NAME
);
1831 /* C_UTF is null-terminated. */
1832 utf
= scm_r6rs_c_take_bytevector ((signed char *) c_utf
,
1833 UTF_STRLEN (8, c_utf
));
1839 SCM_DEFINE (scm_r6rs_string_to_utf16
, "string->utf16",
1841 (SCM str
, SCM endianness
),
1842 "Return a newly allocated bytevector that contains the UTF-16 "
1843 "encoding of @var{str}.")
1844 #define FUNC_NAME s_scm_r6rs_string_to_utf16
1850 SCM_DEFINE (scm_r6rs_string_to_utf32
, "string->utf32",
1852 (SCM str
, SCM endianness
),
1853 "Return a newly allocated bytevector that contains the UTF-32 "
1854 "encoding of @var{str}.")
1855 #define FUNC_NAME s_scm_r6rs_string_to_utf32
1862 /* Produce the body of a function that converts a UTF-encoded bytevector to a
1864 #define UTF_TO_STRING(_utf_width) \
1865 SCM str = SCM_BOOL_F; \
1867 char *c_str = NULL, *c_locale; \
1868 char c_utf_name[MAX_UTF_ENCODING_NAME_LEN]; \
1869 const char *c_utf; \
1870 size_t c_strlen = 0, c_utf_len; \
1872 SCM_VALIDATE_R6RS_BYTEVECTOR (1, utf); \
1873 if (endianness == SCM_UNDEFINED) \
1874 endianness = scm_sym_big; \
1876 SCM_VALIDATE_SYMBOL (2, endianness); \
1878 c_utf_len = SCM_R6RS_BYTEVECTOR_LENGTH (utf); \
1879 c_utf = (char *) SCM_R6RS_BYTEVECTOR_CONTENTS (utf); \
1880 utf_encoding_name (c_utf_name, (_utf_width), endianness); \
1882 c_locale = (char *) alloca (strlen (locale_charset ()) + 1); \
1883 strcpy (c_locale, locale_charset ()); \
1885 err = mem_iconveh (c_utf, c_utf_len, \
1886 c_utf_name, c_locale, \
1887 iconveh_question_mark, NULL, \
1888 &c_str, &c_strlen); \
1889 if (SCM_UNLIKELY (err)) \
1890 scm_syserror_msg (FUNC_NAME, "failed to convert to string: ~A", \
1891 scm_list_1 (utf), err); \
1893 /* C_STR is null-terminated. */ \
1894 str = scm_take_locale_stringn (c_str, c_strlen); \
1899 SCM_DEFINE (scm_r6rs_utf8_to_string
, "utf8->string",
1902 "Return a newly allocate string that contains from the UTF-8-"
1903 "encoded contents of bytevector @var{utf}.")
1904 #define FUNC_NAME s_scm_r6rs_utf8_to_string
1908 char *c_str
= NULL
, *c_locale
;
1910 size_t c_utf_len
, c_strlen
= 0;
1912 SCM_VALIDATE_R6RS_BYTEVECTOR (1, utf
);
1914 c_utf_len
= SCM_R6RS_BYTEVECTOR_LENGTH (utf
);
1916 c_locale
= (char *) alloca (strlen (locale_charset ()) + 1);
1917 strcpy (c_locale
, locale_charset ());
1919 c_utf
= (char *) SCM_R6RS_BYTEVECTOR_CONTENTS (utf
);
1920 err
= mem_iconveh (c_utf
, c_utf_len
,
1922 iconveh_question_mark
, NULL
,
1924 if (SCM_UNLIKELY (err
))
1925 scm_syserror_msg (FUNC_NAME
, "failed to convert to string: ~A",
1926 scm_list_1 (utf
), err
);
1928 /* C_STR is null-terminated. */
1929 str
= scm_take_locale_stringn (c_str
, c_strlen
);
1935 SCM_DEFINE (scm_r6rs_utf16_to_string
, "utf16->string",
1937 (SCM utf
, SCM endianness
),
1938 "Return a newly allocate string that contains from the UTF-17-"
1939 "encoded contents of bytevector @var{utf}.")
1940 #define FUNC_NAME s_scm_r6rs_utf16_to_string
1946 SCM_DEFINE (scm_r6rs_utf32_to_string
, "utf32->string",
1948 (SCM utf
, SCM endianness
),
1949 "Return a newly allocate string that contains from the UTF-17-"
1950 "encoded contents of bytevector @var{utf}.")
1951 #define FUNC_NAME s_scm_r6rs_utf32_to_string
1959 /* Initialization. */
1962 scm_init_r6rs_bytevector (void)
1964 #include "bytevector.x"
1966 #ifdef WORDS_BIGENDIAN
1967 native_endianness
= scm_sym_big
;
1969 native_endianness
= scm_sym_little
;
1972 scm_r6rs_endianness_big
= scm_sym_big
;
1973 scm_r6rs_endianness_little
= scm_sym_little
;
1975 scm_r6rs_null_bytevector
=
1976 scm_gc_protect_object (make_bytevector_from_buffer (0, NULL
));