num_log.d 14.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 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

#define BOOLCLR		0
#define BOOLSET		017
#define BOOL1		03
#define BOOL2		05
#define BOOLC1		014
#define BOOLC2		012
#define BOOLAND		01
#define BOOLIOR		07
#define BOOLXOR		06
#define BOOLEQV		011
#define BOOLNAND	016
#define BOOLNOR		010
#define BOOLANDC1	04
#define BOOLANDC2	02
#define BOOLORC1	015
#define BOOLORC2	013

/*
	x : fixnum or bignum (may be not normalized)
	y : integer
   returns
	fixnum or bignum ( not normalized )
*/

43 44 45
typedef cl_fixnum (*bit_operator)(cl_fixnum, cl_fixnum);

static cl_object big_log_op(cl_object x, cl_object y, bit_operator op);
jjgarcia's avatar
jjgarcia committed
46 47

static cl_object
48
log_op(int narg, bit_operator op, cl_va_list ARGS)
jjgarcia's avatar
jjgarcia committed
49
{
50
	cl_type t;
jjgarcia's avatar
jjgarcia committed
51 52 53
	cl_object x, numi;
	int i = 1, j;

54 55
	if (narg < 2) FEtoo_few_arguments(narg);
	x = cl_va_arg(ARGS);
jjgarcia's avatar
jjgarcia committed
56 57 58 59 60 61 62 63 64
	t = type_of(x);
	if (t == t_bignum) {
		x = big_copy(x);	/* since big_log_op clobbers it */
		goto BIG_OP;
	} if (t != t_fixnum) {
		FEtype_error_integer(x);
	}
	j = fix(x);
	for (; i < narg; i++) {
65
	  numi = cl_va_arg(ARGS);
jjgarcia's avatar
jjgarcia committed
66 67
	  t = type_of(numi);
	  if (t == t_bignum) {
68
	    x = big_log_op(bignum1(j), numi, op);
jjgarcia's avatar
jjgarcia committed
69 70 71 72 73 74 75 76 77 78 79
	    i++;
	    goto BIG_OP;
	  } else if (t != t_fixnum) {
	    FEtype_error_integer(numi);
	  }
	  j = (*op)(j, fix(numi));
	}
	return(MAKE_FIXNUM(j));

BIG_OP:
	for (; i < narg; i++)	  
80
	  x = big_log_op(x, cl_va_arg(ARGS), op);
jjgarcia's avatar
jjgarcia committed
81 82
	return(big_normalize(x));
}
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

static cl_object
log_op2(cl_object x, cl_object y, bit_operator op)
{
	switch (type_of(x)) {
	case t_fixnum:
		if (FIXNUMP(y))
			return MAKE_FIXNUM((*op)(fix(x),fix(y)));
		else
			x = big_log_op(bignum1(fix(x)), y, op);
		break;
	case t_bignum:
		x = big_log_op(big_copy(x), y, op);
		break;
	default:
		FEtype_error_integer(x);
	}
	return big_normalize(x);
}

jjgarcia's avatar
jjgarcia committed
103 104 105 106 107
/*
	big_log_op(x, y, op) performs the logical operation op on
	bignum x and integer y, and returns the result in x destructively.
*/
static cl_object
108
big_log_op(cl_object x, cl_object y, bit_operator op)
jjgarcia's avatar
jjgarcia committed
109
{
110 111 112
	int flag;
	int y_size, x_size;
	mp_limb_t word, *x_limbs, *y_limbs;
jjgarcia's avatar
jjgarcia committed
113 114

	if (FIXNUMP(y)) {
115 116 117
		cl_object z = big_register1_get();
		mpz_set_si(y->big.big_num, fix(y));
		y = z;
jjgarcia's avatar
jjgarcia committed
118
	} else if (type_of(y) != t_bignum) {
119
		FEtype_error_integer(y);
jjgarcia's avatar
jjgarcia committed
120
	}
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
	if (big_sign(y) > 0)
		flag = 0;
	else {
		cl_object z = big_register2_get();
		mpz_com(z->big.big_num, y->big.big_num);
		y = z;
		flag = ~0;
	}
	y_size = y->big.big_size;
	y_limbs = y->big.big_limbs;
	if (big_sign(x) > 0)
		flag = (*op)(0,flag);
	else {
		flag = (*op)(0,flag);
		mpz_com(x->big.big_num, x->big.big_num);
	}
	x_size = x->big.big_size;
	if (y_size > x_size) {
		x->big.big_size = x_size = y_size;
		mpz_realloc(x->big.big_num, x_size);
	}
	x_limbs = x->big.big_limbs;

	/* Compute the logical operation */
	for (word = 0; x_size--; ) {
		mp_limb_t aux = (*op)(x_limbs[x_size], y_limbs[x_size]);
		x_limbs[x_size] = aux;
		word |= aux;
	}
	/* When output is zero, notice that */
	if (word == 0)
		x->big.big_size = 0;
	/* If result should be a negative number, perform two's complement. */
	if (flag)
		mpz_com(x->big.big_num, x->big.big_num);
	return x;
jjgarcia's avatar
jjgarcia committed
157 158
}

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

165 166
static cl_fixnum
xor_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
167 168 169 170
{
	return(i ^ j);
}

171 172
static cl_fixnum
and_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
173 174 175 176
{
	return(i & j);
}

177 178
static cl_fixnum
eqv_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
179 180 181 182
{
	return(~(i ^ j));
}

183 184
static cl_fixnum
nand_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
185 186 187 188
{
	return(~(i & j));
}

189 190
static cl_fixnum
nor_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
191 192 193 194
{
	return(~(i | j));
}

195 196
static cl_fixnum
andc1_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
197 198 199 200
{
	return((~i) & j);
}

201 202
static cl_fixnum
andc2_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
203 204 205 206
{
	return(i & (~j));
}

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

213 214
static cl_fixnum
orc2_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
215 216 217 218
{
	return(i | (~j));
}

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

225 226
static cl_fixnum
b_set_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
227 228 229 230
{
	return(-1);
}

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

237 238
static cl_fixnum
b_2_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
239 240 241 242
{
	return(j);
}

243 244
static cl_fixnum
b_c1_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
245 246 247 248
{
	return(~i);
}

249 250
static cl_fixnum
b_c2_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
251 252 253 254 255 256
{
	return(~j);
}

@(defun lognot (x)
@
257
	return @logxor(2,x,MAKE_FIXNUM(-1));
jjgarcia's avatar
jjgarcia committed
258 259
@)

260
static cl_fixnum
jjgarcia's avatar
jjgarcia committed
261 262 263 264 265 266 267 268 269 270 271 272 273
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:
274 275 276 277 278 279 280
		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
281 282 283 284 285 286 287 288 289 290 291 292
		}
		break;
	default:
		FEtype_error_integer(x);
	}
	return count;
}

/*
   Left shift if w > 0, right shift if w < 0.
 */
cl_object
293
integer_shift(cl_object x, cl_fixnum w)
jjgarcia's avatar
jjgarcia committed
294 295
{
	cl_object y;
296 297 298

	if (w == 0)
		return(x);
jjgarcia's avatar
jjgarcia committed
299 300
	y = big_register0_get();
	if (w < 0) {
301 302 303
		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
304
	} else {
305 306 307 308 309
		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
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
	}
	return(big_register_normalize(y));
}

int
int_bit_length(int i)
{
	register int	count, j;

	count = 0;
	for (j = 0; j < 31 ; j++)
		if (((i >> j) & 1) == 1) count = j + 1;
	return(count);
}

@(defun logior (&rest nums)
@
	if (narg == 0)
		@(return MAKE_FIXNUM(0))
329
	/* INV: log_op() checks types and outputs first argument as default. */
330
	@(return log_op(narg, ior_op, nums))
jjgarcia's avatar
jjgarcia committed
331 332 333 334 335 336
@)

@(defun logxor (&rest nums)
@
	if (narg == 0)
		@(return MAKE_FIXNUM(0))
337
	/* INV: log_op() checks types and outputs first argument as default. */
338
	@(return log_op(narg, xor_op, nums))
jjgarcia's avatar
jjgarcia committed
339 340 341 342 343 344
@)

@(defun logand (&rest nums)
@
	if (narg == 0)
		@(return MAKE_FIXNUM(-1))
345
	/* INV: log_op() checks types and outputs first argument as default. */
346
	@(return log_op(narg, and_op, nums))
jjgarcia's avatar
jjgarcia committed
347 348 349 350 351 352
@)

@(defun logeqv (&rest nums)
@
	if (narg == 0)
		@(return MAKE_FIXNUM(-1))
353
	/* INV: log_op() checks types and outputs first argument as default. */
354
	@(return log_op(narg, eqv_op, nums))
jjgarcia's avatar
jjgarcia committed
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
@(defun lognand (x y)
@
	@(return log_op2(x, y, nand_op))
@)

@(defun lognor (x y)
@
	@(return log_op2(x, y, nor_op))
@)

@(defun logandc1 (x y)
@
	@(return log_op2(x, y, andc1_op))
@)

@(defun logandc2 (x y)
@
	@(return log_op2(x, y, andc2_op))
@)

@(defun logorc1 (x y)
@
	@(return log_op2(x, y, orc1_op))
@)

@(defun logorc2 (x y)
@
	@(return log_op2(x, y, orc2_op))
@)

@(defun boole (o x y)
388
	bit_operator op;
jjgarcia's avatar
jjgarcia committed
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
@
	/* INV: log_op() checks types */
	switch(fixint(o)) {
		case BOOLCLR:	op = b_clr_op;	break;
		case BOOLSET:	op = b_set_op;	break;
		case BOOL1:	op = b_1_op;	break;
		case BOOL2:	op = b_2_op;	break;
		case BOOLC1:	op = b_c1_op;	break;
		case BOOLC2:	op = b_c2_op;	break;
		case BOOLAND:	op = and_op;	break;
		case BOOLIOR:	op = ior_op;	break;
		case BOOLXOR:	op = xor_op;	break;
		case BOOLEQV:	op = eqv_op;	break;
		case BOOLNAND:	op = nand_op;	break;
		case BOOLNOR:	op = nor_op;	break;
		case BOOLANDC1:	op = andc1_op;	break;
		case BOOLANDC2:	op = andc2_op;	break;
		case BOOLORC1:	op = orc1_op;	break;
		case BOOLORC2:	op = orc2_op;	break;
		default:
			FEerror("~S is an invalid logical operator.",
				1, o);
	}
412
	@(return log_op2(x, y, op))
jjgarcia's avatar
jjgarcia committed
413 414 415 416
@)

@(defun logbitp (p x)
	bool	i;
417
	int	n;
jjgarcia's avatar
jjgarcia committed
418 419
@
	assert_type_integer(x);
420 421 422 423
	if (FIXNUMP(p)) {
		cl_fixnum n = fixnnint(p);
		if (n < 0)
			FEtype_error_index(p);
jjgarcia's avatar
jjgarcia committed
424
		if (FIXNUMP(x))
425
			i = ((fix(x) >> n) & 1);
jjgarcia's avatar
jjgarcia committed
426
		else
427 428 429 430 431 432 433 434
			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
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
	@(return (i ? Ct : Cnil))
@)

@(defun ash (x y)
	cl_object r;
	int sign_x;
@
        assert_type_integer(x);
	assert_type_integer(y);
	if (FIXNUMP(y))
	  r = integer_shift(x, fix(y));
	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))
453 454 455
	    if (FIXNUM_MINUSP(x))
	      sign_x = -1;
	    else if (x == MAKE_FIXNUM(0))
jjgarcia's avatar
jjgarcia committed
456 457
	      sign_x = 0;
	    else
458
	      sign_x = 1;
jjgarcia's avatar
jjgarcia committed
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
	  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)
@)

@(defun logcount (x)
@
	@(return MAKE_FIXNUM(count_bits(x)))
@)

@(defun integer_length (x)
	int	count, i;
@
	switch (type_of(x)) {
	case t_fixnum:
		i = fix(x);
		count = int_bit_length((i < 0) ? ~i : i);
		break;
	case t_bignum: {
	  	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))
@)

void
init_num_log(void)
{
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
	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
518 519 520 521 522
}

@(defun si::bit_array_op (o x y r)
	cl_fixnum i, j, n, d;
	cl_object r0;
523
	bit_operator op;
jjgarcia's avatar
jjgarcia committed
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
	bool replace = FALSE;
	int xi, yi, ri;
	byte *xp, *yp, *rp;
	int xo, yo, ro;
@
	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)) {
562
			r = @si::make-vector(6, @'bit', MAKE_FIXNUM(d), Cnil, Cnil, Cnil, Cnil);
jjgarcia's avatar
jjgarcia committed
563 564 565 566
		}
	} else {
		if (type_of(x) != t_array)
			goto ERROR;
567
		if ((cl_elttype)x->array.elttype != aet_bit)
jjgarcia's avatar
jjgarcia committed
568 569 570 571 572 573
			goto ERROR;
		d = x->array.dim;
		xp = x->vector.self.bit;
		xo = x->vector.offset;
		if (type_of(y) != t_array)
			goto ERROR;
574
		if ((cl_elttype)y->array.elttype != aet_bit)
jjgarcia's avatar
jjgarcia committed
575 576 577 578 579 580 581 582 583 584 585 586 587
			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;
588
			if ((cl_elttype)r->array.elttype != aet_bit)
jjgarcia's avatar
jjgarcia committed
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
				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)) {
611
		  r = cl_alloc_object(t_array);
jjgarcia's avatar
jjgarcia committed
612 613 614 615
		  r->array.self.t = NULL;
		  r->array.displaced = Cnil;
		  r->array.rank = 1;
		  r->array.dims = NULL;
616
		  r->array.elttype = get_elttype(@'bit');
617
		  r->array.dims = (cl_index *)cl_alloc_atomic_align(x->array.dim * sizeof(cl_index), sizeof(cl_index));
jjgarcia's avatar
jjgarcia committed
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 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
		  r->array.dim = x->array.dim;
		  r->array.adjustable = FALSE;
		  array_allocself(r);
		}
	}
	rp = r->vector.self.bit;
	ro = r->vector.offset;
	switch(fixint(o)) {
		case BOOLCLR:	op = b_clr_op;	break;
		case BOOLSET:	op = b_set_op;	break;
		case BOOL1:	op = b_1_op;	break;
		case BOOL2:	op = b_2_op;	break;
		case BOOLC1:	op = b_c1_op;	break;
		case BOOLC2:	op = b_c2_op;	break;
		case BOOLAND:	op = and_op;	break;
		case BOOLIOR:	op = ior_op;	break;
		case BOOLXOR:	op = xor_op;	break;
		case BOOLEQV:	op = eqv_op;	break;
		case BOOLNAND:	op = nand_op;	break;
		case BOOLNOR:	op = nor_op;	break;
		case BOOLANDC1:	op = andc1_op;	break;
		case BOOLANDC2:	op = andc2_op;	break;
		case BOOLORC1:	op = orc1_op;	break;
		case BOOLORC2:	op = orc2_op;	break;
		default:
			FEerror("~S is an invalid logical operator.", 1, o);
	}

#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);
@)