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
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};


267 268
static cl_object log_op2(cl_object x, cl_object y, int op);

269 270 271
static cl_object
log_op(int narg, int op, cl_va_list ARGS)
{
272 273 274 275 276 277 278 279 280 281 282 283 284 285
#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);
			x = log_op2(x, y, op);
		} while (--narg);
	}
	return x;
#else
286 287 288
	cl_object x, numi;
	bit_operator fix_log_op;
	bignum_bit_operator big_log_op;
289 290
	int i = 1;
	cl_fixnum j;
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 342 343

	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));
344
#endif
345 346 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 393 394
}

static cl_object
log_op2(cl_object x, cl_object y, int op)
{
	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);
}

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

401
static cl_fixnum
jjgarcia's avatar
jjgarcia committed
402 403 404 405 406 407 408 409 410
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)
411
			if (j & 1) count++;
jjgarcia's avatar
jjgarcia committed
412 413 414
		break;
	}
	case t_bignum:
415 416 417 418 419
		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);
420
			count = mpz_popcount(z->big.big_num);
421
			big_register_free(z);
jjgarcia's avatar
jjgarcia committed
422 423 424 425 426 427 428 429 430 431 432 433
		}
		break;
	default:
		FEtype_error_integer(x);
	}
	return count;
}

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

	if (w == 0)
		return(x);
jjgarcia's avatar
jjgarcia committed
440 441
	y = big_register0_get();
	if (w < 0) {
442
		cl_index bits = -w;
443
		if (FIXNUMP(x)) {
444 445 446 447 448 449 450
			/* 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 :-/
			 */
451
			cl_fixnum y = fix(x);
452 453 454 455 456
			if (bits >= FIXNUM_BITS) {
				y = (y < 0)? -1 : 0;
			} else {
				y >>= bits;
			}
457 458
			return MAKE_FIXNUM(y);
		}
459
		mpz_div_2exp(y->big.big_num, x->big.big_num, bits);
jjgarcia's avatar
jjgarcia committed
460
	} else {
461 462 463 464 465
		if (FIXNUMP(x)) {
			mpz_set_si(y->big.big_num, fix(x));
			x = y;
		}
		mpz_mul_2exp(y->big.big_num, x->big.big_num, w);
jjgarcia's avatar
jjgarcia committed
466 467 468 469
	}
	return(big_register_normalize(y));
}

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

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

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

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

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

513 514 515
cl_object
cl_lognand(cl_object x, cl_object y)
{
516
	@(return log_op2(x, y, BOOLNAND))
517
}
518

519 520 521
cl_object
cl_lognor(cl_object x, cl_object y)
{
522
	@(return log_op2(x, y, BOOLNOR))
523
}
524

525 526 527
cl_object
cl_logandc1(cl_object x, cl_object y)
{
528
	@(return log_op2(x, y, BOOLANDC1))
529
}
530

531 532 533
cl_object
cl_logandc2(cl_object x, cl_object y)
{
534
	@(return log_op2(x, y, BOOLANDC2))
535
}
536

537 538 539
cl_object
cl_logorc1(cl_object x, cl_object y)
{
540
	@(return log_op2(x, y, BOOLORC1))
541
}
542

543 544 545
cl_object
cl_logorc2(cl_object x, cl_object y)
{
546 547 548 549 550 551 552 553 554 555 556
	@(return log_op2(x, y, BOOLORC2))
}

static int
coerce_to_logical_operator(cl_object o)
{
	cl_fixnum op;
	op = fixint(o);
	if (op < 0 || op > BOOLSET)
		FEerror("~S is an invalid logical operator.", 1, o);
	return op;
557
}
558

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

566 567 568
cl_object
cl_logbitp(cl_object p, cl_object x)
{
569
	bool i;
570

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

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

jjgarcia's avatar
jjgarcia committed
600 601 602
        assert_type_integer(x);
	assert_type_integer(y);
	if (FIXNUMP(y))
603
	  r = ecl_ash(x, fix(y));
jjgarcia's avatar
jjgarcia committed
604 605 606 607 608 609 610
	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))
611 612 613
	    if (FIXNUM_MINUSP(x))
	      sign_x = -1;
	    else if (x == MAKE_FIXNUM(0))
jjgarcia's avatar
jjgarcia committed
614 615
	      sign_x = 0;
	    else
616
	      sign_x = 1;
jjgarcia's avatar
jjgarcia committed
617 618 619 620 621 622 623 624 625 626 627 628 629
	  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)
630
}
jjgarcia's avatar
jjgarcia committed
631

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

638 639 640
cl_object
cl_integer_length(cl_object x)
{
641 642
	int count;
	cl_fixnum i;
643

jjgarcia's avatar
jjgarcia committed
644 645 646
	switch (type_of(x)) {
	case t_fixnum:
		i = fix(x);
647
		count = int_bit_length(i);
jjgarcia's avatar
jjgarcia committed
648
		break;
649 650 651 652
	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
653 654 655 656 657
		break;
	default:
		FEtype_error_integer(x);
	}
	@(return MAKE_FIXNUM(count))
658
}
jjgarcia's avatar
jjgarcia committed
659

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

jjgarcia's avatar
jjgarcia committed
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 702 703
	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)) {
704
			r = si_make_vector(@'bit', MAKE_FIXNUM(d), Cnil, Cnil, Cnil, Cnil);
jjgarcia's avatar
jjgarcia committed
705 706 707 708
		}
	} else {
		if (type_of(x) != t_array)
			goto ERROR;
709
		if ((cl_elttype)x->array.elttype != aet_bit)
jjgarcia's avatar
jjgarcia committed
710 711 712 713 714 715
			goto ERROR;
		d = x->array.dim;
		xp = x->vector.self.bit;
		xo = x->vector.offset;
		if (type_of(y) != t_array)
			goto ERROR;
716
		if ((cl_elttype)y->array.elttype != aet_bit)
jjgarcia's avatar
jjgarcia committed
717 718 719 720 721 722 723 724 725 726 727 728 729
			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;
730
			if ((cl_elttype)r->array.elttype != aet_bit)
jjgarcia's avatar
jjgarcia committed
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
				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)) {
753
		  r = cl_alloc_object(t_array);
jjgarcia's avatar
jjgarcia committed
754 755
		  r->array.self.t = NULL;
		  r->array.displaced = Cnil;
jjgarcia's avatar
jjgarcia committed
756 757 758
		  r->array.rank = x->array.rank;
		  r->array.dims = x->array.dims;
		  r->array.elttype = aet_bit;
jjgarcia's avatar
jjgarcia committed
759 760 761 762 763 764 765
		  r->array.dim = x->array.dim;
		  r->array.adjustable = FALSE;
		  array_allocself(r);
		}
	}
	rp = r->vector.self.bit;
	ro = r->vector.offset;
766
	op = fixnum_operations[coerce_to_logical_operator(o)];
jjgarcia's avatar
jjgarcia committed
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 818 819

#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);
820
}