num_log.d 15.6 KB
Newer Older
jjgarcia's avatar
jjgarcia committed
1 2 3 4 5 6 7 8
/*
    num_log.c  -- Logical operations on numbers.
*/
/*
    Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya.
    Copyright (c) 1990, Giuseppe Attardi.
    Copyright (c) 2001, Juan Jose Garcia Ripoll.

9
    ECL is free software; you can redistribute it and/or
jjgarcia's avatar
jjgarcia committed
10 11 12 13 14 15 16
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    See file '../Copyright' for full details.
*/

17
#include "ecl.h"
jjgarcia's avatar
jjgarcia committed
18
#include <stdlib.h>
19
#include "internal.h"
jjgarcia's avatar
jjgarcia committed
20 21

/*
22 23
 * BIT OPERATIONS FOR FIXNUMS
 */
jjgarcia's avatar
jjgarcia committed
24

25 26
static cl_fixnum
ior_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
27
{
28
	return(i | j);
jjgarcia's avatar
jjgarcia committed
29
}
30

31 32
static void
mpz_ior_op(cl_object i, cl_object j)
33
{
34
	mpz_ior(i->big.big_num, i->big.big_num, j->big.big_num);
jjgarcia's avatar
jjgarcia committed
35 36
}

37
static cl_fixnum
38
xor_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
39
{
40
	return(i ^ j);
jjgarcia's avatar
jjgarcia committed
41 42
}

43 44
static void
mpz_xor_op(cl_object i, cl_object j)
jjgarcia's avatar
jjgarcia committed
45
{
46
	mpz_xor(i->big.big_num, i->big.big_num, j->big.big_num);
jjgarcia's avatar
jjgarcia committed
47 48
}

49 50
static cl_fixnum
and_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
51 52 53 54
{
	return(i & j);
}

55 56 57 58 59 60
static void
mpz_and_op(cl_object i, cl_object j)
{
	mpz_and(i->big.big_num, i->big.big_num, j->big.big_num);
}

61 62
static cl_fixnum
eqv_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
63 64 65 66
{
	return(~(i ^ j));
}

67 68 69 70 71 72 73
static void
mpz_eqv_op(cl_object i, cl_object j)
{
	mpz_xor(i->big.big_num, i->big.big_num, j->big.big_num);
	mpz_com(i->big.big_num, i->big.big_num);
}

74 75
static cl_fixnum
nand_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
76 77 78 79
{
	return(~(i & j));
}

80 81 82 83 84 85 86
static void
mpz_nand_op(cl_object i, cl_object j)
{
	mpz_and(i->big.big_num, i->big.big_num, j->big.big_num);
	mpz_com(i->big.big_num, i->big.big_num);
}

87 88
static cl_fixnum
nor_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
89 90 91 92
{
	return(~(i | j));
}

93 94 95 96 97 98 99
static void
mpz_nor_op(cl_object i, cl_object j)
{
	mpz_ior(i->big.big_num, i->big.big_num, j->big.big_num);
	mpz_com(i->big.big_num, i->big.big_num);
}

100 101
static cl_fixnum
andc1_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
102 103 104 105
{
	return((~i) & j);
}

106 107 108 109 110 111 112
static void
mpz_andc1_op(cl_object i, cl_object j)
{
	mpz_com(i->big.big_num, i->big.big_num);
	mpz_and(i->big.big_num, i->big.big_num, j->big.big_num);
}

113 114
static cl_fixnum
andc2_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
115 116 117 118
{
	return(i & (~j));
}

119 120 121 122 123 124 125 126 127 128
static void mpz_orc1_op(cl_object, cl_object);

static void
mpz_andc2_op(cl_object i, cl_object j)
{
	/* (i & ~j) = ~((~i) | j) */
	mpz_orc1_op(i, j);
	mpz_com(i->big.big_num, i->big.big_num);
}

