1 /* Copyright (C) 2009 Free Software Foundation, Inc.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
28 #include "libguile/_scm.h"
29 #include "libguile/extensions.h"
30 #include "libguile/bytevectors.h"
31 #include "libguile/strings.h"
32 #include "libguile/validate.h"
33 #include "libguile/ieee-754.h"
34 #include "libguile/unif.h"
35 #include "libguile/srfi-4.h"
38 #include <striconveh.h>
44 /* Assuming 32-bit longs. */
45 # define ULONG_MAX 4294967295UL
54 /* Convenience macros. These are used by the various templates (macros) that
55 are parameterized by integer signedness. */
56 #define INT8_T_signed scm_t_int8
57 #define INT8_T_unsigned scm_t_uint8
58 #define INT16_T_signed scm_t_int16
59 #define INT16_T_unsigned scm_t_uint16
60 #define INT32_T_signed scm_t_int32
61 #define INT32_T_unsigned scm_t_uint32
62 #define is_signed_int8(_x) (((_x) >= -128L) && ((_x) <= 127L))
63 #define is_unsigned_int8(_x) ((_x) <= 255UL)
64 #define is_signed_int16(_x) (((_x) >= -32768L) && ((_x) <= 32767L))
65 #define is_unsigned_int16(_x) ((_x) <= 65535UL)
66 #define is_signed_int32(_x) (((_x) >= -2147483648L) && ((_x) <= 2147483647L))
67 #define is_unsigned_int32(_x) ((_x) <= 4294967295UL)
68 #define SIGNEDNESS_signed 1
69 #define SIGNEDNESS_unsigned 0
71 #define INT_TYPE(_size, _sign) INT ## _size ## _T_ ## _sign
72 #define INT_SWAP(_size) bswap_ ## _size
73 #define INT_VALID_P(_size, _sign) is_ ## _sign ## _int ## _size
74 #define SIGNEDNESS(_sign) SIGNEDNESS_ ## _sign
77 #define INTEGER_ACCESSOR_PROLOGUE(_len, _sign) \
78 size_t c_len, c_index; \
81 SCM_VALIDATE_BYTEVECTOR (1, bv); \
82 c_index = scm_to_uint (index); \
84 c_len = SCM_BYTEVECTOR_LENGTH (bv); \
85 c_bv = (_sign char *) SCM_BYTEVECTOR_CONTENTS (bv); \
87 if (SCM_UNLIKELY (c_index + ((_len) >> 3UL) - 1 >= c_len)) \
88 scm_out_of_range (FUNC_NAME, index);
90 /* Template for fixed-size integer access (only 8, 16 or 32-bit). */
91 #define INTEGER_REF(_len, _sign) \
94 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
95 SCM_VALIDATE_SYMBOL (3, endianness); \
98 INT_TYPE (_len, _sign) c_result; \
100 memcpy (&c_result, &c_bv[c_index], (_len) / 8); \
101 if (!scm_is_eq (endianness, scm_i_native_endianness)) \
102 c_result = INT_SWAP (_len) (c_result); \
104 result = SCM_I_MAKINUM (c_result); \
109 /* Template for fixed-size integer access using the native endianness. */
110 #define INTEGER_NATIVE_REF(_len, _sign) \
113 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
116 INT_TYPE (_len, _sign) c_result; \
118 memcpy (&c_result, &c_bv[c_index], (_len) / 8); \
119 result = SCM_I_MAKINUM (c_result); \
124 /* Template for fixed-size integer modification (only 8, 16 or 32-bit). */
125 #define INTEGER_SET(_len, _sign) \
126 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
127 SCM_VALIDATE_SYMBOL (3, endianness); \
130 _sign long c_value; \
131 INT_TYPE (_len, _sign) c_value_short; \
133 if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
134 scm_wrong_type_arg (FUNC_NAME, 3, value); \
136 c_value = SCM_I_INUM (value); \
137 if (SCM_UNLIKELY (!INT_VALID_P (_len, _sign) (c_value))) \
138 scm_out_of_range (FUNC_NAME, value); \
140 c_value_short = (INT_TYPE (_len, _sign)) c_value; \
141 if (!scm_is_eq (endianness, scm_i_native_endianness)) \
142 c_value_short = INT_SWAP (_len) (c_value_short); \
144 memcpy (&c_bv[c_index], &c_value_short, (_len) / 8); \
147 return SCM_UNSPECIFIED;
149 /* Template for fixed-size integer modification using the native
151 #define INTEGER_NATIVE_SET(_len, _sign) \
152 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
155 _sign long c_value; \
156 INT_TYPE (_len, _sign) c_value_short; \
158 if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
159 scm_wrong_type_arg (FUNC_NAME, 3, value); \
161 c_value = SCM_I_INUM (value); \
162 if (SCM_UNLIKELY (!INT_VALID_P (_len, _sign) (c_value))) \
163 scm_out_of_range (FUNC_NAME, value); \
165 c_value_short = (INT_TYPE (_len, _sign)) c_value; \
167 memcpy (&c_bv[c_index], &c_value_short, (_len) / 8); \
170 return SCM_UNSPECIFIED;
174 /* Bytevector type. */
176 scm_t_bits scm_tc16_bytevector
;
178 #define SCM_BYTEVECTOR_SET_LENGTH(_bv, _len) \
179 SCM_SET_SMOB_DATA ((_bv), (scm_t_bits) (_len))
180 #define SCM_BYTEVECTOR_SET_CONTENTS(_bv, _buf) \
181 SCM_SET_SMOB_DATA_2 ((_bv), (scm_t_bits) (_buf))
183 /* The empty bytevector. */
184 SCM scm_null_bytevector
= SCM_UNSPECIFIED
;
188 make_bytevector_from_buffer (size_t len
, signed char *contents
)
190 /* Assuming LEN > SCM_BYTEVECTOR_INLINE_THRESHOLD. */
191 SCM_RETURN_NEWSMOB2 (scm_tc16_bytevector
, len
, contents
);
195 make_bytevector (size_t len
)
199 if (SCM_UNLIKELY (len
== 0))
200 bv
= scm_null_bytevector
;
203 signed char *contents
= NULL
;
205 if (!SCM_BYTEVECTOR_INLINEABLE_SIZE_P (len
))
206 contents
= (signed char *) scm_gc_malloc (len
, SCM_GC_BYTEVECTOR
);
208 bv
= make_bytevector_from_buffer (len
, contents
);
214 /* Return a new bytevector of size LEN octets. */
216 scm_c_make_bytevector (size_t len
)
218 return (make_bytevector (len
));
221 /* Return a bytevector of size LEN made up of CONTENTS. The area pointed to
222 by CONTENTS must have been allocated using `scm_gc_malloc ()'. */
224 scm_c_take_bytevector (signed char *contents
, size_t len
)
228 if (SCM_UNLIKELY (SCM_BYTEVECTOR_INLINEABLE_SIZE_P (len
)))
230 /* Copy CONTENTS into an "in-line" buffer, then free CONTENTS. */
233 bv
= make_bytevector (len
);
234 c_bv
= SCM_BYTEVECTOR_CONTENTS (bv
);
235 memcpy (c_bv
, contents
, len
);
236 scm_gc_free (contents
, len
, SCM_GC_BYTEVECTOR
);
239 bv
= make_bytevector_from_buffer (len
, contents
);
244 /* Shrink BV to C_NEW_LEN (which is assumed to be smaller than its current
245 size) and return BV. */
247 scm_i_shrink_bytevector (SCM bv
, size_t c_new_len
)
249 if (!SCM_BYTEVECTOR_INLINE_P (bv
))
252 signed char *c_bv
, *c_new_bv
;
254 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
255 c_bv
= SCM_BYTEVECTOR_CONTENTS (bv
);
257 SCM_BYTEVECTOR_SET_LENGTH (bv
, c_new_len
);
259 if (SCM_BYTEVECTOR_INLINEABLE_SIZE_P (c_new_len
))
261 /* Copy to the in-line buffer and free the current buffer. */
262 c_new_bv
= SCM_BYTEVECTOR_CONTENTS (bv
);
263 memcpy (c_new_bv
, c_bv
, c_new_len
);
264 scm_gc_free (c_bv
, c_len
, SCM_GC_BYTEVECTOR
);
268 /* Resize the existing buffer. */
269 c_new_bv
= scm_gc_realloc (c_bv
, c_len
, c_new_len
,
271 SCM_BYTEVECTOR_SET_CONTENTS (bv
, c_new_bv
);
279 scm_is_bytevector (SCM obj
)
281 return SCM_SMOB_PREDICATE (scm_tc16_bytevector
, obj
);
285 scm_c_bytevector_length (SCM bv
)
286 #define FUNC_NAME "scm_c_bytevector_length"
288 SCM_VALIDATE_BYTEVECTOR (1, bv
);
290 return SCM_BYTEVECTOR_LENGTH (bv
);
295 scm_c_bytevector_ref (SCM bv
, size_t index
)
296 #define FUNC_NAME "scm_c_bytevector_ref"
299 const scm_t_uint8
*c_bv
;
301 SCM_VALIDATE_BYTEVECTOR (1, bv
);
303 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
304 c_bv
= (scm_t_uint8
*) SCM_BYTEVECTOR_CONTENTS (bv
);
306 if (SCM_UNLIKELY (index
>= c_len
))
307 scm_out_of_range (FUNC_NAME
, scm_from_size_t (index
));
314 scm_c_bytevector_set_x (SCM bv
, size_t index
, scm_t_uint8 value
)
315 #define FUNC_NAME "scm_c_bytevector_set_x"
320 SCM_VALIDATE_BYTEVECTOR (1, bv
);
322 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
323 c_bv
= (scm_t_uint8
*) SCM_BYTEVECTOR_CONTENTS (bv
);
325 if (SCM_UNLIKELY (index
>= c_len
))
326 scm_out_of_range (FUNC_NAME
, scm_from_size_t (index
));
332 /* This procedure is used by `scm_c_generalized_vector_set_x ()'. */
334 scm_i_bytevector_generalized_set_x (SCM bv
, size_t index
, SCM value
)
335 #define FUNC_NAME "scm_i_bytevector_generalized_set_x"
337 scm_c_bytevector_set_x (bv
, index
, scm_to_uint8 (value
));
342 print_bytevector (SCM bv
, SCM port
, scm_print_state
*pstate
)
347 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
348 c_bv
= (unsigned char *) SCM_BYTEVECTOR_CONTENTS (bv
);
350 scm_puts ("#vu8(", port
);
351 for (i
= 0; i
< c_len
; i
++)
354 scm_putc (' ', port
);
356 scm_uintprint (c_bv
[i
], 10, port
);
359 scm_putc (')', port
);
361 /* Make GCC think we use it. */
362 scm_remember_upto_here ((SCM
) pstate
);
368 bytevector_equal_p (SCM bv1
, SCM bv2
)
370 return scm_bytevector_eq_p (bv1
, bv2
);
374 /* General operations. */
376 SCM_SYMBOL (scm_sym_big
, "big");
377 SCM_SYMBOL (scm_sym_little
, "little");
379 SCM scm_endianness_big
, scm_endianness_little
;
381 /* Host endianness (a symbol). */
382 SCM scm_i_native_endianness
= SCM_UNSPECIFIED
;
386 # define bswap_24(_x) \
387 ((((_x) & 0xff0000) >> 16) | \
388 (((_x) & 0x00ff00)) | \
389 (((_x) & 0x0000ff) << 16))
393 SCM_DEFINE (scm_native_endianness
, "native-endianness", 0, 0, 0,
395 "Return a symbol denoting the machine's native endianness.")
396 #define FUNC_NAME s_scm_native_endianness
398 return scm_i_native_endianness
;
402 SCM_DEFINE (scm_bytevector_p
, "bytevector?", 1, 0, 0,
404 "Return true if @var{obj} is a bytevector.")
405 #define FUNC_NAME s_scm_bytevector_p
407 return scm_from_bool (scm_is_bytevector (obj
));
411 SCM_DEFINE (scm_make_bytevector
, "make-bytevector", 1, 1, 0,
413 "Return a newly allocated bytevector of @var{len} bytes, "
414 "optionally filled with @var{fill}.")
415 #define FUNC_NAME s_scm_make_bytevector
419 signed char c_fill
= '\0';
421 SCM_VALIDATE_UINT_COPY (1, len
, c_len
);
422 if (fill
!= SCM_UNDEFINED
)
426 value
= scm_to_int (fill
);
427 if (SCM_UNLIKELY ((value
< -128) || (value
> 255)))
428 scm_out_of_range (FUNC_NAME
, fill
);
429 c_fill
= (signed char) value
;
432 bv
= make_bytevector (c_len
);
433 if (fill
!= SCM_UNDEFINED
)
436 signed char *contents
;
438 contents
= SCM_BYTEVECTOR_CONTENTS (bv
);
439 for (i
= 0; i
< c_len
; i
++)
440 contents
[i
] = c_fill
;
447 SCM_DEFINE (scm_bytevector_length
, "bytevector-length", 1, 0, 0,
449 "Return the length (in bytes) of @var{bv}.")
450 #define FUNC_NAME s_scm_bytevector_length
452 return scm_from_uint (scm_c_bytevector_length (bv
));
456 SCM_DEFINE (scm_bytevector_eq_p
, "bytevector=?", 2, 0, 0,
458 "Return is @var{bv1} equals to @var{bv2}---i.e., if they "
459 "have the same length and contents.")
460 #define FUNC_NAME s_scm_bytevector_eq_p
462 SCM result
= SCM_BOOL_F
;
463 unsigned c_len1
, c_len2
;
465 SCM_VALIDATE_BYTEVECTOR (1, bv1
);
466 SCM_VALIDATE_BYTEVECTOR (2, bv2
);
468 c_len1
= SCM_BYTEVECTOR_LENGTH (bv1
);
469 c_len2
= SCM_BYTEVECTOR_LENGTH (bv2
);
471 if (c_len1
== c_len2
)
473 signed char *c_bv1
, *c_bv2
;
475 c_bv1
= SCM_BYTEVECTOR_CONTENTS (bv1
);
476 c_bv2
= SCM_BYTEVECTOR_CONTENTS (bv2
);
478 result
= scm_from_bool (!memcmp (c_bv1
, c_bv2
, c_len1
));
485 SCM_DEFINE (scm_bytevector_fill_x
, "bytevector-fill!", 2, 0, 0,
487 "Fill bytevector @var{bv} with @var{fill}, a byte.")
488 #define FUNC_NAME s_scm_bytevector_fill_x
491 signed char *c_bv
, c_fill
;
493 SCM_VALIDATE_BYTEVECTOR (1, bv
);
494 c_fill
= scm_to_int8 (fill
);
496 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
497 c_bv
= SCM_BYTEVECTOR_CONTENTS (bv
);
499 for (i
= 0; i
< c_len
; i
++)
502 return SCM_UNSPECIFIED
;
506 SCM_DEFINE (scm_bytevector_copy_x
, "bytevector-copy!", 5, 0, 0,
507 (SCM source
, SCM source_start
, SCM target
, SCM target_start
,
509 "Copy @var{len} bytes from @var{source} into @var{target}, "
510 "starting reading from @var{source_start} (a positive index "
511 "within @var{source}) and start writing at "
512 "@var{target_start}.")
513 #define FUNC_NAME s_scm_bytevector_copy_x
515 unsigned c_len
, c_source_len
, c_target_len
;
516 unsigned c_source_start
, c_target_start
;
517 signed char *c_source
, *c_target
;
519 SCM_VALIDATE_BYTEVECTOR (1, source
);
520 SCM_VALIDATE_BYTEVECTOR (3, target
);
522 c_len
= scm_to_uint (len
);
523 c_source_start
= scm_to_uint (source_start
);
524 c_target_start
= scm_to_uint (target_start
);
526 c_source
= SCM_BYTEVECTOR_CONTENTS (source
);
527 c_target
= SCM_BYTEVECTOR_CONTENTS (target
);
528 c_source_len
= SCM_BYTEVECTOR_LENGTH (source
);
529 c_target_len
= SCM_BYTEVECTOR_LENGTH (target
);
531 if (SCM_UNLIKELY (c_source_start
+ c_len
> c_source_len
))
532 scm_out_of_range (FUNC_NAME
, source_start
);
533 if (SCM_UNLIKELY (c_target_start
+ c_len
> c_target_len
))
534 scm_out_of_range (FUNC_NAME
, target_start
);
536 memcpy (c_target
+ c_target_start
,
537 c_source
+ c_source_start
,
540 return SCM_UNSPECIFIED
;
544 SCM_DEFINE (scm_bytevector_copy
, "bytevector-copy", 1, 0, 0,
546 "Return a newly allocated copy of @var{bv}.")
547 #define FUNC_NAME s_scm_bytevector_copy
551 signed char *c_bv
, *c_copy
;
553 SCM_VALIDATE_BYTEVECTOR (1, bv
);
555 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
556 c_bv
= SCM_BYTEVECTOR_CONTENTS (bv
);
558 copy
= make_bytevector (c_len
);
559 c_copy
= SCM_BYTEVECTOR_CONTENTS (copy
);
560 memcpy (c_copy
, c_bv
, c_len
);
566 SCM_DEFINE (scm_uniform_array_to_bytevector
, "uniform-array->bytevector",
567 1, 0, 0, (SCM array
),
568 "Return a newly allocated bytevector whose contents\n"
569 "will be copied from the uniform array @var{array}.")
570 #define FUNC_NAME s_scm_uniform_array_to_bytevector
574 scm_t_array_handle h
;
578 contents
= scm_array_contents (array
, SCM_BOOL_T
);
579 if (scm_is_false (contents
))
580 scm_wrong_type_arg_msg (FUNC_NAME
, 0, array
, "uniform contiguous array");
582 scm_array_get_handle (contents
, &h
);
584 base
= scm_array_handle_uniform_elements (&h
);
585 len
= h
.dims
->inc
* (h
.dims
->ubnd
- h
.dims
->lbnd
+ 1);
586 sz
= scm_array_handle_uniform_element_size (&h
);
588 ret
= make_bytevector (len
* sz
);
589 memcpy (SCM_BYTEVECTOR_CONTENTS (ret
), base
, len
* sz
);
591 scm_array_handle_release (&h
);
598 /* Operations on bytes and octets. */
600 SCM_DEFINE (scm_bytevector_u8_ref
, "bytevector-u8-ref", 2, 0, 0,
602 "Return the octet located at @var{index} in @var{bv}.")
603 #define FUNC_NAME s_scm_bytevector_u8_ref
605 INTEGER_NATIVE_REF (8, unsigned);
609 SCM_DEFINE (scm_bytevector_s8_ref
, "bytevector-s8-ref", 2, 0, 0,
611 "Return the byte located at @var{index} in @var{bv}.")
612 #define FUNC_NAME s_scm_bytevector_s8_ref
614 INTEGER_NATIVE_REF (8, signed);
618 SCM_DEFINE (scm_bytevector_u8_set_x
, "bytevector-u8-set!", 3, 0, 0,
619 (SCM bv
, SCM index
, SCM value
),
620 "Return the octet located at @var{index} in @var{bv}.")
621 #define FUNC_NAME s_scm_bytevector_u8_set_x
623 INTEGER_NATIVE_SET (8, unsigned);
627 SCM_DEFINE (scm_bytevector_s8_set_x
, "bytevector-s8-set!", 3, 0, 0,
628 (SCM bv
, SCM index
, SCM value
),
629 "Return the octet located at @var{index} in @var{bv}.")
630 #define FUNC_NAME s_scm_bytevector_s8_set_x
632 INTEGER_NATIVE_SET (8, signed);
636 #undef OCTET_ACCESSOR_PROLOGUE
639 SCM_DEFINE (scm_bytevector_to_u8_list
, "bytevector->u8-list", 1, 0, 0,
641 "Return a newly allocated list of octets containing the "
642 "contents of @var{bv}.")
643 #define FUNC_NAME s_scm_bytevector_to_u8_list
649 SCM_VALIDATE_BYTEVECTOR (1, bv
);
651 c_len
= SCM_BYTEVECTOR_LENGTH (bv
);
652 c_bv
= (unsigned char *) SCM_BYTEVECTOR_CONTENTS (bv
);
654 lst
= scm_make_list (scm_from_uint (c_len
), SCM_UNSPECIFIED
);
655 for (i
= 0, pair
= lst
;
657 i
++, pair
= SCM_CDR (pair
))
659 SCM_SETCAR (pair
, SCM_I_MAKINUM (c_bv
[i
]));
666 SCM_DEFINE (scm_u8_list_to_bytevector
, "u8-list->bytevector", 1, 0, 0,
668 "Turn @var{lst}, a list of octets, into a bytevector.")
669 #define FUNC_NAME s_scm_u8_list_to_bytevector
675 SCM_VALIDATE_LIST_COPYLEN (1, lst
, c_len
);
677 bv
= make_bytevector (c_len
);
678 c_bv
= (unsigned char *) SCM_BYTEVECTOR_CONTENTS (bv
);
680 for (i
= 0; i
< c_len
; lst
= SCM_CDR (lst
), i
++)
682 item
= SCM_CAR (lst
);
684 if (SCM_LIKELY (SCM_I_INUMP (item
)))
688 c_item
= SCM_I_INUM (item
);
689 if (SCM_LIKELY ((c_item
>= 0) && (c_item
< 256)))
690 c_bv
[i
] = (unsigned char) c_item
;
701 scm_wrong_type_arg (FUNC_NAME
, 1, item
);
707 /* Compute the two's complement of VALUE (a positive integer) on SIZE octets
708 using (2^(SIZE * 8) - VALUE). */
710 twos_complement (mpz_t value
, size_t size
)
712 unsigned long bit_count
;
714 /* We expect BIT_COUNT to fit in a unsigned long thanks to the range
715 checking on SIZE performed earlier. */
716 bit_count
= (unsigned long) size
<< 3UL;
718 if (SCM_LIKELY (bit_count
< sizeof (unsigned long)))
719 mpz_ui_sub (value
, 1UL << bit_count
, value
);
725 mpz_ui_pow_ui (max
, 2, bit_count
);
726 mpz_sub (value
, max
, value
);
732 bytevector_large_ref (const char *c_bv
, size_t c_size
, int signed_p
,
737 int c_endianness
, negative_p
= 0;
741 if (scm_is_eq (endianness
, scm_sym_big
))
742 negative_p
= c_bv
[0] & 0x80;
744 negative_p
= c_bv
[c_size
- 1] & 0x80;
747 c_endianness
= scm_is_eq (endianness
, scm_sym_big
) ? 1 : -1;
750 mpz_import (c_mpz
, 1 /* 1 word */, 1 /* word order doesn't matter */,
751 c_size
/* word is C_SIZE-byte long */,
753 0 /* nails */, c_bv
);
755 if (signed_p
&& negative_p
)
757 twos_complement (c_mpz
, c_size
);
758 mpz_neg (c_mpz
, c_mpz
);
761 result
= scm_from_mpz (c_mpz
);
762 mpz_clear (c_mpz
); /* FIXME: Needed? */
768 bytevector_large_set (char *c_bv
, size_t c_size
, int signed_p
,
769 SCM value
, SCM endianness
)
772 int c_endianness
, c_sign
, err
= 0;
774 c_endianness
= scm_is_eq (endianness
, scm_sym_big
) ? 1 : -1;
777 scm_to_mpz (value
, c_mpz
);
779 c_sign
= mpz_sgn (c_mpz
);
782 if (SCM_LIKELY (signed_p
))
784 mpz_neg (c_mpz
, c_mpz
);
785 twos_complement (c_mpz
, c_size
);
796 memset (c_bv
, 0, c_size
);
799 size_t word_count
, value_size
;
801 value_size
= (mpz_sizeinbase (c_mpz
, 2) + (8 * c_size
)) / (8 * c_size
);
802 if (SCM_UNLIKELY (value_size
> c_size
))
809 mpz_export (c_bv
, &word_count
, 1 /* word order doesn't matter */,
810 c_size
, c_endianness
,
811 0 /* nails */, c_mpz
);
812 if (SCM_UNLIKELY (word_count
!= 1))
813 /* Shouldn't happen since we already checked with VALUE_SIZE. */
823 #define GENERIC_INTEGER_ACCESSOR_PROLOGUE(_sign) \
824 unsigned long c_len, c_index, c_size; \
827 SCM_VALIDATE_BYTEVECTOR (1, bv); \
828 c_index = scm_to_ulong (index); \
829 c_size = scm_to_ulong (size); \
831 c_len = SCM_BYTEVECTOR_LENGTH (bv); \
832 c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
834 /* C_SIZE must have its 3 higher bits set to zero so that \
835 multiplying it by 8 yields a number that fits in an \
837 if (SCM_UNLIKELY ((c_size == 0) || (c_size >= (ULONG_MAX >> 3L)))) \
838 scm_out_of_range (FUNC_NAME, size); \
839 if (SCM_UNLIKELY (c_index + c_size > c_len)) \
840 scm_out_of_range (FUNC_NAME, index);
843 /* Template of an integer reference function. */
844 #define GENERIC_INTEGER_REF(_sign) \
852 swap = !scm_is_eq (endianness, scm_i_native_endianness); \
857 _sign char c_value8; \
858 memcpy (&c_value8, c_bv, 1); \
864 INT_TYPE (16, _sign) c_value16; \
865 memcpy (&c_value16, c_bv, 2); \
867 value = (INT_TYPE (16, _sign)) bswap_16 (c_value16); \
876 result = SCM_I_MAKINUM ((_sign int) value); \
879 result = bytevector_large_ref ((char *) c_bv, \
880 c_size, SIGNEDNESS (_sign), \
886 bytevector_signed_ref (const char *c_bv
, size_t c_size
, SCM endianness
)
888 GENERIC_INTEGER_REF (signed);
892 bytevector_unsigned_ref (const char *c_bv
, size_t c_size
, SCM endianness
)
894 GENERIC_INTEGER_REF (unsigned);
898 /* Template of an integer assignment function. */
899 #define GENERIC_INTEGER_SET(_sign) \
904 if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
907 c_value = SCM_I_INUM (value); \
911 if (SCM_LIKELY (INT_VALID_P (8, _sign) (c_value))) \
913 _sign char c_value8; \
914 c_value8 = (_sign char) c_value; \
915 memcpy (c_bv, &c_value8, 1); \
922 if (SCM_LIKELY (INT_VALID_P (16, _sign) (c_value))) \
925 INT_TYPE (16, _sign) c_value16; \
927 swap = !scm_is_eq (endianness, scm_i_native_endianness); \
930 c_value16 = (INT_TYPE (16, _sign)) bswap_16 (c_value); \
932 c_value16 = c_value; \
934 memcpy (c_bv, &c_value16, 2); \
948 err = bytevector_large_set (c_bv, c_size, \
949 SIGNEDNESS (_sign), \
950 value, endianness); \
958 scm_out_of_range (FUNC_NAME, value); \
962 bytevector_signed_set (char *c_bv
, size_t c_size
,
963 SCM value
, SCM endianness
,
964 const char *func_name
)
965 #define FUNC_NAME func_name
967 GENERIC_INTEGER_SET (signed);
972 bytevector_unsigned_set (char *c_bv
, size_t c_size
,
973 SCM value
, SCM endianness
,
974 const char *func_name
)
975 #define FUNC_NAME func_name
977 GENERIC_INTEGER_SET (unsigned);
981 #undef GENERIC_INTEGER_SET
982 #undef GENERIC_INTEGER_REF
985 SCM_DEFINE (scm_bytevector_uint_ref
, "bytevector-uint-ref", 4, 0, 0,
986 (SCM bv
, SCM index
, SCM endianness
, SCM size
),
987 "Return the @var{size}-octet long unsigned integer at index "
988 "@var{index} in @var{bv}.")
989 #define FUNC_NAME s_scm_bytevector_uint_ref
991 GENERIC_INTEGER_ACCESSOR_PROLOGUE (unsigned);
993 return (bytevector_unsigned_ref (&c_bv
[c_index
], c_size
, endianness
));
997 SCM_DEFINE (scm_bytevector_sint_ref
, "bytevector-sint-ref", 4, 0, 0,
998 (SCM bv
, SCM index
, SCM endianness
, SCM size
),
999 "Return the @var{size}-octet long unsigned integer at index "
1000 "@var{index} in @var{bv}.")
1001 #define FUNC_NAME s_scm_bytevector_sint_ref
1003 GENERIC_INTEGER_ACCESSOR_PROLOGUE (signed);
1005 return (bytevector_signed_ref (&c_bv
[c_index
], c_size
, endianness
));
1009 SCM_DEFINE (scm_bytevector_uint_set_x
, "bytevector-uint-set!", 5, 0, 0,
1010 (SCM bv
, SCM index
, SCM value
, SCM endianness
, SCM size
),
1011 "Set the @var{size}-octet long unsigned integer at @var{index} "
1013 #define FUNC_NAME s_scm_bytevector_uint_set_x
1015 GENERIC_INTEGER_ACCESSOR_PROLOGUE (unsigned);
1017 bytevector_unsigned_set (&c_bv
[c_index
], c_size
, value
, endianness
,
1020 return SCM_UNSPECIFIED
;
1024 SCM_DEFINE (scm_bytevector_sint_set_x
, "bytevector-sint-set!", 5, 0, 0,
1025 (SCM bv
, SCM index
, SCM value
, SCM endianness
, SCM size
),
1026 "Set the @var{size}-octet long signed integer at @var{index} "
1028 #define FUNC_NAME s_scm_bytevector_sint_set_x
1030 GENERIC_INTEGER_ACCESSOR_PROLOGUE (signed);
1032 bytevector_signed_set (&c_bv
[c_index
], c_size
, value
, endianness
,
1035 return SCM_UNSPECIFIED
;
1041 /* Operations on integers of arbitrary size. */
1043 #define INTEGERS_TO_LIST(_sign) \
1045 size_t i, c_len, c_size; \
1047 SCM_VALIDATE_BYTEVECTOR (1, bv); \
1048 SCM_VALIDATE_SYMBOL (2, endianness); \
1049 c_size = scm_to_uint (size); \
1051 c_len = SCM_BYTEVECTOR_LENGTH (bv); \
1052 if (SCM_UNLIKELY (c_len == 0)) \
1054 else if (SCM_UNLIKELY (c_len < c_size)) \
1055 scm_out_of_range (FUNC_NAME, size); \
1060 c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
1062 lst = scm_make_list (scm_from_uint (c_len / c_size), \
1064 for (i = 0, pair = lst; \
1065 i <= c_len - c_size; \
1066 i += c_size, c_bv += c_size, pair = SCM_CDR (pair)) \
1069 bytevector_ ## _sign ## _ref (c_bv, c_size, \
1076 SCM_DEFINE (scm_bytevector_to_sint_list
, "bytevector->sint-list",
1078 (SCM bv
, SCM endianness
, SCM size
),
1079 "Return a list of signed integers of @var{size} octets "
1080 "representing the contents of @var{bv}.")
1081 #define FUNC_NAME s_scm_bytevector_to_sint_list
1083 INTEGERS_TO_LIST (signed);
1087 SCM_DEFINE (scm_bytevector_to_uint_list
, "bytevector->uint-list",
1089 (SCM bv
, SCM endianness
, SCM size
),
1090 "Return a list of unsigned integers of @var{size} octets "
1091 "representing the contents of @var{bv}.")
1092 #define FUNC_NAME s_scm_bytevector_to_uint_list
1094 INTEGERS_TO_LIST (unsigned);
1098 #undef INTEGER_TO_LIST
1101 #define INTEGER_LIST_TO_BYTEVECTOR(_sign) \
1105 char *c_bv, *c_bv_ptr; \
1107 SCM_VALIDATE_LIST_COPYLEN (1, lst, c_len); \
1108 SCM_VALIDATE_SYMBOL (2, endianness); \
1109 c_size = scm_to_uint (size); \
1111 if (SCM_UNLIKELY ((c_size == 0) || (c_size >= (ULONG_MAX >> 3L)))) \
1112 scm_out_of_range (FUNC_NAME, size); \
1114 bv = make_bytevector (c_len * c_size); \
1115 c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
1117 for (c_bv_ptr = c_bv; \
1118 !scm_is_null (lst); \
1119 lst = SCM_CDR (lst), c_bv_ptr += c_size) \
1121 bytevector_ ## _sign ## _set (c_bv_ptr, c_size, \
1122 SCM_CAR (lst), endianness, \
1129 SCM_DEFINE (scm_uint_list_to_bytevector
, "uint-list->bytevector",
1131 (SCM lst
, SCM endianness
, SCM size
),
1132 "Return a bytevector containing the unsigned integers "
1133 "listed in @var{lst} and encoded on @var{size} octets "
1134 "according to @var{endianness}.")
1135 #define FUNC_NAME s_scm_uint_list_to_bytevector
1137 INTEGER_LIST_TO_BYTEVECTOR (unsigned);
1141 SCM_DEFINE (scm_sint_list_to_bytevector
, "sint-list->bytevector",
1143 (SCM lst
, SCM endianness
, SCM size
),
1144 "Return a bytevector containing the signed integers "
1145 "listed in @var{lst} and encoded on @var{size} octets "
1146 "according to @var{endianness}.")
1147 #define FUNC_NAME s_scm_sint_list_to_bytevector
1149 INTEGER_LIST_TO_BYTEVECTOR (signed);
1153 #undef INTEGER_LIST_TO_BYTEVECTOR
1157 /* Operations on 16-bit integers. */
1159 SCM_DEFINE (scm_bytevector_u16_ref
, "bytevector-u16-ref",
1161 (SCM bv
, SCM index
, SCM endianness
),
1162 "Return the unsigned 16-bit integer from @var{bv} at "
1164 #define FUNC_NAME s_scm_bytevector_u16_ref
1166 INTEGER_REF (16, unsigned);
1170 SCM_DEFINE (scm_bytevector_s16_ref
, "bytevector-s16-ref",
1172 (SCM bv
, SCM index
, SCM endianness
),
1173 "Return the signed 16-bit integer from @var{bv} at "
1175 #define FUNC_NAME s_scm_bytevector_s16_ref
1177 INTEGER_REF (16, signed);
1181 SCM_DEFINE (scm_bytevector_u16_native_ref
, "bytevector-u16-native-ref",
1183 (SCM bv
, SCM index
),
1184 "Return the unsigned 16-bit integer from @var{bv} at "
1185 "@var{index} using the native endianness.")
1186 #define FUNC_NAME s_scm_bytevector_u16_native_ref
1188 INTEGER_NATIVE_REF (16, unsigned);
1192 SCM_DEFINE (scm_bytevector_s16_native_ref
, "bytevector-s16-native-ref",
1194 (SCM bv
, SCM index
),
1195 "Return the unsigned 16-bit integer from @var{bv} at "
1196 "@var{index} using the native endianness.")
1197 #define FUNC_NAME s_scm_bytevector_s16_native_ref
1199 INTEGER_NATIVE_REF (16, signed);
1203 SCM_DEFINE (scm_bytevector_u16_set_x
, "bytevector-u16-set!",
1205 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1206 "Store @var{value} in @var{bv} at @var{index} according to "
1207 "@var{endianness}.")
1208 #define FUNC_NAME s_scm_bytevector_u16_set_x
1210 INTEGER_SET (16, unsigned);
1214 SCM_DEFINE (scm_bytevector_s16_set_x
, "bytevector-s16-set!",
1216 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1217 "Store @var{value} in @var{bv} at @var{index} according to "
1218 "@var{endianness}.")
1219 #define FUNC_NAME s_scm_bytevector_s16_set_x
1221 INTEGER_SET (16, signed);
1225 SCM_DEFINE (scm_bytevector_u16_native_set_x
, "bytevector-u16-native-set!",
1227 (SCM bv
, SCM index
, SCM value
),
1228 "Store the unsigned integer @var{value} at index @var{index} "
1229 "of @var{bv} using the native endianness.")
1230 #define FUNC_NAME s_scm_bytevector_u16_native_set_x
1232 INTEGER_NATIVE_SET (16, unsigned);
1236 SCM_DEFINE (scm_bytevector_s16_native_set_x
, "bytevector-s16-native-set!",
1238 (SCM bv
, SCM index
, SCM value
),
1239 "Store the signed integer @var{value} at index @var{index} "
1240 "of @var{bv} using the native endianness.")
1241 #define FUNC_NAME s_scm_bytevector_s16_native_set_x
1243 INTEGER_NATIVE_SET (16, signed);
1249 /* Operations on 32-bit integers. */
1251 /* Unfortunately, on 32-bit machines `SCM' is not large enough to hold
1252 arbitrary 32-bit integers. Thus we fall back to using the
1253 `large_{ref,set}' variants on 32-bit machines. */
1255 #define LARGE_INTEGER_REF(_len, _sign) \
1256 INTEGER_ACCESSOR_PROLOGUE(_len, _sign); \
1257 SCM_VALIDATE_SYMBOL (3, endianness); \
1259 return (bytevector_large_ref ((char *) c_bv + c_index, _len / 8, \
1260 SIGNEDNESS (_sign), endianness));
1262 #define LARGE_INTEGER_SET(_len, _sign) \
1264 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
1265 SCM_VALIDATE_SYMBOL (4, endianness); \
1267 err = bytevector_large_set ((char *) c_bv + c_index, _len / 8, \
1268 SIGNEDNESS (_sign), value, endianness); \
1269 if (SCM_UNLIKELY (err)) \
1270 scm_out_of_range (FUNC_NAME, value); \
1272 return SCM_UNSPECIFIED;
1274 #define LARGE_INTEGER_NATIVE_REF(_len, _sign) \
1275 INTEGER_ACCESSOR_PROLOGUE(_len, _sign); \
1276 return (bytevector_large_ref ((char *) c_bv + c_index, _len / 8, \
1277 SIGNEDNESS (_sign), scm_i_native_endianness));
1279 #define LARGE_INTEGER_NATIVE_SET(_len, _sign) \
1281 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
1283 err = bytevector_large_set ((char *) c_bv + c_index, _len / 8, \
1284 SIGNEDNESS (_sign), value, \
1285 scm_i_native_endianness); \
1286 if (SCM_UNLIKELY (err)) \
1287 scm_out_of_range (FUNC_NAME, value); \
1289 return SCM_UNSPECIFIED;
1292 SCM_DEFINE (scm_bytevector_u32_ref
, "bytevector-u32-ref",
1294 (SCM bv
, SCM index
, SCM endianness
),
1295 "Return the unsigned 32-bit integer from @var{bv} at "
1297 #define FUNC_NAME s_scm_bytevector_u32_ref
1299 #if SIZEOF_VOID_P > 4
1300 INTEGER_REF (32, unsigned);
1302 LARGE_INTEGER_REF (32, unsigned);
1307 SCM_DEFINE (scm_bytevector_s32_ref
, "bytevector-s32-ref",
1309 (SCM bv
, SCM index
, SCM endianness
),
1310 "Return the signed 32-bit integer from @var{bv} at "
1312 #define FUNC_NAME s_scm_bytevector_s32_ref
1314 #if SIZEOF_VOID_P > 4
1315 INTEGER_REF (32, signed);
1317 LARGE_INTEGER_REF (32, signed);
1322 SCM_DEFINE (scm_bytevector_u32_native_ref
, "bytevector-u32-native-ref",
1324 (SCM bv
, SCM index
),
1325 "Return the unsigned 32-bit integer from @var{bv} at "
1326 "@var{index} using the native endianness.")
1327 #define FUNC_NAME s_scm_bytevector_u32_native_ref
1329 #if SIZEOF_VOID_P > 4
1330 INTEGER_NATIVE_REF (32, unsigned);
1332 LARGE_INTEGER_NATIVE_REF (32, unsigned);
1337 SCM_DEFINE (scm_bytevector_s32_native_ref
, "bytevector-s32-native-ref",
1339 (SCM bv
, SCM index
),
1340 "Return the unsigned 32-bit integer from @var{bv} at "
1341 "@var{index} using the native endianness.")
1342 #define FUNC_NAME s_scm_bytevector_s32_native_ref
1344 #if SIZEOF_VOID_P > 4
1345 INTEGER_NATIVE_REF (32, signed);
1347 LARGE_INTEGER_NATIVE_REF (32, signed);
1352 SCM_DEFINE (scm_bytevector_u32_set_x
, "bytevector-u32-set!",
1354 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1355 "Store @var{value} in @var{bv} at @var{index} according to "
1356 "@var{endianness}.")
1357 #define FUNC_NAME s_scm_bytevector_u32_set_x
1359 #if SIZEOF_VOID_P > 4
1360 INTEGER_SET (32, unsigned);
1362 LARGE_INTEGER_SET (32, unsigned);
1367 SCM_DEFINE (scm_bytevector_s32_set_x
, "bytevector-s32-set!",
1369 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1370 "Store @var{value} in @var{bv} at @var{index} according to "
1371 "@var{endianness}.")
1372 #define FUNC_NAME s_scm_bytevector_s32_set_x
1374 #if SIZEOF_VOID_P > 4
1375 INTEGER_SET (32, signed);
1377 LARGE_INTEGER_SET (32, signed);
1382 SCM_DEFINE (scm_bytevector_u32_native_set_x
, "bytevector-u32-native-set!",
1384 (SCM bv
, SCM index
, SCM value
),
1385 "Store the unsigned integer @var{value} at index @var{index} "
1386 "of @var{bv} using the native endianness.")
1387 #define FUNC_NAME s_scm_bytevector_u32_native_set_x
1389 #if SIZEOF_VOID_P > 4
1390 INTEGER_NATIVE_SET (32, unsigned);
1392 LARGE_INTEGER_NATIVE_SET (32, unsigned);
1397 SCM_DEFINE (scm_bytevector_s32_native_set_x
, "bytevector-s32-native-set!",
1399 (SCM bv
, SCM index
, SCM value
),
1400 "Store the signed integer @var{value} at index @var{index} "
1401 "of @var{bv} using the native endianness.")
1402 #define FUNC_NAME s_scm_bytevector_s32_native_set_x
1404 #if SIZEOF_VOID_P > 4
1405 INTEGER_NATIVE_SET (32, signed);
1407 LARGE_INTEGER_NATIVE_SET (32, signed);
1414 /* Operations on 64-bit integers. */
1416 /* For 64-bit integers, we use only the `large_{ref,set}' variant. */
1418 SCM_DEFINE (scm_bytevector_u64_ref
, "bytevector-u64-ref",
1420 (SCM bv
, SCM index
, SCM endianness
),
1421 "Return the unsigned 64-bit integer from @var{bv} at "
1423 #define FUNC_NAME s_scm_bytevector_u64_ref
1425 LARGE_INTEGER_REF (64, unsigned);
1429 SCM_DEFINE (scm_bytevector_s64_ref
, "bytevector-s64-ref",
1431 (SCM bv
, SCM index
, SCM endianness
),
1432 "Return the signed 64-bit integer from @var{bv} at "
1434 #define FUNC_NAME s_scm_bytevector_s64_ref
1436 LARGE_INTEGER_REF (64, signed);
1440 SCM_DEFINE (scm_bytevector_u64_native_ref
, "bytevector-u64-native-ref",
1442 (SCM bv
, SCM index
),
1443 "Return the unsigned 64-bit integer from @var{bv} at "
1444 "@var{index} using the native endianness.")
1445 #define FUNC_NAME s_scm_bytevector_u64_native_ref
1447 LARGE_INTEGER_NATIVE_REF (64, unsigned);
1451 SCM_DEFINE (scm_bytevector_s64_native_ref
, "bytevector-s64-native-ref",
1453 (SCM bv
, SCM index
),
1454 "Return the unsigned 64-bit integer from @var{bv} at "
1455 "@var{index} using the native endianness.")
1456 #define FUNC_NAME s_scm_bytevector_s64_native_ref
1458 LARGE_INTEGER_NATIVE_REF (64, signed);
1462 SCM_DEFINE (scm_bytevector_u64_set_x
, "bytevector-u64-set!",
1464 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1465 "Store @var{value} in @var{bv} at @var{index} according to "
1466 "@var{endianness}.")
1467 #define FUNC_NAME s_scm_bytevector_u64_set_x
1469 LARGE_INTEGER_SET (64, unsigned);
1473 SCM_DEFINE (scm_bytevector_s64_set_x
, "bytevector-s64-set!",
1475 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1476 "Store @var{value} in @var{bv} at @var{index} according to "
1477 "@var{endianness}.")
1478 #define FUNC_NAME s_scm_bytevector_s64_set_x
1480 LARGE_INTEGER_SET (64, signed);
1484 SCM_DEFINE (scm_bytevector_u64_native_set_x
, "bytevector-u64-native-set!",
1486 (SCM bv
, SCM index
, SCM value
),
1487 "Store the unsigned integer @var{value} at index @var{index} "
1488 "of @var{bv} using the native endianness.")
1489 #define FUNC_NAME s_scm_bytevector_u64_native_set_x
1491 LARGE_INTEGER_NATIVE_SET (64, unsigned);
1495 SCM_DEFINE (scm_bytevector_s64_native_set_x
, "bytevector-s64-native-set!",
1497 (SCM bv
, SCM index
, SCM value
),
1498 "Store the signed integer @var{value} at index @var{index} "
1499 "of @var{bv} using the native endianness.")
1500 #define FUNC_NAME s_scm_bytevector_s64_native_set_x
1502 LARGE_INTEGER_NATIVE_SET (64, signed);
1508 /* Operations on IEEE-754 numbers. */
1510 /* There are two possible word endians, visible in glibc's <ieee754.h>.
1511 However, in R6RS, when the endianness is `little', little endian is
1512 assumed for both the byte order and the word order. This is clear from
1513 Section 2.1 of R6RS-lib (in response to
1514 http://www.r6rs.org/formal-comments/comment-187.txt). */
1517 /* Convert to/from a floating-point number with different endianness. This
1518 method is probably not the most efficient but it should be portable. */
1521 float_to_foreign_endianness (union scm_ieee754_float
*target
,
1524 union scm_ieee754_float src
;
1528 #ifdef WORDS_BIGENDIAN
1529 /* Assuming little endian for both byte and word order. */
1530 target
->little_endian
.negative
= src
.big_endian
.negative
;
1531 target
->little_endian
.exponent
= src
.big_endian
.exponent
;
1532 target
->little_endian
.mantissa
= src
.big_endian
.mantissa
;
1534 target
->big_endian
.negative
= src
.little_endian
.negative
;
1535 target
->big_endian
.exponent
= src
.little_endian
.exponent
;
1536 target
->big_endian
.mantissa
= src
.little_endian
.mantissa
;
1541 float_from_foreign_endianness (const union scm_ieee754_float
*source
)
1543 union scm_ieee754_float result
;
1545 #ifdef WORDS_BIGENDIAN
1546 /* Assuming little endian for both byte and word order. */
1547 result
.big_endian
.negative
= source
->little_endian
.negative
;
1548 result
.big_endian
.exponent
= source
->little_endian
.exponent
;
1549 result
.big_endian
.mantissa
= source
->little_endian
.mantissa
;
1551 result
.little_endian
.negative
= source
->big_endian
.negative
;
1552 result
.little_endian
.exponent
= source
->big_endian
.exponent
;
1553 result
.little_endian
.mantissa
= source
->big_endian
.mantissa
;
1560 double_to_foreign_endianness (union scm_ieee754_double
*target
,
1563 union scm_ieee754_double src
;
1567 #ifdef WORDS_BIGENDIAN
1568 /* Assuming little endian for both byte and word order. */
1569 target
->little_little_endian
.negative
= src
.big_endian
.negative
;
1570 target
->little_little_endian
.exponent
= src
.big_endian
.exponent
;
1571 target
->little_little_endian
.mantissa0
= src
.big_endian
.mantissa0
;
1572 target
->little_little_endian
.mantissa1
= src
.big_endian
.mantissa1
;
1574 target
->big_endian
.negative
= src
.little_little_endian
.negative
;
1575 target
->big_endian
.exponent
= src
.little_little_endian
.exponent
;
1576 target
->big_endian
.mantissa0
= src
.little_little_endian
.mantissa0
;
1577 target
->big_endian
.mantissa1
= src
.little_little_endian
.mantissa1
;
1581 static inline double
1582 double_from_foreign_endianness (const union scm_ieee754_double
*source
)
1584 union scm_ieee754_double result
;
1586 #ifdef WORDS_BIGENDIAN
1587 /* Assuming little endian for both byte and word order. */
1588 result
.big_endian
.negative
= source
->little_little_endian
.negative
;
1589 result
.big_endian
.exponent
= source
->little_little_endian
.exponent
;
1590 result
.big_endian
.mantissa0
= source
->little_little_endian
.mantissa0
;
1591 result
.big_endian
.mantissa1
= source
->little_little_endian
.mantissa1
;
1593 result
.little_little_endian
.negative
= source
->big_endian
.negative
;
1594 result
.little_little_endian
.exponent
= source
->big_endian
.exponent
;
1595 result
.little_little_endian
.mantissa0
= source
->big_endian
.mantissa0
;
1596 result
.little_little_endian
.mantissa1
= source
->big_endian
.mantissa1
;
1602 /* Template macros to abstract over doubles and floats.
1603 XXX: Guile can only convert to/from doubles. */
1604 #define IEEE754_UNION(_c_type) union scm_ieee754_ ## _c_type
1605 #define IEEE754_TO_SCM(_c_type) scm_from_double
1606 #define IEEE754_FROM_SCM(_c_type) scm_to_double
1607 #define IEEE754_FROM_FOREIGN_ENDIANNESS(_c_type) \
1608 _c_type ## _from_foreign_endianness
1609 #define IEEE754_TO_FOREIGN_ENDIANNESS(_c_type) \
1610 _c_type ## _to_foreign_endianness
1613 /* Templace getters and setters. */
1615 #define IEEE754_ACCESSOR_PROLOGUE(_type) \
1616 INTEGER_ACCESSOR_PROLOGUE (sizeof (_type) << 3UL, signed);
1618 #define IEEE754_REF(_type) \
1621 IEEE754_ACCESSOR_PROLOGUE (_type); \
1622 SCM_VALIDATE_SYMBOL (3, endianness); \
1624 if (scm_is_eq (endianness, scm_i_native_endianness)) \
1625 memcpy (&c_result, &c_bv[c_index], sizeof (c_result)); \
1628 IEEE754_UNION (_type) c_raw; \
1630 memcpy (&c_raw, &c_bv[c_index], sizeof (c_raw)); \
1632 IEEE754_FROM_FOREIGN_ENDIANNESS (_type) (&c_raw); \
1635 return (IEEE754_TO_SCM (_type) (c_result));
1637 #define IEEE754_NATIVE_REF(_type) \
1640 IEEE754_ACCESSOR_PROLOGUE (_type); \
1642 memcpy (&c_result, &c_bv[c_index], sizeof (c_result)); \
1643 return (IEEE754_TO_SCM (_type) (c_result));
1645 #define IEEE754_SET(_type) \
1648 IEEE754_ACCESSOR_PROLOGUE (_type); \
1649 SCM_VALIDATE_REAL (3, value); \
1650 SCM_VALIDATE_SYMBOL (4, endianness); \
1651 c_value = IEEE754_FROM_SCM (_type) (value); \
1653 if (scm_is_eq (endianness, scm_i_native_endianness)) \
1654 memcpy (&c_bv[c_index], &c_value, sizeof (c_value)); \
1657 IEEE754_UNION (_type) c_raw; \
1659 IEEE754_TO_FOREIGN_ENDIANNESS (_type) (&c_raw, c_value); \
1660 memcpy (&c_bv[c_index], &c_raw, sizeof (c_raw)); \
1663 return SCM_UNSPECIFIED;
1665 #define IEEE754_NATIVE_SET(_type) \
1668 IEEE754_ACCESSOR_PROLOGUE (_type); \
1669 SCM_VALIDATE_REAL (3, value); \
1670 c_value = IEEE754_FROM_SCM (_type) (value); \
1672 memcpy (&c_bv[c_index], &c_value, sizeof (c_value)); \
1673 return SCM_UNSPECIFIED;
1676 /* Single precision. */
1678 SCM_DEFINE (scm_bytevector_ieee_single_ref
,
1679 "bytevector-ieee-single-ref",
1681 (SCM bv
, SCM index
, SCM endianness
),
1682 "Return the IEEE-754 single from @var{bv} at "
1684 #define FUNC_NAME s_scm_bytevector_ieee_single_ref
1686 IEEE754_REF (float);
1690 SCM_DEFINE (scm_bytevector_ieee_single_native_ref
,
1691 "bytevector-ieee-single-native-ref",
1693 (SCM bv
, SCM index
),
1694 "Return the IEEE-754 single from @var{bv} at "
1695 "@var{index} using the native endianness.")
1696 #define FUNC_NAME s_scm_bytevector_ieee_single_native_ref
1698 IEEE754_NATIVE_REF (float);
1702 SCM_DEFINE (scm_bytevector_ieee_single_set_x
,
1703 "bytevector-ieee-single-set!",
1705 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1706 "Store real @var{value} in @var{bv} at @var{index} according to "
1707 "@var{endianness}.")
1708 #define FUNC_NAME s_scm_bytevector_ieee_single_set_x
1710 IEEE754_SET (float);
1714 SCM_DEFINE (scm_bytevector_ieee_single_native_set_x
,
1715 "bytevector-ieee-single-native-set!",
1717 (SCM bv
, SCM index
, SCM value
),
1718 "Store the real @var{value} at index @var{index} "
1719 "of @var{bv} using the native endianness.")
1720 #define FUNC_NAME s_scm_bytevector_ieee_single_native_set_x
1722 IEEE754_NATIVE_SET (float);
1727 /* Double precision. */
1729 SCM_DEFINE (scm_bytevector_ieee_double_ref
,
1730 "bytevector-ieee-double-ref",
1732 (SCM bv
, SCM index
, SCM endianness
),
1733 "Return the IEEE-754 double from @var{bv} at "
1735 #define FUNC_NAME s_scm_bytevector_ieee_double_ref
1737 IEEE754_REF (double);
1741 SCM_DEFINE (scm_bytevector_ieee_double_native_ref
,
1742 "bytevector-ieee-double-native-ref",
1744 (SCM bv
, SCM index
),
1745 "Return the IEEE-754 double from @var{bv} at "
1746 "@var{index} using the native endianness.")
1747 #define FUNC_NAME s_scm_bytevector_ieee_double_native_ref
1749 IEEE754_NATIVE_REF (double);
1753 SCM_DEFINE (scm_bytevector_ieee_double_set_x
,
1754 "bytevector-ieee-double-set!",
1756 (SCM bv
, SCM index
, SCM value
, SCM endianness
),
1757 "Store real @var{value} in @var{bv} at @var{index} according to "
1758 "@var{endianness}.")
1759 #define FUNC_NAME s_scm_bytevector_ieee_double_set_x
1761 IEEE754_SET (double);
1765 SCM_DEFINE (scm_bytevector_ieee_double_native_set_x
,
1766 "bytevector-ieee-double-native-set!",
1768 (SCM bv
, SCM index
, SCM value
),
1769 "Store the real @var{value} at index @var{index} "
1770 "of @var{bv} using the native endianness.")
1771 #define FUNC_NAME s_scm_bytevector_ieee_double_native_set_x
1773 IEEE754_NATIVE_SET (double);
1778 #undef IEEE754_UNION
1779 #undef IEEE754_TO_SCM
1780 #undef IEEE754_FROM_SCM
1781 #undef IEEE754_FROM_FOREIGN_ENDIANNESS
1782 #undef IEEE754_TO_FOREIGN_ENDIANNESS
1784 #undef IEEE754_NATIVE_REF
1786 #undef IEEE754_NATIVE_SET
1789 /* Operations on strings. */
1792 /* Produce a function that returns the length of a UTF-encoded string. */
1793 #define UTF_STRLEN_FUNCTION(_utf_width) \
1794 static inline size_t \
1795 utf ## _utf_width ## _strlen (const uint ## _utf_width ## _t *str) \
1798 const uint ## _utf_width ## _t *ptr; \
1806 return (len * ((_utf_width) / 8)); \
1809 UTF_STRLEN_FUNCTION (8)
1812 /* Return the length (in bytes) of STR, a UTF-(UTF_WIDTH) encoded string. */
1813 #define UTF_STRLEN(_utf_width, _str) \
1814 utf ## _utf_width ## _strlen (_str)
1816 /* Return the "portable" name of the UTF encoding of size UTF_WIDTH and
1817 ENDIANNESS (Gnulib's `iconv_open' module guarantees the portability of the
1820 utf_encoding_name (char *name
, size_t utf_width
, SCM endianness
)
1822 strcpy (name
, "UTF-");
1823 strcat (name
, ((utf_width
== 8)
1825 : ((utf_width
== 16)
1827 : ((utf_width
== 32)
1831 ((scm_is_eq (endianness
, scm_sym_big
))
1833 : ((scm_is_eq (endianness
, scm_sym_little
))
1838 /* Maximum length of a UTF encoding name. */
1839 #define MAX_UTF_ENCODING_NAME_LEN 16
1841 /* Produce the body of a `string->utf' function. */
1842 #define STRING_TO_UTF(_utf_width) \
1846 char c_utf_name[MAX_UTF_ENCODING_NAME_LEN]; \
1847 char *c_utf = NULL, *c_locale; \
1848 size_t c_strlen, c_raw_strlen, c_utf_len = 0; \
1850 SCM_VALIDATE_STRING (1, str); \
1851 if (endianness == SCM_UNDEFINED) \
1852 endianness = scm_sym_big; \
1854 SCM_VALIDATE_SYMBOL (2, endianness); \
1856 c_strlen = scm_c_string_length (str); \
1857 c_raw_strlen = c_strlen * ((_utf_width) / 8); \
1860 c_str = (char *) alloca (c_raw_strlen + 1); \
1861 c_raw_strlen = scm_to_locale_stringbuf (str, c_str, c_strlen); \
1863 while (c_raw_strlen > c_strlen); \
1864 c_str[c_raw_strlen] = '\0'; \
1866 utf_encoding_name (c_utf_name, (_utf_width), endianness); \
1868 c_locale = (char *) alloca (strlen (locale_charset ()) + 1); \
1869 strcpy (c_locale, locale_charset ()); \
1871 err = mem_iconveh (c_str, c_raw_strlen, \
1872 c_locale, c_utf_name, \
1873 iconveh_question_mark, NULL, \
1874 &c_utf, &c_utf_len); \
1875 if (SCM_UNLIKELY (err)) \
1876 scm_syserror_msg (FUNC_NAME, "failed to convert string: ~A", \
1877 scm_list_1 (str), err); \
1880 /* C_UTF is null-terminated. It is malloc(3)-allocated, so we cannot \
1881 use `scm_c_take_bytevector ()'. */ \
1882 scm_dynwind_begin (0); \
1883 scm_dynwind_free (c_utf); \
1885 utf = make_bytevector (c_utf_len); \
1886 memcpy (SCM_BYTEVECTOR_CONTENTS (utf), c_utf, \
1889 scm_dynwind_end (); \
1896 SCM_DEFINE (scm_string_to_utf8
, "string->utf8",
1899 "Return a newly allocated bytevector that contains the UTF-8 "
1900 "encoding of @var{str}.")
1901 #define FUNC_NAME s_scm_string_to_utf8
1906 size_t c_strlen
, c_raw_strlen
;
1908 SCM_VALIDATE_STRING (1, str
);
1910 c_strlen
= scm_c_string_length (str
);
1911 c_raw_strlen
= c_strlen
;
1914 c_str
= (char *) alloca (c_raw_strlen
+ 1);
1915 c_raw_strlen
= scm_to_locale_stringbuf (str
, c_str
, c_strlen
);
1917 while (c_raw_strlen
> c_strlen
);
1918 c_str
[c_raw_strlen
] = '\0';
1920 c_utf
= u8_strconv_from_locale (c_str
);
1921 if (SCM_UNLIKELY (c_utf
== NULL
))
1922 scm_syserror (FUNC_NAME
);
1925 /* C_UTF is null-terminated. It is malloc(3)-allocated, so we cannot
1926 use `scm_c_take_bytevector ()'. */
1927 scm_dynwind_begin (0);
1928 scm_dynwind_free (c_utf
);
1930 utf
= make_bytevector (UTF_STRLEN (8, c_utf
));
1931 memcpy (SCM_BYTEVECTOR_CONTENTS (utf
), c_utf
,
1932 UTF_STRLEN (8, c_utf
));
1941 SCM_DEFINE (scm_string_to_utf16
, "string->utf16",
1943 (SCM str
, SCM endianness
),
1944 "Return a newly allocated bytevector that contains the UTF-16 "
1945 "encoding of @var{str}.")
1946 #define FUNC_NAME s_scm_string_to_utf16
1952 SCM_DEFINE (scm_string_to_utf32
, "string->utf32",
1954 (SCM str
, SCM endianness
),
1955 "Return a newly allocated bytevector that contains the UTF-32 "
1956 "encoding of @var{str}.")
1957 #define FUNC_NAME s_scm_string_to_utf32
1964 /* Produce the body of a function that converts a UTF-encoded bytevector to a
1966 #define UTF_TO_STRING(_utf_width) \
1967 SCM str = SCM_BOOL_F; \
1969 char *c_str = NULL, *c_locale; \
1970 char c_utf_name[MAX_UTF_ENCODING_NAME_LEN]; \
1971 const char *c_utf; \
1972 size_t c_strlen = 0, c_utf_len; \
1974 SCM_VALIDATE_BYTEVECTOR (1, utf); \
1975 if (endianness == SCM_UNDEFINED) \
1976 endianness = scm_sym_big; \
1978 SCM_VALIDATE_SYMBOL (2, endianness); \
1980 c_utf_len = SCM_BYTEVECTOR_LENGTH (utf); \
1981 c_utf = (char *) SCM_BYTEVECTOR_CONTENTS (utf); \
1982 utf_encoding_name (c_utf_name, (_utf_width), endianness); \
1984 c_locale = (char *) alloca (strlen (locale_charset ()) + 1); \
1985 strcpy (c_locale, locale_charset ()); \
1987 err = mem_iconveh (c_utf, c_utf_len, \
1988 c_utf_name, c_locale, \
1989 iconveh_question_mark, NULL, \
1990 &c_str, &c_strlen); \
1991 if (SCM_UNLIKELY (err)) \
1992 scm_syserror_msg (FUNC_NAME, "failed to convert to string: ~A", \
1993 scm_list_1 (utf), err); \
1995 /* C_STR is null-terminated. */ \
1996 str = scm_take_locale_stringn (c_str, c_strlen); \
2001 SCM_DEFINE (scm_utf8_to_string
, "utf8->string",
2004 "Return a newly allocate string that contains from the UTF-8-"
2005 "encoded contents of bytevector @var{utf}.")
2006 #define FUNC_NAME s_scm_utf8_to_string
2010 char *c_str
= NULL
, *c_locale
;
2012 size_t c_utf_len
, c_strlen
= 0;
2014 SCM_VALIDATE_BYTEVECTOR (1, utf
);
2016 c_utf_len
= SCM_BYTEVECTOR_LENGTH (utf
);
2018 c_locale
= (char *) alloca (strlen (locale_charset ()) + 1);
2019 strcpy (c_locale
, locale_charset ());
2021 c_utf
= (char *) SCM_BYTEVECTOR_CONTENTS (utf
);
2022 err
= mem_iconveh (c_utf
, c_utf_len
,
2024 iconveh_question_mark
, NULL
,
2026 if (SCM_UNLIKELY (err
))
2027 scm_syserror_msg (FUNC_NAME
, "failed to convert to string: ~A",
2028 scm_list_1 (utf
), err
);
2030 /* C_STR is null-terminated. */
2031 str
= scm_take_locale_stringn (c_str
, c_strlen
);
2037 SCM_DEFINE (scm_utf16_to_string
, "utf16->string",
2039 (SCM utf
, SCM endianness
),
2040 "Return a newly allocate string that contains from the UTF-16-"
2041 "encoded contents of bytevector @var{utf}.")
2042 #define FUNC_NAME s_scm_utf16_to_string
2048 SCM_DEFINE (scm_utf32_to_string
, "utf32->string",
2050 (SCM utf
, SCM endianness
),
2051 "Return a newly allocate string that contains from the UTF-32-"
2052 "encoded contents of bytevector @var{utf}.")
2053 #define FUNC_NAME s_scm_utf32_to_string
2061 /* Initialization. */
2064 scm_bootstrap_bytevectors (void)
2066 /* The SMOB type must be instantiated here because the
2067 generalized-vector API may want to access bytevectors even though
2068 `(rnrs bytevector)' hasn't been loaded. */
2069 scm_tc16_bytevector
= scm_make_smob_type ("bytevector", 0);
2070 scm_set_smob_print (scm_tc16_bytevector
, print_bytevector
);
2071 scm_set_smob_equalp (scm_tc16_bytevector
, bytevector_equal_p
);
2073 scm_null_bytevector
=
2074 scm_gc_protect_object (make_bytevector_from_buffer (0, NULL
));
2076 #ifdef WORDS_BIGENDIAN
2077 scm_i_native_endianness
= scm_permanent_object (scm_from_locale_symbol ("big"));
2079 scm_i_native_endianness
= scm_permanent_object (scm_from_locale_symbol ("little"));
2082 scm_c_register_extension ("libguile", "scm_init_bytevectors",
2083 (scm_t_extension_init_func
) scm_init_bytevectors
,
2088 scm_init_bytevectors (void)
2090 #include "libguile/bytevectors.x"
2092 scm_endianness_big
= scm_sym_big
;
2093 scm_endianness_little
= scm_sym_little
;