Debian Codegraph @

src/c/array.d

raw
1 /* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */
3
4 /*
5 * array.c - array routines
6 *
7 * Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya
8 * Copyright (c) 1990 Giuseppe Attardi
9 * Copyright (c) 2001 Juan Jose Garcia Ripoll
10 *
11 * See file 'LICENSE' for the copyright details.
12 *
13 */
14
15 #include <limits.h>
16 #include <string.h>
17 #include <ecl/ecl.h>
18 #define ECL_DEFINE_AET_SIZE
19 #include <ecl/internal.h>
20
21 static const cl_object ecl_aet_name[] = {
22 ECL_T, /* ecl_aet_object */
23 @'single-float', /* ecl_aet_sf */
24 @'double-float', /* ecl_aet_df */
25 @'long-float', /* ecl_aet_lf */
26 #ifdef ECL_COMPLEX_FLOAT
27 @'si::complex-single-float', /* ecl_aet_csf */
28 @'si::complex-double-float', /* ecl_aet_cdf */
29 @'si::complex-long-float', /* ecl_aet_clf */
30 #endif
31 @'bit', /* ecl_aet_bit */
32 @'ext::cl-fixnum', /* ecl_aet_fix */
33 @'ext::cl-index', /* ecl_aet_index */
34 @'ext::byte8', /* ecl_aet_b8 */
35 @'ext::integer8', /* ecl_aet_i8 */
36 #ifdef ecl_uint16_t
37 @'ext::byte16',
38 @'ext::integer16',
39 #endif
40 #ifdef ecl_uint32_t
41 @'ext::byte32',
42 @'ext::integer32',
43 #endif
44 #ifdef ecl_uint64_t
45 @'ext::byte64',
46 @'ext::integer64',
47 #endif
48 #ifdef ECL_UNICODE
49 @'character', /* ecl_aet_ch */
50 #endif
51 @'base-char' /* ecl_aet_bc */
52 };
53
54 static void FEbad_aet() ecl_attr_noreturn;
55
56 static void
57 FEbad_aet()
58 {
59 FEerror(
60 "A routine from ECL got an object with a bad array element type.\n"
61 "If you are running a standard copy of ECL, please report this bug.\n"
62 "If you are embedding ECL into an application, please ensure you\n"
63 "passed the right value to the array creation routines.\n",0);
64 }
65
66 static cl_index
67 out_of_bounds_error(cl_index ndx, cl_object x)
68 {
69 cl_object type = cl_list(3, @'integer', ecl_make_fixnum(0),
70 ecl_make_fixnum(x->array.dim));
71 FEwrong_type_argument(ecl_make_integer(ndx), type);
72 }
73
74 void
75 FEwrong_dimensions(cl_object a, cl_index rank)
76 {
77 cl_object list = cl_make_list(3, ecl_make_fixnum(rank),
78 @':initial-element', @'*');
79 cl_object type = cl_list(3, @'array', @'*', list);
80 FEwrong_type_argument(type, a);
81 }
82
83 static ECL_INLINE cl_index
84 checked_index(cl_object function, cl_object a, int which, cl_object index,
85 cl_index nonincl_limit)
86 {
87 cl_index output;
88 unlikely_if (!ECL_FIXNUMP(index) || ecl_fixnum_minusp(index))
89 FEwrong_index(function, a, which, index, nonincl_limit);
90 output = ecl_fixnum(index);
91 unlikely_if (output >= nonincl_limit)
92 FEwrong_index(function, a, which, index, nonincl_limit);
93 return output;
94 }
95
96 cl_index
97 ecl_to_index(cl_object n)
98 {
99 switch (ecl_t_of(n)) {
100 case t_fixnum: {
101 cl_fixnum out = ecl_fixnum(n);
102 if (out < 0 || out >= ECL_ARRAY_DIMENSION_LIMIT)
103 FEtype_error_index(ECL_NIL, out);
104 return out;
105 }
106 default:
107 FEwrong_type_only_arg(@[coerce], n, @[fixnum]);
108 }
109 }
110
111 cl_object
112 cl_row_major_aref(cl_object x, cl_object indx)
113 {
114 cl_index j = ecl_to_size(indx);
115 @(return ecl_aref(x, j));
116 }
117
118 cl_object
119 si_row_major_aset(cl_object x, cl_object indx, cl_object val)
120 {
121 cl_index j = ecl_to_size(indx);
122 @(return ecl_aset(x, j, val));
123 }
124
125 @(defun aref (x &rest indx)
126 @ {
127 cl_index i, j;
128 cl_index r = narg - 1;
129 switch (ecl_t_of(x)) {
130 case t_array:
131 if (r != x->array.rank)
132 FEerror("Wrong number of indices.", 0);
133 for (i = j = 0; i < r; i++) {
134 cl_index s = checked_index(@[aref], x, i,
135 ecl_va_arg(indx),
136 x->array.dims[i]);
137 j = j*(x->array.dims[i]) + s;
138 }
139 break;
140 case t_vector:
141 #ifdef ECL_UNICODE
142 case t_string:
143 #endif
144 case t_base_string:
145 case t_bitvector:
146 if (r != 1)
147 FEerror("Wrong number of indices.", 0);
148 j = checked_index(@[aref], x, -1, ecl_va_arg(indx), x->vector.dim);
149 break;
150 default:
151 FEwrong_type_nth_arg(@[aref], 1, x, @[array]);
152 }
153 @(return ecl_aref_unsafe(x, j));
154 } @)
155
156 cl_object
157 ecl_aref_unsafe(cl_object x, cl_index index)
158 {
159 switch (x->array.elttype) {
160 case ecl_aet_object:
161 return x->array.self.t[index];
162 case ecl_aet_bc:
163 return ECL_CODE_CHAR(x->base_string.self[index]);
164 #ifdef ECL_UNICODE
165 case ecl_aet_ch:
166 return ECL_CODE_CHAR(x->string.self[index]);
167 #endif
168 case ecl_aet_bit:
169 index += x->vector.offset;
170 if (x->vector.self.bit[index/CHAR_BIT] & (0200>>index%CHAR_BIT))
171 return(ecl_make_fixnum(1));
172 else
173 return(ecl_make_fixnum(0));
174 case ecl_aet_fix:
175 return ecl_make_integer(x->array.self.fix[index]);
176 case ecl_aet_index:
177 return ecl_make_unsigned_integer(x->array.self.index[index]);
178 case ecl_aet_sf:
179 return(ecl_make_single_float(x->array.self.sf[index]));
180 case ecl_aet_df:
181 return(ecl_make_double_float(x->array.self.df[index]));
182 case ecl_aet_lf:
183 return(ecl_make_long_float(x->array.self.lf[index]));
184 #ifdef ECL_COMPLEX_FLOAT
185 case ecl_aet_csf:
186 return(ecl_make_csfloat(x->array.self.csf[index]));
187 case ecl_aet_cdf:
188 return(ecl_make_cdfloat(x->array.self.cdf[index]));
189 case ecl_aet_clf:
190 return(ecl_make_clfloat(x->array.self.clf[index]));
191 #endif
192 case ecl_aet_b8:
193 return ecl_make_uint8_t(x->array.self.b8[index]);
194 case ecl_aet_i8:
195 return ecl_make_int8_t(x->array.self.i8[index]);
196 #ifdef ecl_uint16_t
197 case ecl_aet_b16:
198 return ecl_make_uint16_t(x->array.self.b16[index]);
199 case ecl_aet_i16:
200 return ecl_make_int16_t(x->array.self.i16[index]);
201 #endif
202 #ifdef ecl_uint32_t
203 case ecl_aet_b32:
204 return ecl_make_uint32_t(x->array.self.b32[index]);
205 case ecl_aet_i32:
206 return ecl_make_int32_t(x->array.self.i32[index]);
207 #endif
208 #ifdef ecl_uint64_t
209 case ecl_aet_b64:
210 return ecl_make_uint64_t(x->array.self.b64[index]);
211 case ecl_aet_i64:
212 return ecl_make_int64_t(x->array.self.i64[index]);
213 #endif
214 default:
215 FEbad_aet();
216 }
217 }
218
219 cl_object
220 ecl_aref(cl_object x, cl_index index)
221 {
222 if (ecl_unlikely(!ECL_ARRAYP(x))) {
223 FEwrong_type_nth_arg(@[aref], 1, x, @[array]);
224 }
225 if (ecl_unlikely(index >= x->array.dim)) {
226 FEwrong_index(@[row-major-aref], x, -1, ecl_make_fixnum(index),
227 x->array.dim);
228 }
229 return ecl_aref_unsafe(x, index);
230 }
231
232 cl_object
233 ecl_aref1(cl_object x, cl_index index)
234 {
235 if (ecl_unlikely(!ECL_VECTORP(x))) {
236 FEwrong_type_nth_arg(@[aref], 1, x, @[array]);
237 }
238 if (ecl_unlikely(index >= x->array.dim)) {
239 FEwrong_index(@[aref], x, -1, ecl_make_fixnum(index),
240 x->array.dim);
241 }
242 return ecl_aref_unsafe(x, index);
243 }
244
245 void *
246 ecl_row_major_ptr(cl_object x, cl_index index, cl_index bytes)
247 {
248 cl_index elt_size, offset;
249 cl_elttype elt_type;
250
251 if (ecl_unlikely(!ECL_ARRAYP(x))) {
252 FEwrong_type_nth_arg(@[aref], 1, x, @[array]);
253 }
254
255 elt_type = x->array.elttype;
256 if (ecl_unlikely(elt_type == ecl_aet_bit || elt_type == ecl_aet_object))
257 FEerror("In ecl_row_major_ptr: Specialized array expected, element type ~S found.",
258 1,ecl_elttype_to_symbol(elt_type));
259
260 elt_size = ecl_aet_size[elt_type];
261 offset = index*elt_size;
262
263 /* don't check bounds if bytes == 0 */
264 if (ecl_unlikely(bytes > 0 && offset + bytes > x->array.dim*elt_size)) {
265 FEwrong_index(@[row-major-aref], x, -1, ecl_make_fixnum(index),
266 x->array.dim);
267 }
268
269 return x->array.self.b8 + offset;
270 }
271
272 /*
273 Internal function for setting array elements:
274
275 (si:aset value array dim0 ... dimN)
276 */
277 @(defun si::aset (x &rest dims)
278 @ {
279 cl_index i, j;
280 cl_index r = narg - 2;
281 cl_object v;
282 switch (ecl_t_of(x)) {
283 case t_array:
284 if (ecl_unlikely(r != x->array.rank))
285 FEerror("Wrong number of indices.", 0);
286 for (i = j = 0; i < r; i++) {
287 cl_index s = checked_index(@[si::aset], x, i,
288 ecl_va_arg(dims),
289 x->array.dims[i]);
290 j = j*(x->array.dims[i]) + s;
291 }
292 break;
293 case t_vector:
294 #ifdef ECL_UNICODE
295 case t_string:
296 #endif
297 case t_base_string:
298 case t_bitvector:
299 if (ecl_unlikely(r != 1))
300 FEerror("Wrong number of indices.", 0);
301 j = checked_index(@[si::aset], x, -1, ecl_va_arg(dims),
302 x->vector.dim);
303 break;
304 default:
305 FEwrong_type_nth_arg(@[si::aset], 1, x, @[array]);
306 }
307 v = ecl_va_arg(dims);
308 @(return ecl_aset_unsafe(x, j, v));
309 } @)
310
311 cl_object
312 ecl_aset_unsafe(cl_object x, cl_index index, cl_object value)
313 {
314 switch (x->array.elttype) {
315 case ecl_aet_object:
316 x->array.self.t[index] = value;
317 break;
318 case ecl_aet_bc:
319 /* INV: ecl_char_code() checks the type of `value' */
320 x->base_string.self[index] = ecl_char_code(value);
321 break;
322 #ifdef ECL_UNICODE
323 case ecl_aet_ch:
324 x->string.self[index] = ecl_char_code(value);
325 break;
326 #endif
327 case ecl_aet_bit: {
328 cl_fixnum i = ecl_to_bit(value);
329 index += x->vector.offset;
330 if (i == 0)
331 x->vector.self.bit[index/CHAR_BIT] &= ~(0200>>index%CHAR_BIT);
332 else
333 x->vector.self.bit[index/CHAR_BIT] |= 0200>>index%CHAR_BIT;
334 break;
335 }
336 case ecl_aet_fix:
337 x->array.self.fix[index] = ecl_to_fix(value);
338 break;
339 case ecl_aet_index:
340 x->array.self.index[index] = ecl_to_size(value);
341 break;
342 case ecl_aet_sf:
343 x->array.self.sf[index] = ecl_to_float(value);
344 break;
345 case ecl_aet_df:
346 x->array.self.df[index] = ecl_to_double(value);
347 break;
348 case ecl_aet_lf:
349 x->array.self.lf[index] = ecl_to_long_double(value);
350 break;
351 #ifdef ECL_COMPLEX_FLOAT
352 case ecl_aet_csf:
353 x->array.self.csf[index] = ecl_to_csfloat(value);
354 break;
355 case ecl_aet_cdf:
356 x->array.self.cdf[index] = ecl_to_cdfloat(value);
357 break;
358 case ecl_aet_clf:
359 x->array.self.clf[index] = ecl_to_clfloat(value);
360 break;
361 #endif
362 case ecl_aet_b8:
363 x->array.self.b8[index] = ecl_to_uint8_t(value);
364 break;
365 case ecl_aet_i8:
366 x->array.self.i8[index] = ecl_to_int8_t(value);
367 break;
368 #ifdef ecl_uint16_t
369 case ecl_aet_b16:
370 x->array.self.b16[index] = ecl_to_uint16_t(value);
371 break;
372 case ecl_aet_i16:
373 x->array.self.i16[index] = ecl_to_int16_t(value);
374 break;
375 #endif
376 #ifdef ecl_uint32_t
377 case ecl_aet_b32:
378 x->array.self.b32[index] = ecl_to_uint32_t(value);
379 break;
380 case ecl_aet_i32:
381 x->array.self.i32[index] = ecl_to_int32_t(value);
382 break;
383 #endif
384 #ifdef ecl_uint64_t
385 case ecl_aet_b64:
386 x->array.self.b64[index] = ecl_to_uint64_t(value);
387 break;
388 case ecl_aet_i64:
389 x->array.self.i64[index] = ecl_to_int64_t(value);
390 break;
391 #endif
392 }
393 return(value);
394 }
395
396 cl_object
397 ecl_aset(cl_object x, cl_index index, cl_object value)
398 {
399 if (ecl_unlikely(!ECL_ARRAYP(x))) {
400 FEwrong_type_nth_arg(@[si::aset], 1, x, @[array]);
401 }
402 if (ecl_unlikely(index >= x->array.dim)) {
403 out_of_bounds_error(index, x);
404 }
405 return ecl_aset_unsafe(x, index, value);
406 }
407
408 cl_object
409 ecl_aset1(cl_object x, cl_index index, cl_object value)
410 {
411 if (ecl_unlikely(!ECL_VECTORP(x))) {
412 FEwrong_type_nth_arg(@[si::aset], 1, x, @[array]);
413 }
414 if (ecl_unlikely(index >= x->array.dim)) {
415 out_of_bounds_error(index, x);
416 }
417 return ecl_aset_unsafe(x, index, value);
418 }
419
420 /*
421 Internal function for making arrays of more than one dimension:
422
423 (si:make-pure-array element-type dimension-list adjustable
424 displaced-to displaced-index-offset)
425 */
426 cl_object
427 si_make_pure_array(cl_object etype, cl_object dims, cl_object adj,
428 cl_object fillp, cl_object displ, cl_object disploff)
429 {
430 cl_index r, s, i, j;
431 cl_object x;
432 if (ECL_FIXNUMP(dims)) {
433 return si_make_vector(etype, dims, adj, fillp, displ, disploff);
434 } else if (ecl_unlikely(!ECL_LISTP(dims))) {
435 FEwrong_type_nth_arg(@[make-array], 1, dims,
436 cl_list(3, @'or', @'list', @'fixnum'));
437 }
438 r = ecl_length(dims);
439 if (ecl_unlikely(r >= ECL_ARRAY_RANK_LIMIT)) {
440 FEerror("The array rank, ~R, is too large.", 1, ecl_make_fixnum(r));
441 } else if (r == 1) {
442 return si_make_vector(etype, ECL_CONS_CAR(dims), adj, fillp,
443 displ, disploff);
444 } else if (ecl_unlikely(!Null(fillp))) {
445 FEerror(":FILL-POINTER may not be specified for an array of rank ~D",
446 1, ecl_make_fixnum(r));
447 }
448 x = ecl_alloc_object(t_array);
449 x->array.displaced = ECL_NIL;
450 x->array.self.t = NULL; /* for GC sake */
451 x->array.rank = r;
452 x->array.elttype = (short)ecl_symbol_to_elttype(etype);
453 x->array.flags = 0; /* no fill pointer, no adjustable */
454 x->array.dims = (cl_index *)ecl_alloc_atomic_align(sizeof(cl_index)*r, sizeof(cl_index));
455 for (i = 0, s = 1; i < r; i++, dims = ECL_CONS_CDR(dims)) {
456 cl_object d = ECL_CONS_CAR(dims);
457 if (ecl_unlikely(!ECL_FIXNUMP(d) ||
458 ecl_fixnum_minusp(d) ||
459 ecl_fixnum_greater(d, ecl_make_fixnum(ECL_ARRAY_DIMENSION_LIMIT))))
460 {
461 cl_object type = ecl_make_integer_type(ecl_make_fixnum(0),
462 ecl_make_fixnum(ECL_ARRAY_DIMENSION_LIMIT));
463 FEwrong_type_nth_arg(@[make-array], 1, d, type);
464 }
465 j = ecl_fixnum(d);
466 s *= (x->array.dims[i] = j);
467 if (ecl_unlikely(s > ECL_ARRAY_TOTAL_LIMIT)) {
468 cl_object type = ecl_make_integer_type(ecl_make_fixnum(0),
469 ecl_make_fixnum(ECL_ARRAY_TOTAL_LIMIT));
470 FEwrong_type_key_arg(@[make-array], @[array-total-size],
471 ecl_make_fixnum(s), type);
472 }
473 }
474 x->array.dim = s;
475 if (adj != ECL_NIL) {
476 x->array.flags |= ECL_FLAG_ADJUSTABLE;
477 }
478 if (Null(displ))
479 ecl_array_allocself(x);
480 else
481 ecl_displace(x, displ, disploff);
482 @(return x);
483 }
484
485 /* Internal internal function for making simple actually adjustable vectors. */
486 cl_object
487 ecl_make_vector(cl_index dim)
488 {
489 cl_object x = ecl_alloc_object(t_vector);
490 x->vector.elttype = ecl_aet_object;
491 x->vector.self.t = NULL;
492 x->vector.displaced = ECL_NIL;
493 x->vector.dim = dim;
494 x->vector.fillp = 0;
495 x->vector.flags = ECL_FLAG_ADJUSTABLE | ECL_FLAG_HAS_FILL_POINTER;
496 ecl_array_allocself(x);
497 return x;
498 }
499
500 /*
501 Internal function for making vectors:
502
503 (si:make-vector element-type dimension adjustable fill-pointer
504 displaced-to displaced-index-offset)
505 */
506 cl_object
507 si_make_vector(cl_object etype, cl_object dim, cl_object adj,
508 cl_object fillp, cl_object displ, cl_object disploff)
509 {
510 cl_index d, f;
511 cl_object x;
512 cl_elttype aet;
513 AGAIN:
514 aet = ecl_symbol_to_elttype(etype);
515 if (ecl_unlikely(!ECL_FIXNUMP(dim) || ecl_fixnum_minusp(dim) ||
516 ecl_fixnum_greater(dim, ECL_ARRAY_DIMENSION_LIMIT))) {
517 cl_object type = ecl_make_integer_type(ecl_make_fixnum(0),
518 ecl_make_fixnum(ECL_ARRAY_DIMENSION_LIMIT));
519 FEwrong_type_nth_arg(@[make-array], 1, dim, type);
520 }
521 d = ecl_fixnum(dim);
522 if (aet == ecl_aet_bc) {
523 x = ecl_alloc_object(t_base_string);
524 x->base_string.elttype = (short)aet;
525 } else if (aet == ecl_aet_bit) {
526 x = ecl_alloc_object(t_bitvector);
527 x->vector.elttype = (short)aet;
528 #ifdef ECL_UNICODE
529 } else if (aet == ecl_aet_ch) {
530 x = ecl_alloc_object(t_string);
531 x->string.elttype = (short)aet;
532 #endif
533 } else {
534 x = ecl_alloc_object(t_vector);
535 x->vector.elttype = (short)aet;
536 }
537 x->vector.self.t = NULL; /* for GC sake */
538 x->vector.displaced = ECL_NIL;
539 x->vector.dim = d;
540 x->vector.flags = 0;
541 if (adj != ECL_NIL) {
542 x->vector.flags |= ECL_FLAG_ADJUSTABLE;
543 }
544 if (Null(fillp)) {
545 f = d;
546 } else if (fillp == ECL_T) {
547 x->vector.flags |= ECL_FLAG_HAS_FILL_POINTER;
548 f = d;
549 } else if (ECL_FIXNUMP(fillp) && ecl_fixnum_geq(fillp,ecl_make_fixnum(0)) &&
550 ((f = ecl_fixnum(fillp)) <= d)) {
551 x->vector.flags |= ECL_FLAG_HAS_FILL_POINTER;
552 } else {
553 fillp = ecl_type_error(@'make-array',"fill pointer",fillp,
554 cl_list(3,@'or',cl_list(3,@'member',ECL_NIL,ECL_T),
555 cl_list(3,@'integer',ecl_make_fixnum(0),
556 dim)));
557 goto AGAIN;
558 }
559 x->vector.fillp = f;
560
561 if (Null(displ))
562 ecl_array_allocself(x);
563 else
564 ecl_displace(x, displ, disploff);
565 @(return x);
566 }
567
568 cl_object
569 si_adjust_vector(cl_object vector, cl_object new_dim) {
570 cl_object new_vector;
571 if (!ECL_ADJUSTABLE_ARRAY_P(vector)) {
572 FEerror("The vector is not adjustable.", 0);
573 }
574 new_vector = si_make_vector(ecl_elttype_to_symbol(ecl_array_elttype(vector)),
575 new_dim,
576 ECL_T,
577 ecl_make_fixnum(vector->vector.fillp),
578 ECL_NIL,
579 ECL_NIL);
580 ecl_copy_subarray(new_vector, 0, vector, 0, vector->vector.dim);
581 si_replace_array(vector, new_vector);
582 return vector;
583 }
584
585 cl_object *
586 alloc_pointerfull_memory(cl_index l)
587 {
588 cl_object *p = ecl_alloc_align(sizeof(cl_object) * l, sizeof(cl_object));
589 cl_index i;
590 for (i = 0; l--;)
591 p[i++] = ECL_NIL;
592 return p;
593 }
594
595 void
596 ecl_array_allocself(cl_object x)
597 {
598 cl_elttype t = x->array.elttype;
599 cl_index d = x->array.dim;
600 switch (t) {
601 /* assign self field only after it has been filled, for GC sake */
602 case ecl_aet_object:
603 x->array.self.t = alloc_pointerfull_memory(d);
604 return;
605 #ifdef ECL_UNICODE
606 case ecl_aet_ch: {
607 ecl_character *elts;
608 d *= sizeof(ecl_character);
609 elts = (ecl_character *)ecl_alloc_atomic_align(d, sizeof(ecl_character));
610 x->string.self = elts;
611 return;
612 }
613 #endif
614 case ecl_aet_bc: {
615 x->vector.self.bc = (ecl_base_char *)ecl_alloc_atomic(d+1);
616 /* Null terminate the string */
617 x->vector.self.bc[d] = 0;
618 break;
619 }
620 case ecl_aet_bit:
621 d = (d + (CHAR_BIT-1)) / CHAR_BIT;
622 x->vector.self.bit = (byte *)ecl_alloc_atomic(d);
623 x->vector.offset = 0;
624 break;
625 default: {
626 cl_index elt_size = ecl_aet_size[t];
627 d *= elt_size;
628 x->vector.self.bc = (ecl_base_char *)ecl_alloc_atomic_align(d, elt_size);
629 }
630 }
631 }
632
633 cl_object
634 ecl_alloc_simple_vector(cl_index l, cl_elttype aet)
635 {
636 cl_object x;
637
638 switch (aet) {
639 case ecl_aet_bc:
640 x = ecl_alloc_compact_object(t_base_string, l+1);
641 x->base_string.self = ECL_COMPACT_OBJECT_EXTRA(x);
642 x->base_string.self[l] = 0;
643 break;
644 #ifdef ECL_UNICODE
645 case ecl_aet_ch:
646 {
647 cl_index bytes = sizeof(ecl_character) * l;
648 x = ecl_alloc_compact_object(t_string, bytes);
649 x->string.self = ECL_COMPACT_OBJECT_EXTRA(x);
650 }
651 break;
652 #endif
653 case ecl_aet_bit:
654 {
655 cl_index bytes = (l + (CHAR_BIT-1))/CHAR_BIT;
656 x = ecl_alloc_compact_object(t_bitvector, bytes);
657 x->vector.self.bit = ECL_COMPACT_OBJECT_EXTRA(x);
658 x->vector.offset = 0;
659 }
660 break;
661 case ecl_aet_object:
662 {
663 x = ecl_alloc_object(t_vector);
664 x->vector.self.t = alloc_pointerfull_memory(l);
665 }
666 break;
667 default:
668 x = ecl_alloc_compact_object(t_vector, l * ecl_aet_size[aet]);
669 x->vector.self.bc = ECL_COMPACT_OBJECT_EXTRA(x);
670 }
671 x->base_string.elttype = aet;
672 x->base_string.flags = 0; /* no fill pointer, not adjustable */
673 x->base_string.displaced = ECL_NIL;
674 x->base_string.dim = x->base_string.fillp = l;
675 return x;
676 }
677
678 cl_elttype
679 ecl_symbol_to_elttype(cl_object x)
680 {
681 BEGIN:
682 if (x == @'base-char')
683 return(ecl_aet_bc);
684 #ifdef ECL_UNICODE
685 if (x == @'character')
686 return(ecl_aet_ch);
687 #endif
688 else if (x == @'bit')
689 return(ecl_aet_bit);
690 else if (x == @'ext::cl-fixnum')
691 return(ecl_aet_fix);
692 else if (x == @'ext::cl-index')
693 return(ecl_aet_index);
694 else if (x == @'single-float' || x == @'short-float')
695 return(ecl_aet_sf);
696 else if (x == @'double-float')
697 return(ecl_aet_df);
698 else if (x == @'long-float')
699 return(ecl_aet_lf);
700 #ifdef ECL_COMPLEX_FLOAT
701 else if (x == @'si::complex-single-float')
702 return(ecl_aet_csf);
703 else if (x == @'si::complex-double-float')
704 return(ecl_aet_cdf);
705 else if (x == @'si::complex-long-float')
706 return(ecl_aet_clf);
707 #endif
708 else if (x == @'ext::byte8')
709 return(ecl_aet_b8);
710 else if (x == @'ext::integer8')
711 return(ecl_aet_i8);
712 #ifdef ecl_uint16_t
713 else if (x == @'ext::byte16')
714 return(ecl_aet_b16);
715 else if (x == @'ext::integer16')
716 return(ecl_aet_i16);
717 #endif
718 #ifdef ecl_uint32_t
719 else if (x == @'ext::byte32')
720 return(ecl_aet_b32);
721 else if (x == @'ext::integer32')
722 return(ecl_aet_i32);
723 #endif
724 #ifdef ecl_uint64_t
725 else if (x == @'ext::byte64')
726 return(ecl_aet_b64);
727 else if (x == @'ext::integer64')
728 return(ecl_aet_i64);
729 #endif
730 else if (x == ECL_T)
731 return(ecl_aet_object);
732 else if (x == ECL_NIL) {
733 FEerror("ECL does not support arrays with element type NIL", 0);
734 }
735 x = cl_upgraded_array_element_type(1, x);
736 goto BEGIN;
737 }
738
739 cl_object
740 ecl_elttype_to_symbol(cl_elttype aet)
741 {
742 return ecl_aet_name[aet];
743 }
744
745 cl_object
746 si_array_element_type_byte_size(cl_object type) {
747 cl_elttype aet = ECL_ARRAYP(type) ?
748 type->array.elttype :
749 ecl_symbol_to_elttype(type);
750 cl_object size = ecl_make_fixnum(ecl_aet_size[aet]);
751 if (aet == ecl_aet_bit)
752 size = ecl_make_ratio(ecl_make_fixnum(1),ecl_make_fixnum(CHAR_BIT));
753
754 @(return size ecl_elttype_to_symbol(aet));
755 }
756
757 static void *
758 address_inc(void *address, cl_fixnum inc, cl_elttype elt_type)
759 {
760 union ecl_array_data aux;
761 aux.t = address;
762 switch (elt_type) {
763 case ecl_aet_object:
764 return aux.t + inc;
765 case ecl_aet_fix:
766 return aux.fix + inc;
767 case ecl_aet_index:
768 return aux.fix + inc;
769 case ecl_aet_sf:
770 return aux.sf + inc;
771 case ecl_aet_bc:
772 return aux.bc + inc;
773 #ifdef ECL_UNICODE
774 case ecl_aet_ch:
775 return aux.c + inc;
776 #endif
777 case ecl_aet_df:
778 return aux.df + inc;
779 case ecl_aet_lf:
780 return aux.lf + inc;
781 #ifdef ECL_COMPLEX_FLOAT
782 case ecl_aet_csf:
783 return aux.csf + inc;
784 case ecl_aet_cdf:
785 return aux.cdf + inc;
786 case ecl_aet_clf:
787 return aux.clf + inc;
788 #endif
789 case ecl_aet_b8:
790 case ecl_aet_i8:
791 return aux.b8 + inc;
792 #ifdef ecl_uint16_t
793 case ecl_aet_b16:
794 case ecl_aet_i16:
795 return aux.b16 + inc;
796 #endif
797 #ifdef ecl_uint32_t
798 case ecl_aet_b32:
799 case ecl_aet_i32:
800 return aux.b32 + inc;
801 #endif
802 #ifdef ecl_uint64_t
803 case ecl_aet_b64:
804 case ecl_aet_i64:
805 return aux.b64 + inc;
806 #endif
807 default:
808 FEbad_aet();
809 }
810 }
811
812 cl_object
813 cl_array_element_type(cl_object a)
814 {
815 @(return ecl_elttype_to_symbol(ecl_array_elttype(a)));
816 }
817
818 /*
819 Displace(from, to, offset) displaces the from-array
820 to the to-array (the original array) by the specified offset.
821 It changes the displaced field of both arrays.
822 The field is a cons; the car of the from-array points to
823 the to-array and the cdr of the to-array is a list of arrays
824 displaced to the to-array, so the from-array is pushed to the
825 cdr of the to-array's array.displaced.
826 */
827 void
828 ecl_displace(cl_object from, cl_object to, cl_object offset)
829 {
830 cl_fixnum j;
831 void *base;
832 cl_elttype totype, fromtype;
833 fromtype = from->array.elttype;
834 if (ecl_unlikely(!ECL_FIXNUMP(offset) || ((j = ecl_fixnum(offset)) < 0))) {
835 FEwrong_type_key_arg(@[adjust-array], @[:displaced-index-offset],
836 offset, @[fixnum]);
837 }
838 if (ecl_t_of(to) == t_foreign) {
839 if (fromtype == ecl_aet_bit || fromtype == ecl_aet_object) {
840 FEerror("Cannot displace arrays with element type T or BIT onto foreign data",0);
841 }
842 base = to->foreign.data;
843 from->array.displaced = to;
844 } else {
845 cl_fixnum maxdim;
846 totype = to->array.elttype;
847 if (totype != fromtype)
848 FEerror("Cannot displace the array, "
849 "because the element types don't match.", 0);
850 maxdim = to->array.dim - from->array.dim;
851 if (maxdim < 0)
852 FEerror("Cannot displace the array, "
853 "because the total size of the to-array"
854 "is too small.", 0);
855 if (j > maxdim) {
856 cl_object type = ecl_make_integer_type(ecl_make_fixnum(0),
857 ecl_make_fixnum(maxdim));
858 FEwrong_type_key_arg(@[adjust-array], @[:displaced-index-offset],
859 offset, type);
860 }
861 from->array.displaced = ecl_list1(to);
862 /* We only need to keep track of the arrays that displace to us
863 * when this one array is adjustable */
864 if (ECL_ADJUSTABLE_ARRAY_P(to)) {
865 cl_object track_list = to->array.displaced;
866 if (Null(track_list))
867 to->array.displaced =
868 track_list = ecl_list1(ECL_NIL);
869 ECL_RPLACD(track_list,
870 CONS(from, ECL_CONS_CDR(track_list)));
871 }
872 if (fromtype == ecl_aet_bit) {
873 j += to->vector.offset;
874 from->vector.offset = j%CHAR_BIT;
875 from->vector.self.bit = to->vector.self.bit + j/CHAR_BIT;
876 return;
877 }
878 base = to->array.self.t;
879 }
880 from->array.self.t = address_inc(base, j, fromtype);
881 }
882
883 cl_object
884 si_array_raw_data(cl_object x)
885 {
886 cl_elttype et = ecl_array_elttype(x);
887 cl_index total_size = x->vector.dim * ecl_aet_size[et];
888 cl_object output, to_array;
889 uint8_t *data;
890 if (et == ecl_aet_object) {
891 FEerror("EXT:ARRAY-RAW-DATA can not get data "
892 "from an array with element type T.", 0);
893 }
894 data = x->vector.self.b8;
895 to_array = x->array.displaced;
896 if (to_array == ECL_NIL || ((to_array = ECL_CONS_CAR(to_array)) == ECL_NIL)) {
897 cl_index used_size = total_size;
898 int flags = 0;
899 if (ECL_ARRAY_HAS_FILL_POINTER_P(x)) {
900 used_size = x->vector.fillp * ecl_aet_size[et];
901 flags = ECL_FLAG_HAS_FILL_POINTER;
902 }
903 output = ecl_alloc_object(t_vector);
904 output->vector.elttype = ecl_aet_b8;
905 output->vector.self.b8 = data;
906 output->vector.dim = total_size;
907 output->vector.fillp = used_size;
908 output->vector.flags = flags;
909 output->vector.displaced = ECL_NIL;
910 } else {
911 cl_index displ = data - to_array->vector.self.b8;
912 cl_object fillp = ECL_NIL;
913 if (ECL_ARRAY_HAS_FILL_POINTER_P(x)) {
914 fillp = ecl_make_fixnum(x->vector.fillp * ecl_aet_size[et]);
915 }
916 output = si_make_vector(@'ext::byte8',
917 ecl_make_fixnum(total_size),
918 ECL_NIL,
919 fillp,
920 si_array_raw_data(to_array),
921 ecl_make_fixnum(displ));
922 }
923 @(return output);
924 }
925
926 cl_elttype
927 ecl_array_elttype(cl_object x)
928 {
929 if (ecl_unlikely(!ECL_ARRAYP(x)))
930 FEwrong_type_argument(@[array], x);
931 return x->array.elttype;
932 }
933
934 cl_object
935 cl_array_rank(cl_object a)
936 {
937 @(return ecl_make_fixnum(ecl_array_rank(a)));
938 }
939
940 cl_index
941 ecl_array_rank(cl_object a)
942 {
943 switch (ecl_t_of(a)) {
944 case t_array:
945 return a->array.rank;
946 #ifdef ECL_UNICODE
947 case t_string:
948 #endif
949 case t_base_string:
950 case t_vector:
951 case t_bitvector:
952 return 1;
953 default:
954 FEwrong_type_only_arg(@[array-rank], a, @[array]);
955 }
956 }
957
958 cl_object
959 cl_array_dimension(cl_object a, cl_object index)
960 {
961 @(return ecl_make_fixnum(ecl_array_dimension(a, ecl_to_size(index))));
962 }
963
964 cl_index
965 ecl_array_dimension(cl_object a, cl_index index)
966 {
967 switch (ecl_t_of(a)) {
968 case t_array: {
969 if (ecl_unlikely(index > a->array.rank))
970 FEwrong_dimensions(a, index+1);
971 return a->array.dims[index];
972 }
973 #ifdef ECL_UNICODE
974 case t_string:
975 #endif
976 case t_base_string:
977 case t_vector:
978 case t_bitvector:
979 if (ecl_unlikely(index))
980 FEwrong_dimensions(a, index+1);
981 return a->vector.dim;
982 default:
983 FEwrong_type_only_arg(@[array-dimension], a, @[array]);
984 }
985 }
986
987 cl_object
988 cl_array_total_size(cl_object a)
989 {
990 if (ecl_unlikely(!ECL_ARRAYP(a)))
991 FEwrong_type_only_arg(@[array-total-size], a, @[array]);
992 @(return ecl_make_fixnum(a->array.dim));
993 }
994
995 cl_object
996 cl_adjustable_array_p(cl_object a)
997 {
998 if (ecl_unlikely(!ECL_ARRAYP(a)))
999 FEwrong_type_only_arg(@[adjustable-array-p], a, @[array]);
1000 @(return (ECL_ADJUSTABLE_ARRAY_P(a) ? ECL_T : ECL_NIL));
1001 }
1002
1003 /*
1004 Internal function for checking if an array is displaced.
1005 */
1006 cl_object
1007 cl_array_displacement(cl_object a)
1008 {
1009 const cl_env_ptr the_env = ecl_process_env();
1010 cl_object to_array;
1011 cl_index offset;
1012
1013 if (ecl_unlikely(!ECL_ARRAYP(a)))
1014 FEwrong_type_only_arg(@[array-displacement], a, @[array]);
1015 to_array = a->array.displaced;
1016 if (Null(to_array)) {
1017 offset = 0;
1018 } else if (Null(to_array = CAR(a->array.displaced))) {
1019 offset = 0;
1020 } else {
1021 switch (a->array.elttype) {
1022 case ecl_aet_object:
1023 offset = a->array.self.t - to_array->array.self.t;
1024 break;
1025 case ecl_aet_bc:
1026 offset = a->array.self.bc - to_array->array.self.bc;
1027 break;
1028 #ifdef ECL_UNICODE
1029 case ecl_aet_ch:
1030 offset = a->array.self.c - to_array->array.self.c;
1031 break;
1032 #endif
1033 case ecl_aet_bit:
1034 offset = a->array.self.bit - to_array->array.self.bit;
1035 offset = offset * CHAR_BIT + a->array.offset
1036 - to_array->array.offset;
1037 break;
1038 case ecl_aet_fix:
1039 offset = a->array.self.fix - to_array->array.self.fix;
1040 break;
1041 case ecl_aet_index:
1042 offset = a->array.self.fix - to_array->array.self.fix;
1043 break;
1044 case ecl_aet_sf:
1045 offset = a->array.self.sf - to_array->array.self.sf;
1046 break;
1047 case ecl_aet_df:
1048 offset = a->array.self.df - to_array->array.self.df;
1049 break;
1050 case ecl_aet_lf:
1051 offset = a->array.self.lf - to_array->array.self.lf;
1052 break;
1053 #ifdef ECL_COMPLEX_FLOAT
1054 case ecl_aet_csf:
1055 offset = a->array.self.csf - to_array->array.self.csf;
1056 break;
1057 case ecl_aet_cdf:
1058 offset = a->array.self.cdf - to_array->array.self.cdf;
1059 break;
1060 case ecl_aet_clf:
1061 offset = a->array.self.clf - to_array->array.self.clf;
1062 break;
1063 #endif
1064 case ecl_aet_b8:
1065 case ecl_aet_i8:
1066 offset = a->array.self.b8 - to_array->array.self.b8;
1067 break;
1068 #ifdef ecl_uint16_t
1069 case ecl_aet_b16:
1070 case ecl_aet_i16:
1071 offset = a->array.self.b16 - to_array->array.self.b16;
1072 break;
1073 #endif
1074 #ifdef ecl_uint32_t
1075 case ecl_aet_b32:
1076 case ecl_aet_i32:
1077 offset = a->array.self.b32 - to_array->array.self.b32;
1078 break;
1079 #endif
1080 #ifdef ecl_uint64_t
1081 case ecl_aet_b64:
1082 case ecl_aet_i64:
1083 offset = a->array.self.b64 - to_array->array.self.b64;
1084 break;
1085 #endif
1086 default:
1087 FEbad_aet();
1088 }
1089 }
1090 ecl_return2(the_env, to_array, ecl_make_fixnum(offset));
1091 }
1092
1093 cl_object
1094 cl_svref(cl_object x, cl_object index)
1095 {
1096 const cl_env_ptr the_env = ecl_process_env();
1097 cl_index i;
1098
1099 if (ecl_unlikely(ecl_t_of(x) != t_vector ||
1100 (x->vector.flags & (ECL_FLAG_ADJUSTABLE | ECL_FLAG_HAS_FILL_POINTER)) ||
1101 CAR(x->vector.displaced) != ECL_NIL ||
1102 (cl_elttype)x->vector.elttype != ecl_aet_object))
1103 {
1104 FEwrong_type_nth_arg(@[svref],1,x,@[simple-vector]);
1105 }
1106 i = checked_index(@[svref], x, -1, index, x->vector.dim);
1107 ecl_return1(the_env, x->vector.self.t[i]);
1108 }
1109
1110 cl_object
1111 si_svset(cl_object x, cl_object index, cl_object v)
1112 {
1113 const cl_env_ptr the_env = ecl_process_env();
1114 cl_index i;
1115
1116 if (ecl_unlikely(ecl_t_of(x) != t_vector ||
1117 (x->vector.flags & (ECL_FLAG_ADJUSTABLE | ECL_FLAG_HAS_FILL_POINTER)) ||
1118 CAR(x->vector.displaced) != ECL_NIL ||
1119 (cl_elttype)x->vector.elttype != ecl_aet_object))
1120 {
1121 FEwrong_type_nth_arg(@[si::svset],1,x,@[simple-vector]);
1122 }
1123 i = checked_index(@[svref], x, -1, index, x->vector.dim);
1124 ecl_return1(the_env, x->vector.self.t[i] = v);
1125 }
1126
1127 #ifdef ECL_THREADS
1128 cl_object
1129 mp_compare_and_swap_svref(cl_object x, cl_object index, cl_object old, cl_object new)
1130 {
1131 cl_index i;
1132 if (ecl_unlikely(ecl_t_of(x) != t_vector ||
1133 (x->vector.flags & (ECL_FLAG_ADJUSTABLE | ECL_FLAG_HAS_FILL_POINTER)) ||
1134 CAR(x->vector.displaced) != ECL_NIL ||
1135 (cl_elttype)x->vector.elttype != ecl_aet_object))
1136 {
1137 FEwrong_type_nth_arg(@[mp::compare-and-swap-svref], 1, x, @[simple-vector]);
1138 }
1139 i = checked_index(@[mp::compare-and-swap-svref], x, -1, index, x->vector.dim);
1140 return ecl_compare_and_swap(x->vector.self.t + i, old, new);
1141 }
1142
1143 cl_object
1144 mp_atomic_incf_svref(cl_object x, cl_object index, cl_object increment)
1145 {
1146 cl_index i;
1147 if (ecl_unlikely(ecl_t_of(x) != t_vector ||
1148 (x->vector.flags & (ECL_FLAG_ADJUSTABLE | ECL_FLAG_HAS_FILL_POINTER)) ||
1149 CAR(x->vector.displaced) != ECL_NIL ||
1150 (cl_elttype)x->vector.elttype != ecl_aet_object))
1151 {
1152 FEwrong_type_nth_arg(@[mp::atomic-incf-svref], 1, x, @[simple-vector]);
1153 }
1154 i = checked_index(@[mp::atomic-incf-svref], x, -1, index, x->vector.dim);
1155 return ecl_atomic_incf(x->vector.self.t + i, increment);
1156 }
1157 #endif /* ECL_THREADS */
1158
1159 cl_object
1160 cl_array_has_fill_pointer_p(cl_object a)
1161 {
1162 const cl_env_ptr the_env = ecl_process_env();
1163 cl_object r;
1164 switch (ecl_t_of(a)) {
1165 case t_array:
1166 r = ECL_NIL; break;
1167 case t_vector:
1168 case t_bitvector:
1169 #ifdef ECL_UNICODE
1170 case t_string:
1171 #endif
1172 case t_base_string:
1173 r = ECL_ARRAY_HAS_FILL_POINTER_P(a)? ECL_T : ECL_NIL;
1174 break;
1175 default:
1176 FEwrong_type_only_arg(@[array-has-fill-pointer-p],a,@[array]);
1177 }
1178 ecl_return1(the_env, r);
1179 }
1180
1181 cl_object
1182 cl_fill_pointer(cl_object a)
1183 {
1184 const cl_env_ptr the_env = ecl_process_env();
1185 if (ecl_unlikely(!ECL_VECTORP(a)))
1186 FEwrong_type_only_arg(@[fill-pointer], a, @[vector]);
1187 if (ecl_unlikely(!ECL_ARRAY_HAS_FILL_POINTER_P(a))) {
1188 const char *type = "(AND VECTOR (SATISFIES ARRAY-HAS-FILL-POINTER-P))";
1189 FEwrong_type_only_arg(@[fill-pointer], a, ecl_read_from_cstring(type));
1190 }
1191 ecl_return1(the_env, ecl_make_fixnum(a->vector.fillp));
1192 }
1193
1194 /*
1195 Internal function for setting fill pointer.
1196 */
1197 cl_object
1198 si_fill_pointer_set(cl_object a, cl_object fp)
1199 {
1200 const cl_env_ptr the_env = ecl_process_env();
1201 cl_fixnum i;
1202 if (ecl_unlikely(!ECL_VECTORP(a) || !ECL_ARRAY_HAS_FILL_POINTER_P(a))) {
1203 const char *type = "(AND VECTOR (SATISFIES ARRAY-HAS-FILL-POINTER-P))";
1204 FEwrong_type_nth_arg(@[adjust-array], 1, a,
1205 ecl_read_from_cstring(type));
1206 }
1207 if (ecl_unlikely(!ECL_FIXNUMP(fp) || ((i = ecl_fixnum(fp)) < 0) ||
1208 (i > a->vector.dim))) {
1209 cl_object type = ecl_make_integer_type(ecl_make_fixnum(0),
1210 ecl_make_fixnum(a->vector.dim-1));
1211 FEwrong_type_key_arg(@[adjust-array], @[:fill-pointer], fp, type);
1212 }
1213 a->vector.fillp = i;
1214 ecl_return1(the_env, fp);
1215 }
1216
1217 /*
1218 Internal function for replacing the contents of arrays:
1219
1220 (si:replace-array old-array new-array).
1221
1222 Used in ADJUST-ARRAY and SI:ADJUST-VECTOR.
1223 */
1224 cl_object
1225 si_replace_array(cl_object olda, cl_object newa)
1226 {
1227 const cl_env_ptr the_env = ecl_process_env();
1228 cl_object dlist;
1229 if (ecl_t_of(olda) != ecl_t_of(newa)
1230 || (ecl_t_of(olda) == t_array && olda->array.rank != newa->array.rank))
1231 goto CANNOT;
1232 if (!ECL_ADJUSTABLE_ARRAY_P(olda)) {
1233 /* When an array is not adjustable, we simply output the new array */
1234 olda = newa;
1235 goto OUTPUT;
1236 }
1237 for (dlist = CDR(olda->array.displaced); dlist != ECL_NIL; dlist = CDR(dlist)) {
1238 cl_object other_array = CAR(dlist);
1239 cl_object offset;
1240 cl_array_displacement(other_array);
1241 offset = ecl_nth_value(the_env, 1);
1242 ecl_displace(other_array, newa, offset);
1243 }
1244 switch (ecl_t_of(olda)) {
1245 case t_array:
1246 case t_vector:
1247 case t_bitvector:
1248 olda->array = newa->array;
1249 break;
1250 #ifdef ECL_UNICODE
1251 case t_string:
1252 #endif
1253 case t_base_string:
1254 olda->base_string = newa->base_string;
1255 break;
1256 default:
1257 CANNOT:
1258 FEerror("Cannot replace the array ~S by the array ~S.",
1259 2, olda, newa);
1260 }
1261 OUTPUT:
1262 ecl_return1(the_env, olda);
1263 }
1264
1265 void
1266 ecl_copy_subarray(cl_object dest, cl_index i0, cl_object orig,
1267 cl_index i1, cl_index l)
1268 {
1269 cl_elttype t = ecl_array_elttype(dest);
1270 if (i0 + l > dest->array.dim) {
1271 l = dest->array.dim - i0;
1272 }
1273 if (i1 + l > orig->array.dim) {
1274 l = orig->array.dim - i1;
1275 }
1276 if (t != ecl_array_elttype(orig) || t == ecl_aet_bit) {
1277 if (dest == orig && i0 > i1) {
1278 for (i0 += l, i1 += l; l--; ) {
1279 ecl_aset_unsafe(dest, --i0,
1280 ecl_aref_unsafe(orig, --i1));
1281 }
1282 } else {
1283 while (l--) {
1284 ecl_aset_unsafe(dest, i0++,
1285 ecl_aref_unsafe(orig, i1++));
1286 }
1287 }
1288 } else {
1289 /* We could have singled out also dest == orig and used memcpy
1290 * but gcc-4.6 breaks this code even when i0 < i1 if the regions
1291 * overlap sufficiently. */
1292 cl_index elt_size = ecl_aet_size[t];
1293 memmove(dest->array.self.bc + i0 * elt_size,
1294 orig->array.self.bc + i1 * elt_size,
1295 l * elt_size);
1296 }
1297 }
1298
1299 void
1300 ecl_reverse_subarray(cl_object x, cl_index i0, cl_index i1)
1301 {
1302 cl_elttype t = ecl_array_elttype(x);
1303 cl_index i, j;
1304 if (x->array.dim == 0) {
1305 return;
1306 }
1307 if (i1 >= x->array.dim) {
1308 i1 = x->array.dim;
1309 }
1310 switch (t) {
1311 case ecl_aet_object:
1312 case ecl_aet_fix:
1313 case ecl_aet_index:
1314 for (i = i0, j = i1-1; i < j; i++, --j) {
1315 cl_object y = x->vector.self.t[i];
1316 x->vector.self.t[i] = x->vector.self.t[j];
1317 x->vector.self.t[j] = y;
1318 }
1319 break;
1320 case ecl_aet_sf:
1321 for (i = i0, j = i1-1; i < j; i++, --j) {
1322 float y = x->array.self.sf[i];
1323 x->array.self.sf[i] = x->array.self.sf[j];
1324 x->array.self.sf[j] = y;
1325 }
1326 break;
1327 case ecl_aet_df:
1328 for (i = i0, j = i1-1; i < j; i++, --j) {
1329 double y = x->array.self.df[i];
1330 x->array.self.df[i] = x->array.self.df[j];
1331 x->array.self.df[j] = y;
1332 }
1333 break;
1334 case ecl_aet_lf:
1335 for (i = i0, j = i1-1; i < j; i++, --j) {
1336 long double y = x->array.self.lf[i];
1337 x->array.self.lf[i] = x->array.self.lf[j];
1338 x->array.self.lf[j] = y;
1339 }
1340 break;
1341 #ifdef ECL_COMPLEX_FLOAT
1342 case ecl_aet_csf:
1343 for (i = i0, j = i1-1; i < j; i++, --j) {
1344 _Complex float y = x->array.self.csf[i];
1345 x->array.self.csf[i] = x->array.self.csf[j];
1346 x->array.self.csf[j] = y;
1347 }
1348 break;
1349 case ecl_aet_cdf:
1350 for (i = i0, j = i1-1; i < j; i++, --j) {
1351 _Complex double y = x->array.self.cdf[i];
1352 x->array.self.cdf[i] = x->array.self.cdf[j];
1353 x->array.self.cdf[j] = y;
1354 }
1355 break;
1356 case ecl_aet_clf:
1357 for (i = i0, j = i1-1; i < j; i++, --j) {
1358 _Complex long double y = x->array.self.clf[i];
1359 x->array.self.clf[i] = x->array.self.clf[j];
1360 x->array.self.clf[j] = y;
1361 }
1362 break;
1363 #endif
1364 case ecl_aet_bc:
1365 for (i = i0, j = i1-1; i < j; i++, --j) {
1366 ecl_base_char y = x->array.self.bc[i];
1367 x->array.self.bc[i] = x->array.self.bc[j];
1368 x->array.self.bc[j] = y;
1369 }
1370 break;
1371 case ecl_aet_b8:
1372 case ecl_aet_i8:
1373 for (i = i0, j = i1-1; i < j; i++, --j) {
1374 ecl_uint8_t y = x->array.self.b8[i];
1375 x->array.self.b8[i] = x->array.self.b8[j];
1376 x->array.self.b8[j] = y;
1377 }
1378 break;
1379 #ifdef ecl_uint16_t
1380 case ecl_aet_b16:
1381 case ecl_aet_i16:
1382 for (i = i0, j = i1-1; i < j; i++, --j) {
1383 ecl_uint16_t y = x->array.self.b16[i];
1384 x->array.self.b16[i] = x->array.self.b16[j];
1385 x->array.self.b16[j] = y;
1386 }
1387 break;
1388 #endif
1389 #ifdef ecl_uint32_t
1390 case ecl_aet_b32:
1391 case ecl_aet_i32:
1392 for (i = i0, j = i1-1; i < j; i++, --j) {
1393 ecl_uint32_t y = x->array.self.b32[i];
1394 x->array.self.b32[i] = x->array.self.b32[j];
1395 x->array.self.b32[j] = y;
1396 }
1397 break;
1398 #endif
1399 #ifdef ecl_uint64_t
1400 case ecl_aet_b64:
1401 case ecl_aet_i64:
1402 for (i = i0, j = i1-1; i < j; i++, --j) {
1403 ecl_uint64_t y = x->array.self.b64[i];
1404 x->array.self.b64[i] = x->array.self.b64[j];
1405 x->array.self.b64[j] = y;
1406 }
1407 break;
1408 #endif
1409 #ifdef ECL_UNICODE
1410 case ecl_aet_ch:
1411 for (i = i0, j = i1-1; i < j; i++, --j) {
1412 ecl_character y = x->array.self.c[i];
1413 x->array.self.c[i] = x->array.self.c[j];
1414 x->array.self.c[j] = y;
1415 }
1416 break;
1417 #endif
1418 case ecl_aet_bit:
1419 for (i = i0 + x->vector.offset,
1420 j = i1 + x->vector.offset - 1;
1421 i < j;
1422 i++, --j) {
1423 int k = x->array.self.bit[i/CHAR_BIT]&(0200>>i%CHAR_BIT);
1424 if (x->array.self.bit[j/CHAR_BIT]&(0200>>j%CHAR_BIT))
1425 x->array.self.bit[i/CHAR_BIT]
1426 |= 0200>>i%CHAR_BIT;
1427 else
1428 x->array.self.bit[i/CHAR_BIT]
1429 &= ~(0200>>i%CHAR_BIT);
1430 if (k)
1431 x->array.self.bit[j/CHAR_BIT]
1432 |= 0200>>j%CHAR_BIT;
1433 else
1434 x->array.self.bit[j/CHAR_BIT]
1435 &= ~(0200>>j%CHAR_BIT);
1436 }
1437 break;
1438 default:
1439 FEbad_aet();
1440 }
1441 }
1442
1443 cl_object
1444 si_copy_subarray(cl_object dest, cl_object start0,
1445 cl_object orig, cl_object start1, cl_object length)
1446 {
1447 ecl_copy_subarray(dest, ecl_to_size(start0),
1448 orig, ecl_to_size(start1),
1449 ecl_to_size(length));
1450 @(return dest);
1451 }
1452
1453 cl_object
1454 si_fill_array_with_elt(cl_object x, cl_object elt, cl_object start, cl_object end)
1455 {
1456 cl_elttype t = ecl_array_elttype(x);
1457 cl_index first = ecl_to_size(start);
1458 cl_index last = Null(end)? x->array.dim : ecl_to_size(end);
1459 if (first >= last) {
1460 goto END;
1461 }
1462 switch (t) {
1463 case ecl_aet_object: {
1464 cl_object *p = x->vector.self.t + first;
1465 for (first = last - first; first; --first, ++p) { *p = elt; }
1466 break;
1467 }
1468 case ecl_aet_bc: {
1469 ecl_base_char e = ecl_char_code(elt);
1470 ecl_base_char *p = x->vector.self.bc + first;
1471 for (first = last - first; first; --first, ++p) { *p = e; }
1472 break;
1473 }
1474 #ifdef ECL_UNICODE
1475 case ecl_aet_ch: {
1476 ecl_character e = ecl_char_code(elt);
1477 ecl_character *p = x->vector.self.c + first;
1478 for (first = last - first; first; --first, ++p) { *p = e; }
1479 break;
1480 }
1481 #endif
1482 case ecl_aet_fix: {
1483 cl_fixnum e = ecl_to_fix(elt);
1484 cl_fixnum *p = x->vector.self.fix + first;
1485 for (first = last - first; first; --first, ++p) { *p = e; }
1486 break;
1487 }
1488 case ecl_aet_index: {
1489 cl_index e = ecl_to_size(elt);
1490 cl_index *p = x->vector.self.index + first;
1491 for (first = last - first; first; --first, ++p) { *p = e; }
1492 break;
1493 }
1494 case ecl_aet_sf: {
1495 float e = ecl_to_float(elt);
1496 float *p = x->vector.self.sf + first;
1497 for (first = last - first; first; --first, ++p) { *p = e; }
1498 break;
1499 }
1500 case ecl_aet_df: {
1501 double e = ecl_to_double(elt);
1502 double *p = x->vector.self.df + first;
1503 for (first = last - first; first; --first, ++p) { *p = e; }
1504 break;
1505 }
1506 case ecl_aet_lf: {
1507 long double e = ecl_to_long_double(elt);
1508 long double *p = x->vector.self.lf + first;
1509 for (first = last - first; first; --first, ++p) { *p = e; }
1510 break;
1511 }
1512 #ifdef ECL_COMPLEX_FLOAT
1513 case ecl_aet_csf: {
1514 _Complex float e = ecl_to_csfloat(elt);
1515 _Complex float *p = x->vector.self.csf + first;
1516 for (first = last - first; first; --first, ++p) { *p = e; }
1517 break;
1518 }
1519 case ecl_aet_cdf: {
1520 _Complex double e = ecl_to_cdfloat(elt);
1521 _Complex double *p = x->vector.self.cdf + first;
1522 for (first = last - first; first; --first, ++p) { *p = e; }
1523 break;
1524 }
1525 case ecl_aet_clf: {
1526 _Complex long double e = ecl_to_clfloat(elt);
1527 _Complex long double *p = x->vector.self.clf + first;
1528 for (first = last - first; first; --first, ++p) { *p = e; }
1529 break;
1530 }
1531 #endif
1532 case ecl_aet_b8: {
1533 uint8_t e = ecl_to_uint8_t(elt);
1534 uint8_t *p = x->vector.self.b8 + first;
1535 for (first = last - first; first; --first, ++p) { *p = e; }
1536 break;
1537 }
1538 case ecl_aet_i8: {
1539 int8_t e = ecl_to_int8_t(elt);
1540 int8_t *p = x->vector.self.i8 + first;
1541 for (first = last - first; first; --first, ++p) { *p = e; }
1542 break;
1543 }
1544 #ifdef ecl_uint16_t
1545 case ecl_aet_b16: {
1546 ecl_uint16_t e = ecl_to_uint16_t(elt);
1547 ecl_uint16_t *p = x->vector.self.b16 + first;
1548 for (first = last - first; first; --first, ++p) { *p = e; }
1549 break;
1550 }
1551 case ecl_aet_i16: {
1552 ecl_int16_t e = ecl_to_int16_t(elt);
1553 ecl_int16_t *p = x->vector.self.i16 + first;
1554 for (first = last - first; first; --first, ++p) { *p = e; }
1555 break;
1556 }
1557 #endif
1558 #ifdef ecl_uint32_t
1559 case ecl_aet_b32: {
1560 ecl_uint32_t e = ecl_to_uint32_t(elt);
1561 ecl_uint32_t *p = x->vector.self.b32 + first;
1562 for (first = last - first; first; --first, ++p) { *p = e; }
1563 break;
1564 }
1565 case ecl_aet_i32: {
1566 ecl_int32_t e = ecl_to_int32_t(elt);
1567 ecl_int32_t *p = x->vector.self.i32 + first;
1568 for (first = last - first; first; --first, ++p) { *p = e; }
1569 break;
1570 }
1571 #endif
1572 #ifdef ecl_uint64_t
1573 case ecl_aet_b64: {
1574 ecl_uint64_t e = ecl_to_uint64_t(elt);
1575 ecl_uint64_t *p = x->vector.self.b64 + first;
1576 for (first = last - first; first; --first, ++p) { *p = e; }
1577 break;
1578 }
1579 case ecl_aet_i64: {
1580 ecl_int64_t e = ecl_to_int64_t(elt);
1581 ecl_int64_t *p = x->vector.self.i64 + first;
1582 for (first = last - first; first; --first, ++p) { *p = e; }
1583 break;
1584 }
1585 #endif
1586 case ecl_aet_bit: {
1587 int i = ecl_to_bit(elt);
1588 for (last -= first, first += x->vector.offset; last; --last, ++first) {
1589 int mask = 0200>>first%CHAR_BIT;
1590 if (i == 0)
1591 x->vector.self.bit[first/CHAR_BIT] &= ~mask;
1592 else
1593 x->vector.self.bit[first/CHAR_BIT] |= mask;
1594 }
1595 break;
1596 }
1597 default:
1598 FEbad_aet();
1599 }
1600 END:
1601 @(return x);
1602 }