Debian Codegraph @

ext/uvector/uvector.c.tmpl

raw
1 ///; Process this file with uvgen.scm to generate uvect.c
2 ///; Lines beginning with '///' are directives for ugven.scm.
3
4 ///;; Prologue ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5 ///(define *tmpl-prologue* (list
6 /*
7 * uvector.c.tmpl - uniform vector support code template
8 *
9 * Copyright (c) 1999-2007 Shiro Kawai, All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 *
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 *
22 * 3. Neither the name of the authors nor the names of its contributors
23 * may be used to endorse or promote products derived from this
24 * software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
32 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <stdlib.h>
40 #include <math.h>
41 #include <limits.h>
42 #include <string.h> /* for memmove() */
43 #include <gauche.h>
44 #include <gauche/extend.h>
45 #include <gauche/priv/builtin-syms.h>
46 #include <gauche/priv/arith.h>
47 #include <gauche/priv/bytesP.h>
48 #include <gauche/endian.h>
49 #include <gauche/scmconst.h>
50
51 #define EXTUVECTOR_EXPORTS
52 #include "gauche/uvector.h"
53 #include "uvectorP.h"
54
55 /*
56 * Generic aliasing
57 */
58 ScmObj Scm_UVectorAlias(ScmClass *klass, ScmUVector *v,
59 ScmSmallInt start, ScmSmallInt end)
60 {
61 ScmSmallInt len = SCM_UVECTOR_SIZE(v), dstsize;
62
63 SCM_CHECK_START_END(start, end, len);
64 int reqalign = Scm_UVectorElementSize(klass);
65 int srcalign = Scm_UVectorElementSize(Scm_ClassOf(SCM_OBJ(v)));
66 if (reqalign < 0) {
67 Scm_Error("uvector-alias requires uniform vector class, but got %S",
68 klass);
69 }
70 if ((start*srcalign)%reqalign != 0 || (end*srcalign)%reqalign != 0) {
71 Scm_Error("aliasing %S of range (%d, %d) to %S doesn't satisfy alignment requirement.",
72 Scm_ClassOf(SCM_OBJ(v)), start, end, klass);
73 }
74 if (reqalign >= srcalign) dstsize = (end-start) / (reqalign/srcalign);
75 else dstsize = (end-start) * (srcalign/reqalign);
76 SCM_RETURN(Scm_MakeUVectorFull(klass,
77 dstsize,
78 (char*)v->elements + start*srcalign,
79 SCM_UVECTOR_IMMUTABLE_P(v),
80 SCM_UVECTOR_OWNER(v)));
81 }
82
83 /*===========================================================
84 * Helper functions
85 */
86
87 static void size_mismatch(const char *name, ScmObj x, ScmObj y)
88 {
89 Scm_Error("%s: argument object sizes do not match: %S vs %S", name, x, y);
90 }
91
92 /* many numeric op procedures takes either uvector, vector, list or
93 a constant number as the second arg. this factors out the common
94 code. */
95 typedef enum {
96 ARGTYPE_UVECTOR,
97 ARGTYPE_VECTOR,
98 ARGTYPE_LIST,
99 ARGTYPE_CONST
100 } ArgType;
101
102 static ArgType arg2_check(const char *name, ScmObj x, ScmObj y, int const_ok)
103 {
104 int size = SCM_UVECTOR_SIZE(x);
105 if (SCM_UVECTORP(y)) {
106 if (SCM_UVECTOR_SIZE(y) != size) size_mismatch(name, SCM_OBJ(x), y);
107 return ARGTYPE_UVECTOR;
108 } else if (SCM_VECTORP(y)) {
109 if (SCM_VECTOR_SIZE(y) != size) size_mismatch(name, SCM_OBJ(x), y);
110 return ARGTYPE_VECTOR;
111 } else if (SCM_LISTP(y)) {
112 if (Scm_Length(y) != size) size_mismatch(name, SCM_OBJ(x), y);
113 return ARGTYPE_LIST;
114 } else if (!const_ok) {
115 Scm_Error("%s: second operand must be either a matching uvector, a vector, or a list, but got %S", name, y);
116 } else if (!SCM_REALP(y)) {
117 Scm_Error("%s: second operand must be either a matching uvector, a vector, a list or a number, but got %S", name, y);
118 }
119 return ARGTYPE_CONST;
120 }
121
122 ///)) ;; End prologue
123
124 ///;; Begin template ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
125 ///;;
126 ///;; Straight substitutions
127 ///;; ${t} -> s8, u8, s16, ...
128 ///;; ${T} -> S8, U8, S16, ...
129 ///;; ${etype} -> char, u_char, short, ...
130 ///;; ${ntype} -> long, long, long, ... (type big enough to hold element)
131 ///;;
132 ///;; Parameterized substitutions
133 ///;; ${UNBOX dst src clamp} where dst::${etype}, src::ScmObj, clamp::int
134 ///;; -> C stmt that unboxes src and set it to dst.
135 ///;; ${BOX dst src} where dst::ScmObj, src::${etype}
136 ///;; -> C stmt that boxes src and set it to dst.
137 ///;; ${VMBOX dst src} where dst::ScmObj, src::${etype}
138 ///;; -> C stmt that boxes src and set it to dst.
139 ///;; (used in Scm_VM* API)
140 ///;; ${NBOX dst src} where dst::ScmObj, src::${ntype}
141 ///;; -> Like BOX, but the source value is given in ntype.
142 ///;; Can be implemented with CAST_N2E and BOX, but some types may
143 ///;; have a shortcut.
144 ///;; ${REF_NTYPE array i}
145 ///;; -> C expr to extract i-th element of etype array *as ntype*.
146 ///;; ${CAST_N2E expr}
147 ///;; -> convert ntype expr into etype.
148 ///;; ${PRINT out elt} where out::ScmPort*, elt::${etype}
149 ///;; -> C stmt to print elt
150 ///;; ${EQ x y} where x, y ::${etype}
151 ///;; -> compare two values
152
153
154 ///(define *tmpl-body* '(
155
156 /*---------------------------------------------------------------
157 * ${T}Vector
158 */
159
160 /*
161 * Class stuff
162 */
163
164 /*
165 * Converters
166 */
167
168 ScmObj Scm_ObjArrayTo${T}Vector(ScmObj *arr, int size, int clamp SCM_UNUSED)
169 {
170 ScmUVector *vec = (ScmUVector*)Scm_Make${T}Vector(size, ${ZERO});
171 for (int i=0; i<size; i++) {
172 ${etype} elt;
173 ScmObj obj = arr[i];
174 ${UNBOX elt obj clamp};
175 SCM_${T}VECTOR_ELEMENTS(vec)[i] = elt;
176 }
177 return SCM_OBJ(vec);
178 }
179
180 ScmObj Scm_VectorTo${T}Vector(ScmVector *ivec, int start, int end, int clamp)
181 {
182 int length = SCM_VECTOR_SIZE(ivec);
183 SCM_CHECK_START_END(start, end, length);
184 return Scm_ObjArrayTo${T}Vector(SCM_VECTOR_ELEMENTS(ivec)+start,
185 end-start, clamp);
186 }
187
188 /*
189 * Accessors and modifiers
190 */
191
192 ScmObj Scm_${T}VectorFill(ScmUVector *vec, ${etype} fill, int start, int end)
193 {
194 int size = SCM_${T}VECTOR_SIZE(vec);
195 SCM_CHECK_START_END(start, end, size);
196 SCM_UVECTOR_CHECK_MUTABLE(vec);
197 for (int i=start; i<end; i++) SCM_${T}VECTOR_ELEMENTS(vec)[i] = fill;
198 return SCM_OBJ(vec);
199 }
200
201 ScmObj Scm_${T}VectorToList(ScmUVector *vec, int start, int end)
202 {
203 ScmObj head = SCM_NIL, tail = SCM_NIL;
204 int size = SCM_${T}VECTOR_SIZE(vec);
205 SCM_CHECK_START_END(start, end, size);
206 for (int i=start; i<end; i++) {
207 ScmObj obj;
208 ${etype} elt = SCM_${T}VECTOR_ELEMENTS(vec)[i];
209 ${BOX obj elt};
210 SCM_APPEND1(head, tail, obj);
211 }
212 return head;
213 }
214
215 ScmObj Scm_${T}VectorToVector(ScmUVector *vec, int start, int end)
216 {
217 int size = SCM_${T}VECTOR_SIZE(vec);
218 SCM_CHECK_START_END(start, end, size);
219 ScmObj ovec = Scm_MakeVector(end-start, SCM_UNDEFINED);
220 for (int i=start; i<end; i++) {
221 ScmObj obj;
222 ${etype} elt = SCM_${T}VECTOR_ELEMENTS(vec)[i];
223 ${BOX obj elt};
224 SCM_VECTOR_ELEMENT(ovec, i-start) = obj;
225 }
226 return ovec;
227 }
228
229 ScmObj Scm_${T}VectorCopy(ScmUVector *vec, ScmSmallInt start, ScmSmallInt end)
230 {
231 ScmSmallInt size = SCM_${T}VECTOR_SIZE(vec);
232 SCM_CHECK_START_END(start, end, size);
233 return Scm_Make${T}VectorFromArray(end-start,
234 SCM_${T}VECTOR_ELEMENTS(vec)+start);
235 }
236
237 ScmObj Scm_${T}VectorCopyX(ScmUVector *dst,
238 ScmSmallInt dstart,
239 ScmUVector *src,
240 ScmSmallInt sstart,
241 ScmSmallInt send)
242 {
243 ScmSmallInt dlen = SCM_${T}VECTOR_SIZE(dst);
244 ScmSmallInt slen = SCM_${T}VECTOR_SIZE(src);
245 ScmSmallInt size;
246
247 SCM_UVECTOR_CHECK_MUTABLE(dst);
248 SCM_CHECK_START_END(sstart, send, slen);
249
250 if (dstart < 0 || dstart >= dlen) return SCM_OBJ(dst);
251 if (dlen - dstart > send - sstart) size = send - sstart;
252 else size = dlen - dstart;
253
254 memmove(SCM_${T}VECTOR_ELEMENTS(dst) + dstart,
255 SCM_${T}VECTOR_ELEMENTS(src) + sstart,
256 size * sizeof(${etype}));
257 return SCM_OBJ(dst);
258 }
259
260 ///)) ;; end of tmpl-body
261
262 ///;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
263 ///;; Numeric operator template
264 ///(append! *tmpl-prologue* (list
265 /****** Add, sub, mul and div (f32 and f64 only) *****/
266 #define s8s8_add(x, y, clamp) clamp_s8(x+y, clamp)
267 #define s8s8_sub(x, y, clamp) clamp_s8(x-y, clamp)
268 #define s8s8_mul(x, y, clamp) clamp_s8(x*y, clamp)
269
270 static inline long s8g_add(long x, long y, int clamp)
271 {
272 if (y > 255) return range_s8hi(0, clamp);
273 if (y < -256) return range_s8lo(0, clamp);
274 return clamp_s8(x+y, clamp);
275 }
276
277 static inline long s8g_sub(long x, long y, int clamp)
278 {
279 if (y < -255) return range_s8hi(0, clamp);
280 if (y > 256) return range_s8lo(0, clamp);
281 return clamp_s8(x-y, clamp);
282 }
283
284 static inline long s8g_mul(long x, long y, int clamp)
285 {
286 if (x == 0) return 0;
287 if (y > 128) return (x>0)?range_s8hi(0, clamp):range_s8lo(0, clamp);
288 return clamp_s8(x*y, clamp);
289 }
290
291 #define u8u8_add(x, y, clamp) clamp_u8(x+y, clamp)
292 #define u8u8_sub(x, y, clamp) clamp_u8((long)(x-y), clamp)
293 #define u8u8_mul(x, y, clamp) clamp_u8(x*y, clamp)
294
295 static inline u_long u8g_add(u_long x, u_long y, int clamp)
296 {
297 if (y > 255) return range_u8hi(0, clamp);
298 return clamp_u8(x+y, clamp);
299 }
300
301 static inline u_long u8g_sub(u_long x, u_long y, int clamp)
302 {
303 if (y > x) return range_u8lo(0, clamp);
304 return x-y; /* never overflows */
305 }
306
307 static inline u_long u8g_mul(u_long x, u_long y, int clamp)
308 {
309 if (x == 0) return 0;
310 if (y > 255) return range_u8hi(0, clamp);
311 return clamp_u8(x*y, clamp);
312 }
313
314 #define s16s16_add(x, y, clamp) clamp_s16(x+y, clamp)
315 #define s16s16_sub(x, y, clamp) clamp_s16(x-y, clamp)
316 #define s16s16_mul(x, y, clamp) clamp_s16(x*y, clamp)
317
318 static inline long s16g_add(long x, long y, int clamp)
319 {
320 if (y > 65535) return range_s16hi(0, clamp);
321 if (y < -65536) return range_s16lo(0, clamp);
322 return clamp_s16(x+y, clamp);
323 }
324
325 static inline long s16g_sub(long x, long y, int clamp)
326 {
327 if (y < -65535) return range_s16hi(0, clamp);
328 if (y > 65536) return range_s16lo(0, clamp);
329 return clamp_s16(x-y, clamp);
330 }
331
332 static inline long s16g_mul(long x, long y, int clamp)
333 {
334 if (x == 0) return 0;
335 if (y > 32767) return (x>0)?range_s16hi(0, clamp):range_s16lo(0, clamp);
336 return clamp_s16(x*y, clamp);
337 }
338
339 #define u16u16_add(x, y, clamp) clamp_u16(x+y, clamp)
340 #define u16u16_sub(x, y, clamp) clamp_u16((long)(x-y), clamp)
341 #define u16u16_mul(x, y, clamp) clamp_u16(x*y, clamp)
342
343 static inline u_long u16g_add(u_long x, u_long y, int clamp)
344 {
345 if (y > 65535) return range_u16hi(0, clamp);
346 return clamp_u16(x+y, clamp);
347 }
348
349 static inline u_long u16g_sub(u_long x, u_long y, int clamp)
350 {
351 if (y > x) return range_u16lo(0, clamp);
352 return x-y; /* never overflows */
353 }
354
355 static inline u_long u16g_mul(u_long x, u_long y, int clamp)
356 {
357 if (x == 0) return 0;
358 if (y > 65535) return range_u16hi(0, clamp);
359 return clamp_u16(x*y, clamp);
360 }
361
362 #if SIZEOF_LONG == 4
363 #define s32s32_add(x, y, clamp) s32_add_safe(x, y, clamp)
364 #define s32s32_sub(x, y, clamp) s32_sub_safe(x, y, clamp)
365 #define s32s32_mul(x, y, clamp) s32_mul_safe(x, y, clamp)
366 #define s32g_add(x, y, clamp) s32_add_safe(x, y, clamp)
367 #define s32g_sub(x, y, clamp) s32_sub_safe(x, y, clamp)
368 #define s32g_mul(x, y, clamp) s32_mul_safe(x, y, clamp)
369 #else /* SIZEOF_LONG >= 8 */
370 #define s32s32_add(x, y, clamp) clamp_s32(x+y, clamp)
371 #define s32s32_sub(x, y, clamp) clamp_s32(x-y, clamp)
372 #define s32s32_mul(x, y, clamp) clamp_s32(x*y, clamp)
373 #define s32g_add(x, y, clamp) s32_add_safe(x, y, clamp)
374 #define s32g_sub(x, y, clamp) s32_sub_safe(x, y, clamp)
375 #define s32g_mul(x, y, clamp) s32_mul_safe(x, y, clamp)
376 #endif /* SIZEOF_LONG >= 8 */
377
378 static inline long s32_add_safe(long x, long y, int clamp)
379 {
380 long r, v;
381 SADDOV(r, v, x, y);
382 if (v == 0) return clamp_s32(r, clamp);
383 if (v > 0) return range_s32hi(0, clamp);
384 else return range_s32lo(0, clamp);
385 }
386
387
388 static inline long s32_sub_safe(long x, long y, int clamp)
389 {
390 long r, v;
391 SSUBOV(r, v, x, y);
392 if (v == 0) return clamp_s32(r, clamp);
393 if (v > 0) return range_s32hi(0, clamp);
394 else return range_s32lo(0, clamp);
395 }
396
397 static inline long s32_mul_safe(long x, long y, int clamp)
398 {
399 long r, v;
400 SMULOV(r, v, x, y);
401 if (v == 0) return clamp_s32(r, clamp);
402 if (v > 0) return range_s32hi(0, clamp);
403 else return range_s32lo(0, clamp);
404 }
405
406 #if SIZEOF_LONG == 4
407 #define u32u32_add(x, y, clamp) u32_add_safe(x, y, clamp)
408 #define u32u32_sub(x, y, clamp) u32_sub_safe(x, y, clamp)
409 #define u32u32_mul(x, y, clamp) u32_mul_safe(x, y, clamp)
410 #define u32g_add(x, y, clamp) u32_add_safe(x, y, clamp)
411 #define u32g_sub(x, y, clamp) u32_sub_safe(x, y, clamp)
412 #define u32g_mul(x, y, clamp) u32_mul_safe(x, y, clamp)
413 #else /* SIZEOF_LONG >= 8 */
414 #define u32u32_add(x, y, clamp) clamp_u32(x+y, clamp)
415 #define u32u32_sub(x, y, clamp) u32_sub_safe(x, y, clamp)
416 #define u32u32_mul(x, y, clamp) clamp_u32(x*y, clamp)
417 #define u32g_add(x, y, clamp) u32_add_safe(x, y, clamp)
418 #define u32g_sub(x, y, clamp) u32_sub_safe(x, y, clamp)
419 #define u32g_mul(x, y, clamp) u32_mul_safe(x, y, clamp)
420 #endif /* SIZEOF_LONG >= 8 */
421
422 static inline u_long u32_add_safe(u_long x, u_long y, int clamp)
423 {
424 u_long r, v;
425 UADDOV(r, v, x, y);
426 if (v == 0) return clamp_u32(r, clamp);
427 else return range_u32hi(0, clamp);
428 }
429
430 static inline u_long u32_sub_safe(u_long x, u_long y, int clamp)
431 {
432 u_long r, v;
433 USUBOV(r, v, x, y);
434 if (v == 0) return clamp_u32(r, clamp);
435 else return range_u32lo(0, clamp);
436 }
437
438 static inline u_long u32_mul_safe(u_long x, u_long y, int clamp)
439 {
440 u_long r, v;
441 UMULOV(r, v, x, y);
442 if (v == 0) return clamp_u32(r, clamp);
443 else return range_u32hi(0, clamp);
444 }
445
446 #define s64s64_add(x, y, clamp) s64g_add(x, y, clamp)
447 #define s64s64_sub(x, y, clamp) s64g_sub(x, y, clamp)
448 #define s64s64_mul(x, y, clamp) s64g_mul(x, y, clamp)
449
450 static inline int64_t s64g_add(int64_t x, int64_t y, int clamp)
451 {
452 #if SIZEOF_LONG == 4
453 ScmObj xx = Scm_MakeInteger64(x);
454 ScmObj yy = Scm_MakeInteger64(y);
455 ScmObj r = Scm_Add(xx, yy);
456 return Scm_GetInteger64Clamp(r, clamp, NULL);
457 #else
458 long r, v;
459 SADDOV(r, v, x, y);
460 if (v == 0) return r;
461 if (v > 0) return range_s64hi(0, clamp);
462 else return range_s64lo(0, clamp);
463 #endif
464 }
465
466 static inline int64_t s64g_sub(int64_t x, int64_t y, int clamp)
467 {
468 #if SIZEOF_LONG == 4
469 ScmObj xx = Scm_MakeInteger64(x);
470 ScmObj yy = Scm_MakeInteger64(y);
471 ScmObj r = Scm_Sub(xx, yy);
472 return Scm_GetInteger64Clamp(r, clamp, NULL);
473 #else
474 long r, v;
475 SSUBOV(r, v, x, y);
476 if (v == 0) return r;
477 if (v > 0) return range_s64hi(0, clamp);
478 else return range_s64lo(0, clamp);
479 #endif
480 }
481
482 static inline int64_t s64g_mul(int64_t x, int64_t y, int clamp)
483 {
484 #if SIZEOF_LONG == 4
485 ScmObj xx = Scm_MakeInteger64(x);
486 ScmObj yy = Scm_MakeInteger64(y);
487 ScmObj r = Scm_Mul(xx, yy);
488 return Scm_GetInteger64Clamp(r, clamp, NULL);
489 #else
490 long r, v;
491 SMULOV(r, v, x, y);
492 if (v == 0) return r;
493 if (v > 0) return range_s64hi(0, clamp);
494 else return range_s64lo(0, clamp);
495 #endif
496 }
497
498 #define u64u64_add(x, y, clamp) u64g_add(x, y, clamp)
499 #define u64u64_sub(x, y, clamp) u64g_sub(x, y, clamp)
500 #define u64u64_mul(x, y, clamp) u64g_mul(x, y, clamp)
501
502 static inline uint64_t u64g_add(uint64_t x, uint64_t y, int clamp)
503 {
504 #if SIZEOF_LONG == 4
505 ScmObj xx = Scm_MakeIntegerU64(x);
506 ScmObj yy = Scm_MakeIntegerU64(y);
507 ScmObj r = Scm_Add(xx, yy);
508 return Scm_GetIntegerU64Clamp(r, clamp, NULL);
509 #else
510 u_long r, v;
511 UADDOV(r, v, x, y);
512 if (v == 0) return r;
513 else return range_u64hi(0, clamp);
514 #endif
515 }
516
517 static inline uint64_t u64g_sub(uint64_t x, uint64_t y, int clamp)
518 {
519 #if SIZEOF_LONG == 4
520 ScmObj xx = Scm_MakeIntegerU64(x);
521 ScmObj yy = Scm_MakeIntegerU64(y);
522 ScmObj r = Scm_Sub(xx, yy);
523 return Scm_GetIntegerU64Clamp(r, clamp, NULL);
524 #else
525 u_long r, v;
526 USUBOV(r, v, x, y);
527 if (v == 0) return r;
528 else return range_u64lo(0, clamp);
529 #endif
530 }
531
532 static inline uint64_t u64g_mul(uint64_t x, uint64_t y, int clamp)
533 {
534 #if SIZEOF_LONG == 4
535 ScmObj xx = Scm_MakeIntegerU64(x);
536 ScmObj yy = Scm_MakeIntegerU64(y);
537 ScmObj r = Scm_Mul(xx, yy);
538 return Scm_GetIntegerU64Clamp(r, clamp, NULL);
539 #else
540 u_long r, v;
541 UMULOV(r, v, x, y);
542 if (v == 0) return r;
543 else return range_u64hi(0, clamp);
544 #endif
545 }
546
547 #define f16f16_add(x, y, clamp) (x+y)
548 #define f16f16_sub(x, y, clamp) (x-y)
549 #define f16f16_mul(x, y, clamp) (x*y)
550 #define f16f16_div(x, y, clamp) (x/y)
551
552 #define f16g_add(x, y, clamp) (x+y)
553 #define f16g_sub(x, y, clamp) (x-y)
554 #define f16g_mul(x, y, clamp) (x*y)
555 #define f16g_div(x, y, clamp) (x/y)
556
557 #define f32f32_add(x, y, clamp) (x+y)
558 #define f32f32_sub(x, y, clamp) (x-y)
559 #define f32f32_mul(x, y, clamp) (x*y)
560 #define f32f32_div(x, y, clamp) (x/y)
561
562 #define f32g_add(x, y, clamp) (x+y)
563 #define f32g_sub(x, y, clamp) (x-y)
564 #define f32g_mul(x, y, clamp) (x*y)
565 #define f32g_div(x, y, clamp) (x/y)
566
567 #define f64f64_add(x, y, clamp) (x+y)
568 #define f64f64_sub(x, y, clamp) (x-y)
569 #define f64f64_mul(x, y, clamp) (x*y)
570 #define f64f64_div(x, y, clamp) (x/y)
571
572 #define f64g_add(x, y, clamp) (x+y)
573 #define f64g_sub(x, y, clamp) (x-y)
574 #define f64g_mul(x, y, clamp) (x*y)
575 #define f64g_div(x, y, clamp) (x/y)
576
577 static inline ScmDoubleComplex complex_half_to_double(ScmHalfComplex x)
578 {
579 return (ScmDoubleComplex)(Scm_HalfToDouble(x.r)
580 + Scm_HalfToDouble(x.i) * _Complex_I);
581 }
582
583 static inline ScmHalfComplex complex_double_to_half(ScmDoubleComplex x)
584 {
585 return (ScmHalfComplex){Scm_DoubleToHalf(creal(x)),
586 Scm_DoubleToHalf(cimag(x))};
587 }
588
589
590 static inline ScmHalfComplex c32c32_add(ScmHalfComplex x,
591 ScmHalfComplex y,
592 int clamp SCM_UNUSED)
593 {
594 ScmDoubleComplex cx = complex_half_to_double(x);
595 ScmDoubleComplex cy = complex_half_to_double(y);
596 return complex_double_to_half(cx+cy);
597 }
598
599 static inline ScmHalfComplex c32c32_sub(ScmHalfComplex x,
600 ScmHalfComplex y,
601 int clamp SCM_UNUSED)
602 {
603 ScmDoubleComplex cx = complex_half_to_double(x);
604 ScmDoubleComplex cy = complex_half_to_double(y);
605 return complex_double_to_half(cx-cy);
606 }
607
608 static inline ScmHalfComplex c32c32_mul(ScmHalfComplex x,
609 ScmHalfComplex y,
610 int clamp SCM_UNUSED)
611 {
612 ScmDoubleComplex cx = complex_half_to_double(x);
613 ScmDoubleComplex cy = complex_half_to_double(y);
614 return complex_double_to_half(cx*cy);
615 }
616
617 static inline ScmHalfComplex c32c32_div(ScmHalfComplex x,
618 ScmHalfComplex y,
619 int clamp SCM_UNUSED)
620 {
621 ScmDoubleComplex cx = complex_half_to_double(x);
622 ScmDoubleComplex cy = complex_half_to_double(y);
623 return complex_double_to_half(cx/cy);
624 }
625
626
627 #define c32g_add(x, y, clamp) c32c32_add(x, y, clamp)
628 #define c32g_sub(x, y, clamp) c32c32_sub(x, y, clamp)
629 #define c32g_mul(x, y, clamp) c32c32_mul(x, y, clamp)
630 #define c32g_div(x, y, clamp) c32c32_div(x, y, clamp)
631
632 #define c64c64_add(x, y, clamp) (x+y)
633 #define c64c64_sub(x, y, clamp) (x-y)
634 #define c64c64_mul(x, y, clamp) (x*y)
635 #define c64c64_div(x, y, clamp) (x/y)
636
637 #define c64g_add(x, y, clamp) (x+y)
638 #define c64g_sub(x, y, clamp) (x-y)
639 #define c64g_mul(x, y, clamp) (x*y)
640 #define c64g_div(x, y, clamp) (x/y)
641
642 #define c128c128_add(x, y, clamp) (x+y)
643 #define c128c128_sub(x, y, clamp) (x-y)
644 #define c128c128_mul(x, y, clamp) (x*y)
645 #define c128c128_div(x, y, clamp) (x/y)
646
647 #define c128g_add(x, y, clamp) (x+y)
648 #define c128g_sub(x, y, clamp) (x-y)
649 #define c128g_mul(x, y, clamp) (x*y)
650 #define c128g_div(x, y, clamp) (x/y)
651
652
653 /****** Number extraction *****/
654 /* like unbox, but not as strict. sets *oor = TRUE if x is out of range. */
655
656 static inline long s8num(ScmObj x, int *oor)
657 {
658 return Scm_GetIntegerClamp(x, SCM_CLAMP_NONE, oor);
659 }
660
661 #define s16num(x, oor) s8num(x, oor)
662 #define s32num(x, oor) s8num(x, oor)
663
664 static inline u_long u8num(ScmObj x, int *oor)
665 {
666 return Scm_GetIntegerUClamp(x, SCM_CLAMP_NONE, oor);
667 }
668
669 #define u16num(x, oor) u8num(x, oor)
670 #define u32num(x, oor) u8num(x, oor)
671
672 static inline int64_t s64num(ScmObj x, int *oor)
673 {
674 return Scm_GetInteger64Clamp(x, SCM_CLAMP_NONE, oor);
675 }
676
677 static inline uint64_t u64num(ScmObj x, int *oor)
678 {
679 return Scm_GetIntegerU64Clamp(x, SCM_CLAMP_NONE, oor);
680 }
681
682 #define f16num(x, oor) ((*oor = FALSE), Scm_GetDouble(x))
683 #define f32num(x, oor) ((*oor = FALSE),((float)Scm_GetDouble(x)))
684 #define f64num(x, oor) ((*oor = FALSE), Scm_GetDouble(x))
685
686 #define c32num(x, oor) ((*oor = FALSE), Scm_GetHalfComplex(x))
687 #define c64num(x, oor) ((*oor = FALSE), Scm_GetFloatComplex(x))
688 #define c128num(x, oor) ((*oor = FALSE), Scm_GetDoubleComplex(x))
689
690 ///))
691
692 ///(define *tmpl-numop* '(
693 /* NB: s1 can be a register flonum. */
694 static void ${t}vector_${opname}(const char *name,
695 ScmObj d, ScmObj s0, ScmObj s1,
696 int clamp SCM_UNUSED)
697 /* clamp appears in macro call below, but
698 the macro may not use it. */
699 {
700 int size = SCM_${T}VECTOR_SIZE(d), oor;
701 ${ntype} r, v0, v1;
702 ScmObj rr, vv1;
703
704 switch (arg2_check(name, s0, s1, TRUE)) {
705 case ARGTYPE_UVECTOR:
706 for (int i=0; i<size; i++) {
707 v0 = ${REF_NTYPE s0 i};
708 v1 = ${REF_NTYPE s1 i};
709 r = ${t}${t}_${opname}(v0, v1, clamp);
710 SCM_${T}VECTOR_ELEMENTS(d)[i] = ${CAST_N2E r};
711 }
712 break;
713 case ARGTYPE_VECTOR:
714 for (int i=0; i<size; i++) {
715 v0 = ${REF_NTYPE s0 i};
716 vv1 = SCM_VECTOR_ELEMENTS(s1)[i];
717 v1 = ${t}num(vv1, &oor);
718 if (!oor) {
719 r = ${t}g_${opname}(v0, v1, clamp);
720 } else {
721 ${NBOX rr v0};
722 rr = Scm_${Sopname}(rr, vv1);
723 ${NUNBOX r rr clamp};
724 }
725 SCM_${T}VECTOR_ELEMENTS(d)[i] = ${CAST_N2E r};
726 }
727 break;
728 case ARGTYPE_LIST:
729 for (int i=0; i<size; i++) {
730 v0 = ${REF_NTYPE s0 i};
731 vv1 = SCM_CAR(s1); s1 = SCM_CDR(s1);
732 v1 = ${t}num(vv1, &oor);
733 if (!oor) {
734 r = ${t}g_${opname}(v0, v1, clamp);
735 } else {
736 ${NBOX rr v0};
737 rr = Scm_${Sopname}(rr, vv1);
738 ${NUNBOX r rr clamp};
739 }
740 SCM_${T}VECTOR_ELEMENTS(d)[i] = ${CAST_N2E r};
741 }
742 break;
743 case ARGTYPE_CONST:
744 v1 = ${t}num(s1, &oor);
745 for (int i=0; i<size; i++) {
746 v0 = ${REF_NTYPE s0 i};
747 if (!oor) {
748 r = ${t}g_${opname}(v0, v1, clamp);
749 } else {
750 ${NBOX rr v0};
751 rr = Scm_${Sopname}(rr, s1);
752 ${NUNBOX r rr clamp};
753 }
754 SCM_${T}VECTOR_ELEMENTS(d)[i] = ${CAST_N2E r};
755 }
756 }
757 }
758
759 ScmObj Scm_${T}Vector${Opname}(ScmUVector *s0, ScmObj s1, int clamp)
760 {
761 ScmObj d = Scm_MakeUVector(SCM_CLASS_${T}VECTOR,
762 SCM_${T}VECTOR_SIZE(s0),
763 NULL);
764 ${t}vector_${opname}("${t}vector-${opname}", d, SCM_OBJ(s0), s1, clamp);
765 return d;
766 }
767
768 ScmObj Scm_${T}Vector${Opname}X(ScmUVector *s0, ScmObj s1, int clamp)
769 {
770 ${t}vector_${opname}("${t}vector-${opname}!", SCM_OBJ(s0), SCM_OBJ(s0), s1, clamp);
771 return SCM_OBJ(s0);
772 }
773
774 ///)) ;; end of tmpl-numop
775
776 ///;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
777 ///;; Bitwise operator template
778 ///;; very similar to numop, except boundary checks.
779 ///(append! *tmpl-prologue* (list
780 /****** Bit operation *****/
781
782 static inline u_long bitext(ScmObj x)
783 {
784 if (SCM_INTP(x)) return (u_long)SCM_INT_VALUE(x);
785 if (SCM_BIGNUMP(x)) {
786 if (SCM_BIGNUM_SIGN(x) > 0) {
787 return SCM_BIGNUM(x)->values[0];
788 } else {
789 return ~(SCM_BIGNUM(x)->values[0]) + 1;
790 }
791 }
792 Scm_Error("integer required, but got %S", x);
793 return 0;
794 }
795
796 static inline uint64_t bitext64(ScmObj x)
797 {
798 uint64_t r = 0;
799 if (SCM_INTP(x)) r = SCM_INT_VALUE(x);
800 else if (SCM_BIGNUMP(x)) {
801 ScmObj xx = Scm_LogAnd(x, SCM_2_64_MINUS_1);
802 r = Scm_GetIntegerU64(xx);
803 }
804 else {
805 Scm_Error("integer required, but got %S", x);
806 }
807 return r;
808 }
809
810 #define INT64BITOP(r, x, op, y) (r = x op y)
811 ///))
812 ///(define *tmpl-bitop* '(
813 static void ${t}vector_${opname}(const char *name,
814 ScmObj d, ScmObj s0, ScmObj s1)
815 {
816 int size = SCM_${T}VECTOR_SIZE(d);
817 ${ntype} r, v0, v1;
818 ScmObj vv1;
819
820 switch(arg2_check(name, s0, s1, TRUE)) {
821 case ARGTYPE_UVECTOR:
822 for (int i=0; i<size; i++) {
823 v0 = ${REF_NTYPE s0 i};
824 v1 = ${REF_NTYPE s1 i};
825 ${BITOP r v0 v1};
826 SCM_${T}VECTOR_ELEMENTS(d)[i] = ${CAST_N2E r};
827 }
828 break;
829 case ARGTYPE_VECTOR:
830 for (int i=0; i<size; i++) {
831 v0 = ${REF_NTYPE s0 i};
832 vv1 = SCM_VECTOR_ELEMENTS(s1)[i];
833 ${BITEXT v1 vv1};
834 ${BITOP r v0 v1};
835 SCM_${T}VECTOR_ELEMENTS(d)[i] = ${CAST_N2E r};
836 }
837 break;
838 case ARGTYPE_LIST:
839 for (int i=0; i<size; i++) {
840 v0 = ${REF_NTYPE s0 i};
841 vv1 = SCM_VECTOR_ELEMENTS(s1)[i];
842 ${BITEXT v1 vv1};
843 ${BITOP r v0 v1};
844 SCM_${T}VECTOR_ELEMENTS(d)[i] = ${CAST_N2E r};
845 }
846 break;
847 case ARGTYPE_CONST:
848 ${BITEXT v1 s1};
849 for (int i=0; i<size; i++) {
850 v0 = ${REF_NTYPE s0 i};
851 ${BITOP r v0 v1};
852 SCM_${T}VECTOR_ELEMENTS(d)[i] = ${CAST_N2E r};
853 }
854 }
855 }
856
857 ScmObj Scm_${T}Vector${Opname}(ScmUVector *s0, ScmObj s1)
858 {
859 ScmObj d = Scm_MakeUVector(SCM_CLASS_${T}VECTOR,
860 SCM_${T}VECTOR_SIZE(s0),
861 NULL);
862 ${t}vector_${opname}("${t}vector-${opname}", d, SCM_OBJ(s0), s1);
863 return d;
864 }
865
866 ScmObj Scm_${T}Vector${Opname}X(ScmUVector *s0, ScmObj s1)
867 {
868 ${t}vector_${opname}("${t}vector-${opname}!", SCM_OBJ(s0), SCM_OBJ(s0), s1);
869 return SCM_OBJ(s0);
870 }
871 ///)) ;; end of tmpl-bitop
872
873 ///;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
874 ///;; Dot operator template
875 ///(append! *tmpl-prologue* (list
876 /****** Multiply-and-add operation. *****/
877
878 static inline long s8muladd(long x, long y, long acc, ScmObj *sacc)
879 {
880 long k, v, m;
881 m = x * y;
882 SADDOV(k, v, acc, m);
883 if (v) {
884 *sacc = Scm_Add(*sacc, Scm_MakeInteger(acc));
885 return m;
886 } else {
887 return k;
888 }
889 }
890
891 #define s16muladd(x, y, acc, sacc) s8muladd(x, y, acc, sacc)
892
893 static inline long s32muladd(long x, long y, long acc, ScmObj *sacc)
894 {
895 long k, v, m;
896 SMULOV(m, v, x, y);
897 if (v) {
898 *sacc = Scm_Add(*sacc, Scm_Mul(Scm_MakeInteger(x),
899 Scm_MakeInteger(y)));
900 return acc;
901 } else {
902 SADDOV(k, v, acc, m);
903 if (v) {
904 *sacc = Scm_Add(*sacc, Scm_MakeInteger(acc));
905 return m;
906 } else {
907 return k;
908 }
909 }
910 }
911
912 #if SIZEOF_LONG == 4
913 static inline int64_t s64muladd(int64_t x, int64_t y, int64_t acc, ScmObj *sacc)
914 {
915 /* we don't use acc, and operate only on sacc. */
916 *sacc = Scm_Add(*sacc, Scm_Mul(Scm_MakeInteger64(x),
917 Scm_MakeInteger64(y)));
918 return acc;
919 }
920 #else
921 #define s64muladd(x, y, acc, sacc) s32muladd(x, y, acc, sacc)
922 #endif
923
924 static inline u_long u8muladd(u_long x, u_long y, u_long acc, ScmObj *sacc)
925 {
926 u_long k, v, m;
927 m = x * y;
928 UADDOV(k, v, acc, m);
929 if (v) {
930 *sacc = Scm_Add(*sacc, Scm_MakeIntegerU(acc));
931 return m;
932 } else {
933 return k;
934 }
935 }
936
937 #define u16muladd(x, y, acc, sacc) u8muladd(x, y, acc, sacc)
938
939 static inline u_long u32muladd(u_long x, u_long y, u_long acc, ScmObj *sacc)
940 {
941 u_long k, v, m;
942 UMULOV(m, v, x, y);
943 if (v) {
944 *sacc = Scm_Add(*sacc, Scm_Mul(Scm_MakeIntegerU(x),
945 Scm_MakeIntegerU(y)));
946 return acc;
947 } else {
948 UADDOV(k, v, acc, m);
949 if (v) {
950 *sacc = Scm_Add(*sacc, Scm_MakeIntegerU(acc));
951 return m;
952 } else {
953 return k;
954 }
955 }
956 }
957
958 #if SIZEOF_LONG == 4
959 static inline uint64_t u64muladd(uint64_t x, uint64_t y, uint64_t acc, ScmObj *sacc)
960 {
961 /* we don't use acc, and operate only on sacc. */
962 *sacc = Scm_Add(*sacc, Scm_Mul(Scm_MakeIntegerU64(x),
963 Scm_MakeIntegerU64(y)));
964 return acc;
965 }
966 #else
967 #define u64muladd(x, y, acc, sacc) u32muladd(x, y, acc, sacc)
968 #endif
969
970 #define f16muladd(x, y, acc, sacc) (acc + x*y)
971 #define f32muladd(x, y, acc, sacc) (acc + x*y)
972 #define f64muladd(x, y, acc, sacc) (acc + x*y)
973
974 #define c32muladd(x, y, acc, sacc) \
975 (ScmHalfComplex){acc.r + x.r*y.r - x.i*y.i, acc.i + x.i*y.r + x.r*y.i}
976 #define c64muladd(x, y, acc, sacc) (acc + x*y)
977 #define c128muladd(x, y, acc, sacc) (acc + x*y)
978 ///))
979
980 ///(define *tmpl-dotop* '(
981 static ScmObj ${T}VectorDotProd(ScmUVector *x, ScmObj y, int vmp)
982 {
983 int size = SCM_${T}VECTOR_SIZE(x), oor;
984 ${ntype} r, vx, vy;
985 ScmObj rr = SCM_MAKE_INT(0), vvy, vvx;
986
987 r = ${ZERO};
988 switch (arg2_check("${t}vector-dot", SCM_OBJ(x), y, FALSE)) {
989 case ARGTYPE_UVECTOR:
990 for (int i=0; i<size; i++) {
991 vx = ${REF_NTYPE x i};
992 vy = ${REF_NTYPE y i};
993 r = ${t}muladd(vx, vy, r, &rr);
994 }
995 break;
996 case ARGTYPE_VECTOR:
997 for (int i=0; i<size; i++) {
998 vx = ${REF_NTYPE x i};
999 vvy = SCM_VECTOR_ELEMENTS(y)[i];
1000 vy = ${t}num(vvy, &oor);
1001 if (!oor) {
1002 r = ${t}muladd(vx, vy, r, &rr);
1003 } else {
1004 ${NBOX vvx vx};
1005 rr = Scm_Add(rr, Scm_Mul(vvx, vvy));
1006 }
1007 }
1008 break;
1009 case ARGTYPE_LIST:
1010 for (int i=0; i<size; i++) {
1011 vx = ${REF_NTYPE x i};
1012 vvy = SCM_CAR(y); y = SCM_CDR(y);
1013 vy = ${t}num(vvy, &oor);
1014 if (!oor) {
1015 r = ${t}muladd(vx, vy, r, &rr);
1016 } else {
1017 ${NBOX vvx vx};
1018 rr = Scm_Add(rr, Scm_Mul(vvx, vvy));
1019 }
1020 }
1021 break;
1022 case ARGTYPE_CONST:
1023 /* this case should've been eliminated by arg2_check. */
1024 Scm_Panic("something wrong");
1025 }
1026
1027 /* r may contain a value bigger than the normal element value
1028 of ${t}vector, so it needs some care. */
1029 if (SCM_EQ(rr, SCM_MAKE_INT(0))) {
1030 if (vmp) {
1031 ${VMNBOX rr r};
1032 } else {
1033 ${NBOX rr r};
1034 }
1035 } else {
1036 ScmObj sr;
1037 ${NBOX sr r};
1038 rr = Scm_Add(rr, sr);
1039 }
1040 return rr;
1041 }
1042
1043 ScmObj Scm_${T}VectorDotProd(ScmUVector *x, ScmObj y)
1044 {
1045 return ${T}VectorDotProd(x, y, FALSE);
1046 }
1047
1048 ScmObj Scm_VM${T}VectorDotProd(ScmUVector *x, ScmObj y)
1049 {
1050 return ${T}VectorDotProd(x, y, TRUE);
1051 }
1052 ///)) ;; end of tmpl-dotop
1053
1054 ///;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1055 ///;; Range check template
1056 ///(append! *tmpl-prologue* (list
1057
1058 #if SCM_EMULATE_INT64
1059 #define INT64LT(a, b) ((a.hi < b.hi) || (a.hi == b.hi) && (a.lo < b.lo))
1060 #else
1061 #define INT64LT(a, b) (a < b)
1062 #endif
1063
1064 ///))
1065 ///(define *tmpl-rangeop* '(
1066
1067 ScmObj Scm_${T}Vector${Opname}(ScmUVector *x, ScmObj min, ScmObj max)
1068 {
1069 int size = SCM_${T}VECTOR_SIZE(x);
1070 ArgType mintype, maxtype;
1071 ${ntype} val, minval, maxval;
1072 int mindc = FALSE, maxdc = FALSE; /* true if "don't care" */
1073 ScmObj vv;
1074 ${dstdecl};
1075
1076 minval = 0; /* make compiler happy */
1077 maxval = 0; /* dittox */
1078
1079 /* size check */
1080 if (SCM_FALSEP(min)) mintype = ARGTYPE_CONST;
1081 else mintype = arg2_check("${t}vector-${opname}", SCM_OBJ(x), min, TRUE);
1082
1083 if (SCM_FALSEP(max)) maxtype = ARGTYPE_CONST;
1084 else maxtype = arg2_check("${t}vector-${opname}", SCM_OBJ(x), max, TRUE);
1085
1086 if (mintype == ARGTYPE_CONST) {
1087 ${GETLIM minval mindc min};
1088 }
1089 if (maxtype == ARGTYPE_CONST) {
1090 ${GETLIM maxval maxdc max};
1091 }
1092
1093 for (int i=0; i<size; i++) {
1094 val = ${REF_NTYPE x i};
1095 switch (mintype) {
1096 case ARGTYPE_UVECTOR:
1097 minval = ${REF_NTYPE min i}; break;
1098 case ARGTYPE_VECTOR:
1099 vv = SCM_VECTOR_ELEMENTS(min)[i];
1100 ${GETLIM minval mindc vv};
1101 break;
1102 case ARGTYPE_LIST:
1103 vv = SCM_CAR(min); min = SCM_CDR(min);
1104 ${GETLIM minval mindc vv};
1105 break;
1106 case ARGTYPE_CONST:
1107 /* alreadh handled above */
1108 break;
1109 }
1110 switch (maxtype) {
1111 case ARGTYPE_UVECTOR:
1112 maxval = ${REF_NTYPE max i}; break;
1113 case ARGTYPE_VECTOR:
1114 vv = SCM_VECTOR_ELEMENTS(max)[i];
1115 ${GETLIM maxval maxdc vv};
1116 break;
1117 case ARGTYPE_LIST:
1118 vv = SCM_CAR(max); max = SCM_CDR(max);
1119 ${GETLIM maxval maxdc vv};
1120 break;
1121 case ARGTYPE_CONST:
1122 /* alreadh handled above */
1123 break;
1124 }
1125
1126 if (!mindc && ${LT val minval}) {
1127 val = minval;
1128 ${action};
1129 }
1130 if (!maxdc && ${LT maxval val}) {
1131 val = maxval;
1132 ${action};
1133 }
1134 }
1135 return ${okval};
1136 }
1137 ///)) ;; end of tmpl-rangeop
1138
1139 ///;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1140 ///;; Byte swap template
1141 ///;;
1142
1143 ///(append! *tmpl-prologue* (list
1144
1145 static void f64vector_swapb_arm2le(ScmF64Vector *v)
1146 {
1147 int len = SCM_UVECTOR_SIZE(v);
1148 double *d = SCM_F64VECTOR_ELEMENTS(v);
1149 for (int i=0; i<len; i++, d++) {
1150 swap_f64_t v;
1151 v.val = *d;
1152 SWAP_ARM2LE(v);
1153 *d = v.val;
1154 }
1155 }
1156
1157 static void f64vector_swapb_arm2be(ScmF64Vector *v)
1158 {
1159 int len = SCM_UVECTOR_SIZE(v);
1160 double *d = SCM_F64VECTOR_ELEMENTS(v);
1161 for (int i=0; i<len; i++, d++) {
1162 swap_f64_t v;
1163 v.val = *d;
1164 SWAP_ARM2BE(v);
1165 *d = v.val;
1166 }
1167 }
1168
1169 ScmObj Scm_F64VectorSwapBytes_ARM2LE(ScmF64Vector *v)
1170 {
1171 ScmObj d = Scm_F64VectorCopy(v, 0, -1);
1172 f64vector_swapb_arm2le(SCM_F64VECTOR(d));
1173 return d;
1174 }
1175
1176 ScmObj Scm_F64VectorSwapBytesX_ARM2LE(ScmF64Vector *v)
1177 {
1178 SCM_UVECTOR_CHECK_MUTABLE(v);
1179 f64vector_swapb_arm2le(v);
1180 return SCM_OBJ(v);
1181 }
1182
1183 ScmObj Scm_F64VectorSwapBytes_ARM2BE(ScmF64Vector *v)
1184 {
1185 ScmObj d = Scm_F64VectorCopy(v, 0, -1);
1186 f64vector_swapb_arm2be(SCM_F64VECTOR(d));
1187 return d;
1188 }
1189
1190 ScmObj Scm_F64VectorSwapBytesX_ARM2BE(ScmF64Vector *v)
1191 {
1192 SCM_UVECTOR_CHECK_MUTABLE(v);
1193 f64vector_swapb_arm2be(v);
1194 return SCM_OBJ(v);
1195 }
1196
1197 ///))
1198
1199
1200 ///(define *tmpl-swapb* '(
1201
1202 static void ${t}vector_swapb(ScmUVector *v)
1203 {
1204 int len = SCM_UVECTOR_SIZE(v);
1205 ${etype} *d = SCM_${T}VECTOR_ELEMENTS(v);
1206 for (int i=0; i<len; i++, d++) {
1207 swap_${t}_t v;
1208 v.val = *d;
1209 ${SWAPB}(v);
1210 *d = v.val;
1211 }
1212 }
1213
1214 ScmObj Scm_${T}VectorSwapBytes(ScmUVector *v)
1215 {
1216 ScmObj d = Scm_${T}VectorCopy(v, 0, -1);
1217 ${t}vector_swapb(SCM_${T}VECTOR(d));
1218 return d;
1219 }
1220
1221 ScmObj Scm_${T}VectorSwapBytesX(ScmUVector *v)
1222 {
1223 SCM_UVECTOR_CHECK_MUTABLE(v);
1224 ${t}vector_swapb(v);
1225 return SCM_OBJ(v);
1226 }
1227
1228 ///))
1229
1230
1231 ///(define *extra-procedure* ;; procedurally generates code
1232 /// (lambda ()
1233 /// (generate-numop)
1234 /// (generate-bitop)
1235 /// (generate-dotop)
1236 /// (generate-rangeop)
1237 /// (generate-swapb)
1238 ///)) ;; end of extra-procedure
1239
1240 ///(define *tmpl-epilogue* '(
1241
1242 /*==============================================================
1243 * Some generic functions
1244 */
1245
1246 /*
1247 * Generic copy
1248 */
1249 ScmObj Scm_UVectorCopy(ScmUVector *v, ScmSmallInt start, ScmSmallInt end)
1250 {
1251 switch (Scm_UVectorType(Scm_ClassOf(SCM_OBJ(v)))) {
1252 case SCM_UVECTOR_S8: return Scm_S8VectorCopy(v, start, end);
1253 case SCM_UVECTOR_U8: return Scm_U8VectorCopy(v, start, end);
1254 case SCM_UVECTOR_S16: return Scm_S16VectorCopy(v, start, end);
1255 case SCM_UVECTOR_U16: return Scm_U16VectorCopy(v, start, end);
1256 case SCM_UVECTOR_S32: return Scm_S32VectorCopy(v, start, end);
1257 case SCM_UVECTOR_U32: return Scm_U32VectorCopy(v, start, end);
1258 case SCM_UVECTOR_S64: return Scm_S64VectorCopy(v, start, end);
1259 case SCM_UVECTOR_U64: return Scm_U64VectorCopy(v, start, end);
1260 case SCM_UVECTOR_F16: return Scm_F16VectorCopy(v, start, end);
1261 case SCM_UVECTOR_F32: return Scm_F32VectorCopy(v, start, end);
1262 case SCM_UVECTOR_F64: return Scm_F64VectorCopy(v, start, end);
1263 case SCM_UVECTOR_C32: return Scm_C32VectorCopy(v, start, end);
1264 case SCM_UVECTOR_C64: return Scm_C64VectorCopy(v, start, end);
1265 case SCM_UVECTOR_C128: return Scm_C128VectorCopy(v, start, end);
1266 default: Scm_Error("uniform vector required, but got %S", v);
1267 return SCM_UNDEFINED;
1268 }
1269 }
1270
1271 /*
1272 * Generic swapb
1273 */
1274 ScmObj Scm_UVectorSwapBytes(ScmUVector *v, int option)
1275 {
1276 switch (Scm_UVectorType(Scm_ClassOf(SCM_OBJ(v)))) {
1277 case SCM_UVECTOR_S8: return SCM_OBJ(v);
1278 case SCM_UVECTOR_U8: return SCM_OBJ(v);
1279 case SCM_UVECTOR_S16: return Scm_S16VectorSwapBytes(v);
1280 case SCM_UVECTOR_U16: return Scm_U16VectorSwapBytes(v);
1281 case SCM_UVECTOR_S32: return Scm_S32VectorSwapBytes(v);
1282 case SCM_UVECTOR_U32: return Scm_U32VectorSwapBytes(v);
1283 case SCM_UVECTOR_S64: return Scm_S64VectorSwapBytes(v);
1284 case SCM_UVECTOR_U64: return Scm_U64VectorSwapBytes(v);
1285 case SCM_UVECTOR_F16: return Scm_F16VectorSwapBytes(v);
1286 case SCM_UVECTOR_F32: return Scm_F32VectorSwapBytes(v);
1287 case SCM_UVECTOR_F64:
1288 switch (option) {
1289 case SWAPB_ARM_LE: return Scm_F64VectorSwapBytes_ARM2LE(v);
1290 case SWAPB_ARM_BE: return Scm_F64VectorSwapBytes_ARM2BE(v);
1291 default: return Scm_F64VectorSwapBytes(v);
1292 }
1293 default: Scm_Error("uniform vector required, but got %S", v);
1294 return SCM_UNDEFINED;
1295 }
1296 }
1297
1298 ScmObj Scm_UVectorSwapBytesX(ScmUVector *v, int option)
1299 {
1300 switch (Scm_UVectorType(Scm_ClassOf(SCM_OBJ(v)))) {
1301 case SCM_UVECTOR_S8: return SCM_OBJ(v);
1302 case SCM_UVECTOR_U8: return SCM_OBJ(v);
1303 case SCM_UVECTOR_S16: return Scm_S16VectorSwapBytesX(v);
1304 case SCM_UVECTOR_U16: return Scm_U16VectorSwapBytesX(v);
1305 case SCM_UVECTOR_S32: return Scm_S32VectorSwapBytesX(v);
1306 case SCM_UVECTOR_U32: return Scm_U32VectorSwapBytesX(v);
1307 case SCM_UVECTOR_S64: return Scm_S64VectorSwapBytesX(v);
1308 case SCM_UVECTOR_U64: return Scm_U64VectorSwapBytesX(v);
1309 case SCM_UVECTOR_F16: return Scm_F16VectorSwapBytesX(v);
1310 case SCM_UVECTOR_F32: return Scm_F32VectorSwapBytesX(v);
1311 case SCM_UVECTOR_F64:
1312 switch (option) {
1313 case SWAPB_ARM_LE: return Scm_F64VectorSwapBytesX_ARM2LE(v);
1314 case SWAPB_ARM_BE: return Scm_F64VectorSwapBytesX_ARM2BE(v);
1315 default: return Scm_F64VectorSwapBytesX(v);
1316 }
1317 default: Scm_Error("uniform vector required, but got %S", v);
1318 return SCM_UNDEFINED;
1319 }
1320 }
1321
1322 /*
1323 * Block I/O
1324 */
1325
1326 ScmObj Scm_ReadBlockX(ScmUVector *v, ScmPort *port,
1327 ScmSmallInt start, ScmSmallInt end, ScmSymbol *endian)
1328 {
1329 ScmSmallInt len = SCM_UVECTOR_SIZE(v);
1330
1331 SCM_CHECK_START_END(start, end, len);
1332 SCM_UVECTOR_CHECK_MUTABLE(v);
1333 SCM_CHECK_ENDIAN(endian);
1334
1335 int eltsize = Scm_UVectorElementSize(Scm_ClassOf(SCM_OBJ(v)));
1336 SCM_ASSERT(eltsize >= 1);
1337 ScmSize r = Scm_Getz((char*)v->elements + start*eltsize,
1338 (end-start)*eltsize, port);
1339 if (r == EOF) SCM_RETURN(SCM_EOF);
1340 #ifdef DOUBLE_ARMENDIAN
1341 if (Scm_IsArmLE(Scm_NativeEndian())) {
1342 if (Scm_IsLE(SCM_OBJ(endian))) {
1343 /* arm-le and le is equivalent when eltsize <= 4 */
1344 if (eltsize == 8) {
1345 Scm_UVectorSwapBytesX(SCM_UVECTOR(v), SWAPB_ARM_LE);
1346 }
1347 } else if (Scm_IsBE(SCM_OBJ(endian))) {
1348 Scm_UVectorSwapBytesX(SCM_UVECTOR(v), SWAPB_ARM_BE);
1349 }
1350 } else
1351 #endif /*!DOUBLE_ARMENDIAN*/
1352 {
1353 #ifdef WORDS_BIGENDIAN
1354 if (Scm_IsLE(SCM_OBJ(endian))) {
1355 Scm_UVectorSwapBytesX(SCM_UVECTOR(v), SWAPB_STD);
1356 } else if (Scm_IsArmLE(SCM_OBJ(endian))) {
1357 Scm_UVectorSwapBytesX(SCM_UVECTOR(v), SWAPB_ARM_BE);
1358 }
1359 #else /*!WORDS_BIGENDIAN*/
1360 if (Scm_IsBE(SCM_OBJ(endian))) {
1361 Scm_UVectorSwapBytesX(SCM_UVECTOR(v), SWAPB_STD);
1362 } else if (Scm_IsArmLE(SCM_OBJ(endian))) {
1363 if (eltsize == 8) {
1364 Scm_UVectorSwapBytesX(SCM_UVECTOR(v), SWAPB_ARM_LE);
1365 }
1366 }
1367 #endif /*!WORDS_BIGENDIAN*/
1368 }
1369 SCM_RETURN(Scm_MakeInteger(r/eltsize));
1370 }
1371
1372 ScmObj Scm_WriteBlock(ScmUVector *v, ScmPort *port,
1373 ScmSmallInt start, ScmSmallInt end, ScmSymbol *endian)
1374 {
1375 ScmSmallInt len = SCM_UVECTOR_SIZE(v);
1376 int swap_needed = FALSE, swap_type;
1377 SCM_CHECK_START_END(start, end, len);
1378 SCM_CHECK_ENDIAN(endian);
1379
1380 int eltsize = Scm_UVectorElementSize(Scm_ClassOf(SCM_OBJ(v)));
1381 SCM_ASSERT(eltsize >= 1);
1382 #ifdef DOUBLE_ARMENDIAN
1383 if (Scm_IsArmLE(Scm_NativeEndian())) {
1384 if (Scm_IsBE(SCM_OBJ(endian))) {
1385 swap_needed = TRUE;
1386 swap_type = SWAPB_ARM_BE;
1387 } else if (Scm_IsLE(SCM_OBJ(endian)) && eltsize == 8) {
1388 swap_needed = TRUE;
1389 swap_type = SWAPB_ARM_LE;
1390 }
1391 } else
1392 #endif /*DOUBLE_ARMENDIAN*/
1393 {
1394 #ifdef WORDS_BIGENDIAN
1395 if (Scm_IsLE(SCM_OBJ(endian))) {
1396 swap_needed = TRUE;
1397 swap_type = SWAPB_STD;
1398 } else if (Scm_IsArmLE(SCM_OBJ(endian))) {
1399 swap_needed = TRUE;
1400 swap_type = SWAPB_ARM_BE;
1401 }
1402 #else /*!WORDS_BIGENDIAN*/
1403 if (Scm_IsBE(SCM_OBJ(endian))) {
1404 swap_needed = TRUE;
1405 swap_type = SWAPB_STD;
1406 } else if (Scm_IsArmLE(SCM_OBJ(endian)) && eltsize == 8) {
1407 swap_needed = TRUE;
1408 swap_type = SWAPB_ARM_LE;
1409 }
1410 #endif /*!WORDS_BIGENDIAN*/
1411 }
1412 if (!swap_needed || eltsize == 1) {
1413 Scm_Putz((const char*)v->elements + start*eltsize,
1414 (end-start)*eltsize, port);
1415 } else {
1416 /* ugly */
1417 switch (eltsize) {
1418 case 2: {
1419 swap_u16_t d;
1420 for (ScmSmallInt i=start; i<end; i++) {
1421 d.val = ((uint16_t*)v->elements)[i];
1422 SWAP_2(d);
1423 Scm_Putz((const char*)d.buf, 2, port);
1424 }
1425 break;
1426 }
1427 case 4: {
1428 swap_u32_t d;
1429 for (ScmSmallInt i=start; i<end; i++) {
1430 d.val = ((uint32_t*)v->elements)[i];
1431 SWAP_4(d);
1432 Scm_Putz((const char*)d.buf, 4, port);
1433 }
1434 break;
1435 }
1436 case 8:{
1437 swap_u64_t d;
1438 switch (swap_type) {
1439 case SWAPB_STD:
1440 for (ScmSmallInt i=start; i<end; i++) {
1441 d.val = ((uint64_t*)v->elements)[i];
1442 SWAP_8(d);
1443 Scm_Putz((const char*)d.buf, 8, port);
1444 }
1445 break;
1446 case SWAPB_ARM_LE:
1447 for (ScmSmallInt i=start; i<end; i++) {
1448 d.val = ((uint64_t*)v->elements)[i];
1449 SWAP_ARM2LE(d);
1450 Scm_Putz((const char*)d.buf, 8, port);
1451 }
1452 break;
1453 case SWAPB_ARM_BE:
1454 for (ScmSmallInt i=start; i<end; i++) {
1455 d.val = ((uint64_t*)v->elements)[i];
1456 SWAP_ARM2BE(d);
1457 Scm_Putz((const char*)d.buf, 8, port);
1458 }
1459 break;
1460 }
1461 break;
1462 }
1463 }
1464 }
1465 SCM_RETURN(SCM_UNDEFINED);
1466 }
1467
1468 ///)) ;; end of tmpl-epilogue
1469
1470 ///; Local variables:
1471 ///; mode: c
1472 ///; end: