num_log.d 15.9 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>
jjgarcia's avatar
jjgarcia committed
19 20

#define BOOLCLR		0
21 22
#define BOOLAND		01
#define BOOLANDC2	02
jjgarcia's avatar
jjgarcia committed
23
#define BOOL1		03
24
#define BOOLANDC1	04
jjgarcia's avatar
jjgarcia committed
25 26
#define BOOL2		05
#define BOOLXOR		06
27
#define BOOLIOR		07
jjgarcia's avatar
jjgarcia committed
28
#define BOOLNOR		010
29 30
#define BOOLEQV		011
#define BOOLC2		012
jjgarcia's avatar
jjgarcia committed
31
#define BOOLORC2	013
32 33 34 35
#define BOOLC1		014
#define BOOLORC1	015
#define BOOLNAND	016
#define BOOLSET		017
jjgarcia's avatar
jjgarcia committed
36 37

/*
38 39
 * BIT OPERATIONS FOR FIXNUMS
 */
jjgarcia's avatar
jjgarcia committed
40

41 42
static cl_fixnum
ior_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
43
{
44
	return(i | j);
jjgarcia's avatar
jjgarcia committed
45
}
46

47 48
static void
mpz_ior_op(cl_object i, cl_object j)
49
{
50
	mpz_ior(i->big.big_num, i->big.big_num, j->big.big_num);
jjgarcia's avatar
jjgarcia committed
51 52
}

53
static cl_fixnum
54
xor_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
55
{
56
	return(i ^ j);
jjgarcia's avatar
jjgarcia committed
57 58
}

59 60
static void
mpz_xor_op(cl_object i, cl_object j)
jjgarcia's avatar
jjgarcia committed
61
{
62
	mpz_xor(i->big.big_num, i->big.big_num, j->big.big_num);
jjgarcia's avatar
jjgarcia committed
63 64
}

65 66
static cl_fixnum
and_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
67 68 69 70
{
	return(i & j);
}

71 72 73 74 75 76
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);
}

77 78
static cl_fixnum
eqv_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
79 80 81 82
{
	return(~(i ^ j));
}

83 84 85 86 87 88 89
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);
}

90 91
static cl_fixnum
nand_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
92 93 94 95
{
	return(~(i & j));
}

96 97 98 99 100 101 102
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);
}

103 104
static cl_fixnum
nor_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
105 106 107 108
{
	return(~(i | j));
}

109 110 111 112 113 114 115
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);
}

116 117
static cl_fixnum
andc1_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
118 119 120 121
{
	return((~i) & j);
}

122 123 124 125 126 127 128
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);
}

129 130
static cl_fixnum
andc2_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 142 143 144
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);
}

145 146
static cl_fixnum
orc1_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
147 148 149 150
{
	return((~i) | j);
}

151 152 153 154 155 156 157
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);
}

158 159
static cl_fixnum
orc2_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
160 161 162 163
{
	return(i | (~j));
}

164 165 166 167 168 169 170 171
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);
}

172 173
static cl_fixnum
b_clr_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
174 175 176 177
{
	return(0);
}

178 179 180 181 182 183
static void
mpz_b_clr_op(cl_object i, cl_object j)
{
	mpz_set_si(i->big.big_num, 0);
}

184 185
static cl_fixnum
b_set_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
186 187 188 189
{
	return(-1);
}

190 191 192 193 194 195
static void
mpz_b_set_op(cl_object i, cl_object j)
{
	mpz_set_si(i->big.big_num, -1);
}

196 197
static cl_fixnum
b_1_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
198 199 200 201
{
	return(i);
}

202 203 204 205 206
static void
mpz_b_1_op(cl_object i, cl_object j)
{
}

207 208
static cl_fixnum
b_2_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
209 210 211 212
{
	return(j);
}

213 214 215 216 217 218
static void
mpz_b_2_op(cl_object i, cl_object j)
{
	mpz_set(i->big.big_num, j->big.big_num);
}

219 220
static cl_fixnum
b_c1_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
221 222 223 224
{
	return(~i);
}