129 130
static cl_fixnum
orc1_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
131 132 133 134
{
	return((~i) | j);
}

135 136 137 138 139 140 141
static void
mpz_orc1_op(cl_object i, cl_object j)
{
	mpz_com(i->big.big_num, i->big.big_num);
	mpz_ior(i->big.big_num, i->big.big_num, j->big.big_num);
}

142 143
static cl_fixnum
orc2_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
144 145 146 147
{
	return(i | (~j));
}

148 149 150 151 152 153 154 155
static void
mpz_orc2_op(cl_object i, cl_object j)
{
	/* (i | ~j) = ~((~i) & j) */
	mpz_andc1_op(i, j);
	mpz_com(i->big.big_num, i->big.big_num);
}

156 157
static cl_fixnum
b_clr_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
158 159 160 161
{
	return(0);
}

162 163 164 165 166 167
static void
mpz_b_clr_op(cl_object i, cl_object j)
{
	mpz_set_si(i->big.big_num, 0);
}

168 169
static cl_fixnum
b_set_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
170 171 172 173
{
	return(-1);
}

174 175 176 177 178 179
static void
mpz_b_set_op(cl_object i, cl_object j)
{
	mpz_set_si(i->big.big_num, -1);
}

180 181
static cl_fixnum
b_1_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
182 183 184 185
{
	return(i);
}

186 187 188 189 190
static void
mpz_b_1_op(cl_object i, cl_object j)
{
}

191 192
static cl_fixnum
b_2_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
193 194 195 196
{
	return(j);
}

197 198 199 200 201 202
static void
mpz_b_2_op(cl_object i, cl_object j)
{
	mpz_set(i->big.big_num, j->big.big_num);
}

203 204
static cl_fixnum
b_c1_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
205 206 207 208
{
	return(~i);
}

209 210 211 212 213 214
static void
mpz_b_c1_op(cl_object i, cl_object j)
{
	mpz_com(i->big.big_num, i->big.big_num);
}

215 216
static cl_fixnum
b_c2_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
217 218 219 220
{
	return(~j);
}

221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
static void
mpz_b_c2_op(cl_object i, cl_object j)
{
	mpz_com(i->big.big_num, j->big.big_num);
}

typedef cl_fixnum (*bit_operator)(cl_fixnum, cl_fixnum);
typedef void (*bignum_bit_operator)(cl_object, cl_object);

static bit_operator fixnum_operations[16] = {
	b_clr_op,
	and_op,
	andc2_op,
	b_1_op,
	andc1_op,
	b_2_op,
	xor_op,
	ior_op,
	nor_op,
	eqv_op,
	b_c2_op,
	orc2_op,
	b_c1_op,
	orc1_op,
	nand_op,
	b_set_op};

static bignum_bit_operator bignum_operations[16] = {
	mpz_b_clr_op,
	mpz_and_op,
	mpz_andc2_op,
	mpz_b_1_op,
	mpz_andc1_op,
	mpz_b_2_op,
	mpz_xor_op,
	mpz_ior_op,
	mpz_nor_op,
	mpz_eqv_op,
	mpz_b_c2_op,
	mpz_orc2_op,
	mpz_b_c1_op,
	mpz_orc1_op,
	mpz_nand_op,
	mpz_b_set_op};


