Debian Codegraph @

src/vector.c

raw
1 /*
2 * vector.c - vector implementation
3 *
4 * Copyright (c) 2000-2024 Shiro Kawai <[email protected]>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the authors nor the names of its contributors
18 * may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #define LIBGAUCHE_BODY
35 #include "gauche.h"
36 #include "gauche/priv/configP.h"
37 #include "gauche/priv/writerP.h"
38 #include "gauche/priv/mmapP.h"
39
40 #if defined(HAVE_SYS_MMAN_H)
41 #include <sys/mman.h>
42 #endif
43
44 /* Catch integer overflow.
45 NB: If total size is too big, GC_malloc aborts. But we have to prevent
46 total size from overflowing before passed to GC_malloc.
47
48 We'll try to allocate size*eltsize (+ up to two words of header).
49 The GC's malloc routine first round it up to GC allocation unit boundary
50 (8 or 16 bytes). If there's not enough heap, then it tries to expand
51 the heap by the size rounded up to the pagesize. We don't want the final
52 value overflows signed long.
53 (In reality, expanding heap with close to LONG_MAX surely fails, so it
54 should suffice to avoid overflow before calling GC_MALLOC. But it won't
55 harm to have a bit of margin here...)
56 */
57 static void check_size(ScmSmallInt size, int eltsize)
58 {
59 if (size < 0) {
60 Scm_Error("Invalid size: %ld", size);
61 }
62 if (size >= (ScmSmallInt)((LONG_MAX - 0x400000)/eltsize)) {
63 Scm_Error("Size too big: %ld", size);
64 }
65 }
66
67 /*=====================================================================
68 * Generic vectors
69 */
70
71 /*
72 * Constructor
73 */
74
75 static void vector_print(ScmObj obj, ScmPort *port, ScmWriteContext *ctx)
76 {
77 SCM_PUTZ("#(", -1, port);
78 for (int i=0; i<SCM_VECTOR_SIZE(obj); i++) {
79 if (i != 0) SCM_PUTC(' ', port);
80 Scm_Write(SCM_VECTOR_ELEMENT(obj, i), SCM_OBJ(port),
81 Scm_WriteContextMode(ctx));
82 }
83 SCM_PUTZ(")", -1, port);
84 }
85
86 static int vector_compare(ScmObj x, ScmObj y, int equalp)
87 {
88 if (equalp) {
89 /* Vector equality is handled in Scm_Eq* and will never come
90 here, but just in case. */
91 return Scm_EqualP(x, y)? 0 : 1;
92 }
93 /* Follow srfi-114 */
94 ScmWord xlen = SCM_VECTOR_SIZE(x);
95 ScmWord ylen = SCM_VECTOR_SIZE(y);
96 if (xlen < ylen) return -1;
97 if (xlen > ylen) return 1;
98 for (int i=0; i<xlen; i++) {
99 int r = Scm_Compare(SCM_VECTOR_ELEMENT(x, i),
100 SCM_VECTOR_ELEMENT(y, i));
101 if (r != 0) return r;
102 }
103 return 0;
104 }
105
106
107 SCM_DEFINE_BUILTIN_CLASS_FLAGS(Scm_VectorClass, vector_print, vector_compare,
108 NULL, NULL, SCM_CLASS_SEQUENCE_CPL,
109 SCM_CLASS_AGGREGATE);
110
111 static ScmVector *make_vector(ScmSmallInt size)
112 {
113 check_size(size, sizeof(ScmObj));
114
115 /* We allocate at least one element for zero-length vector, since
116 ScmVector is declared to have at least one element.
117 Alternatively, we can have a singleton instance for #().
118 */
119 ScmSmallInt alloc_size = (size == 0)? 0 : (size-1);
120
121 ScmVector *v = SCM_NEW2(ScmVector *,
122 sizeof(ScmVector) + sizeof(ScmObj)*alloc_size);
123 SCM_SET_CLASS(v, SCM_CLASS_VECTOR);
124 #if GAUCHE_API_VERSION >= 98
125 v->size_flags = (size << 1);
126 #else
127 v->size = size;
128 #endif
129 return v;
130 }
131
132 ScmObj Scm_MakeVector(ScmSmallInt size, ScmObj fill)
133 {
134 if (size < 0) {
135 Scm_Error("vector size must be a positive integer, but got %d", size);
136 }
137 ScmVector *v = make_vector(size);
138 if (SCM_UNBOUNDP(fill)) fill = SCM_UNDEFINED;
139 for (ScmSmallInt i=0; i<size; i++) v->elements[i] = fill;
140 return SCM_OBJ(v);
141 }
142
143 ScmObj Scm_ListToVector(ScmObj l, ScmSmallInt start, ScmSmallInt end)
144 {
145 ScmVector *v;
146
147 if (end < 0) {
148 ScmSmallInt size = Scm_Length(l);
149 if (size < 0) Scm_Error("bad list: %S", l);
150 SCM_CHECK_START_END(start, end, size);
151 v = make_vector(size - start);
152 } else {
153 SCM_CHECK_START_END(start, end, end);
154 v = make_vector(end - start);
155 }
156 ScmObj e = Scm_ListTail(l, start, SCM_UNBOUND);
157 for (ScmSmallInt i=0; i<end-start; i++, e=SCM_CDR(e)) {
158 if (!SCM_PAIRP(e)) {
159 Scm_Error("list too short: %S", l);
160 }
161 v->elements[i] = SCM_CAR(e);
162 }
163 return SCM_OBJ(v);
164 }
165
166 ScmObj Scm_VectorToList(ScmVector *v, ScmSmallInt start, ScmSmallInt end)
167 {
168 ScmWord len = SCM_VECTOR_SIZE(v);
169 SCM_CHECK_START_END(start, end, len);
170 return Scm_ArrayToList(SCM_VECTOR_ELEMENTS(v)+start,
171 end-start);
172 }
173
174 /*
175 * Accessors
176 */
177
178 /* NB: we're permissive about the out-of-range index here; the strict
179 check (for Scheme routines) should be done in the stub file, since
180 Scheme version may receive bignum, which can't be passed to C API. */
181
182 ScmObj Scm_VectorRef(ScmVector *vec, ScmSmallInt i, ScmObj fallback)
183 {
184 if (i < 0 || i >= SCM_VECTOR_SIZE(vec)) return fallback;
185 return vec->elements[i];
186 }
187
188 ScmObj Scm_VectorSet(ScmVector *vec, ScmSmallInt i, ScmObj obj)
189 {
190 SCM_VECTOR_CHECK_MUTABLE(vec);
191 if (i >= 0 && i < SCM_VECTOR_SIZE(vec)) vec->elements[i] = obj;
192 return obj;
193 }
194
195 ScmObj Scm_VectorFill(ScmVector *vec, ScmObj fill,
196 ScmSmallInt start, ScmSmallInt end)
197 {
198 SCM_VECTOR_CHECK_MUTABLE(vec);
199 ScmSmallInt len = SCM_VECTOR_SIZE(vec);
200 SCM_CHECK_START_END(start, end, len);
201 for (ScmSmallInt i=start; i < end; i++) {
202 SCM_VECTOR_ELEMENT(vec, i) = fill;
203 }
204 return SCM_OBJ(vec);
205 }
206
207 ScmObj Scm_VectorCopy(ScmVector *vec,
208 ScmSmallInt start, ScmSmallInt end, ScmObj fill)
209 {
210 ScmSmallInt len = SCM_VECTOR_SIZE(vec);
211 ScmVector *v = NULL;
212 if (end < 0) end = len;
213 if (end < start) {
214 Scm_Error("vector-copy: start (%ld) is greater than end (%ld)",
215 start, end);
216 } else if (end == start) {
217 v = make_vector(0);
218 } else {
219 if (SCM_UNBOUNDP(fill)) fill = SCM_UNDEFINED;
220 v = make_vector(end - start);
221 for (ScmSmallInt i=0; i<end-start; i++) {
222 if (i+start < 0 || i+start >= len) {
223 SCM_VECTOR_ELEMENT(v, i) = fill;
224 } else {
225 SCM_VECTOR_ELEMENT(v, i) = SCM_VECTOR_ELEMENT(vec, i+start);
226 }
227 }
228 }
229 return SCM_OBJ(v);
230 }
231
232 /*=====================================================================
233 * Uniform vectors
234 */
235
236 /*
237 * Class stuff
238 */
239 static ScmClass *uvector_cpl[] = {
240 SCM_CLASS_STATIC_PTR(Scm_UVectorClass),
241 SCM_CLASS_STATIC_PTR(Scm_SequenceClass),
242 SCM_CLASS_STATIC_PTR(Scm_CollectionClass),
243 SCM_CLASS_STATIC_PTR(Scm_TopClass),
244 NULL
245 };
246
247 SCM_DEFINE_BUILTIN_CLASS(Scm_UVectorClass, NULL, NULL, NULL, NULL,
248 uvector_cpl+1);
249
250 #define DEF_UVCLASS(TAG, tag) \
251 static void SCM_CPP_CAT3(print_,tag,vector)(ScmObj obj, ScmPort *out, \
252 ScmWriteContext *ctx); \
253 static int SCM_CPP_CAT3(compare_,tag,vector)(ScmObj x, ScmObj y, int equalp); \
254 SCM_DEFINE_BUILTIN_CLASS_FLAGS(SCM_CPP_CAT3(Scm_,TAG,VectorClass), \
255 SCM_CPP_CAT3(print_,tag,vector), \
256 SCM_CPP_CAT3(compare_,tag,vector), \
257 NULL, NULL, uvector_cpl, \
258 SCM_CLASS_AGGREGATE);
259
260 DEF_UVCLASS(S8, s8)
261 DEF_UVCLASS(U8, u8)
262 DEF_UVCLASS(S16, s16)
263 DEF_UVCLASS(U16, u16)
264 DEF_UVCLASS(S32, s32)
265 DEF_UVCLASS(U32, u32)
266 DEF_UVCLASS(S64, s64)
267 DEF_UVCLASS(U64, u64)
268 DEF_UVCLASS(F16, f16)
269 DEF_UVCLASS(F32, f32)
270 DEF_UVCLASS(F64, f64)
271 DEF_UVCLASS(C32, c32)
272 DEF_UVCLASS(C64, c64)
273 DEF_UVCLASS(C128, c128)
274
275 /*
276 * Some generic APIs
277 */
278 ScmUVectorType Scm_UVectorType(ScmClass *klass)
279 {
280 if (SCM_EQ(klass, SCM_CLASS_S8VECTOR)) return SCM_UVECTOR_S8;
281 if (SCM_EQ(klass, SCM_CLASS_U8VECTOR)) return SCM_UVECTOR_U8;
282 if (SCM_EQ(klass, SCM_CLASS_S16VECTOR)) return SCM_UVECTOR_S16;
283 if (SCM_EQ(klass, SCM_CLASS_U16VECTOR)) return SCM_UVECTOR_U16;
284 if (SCM_EQ(klass, SCM_CLASS_S32VECTOR)) return SCM_UVECTOR_S32;
285 if (SCM_EQ(klass, SCM_CLASS_U32VECTOR)) return SCM_UVECTOR_U32;
286 if (SCM_EQ(klass, SCM_CLASS_S64VECTOR)) return SCM_UVECTOR_S64;
287 if (SCM_EQ(klass, SCM_CLASS_U64VECTOR)) return SCM_UVECTOR_U64;
288 if (SCM_EQ(klass, SCM_CLASS_F16VECTOR)) return SCM_UVECTOR_F16;
289 if (SCM_EQ(klass, SCM_CLASS_F32VECTOR)) return SCM_UVECTOR_F32;
290 if (SCM_EQ(klass, SCM_CLASS_F64VECTOR)) return SCM_UVECTOR_F64;
291 if (SCM_EQ(klass, SCM_CLASS_C32VECTOR)) return SCM_UVECTOR_C32;
292 if (SCM_EQ(klass, SCM_CLASS_C64VECTOR)) return SCM_UVECTOR_C64;
293 if (SCM_EQ(klass, SCM_CLASS_C128VECTOR)) return SCM_UVECTOR_C128;
294 else return SCM_UVECTOR_INVALID;
295 }
296
297 const char *Scm_UVectorTypeName(int type) /* for error msgs etc. */
298 {
299 switch (type) {
300 case SCM_UVECTOR_S8: return "s8vector";
301 case SCM_UVECTOR_U8: return "u8vector";
302 case SCM_UVECTOR_S16: return "s16vector";
303 case SCM_UVECTOR_U16: return "u16vector";
304 case SCM_UVECTOR_S32: return "s32vector";
305 case SCM_UVECTOR_U32: return "u32vector";
306 case SCM_UVECTOR_S64: return "s64vector";
307 case SCM_UVECTOR_U64: return "u64vector";
308 case SCM_UVECTOR_F16: return "f16vector";
309 case SCM_UVECTOR_F32: return "f32vector";
310 case SCM_UVECTOR_F64: return "f64vector";
311 case SCM_UVECTOR_C32: return "c32vector";
312 case SCM_UVECTOR_C64: return "c64vector";
313 case SCM_UVECTOR_C128: return "c128vector";
314 default: return "invalid type of uvector (possibly implementation error)";
315 }
316 }
317
318 /* Returns the size of element of the uvector of given class */
319 int Scm_UVectorElementSize(ScmClass *klass)
320 {
321 static const int sizes[] = { 1, 1, 2, 2, 4, 4, 8, 8,
322 2, sizeof(float), sizeof(double), -1,
323 sizeof(ScmHalfComplex),
324 sizeof(ScmFloatComplex),
325 sizeof(ScmDoubleComplex),
326 -1 };
327 int ind = (int)Scm_UVectorType(klass);
328 if (ind >= 0) return sizes[ind];
329 return -1;
330 }
331
332 /* Returns the size of the vector body in bytes */
333 int Scm_UVectorSizeInBytes(ScmUVector *uv)
334 {
335 return SCM_UVECTOR_SIZE(uv) * Scm_UVectorElementSize(Scm_ClassOf(SCM_OBJ(uv)));
336 }
337
338 /* Generic constructor */
339 ScmObj Scm_MakeUVectorFull(ScmClass *klass, ScmSmallInt size, void *init,
340 int immutable, void *owner)
341 {
342 int eltsize = Scm_UVectorElementSize(klass);
343 SCM_ASSERT(eltsize >= 1);
344 ScmUVector *vec = SCM_NEW(ScmUVector);
345 SCM_SET_CLASS(vec, klass);
346 if (init) {
347 vec->elements = init; /* trust the caller */
348 } else {
349 check_size(size, eltsize);
350 vec->elements = SCM_NEW_ATOMIC2(void*, size*eltsize);
351 }
352 vec->size_flags = (size << 1)|(immutable?1:0);
353 vec->owner = owner;
354 return SCM_OBJ(vec);
355 }
356
357 ScmObj Scm_MakeUVector(ScmClass *klass, ScmSmallInt size, void *init)
358 {
359 return Scm_MakeUVectorFull(klass, size, init, FALSE, NULL);
360 }
361
362 ScmObj Scm_ListToUVector(ScmClass *klass, ScmObj list, int clamp)
363 {
364 ScmUVectorType type = Scm_UVectorType(klass);
365 if (type < 0) Scm_Error("uvector class required, but got: %S", klass);
366 ScmSize length = Scm_Length(list);
367 if (length < 0) Scm_Error("improper list not allowed: %S", list);
368 if (length > SCM_SMALL_INT_MAX) Scm_Error("list is too long: %,,,,100S", list);
369
370 ScmUVector *v = (ScmUVector*)Scm_MakeUVector(klass,
371 (ScmSmallInt)length,
372 NULL);
373 ScmObj cp = list;
374 for (ScmSize i=0; i<length; i++, cp = SCM_CDR(cp)) {
375 switch (type) {
376 case SCM_UVECTOR_S8:
377 SCM_S8VECTOR_ELEMENTS(v)[i] =
378 (int8_t)Scm_GetInteger8Clamp(SCM_CAR(cp), clamp, NULL);
379 break;
380 case SCM_UVECTOR_U8:
381 SCM_U8VECTOR_ELEMENTS(v)[i] =
382 (uint8_t)Scm_GetIntegerU8Clamp(SCM_CAR(cp), clamp, NULL);
383 break;
384 case SCM_UVECTOR_S16:
385 SCM_S16VECTOR_ELEMENTS(v)[i] =
386 (int16_t)Scm_GetInteger16Clamp(SCM_CAR(cp), clamp, NULL);
387 break;
388 case SCM_UVECTOR_U16:
389 SCM_U16VECTOR_ELEMENTS(v)[i] =
390 (uint16_t)Scm_GetIntegerU16Clamp(SCM_CAR(cp), clamp, NULL);
391 break;
392 case SCM_UVECTOR_S32:
393 SCM_S32VECTOR_ELEMENTS(v)[i] =
394 (int32_t)Scm_GetInteger32Clamp(SCM_CAR(cp), clamp, NULL);
395 break;
396 case SCM_UVECTOR_U32:
397 SCM_U32VECTOR_ELEMENTS(v)[i] =
398 (uint32_t)Scm_GetIntegerU32Clamp(SCM_CAR(cp), clamp, NULL);
399 break;
400 case SCM_UVECTOR_S64:
401 SCM_S64VECTOR_ELEMENTS(v)[i] =
402 (int64_t)Scm_GetInteger64Clamp(SCM_CAR(cp), clamp, NULL);
403 break;
404 case SCM_UVECTOR_U64:
405 SCM_U64VECTOR_ELEMENTS(v)[i] =
406 (uint64_t)Scm_GetIntegerU64Clamp(SCM_CAR(cp), clamp, NULL);
407 break;
408 case SCM_UVECTOR_F16:
409 SCM_F16VECTOR_ELEMENTS(v)[i] =
410 (ScmHalfFloat)Scm_DoubleToHalf(Scm_GetDouble(SCM_CAR(cp)));
411 break;
412 case SCM_UVECTOR_F32:
413 SCM_F32VECTOR_ELEMENTS(v)[i] =
414 (float)Scm_GetDouble(SCM_CAR(cp));
415 break;
416 case SCM_UVECTOR_F64:
417 SCM_F64VECTOR_ELEMENTS(v)[i] = Scm_GetDouble(SCM_CAR(cp));
418 break;
419 case SCM_UVECTOR_C32:
420 SCM_C32VECTOR_ELEMENTS(v)[i] = Scm_GetHalfComplex(SCM_CAR(cp));
421 break;
422 case SCM_UVECTOR_C64:
423 SCM_C64VECTOR_ELEMENTS(v)[i] = Scm_GetFloatComplex(SCM_CAR(cp));
424 break;
425 case SCM_UVECTOR_C128:
426 SCM_C128VECTOR_ELEMENTS(v)[i] = Scm_GetDoubleComplex(SCM_CAR(cp));
427 break;
428 default:
429 Scm_Error("[internal error] unknown uvector type given to Scm_ListToUVector");
430 }
431 }
432 return SCM_OBJ(v);
433 }
434
435 /* Generic accessor, intended to be called from VM loop.
436 (As the 'VM' in the name suggests, the return value of this API
437 should immediately be passed to VM. See comments on FFX in gauche/number.h)
438 */
439 ScmObj Scm_VMUVectorRef(ScmUVector *v, int t, ScmSmallInt k, ScmObj fallback)
440 {
441 if (t == SCM_UVECTOR_GENERIC) {
442 t = Scm_UVectorType(SCM_CLASS_OF(v));
443 } else if (Scm_UVectorType(SCM_CLASS_OF(v)) != t) {
444 Scm_Error("%s required, but got %S", Scm_UVectorTypeName(t), v);
445 }
446 if (k < 0 || k >= SCM_UVECTOR_SIZE(v)) {
447 if (SCM_UNBOUNDP(fallback)) {
448 Scm_Error("%s-ref index out of range: %ld",
449 Scm_UVectorTypeName(t), k);
450 }
451 return fallback;
452 }
453 switch (t) {
454 case SCM_UVECTOR_S8: return SCM_MAKE_INT(SCM_S8VECTOR_ELEMENT(v, k));
455 case SCM_UVECTOR_U8: return SCM_MAKE_INT(SCM_U8VECTOR_ELEMENT(v, k));
456 case SCM_UVECTOR_S16: return SCM_MAKE_INT(SCM_S16VECTOR_ELEMENT(v, k));
457 case SCM_UVECTOR_U16: return SCM_MAKE_INT(SCM_U16VECTOR_ELEMENT(v, k));
458 case SCM_UVECTOR_S32: return Scm_MakeInteger(SCM_S32VECTOR_ELEMENT(v, k));
459 case SCM_UVECTOR_U32: return Scm_MakeIntegerU(SCM_U32VECTOR_ELEMENT(v, k));
460 case SCM_UVECTOR_S64: return Scm_MakeInteger64(SCM_S64VECTOR_ELEMENT(v, k));
461 case SCM_UVECTOR_U64: return Scm_MakeIntegerU64(SCM_U64VECTOR_ELEMENT(v, k));
462 case SCM_UVECTOR_F16:
463 return Scm_VMReturnFlonum(Scm_HalfToDouble(SCM_F16VECTOR_ELEMENT(v, k)));
464 case SCM_UVECTOR_F32:
465 return Scm_VMReturnFlonum((double)(SCM_F32VECTOR_ELEMENT(v, k)));
466 case SCM_UVECTOR_F64:
467 return Scm_VMReturnFlonum(SCM_F64VECTOR_ELEMENT(v, k));
468 case SCM_UVECTOR_C32:
469 return Scm_HalfComplexToComplex(SCM_C32VECTOR_ELEMENT(v, k));
470 case SCM_UVECTOR_C64:
471 return Scm_FloatComplexToComplex(SCM_C64VECTOR_ELEMENT(v, k));
472 case SCM_UVECTOR_C128:
473 return Scm_DoubleComplexToComplex(SCM_C128VECTOR_ELEMENT(v, k));
474 default:
475 Scm_Error("[internal error] unknown uvector type given to Scm_VMUVectorRef");
476 return SCM_UNDEFINED; /* dummy */
477 }
478 }
479
480 /* Generic modifier */
481 ScmObj Scm_UVectorSet(ScmUVector *v, int t, ScmSmallInt k, ScmObj val, int clamp)
482 {
483 SCM_ASSERT(Scm_UVectorType(SCM_CLASS_OF(v)) == t);
484 SCM_UVECTOR_CHECK_MUTABLE(SCM_OBJ(v));
485 if (k < 0 || k >= SCM_UVECTOR_SIZE(v)) {
486 Scm_Error("%s-set! index out of range: %ld", Scm_UVectorTypeName(t), k);
487 }
488 switch (t) {
489 case SCM_UVECTOR_S8:
490 SCM_S8VECTOR_ELEMENTS(v)[k] = Scm_GetInteger8Clamp(val, clamp, NULL);
491 break;
492 case SCM_UVECTOR_U8:
493 SCM_U8VECTOR_ELEMENTS(v)[k] = Scm_GetIntegerU8Clamp(val, clamp, NULL);
494 break;
495 case SCM_UVECTOR_S16:
496 SCM_S16VECTOR_ELEMENTS(v)[k] = Scm_GetInteger16Clamp(val, clamp, NULL);
497 break;
498 case SCM_UVECTOR_U16:
499 SCM_U16VECTOR_ELEMENTS(v)[k] = Scm_GetIntegerU16Clamp(val, clamp, NULL);
500 break;
501 case SCM_UVECTOR_S32:
502 SCM_S32VECTOR_ELEMENTS(v)[k] = Scm_GetInteger32Clamp(val, clamp, NULL);
503 break;
504 case SCM_UVECTOR_U32:
505 SCM_U32VECTOR_ELEMENTS(v)[k] = Scm_GetIntegerU32Clamp(val, clamp, NULL);
506 break;
507 case SCM_UVECTOR_S64:
508 SCM_S64VECTOR_ELEMENTS(v)[k] = Scm_GetInteger64Clamp(val, clamp, NULL);
509 break;
510 case SCM_UVECTOR_U64:
511 SCM_U64VECTOR_ELEMENTS(v)[k] = Scm_GetIntegerU64Clamp(val, clamp, NULL);
512 break;
513 case SCM_UVECTOR_F16:
514 SCM_F16VECTOR_ELEMENTS(v)[k] = Scm_DoubleToHalf(Scm_GetDouble(val));
515 break;
516 case SCM_UVECTOR_F32:
517 SCM_F32VECTOR_ELEMENTS(v)[k] = (float)Scm_GetDouble(val);
518 break;
519 case SCM_UVECTOR_F64:
520 SCM_F64VECTOR_ELEMENTS(v)[k] = Scm_GetDouble(val);
521 break;
522 case SCM_UVECTOR_C32:
523 SCM_C32VECTOR_ELEMENTS(v)[k] = Scm_GetHalfComplex(val);
524 break;
525 case SCM_UVECTOR_C64:
526 SCM_C64VECTOR_ELEMENTS(v)[k] = Scm_GetFloatComplex(val);
527 break;
528 case SCM_UVECTOR_C128:
529 SCM_C128VECTOR_ELEMENTS(v)[k] = Scm_GetDoubleComplex(val);
530 break;
531 default:
532 Scm_Error("[internal error] unknown uvector type given to Scm_VMUVectorRef");
533 }
534 return SCM_UNDEFINED;
535 }
536
537 /*
538 * Inidividual constructors for convenience
539 */
540 #define DEF_UVCTOR_FILL(tag, T) \
541 ScmObj SCM_CPP_CAT3(Scm_Make,tag,Vector)(ScmSmallInt size, T fill) \
542 { \
543 ScmUVector *u = \
544 (ScmUVector*)Scm_MakeUVector(SCM_CPP_CAT3(SCM_CLASS_,tag,VECTOR),\
545 size, NULL); \
546 T *elts = SCM_CPP_CAT3(SCM_,tag,VECTOR_ELEMENTS)(u); \
547 for (ScmSmallInt i=0; i<size; i++) *elts++ = fill; \
548 return SCM_OBJ(u); \
549 }
550
551 #define DEF_UVCTOR_ARRAY(tag, T) \
552 ScmObj SCM_CPP_CAT3(Scm_Make,tag,VectorFromArray)(ScmSmallInt size, \
553 const T array[]) \
554 { \
555 check_size(size, sizeof(T)); \
556 T *z = SCM_NEW_ATOMIC_ARRAY(T, size); \
557 memcpy(z, array, size*sizeof(T)); \
558 return Scm_MakeUVector(SCM_CPP_CAT3(SCM_CLASS_,tag,VECTOR), \
559 size, (void*)z); \
560 } \
561 ScmObj SCM_CPP_CAT3(Scm_Make,tag,VectorFromArrayShared)(ScmSmallInt size,\
562 T array[]) \
563 { \
564 return Scm_MakeUVector(SCM_CPP_CAT3(SCM_CLASS_,tag,VECTOR), \
565 size, (void*)array); \
566 }
567
568 /* NB: For u8vector and s8vector we can let memset() to fill the
569 contents, expecting it's optimized. */
570 ScmObj Scm_MakeS8Vector(ScmSmallInt size, int8_t fill)
571 {
572 ScmUVector *u =
573 (ScmUVector*)Scm_MakeUVector(SCM_CLASS_S8VECTOR, size, NULL);
574 (void)memset(SCM_S8VECTOR_ELEMENTS(u), fill, size);
575 return SCM_OBJ(u);
576 }
577
578 ScmObj Scm_MakeU8Vector(ScmSmallInt size, uint8_t fill)
579 {
580 ScmUVector *u =
581 (ScmUVector*)Scm_MakeUVector(SCM_CLASS_U8VECTOR, size, NULL);
582 (void)memset(SCM_U8VECTOR_ELEMENTS(u), fill, size);
583 return SCM_OBJ(u);
584 }
585
586 DEF_UVCTOR_FILL(S16, int16_t)
587 DEF_UVCTOR_FILL(U16, uint16_t)
588 DEF_UVCTOR_FILL(S32, int32_t)
589 DEF_UVCTOR_FILL(U32, uint32_t)
590 DEF_UVCTOR_FILL(S64, int64_t)
591 DEF_UVCTOR_FILL(U64, uint64_t)
592 DEF_UVCTOR_FILL(F16, ScmHalfFloat)
593 DEF_UVCTOR_FILL(F32, float)
594 DEF_UVCTOR_FILL(F64, double)
595 DEF_UVCTOR_FILL(C32, ScmHalfComplex)
596 DEF_UVCTOR_FILL(C64, ScmFloatComplex)
597 DEF_UVCTOR_FILL(C128,ScmDoubleComplex)
598
599 DEF_UVCTOR_ARRAY(S8, int8_t)
600 DEF_UVCTOR_ARRAY(U8, uint8_t)
601 DEF_UVCTOR_ARRAY(S16, int16_t)
602 DEF_UVCTOR_ARRAY(U16, uint16_t)
603 DEF_UVCTOR_ARRAY(S32, int32_t)
604 DEF_UVCTOR_ARRAY(U32, uint32_t)
605 DEF_UVCTOR_ARRAY(S64, int64_t)
606 DEF_UVCTOR_ARRAY(U64, uint64_t)
607 DEF_UVCTOR_ARRAY(F16, ScmHalfFloat)
608 DEF_UVCTOR_ARRAY(F32, float)
609 DEF_UVCTOR_ARRAY(F64, double)
610 DEF_UVCTOR_ARRAY(C32, ScmHalfComplex)
611 DEF_UVCTOR_ARRAY(C64, ScmFloatComplex)
612 DEF_UVCTOR_ARRAY(C128,ScmDoubleComplex)
613
614 /*
615 * Reader
616 */
617 ScmObj Scm_ReadUVector(ScmPort *port, const char *tag, ScmReadContext *ctx)
618 {
619 ScmObj uv;
620 ScmChar c;
621 SCM_GETC(c, port);
622
623 if (c == '"' && strcmp(tag, "u8") == 0) {
624 /* SRFI-207 string-notated bytevector.
625 TODO: SRFI-207 is stricter than what Scm_ReadStringLiteral allows.
626 We might want better error checking.
627 TODO: Avoid copying the string content. */
628 ScmObj s = Scm_ReadStringLiteral(port, ctx, SCM_STRING_INCOMPLETE,
629 SCM_CHAR_INVALID);
630 SCM_ASSERT(SCM_STRINGP(s));
631 const ScmStringBody *sb = SCM_STRING_BODY(SCM_STRING(s));
632 char *content = Scm_StrdupPartial(SCM_STRING_BODY_START(sb),
633 SCM_STRING_BODY_SIZE(sb));
634 uv = Scm_MakeUVector(SCM_CLASS_U8VECTOR, SCM_STRING_BODY_SIZE(sb),
635 content);
636 } else {
637 if (c != '(') Scm_Error("bad uniform vector syntax for %s", tag);
638 ScmObj list = Scm_ReadList(SCM_OBJ(port), ')');
639 ScmClass *klass = NULL;
640 if (strcmp(tag, "s8") == 0) klass = SCM_CLASS_S8VECTOR;
641 else if (strcmp(tag, "u8") == 0) klass = SCM_CLASS_U8VECTOR;
642 else if (strcmp(tag, "s16") == 0) klass = SCM_CLASS_S16VECTOR;
643 else if (strcmp(tag, "u16") == 0) klass = SCM_CLASS_U16VECTOR;
644 else if (strcmp(tag, "s32") == 0) klass = SCM_CLASS_S32VECTOR;
645 else if (strcmp(tag, "u32") == 0) klass = SCM_CLASS_U32VECTOR;
646 else if (strcmp(tag, "s64") == 0) klass = SCM_CLASS_S64VECTOR;
647 else if (strcmp(tag, "u64") == 0) klass = SCM_CLASS_U64VECTOR;
648 else if (strcmp(tag, "f16") == 0) klass = SCM_CLASS_F16VECTOR;
649 else if (strcmp(tag, "f32") == 0) klass = SCM_CLASS_F32VECTOR;
650 else if (strcmp(tag, "f64") == 0) klass = SCM_CLASS_F64VECTOR;
651 else if (strcmp(tag, "c32") == 0) klass = SCM_CLASS_C32VECTOR;
652 else if (strcmp(tag, "c64") == 0) klass = SCM_CLASS_C64VECTOR;
653 else if (strcmp(tag, "c128") == 0) klass = SCM_CLASS_C128VECTOR;
654 else Scm_Error("invalid unform vector tag: %s", tag);
655
656 uv = Scm_ListToUVector(klass, list, 0);
657 }
658
659 /* If we are reading source file, let literal uvectors be immutable. */
660 if (Scm_ReadContextLiteralImmutable(ctx)) {
661 SCM_UVECTOR_IMMUTABLE_SET(uv, TRUE);
662 }
663 return uv;
664 }
665
666 /*
667 * Class-dependent functions
668 */
669
670 /* printer */
671
672 #define DEF_PRINT(TAG, tag, T, pr) \
673 static void SCM_CPP_CAT3(print_,tag,vector)(ScmObj obj, \
674 ScmPort *out, \
675 ScmWriteContext *ctx) \
676 { \
677 const ScmWriteControls *wp = \
678 Scm_GetWriteControls(ctx, Scm_PortWriteState(out)); \
679 Scm_Printf(out, "#"#tag"("); \
680 for (int i=0; i<SCM_CPP_CAT3(SCM_,TAG,VECTOR_SIZE)(obj); i++) { \
681 T elt = SCM_CPP_CAT3(SCM_,TAG,VECTOR_ELEMENTS)(obj)[i]; \
682 if (i != 0) Scm_Printf(out, " "); \
683 if (wp->printLength >= 0 && i >= wp->printLength) { \
684 Scm_Printf(out, SCM_WRITTEN_ELLIPSIS); \
685 break; \
686 } \
687 pr(out, elt); \
688 } \
689 Scm_Printf(out, ")"); \
690 }
691
692 #define spr(out, elt) Scm_Printf(out, "%d", elt)
693 #define upr(out, elt) Scm_Printf(out, "%u", elt)
694 #define fpr(out, elt) Scm_PrintDouble(out, (double)elt, 0)
695 #define c32pr(out, elt) \
696 do { \
697 Scm_PrintDouble(out, Scm_HalfToDouble(SCM_HALF_COMPLEX_REAL(elt)), 0); \
698 Scm_Putz("+", 1, out); \
699 Scm_PrintDouble(out, Scm_HalfToDouble(SCM_HALF_COMPLEX_IMAG(elt)), 0); \
700 Scm_Putz("i", 1, out); \
701 } while (0)
702 #define c64pr(out, elt) \
703 do { \
704 Scm_PrintDouble(out, (double)crealf(elt), 0); \
705 Scm_Putz("+", 1, out); \
706 Scm_PrintDouble(out, (double)cimagf(elt), 0); \
707 Scm_Putz("i", 1, out); \
708 } while (0)
709 #define c128pr(out, elt) \
710 do { \
711 Scm_PrintDouble(out, creal(elt), 0); \
712 Scm_Putz("+", 1, out); \
713 Scm_PrintDouble(out, cimag(elt), 0); \
714 Scm_Putz("i", 1, out); \
715 } while (0)
716
717
718 static inline void s64pr(ScmPort *out, int64_t elt)
719 {
720 #if SIZEOF_LONG == 4
721 char buf[50];
722 snprintf(buf, 50, "%lld", elt);
723 Scm_Printf(out, "%s", buf);
724 #else
725 Scm_Printf(out, "%ld", elt);
726 #endif
727 }
728
729 static inline void u64pr(ScmPort *out, uint64_t elt)
730 {
731 #if SIZEOF_LONG == 4
732 char buf[50];
733 snprintf(buf, 50, "%llu", elt);
734 Scm_Printf(out, "%s", buf);
735 #else
736 Scm_Printf(out, "%lu", elt);
737 #endif
738 }
739
740 static inline void f16pr(ScmPort *out, ScmHalfFloat elt)
741 {
742 Scm_PrintDouble(out, Scm_HalfToDouble(elt), 0);
743 }
744
745 DEF_PRINT(S8, s8, int8_t, spr)
746 DEF_PRINT(U8, u8, uint8_t, upr)
747 DEF_PRINT(S16, s16, int16_t, spr)
748 DEF_PRINT(U16, u16, uint16_t, upr)
749 DEF_PRINT(S32, s32, int32_t, spr)
750 DEF_PRINT(U32, u32, uint32_t, upr)
751 DEF_PRINT(S64, s64, int64_t, s64pr)
752 DEF_PRINT(U64, u64, uint64_t, u64pr)
753 DEF_PRINT(F16, f16, ScmHalfFloat, f16pr)
754 DEF_PRINT(F32, f32, float, fpr)
755 DEF_PRINT(F64, f64, double, fpr)
756 DEF_PRINT(C32, c32, ScmHalfComplex, c32pr)
757 DEF_PRINT(C64, c64, ScmFloatComplex, c64pr)
758 DEF_PRINT(C128, c128, ScmDoubleComplex, c128pr)
759
760
761 /* comparer */
762
763 #define DEF_CMP(TAG, tag, T, eq, lt) \
764 static int SCM_CPP_CAT3(compare_,tag,vector)(ScmObj x, ScmObj y, int equalp) \
765 { \
766 ScmSmallInt xlen = SCM_CPP_CAT3(SCM_,TAG,VECTOR_SIZE)(x); \
767 ScmSmallInt ylen = SCM_CPP_CAT3(SCM_,TAG,VECTOR_SIZE)(y); \
768 if (equalp) { \
769 if (xlen != ylen) return -1; \
770 for (ScmSmallInt i=0; i<xlen; i++) { \
771 T xx = SCM_CPP_CAT3(SCM_,TAG,VECTOR_ELEMENTS)(x)[i]; \
772 T yy = SCM_CPP_CAT3(SCM_,TAG,VECTOR_ELEMENTS)(y)[i]; \
773 if (!eq(xx,yy)) return -1; \
774 } \
775 return 0; \
776 } else { \
777 if (xlen != ylen) return (xlen < ylen) ? -1 : 1; \
778 for (ScmSmallInt i=0; i<xlen; i++) { \
779 T xx = SCM_CPP_CAT3(SCM_,TAG,VECTOR_ELEMENTS)(x)[i]; \
780 T yy = SCM_CPP_CAT3(SCM_,TAG,VECTOR_ELEMENTS)(y)[i]; \
781 if (lt(xx, yy)) return -1; \
782 if (!eq(xx,yy)) return 1; \
783 } \
784 return 0; \
785 } \
786 }
787
788 #define common_eqv(x, y) ((x)==(y))
789 #define common_lt(x, y) ((x)<(y))
790
791 #define f16eqv(a, b) SCM_HALF_FLOAT_CMP(==, a, b)
792 #define f16lt(a, b) SCM_HALF_FLOAT_CMP(<, a, b)
793
794 static inline int c32eqv(ScmHalfComplex x, ScmHalfComplex y)
795 {
796 return (SCM_HALF_FLOAT_CMP(==, x.r, y.r)
797 && SCM_HALF_FLOAT_CMP(==, x.i, y.i));
798 }
799
800 static inline int c32lt(ScmHalfComplex x, ScmHalfComplex y)
801 {
802 return (SCM_HALF_FLOAT_CMP(<, x.r, y.r)
803 || (SCM_HALF_FLOAT_CMP(==, x.r, y.r)
804 && SCM_HALF_FLOAT_CMP(<, x.i, y.i)));
805 }
806
807 static inline int c64lt(ScmFloatComplex x, ScmFloatComplex y)
808 {
809 return (crealf(x) < crealf(y)
810 || (crealf(x) == crealf(y)
811 && cimagf(x) < cimagf(y)));
812 }
813
814 static inline int c128lt(ScmDoubleComplex x, ScmDoubleComplex y)
815 {
816 return (creal(x) < creal(y)
817 || (creal(x) == creal(y)
818 && cimag(x) < cimag(y)));
819 }
820
821
822 DEF_CMP(S8, s8, int8_t, common_eqv, common_lt)
823 DEF_CMP(U8, u8, uint8_t, common_eqv, common_lt)
824 DEF_CMP(S16, s16, int16_t, common_eqv, common_lt)
825 DEF_CMP(U16, u16, uint16_t, common_eqv, common_lt)
826 DEF_CMP(S32, s32, int32_t, common_eqv, common_lt)
827 DEF_CMP(U32, u32, uint32_t, common_eqv, common_lt)
828 DEF_CMP(S64, s64, int64_t, common_eqv, common_lt)
829 DEF_CMP(U64, u64, uint64_t, common_eqv, common_lt)
830 DEF_CMP(F16, f16, ScmHalfFloat, f16eqv, f16lt)
831 DEF_CMP(F32, f32, float, common_eqv, common_lt)
832 DEF_CMP(F64, f64, double, common_eqv, common_lt)
833 DEF_CMP(C32, c32, ScmHalfComplex, c32eqv, c32lt)
834 DEF_CMP(C64, c64, ScmFloatComplex, common_eqv, c64lt)
835 DEF_CMP(C128, c128, ScmDoubleComplex, common_eqv, c128lt)
836
837 /*
838 * Extract mmapped region
839 */
840
841 /* Returns a uvector of KLASS with length LEN, that provides a view
842 to the memory region MEM starting from OFFSET.
843 LEN can be negative, in that case byte OFFSET to the end of the memory region
844 is viewed. If MEM is mapped read-only, the resulting uvector becomes
845 immutable. Otherwise, the resulting vector's immutablity is
846 defined by IMMUTABLE flag.
847 */
848 ScmObj Scm_MakeViewUVector(ScmMemoryRegion *mem, ScmClass *klass,
849 ScmSmallInt len, ScmSmallInt offset,
850 int immutable)
851 {
852 if (offset < 0) Scm_Error("offset must not be negative: %ld", offset);
853 int esize = Scm_UVectorElementSize(klass);
854 if (esize < 0) Scm_Error("uvector class required, but got: %S", klass);
855 if ((offset % esize) != 0) {
856 Scm_Error("offset %ld is not properly aligned for %S", offset, klass);
857 }
858 if (len < 0) len = (mem->size - offset)/esize;
859
860 if (!(mem->prot & PROT_WRITE)) immutable = TRUE;
861 return Scm_MakeUVectorFull(klass, len, mem->ptr + offset, immutable,
862 (void*)mem);
863 }
864
865 /*=====================================================================
866 * Bitvectors
867 */
868
869 static void bitvector_write_int(ScmBitvector *v, int prefix, ScmPort *port)
870 {
871 if (prefix) Scm_Putz("#*", -1, port);
872 for (int i=0; i<SCM_BITVECTOR_SIZE(v); i++) {
873 if (SCM_BITS_TEST(v->bits, i)) SCM_PUTC('1', port);
874 else SCM_PUTC('0', port);
875 }
876 }
877
878 static void bitvector_print(ScmObj obj,
879 ScmPort *port,
880 ScmWriteContext *ctx SCM_UNUSED)
881 {
882 bitvector_write_int(SCM_BITVECTOR(obj), TRUE, port);
883 }
884
885 static int bitvector_compare(ScmObj x, ScmObj y, int equalp SCM_UNUSED)
886 {
887 SCM_ASSERT(SCM_BITVECTORP(x)&&SCM_BITVECTORP(y));
888 ScmBits *bx = SCM_BITVECTOR_BITS(x);
889 ScmBits *by = SCM_BITVECTOR_BITS(y);
890 ScmWord xlen = SCM_BITVECTOR_SIZE(x);
891 ScmWord ylen = SCM_BITVECTOR_SIZE(y);
892
893 /* NB: The ordering is somewhat counterintuitive, for the bits are
894 stored in little-endian. That is, #*0100 comes after #*1000.
895 This is a lot faster. Srfi-178 doesn't define the ordering. */
896 if (xlen < ylen) return -1;
897 if (xlen > ylen) return 1;
898 size_t nw = SCM_BITS_NUM_WORDS(xlen);
899 for (size_t i = 0; i < nw; i++, bx++, by++) {
900 if (*bx < *by) return -1;
901 if (*bx > *by) return 1;
902 }
903 return 0;
904 }
905
906 SCM_DEFINE_BUILTIN_CLASS_FLAGS(Scm_BitvectorClass,
907 bitvector_print, bitvector_compare,
908 NULL, NULL, SCM_CLASS_SEQUENCE_CPL,
909 SCM_CLASS_AGGREGATE);
910
911
912 int Scm_Bit2Int(ScmObj bit)
913 {
914 if (SCM_EQ(bit, SCM_TRUE) || SCM_EQ(bit, SCM_MAKE_INT(1))) return 1;
915 if (SCM_FALSEP(bit) || SCM_EQ(bit, SCM_MAKE_INT(0))) return 0;
916 Scm_Error("bit value must be 0, 1, #f or #t, but got: %S", bit);
917 return 0; /* dummy */
918 }
919
920 ScmObj Scm_Bit2Bool(ScmObj bit)
921 {
922 if (SCM_EQ(bit, SCM_TRUE) || SCM_EQ(bit, SCM_MAKE_INT(1))) return SCM_TRUE;
923 if (SCM_FALSEP(bit) || SCM_EQ(bit, SCM_MAKE_INT(0))) return SCM_FALSE;
924 Scm_Error("bit value must be 0, 1, #f or #t, but got: %S", bit);
925 return SCM_UNDEFINED; /* dummy */
926 }
927
928 /* init can be 0, 1, #f or #t. */
929 ScmObj Scm_MakeBitvector(ScmSmallInt size, ScmObj init)
930 {
931 if (size < 0) {
932 Scm_Error("bitvector size must be a positive integer, but got %d", size);
933 }
934 ScmBitvector *v = SCM_NEW(ScmBitvector);
935 SCM_SET_CLASS(v, SCM_CLASS_BITVECTOR);
936 v->size_flags = (size << 1);
937 v->bits = Scm_MakeBits(size);
938
939 int fill = Scm_Bit2Int(init);
940 Scm_BitsFill(v->bits, 0, size, fill);
941 return SCM_OBJ(v);
942 }
943
944 ScmObj Scm_ListToBitvector(ScmObj lis)
945 {
946 ScmSmallInt len = Scm_Length(lis);
947 if (len < 0) {
948 Scm_Error("proper list required, but got: %S", lis);
949 }
950
951 ScmBitvector *v = SCM_BITVECTOR(Scm_MakeBitvector(len, SCM_FALSE));
952 ScmObj cp;
953 ScmSmallInt i = 0;
954 SCM_FOR_EACH(cp, lis) {
955 if (Scm_Bit2Int(SCM_CAR(cp))) SCM_BITS_SET(v->bits, i);
956 else SCM_BITS_RESET(v->bits, i);
957 i++;
958 }
959 return SCM_OBJ(v);
960 }
961
962 /* Parse string of 0's and 1's to a bitvector. If PREFIX is true,
963 "#*" prefix is assumed. Returns #f if unparsable. */
964
965 ScmObj Scm_StringToBitvector(ScmString *s, int prefix)
966 {
967 const ScmStringBody *b = SCM_STRING_BODY(s);
968 const char *p = SCM_STRING_BODY_START(b);
969 /* multibyte string can't be a bitvector literal. */
970 if (SCM_STRING_BODY_SIZE(b) != SCM_STRING_BODY_LENGTH(b)) return SCM_FALSE;
971 ScmSmallInt len = SCM_STRING_BODY_LENGTH(b);
972
973 if (prefix) {
974 if (len < 2) return SCM_FALSE;
975 if (strncmp(p, "#*", 2) != 0) return SCM_FALSE;
976 p += 2;
977 len -= 2;
978 }
979
980 ScmBitvector *v = SCM_BITVECTOR(Scm_MakeBitvector(len, SCM_FALSE));
981 int i = 0;
982 for (; i < len; p++, i++) {
983 if (*p == '0') SCM_BITS_RESET(v->bits, i);
984 else if (*p == '1') SCM_BITS_SET(v->bits, i);
985 else return SCM_FALSE;
986 }
987 return SCM_OBJ(v);
988 }
989
990 ScmObj Scm_BitvectorToString(ScmBitvector *v, int prefix)
991 {
992 ScmObj out = Scm_MakeOutputStringPort(TRUE);
993 bitvector_write_int(v, prefix, SCM_PORT(out));
994 return Scm_GetOutputString(SCM_PORT(out), 0);
995 }
996
997 ScmObj Scm_BitvectorCopy(ScmBitvector *v, ScmSmallInt start, ScmSmallInt end)
998 {
999 ScmSmallInt size = SCM_BITVECTOR_SIZE(v);
1000 SCM_CHECK_START_END(start, end, size);
1001 ScmBitvector *vv = SCM_BITVECTOR(Scm_MakeBitvector(end-start, SCM_FALSE));
1002 Scm_BitsCopyX(vv->bits, 0, v->bits, start, end);
1003 return SCM_OBJ(vv);
1004 }
1005
1006 ScmObj Scm_BitvectorCopyX(ScmBitvector *dest, ScmSmallInt dstart,
1007 ScmBitvector *src,
1008 ScmSmallInt sstart, ScmSmallInt send)
1009 {
1010 SCM_BITVECTOR_CHECK_MUTABLE(dest);
1011 ScmSmallInt ssize = SCM_BITVECTOR_SIZE(src);
1012 SCM_CHECK_START_END(sstart, send, ssize);
1013 ScmSmallInt dsize = SCM_BITVECTOR_SIZE(dest);
1014 ScmSmallInt dend = dstart + send - sstart;
1015 if (dstart > dsize || dstart < 0 || dend > dsize) {
1016 Scm_Error("destination index out of range (size=%ld, start=%ld, end=%ld)",
1017 dsize, dstart, dend);
1018 }
1019
1020 Scm_BitsCopyX(dest->bits, dstart, src->bits, sstart, send);
1021 return SCM_OBJ(dest);
1022 }
1023
1024
1025 /*=====================================================================
1026 * Utility
1027 */
1028
1029 const uint8_t *Scm_GetBytes(ScmObj obj, ScmSize *size)
1030 {
1031 if (SCM_UVECTORP(obj)) {
1032 *size = Scm_UVectorSizeInBytes(SCM_UVECTOR(obj));
1033 return (const uint8_t*)SCM_UVECTOR_ELEMENTS(obj);
1034 } else if (SCM_STRINGP(obj)) {
1035 ScmSmallInt s;
1036 const char *z = Scm_GetStringContent(SCM_STRING(obj), &s, 0, 0);
1037 *size = s;
1038 return (const uint8_t*)z;
1039 } else {
1040 *size = 0;
1041 return 0;
1042 }
1043 }