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