static cl_object
268
log_op(cl_narg narg, int op, cl_va_list ARGS)
269
{
270 271 272 273 274 275 276 277 278
#if 1
	cl_object x, y;
	/* FIXME! This can be optimized */
	x = cl_va_arg(ARGS);
	if (narg-- == 1) {
		assert_type_integer(x);
	} else {
		do {
			y = cl_va_arg(ARGS);
279
			x = ecl_boole(op, x, y);
280 281 282 283
		} while (--narg);
	}
	return x;
#else
284 285 286
	cl_object x, numi;
	bit_operator fix_log_op;
	bignum_bit_operator big_log_op;
287 288
	int i = 1;
	cl_fixnum j;
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341

	x = cl_va_arg(ARGS);
	switch (type_of(x)) {
	    case t_fixnum:
		break;
	    case t_bignum:
		x = big_copy(x);	/* since big_log_op clobbers it */
		goto BIG_OP;
	    default:
		FEtype_error_integer(x);
	}
	if (narg == 1)
		return x;
	j = fix(x);
	fix_log_op = fixnum_operations[op];
	for (; i < narg; i++) {
		numi = cl_va_arg(ARGS);
		switch (type_of(numi)) {
		    case t_fixnum:
			j = (*fix_log_op)(j, fix(numi));
			break;
		    case t_bignum:
			big_log_op = bignum_operations[op];
			x = bignum1(j);
			goto BIG_OP2;
		    default:
			FEtype_error_integer(numi);
		}
	}
	return(MAKE_FIXNUM(j));

BIG_OP:
	if (narg == 1)
		return x;
	big_log_op = bignum_operations[op];
	for (; i < narg; i++) {
		numi = cl_va_arg(ARGS);
		switch (type_of(numi)) {
		    case t_fixnum: {
			cl_object z = big_register1_get();
			mpz_set_si(z->big.big_num, fix(numi));
			(*big_log_op)(x, z);
			big_register_free(z);
			break;
		    }
		    case t_bignum: BIG_OP2:
			(*big_log_op)(x, numi);
			break;
		    default:
			FEtype_error_integer(numi);
		}
	}
	return(big_normalize(x));
342
#endif
343 344
}

345 346
cl_object
ecl_boole(int op, cl_object x, cl_object y)
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
{
	switch (type_of(x)) {
	case t_fixnum:
		switch (type_of(y)) {
		case t_fixnum: {
			cl_fixnum (*fix_log_op)(cl_fixnum, cl_fixnum);
			fix_log_op = fixnum_operations[op];
			return MAKE_FIXNUM((*fix_log_op)(fix(x), fix(y)));
		}
		case t_bignum: {
			void (*big_log_op)(cl_object, cl_object);
			big_log_op = bignum_operations[op];
			x = bignum1(fix(x));
			(*big_log_op)(x, y);
			break;
		}
		default:
			FEtype_error_integer(y);
		}
		break;
	case t_bignum: {
		void (*big_log_op)(cl_object, cl_object);
		big_log_op = bignum_operations[op];
		x = big_copy(x);
		switch (type_of(y)) {
		case t_fixnum: {
			cl_object z = big_register1_get();
			mpz_set_si(z->big.big_num, fix(y));
			(*big_log_op)(x, z);
			big_register_free(z);
			break;
		}
		case t_bignum:
			(*big_log_op)(x,y);
			break;
		default:
			FEtype_error_integer(y);
		}
		break;
	}
	default:
		FEtype_error_integer(x);
	}
	return big_normalize(x);
}

393 394 395
cl_object
cl_lognot(cl_object x)
{
396
	return @logxor(2,x,MAKE_FIXNUM(-1));
397
}
jjgarcia's avatar
jjgarcia committed
398

399
static cl_fixnum
jjgarcia's avatar
jjgarcia committed
400 401 402 403 404 405 406 407 408
count_bits(cl_object x)
{
	cl_fixnum count;

	switch (type_of(x)) {
	case t_fixnum: {
		cl_fixnum i = fix(x);
		cl_fixnum j = (i < 0) ? ~i : i;
		for (count=0 ; j ; j >>= 1)
409
			if (j & 1) count++;
jjgarcia's avatar
jjgarcia committed
410 411 412
		break;
	}
	case t_bignum:
413 414 415 416 417
		if (big_sign(x) >= 0)
			count = mpz_popcount(x->big.big_num);
		else {
			cl_object z = big_register0_get();
			mpz_com(z->big.big_num, x->big.big_num);
418
			count = mpz_popcount(z->big.big_num);
419
			big_register_free(z);
jjgarcia's avatar
jjgarcia committed
420 421 422 423 424 425 426 427 428 429 430 431
		}
		break;
	default:
		FEtype_error_integer(x);
	}
	return count;
}