225 226 227 228 229 230
static void
mpz_b_c1_op(cl_object i, cl_object j)
{
	mpz_com(i->big.big_num, i->big.big_num);
}

231 232
static cl_fixnum
b_c2_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
233 234 235 236
{
	return(~j);
}

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

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

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

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 409 410 411 412
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)
		  if (j & 1) count++;
		break;
	}
	case t_bignum:
413 414 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);
			count = mpz_popcount(x->big.big_num);
			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 441 442
		if (FIXNUMP(x)) 
			return MAKE_FIXNUM(fix(x) >> -w);
		mpz_div_2exp(y->big.big_num, x->big.big_num, -w);
jjgarcia's avatar
jjgarcia committed
443
	} else {
444 445 446 447 448
		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
449 450 451 452
	}
	return(big_register_normalize(y));
}

453
static int
454
int_bit_length(cl_fixnum i)
jjgarcia's avatar
jjgarcia committed
455
{
456
	register int count, j;
jjgarcia's avatar
jjgarcia committed
457 458

	count = 0;
jjgarcia's avatar
jjgarcia committed
459
	for (j = 0; j < (sizeof(cl_index)/sizeof(uint8_t))*8-1; j++)
jjgarcia's avatar
jjgarcia committed
460 461 462 463 464 465 466 467
		if (((i >> j) & 1) == 1) count = j + 1;
	return(count);
}

@(defun logior (&rest nums)
@
	if (narg == 0)
		@(return MAKE_FIXNUM(0))
468
	/* INV: log_op() checks types and outputs first argument as default. */
469
	@(return log_op(narg, BOOLIOR, nums))
jjgarcia's avatar
jjgarcia committed
470 471 472 473 474 475
@)

@(defun logxor (&rest nums)
@
	if (narg == 0)
		@(return MAKE_FIXNUM(0))
476
	/* INV: log_op() checks types and outputs first argument as default. */
477
	@(return log_op(narg, BOOLXOR, nums))
jjgarcia's avatar
jjgarcia committed
478 479 480 481 482 483
@)

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

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

496 497 498
cl_object
cl_lognand(cl_object x, cl_object y)
{
499
	@(return log_op2(x, y, BOOLNAND))
500
}
501

502 503 504
cl_object
cl_lognor(cl_object x, cl_object y)
{
505
	@(return log_op2(x, y, BOOLNOR))
506
}
507

508 509 510
cl_object
cl_logandc1(cl_object x, cl_object y)
{
511
	@(return log_op2(x, y, BOOLANDC1))
512
}
513

514 515 516
cl_object
cl_logandc2(cl_object x, cl_object y)
{
517
	@(return log_op2(x, y, BOOLANDC2))
518
}
519

520 521 522
cl_object
cl_logorc1(cl_object x, cl_object y)
{
523
	@(return log_op2(x, y, BOOLORC1))
524
}
525

526 527 528
cl_object
cl_logorc2(cl_object x, cl_object y)
{
529 530 531 532 533 534 535 536 537 538 539
	@(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;
540
}
541

542 543 544
cl_object
cl_boole(cl_object o, cl_object x, cl_object y)
{
545 546
	/* INV: log_op2() checks types */
	@(return log_op2(x, y, coerce_to_logical_operator(o)))
547
}
jjgarcia's avatar
jjgarcia committed
548

549 550 551
cl_object
cl_logbitp(cl_object p, cl_object x)
{
552
	bool i;
553

jjgarcia's avatar
jjgarcia committed
554
	assert_type_integer(x);
555 556
	if (FIXNUMP(p)) {
		cl_fixnum n = fixnnint(p);
jjgarcia's avatar
jjgarcia committed
557
		if (FIXNUMP(x))
558
			i = ((fix(x) >> n) & 1);
jjgarcia's avatar
jjgarcia committed
559
		else
560 561 562 563 564 565 566 567
			i = mpz_tstbit(x->big.big_num, n);
	} 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
568
	@(return (i ? Ct : Cnil))
569
}
jjgarcia's avatar
jjgarcia committed
570

571 572 573
cl_object
cl_ash(cl_object x, cl_object y)
{
jjgarcia's avatar
jjgarcia committed
574 575
	cl_object r;
	int sign_x;
576

jjgarcia's avatar
jjgarcia committed
577 578 579
        assert_type_integer(x);
	assert_type_integer(y);
	if (FIXNUMP(y))
580
	  r = ecl_ash(x, fix(y));
jjgarcia's avatar
jjgarcia committed
581 582 583 584 585 586 587
	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))
