Merge remote-tracking branch 'origin/stable-2.0'
[bpt/guile.git] / libguile / bytevectors.c
CommitLineData
8c76a897 1/* Copyright (C) 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
1ee2c72e
LC
2 *
3 * This library is free software; you can redistribute it and/or
53befeb7
NJ
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.
1ee2c72e 7 *
53befeb7
NJ
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
1ee2c72e
LC
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
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
53befeb7
NJ
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301 USA
1ee2c72e
LC
17 */
18
19
20#ifdef HAVE_CONFIG_H
21# include <config.h>
22#endif
23
24#include <alloca.h>
f5a51cae 25#include <assert.h>
1ee2c72e
LC
26
27#include <gmp.h>
28
29#include "libguile/_scm.h"
cfb4702f 30#include "libguile/extensions.h"
1ee2c72e
LC
31#include "libguile/bytevectors.h"
32#include "libguile/strings.h"
33#include "libguile/validate.h"
2fa901a5 34#include "libguile/arrays.h"
2a610be5 35#include "libguile/array-handle.h"
476b894c 36#include "libguile/uniform.h"
782a82ee 37#include "libguile/srfi-4.h"
1ee2c72e
LC
38
39#include <byteswap.h>
40#include <striconveh.h>
41#include <uniconv.h>
3a5bc4fa 42#include <unistr.h>
1ee2c72e
LC
43
44#ifdef HAVE_LIMITS_H
45# include <limits.h>
46#else
47/* Assuming 32-bit longs. */
48# define ULONG_MAX 4294967295UL
49#endif
50
51#include <string.h>
52
53
54\f
55/* Utilities. */
56
57/* Convenience macros. These are used by the various templates (macros) that
58 are parameterized by integer signedness. */
59#define INT8_T_signed scm_t_int8
60#define INT8_T_unsigned scm_t_uint8
61#define INT16_T_signed scm_t_int16
62#define INT16_T_unsigned scm_t_uint16
63#define INT32_T_signed scm_t_int32
64#define INT32_T_unsigned scm_t_uint32
65#define is_signed_int8(_x) (((_x) >= -128L) && ((_x) <= 127L))
66#define is_unsigned_int8(_x) ((_x) <= 255UL)
67#define is_signed_int16(_x) (((_x) >= -32768L) && ((_x) <= 32767L))
68#define is_unsigned_int16(_x) ((_x) <= 65535UL)
69#define is_signed_int32(_x) (((_x) >= -2147483648L) && ((_x) <= 2147483647L))
70#define is_unsigned_int32(_x) ((_x) <= 4294967295UL)
71#define SIGNEDNESS_signed 1
72#define SIGNEDNESS_unsigned 0
73
74#define INT_TYPE(_size, _sign) INT ## _size ## _T_ ## _sign
75#define INT_SWAP(_size) bswap_ ## _size
76#define INT_VALID_P(_size, _sign) is_ ## _sign ## _int ## _size
77#define SIGNEDNESS(_sign) SIGNEDNESS_ ## _sign
78
79
80#define INTEGER_ACCESSOR_PROLOGUE(_len, _sign) \
2d34e924 81 size_t c_len, c_index; \
1ee2c72e
LC
82 _sign char *c_bv; \
83 \
84 SCM_VALIDATE_BYTEVECTOR (1, bv); \
85 c_index = scm_to_uint (index); \
86 \
87 c_len = SCM_BYTEVECTOR_LENGTH (bv); \
88 c_bv = (_sign char *) SCM_BYTEVECTOR_CONTENTS (bv); \
89 \
90 if (SCM_UNLIKELY (c_index + ((_len) >> 3UL) - 1 >= c_len)) \
91 scm_out_of_range (FUNC_NAME, index);
92
93/* Template for fixed-size integer access (only 8, 16 or 32-bit). */
caa92f5e
AW
94#define INTEGER_REF(_len, _sign) \
95 SCM result; \
96 \
97 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
98 SCM_VALIDATE_SYMBOL (3, endianness); \
99 \
100 { \
101 INT_TYPE (_len, _sign) c_result; \
102 \
103 memcpy (&c_result, &c_bv[c_index], (_len) / 8); \
104 if (!scm_is_eq (endianness, scm_i_native_endianness)) \
105 c_result = INT_SWAP (_len) (c_result); \
106 \
107 result = SCM_I_MAKINUM (c_result); \
108 } \
109 \
1ee2c72e
LC
110 return result;
111
112/* Template for fixed-size integer access using the native endianness. */
113#define INTEGER_NATIVE_REF(_len, _sign) \
114 SCM result; \
115 \
116 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
117 \
118 { \
119 INT_TYPE (_len, _sign) c_result; \
120 \
121 memcpy (&c_result, &c_bv[c_index], (_len) / 8); \
122 result = SCM_I_MAKINUM (c_result); \
123 } \
124 \
125 return result;
126
127/* Template for fixed-size integer modification (only 8, 16 or 32-bit). */
128#define INTEGER_SET(_len, _sign) \
129 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
130 SCM_VALIDATE_SYMBOL (3, endianness); \
131 \
132 { \
e25f3727 133 scm_t_signed_bits c_value; \
1ee2c72e
LC
134 INT_TYPE (_len, _sign) c_value_short; \
135 \
136 if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
137 scm_wrong_type_arg (FUNC_NAME, 3, value); \
138 \
139 c_value = SCM_I_INUM (value); \
140 if (SCM_UNLIKELY (!INT_VALID_P (_len, _sign) (c_value))) \
141 scm_out_of_range (FUNC_NAME, value); \
142 \
143 c_value_short = (INT_TYPE (_len, _sign)) c_value; \
caa92f5e 144 if (!scm_is_eq (endianness, scm_i_native_endianness)) \
1ee2c72e
LC
145 c_value_short = INT_SWAP (_len) (c_value_short); \
146 \
147 memcpy (&c_bv[c_index], &c_value_short, (_len) / 8); \
148 } \
149 \
150 return SCM_UNSPECIFIED;
151
152/* Template for fixed-size integer modification using the native
153 endianness. */
154#define INTEGER_NATIVE_SET(_len, _sign) \
155 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
156 \
157 { \
e25f3727 158 scm_t_signed_bits c_value; \
1ee2c72e
LC
159 INT_TYPE (_len, _sign) c_value_short; \
160 \
161 if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
162 scm_wrong_type_arg (FUNC_NAME, 3, value); \
163 \
164 c_value = SCM_I_INUM (value); \
165 if (SCM_UNLIKELY (!INT_VALID_P (_len, _sign) (c_value))) \
166 scm_out_of_range (FUNC_NAME, value); \
167 \
168 c_value_short = (INT_TYPE (_len, _sign)) c_value; \
169 \
170 memcpy (&c_bv[c_index], &c_value_short, (_len) / 8); \
171 } \
172 \
173 return SCM_UNSPECIFIED;
174
175
176\f
177/* Bytevector type. */
178
0665b3ff 179#define SCM_BYTEVECTOR_HEADER_BYTES \
3b08b1c2 180 (SCM_BYTEVECTOR_HEADER_SIZE * sizeof (scm_t_bits))
0665b3ff 181
f332089e 182#define SCM_BYTEVECTOR_SET_LENGTH(_bv, _len) \
807e5a66 183 SCM_SET_CELL_WORD_1 ((_bv), (scm_t_bits) (_len))
3fe87cf7
LC
184#define SCM_BYTEVECTOR_SET_CONTENTS(_bv, _contents) \
185 SCM_SET_CELL_WORD_2 ((_bv), (scm_t_bits) (_contents))
186#define SCM_BYTEVECTOR_SET_CONTIGUOUS_P(bv, contiguous_p) \
187 SCM_SET_BYTEVECTOR_FLAGS ((bv), \
188 SCM_BYTEVECTOR_ELEMENT_TYPE (bv) \
189 | ((contiguous_p) << 8UL))
190
191#define SCM_BYTEVECTOR_SET_ELEMENT_TYPE(bv, hint) \
192 SCM_SET_BYTEVECTOR_FLAGS ((bv), \
193 (hint) \
194 | (SCM_BYTEVECTOR_CONTIGUOUS_P (bv) << 8UL))
059a588f
AW
195#define SCM_BYTEVECTOR_SET_PARENT(_bv, _parent) \
196 SCM_SET_CELL_OBJECT_3 ((_bv), (_parent))
197
e286c973
AW
198#define SCM_BYTEVECTOR_TYPE_SIZE(var) \
199 (scm_i_array_element_type_sizes[SCM_BYTEVECTOR_ELEMENT_TYPE (var)]/8)
200#define SCM_BYTEVECTOR_TYPED_LENGTH(var) \
3fe87cf7 201 (SCM_BYTEVECTOR_LENGTH (var) / SCM_BYTEVECTOR_TYPE_SIZE (var))
1ee2c72e
LC
202
203/* The empty bytevector. */
204SCM scm_null_bytevector = SCM_UNSPECIFIED;
205
206
207static inline SCM
0665b3ff 208make_bytevector (size_t len, scm_t_array_element_type element_type)
1ee2c72e 209{
f332089e 210 SCM ret;
e286c973 211 size_t c_len;
0665b3ff 212
e286c973
AW
213 if (SCM_UNLIKELY (element_type > SCM_ARRAY_ELEMENT_TYPE_LAST
214 || scm_i_array_element_type_sizes[element_type] < 8
871054f0 215 || len >= (((size_t) -1)
e286c973
AW
216 / (scm_i_array_element_type_sizes[element_type]/8))))
217 /* This would be an internal Guile programming error */
218 abort ();
0665b3ff
LC
219
220 if (SCM_UNLIKELY (len == 0 && element_type == SCM_ARRAY_ELEMENT_TYPE_VU8
221 && SCM_BYTEVECTOR_P (scm_null_bytevector)))
222 ret = scm_null_bytevector;
f332089e
AW
223 else
224 {
3fe87cf7
LC
225 signed char *contents;
226
0665b3ff
LC
227 c_len = len * (scm_i_array_element_type_sizes[element_type] / 8);
228
3fe87cf7
LC
229 contents = scm_gc_malloc_pointerless (SCM_BYTEVECTOR_HEADER_BYTES + c_len,
230 SCM_GC_BYTEVECTOR);
21041372 231 ret = SCM_PACK_POINTER (contents);
3fe87cf7 232 contents += SCM_BYTEVECTOR_HEADER_BYTES;
0665b3ff 233
0665b3ff 234 SCM_BYTEVECTOR_SET_LENGTH (ret, c_len);
3fe87cf7
LC
235 SCM_BYTEVECTOR_SET_CONTENTS (ret, contents);
236 SCM_BYTEVECTOR_SET_CONTIGUOUS_P (ret, 1);
0665b3ff 237 SCM_BYTEVECTOR_SET_ELEMENT_TYPE (ret, element_type);
059a588f 238 SCM_BYTEVECTOR_SET_PARENT (ret, SCM_BOOL_F);
f332089e 239 }
0665b3ff 240
f332089e 241 return ret;
1ee2c72e
LC
242}
243
0665b3ff 244/* Return a bytevector of LEN elements of type ELEMENT_TYPE, with element
3fe87cf7
LC
245 values taken from CONTENTS. Assume that the storage for CONTENTS will be
246 automatically reclaimed when it becomes unreachable. */
1ee2c72e 247static inline SCM
0665b3ff
LC
248make_bytevector_from_buffer (size_t len, void *contents,
249 scm_t_array_element_type element_type)
1ee2c72e 250{
0665b3ff 251 SCM ret;
1ee2c72e 252
3fe87cf7
LC
253 if (SCM_UNLIKELY (len == 0))
254 ret = make_bytevector (len, element_type);
255 else
1ee2c72e 256 {
0665b3ff
LC
257 size_t c_len;
258
21041372 259 ret = SCM_PACK_POINTER (scm_gc_malloc (SCM_BYTEVECTOR_HEADER_BYTES,
3fe87cf7
LC
260 SCM_GC_BYTEVECTOR));
261
0665b3ff 262 c_len = len * (scm_i_array_element_type_sizes[element_type] / 8);
0665b3ff 263
3fe87cf7
LC
264 SCM_BYTEVECTOR_SET_LENGTH (ret, c_len);
265 SCM_BYTEVECTOR_SET_CONTENTS (ret, contents);
266 SCM_BYTEVECTOR_SET_CONTIGUOUS_P (ret, 0);
267 SCM_BYTEVECTOR_SET_ELEMENT_TYPE (ret, element_type);
059a588f 268 SCM_BYTEVECTOR_SET_PARENT (ret, SCM_BOOL_F);
1ee2c72e 269 }
0665b3ff
LC
270
271 return ret;
1ee2c72e
LC
272}
273
0665b3ff 274
1ee2c72e
LC
275/* Return a new bytevector of size LEN octets. */
276SCM
2d34e924 277scm_c_make_bytevector (size_t len)
1ee2c72e 278{
e286c973
AW
279 return make_bytevector (len, SCM_ARRAY_ELEMENT_TYPE_VU8);
280}
281
3dc2afe2
AW
282/* Return a new bytevector of size LEN elements. */
283SCM
284scm_i_make_typed_bytevector (size_t len, scm_t_array_element_type element_type)
285{
286 return make_bytevector (len, element_type);
287}
288
059a588f
AW
289/* Return a bytevector of size LEN made up of CONTENTS. The area
290 pointed to by CONTENTS must be protected from GC somehow: either
291 because it was allocated using `scm_gc_malloc ()', or because it is
292 part of PARENT. */
1ee2c72e 293SCM
8b66aa8f 294scm_c_take_gc_bytevector (signed char *contents, size_t len, SCM parent)
1ee2c72e 295{
059a588f
AW
296 SCM ret;
297
298 ret = make_bytevector_from_buffer (len, contents, SCM_ARRAY_ELEMENT_TYPE_VU8);
299 SCM_BYTEVECTOR_SET_PARENT (ret, parent);
300
301 return ret;
e286c973 302}
1ee2c72e 303
3dc2afe2
AW
304SCM
305scm_c_take_typed_bytevector (signed char *contents, size_t len,
059a588f 306 scm_t_array_element_type element_type, SCM parent)
3dc2afe2 307{
059a588f
AW
308 SCM ret;
309
310 ret = make_bytevector_from_buffer (len, contents, element_type);
311 SCM_BYTEVECTOR_SET_PARENT (ret, parent);
312
313 return ret;
3dc2afe2
AW
314}
315
1ee2c72e 316/* Shrink BV to C_NEW_LEN (which is assumed to be smaller than its current
0665b3ff 317 size) and return the new bytevector (possibly different from BV). */
1ee2c72e 318SCM
0665b3ff 319scm_c_shrink_bytevector (SCM bv, size_t c_new_len)
1ee2c72e 320{
0665b3ff
LC
321 SCM new_bv;
322 size_t c_len;
323
e286c973
AW
324 if (SCM_UNLIKELY (c_new_len % SCM_BYTEVECTOR_TYPE_SIZE (bv)))
325 /* This would be an internal Guile programming error */
326 abort ();
327
0665b3ff
LC
328 c_len = SCM_BYTEVECTOR_LENGTH (bv);
329 if (SCM_UNLIKELY (c_new_len > c_len))
330 abort ();
1ee2c72e 331
0665b3ff 332 SCM_BYTEVECTOR_SET_LENGTH (bv, c_new_len);
1ee2c72e 333
3fe87cf7 334 if (SCM_BYTEVECTOR_CONTIGUOUS_P (bv))
0aed71aa 335 new_bv = PTR2SCM (scm_gc_realloc (SCM2PTR (bv),
3fe87cf7
LC
336 c_len + SCM_BYTEVECTOR_HEADER_BYTES,
337 c_new_len + SCM_BYTEVECTOR_HEADER_BYTES,
338 SCM_GC_BYTEVECTOR));
339 else
340 {
341 signed char *c_bv;
342
343 c_bv = scm_gc_realloc (SCM_BYTEVECTOR_CONTENTS (bv),
344 c_len, c_new_len, SCM_GC_BYTEVECTOR);
345 SCM_BYTEVECTOR_SET_CONTENTS (bv, c_bv);
346
347 new_bv = bv;
348 }
1ee2c72e 349
0665b3ff 350 return new_bv;
1ee2c72e
LC
351}
352
404bb5f8
LC
353int
354scm_is_bytevector (SCM obj)
355{
807e5a66 356 return SCM_BYTEVECTOR_P (obj);
404bb5f8
LC
357}
358
359size_t
360scm_c_bytevector_length (SCM bv)
361#define FUNC_NAME "scm_c_bytevector_length"
362{
363 SCM_VALIDATE_BYTEVECTOR (1, bv);
364
365 return SCM_BYTEVECTOR_LENGTH (bv);
366}
367#undef FUNC_NAME
368
369scm_t_uint8
370scm_c_bytevector_ref (SCM bv, size_t index)
371#define FUNC_NAME "scm_c_bytevector_ref"
372{
373 size_t c_len;
374 const scm_t_uint8 *c_bv;
375
376 SCM_VALIDATE_BYTEVECTOR (1, bv);
377
378 c_len = SCM_BYTEVECTOR_LENGTH (bv);
379 c_bv = (scm_t_uint8 *) SCM_BYTEVECTOR_CONTENTS (bv);
380
381 if (SCM_UNLIKELY (index >= c_len))
382 scm_out_of_range (FUNC_NAME, scm_from_size_t (index));
383
384 return c_bv[index];
385}
386#undef FUNC_NAME
387
388void
389scm_c_bytevector_set_x (SCM bv, size_t index, scm_t_uint8 value)
390#define FUNC_NAME "scm_c_bytevector_set_x"
391{
392 size_t c_len;
393 scm_t_uint8 *c_bv;
394
395 SCM_VALIDATE_BYTEVECTOR (1, bv);
396
397 c_len = SCM_BYTEVECTOR_LENGTH (bv);
398 c_bv = (scm_t_uint8 *) SCM_BYTEVECTOR_CONTENTS (bv);
399
400 if (SCM_UNLIKELY (index >= c_len))
401 scm_out_of_range (FUNC_NAME, scm_from_size_t (index));
402
403 c_bv[index] = value;
404}
405#undef FUNC_NAME
406
e286c973
AW
407
408\f
807e5a66
LC
409int
410scm_i_print_bytevector (SCM bv, SCM port, scm_print_state *pstate SCM_UNUSED)
1ee2c72e 411{
e286c973
AW
412 ssize_t ubnd, inc, i;
413 scm_t_array_handle h;
414
415 scm_array_get_handle (bv, &h);
1ee2c72e 416
0607ebbf 417 scm_putc_unlocked ('#', port);
e286c973 418 scm_write (scm_array_handle_element_type (&h), port);
0607ebbf 419 scm_putc_unlocked ('(', port);
e286c973
AW
420 for (i = h.dims[0].lbnd, ubnd = h.dims[0].ubnd, inc = h.dims[0].inc;
421 i <= ubnd; i += inc)
1ee2c72e
LC
422 {
423 if (i > 0)
0607ebbf 424 scm_putc_unlocked (' ', port);
e286c973 425 scm_write (scm_array_handle_ref (&h, i), port);
1ee2c72e 426 }
0607ebbf 427 scm_putc_unlocked (')', port);
1ee2c72e 428
1ee2c72e
LC
429 return 1;
430}
431
1ee2c72e
LC
432\f
433/* General operations. */
434
435SCM_SYMBOL (scm_sym_big, "big");
436SCM_SYMBOL (scm_sym_little, "little");
437
438SCM scm_endianness_big, scm_endianness_little;
439
440/* Host endianness (a symbol). */
caa92f5e 441SCM scm_i_native_endianness = SCM_UNSPECIFIED;
1ee2c72e
LC
442
443/* Byte-swapping. */
444#ifndef bswap_24
445# define bswap_24(_x) \
446 ((((_x) & 0xff0000) >> 16) | \
447 (((_x) & 0x00ff00)) | \
448 (((_x) & 0x0000ff) << 16))
449#endif
450
451
452SCM_DEFINE (scm_native_endianness, "native-endianness", 0, 0, 0,
453 (void),
454 "Return a symbol denoting the machine's native endianness.")
455#define FUNC_NAME s_scm_native_endianness
456{
caa92f5e 457 return scm_i_native_endianness;
1ee2c72e
LC
458}
459#undef FUNC_NAME
460
461SCM_DEFINE (scm_bytevector_p, "bytevector?", 1, 0, 0,
462 (SCM obj),
463 "Return true if @var{obj} is a bytevector.")
464#define FUNC_NAME s_scm_bytevector_p
465{
404bb5f8 466 return scm_from_bool (scm_is_bytevector (obj));
1ee2c72e
LC
467}
468#undef FUNC_NAME
469
470SCM_DEFINE (scm_make_bytevector, "make-bytevector", 1, 1, 0,
471 (SCM len, SCM fill),
472 "Return a newly allocated bytevector of @var{len} bytes, "
473 "optionally filled with @var{fill}.")
474#define FUNC_NAME s_scm_make_bytevector
475{
476 SCM bv;
477 unsigned c_len;
478 signed char c_fill = '\0';
479
480 SCM_VALIDATE_UINT_COPY (1, len, c_len);
d223c3fc 481 if (!scm_is_eq (fill, SCM_UNDEFINED))
1ee2c72e
LC
482 {
483 int value;
484
485 value = scm_to_int (fill);
486 if (SCM_UNLIKELY ((value < -128) || (value > 255)))
487 scm_out_of_range (FUNC_NAME, fill);
488 c_fill = (signed char) value;
489 }
490
e286c973 491 bv = make_bytevector (c_len, SCM_ARRAY_ELEMENT_TYPE_VU8);
d223c3fc 492 if (!scm_is_eq (fill, SCM_UNDEFINED))
1ee2c72e
LC
493 {
494 unsigned i;
495 signed char *contents;
496
497 contents = SCM_BYTEVECTOR_CONTENTS (bv);
498 for (i = 0; i < c_len; i++)
499 contents[i] = c_fill;
500 }
3ef6650d
AW
501 else
502 memset (SCM_BYTEVECTOR_CONTENTS (bv), 0, c_len);
1ee2c72e
LC
503
504 return bv;
505}
506#undef FUNC_NAME
507
508SCM_DEFINE (scm_bytevector_length, "bytevector-length", 1, 0, 0,
509 (SCM bv),
510 "Return the length (in bytes) of @var{bv}.")
511#define FUNC_NAME s_scm_bytevector_length
512{
404bb5f8 513 return scm_from_uint (scm_c_bytevector_length (bv));
1ee2c72e
LC
514}
515#undef FUNC_NAME
516
517SCM_DEFINE (scm_bytevector_eq_p, "bytevector=?", 2, 0, 0,
518 (SCM bv1, SCM bv2),
519 "Return is @var{bv1} equals to @var{bv2}---i.e., if they "
520 "have the same length and contents.")
521#define FUNC_NAME s_scm_bytevector_eq_p
522{
523 SCM result = SCM_BOOL_F;
524 unsigned c_len1, c_len2;
525
526 SCM_VALIDATE_BYTEVECTOR (1, bv1);
527 SCM_VALIDATE_BYTEVECTOR (2, bv2);
528
529 c_len1 = SCM_BYTEVECTOR_LENGTH (bv1);
530 c_len2 = SCM_BYTEVECTOR_LENGTH (bv2);
531
a587d6a9
AW
532 if (c_len1 == c_len2 && (SCM_BYTEVECTOR_ELEMENT_TYPE (bv1)
533 == SCM_BYTEVECTOR_ELEMENT_TYPE (bv2)))
1ee2c72e
LC
534 {
535 signed char *c_bv1, *c_bv2;
536
537 c_bv1 = SCM_BYTEVECTOR_CONTENTS (bv1);
538 c_bv2 = SCM_BYTEVECTOR_CONTENTS (bv2);
539
540 result = scm_from_bool (!memcmp (c_bv1, c_bv2, c_len1));
541 }
542
543 return result;
544}
545#undef FUNC_NAME
546
547SCM_DEFINE (scm_bytevector_fill_x, "bytevector-fill!", 2, 0, 0,
548 (SCM bv, SCM fill),
549 "Fill bytevector @var{bv} with @var{fill}, a byte.")
550#define FUNC_NAME s_scm_bytevector_fill_x
551{
552 unsigned c_len, i;
553 signed char *c_bv, c_fill;
554
555 SCM_VALIDATE_BYTEVECTOR (1, bv);
556 c_fill = scm_to_int8 (fill);
557
558 c_len = SCM_BYTEVECTOR_LENGTH (bv);
559 c_bv = SCM_BYTEVECTOR_CONTENTS (bv);
560
561 for (i = 0; i < c_len; i++)
562 c_bv[i] = c_fill;
563
564 return SCM_UNSPECIFIED;
565}
566#undef FUNC_NAME
567
568SCM_DEFINE (scm_bytevector_copy_x, "bytevector-copy!", 5, 0, 0,
569 (SCM source, SCM source_start, SCM target, SCM target_start,
570 SCM len),
571 "Copy @var{len} bytes from @var{source} into @var{target}, "
572 "starting reading from @var{source_start} (a positive index "
573 "within @var{source}) and start writing at "
574 "@var{target_start}.")
575#define FUNC_NAME s_scm_bytevector_copy_x
576{
577 unsigned c_len, c_source_len, c_target_len;
578 unsigned c_source_start, c_target_start;
579 signed char *c_source, *c_target;
580
581 SCM_VALIDATE_BYTEVECTOR (1, source);
582 SCM_VALIDATE_BYTEVECTOR (3, target);
583
584 c_len = scm_to_uint (len);
585 c_source_start = scm_to_uint (source_start);
586 c_target_start = scm_to_uint (target_start);
587
588 c_source = SCM_BYTEVECTOR_CONTENTS (source);
589 c_target = SCM_BYTEVECTOR_CONTENTS (target);
590 c_source_len = SCM_BYTEVECTOR_LENGTH (source);
591 c_target_len = SCM_BYTEVECTOR_LENGTH (target);
592
593 if (SCM_UNLIKELY (c_source_start + c_len > c_source_len))
594 scm_out_of_range (FUNC_NAME, source_start);
595 if (SCM_UNLIKELY (c_target_start + c_len > c_target_len))
596 scm_out_of_range (FUNC_NAME, target_start);
597
80719649
LC
598 memmove (c_target + c_target_start,
599 c_source + c_source_start,
600 c_len);
1ee2c72e
LC
601
602 return SCM_UNSPECIFIED;
603}
604#undef FUNC_NAME
605
606SCM_DEFINE (scm_bytevector_copy, "bytevector-copy", 1, 0, 0,
607 (SCM bv),
608 "Return a newly allocated copy of @var{bv}.")
609#define FUNC_NAME s_scm_bytevector_copy
610{
611 SCM copy;
612 unsigned c_len;
613 signed char *c_bv, *c_copy;
614
615 SCM_VALIDATE_BYTEVECTOR (1, bv);
616
617 c_len = SCM_BYTEVECTOR_LENGTH (bv);
618 c_bv = SCM_BYTEVECTOR_CONTENTS (bv);
619
e286c973 620 copy = make_bytevector (c_len, SCM_BYTEVECTOR_ELEMENT_TYPE (bv));
1ee2c72e
LC
621 c_copy = SCM_BYTEVECTOR_CONTENTS (copy);
622 memcpy (c_copy, c_bv, c_len);
623
624 return copy;
625}
626#undef FUNC_NAME
627
782a82ee
AW
628SCM_DEFINE (scm_uniform_array_to_bytevector, "uniform-array->bytevector",
629 1, 0, 0, (SCM array),
630 "Return a newly allocated bytevector whose contents\n"
631 "will be copied from the uniform array @var{array}.")
632#define FUNC_NAME s_scm_uniform_array_to_bytevector
633{
634 SCM contents, ret;
f5a51cae 635 size_t len, sz, byte_len;
782a82ee 636 scm_t_array_handle h;
f5a51cae 637 const void *elts;
782a82ee
AW
638
639 contents = scm_array_contents (array, SCM_BOOL_T);
640 if (scm_is_false (contents))
641 scm_wrong_type_arg_msg (FUNC_NAME, 0, array, "uniform contiguous array");
642
643 scm_array_get_handle (contents, &h);
f5a51cae 644 assert (h.base == 0);
782a82ee 645
f5a51cae 646 elts = h.elements;
782a82ee 647 len = h.dims->inc * (h.dims->ubnd - h.dims->lbnd + 1);
f5a51cae
AW
648 sz = scm_array_handle_uniform_element_bit_size (&h);
649 if (sz >= 8 && ((sz % 8) == 0))
650 byte_len = len * (sz / 8);
b0fae4ec 651 else if (sz < 8)
29553c54
LC
652 /* byte_len = ceil (len * sz / 8) */
653 byte_len = (len * sz + 7) / 8;
b0fae4ec
AW
654 else
655 /* an internal guile error, really */
656 SCM_MISC_ERROR ("uniform elements larger than 8 bits must fill whole bytes", SCM_EOL);
782a82ee 657
f5a51cae
AW
658 ret = make_bytevector (byte_len, SCM_ARRAY_ELEMENT_TYPE_VU8);
659 memcpy (SCM_BYTEVECTOR_CONTENTS (ret), elts, byte_len);
782a82ee
AW
660
661 scm_array_handle_release (&h);
662
663 return ret;
664}
665#undef FUNC_NAME
666
1ee2c72e
LC
667\f
668/* Operations on bytes and octets. */
669
670SCM_DEFINE (scm_bytevector_u8_ref, "bytevector-u8-ref", 2, 0, 0,
671 (SCM bv, SCM index),
672 "Return the octet located at @var{index} in @var{bv}.")
673#define FUNC_NAME s_scm_bytevector_u8_ref
674{
675 INTEGER_NATIVE_REF (8, unsigned);
676}
677#undef FUNC_NAME
678
679SCM_DEFINE (scm_bytevector_s8_ref, "bytevector-s8-ref", 2, 0, 0,
680 (SCM bv, SCM index),
681 "Return the byte located at @var{index} in @var{bv}.")
682#define FUNC_NAME s_scm_bytevector_s8_ref
683{
684 INTEGER_NATIVE_REF (8, signed);
685}
686#undef FUNC_NAME
687
688SCM_DEFINE (scm_bytevector_u8_set_x, "bytevector-u8-set!", 3, 0, 0,
689 (SCM bv, SCM index, SCM value),
690 "Return the octet located at @var{index} in @var{bv}.")
691#define FUNC_NAME s_scm_bytevector_u8_set_x
692{
693 INTEGER_NATIVE_SET (8, unsigned);
694}
695#undef FUNC_NAME
696
697SCM_DEFINE (scm_bytevector_s8_set_x, "bytevector-s8-set!", 3, 0, 0,
698 (SCM bv, SCM index, SCM value),
699 "Return the octet located at @var{index} in @var{bv}.")
cabf1b31 700#define FUNC_NAME s_scm_bytevector_s8_set_x
1ee2c72e
LC
701{
702 INTEGER_NATIVE_SET (8, signed);
703}
704#undef FUNC_NAME
705
706#undef OCTET_ACCESSOR_PROLOGUE
707
708
709SCM_DEFINE (scm_bytevector_to_u8_list, "bytevector->u8-list", 1, 0, 0,
710 (SCM bv),
711 "Return a newly allocated list of octets containing the "
712 "contents of @var{bv}.")
713#define FUNC_NAME s_scm_bytevector_to_u8_list
714{
715 SCM lst, pair;
716 unsigned c_len, i;
717 unsigned char *c_bv;
718
719 SCM_VALIDATE_BYTEVECTOR (1, bv);
720
721 c_len = SCM_BYTEVECTOR_LENGTH (bv);
722 c_bv = (unsigned char *) SCM_BYTEVECTOR_CONTENTS (bv);
723
724 lst = scm_make_list (scm_from_uint (c_len), SCM_UNSPECIFIED);
725 for (i = 0, pair = lst;
726 i < c_len;
727 i++, pair = SCM_CDR (pair))
728 {
729 SCM_SETCAR (pair, SCM_I_MAKINUM (c_bv[i]));
730 }
731
732 return lst;
733}
734#undef FUNC_NAME
735
736SCM_DEFINE (scm_u8_list_to_bytevector, "u8-list->bytevector", 1, 0, 0,
737 (SCM lst),
738 "Turn @var{lst}, a list of octets, into a bytevector.")
739#define FUNC_NAME s_scm_u8_list_to_bytevector
740{
741 SCM bv, item;
742 long c_len, i;
743 unsigned char *c_bv;
744
745 SCM_VALIDATE_LIST_COPYLEN (1, lst, c_len);
746
e286c973 747 bv = make_bytevector (c_len, SCM_ARRAY_ELEMENT_TYPE_VU8);
1ee2c72e
LC
748 c_bv = (unsigned char *) SCM_BYTEVECTOR_CONTENTS (bv);
749
750 for (i = 0; i < c_len; lst = SCM_CDR (lst), i++)
751 {
752 item = SCM_CAR (lst);
753
754 if (SCM_LIKELY (SCM_I_INUMP (item)))
755 {
e25f3727 756 scm_t_signed_bits c_item;
1ee2c72e
LC
757
758 c_item = SCM_I_INUM (item);
759 if (SCM_LIKELY ((c_item >= 0) && (c_item < 256)))
760 c_bv[i] = (unsigned char) c_item;
761 else
762 goto type_error;
763 }
764 else
765 goto type_error;
766 }
767
768 return bv;
769
770 type_error:
771 scm_wrong_type_arg (FUNC_NAME, 1, item);
772
773 return SCM_BOOL_F;
774}
775#undef FUNC_NAME
776
777/* Compute the two's complement of VALUE (a positive integer) on SIZE octets
778 using (2^(SIZE * 8) - VALUE). */
779static inline void
780twos_complement (mpz_t value, size_t size)
781{
782 unsigned long bit_count;
783
784 /* We expect BIT_COUNT to fit in a unsigned long thanks to the range
785 checking on SIZE performed earlier. */
786 bit_count = (unsigned long) size << 3UL;
787
788 if (SCM_LIKELY (bit_count < sizeof (unsigned long)))
789 mpz_ui_sub (value, 1UL << bit_count, value);
790 else
791 {
792 mpz_t max;
793
794 mpz_init (max);
795 mpz_ui_pow_ui (max, 2, bit_count);
796 mpz_sub (value, max, value);
797 mpz_clear (max);
798 }
799}
800
801static inline SCM
802bytevector_large_ref (const char *c_bv, size_t c_size, int signed_p,
803 SCM endianness)
804{
805 SCM result;
806 mpz_t c_mpz;
807 int c_endianness, negative_p = 0;
808
809 if (signed_p)
810 {
811 if (scm_is_eq (endianness, scm_sym_big))
812 negative_p = c_bv[0] & 0x80;
813 else
814 negative_p = c_bv[c_size - 1] & 0x80;
815 }
816
817 c_endianness = scm_is_eq (endianness, scm_sym_big) ? 1 : -1;
818
819 mpz_init (c_mpz);
820 mpz_import (c_mpz, 1 /* 1 word */, 1 /* word order doesn't matter */,
821 c_size /* word is C_SIZE-byte long */,
822 c_endianness,
823 0 /* nails */, c_bv);
824
825 if (signed_p && negative_p)
826 {
827 twos_complement (c_mpz, c_size);
828 mpz_neg (c_mpz, c_mpz);
829 }
830
831 result = scm_from_mpz (c_mpz);
832 mpz_clear (c_mpz); /* FIXME: Needed? */
833
834 return result;
835}
836
837static inline int
838bytevector_large_set (char *c_bv, size_t c_size, int signed_p,
839 SCM value, SCM endianness)
840{
841 mpz_t c_mpz;
842 int c_endianness, c_sign, err = 0;
843
844 c_endianness = scm_is_eq (endianness, scm_sym_big) ? 1 : -1;
845
846 mpz_init (c_mpz);
847 scm_to_mpz (value, c_mpz);
848
849 c_sign = mpz_sgn (c_mpz);
850 if (c_sign < 0)
851 {
852 if (SCM_LIKELY (signed_p))
853 {
854 mpz_neg (c_mpz, c_mpz);
855 twos_complement (c_mpz, c_size);
856 }
857 else
858 {
859 err = -1;
860 goto finish;
861 }
862 }
863
864 if (c_sign == 0)
865 /* Zero. */
866 memset (c_bv, 0, c_size);
867 else
868 {
869 size_t word_count, value_size;
870
871 value_size = (mpz_sizeinbase (c_mpz, 2) + (8 * c_size)) / (8 * c_size);
872 if (SCM_UNLIKELY (value_size > c_size))
873 {
874 err = -2;
875 goto finish;
876 }
877
878
879 mpz_export (c_bv, &word_count, 1 /* word order doesn't matter */,
880 c_size, c_endianness,
881 0 /* nails */, c_mpz);
882 if (SCM_UNLIKELY (word_count != 1))
883 /* Shouldn't happen since we already checked with VALUE_SIZE. */
884 abort ();
885 }
886
887 finish:
888 mpz_clear (c_mpz);
889
890 return err;
891}
892
893#define GENERIC_INTEGER_ACCESSOR_PROLOGUE(_sign) \
894 unsigned long c_len, c_index, c_size; \
895 char *c_bv; \
896 \
897 SCM_VALIDATE_BYTEVECTOR (1, bv); \
898 c_index = scm_to_ulong (index); \
899 c_size = scm_to_ulong (size); \
900 \
901 c_len = SCM_BYTEVECTOR_LENGTH (bv); \
902 c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
903 \
904 /* C_SIZE must have its 3 higher bits set to zero so that \
905 multiplying it by 8 yields a number that fits in an \
906 unsigned long. */ \
907 if (SCM_UNLIKELY ((c_size == 0) || (c_size >= (ULONG_MAX >> 3L)))) \
908 scm_out_of_range (FUNC_NAME, size); \
909 if (SCM_UNLIKELY (c_index + c_size > c_len)) \
910 scm_out_of_range (FUNC_NAME, index);
911
912
913/* Template of an integer reference function. */
914#define GENERIC_INTEGER_REF(_sign) \
915 SCM result; \
916 \
917 if (c_size < 3) \
918 { \
919 int swap; \
920 _sign int value; \
921 \
caa92f5e 922 swap = !scm_is_eq (endianness, scm_i_native_endianness); \
1ee2c72e
LC
923 switch (c_size) \
924 { \
925 case 1: \
926 { \
927 _sign char c_value8; \
928 memcpy (&c_value8, c_bv, 1); \
929 value = c_value8; \
930 } \
931 break; \
932 case 2: \
933 { \
934 INT_TYPE (16, _sign) c_value16; \
935 memcpy (&c_value16, c_bv, 2); \
936 if (swap) \
937 value = (INT_TYPE (16, _sign)) bswap_16 (c_value16); \
938 else \
939 value = c_value16; \
940 } \
941 break; \
942 default: \
943 abort (); \
944 } \
945 \
946 result = SCM_I_MAKINUM ((_sign int) value); \
947 } \
948 else \
949 result = bytevector_large_ref ((char *) c_bv, \
950 c_size, SIGNEDNESS (_sign), \
951 endianness); \
952 \
953 return result;
954
955static inline SCM
956bytevector_signed_ref (const char *c_bv, size_t c_size, SCM endianness)
957{
958 GENERIC_INTEGER_REF (signed);
959}
960
961static inline SCM
962bytevector_unsigned_ref (const char *c_bv, size_t c_size, SCM endianness)
963{
964 GENERIC_INTEGER_REF (unsigned);
965}
966
967
968/* Template of an integer assignment function. */
969#define GENERIC_INTEGER_SET(_sign) \
970 if (c_size < 3) \
971 { \
e25f3727 972 scm_t_signed_bits c_value; \
1ee2c72e
LC
973 \
974 if (SCM_UNLIKELY (!SCM_I_INUMP (value))) \
975 goto range_error; \
976 \
977 c_value = SCM_I_INUM (value); \
978 switch (c_size) \
979 { \
980 case 1: \
981 if (SCM_LIKELY (INT_VALID_P (8, _sign) (c_value))) \
982 { \
983 _sign char c_value8; \
984 c_value8 = (_sign char) c_value; \
985 memcpy (c_bv, &c_value8, 1); \
986 } \
987 else \
988 goto range_error; \
989 break; \
990 \
991 case 2: \
992 if (SCM_LIKELY (INT_VALID_P (16, _sign) (c_value))) \
993 { \
994 int swap; \
995 INT_TYPE (16, _sign) c_value16; \
996 \
caa92f5e 997 swap = !scm_is_eq (endianness, scm_i_native_endianness); \
1ee2c72e
LC
998 \
999 if (swap) \
1000 c_value16 = (INT_TYPE (16, _sign)) bswap_16 (c_value); \
1001 else \
1002 c_value16 = c_value; \
1003 \
1004 memcpy (c_bv, &c_value16, 2); \
1005 } \
1006 else \
1007 goto range_error; \
1008 break; \
1009 \
1010 default: \
1011 abort (); \
1012 } \
1013 } \
1014 else \
1015 { \
1016 int err; \
1017 \
1018 err = bytevector_large_set (c_bv, c_size, \
1019 SIGNEDNESS (_sign), \
1020 value, endianness); \
1021 if (err) \
1022 goto range_error; \
1023 } \
1024 \
1025 return; \
1026 \
1027 range_error: \
1028 scm_out_of_range (FUNC_NAME, value); \
1029 return;
1030
1031static inline void
1032bytevector_signed_set (char *c_bv, size_t c_size,
1033 SCM value, SCM endianness,
1034 const char *func_name)
1035#define FUNC_NAME func_name
1036{
1037 GENERIC_INTEGER_SET (signed);
1038}
1039#undef FUNC_NAME
1040
1041static inline void
1042bytevector_unsigned_set (char *c_bv, size_t c_size,
1043 SCM value, SCM endianness,
1044 const char *func_name)
1045#define FUNC_NAME func_name
1046{
1047 GENERIC_INTEGER_SET (unsigned);
1048}
1049#undef FUNC_NAME
1050
1051#undef GENERIC_INTEGER_SET
1052#undef GENERIC_INTEGER_REF
1053
1054
1055SCM_DEFINE (scm_bytevector_uint_ref, "bytevector-uint-ref", 4, 0, 0,
1056 (SCM bv, SCM index, SCM endianness, SCM size),
1057 "Return the @var{size}-octet long unsigned integer at index "
1058 "@var{index} in @var{bv}.")
1059#define FUNC_NAME s_scm_bytevector_uint_ref
1060{
1061 GENERIC_INTEGER_ACCESSOR_PROLOGUE (unsigned);
1062
1063 return (bytevector_unsigned_ref (&c_bv[c_index], c_size, endianness));
1064}
1065#undef FUNC_NAME
1066
1067SCM_DEFINE (scm_bytevector_sint_ref, "bytevector-sint-ref", 4, 0, 0,
1068 (SCM bv, SCM index, SCM endianness, SCM size),
1069 "Return the @var{size}-octet long unsigned integer at index "
1070 "@var{index} in @var{bv}.")
1071#define FUNC_NAME s_scm_bytevector_sint_ref
1072{
1073 GENERIC_INTEGER_ACCESSOR_PROLOGUE (signed);
1074
1075 return (bytevector_signed_ref (&c_bv[c_index], c_size, endianness));
1076}
1077#undef FUNC_NAME
1078
1079SCM_DEFINE (scm_bytevector_uint_set_x, "bytevector-uint-set!", 5, 0, 0,
1080 (SCM bv, SCM index, SCM value, SCM endianness, SCM size),
1081 "Set the @var{size}-octet long unsigned integer at @var{index} "
1082 "to @var{value}.")
1083#define FUNC_NAME s_scm_bytevector_uint_set_x
1084{
1085 GENERIC_INTEGER_ACCESSOR_PROLOGUE (unsigned);
1086
1087 bytevector_unsigned_set (&c_bv[c_index], c_size, value, endianness,
1088 FUNC_NAME);
1089
1090 return SCM_UNSPECIFIED;
1091}
1092#undef FUNC_NAME
1093
1094SCM_DEFINE (scm_bytevector_sint_set_x, "bytevector-sint-set!", 5, 0, 0,
1095 (SCM bv, SCM index, SCM value, SCM endianness, SCM size),
1096 "Set the @var{size}-octet long signed integer at @var{index} "
1097 "to @var{value}.")
1098#define FUNC_NAME s_scm_bytevector_sint_set_x
1099{
1100 GENERIC_INTEGER_ACCESSOR_PROLOGUE (signed);
1101
1102 bytevector_signed_set (&c_bv[c_index], c_size, value, endianness,
1103 FUNC_NAME);
1104
1105 return SCM_UNSPECIFIED;
1106}
1107#undef FUNC_NAME
1108
1109
1110\f
1111/* Operations on integers of arbitrary size. */
1112
1113#define INTEGERS_TO_LIST(_sign) \
1114 SCM lst, pair; \
1115 size_t i, c_len, c_size; \
1116 \
1117 SCM_VALIDATE_BYTEVECTOR (1, bv); \
1118 SCM_VALIDATE_SYMBOL (2, endianness); \
088cfb7d 1119 c_size = scm_to_unsigned_integer (size, 1, (size_t) -1); \
1ee2c72e
LC
1120 \
1121 c_len = SCM_BYTEVECTOR_LENGTH (bv); \
088cfb7d 1122 if (SCM_UNLIKELY (c_len < c_size)) \
1ee2c72e 1123 lst = SCM_EOL; \
1ee2c72e
LC
1124 else \
1125 { \
1126 const char *c_bv; \
1127 \
1128 c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
1129 \
088cfb7d 1130 lst = scm_make_list (scm_from_size_t (c_len / c_size), \
1ee2c72e
LC
1131 SCM_UNSPECIFIED); \
1132 for (i = 0, pair = lst; \
1133 i <= c_len - c_size; \
1134 i += c_size, c_bv += c_size, pair = SCM_CDR (pair)) \
1135 { \
1136 SCM_SETCAR (pair, \
1137 bytevector_ ## _sign ## _ref (c_bv, c_size, \
1138 endianness)); \
1139 } \
1140 } \
1141 \
1142 return lst;
1143
1144SCM_DEFINE (scm_bytevector_to_sint_list, "bytevector->sint-list",
1145 3, 0, 0,
1146 (SCM bv, SCM endianness, SCM size),
1147 "Return a list of signed integers of @var{size} octets "
1148 "representing the contents of @var{bv}.")
1149#define FUNC_NAME s_scm_bytevector_to_sint_list
1150{
1151 INTEGERS_TO_LIST (signed);
1152}
1153#undef FUNC_NAME
1154
1155SCM_DEFINE (scm_bytevector_to_uint_list, "bytevector->uint-list",
1156 3, 0, 0,
1157 (SCM bv, SCM endianness, SCM size),
1158 "Return a list of unsigned integers of @var{size} octets "
1159 "representing the contents of @var{bv}.")
1160#define FUNC_NAME s_scm_bytevector_to_uint_list
1161{
1162 INTEGERS_TO_LIST (unsigned);
1163}
1164#undef FUNC_NAME
1165
1166#undef INTEGER_TO_LIST
1167
1168
1169#define INTEGER_LIST_TO_BYTEVECTOR(_sign) \
1170 SCM bv; \
1171 long c_len; \
1172 size_t c_size; \
1173 char *c_bv, *c_bv_ptr; \
1174 \
1175 SCM_VALIDATE_LIST_COPYLEN (1, lst, c_len); \
1176 SCM_VALIDATE_SYMBOL (2, endianness); \
1177 c_size = scm_to_uint (size); \
1178 \
1179 if (SCM_UNLIKELY ((c_size == 0) || (c_size >= (ULONG_MAX >> 3L)))) \
1180 scm_out_of_range (FUNC_NAME, size); \
1181 \
e286c973 1182 bv = make_bytevector (c_len * c_size, SCM_ARRAY_ELEMENT_TYPE_VU8); \
1ee2c72e
LC
1183 c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
1184 \
1185 for (c_bv_ptr = c_bv; \
1186 !scm_is_null (lst); \
1187 lst = SCM_CDR (lst), c_bv_ptr += c_size) \
1188 { \
1189 bytevector_ ## _sign ## _set (c_bv_ptr, c_size, \
1190 SCM_CAR (lst), endianness, \
1191 FUNC_NAME); \
1192 } \
1193 \
1194 return bv;
1195
1196
1197SCM_DEFINE (scm_uint_list_to_bytevector, "uint-list->bytevector",
1198 3, 0, 0,
1199 (SCM lst, SCM endianness, SCM size),
1200 "Return a bytevector containing the unsigned integers "
1201 "listed in @var{lst} and encoded on @var{size} octets "
1202 "according to @var{endianness}.")
1203#define FUNC_NAME s_scm_uint_list_to_bytevector
1204{
1205 INTEGER_LIST_TO_BYTEVECTOR (unsigned);
1206}
1207#undef FUNC_NAME
1208
1209SCM_DEFINE (scm_sint_list_to_bytevector, "sint-list->bytevector",
1210 3, 0, 0,
1211 (SCM lst, SCM endianness, SCM size),
1212 "Return a bytevector containing the signed integers "
1213 "listed in @var{lst} and encoded on @var{size} octets "
1214 "according to @var{endianness}.")
1215#define FUNC_NAME s_scm_sint_list_to_bytevector
1216{
1217 INTEGER_LIST_TO_BYTEVECTOR (signed);
1218}
1219#undef FUNC_NAME
1220
1221#undef INTEGER_LIST_TO_BYTEVECTOR
1222
1223
1224\f
1225/* Operations on 16-bit integers. */
1226
1227SCM_DEFINE (scm_bytevector_u16_ref, "bytevector-u16-ref",
1228 3, 0, 0,
1229 (SCM bv, SCM index, SCM endianness),
1230 "Return the unsigned 16-bit integer from @var{bv} at "
1231 "@var{index}.")
1232#define FUNC_NAME s_scm_bytevector_u16_ref
1233{
1234 INTEGER_REF (16, unsigned);
1235}
1236#undef FUNC_NAME
1237
1238SCM_DEFINE (scm_bytevector_s16_ref, "bytevector-s16-ref",
1239 3, 0, 0,
1240 (SCM bv, SCM index, SCM endianness),
1241 "Return the signed 16-bit integer from @var{bv} at "
1242 "@var{index}.")
1243#define FUNC_NAME s_scm_bytevector_s16_ref
1244{
1245 INTEGER_REF (16, signed);
1246}
1247#undef FUNC_NAME
1248
1249SCM_DEFINE (scm_bytevector_u16_native_ref, "bytevector-u16-native-ref",
1250 2, 0, 0,
1251 (SCM bv, SCM index),
1252 "Return the unsigned 16-bit integer from @var{bv} at "
1253 "@var{index} using the native endianness.")
1254#define FUNC_NAME s_scm_bytevector_u16_native_ref
1255{
1256 INTEGER_NATIVE_REF (16, unsigned);
1257}
1258#undef FUNC_NAME
1259
1260SCM_DEFINE (scm_bytevector_s16_native_ref, "bytevector-s16-native-ref",
1261 2, 0, 0,
1262 (SCM bv, SCM index),
1263 "Return the unsigned 16-bit integer from @var{bv} at "
1264 "@var{index} using the native endianness.")
1265#define FUNC_NAME s_scm_bytevector_s16_native_ref
1266{
1267 INTEGER_NATIVE_REF (16, signed);
1268}
1269#undef FUNC_NAME
1270
1271SCM_DEFINE (scm_bytevector_u16_set_x, "bytevector-u16-set!",
1272 4, 0, 0,
1273 (SCM bv, SCM index, SCM value, SCM endianness),
1274 "Store @var{value} in @var{bv} at @var{index} according to "
1275 "@var{endianness}.")
1276#define FUNC_NAME s_scm_bytevector_u16_set_x
1277{
1278 INTEGER_SET (16, unsigned);
1279}
1280#undef FUNC_NAME
1281
1282SCM_DEFINE (scm_bytevector_s16_set_x, "bytevector-s16-set!",
1283 4, 0, 0,
1284 (SCM bv, SCM index, SCM value, SCM endianness),
1285 "Store @var{value} in @var{bv} at @var{index} according to "
1286 "@var{endianness}.")
1287#define FUNC_NAME s_scm_bytevector_s16_set_x
1288{
1289 INTEGER_SET (16, signed);
1290}
1291#undef FUNC_NAME
1292
1293SCM_DEFINE (scm_bytevector_u16_native_set_x, "bytevector-u16-native-set!",
1294 3, 0, 0,
1295 (SCM bv, SCM index, SCM value),
1296 "Store the unsigned integer @var{value} at index @var{index} "
1297 "of @var{bv} using the native endianness.")
1298#define FUNC_NAME s_scm_bytevector_u16_native_set_x
1299{
1300 INTEGER_NATIVE_SET (16, unsigned);
1301}
1302#undef FUNC_NAME
1303
1304SCM_DEFINE (scm_bytevector_s16_native_set_x, "bytevector-s16-native-set!",
1305 3, 0, 0,
1306 (SCM bv, SCM index, SCM value),
1307 "Store the signed integer @var{value} at index @var{index} "
1308 "of @var{bv} using the native endianness.")
1309#define FUNC_NAME s_scm_bytevector_s16_native_set_x
1310{
1311 INTEGER_NATIVE_SET (16, signed);
1312}
1313#undef FUNC_NAME
1314
1315
1316\f
1317/* Operations on 32-bit integers. */
1318
1319/* Unfortunately, on 32-bit machines `SCM' is not large enough to hold
1320 arbitrary 32-bit integers. Thus we fall back to using the
1321 `large_{ref,set}' variants on 32-bit machines. */
1322
1323#define LARGE_INTEGER_REF(_len, _sign) \
1324 INTEGER_ACCESSOR_PROLOGUE(_len, _sign); \
1325 SCM_VALIDATE_SYMBOL (3, endianness); \
1326 \
1327 return (bytevector_large_ref ((char *) c_bv + c_index, _len / 8, \
1328 SIGNEDNESS (_sign), endianness));
1329
1330#define LARGE_INTEGER_SET(_len, _sign) \
1331 int err; \
1332 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
1333 SCM_VALIDATE_SYMBOL (4, endianness); \
1334 \
1335 err = bytevector_large_set ((char *) c_bv + c_index, _len / 8, \
1336 SIGNEDNESS (_sign), value, endianness); \
1337 if (SCM_UNLIKELY (err)) \
1338 scm_out_of_range (FUNC_NAME, value); \
1339 \
1340 return SCM_UNSPECIFIED;
1341
1342#define LARGE_INTEGER_NATIVE_REF(_len, _sign) \
1343 INTEGER_ACCESSOR_PROLOGUE(_len, _sign); \
1344 return (bytevector_large_ref ((char *) c_bv + c_index, _len / 8, \
caa92f5e 1345 SIGNEDNESS (_sign), scm_i_native_endianness));
1ee2c72e
LC
1346
1347#define LARGE_INTEGER_NATIVE_SET(_len, _sign) \
1348 int err; \
1349 INTEGER_ACCESSOR_PROLOGUE (_len, _sign); \
1350 \
1351 err = bytevector_large_set ((char *) c_bv + c_index, _len / 8, \
1352 SIGNEDNESS (_sign), value, \
caa92f5e 1353 scm_i_native_endianness); \
1ee2c72e
LC
1354 if (SCM_UNLIKELY (err)) \
1355 scm_out_of_range (FUNC_NAME, value); \
1356 \
1357 return SCM_UNSPECIFIED;
1358
1359
1360SCM_DEFINE (scm_bytevector_u32_ref, "bytevector-u32-ref",
1361 3, 0, 0,
1362 (SCM bv, SCM index, SCM endianness),
1363 "Return the unsigned 32-bit integer from @var{bv} at "
1364 "@var{index}.")
1365#define FUNC_NAME s_scm_bytevector_u32_ref
1366{
1367#if SIZEOF_VOID_P > 4
1368 INTEGER_REF (32, unsigned);
1369#else
1370 LARGE_INTEGER_REF (32, unsigned);
1371#endif
1372}
1373#undef FUNC_NAME
1374
1375SCM_DEFINE (scm_bytevector_s32_ref, "bytevector-s32-ref",
1376 3, 0, 0,
1377 (SCM bv, SCM index, SCM endianness),
1378 "Return the signed 32-bit integer from @var{bv} at "
1379 "@var{index}.")
1380#define FUNC_NAME s_scm_bytevector_s32_ref
1381{
1382#if SIZEOF_VOID_P > 4
1383 INTEGER_REF (32, signed);
1384#else
1385 LARGE_INTEGER_REF (32, signed);
1386#endif
1387}
1388#undef FUNC_NAME
1389
1390SCM_DEFINE (scm_bytevector_u32_native_ref, "bytevector-u32-native-ref",
1391 2, 0, 0,
1392 (SCM bv, SCM index),
1393 "Return the unsigned 32-bit integer from @var{bv} at "
1394 "@var{index} using the native endianness.")
1395#define FUNC_NAME s_scm_bytevector_u32_native_ref
1396{
1397#if SIZEOF_VOID_P > 4
1398 INTEGER_NATIVE_REF (32, unsigned);
1399#else
1400 LARGE_INTEGER_NATIVE_REF (32, unsigned);
1401#endif
1402}
1403#undef FUNC_NAME
1404
1405SCM_DEFINE (scm_bytevector_s32_native_ref, "bytevector-s32-native-ref",
1406 2, 0, 0,
1407 (SCM bv, SCM index),
1408 "Return the unsigned 32-bit integer from @var{bv} at "
1409 "@var{index} using the native endianness.")
1410#define FUNC_NAME s_scm_bytevector_s32_native_ref
1411{
1412#if SIZEOF_VOID_P > 4
1413 INTEGER_NATIVE_REF (32, signed);
1414#else
1415 LARGE_INTEGER_NATIVE_REF (32, signed);
1416#endif
1417}
1418#undef FUNC_NAME
1419
1420SCM_DEFINE (scm_bytevector_u32_set_x, "bytevector-u32-set!",
1421 4, 0, 0,
1422 (SCM bv, SCM index, SCM value, SCM endianness),
1423 "Store @var{value} in @var{bv} at @var{index} according to "
1424 "@var{endianness}.")
1425#define FUNC_NAME s_scm_bytevector_u32_set_x
1426{
1427#if SIZEOF_VOID_P > 4
1428 INTEGER_SET (32, unsigned);
1429#else
1430 LARGE_INTEGER_SET (32, unsigned);
1431#endif
1432}
1433#undef FUNC_NAME
1434
1435SCM_DEFINE (scm_bytevector_s32_set_x, "bytevector-s32-set!",
1436 4, 0, 0,
1437 (SCM bv, SCM index, SCM value, SCM endianness),
1438 "Store @var{value} in @var{bv} at @var{index} according to "
1439 "@var{endianness}.")
1440#define FUNC_NAME s_scm_bytevector_s32_set_x
1441{
1442#if SIZEOF_VOID_P > 4
1443 INTEGER_SET (32, signed);
1444#else
1445 LARGE_INTEGER_SET (32, signed);
1446#endif
1447}
1448#undef FUNC_NAME
1449
1450SCM_DEFINE (scm_bytevector_u32_native_set_x, "bytevector-u32-native-set!",
1451 3, 0, 0,
1452 (SCM bv, SCM index, SCM value),
1453 "Store the unsigned integer @var{value} at index @var{index} "
1454 "of @var{bv} using the native endianness.")
1455#define FUNC_NAME s_scm_bytevector_u32_native_set_x
1456{
1457#if SIZEOF_VOID_P > 4
1458 INTEGER_NATIVE_SET (32, unsigned);
1459#else
1460 LARGE_INTEGER_NATIVE_SET (32, unsigned);
1461#endif
1462}
1463#undef FUNC_NAME
1464
1465SCM_DEFINE (scm_bytevector_s32_native_set_x, "bytevector-s32-native-set!",
1466 3, 0, 0,
1467 (SCM bv, SCM index, SCM value),
1468 "Store the signed integer @var{value} at index @var{index} "
1469 "of @var{bv} using the native endianness.")
1470#define FUNC_NAME s_scm_bytevector_s32_native_set_x
1471{
1472#if SIZEOF_VOID_P > 4
1473 INTEGER_NATIVE_SET (32, signed);
1474#else
1475 LARGE_INTEGER_NATIVE_SET (32, signed);
1476#endif
1477}
1478#undef FUNC_NAME
1479
1480
1481\f
1482/* Operations on 64-bit integers. */
1483
1484/* For 64-bit integers, we use only the `large_{ref,set}' variant. */
1485
1486SCM_DEFINE (scm_bytevector_u64_ref, "bytevector-u64-ref",
1487 3, 0, 0,
1488 (SCM bv, SCM index, SCM endianness),
1489 "Return the unsigned 64-bit integer from @var{bv} at "
1490 "@var{index}.")
1491#define FUNC_NAME s_scm_bytevector_u64_ref
1492{
1493 LARGE_INTEGER_REF (64, unsigned);
1494}
1495#undef FUNC_NAME
1496
1497SCM_DEFINE (scm_bytevector_s64_ref, "bytevector-s64-ref",
1498 3, 0, 0,
1499 (SCM bv, SCM index, SCM endianness),
1500 "Return the signed 64-bit integer from @var{bv} at "
1501 "@var{index}.")
1502#define FUNC_NAME s_scm_bytevector_s64_ref
1503{
1504 LARGE_INTEGER_REF (64, signed);
1505}
1506#undef FUNC_NAME
1507
1508SCM_DEFINE (scm_bytevector_u64_native_ref, "bytevector-u64-native-ref",
1509 2, 0, 0,
1510 (SCM bv, SCM index),
1511 "Return the unsigned 64-bit integer from @var{bv} at "
1512 "@var{index} using the native endianness.")
1513#define FUNC_NAME s_scm_bytevector_u64_native_ref
1514{
1515 LARGE_INTEGER_NATIVE_REF (64, unsigned);
1516}
1517#undef FUNC_NAME
1518
1519SCM_DEFINE (scm_bytevector_s64_native_ref, "bytevector-s64-native-ref",
1520 2, 0, 0,
1521 (SCM bv, SCM index),
1522 "Return the unsigned 64-bit integer from @var{bv} at "
1523 "@var{index} using the native endianness.")
1524#define FUNC_NAME s_scm_bytevector_s64_native_ref
1525{
1526 LARGE_INTEGER_NATIVE_REF (64, signed);
1527}
1528#undef FUNC_NAME
1529
1530SCM_DEFINE (scm_bytevector_u64_set_x, "bytevector-u64-set!",
1531 4, 0, 0,
1532 (SCM bv, SCM index, SCM value, SCM endianness),
1533 "Store @var{value} in @var{bv} at @var{index} according to "
1534 "@var{endianness}.")
1535#define FUNC_NAME s_scm_bytevector_u64_set_x
1536{
1537 LARGE_INTEGER_SET (64, unsigned);
1538}
1539#undef FUNC_NAME
1540
1541SCM_DEFINE (scm_bytevector_s64_set_x, "bytevector-s64-set!",
1542 4, 0, 0,
1543 (SCM bv, SCM index, SCM value, SCM endianness),
1544 "Store @var{value} in @var{bv} at @var{index} according to "
1545 "@var{endianness}.")
1546#define FUNC_NAME s_scm_bytevector_s64_set_x
1547{
1548 LARGE_INTEGER_SET (64, signed);
1549}
1550#undef FUNC_NAME
1551
1552SCM_DEFINE (scm_bytevector_u64_native_set_x, "bytevector-u64-native-set!",
1553 3, 0, 0,
1554 (SCM bv, SCM index, SCM value),
1555 "Store the unsigned integer @var{value} at index @var{index} "
1556 "of @var{bv} using the native endianness.")
1557#define FUNC_NAME s_scm_bytevector_u64_native_set_x
1558{
1559 LARGE_INTEGER_NATIVE_SET (64, unsigned);
1560}
1561#undef FUNC_NAME
1562
1563SCM_DEFINE (scm_bytevector_s64_native_set_x, "bytevector-s64-native-set!",
1564 3, 0, 0,
1565 (SCM bv, SCM index, SCM value),
1566 "Store the signed integer @var{value} at index @var{index} "
1567 "of @var{bv} using the native endianness.")
1568#define FUNC_NAME s_scm_bytevector_s64_native_set_x
1569{
1570 LARGE_INTEGER_NATIVE_SET (64, signed);
1571}
1572#undef FUNC_NAME
1573
1574
1575\f
1576/* Operations on IEEE-754 numbers. */
1577
1578/* There are two possible word endians, visible in glibc's <ieee754.h>.
1579 However, in R6RS, when the endianness is `little', little endian is
1580 assumed for both the byte order and the word order. This is clear from
1581 Section 2.1 of R6RS-lib (in response to
1582 http://www.r6rs.org/formal-comments/comment-187.txt). */
1583
398446c7
LC
1584union scm_ieee754_float
1585{
1586 float f;
1587 scm_t_uint32 i;
1588};
1589
1590union scm_ieee754_double
1591{
1592 double d;
1593 scm_t_uint64 i;
1594};
1595
1ee2c72e
LC
1596
1597/* Convert to/from a floating-point number with different endianness. This
1598 method is probably not the most efficient but it should be portable. */
1599
1600static inline void
1601float_to_foreign_endianness (union scm_ieee754_float *target,
1602 float source)
1603{
398446c7 1604 union scm_ieee754_float input;
1ee2c72e 1605
398446c7
LC
1606 input.f = source;
1607 target->i = bswap_32 (input.i);
1ee2c72e
LC
1608}
1609
1610static inline float
1611float_from_foreign_endianness (const union scm_ieee754_float *source)
1612{
1613 union scm_ieee754_float result;
1614
398446c7 1615 result.i = bswap_32 (source->i);
1ee2c72e
LC
1616
1617 return (result.f);
1618}
1619
1620static inline void
1621double_to_foreign_endianness (union scm_ieee754_double *target,
1622 double source)
1623{
398446c7 1624 union scm_ieee754_double input;
1ee2c72e 1625
398446c7
LC
1626 input.d = source;
1627 target->i = bswap_64 (input.i);
1ee2c72e
LC
1628}
1629
1630static inline double
1631double_from_foreign_endianness (const union scm_ieee754_double *source)
1632{
1633 union scm_ieee754_double result;
1634
398446c7 1635 result.i = bswap_64 (source->i);
1ee2c72e
LC
1636
1637 return (result.d);
1638}
1639
1640/* Template macros to abstract over doubles and floats.
1641 XXX: Guile can only convert to/from doubles. */
1642#define IEEE754_UNION(_c_type) union scm_ieee754_ ## _c_type
1643#define IEEE754_TO_SCM(_c_type) scm_from_double
1644#define IEEE754_FROM_SCM(_c_type) scm_to_double
1645#define IEEE754_FROM_FOREIGN_ENDIANNESS(_c_type) \
1646 _c_type ## _from_foreign_endianness
1647#define IEEE754_TO_FOREIGN_ENDIANNESS(_c_type) \
1648 _c_type ## _to_foreign_endianness
1649
1650
cd43fdc5
AW
1651/* FIXME: SCM_VALIDATE_REAL rejects integers, etc. grrr */
1652#define VALIDATE_REAL(pos, v) \
1653 do { \
ae255d65 1654 SCM_ASSERT_TYPE (scm_is_real (v), v, pos, FUNC_NAME, "real"); \
cd43fdc5
AW
1655 } while (0)
1656
1ee2c72e
LC
1657/* Templace getters and setters. */
1658
1659#define IEEE754_ACCESSOR_PROLOGUE(_type) \
1660 INTEGER_ACCESSOR_PROLOGUE (sizeof (_type) << 3UL, signed);
1661
1662#define IEEE754_REF(_type) \
1663 _type c_result; \
1664 \
1665 IEEE754_ACCESSOR_PROLOGUE (_type); \
1666 SCM_VALIDATE_SYMBOL (3, endianness); \
1667 \
caa92f5e 1668 if (scm_is_eq (endianness, scm_i_native_endianness)) \
1ee2c72e
LC
1669 memcpy (&c_result, &c_bv[c_index], sizeof (c_result)); \
1670 else \
1671 { \
1672 IEEE754_UNION (_type) c_raw; \
1673 \
1674 memcpy (&c_raw, &c_bv[c_index], sizeof (c_raw)); \
1675 c_result = \
1676 IEEE754_FROM_FOREIGN_ENDIANNESS (_type) (&c_raw); \
1677 } \
1678 \
1679 return (IEEE754_TO_SCM (_type) (c_result));
1680
1681#define IEEE754_NATIVE_REF(_type) \
1682 _type c_result; \
1683 \
1684 IEEE754_ACCESSOR_PROLOGUE (_type); \
1685 \
1686 memcpy (&c_result, &c_bv[c_index], sizeof (c_result)); \
1687 return (IEEE754_TO_SCM (_type) (c_result));
1688
1689#define IEEE754_SET(_type) \
1690 _type c_value; \
1691 \
1692 IEEE754_ACCESSOR_PROLOGUE (_type); \
cd43fdc5 1693 VALIDATE_REAL (3, value); \
1ee2c72e
LC
1694 SCM_VALIDATE_SYMBOL (4, endianness); \
1695 c_value = IEEE754_FROM_SCM (_type) (value); \
1696 \
caa92f5e 1697 if (scm_is_eq (endianness, scm_i_native_endianness)) \
1ee2c72e
LC
1698 memcpy (&c_bv[c_index], &c_value, sizeof (c_value)); \
1699 else \
1700 { \
1701 IEEE754_UNION (_type) c_raw; \
1702 \
1703 IEEE754_TO_FOREIGN_ENDIANNESS (_type) (&c_raw, c_value); \
1704 memcpy (&c_bv[c_index], &c_raw, sizeof (c_raw)); \
1705 } \
1706 \
1707 return SCM_UNSPECIFIED;
1708
1709#define IEEE754_NATIVE_SET(_type) \
1710 _type c_value; \
1711 \
1712 IEEE754_ACCESSOR_PROLOGUE (_type); \
cd43fdc5 1713 VALIDATE_REAL (3, value); \
1ee2c72e
LC
1714 c_value = IEEE754_FROM_SCM (_type) (value); \
1715 \
1716 memcpy (&c_bv[c_index], &c_value, sizeof (c_value)); \
1717 return SCM_UNSPECIFIED;
1718
1719
1720/* Single precision. */
1721
1722SCM_DEFINE (scm_bytevector_ieee_single_ref,
1723 "bytevector-ieee-single-ref",
1724 3, 0, 0,
1725 (SCM bv, SCM index, SCM endianness),
1726 "Return the IEEE-754 single from @var{bv} at "
1727 "@var{index}.")
1728#define FUNC_NAME s_scm_bytevector_ieee_single_ref
1729{
1730 IEEE754_REF (float);
1731}
1732#undef FUNC_NAME
1733
1734SCM_DEFINE (scm_bytevector_ieee_single_native_ref,
1735 "bytevector-ieee-single-native-ref",
1736 2, 0, 0,
1737 (SCM bv, SCM index),
1738 "Return the IEEE-754 single from @var{bv} at "
1739 "@var{index} using the native endianness.")
1740#define FUNC_NAME s_scm_bytevector_ieee_single_native_ref
1741{
1742 IEEE754_NATIVE_REF (float);
1743}
1744#undef FUNC_NAME
1745
1746SCM_DEFINE (scm_bytevector_ieee_single_set_x,
1747 "bytevector-ieee-single-set!",
1748 4, 0, 0,
1749 (SCM bv, SCM index, SCM value, SCM endianness),
1750 "Store real @var{value} in @var{bv} at @var{index} according to "
1751 "@var{endianness}.")
1752#define FUNC_NAME s_scm_bytevector_ieee_single_set_x
1753{
1754 IEEE754_SET (float);
1755}
1756#undef FUNC_NAME
1757
1758SCM_DEFINE (scm_bytevector_ieee_single_native_set_x,
1759 "bytevector-ieee-single-native-set!",
1760 3, 0, 0,
1761 (SCM bv, SCM index, SCM value),
1762 "Store the real @var{value} at index @var{index} "
1763 "of @var{bv} using the native endianness.")
1764#define FUNC_NAME s_scm_bytevector_ieee_single_native_set_x
1765{
1766 IEEE754_NATIVE_SET (float);
1767}
1768#undef FUNC_NAME
1769
1770
1771/* Double precision. */
1772
1773SCM_DEFINE (scm_bytevector_ieee_double_ref,
1774 "bytevector-ieee-double-ref",
1775 3, 0, 0,
1776 (SCM bv, SCM index, SCM endianness),
1777 "Return the IEEE-754 double from @var{bv} at "
1778 "@var{index}.")
1779#define FUNC_NAME s_scm_bytevector_ieee_double_ref
1780{
1781 IEEE754_REF (double);
1782}
1783#undef FUNC_NAME
1784
1785SCM_DEFINE (scm_bytevector_ieee_double_native_ref,
1786 "bytevector-ieee-double-native-ref",
1787 2, 0, 0,
1788 (SCM bv, SCM index),
1789 "Return the IEEE-754 double from @var{bv} at "
1790 "@var{index} using the native endianness.")
1791#define FUNC_NAME s_scm_bytevector_ieee_double_native_ref
1792{
1793 IEEE754_NATIVE_REF (double);
1794}
1795#undef FUNC_NAME
1796
1797SCM_DEFINE (scm_bytevector_ieee_double_set_x,
1798 "bytevector-ieee-double-set!",
1799 4, 0, 0,
1800 (SCM bv, SCM index, SCM value, SCM endianness),
1801 "Store real @var{value} in @var{bv} at @var{index} according to "
1802 "@var{endianness}.")
1803#define FUNC_NAME s_scm_bytevector_ieee_double_set_x
1804{
1805 IEEE754_SET (double);
1806}
1807#undef FUNC_NAME
1808
1809SCM_DEFINE (scm_bytevector_ieee_double_native_set_x,
1810 "bytevector-ieee-double-native-set!",
1811 3, 0, 0,
1812 (SCM bv, SCM index, SCM value),
1813 "Store the real @var{value} at index @var{index} "
1814 "of @var{bv} using the native endianness.")
1815#define FUNC_NAME s_scm_bytevector_ieee_double_native_set_x
1816{
1817 IEEE754_NATIVE_SET (double);
1818}
1819#undef FUNC_NAME
1820
1821
1822#undef IEEE754_UNION
1823#undef IEEE754_TO_SCM
1824#undef IEEE754_FROM_SCM
1825#undef IEEE754_FROM_FOREIGN_ENDIANNESS
1826#undef IEEE754_TO_FOREIGN_ENDIANNESS
1827#undef IEEE754_REF
1828#undef IEEE754_NATIVE_REF
1829#undef IEEE754_SET
1830#undef IEEE754_NATIVE_SET
1831
1832\f
1833/* Operations on strings. */
1834
1835
1836/* Produce a function that returns the length of a UTF-encoded string. */
1837#define UTF_STRLEN_FUNCTION(_utf_width) \
1838static inline size_t \
1839utf ## _utf_width ## _strlen (const uint ## _utf_width ## _t *str) \
1840{ \
1841 size_t len = 0; \
1842 const uint ## _utf_width ## _t *ptr; \
1843 for (ptr = str; \
1844 *ptr != 0; \
1845 ptr++) \
1846 { \
1847 len++; \
1848 } \
1849 \
1850 return (len * ((_utf_width) / 8)); \
1851}
1852
1853UTF_STRLEN_FUNCTION (8)
1854
1855
1856/* Return the length (in bytes) of STR, a UTF-(UTF_WIDTH) encoded string. */
1857#define UTF_STRLEN(_utf_width, _str) \
1858 utf ## _utf_width ## _strlen (_str)
1859
1860/* Return the "portable" name of the UTF encoding of size UTF_WIDTH and
1861 ENDIANNESS (Gnulib's `iconv_open' module guarantees the portability of the
1862 encoding name). */
1863static inline void
1864utf_encoding_name (char *name, size_t utf_width, SCM endianness)
1865{
1866 strcpy (name, "UTF-");
1867 strcat (name, ((utf_width == 8)
1868 ? "8"
1869 : ((utf_width == 16)
1870 ? "16"
1871 : ((utf_width == 32)
1872 ? "32"
1873 : "??"))));
1874 strcat (name,
1875 ((scm_is_eq (endianness, scm_sym_big))
1876 ? "BE"
1877 : ((scm_is_eq (endianness, scm_sym_little))
1878 ? "LE"
1879 : "unknown")));
1880}
1881
1882/* Maximum length of a UTF encoding name. */
1883#define MAX_UTF_ENCODING_NAME_LEN 16
1884
1885/* Produce the body of a `string->utf' function. */
3a5bc4fa
MG
1886#define STRING_TO_UTF(_utf_width) \
1887 SCM utf; \
1888 int err; \
1889 char c_utf_name[MAX_UTF_ENCODING_NAME_LEN]; \
1890 char *c_utf = NULL; \
1891 size_t c_strlen, c_utf_len = 0; \
1892 \
1893 SCM_VALIDATE_STRING (1, str); \
d223c3fc 1894 if (scm_is_eq (endianness, SCM_UNDEFINED)) \
3a5bc4fa
MG
1895 endianness = scm_sym_big; \
1896 else \
1897 SCM_VALIDATE_SYMBOL (2, endianness); \
1898 \
1899 utf_encoding_name (c_utf_name, (_utf_width), endianness); \
1900 \
1901 c_strlen = scm_i_string_length (str); \
1902 if (scm_i_is_narrow_string (str)) \
1903 { \
1904 err = mem_iconveh (scm_i_string_chars (str), c_strlen, \
1905 "ISO-8859-1", c_utf_name, \
1906 iconveh_question_mark, NULL, \
1907 &c_utf, &c_utf_len); \
1908 if (SCM_UNLIKELY (err)) \
1909 scm_syserror_msg (FUNC_NAME, "failed to convert string: ~A", \
1910 scm_list_1 (str), err); \
1911 } \
1912 else \
1913 { \
1914 const scm_t_wchar *wbuf = scm_i_string_wide_chars (str); \
1915 c_utf = u32_conv_to_encoding (c_utf_name, \
1916 iconveh_question_mark, \
1917 (scm_t_uint32 *) wbuf, \
1918 c_strlen, NULL, NULL, &c_utf_len); \
1919 if (SCM_UNLIKELY (c_utf == NULL)) \
1920 scm_syserror_msg (FUNC_NAME, "failed to convert string: ~A", \
1921 scm_list_1 (str), errno); \
1922 } \
1923 scm_dynwind_begin (0); \
1924 scm_dynwind_free (c_utf); \
1925 utf = make_bytevector (c_utf_len, SCM_ARRAY_ELEMENT_TYPE_VU8); \
1926 memcpy (SCM_BYTEVECTOR_CONTENTS (utf), c_utf, c_utf_len); \
1927 scm_dynwind_end (); \
1928 \
1929 return (utf);
1ee2c72e
LC
1930
1931
1932
1933SCM_DEFINE (scm_string_to_utf8, "string->utf8",
1934 1, 0, 0,
1935 (SCM str),
1936 "Return a newly allocated bytevector that contains the UTF-8 "
1937 "encoding of @var{str}.")
1938#define FUNC_NAME s_scm_string_to_utf8
1939{
1940 SCM utf;
c2c3bddb
AW
1941 scm_t_uint8 *c_utf;
1942 size_t c_utf_len = 0;
1ee2c72e
LC
1943
1944 SCM_VALIDATE_STRING (1, str);
1945
c2c3bddb
AW
1946 c_utf = (scm_t_uint8 *) scm_to_utf8_stringn (str, &c_utf_len);
1947 utf = make_bytevector (c_utf_len, SCM_ARRAY_ELEMENT_TYPE_VU8);
1948 memcpy (SCM_BYTEVECTOR_CONTENTS (utf), c_utf, c_utf_len);
1949 free (c_utf);
1ee2c72e
LC
1950
1951 return (utf);
1952}
1953#undef FUNC_NAME
1954
1955SCM_DEFINE (scm_string_to_utf16, "string->utf16",
1956 1, 1, 0,
1957 (SCM str, SCM endianness),
1958 "Return a newly allocated bytevector that contains the UTF-16 "
1959 "encoding of @var{str}.")
1960#define FUNC_NAME s_scm_string_to_utf16
1961{
1962 STRING_TO_UTF (16);
1963}
1964#undef FUNC_NAME
1965
c2c3bddb
AW
1966static void
1967swap_u32 (scm_t_wchar *vals, size_t len)
1968{
1969 size_t n;
1970 for (n = 0; n < len; n++)
1971 vals[n] = bswap_32 (vals[n]);
1972}
1973
1ee2c72e
LC
1974SCM_DEFINE (scm_string_to_utf32, "string->utf32",
1975 1, 1, 0,
1976 (SCM str, SCM endianness),
1977 "Return a newly allocated bytevector that contains the UTF-32 "
1978 "encoding of @var{str}.")
1979#define FUNC_NAME s_scm_string_to_utf32
1980{
c2c3bddb
AW
1981 SCM bv;
1982 scm_t_wchar *wchars;
1983 size_t wchar_len, bytes_len;
1984
1985 wchars = scm_to_utf32_stringn (str, &wchar_len);
1986 bytes_len = wchar_len * sizeof (scm_t_wchar);
1987 if (!scm_is_eq (SCM_UNBNDP (endianness) ? scm_endianness_big : endianness,
1988 scm_i_native_endianness))
1989 swap_u32 (wchars, wchar_len);
1990
1991 bv = make_bytevector (bytes_len, SCM_ARRAY_ELEMENT_TYPE_VU8);
1992 memcpy (SCM_BYTEVECTOR_CONTENTS (bv), wchars, bytes_len);
1993 free (wchars);
1994
1995 return bv;
1ee2c72e
LC
1996}
1997#undef FUNC_NAME
1998
1999
2000/* Produce the body of a function that converts a UTF-encoded bytevector to a
2001 string. */
2002#define UTF_TO_STRING(_utf_width) \
2003 SCM str = SCM_BOOL_F; \
2004 int err; \
3a5bc4fa 2005 char *c_str = NULL; \
1ee2c72e 2006 char c_utf_name[MAX_UTF_ENCODING_NAME_LEN]; \
3a5bc4fa
MG
2007 char *c_utf; \
2008 size_t c_strlen = 0, c_utf_len = 0; \
1ee2c72e
LC
2009 \
2010 SCM_VALIDATE_BYTEVECTOR (1, utf); \
d223c3fc 2011 if (scm_is_eq (endianness, SCM_UNDEFINED)) \
1ee2c72e
LC
2012 endianness = scm_sym_big; \
2013 else \
2014 SCM_VALIDATE_SYMBOL (2, endianness); \
2015 \
2016 c_utf_len = SCM_BYTEVECTOR_LENGTH (utf); \
2017 c_utf = (char *) SCM_BYTEVECTOR_CONTENTS (utf); \
2018 utf_encoding_name (c_utf_name, (_utf_width), endianness); \
2019 \
1ee2c72e 2020 err = mem_iconveh (c_utf, c_utf_len, \
3a5bc4fa 2021 c_utf_name, "UTF-8", \
1ee2c72e
LC
2022 iconveh_question_mark, NULL, \
2023 &c_str, &c_strlen); \
2024 if (SCM_UNLIKELY (err)) \
2025 scm_syserror_msg (FUNC_NAME, "failed to convert to string: ~A", \
2026 scm_list_1 (utf), err); \
2027 else \
3a5bc4fa 2028 { \
686df516 2029 str = scm_from_utf8_stringn (c_str, c_strlen); \
3a5bc4fa
MG
2030 free (c_str); \
2031 } \
1ee2c72e
LC
2032 return (str);
2033
2034
2035SCM_DEFINE (scm_utf8_to_string, "utf8->string",
2036 1, 0, 0,
2037 (SCM utf),
2038 "Return a newly allocate string that contains from the UTF-8-"
2039 "encoded contents of bytevector @var{utf}.")
2040#define FUNC_NAME s_scm_utf8_to_string
2041{
2042 SCM str;
1ee2c72e 2043 const char *c_utf;
3a5bc4fa 2044 size_t c_utf_len = 0;
1ee2c72e
LC
2045
2046 SCM_VALIDATE_BYTEVECTOR (1, utf);
2047
2048 c_utf_len = SCM_BYTEVECTOR_LENGTH (utf);
1ee2c72e 2049 c_utf = (char *) SCM_BYTEVECTOR_CONTENTS (utf);
8c76a897 2050 str = scm_from_utf8_stringn (c_utf, c_utf_len);
1ee2c72e
LC
2051
2052 return (str);
2053}
2054#undef FUNC_NAME
2055
2056SCM_DEFINE (scm_utf16_to_string, "utf16->string",
2057 1, 1, 0,
2058 (SCM utf, SCM endianness),
2059 "Return a newly allocate string that contains from the UTF-16-"
2060 "encoded contents of bytevector @var{utf}.")
2061#define FUNC_NAME s_scm_utf16_to_string
2062{
2063 UTF_TO_STRING (16);
2064}
2065#undef FUNC_NAME
2066
2067SCM_DEFINE (scm_utf32_to_string, "utf32->string",
2068 1, 1, 0,
2069 (SCM utf, SCM endianness),
2070 "Return a newly allocate string that contains from the UTF-32-"
2071 "encoded contents of bytevector @var{utf}.")
2072#define FUNC_NAME s_scm_utf32_to_string
2073{
2074 UTF_TO_STRING (32);
2075}
2076#undef FUNC_NAME
2077
1ee2c72e 2078\f
2a610be5
AW
2079/* Bytevectors as generalized vectors & arrays. */
2080
4bc95fcc
LC
2081#define COMPLEX_ACCESSOR_PROLOGUE(_type) \
2082 size_t c_len, c_index; \
2083 char *c_bv; \
2084 \
2085 SCM_VALIDATE_BYTEVECTOR (1, bv); \
2086 c_index = scm_to_size_t (index); \
2087 \
2088 c_len = SCM_BYTEVECTOR_LENGTH (bv); \
2089 c_bv = (char *) SCM_BYTEVECTOR_CONTENTS (bv); \
2090 \
2091 if (SCM_UNLIKELY (c_index + 2 * sizeof (_type) - 1 >= c_len)) \
2092 scm_out_of_range (FUNC_NAME, index);
e286c973 2093
4bc95fcc
LC
2094/* Template for native access to complex numbers of type TYPE. */
2095#define COMPLEX_NATIVE_REF(_type) \
2096 SCM result; \
2097 \
2098 COMPLEX_ACCESSOR_PROLOGUE (_type); \
2099 \
2100 { \
2101 _type real, imag; \
2102 \
2103 memcpy (&real, &c_bv[c_index], sizeof (_type)); \
2104 memcpy (&imag, &c_bv[c_index + sizeof (_type)], sizeof (_type)); \
2105 \
2106 result = scm_c_make_rectangular (real, imag); \
2107 } \
2108 \
2109 return result;
e286c973
AW
2110
2111static SCM
4bc95fcc
LC
2112bytevector_ref_c32 (SCM bv, SCM index)
2113#define FUNC_NAME "bytevector_ref_c32"
2114{
2115 COMPLEX_NATIVE_REF (float);
e286c973 2116}
4bc95fcc 2117#undef FUNC_NAME
e286c973
AW
2118
2119static SCM
4bc95fcc
LC
2120bytevector_ref_c64 (SCM bv, SCM index)
2121#define FUNC_NAME "bytevector_ref_c64"
2122{
2123 COMPLEX_NATIVE_REF (double);
e286c973 2124}
4bc95fcc 2125#undef FUNC_NAME
e286c973
AW
2126
2127typedef SCM (*scm_t_bytevector_ref_fn)(SCM, SCM);
2128
4bc95fcc
LC
2129static const scm_t_bytevector_ref_fn
2130bytevector_ref_fns[SCM_ARRAY_ELEMENT_TYPE_LAST + 1] =
e286c973
AW
2131{
2132 NULL, /* SCM */
2133 NULL, /* CHAR */
2134 NULL, /* BIT */
2135 scm_bytevector_u8_ref, /* VU8 */
2136 scm_bytevector_u8_ref, /* U8 */
2137 scm_bytevector_s8_ref,
2138 scm_bytevector_u16_native_ref,
2139 scm_bytevector_s16_native_ref,
2140 scm_bytevector_u32_native_ref,
2141 scm_bytevector_s32_native_ref,
2142 scm_bytevector_u64_native_ref,
2143 scm_bytevector_s64_native_ref,
2144 scm_bytevector_ieee_single_native_ref,
2145 scm_bytevector_ieee_double_native_ref,
2146 bytevector_ref_c32,
2147 bytevector_ref_c64
2148};
2149
2a610be5
AW
2150static SCM
2151bv_handle_ref (scm_t_array_handle *h, size_t index)
2152{
e286c973
AW
2153 SCM byte_index;
2154 scm_t_bytevector_ref_fn ref_fn;
2155
2156 ref_fn = bytevector_ref_fns[h->element_type];
2157 byte_index =
2158 scm_from_size_t (index * scm_array_handle_uniform_element_size (h));
2159 return ref_fn (h->array, byte_index);
2160}
2161
4bc95fcc
LC
2162/* Template for native modification of complex numbers of type TYPE. */
2163#define COMPLEX_NATIVE_SET(_type) \
2164 COMPLEX_ACCESSOR_PROLOGUE (_type); \
2165 \
2166 { \
2167 _type real, imag; \
2168 real = scm_c_real_part (value); \
2169 imag = scm_c_imag_part (value); \
2170 \
2171 memcpy (&c_bv[c_index], &real, sizeof (_type)); \
2172 memcpy (&c_bv[c_index + sizeof (_type)], &imag, sizeof (_type)); \
2173 } \
2174 \
e286c973 2175 return SCM_UNSPECIFIED;
4bc95fcc 2176
e286c973 2177static SCM
4bc95fcc
LC
2178bytevector_set_c32 (SCM bv, SCM index, SCM value)
2179#define FUNC_NAME "bytevector_set_c32"
1e8f9392 2180{
4bc95fcc 2181 COMPLEX_NATIVE_SET (float);
2a610be5 2182}
4bc95fcc 2183#undef FUNC_NAME
2a610be5 2184
e286c973 2185static SCM
4bc95fcc
LC
2186bytevector_set_c64 (SCM bv, SCM index, SCM value)
2187#define FUNC_NAME "bytevector_set_c64"
1e8f9392 2188{
4bc95fcc 2189 COMPLEX_NATIVE_SET (double);
e286c973 2190}
4bc95fcc 2191#undef FUNC_NAME
e286c973
AW
2192
2193typedef SCM (*scm_t_bytevector_set_fn)(SCM, SCM, SCM);
2194
2195const scm_t_bytevector_set_fn bytevector_set_fns[SCM_ARRAY_ELEMENT_TYPE_LAST + 1] =
2196{
2197 NULL, /* SCM */
2198 NULL, /* CHAR */
2199 NULL, /* BIT */
2200 scm_bytevector_u8_set_x, /* VU8 */
2201 scm_bytevector_u8_set_x, /* U8 */
2202 scm_bytevector_s8_set_x,
2203 scm_bytevector_u16_native_set_x,
2204 scm_bytevector_s16_native_set_x,
2205 scm_bytevector_u32_native_set_x,
2206 scm_bytevector_s32_native_set_x,
2207 scm_bytevector_u64_native_set_x,
2208 scm_bytevector_s64_native_set_x,
2209 scm_bytevector_ieee_single_native_set_x,
2210 scm_bytevector_ieee_double_native_set_x,
2211 bytevector_set_c32,
2212 bytevector_set_c64
2213};
2214
2a610be5
AW
2215static void
2216bv_handle_set_x (scm_t_array_handle *h, size_t index, SCM val)
2217{
e286c973
AW
2218 SCM byte_index;
2219 scm_t_bytevector_set_fn set_fn;
2220
2221 set_fn = bytevector_set_fns[h->element_type];
2222 byte_index =
2223 scm_from_size_t (index * scm_array_handle_uniform_element_size (h));
2224 set_fn (h->array, byte_index, val);
2a610be5
AW
2225}
2226
2227static void
2228bytevector_get_handle (SCM v, scm_t_array_handle *h)
2229{
2230 h->array = v;
2231 h->ndims = 1;
2232 h->dims = &h->dim0;
2233 h->dim0.lbnd = 0;
e286c973 2234 h->dim0.ubnd = SCM_BYTEVECTOR_TYPED_LENGTH (v) - 1;
2a610be5 2235 h->dim0.inc = 1;
e286c973 2236 h->element_type = SCM_BYTEVECTOR_ELEMENT_TYPE (v);
2a610be5
AW
2237 h->elements = h->writable_elements = SCM_BYTEVECTOR_CONTENTS (v);
2238}
2239
2240\f
1ee2c72e
LC
2241/* Initialization. */
2242
cfb4702f
LC
2243void
2244scm_bootstrap_bytevectors (void)
2245{
807e5a66 2246 /* This must be instantiated here because the generalized-vector API may
07d22c02 2247 want to access bytevectors even though `(rnrs bytevectors)' hasn't been
807e5a66 2248 loaded. */
562cd1b8 2249 scm_null_bytevector = make_bytevector (0, SCM_ARRAY_ELEMENT_TYPE_VU8);
cfb4702f 2250
caa92f5e 2251#ifdef WORDS_BIGENDIAN
4a655e50 2252 scm_i_native_endianness = scm_from_latin1_symbol ("big");
caa92f5e 2253#else
4a655e50 2254 scm_i_native_endianness = scm_from_latin1_symbol ("little");
caa92f5e
AW
2255#endif
2256
44602b08
AW
2257 scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
2258 "scm_init_bytevectors",
cfb4702f
LC
2259 (scm_t_extension_init_func) scm_init_bytevectors,
2260 NULL);
2a610be5
AW
2261
2262 {
2263 scm_t_array_implementation impl;
807e5a66
LC
2264
2265 impl.tag = scm_tc7_bytevector;
2266 impl.mask = 0x7f;
2a610be5
AW
2267 impl.vref = bv_handle_ref;
2268 impl.vset = bv_handle_set_x;
2269 impl.get_handle = bytevector_get_handle;
2270 scm_i_register_array_implementation (&impl);
f45eccff
AW
2271 scm_i_register_vector_constructor
2272 (scm_i_array_element_types[SCM_ARRAY_ELEMENT_TYPE_VU8],
2273 scm_make_bytevector);
2a610be5 2274 }
cfb4702f
LC
2275}
2276
1ee2c72e
LC
2277void
2278scm_init_bytevectors (void)
2279{
2280#include "libguile/bytevectors.x"
2281
1ee2c72e
LC
2282 scm_endianness_big = scm_sym_big;
2283 scm_endianness_little = scm_sym_little;
1ee2c72e 2284}