/*
   Left shift if w > 0, right shift if w < 0.
 */
cl_object
432
ecl_ash(cl_object x, cl_fixnum w)
jjgarcia's avatar
jjgarcia committed
433 434
{
	cl_object y;
435 436 437

	if (w == 0)
		return(x);
jjgarcia's avatar
jjgarcia committed
438 439
	y = big_register0_get();
	if (w < 0) {
440
		cl_index bits = -w;
441
		if (FIXNUMP(x)) {
442 443 444 445 446 447 448
			/* The result of shifting a number further than the number
			 * of digits it has is unpredictable in C. For instance, GCC
			 * on intel masks out all bits of "bits" beyond the 5 and
			 * it may happen that a shift of 37 becomes a shift of 5.
			 * Furthermore, in general, shifting negative numbers leads
			 * to implementation-specific results :-/
			 */
449
			cl_fixnum y = fix(x);
450 451 452 453 454
			if (bits >= FIXNUM_BITS) {
				y = (y < 0)? -1 : 0;
			} else {
				y >>= bits;
			}
455 456
			return MAKE_FIXNUM(y);
		}
457
		mpz_div_2exp(y->big.big_num, x->big.big_num, bits);
jjgarcia's avatar
jjgarcia committed
458
	} else {
459 460 461 462
		if (FIXNUMP(x)) {
			mpz_set_si(y->big.big_num, fix(x));
			x = y;
		}
463
		mpz_mul_2exp(y->big.big_num, x->big.big_num, (unsigned long)w);
jjgarcia's avatar
jjgarcia committed
464 465 466 467
	}
	return(big_register_normalize(y));
}

468 469
int
ecl_fixnum_bit_length(cl_fixnum i)
jjgarcia's avatar
jjgarcia committed
470
{
471 472 473 474 475 476
	int count;
	if (i < 0)
		i = ~i;
	for (count = 0; i && (count < FIXNUM_BITS); i >>= 1, count++)
		;
	return count;
jjgarcia's avatar
jjgarcia committed
477 478 479 480 481 482
}

@(defun logior (&rest nums)
@
	if (narg == 0)
		@(return MAKE_FIXNUM(0))
483
	/* INV: log_op() checks types and outputs first argument as default. */
484
	@(return log_op(narg, ECL_BOOLIOR, nums))
jjgarcia's avatar
jjgarcia committed
485 486 487 488 489 490
@)

@(defun logxor (&rest nums)
@
	if (narg == 0)
		@(return MAKE_FIXNUM(0))
491
	/* INV: log_op() checks types and outputs first argument as default. */
492
	@(return log_op(narg, ECL_BOOLXOR, nums))
jjgarcia's avatar
jjgarcia committed
493 494 495 496 497 498
@)

@(defun logand (&rest nums)
@
	if (narg == 0)
		@(return MAKE_FIXNUM(-1))
499
	/* INV: log_op() checks types and outputs first argument as default. */
500
	@(return log_op(narg, ECL_BOOLAND, nums))
jjgarcia's avatar
jjgarcia committed
501 502 503 504 505 506
@)

@(defun logeqv (&rest nums)
@
	if (narg == 0)
		@(return MAKE_FIXNUM(-1))
507
	/* INV: log_op() checks types and outputs first argument as default. */
508
	@(return log_op(narg, ECL_BOOLEQV, nums))
jjgarcia's avatar
jjgarcia committed
509 510
@)

511 512 513
cl_object
cl_lognand(cl_object x, cl_object y)
{
514
	@(return ecl_boole(ECL_BOOLNAND, x, y))
515
}
516