588 589 590
	    if (FIXNUM_MINUSP(x))
	      sign_x = -1;
	    else if (x == MAKE_FIXNUM(0))
jjgarcia's avatar
jjgarcia committed
591 592
	      sign_x = 0;
	    else
593
	      sign_x = 1;
jjgarcia's avatar
jjgarcia committed
594 595 596 597 598 599 600 601 602 603 604 605 606
	  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)
607
}
jjgarcia's avatar
jjgarcia committed
608

609 610 611
cl_object
cl_logcount(cl_object x)
{
jjgarcia's avatar
jjgarcia committed
612
	@(return MAKE_FIXNUM(count_bits(x)))
613
}
jjgarcia's avatar
jjgarcia committed
614

615 616 617
cl_object
cl_integer_length(cl_object x)
{
618 619
	int count;
	cl_fixnum i;
620

jjgarcia's avatar
jjgarcia committed
621 622 623 624 625 626
	switch (type_of(x)) {
	case t_fixnum:
		i = fix(x);
		count = int_bit_length((i < 0) ? ~i : i);
		break;
	case t_bignum: {
627
		/* FIXME! We should not be using internals of GMP here */
jjgarcia's avatar
jjgarcia committed
628 629 630 631 632 633 634 635 636
	  	int last = abs(x->big.big_size) - 1;
		i = x->big.big_limbs[last];
		count = last * (sizeof(mp_limb_t) * 8) + int_bit_length(i);
		break;
	}
	default:
		FEtype_error_integer(x);
	}
	@(return MAKE_FIXNUM(count))
637
}
jjgarcia's avatar
jjgarcia committed
638 639 640 641

void
init_num_log(void)
{
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
	SYM_VAL(@'BOOLE-CLR') = MAKE_FIXNUM(BOOLCLR);
	SYM_VAL(@'BOOLE-SET') = MAKE_FIXNUM(BOOLSET);
	SYM_VAL(@'BOOLE-1') = MAKE_FIXNUM(BOOL1);
	SYM_VAL(@'BOOLE-2') = MAKE_FIXNUM(BOOL2);
	SYM_VAL(@'BOOLE-C1') = MAKE_FIXNUM(BOOLC1);
	SYM_VAL(@'BOOLE-C2') = MAKE_FIXNUM(BOOLC2);
	SYM_VAL(@'BOOLE-AND') = MAKE_FIXNUM(BOOLAND);
	SYM_VAL(@'BOOLE-IOR') = MAKE_FIXNUM(BOOLIOR);
	SYM_VAL(@'BOOLE-XOR') = MAKE_FIXNUM(BOOLXOR);
	SYM_VAL(@'BOOLE-EQV') = MAKE_FIXNUM(BOOLEQV);
	SYM_VAL(@'BOOLE-NAND') = MAKE_FIXNUM(BOOLNAND);
	SYM_VAL(@'BOOLE-NOR') = MAKE_FIXNUM(BOOLNOR);
	SYM_VAL(@'BOOLE-ANDC1') = MAKE_FIXNUM(BOOLANDC1);
	SYM_VAL(@'BOOLE-ANDC2') = MAKE_FIXNUM(BOOLANDC2);
	SYM_VAL(@'BOOLE-ORC1') = MAKE_FIXNUM(BOOLORC1);
	SYM_VAL(@'BOOLE-ORC2') = MAKE_FIXNUM(BOOLORC2);
jjgarcia's avatar
jjgarcia committed
658 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
}