Debian Codegraph @

src/c/num_log.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 * num_log.c - logical operations on numbers
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 <ecl/ecl.h>
16 #include <stdlib.h>
17 #include <ecl/internal.h>
18
19 /*
20 * BIT OPERATIONS FOR FIXNUMS
21 */
22
23 static cl_fixnum
24 ior_op(cl_fixnum i, cl_fixnum j)
25 {
26 return(i | j);
27 }
28
29 static cl_fixnum
30 xor_op(cl_fixnum i, cl_fixnum j)
31 {
32 return(i ^ j);
33 }
34
35 static cl_fixnum
36 and_op(cl_fixnum i, cl_fixnum j)
37 {
38 return(i & j);
39 }
40
41 static cl_fixnum
42 eqv_op(cl_fixnum i, cl_fixnum j)
43 {
44 return(~(i ^ j));
45 }
46
47 static cl_fixnum
48 nand_op(cl_fixnum i, cl_fixnum j)
49 {
50 return(~(i & j));
51 }
52
53 static cl_fixnum
54 nor_op(cl_fixnum i, cl_fixnum j)
55 {
56 return(~(i | j));
57 }
58
59 static cl_fixnum
60 andc1_op(cl_fixnum i, cl_fixnum j)
61 {
62 return((~i) & j);
63 }
64
65 static cl_fixnum
66 andc2_op(cl_fixnum i, cl_fixnum j)
67 {
68 return(i & (~j));
69 }
70
71 static cl_fixnum
72 orc1_op(cl_fixnum i, cl_fixnum j)
73 {
74 return((~i) | j);
75 }
76
77 static cl_fixnum
78 orc2_op(cl_fixnum i, cl_fixnum j)
79 {
80 return(i | (~j));
81 }
82
83 static cl_fixnum
84 b_clr_op(cl_fixnum i, cl_fixnum j)
85 {
86 return(0);
87 }
88
89 static cl_fixnum
90 b_set_op(cl_fixnum i, cl_fixnum j)
91 {
92 return(-1);
93 }
94
95 static cl_fixnum
96 b_1_op(cl_fixnum i, cl_fixnum j)
97 {
98 return(i);
99 }
100
101 static cl_fixnum
102 b_2_op(cl_fixnum i, cl_fixnum j)
103 {
104 return(j);
105 }
106
107 static cl_fixnum
108 b_c1_op(cl_fixnum i, cl_fixnum j)
109 {
110 return(~i);
111 }
112
113 static cl_fixnum
114 b_c2_op(cl_fixnum i, cl_fixnum j)
115 {
116 return(~j);
117 }
118
119 typedef cl_fixnum (*bit_operator)(cl_fixnum, cl_fixnum);
120
121 static bit_operator fixnum_operations[16] = {
122 b_clr_op,
123 and_op,
124 andc2_op,
125 b_1_op,
126 andc1_op,
127 b_2_op,
128 xor_op,
129 ior_op,
130 nor_op,
131 eqv_op,
132 b_c2_op,
133 orc2_op,
134 b_c1_op,
135 orc1_op,
136 nand_op,
137 b_set_op};
138
139
140 static cl_object
141 log_op(cl_narg narg, int op, ecl_va_list ARGS)
142 {
143 cl_object x, y;
144 /* FIXME! This can be optimized */
145 x = ecl_va_arg(ARGS);
146 if (narg-- == 1) {
147 assert_type_integer(x);
148 } else {
149 do {
150 y = ecl_va_arg(ARGS);
151 x = ecl_boole(op, x, y);
152 } while (--narg);
153 }
154 return x;
155 }
156
157 cl_object
158 ecl_boole(int op, cl_object x, cl_object y)
159 {
160 switch (ecl_t_of(x)) {
161 case t_fixnum:
162 switch (ecl_t_of(y)) {
163 case t_fixnum: {
164 cl_fixnum z = fixnum_operations[op](ecl_fixnum(x), ecl_fixnum(y));
165 return ecl_make_fixnum(z);
166 }
167 case t_bignum: {
168 cl_object x_copy = _ecl_big_register0();
169 _ecl_big_set_fixnum(x_copy, ecl_fixnum(x));
170 (_ecl_big_boole_operator(op))(x_copy, x_copy, y);
171 return _ecl_big_register_normalize(x_copy);
172 }
173 default:
174 FEwrong_type_nth_arg(@[boole], 2, y, @[integer]);
175 }
176 break;
177 case t_bignum: {
178 cl_object x_copy = _ecl_big_register0();
179 switch (ecl_t_of(y)) {
180 case t_fixnum: {
181 cl_object z = _ecl_big_register1();
182 _ecl_big_set_fixnum(z,ecl_fixnum(y));
183 (_ecl_big_boole_operator(op))(x_copy, x, z);
184 _ecl_big_register_free(z);
185 break;
186 }
187 case t_bignum:
188 (_ecl_big_boole_operator(op))(x_copy, x, y);
189 break;
190 default:
191 FEwrong_type_nth_arg(@[boole], 2, y, @[integer]);
192 }
193 return _ecl_big_register_normalize(x_copy);
194 }
195 default:
196 FEwrong_type_nth_arg(@[boole], 1, x, @[integer]);
197 }
198 return x;
199 }
200
201 cl_object
202 cl_lognot(cl_object x)
203 {
204 return @logxor(2,x,ecl_make_fixnum(-1));
205 }
206
207 static cl_fixnum
208 count_bits(cl_object x)
209 {
210 cl_fixnum count;
211
212 switch (ecl_t_of(x)) {
213 case t_fixnum: {
214 cl_fixnum i = ecl_fixnum(x);
215 cl_fixnum j = (i < 0) ? ~i : i;
216 for (count=0 ; j ; j >>= 1)
217 if (j & 1) count++;
218 break;
219 }
220 case t_bignum:
221 if (_ecl_big_sign(x) >= 0)
222 count = _ecl_big_popcount(x);
223 else {
224 cl_object z = _ecl_big_register0();
225 _ecl_big_1com(z, x);
226 count = _ecl_big_popcount(z);
227 _ecl_big_register_free(z);
228 }
229 break;
230 default:
231 FEwrong_type_only_arg(@[logcount], x, @[integer]);
232 }
233 return count;
234 }
235
236 /*
237 Left shift if w > 0, right shift if w < 0.
238 */
239 cl_object
240 ecl_ash(cl_object x, cl_fixnum w)
241 {
242 cl_object y;
243
244 if (w == 0)
245 return(x);
246 y = _ecl_big_register0();
247 if (w < 0) {
248 cl_index bits = -w;
249 if (ECL_FIXNUMP(x)) {
250 /* The result of shifting a number further than the number
251 * of digits it has is unpredictable in C. For instance, GCC
252 * on intel masks out all bits of "bits" beyond the 5 and
253 * it may happen that a shift of 37 becomes a shift of 5.
254 * Furthermore, in general, shifting negative numbers leads
255 * to implementation-specific results :-/
256 */
257 cl_fixnum y = ecl_fixnum(x);
258 if (bits >= ECL_FIXNUM_BITS) {
259 y = (y < 0)? -1 : 0;
260 } else {
261 y >>= bits;
262 }
263 return ecl_make_fixnum(y);
264 }
265 _ecl_big_div_2exp(y, x, bits);
266 } else {
267 if (ECL_FIXNUMP(x)) {
268 _ecl_big_set_fixnum(y, ecl_fixnum(x));
269 x = y;
270 }
271 _ecl_big_mul_2exp(y, x, (unsigned long)w);
272 }
273 return _ecl_big_register_normalize(y);
274 }
275
276 int
277 ecl_fixnum_bit_length(cl_fixnum i)
278 {
279 int count;
280 if (i < 0)
281 i = ~i;
282 for (count = 0; i && (count < ECL_FIXNUM_BITS); i >>= 1, count++)
283 ;
284 return count;
285 }
286
287 @(defun logior (&rest nums)
288 @
289 if (narg == 0)
290 @(return ecl_make_fixnum(0))
291 /* INV: log_op() checks types and outputs first argument as default. */
292 @(return log_op(narg, ECL_BOOLIOR, nums));
293 @)
294
295 @(defun logxor (&rest nums)
296 @
297 if (narg == 0) {
298 @(return ecl_make_fixnum(0));
299 }
300 /* INV: log_op() checks types and outputs first argument as default. */
301 @(return log_op(narg, ECL_BOOLXOR, nums));
302 @)
303
304 @(defun logand (&rest nums)
305 @
306 if (narg == 0) {
307 @(return ecl_make_fixnum(-1));
308 }
309 /* INV: log_op() checks types and outputs first argument as default. */
310 @(return log_op(narg, ECL_BOOLAND, nums));
311 @)
312
313 @(defun logeqv (&rest nums)
314 @
315 if (narg == 0) {
316 @(return ecl_make_fixnum(-1));
317 }
318 /* INV: log_op() checks types and outputs first argument as default. */
319 @(return log_op(narg, ECL_BOOLEQV, nums));
320 @)
321
322 cl_object
323 cl_lognand(cl_object x, cl_object y)
324 {
325 @(return ecl_boole(ECL_BOOLNAND, x, y));
326 }
327
328 cl_object
329 cl_lognor(cl_object x, cl_object y)
330 {
331 @(return ecl_boole(ECL_BOOLNOR, x, y));
332 }
333
334 cl_object
335 cl_logandc1(cl_object x, cl_object y)
336 {
337 @(return ecl_boole(ECL_BOOLANDC1, x, y));
338 }
339
340 cl_object
341 cl_logandc2(cl_object x, cl_object y)
342 {
343 @(return ecl_boole(ECL_BOOLANDC2, x, y));
344 }
345
346 cl_object
347 cl_logorc1(cl_object x, cl_object y)
348 {
349 @(return ecl_boole(ECL_BOOLORC1, x, y));
350 }
351
352 cl_object
353 cl_logorc2(cl_object x, cl_object y)
354 {
355 @(return ecl_boole(ECL_BOOLORC2, x, y));
356 }
357
358 static int
359 coerce_to_logical_operator(cl_object o)
360 {
361 cl_fixnum op;
362 op = ecl_to_fix(o);
363 if (op < 0 || op > ECL_BOOLSET)
364 FEerror("~S is an invalid logical operator.", 1, o);
365 return op;
366 }
367
368 cl_object
369 cl_boole(cl_object o, cl_object x, cl_object y)
370 {
371 /* INV: log_op2() checks types */
372 @(return ecl_boole(coerce_to_logical_operator(o), x, y));
373 }
374
375 cl_object
376 cl_logbitp(cl_object p, cl_object x)
377 {
378 bool i;
379
380 assert_type_integer(x);
381 if (ECL_FIXNUMP(p)) {
382 cl_index n = ecl_to_size(p);
383 if (ECL_FIXNUMP(x)) {
384 cl_fixnum y = ecl_fixnum(x);
385 if (n >= ECL_FIXNUM_BITS) {
386 i = (y < 0);
387 } else {
388 i = ((y >> n) & 1);
389 }
390 } else {
391 i = _ecl_big_tstbit(x, n);
392 }
393 } else {
394 assert_type_non_negative_integer(p);
395 if (ECL_FIXNUMP(x))
396 i = (ecl_fixnum(x) < 0);
397 else
398 i = (_ecl_big_sign(x) < 0);
399 }
400 @(return (i ? ECL_T : ECL_NIL));
401 }
402
403 cl_object
404 cl_ash(cl_object x, cl_object y)
405 {
406 cl_object r;
407 int sign_x;
408
409 assert_type_integer(x);
410 assert_type_integer(y);
411 if (ECL_FIXNUMP(y))
412 r = ecl_ash(x, ecl_fixnum(y));
413 else {
414 /*
415 bit position represented by bignum is probably
416 out of our address space. So, result is returned
417 according to sign of integer.
418 */
419 if (ECL_FIXNUMP(x))
420 if (ecl_fixnum_minusp(x))
421 sign_x = -1;
422 else if (x == ecl_make_fixnum(0))
423 sign_x = 0;
424 else
425 sign_x = 1;
426 else
427 sign_x = _ecl_big_sign(x);
428 if (_ecl_big_sign(y) < 0)
429 if (sign_x < 0)
430 r = ecl_make_fixnum(-1);
431 else
432 r = ecl_make_fixnum(0);
433 else if (sign_x == 0)
434 r = x;
435 else
436 FEerror("Insufficient memory.", 0);
437 }
438 @(return r);
439 }
440
441 cl_object
442 cl_logcount(cl_object x)
443 {
444 @(return ecl_make_fixnum(count_bits(x)));
445 }
446
447 cl_index
448 ecl_integer_length(cl_object x)
449 {
450 int count;
451 cl_fixnum i;
452
453 switch (ecl_t_of(x)) {
454 case t_fixnum:
455 i = ecl_fixnum(x);
456 count = ecl_fixnum_bit_length(i);
457 break;
458 case t_bignum:
459 if (_ecl_big_sign(x) < 0)
460 x = cl_lognot(x);
461 count = _ecl_big_bits(x);
462 break;
463 default:
464 FEwrong_type_only_arg(@[integer-length], x, @[integer]);
465 }
466 return count;
467 }
468
469 cl_object
470 cl_integer_length(cl_object x)
471 {
472 @(return ecl_make_fixnum(ecl_integer_length(x)));
473 }
474
475 cl_object
476 si_bit_array_op(cl_object o, cl_object x, cl_object y, cl_object r)
477 {
478 cl_fixnum i, j, n, d;
479 cl_object r0;
480 bit_operator op;
481 bool replace = FALSE;
482 int xi, yi, ri;
483 byte *xp, *yp, *rp;
484 int xo, yo, ro;
485
486 if (ecl_t_of(x) == t_bitvector) {
487 d = x->vector.dim;
488 xp = x->vector.self.bit;
489 xo = x->vector.offset;
490 if (ecl_t_of(y) != t_bitvector)
491 goto ERROR;
492 if (d != y->vector.dim)
493 goto ERROR;
494 yp = y->vector.self.bit;
495 yo = y->vector.offset;
496 if (r == ECL_T)
497 r = x;
498 if (r != ECL_NIL) {
499 if (ecl_t_of(r) != t_bitvector)
500 goto ERROR;
501 if (r->vector.dim != d)
502 goto ERROR;
503 i = (r->vector.self.bit - xp)*8 + (r->vector.offset - xo);
504 if ((i > 0 && i < d) || (i < 0 && -i < d)) {
505 r0 = r;
506 r = ECL_NIL;
507 replace = TRUE;
508 goto L1;
509 }
510 i = (r->vector.self.bit - yp)*8 + (r->vector.offset - yo);
511 if ((i > 0 && i < d) || (i < 0 && -i < d)) {
512 r0 = r;
513 r = ECL_NIL;
514 replace = TRUE;
515 }
516 }
517 L1:
518 if (Null(r)) {
519 r = ecl_alloc_simple_vector(d, ecl_aet_bit);
520 }
521 } else {
522 if (ecl_t_of(x) != t_array)
523 goto ERROR;
524 if ((cl_elttype)x->array.elttype != ecl_aet_bit)
525 goto ERROR;
526 d = x->array.dim;
527 xp = x->vector.self.bit;
528 xo = x->vector.offset;
529 if (ecl_t_of(y) != t_array)
530 goto ERROR;
531 if ((cl_elttype)y->array.elttype != ecl_aet_bit)
532 goto ERROR;
533 if (x->array.rank != y->array.rank)
534 goto ERROR;
535 yp = y->vector.self.bit;
536 yo = y->vector.offset;
537 for (i = 0; i < x->array.rank; i++)
538 if (x->array.dims[i] != y->array.dims[i])
539 goto ERROR;
540 if (r == ECL_T)
541 r = x;
542 if (r != ECL_NIL) {
543 if (ecl_t_of(r) != t_array)
544 goto ERROR;
545 if ((cl_elttype)r->array.elttype != ecl_aet_bit)
546 goto ERROR;
547 if (r->array.rank != x->array.rank)
548 goto ERROR;
549 for (i = 0; i < x->array.rank; i++)
550 if (r->array.dims[i] != x->array.dims[i])
551 goto ERROR;
552 i = (r->vector.self.bit - xp)*8 + (r->vector.offset - xo);
553 if ((i > 0 && i < d) || (i < 0 && -i < d)) {
554 r0 = r;
555 r = ECL_NIL;
556 replace = TRUE;
557 goto L2;
558 }
559 i = (r->vector.self.bit - yp)*8 + (r->vector.offset - yo);
560 if ((i > 0 && i < d) || (i < 0 && -i < d)) {
561 r0 = r;
562 r = ECL_NIL;
563 replace = TRUE;
564 }
565 }
566 L2:
567 if (Null(r)) {
568 r = ecl_alloc_object(t_array);
569 r->array.self.t = NULL;
570 r->array.displaced = ECL_NIL;
571 r->array.rank = x->array.rank;
572 r->array.dims = x->array.dims;
573 r->array.elttype = ecl_aet_bit;
574 r->array.dim = x->array.dim;
575 r->array.flags = 0; /* no fill pointer, not adjustable */
576 ecl_array_allocself(r);
577 }
578 }
579 rp = r->vector.self.bit;
580 ro = r->vector.offset;
581 op = fixnum_operations[coerce_to_logical_operator(o)];
582
583 #define set_high(place, nbits, value) \
584 (place)=((place)&~(-0400>>(nbits)))|((value)&(-0400>>(nbits)))
585
586 #define set_low(place, nbits, value) \
587 (place)=((place)&(-0400>>(8-(nbits))))|((value)&~(-0400>>(8-(nbits))))
588
589 #define extract_byte(integer, pointer, index, offset) \
590 (integer) = (pointer)[(index)+1] & 0377; \
591 (integer) = ((pointer)[index]<<(offset))|((integer)>>(8-(offset)))
592
593 #define store_byte(pointer, index, offset, value) \
594 set_low((pointer)[index], 8-(offset), (value)>>(offset)); \
595 set_high((pointer)[(index)+1], offset, (value)<<(8-(offset)))
596
597 if (xo == 0 && yo == 0 && ro == 0) {
598 for (n = d/8, i = 0; i < n; i++)
599 rp[i] = (*op)(xp[i], yp[i]);
600 if ((j = d%8) > 0)
601 set_high(rp[n], j, (*op)(xp[n], yp[n]));
602 if (!replace) {
603 @(return r);
604 }
605 } else {
606 for (n = d/8, i = 0; i <= n; i++) {
607 extract_byte(xi, xp, i, xo);
608 extract_byte(yi, yp, i, yo);
609 if (i == n) {
610 if ((j = d%8) == 0)
611 break;
612 extract_byte(ri, rp, n, ro);
613 set_high(ri, j, (*op)(xi, yi));
614 } else
615 ri = (*op)(xi, yi);
616 store_byte(rp, i, ro, ri);
617 }
618 if (!replace) {
619 @(return r);
620 }
621 }
622 rp = r0->vector.self.bit;
623 ro = r0->vector.offset;
624 for (n = d/8, i = 0; i <= n; i++) {
625 if (i == n) {
626 if ((j = d%8) == 0)
627 break;
628 extract_byte(ri, rp, n, ro);
629 set_high(ri, j, r->vector.self.bit[n]);
630 } else
631 ri = r->vector.self.bit[i];
632 store_byte(rp, i, ro, ri);
633 }
634 @(return r0);
635 ERROR:
636 FEerror("Illegal arguments for bit-array operation.", 0);
637 }