517 518 519
cl_object
cl_lognor(cl_object x, cl_object y)
{
520
	@(return ecl_boole(ECL_BOOLNOR, x, y))
521
}
522

523 524 525
cl_object
cl_logandc1(cl_object x, cl_object y)
{
526
	@(return ecl_boole(ECL_BOOLANDC1, x, y))
527
}
528

529 530 531
cl_object
cl_logandc2(cl_object x, cl_object y)
{
532
	@(return ecl_boole(ECL_BOOLANDC2, x, y))
533
}
534

535 536 537
cl_object
cl_logorc1(cl_object x, cl_object y)
{
538
	@(return ecl_boole(ECL_BOOLORC1, x, y))
539
}
540

541 542 543
cl_object
cl_logorc2(cl_object x, cl_object y)
{
544
	@(return ecl_boole(ECL_BOOLORC2, x, y))
545 546 547 548 549 550 551
}

static int
coerce_to_logical_operator(cl_object o)
{
	cl_fixnum op;
	op = fixint(o);
552
	if (op < 0 || op > ECL_BOOLSET)
553 554
		FEerror("~S is an invalid logical operator.", 1, o);
	return op;
555
}
556

557 558 559
cl_object
cl_boole(cl_object o, cl_object x, cl_object y)
{
560
	/* INV: log_op2() checks types */
561
	@(return ecl_boole(coerce_to_logical_operator(o), x, y))
562
}
jjgarcia's avatar
jjgarcia committed
563

564 565 566
cl_object
cl_logbitp(cl_object p, cl_object x)
{
567
	bool i;
568

jjgarcia's avatar
jjgarcia committed
569
	assert_type_integer(x);
570
	if (FIXNUMP(p)) {
571
		cl_index n = fixnnint(p);
572 573 574 575 576 577 578 579
		if (FIXNUMP(x)) {
			cl_fixnum y = fix(x);
			if (n >= FIXNUM_BITS) {
				i = (y < 0);
			} else {
				i = ((y >> n) & 1);
			}
		} else {
580
			i = mpz_tstbit(x->big.big_num, n);
581
		}
582 583 584 585 586 587 588
	} else {
		assert_type_non_negative_integer(p);
		if (FIXNUMP(x))
			i = (fix(x) < 0);
		else
			i = (big_sign(x) < 0);
	}
jjgarcia's avatar
jjgarcia committed
589
	@(return (i ? Ct : Cnil))
590
}
jjgarcia's avatar
jjgarcia committed
591

592 593 594
cl_object
cl_ash(cl_object x, cl_object y)
{
jjgarcia's avatar
jjgarcia committed
595 596
	cl_object r;
	int sign_x;
597

jjgarcia's avatar
jjgarcia committed
598 599 600
        assert_type_integer(x);
	assert_type_integer(y);
	if (FIXNUMP(y))
601
	  r = ecl_ash(x, fix(y));
jjgarcia's avatar
jjgarcia committed
602 603 604 605 606 607 608
	else {
	  /*
	    bit position represented by bignum is probably
	    out of our address space. So, result is returned
	    according to sign of integer.
	    */
	  if (FIXNUMP(x))
609 610 611
	    if (FIXNUM_MINUSP(x))
	      sign_x = -1;
	    else if (x == MAKE_FIXNUM(0))
jjgarcia's avatar
jjgarcia committed
612 613
	      sign_x = 0;
	    else
614
	      sign_x = 1;
jjgarcia's avatar
jjgarcia committed
615 616 617 618 619 620 621 622 623 624 625 626 627
	  else
	    sign_x = big_sign(x);
	  if (big_sign(y) < 0)
	    if (sign_x < 0)
	      r = MAKE_FIXNUM(-1);
	    else
	      r = MAKE_FIXNUM(0);
	  else if (sign_x == 0)
	    r = x;
	  else
	    FEerror("Insufficient memory.", 0);
	}
	@(return r)
628
}
jjgarcia's avatar
jjgarcia committed
629

630 631 632
cl_object
cl_logcount(cl_object x)
{
jjgarcia's avatar
jjgarcia committed
633
	@(return MAKE_FIXNUM(count_bits(x)))
634
}
jjgarcia's avatar
jjgarcia committed
635

636 637 638
cl_object
cl_integer_length(cl_object x)
{
639 640
	int count;
	cl_fixnum i;
641

jjgarcia's avatar
jjgarcia committed
642 643 644
	switch (type_of(x)) {
	case t_fixnum:
		i = fix(x);
645
		count = ecl_fixnum_bit_length(i);
jjgarcia's avatar
jjgarcia committed
646
		break;
647 648 649 650
	case t_bignum:
		if (mpz_sgn(x->big.big_num) < 0)
			x = cl_lognot(x);
		count = mpz_sizeinbase(x->big.big_num, 2);
jjgarcia's avatar
jjgarcia committed
651 652 653 654 655
		break;
	default:
		FEtype_error_integer(x);
	}
	@(return MAKE_FIXNUM(count))
656
}
jjgarcia's avatar
jjgarcia committed
657

658 659 660
cl_object
si_bit_array_op(cl_object o, cl_object x, cl_object y, cl_object r)
{
jjgarcia's avatar
jjgarcia committed
661 662
	cl_fixnum i, j, n, d;
	cl_object r0;
663
	bit_operator op;
jjgarcia's avatar
jjgarcia committed
664 665 666 667
	bool replace = FALSE;
	int xi, yi, ri;
	byte *xp, *yp, *rp;
	int xo, yo, ro;
668

jjgarcia's avatar
jjgarcia committed
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
	if (type_of(x) == t_bitvector) {
		d = x->vector.dim;
		xp = x->vector.self.bit;
		xo = x->vector.offset;
		if (type_of(y) != t_bitvector)
			goto ERROR;
		if (d != y->vector.dim)
			goto ERROR;
		yp = y->vector.self.bit;
		yo = y->vector.offset;
		if (r == Ct)
			r = x;
		if (r != Cnil) {
			if (type_of(r) != t_bitvector)
				goto ERROR;
			if (r->vector.dim != d)
				goto ERROR;
			i = (r->vector.self.bit - xp)*8 + (r->vector.offset - xo);
			if ((i > 0 && i < d) || (i < 0 && -i < d)) {
				r0 = r;
				r = Cnil;
				replace = TRUE;
				goto L1;
			}
			i = (r->vector.self.bit - yp)*8 + (r->vector.offset - yo);
			if ((i > 0 && i < d) || (i < 0 && -i < d)) {
				r0 = r;
				r = Cnil;
				replace = TRUE;
			}
		}
	L1:
		if (Null(r)) {
702
			r = si_make_vector(@'bit', MAKE_FIXNUM(d), Cnil, Cnil, Cnil, Cnil);
jjgarcia's avatar
jjgarcia committed
703 704 705 706
		}
	} else {
		if (type_of(x) != t_array)
			goto ERROR;
707
		if ((cl_elttype)x->array.elttype != aet_bit)
jjgarcia's avatar
jjgarcia committed
708 709 710 711 712 713
			goto ERROR;
		d = x->array.dim;
		xp = x->vector.self.bit;
		xo = x->vector.offset;
		if (type_of(y) != t_array)
			goto ERROR;
714
		if ((cl_elttype)y->array.elttype != aet_bit)
jjgarcia's avatar
jjgarcia committed
715 716 717 718 719 720 721 722 723 724 725 726 727
			goto ERROR;
		if (x->array.rank != y->array.rank)
			goto ERROR;
		yp = y->vector.self.bit;
		yo = y->vector.offset;
		for (i = 0;  i < x->array.rank;  i++)
			if (x->array.dims[i] != y->array.dims[i])
				goto ERROR;
		if (r == Ct)
			r = x;
		if (r != Cnil) {
			if (type_of(r) != t_array)
				goto ERROR;
728
			if ((cl_elttype)r->array.elttype != aet_bit)
jjgarcia's avatar
jjgarcia committed
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
				goto ERROR;
			if (r->array.rank != x->array.rank)
				goto ERROR;
			for (i = 0;  i < x->array.rank;  i++)
				if (r->array.dims[i] != x->array.dims[i])
					goto ERROR;
			i = (r->vector.self.bit - xp)*8 + (r->vector.offset - xo);
			if ((i > 0 && i < d) || (i < 0 && -i < d)) {
				r0 = r;
				r = Cnil;
				replace = TRUE;
				goto L2;
			} 
			i = (r->vector.self.bit - yp)*8 + (r->vector.offset - yo);
			if ((i > 0 && i < d) || (i < 0 && -i < d)) {
				r0 = r;
				r = Cnil;
				replace = TRUE;
			}
		}
	L2:
		if (Null(r)) {
751
		  r = cl_alloc_object(t_array);
jjgarcia's avatar
jjgarcia committed
752 753
		  r->array.self.t = NULL;
		  r->array.displaced = Cnil;
jjgarcia's avatar
jjgarcia committed
754 755 756
		  r->array.rank = x->array.rank;
		  r->array.dims = x->array.dims;
		  r->array.elttype = aet_bit;
jjgarcia's avatar
jjgarcia committed
757 758 759 760 761 762 763
		  r->array.dim = x->array.dim;
		  r->array.adjustable = FALSE;
		  array_allocself(r);
		}
	}
	rp = r->vector.self.bit;
	ro = r->vector.offset;
764
	op = fixnum_operations[coerce_to_logical_operator(o)];
jjgarcia's avatar
jjgarcia committed
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817

#define	set_high(place, nbits, value) \
	(place)=((place)&~(-0400>>(nbits)))|((value)&(-0400>>(nbits)))

#define	set_low(place, nbits, value) \
	(place)=((place)&(-0400>>(8-(nbits))))|((value)&~(-0400>>(8-(nbits))))

#define	extract_byte(integer, pointer, index, offset) \
	(integer) = (pointer)[(index)+1] & 0377; \
	(integer) = ((pointer)[index]<<(offset))|((integer)>>(8-(offset)))

#define	store_byte(pointer, index, offset, value) \
	set_low((pointer)[index], 8-(offset), (value)>>(offset)); \
	set_high((pointer)[(index)+1], offset, (value)<<(8-(offset)))

	if (xo == 0 && yo == 0 && ro == 0) {
		for (n = d/8, i = 0;  i < n;  i++)
			rp[i] = (*op)(xp[i], yp[i]);
		if ((j = d%8) > 0)
			set_high(rp[n], j, (*op)(xp[n], yp[n]));
		if (!replace)
			@(return r)
	} else {
		for (n = d/8, i = 0;  i <= n;  i++) {
			extract_byte(xi, xp, i, xo);
			extract_byte(yi, yp, i, yo);
			if (i == n) {
				if ((j = d%8) == 0)
					break;
				extract_byte(ri, rp, n, ro);
				set_high(ri, j, (*op)(xi, yi));
			} else
				ri = (*op)(xi, yi);
			store_byte(rp, i, ro, ri);
		}
		if (!replace)
			@(return r)
	}
	rp = r0->vector.self.bit;
	ro = r0->vector.offset;
	for (n = d/8, i = 0;  i <= n;  i++) {
		if (i == n) {
			if ((j = d%8) == 0)
				break;
			extract_byte(ri, rp, n, ro);
			set_high(ri, j, r->vector.self.bit[n]);
		} else
			ri = r->vector.self.bit[i];
		store_byte(rp, i, ro, ri);
	}
	@(return r0)
ERROR:
	FEerror("Illegal arguments for bit-array operation.", 0);
818
}