num_log.d 14.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 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
	if (narg < 2) FEwrong_num_arguments_anonym();
55
	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
{
	return(~j);
}

255 256 257
cl_object
cl_lognot(cl_object x)
{
258
	return @logxor(2,x,MAKE_FIXNUM(-1));
259
}
jjgarcia's avatar
jjgarcia committed
260

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

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

	if (w == 0)
		return(x);
jjgarcia's avatar
jjgarcia committed
300 301
	y = big_register0_get();
	if (w < 0) {
302 303 304
		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
305
	} else {
306 307 308 309 310
		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
311 312 313 314
	}
	return(big_register_normalize(y));
}

315
static int
jjgarcia's avatar
jjgarcia committed
316 317
int_bit_length(int i)
{
318
	register int count, j;
jjgarcia's avatar
jjgarcia committed
319 320

	count = 0;
321
	for (j = 0; j < (sizeof(cl_index)/sizeof(u_int8_t))*8-1; j++)
jjgarcia's avatar
jjgarcia committed
322 323 324 325 326 327 328 329
		if (((i >> j) & 1) == 1) count = j + 1;
	return(count);
}

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

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

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

@(defun logeqv (&rest nums)
@
	if (narg == 0)
		@(return MAKE_FIXNUM(-1))
354
	/* INV: log_op() checks types and outputs first argument as default. */
355
	@(return log_op(narg, eqv_op, nums))
jjgarcia's avatar
jjgarcia committed
356 357
@)

358 359 360
cl_object
cl_lognand(cl_object x, cl_object y)
{
361
	@(return log_op2(x, y, nand_op))
362
}
363

364 365 366
cl_object
cl_lognor(cl_object x, cl_object y)
{
367
	@(return log_op2(x, y, nor_op))
368
}
369

370 371 372
cl_object
cl_logandc1(cl_object x, cl_object y)
{
373
	@(return log_op2(x, y, andc1_op))
374
}
375

376 377 378
cl_object
cl_logandc2(cl_object x, cl_object y)
{
379
	@(return log_op2(x, y, andc2_op))
380
}
381

382 383 384
cl_object
cl_logorc1(cl_object x, cl_object y)
{
385
	@(return log_op2(x, y, orc1_op))
386
}
387

388 389 390
cl_object
cl_logorc2(cl_object x, cl_object y)
{
391
	@(return log_op2(x, y, orc2_op))
392
}
393

394 395 396
cl_object
cl_boole(cl_object o, cl_object x, cl_object y)
{
397
	bit_operator op;
398

jjgarcia's avatar
jjgarcia committed
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
	/* 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);
	}
421
	@(return log_op2(x, y, op))
422
}
jjgarcia's avatar
jjgarcia committed
423

424 425 426
cl_object
cl_logbitp(cl_object p, cl_object x)
{
jjgarcia's avatar
jjgarcia committed
427
	bool	i;
428
	int	n;
429

jjgarcia's avatar
jjgarcia committed
430
	assert_type_integer(x);
431 432 433 434
	if (FIXNUMP(p)) {
		cl_fixnum n = fixnnint(p);
		if (n < 0)
			FEtype_error_index(p);
jjgarcia's avatar
jjgarcia committed
435
		if (FIXNUMP(x))
436
			i = ((fix(x) >> n) & 1);
jjgarcia's avatar
jjgarcia committed
437
		else
438 439 440 441 442 443 444 445
			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
446
	@(return (i ? Ct : Cnil))
447
}
jjgarcia's avatar
jjgarcia committed
448

449 450 451
cl_object
cl_ash(cl_object x, cl_object y)
{
jjgarcia's avatar
jjgarcia committed
452 453
	cl_object r;
	int sign_x;
454

jjgarcia's avatar
jjgarcia committed
455 456 457
        assert_type_integer(x);
	assert_type_integer(y);
	if (FIXNUMP(y))
458
	  r = ecl_ash(x, fix(y));
jjgarcia's avatar
jjgarcia committed
459 460 461 462 463 464 465
	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))
466 467 468
	    if (FIXNUM_MINUSP(x))
	      sign_x = -1;
	    else if (x == MAKE_FIXNUM(0))
jjgarcia's avatar
jjgarcia committed
469 470
	      sign_x = 0;
	    else
471
	      sign_x = 1;
jjgarcia's avatar
jjgarcia committed
472 473 474 475 476 477 478 479 480 481 482 483 484
	  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)
485
}
jjgarcia's avatar
jjgarcia committed
486

487 488 489
cl_object
cl_logcount(cl_object x)
{
jjgarcia's avatar
jjgarcia committed
490
	@(return MAKE_FIXNUM(count_bits(x)))
491
}
jjgarcia's avatar
jjgarcia committed
492

493 494 495
cl_object
cl_integer_length(cl_object x)
{
jjgarcia's avatar
jjgarcia committed
496
	int	count, i;
497

jjgarcia's avatar
jjgarcia committed
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
	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))
513
}
jjgarcia's avatar
jjgarcia committed
514 515 516 517

void
init_num_log(void)
{
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
	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
534 535
}

536 537 538
cl_object
si_bit_array_op(cl_object o, cl_object x, cl_object y, cl_object r)
{
jjgarcia's avatar
jjgarcia committed
539 540
	cl_fixnum i, j, n, d;
	cl_object r0;
541
	bit_operator op;
jjgarcia's avatar
jjgarcia committed
542 543 544 545
	bool replace = FALSE;
	int xi, yi, ri;
	byte *xp, *yp, *rp;
	int xo, yo, ro;
546

jjgarcia's avatar
jjgarcia committed
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
	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)) {
580
			r = si_make_vector(@'bit', MAKE_FIXNUM(d), Cnil, Cnil, Cnil, Cnil);
jjgarcia's avatar
jjgarcia committed
581 582 583 584
		}
	} else {
		if (type_of(x) != t_array)
			goto ERROR;
585
		if ((cl_elttype)x->array.elttype != aet_bit)
jjgarcia's avatar
jjgarcia committed
586 587 588 589 590 591
			goto ERROR;
		d = x->array.dim;
		xp = x->vector.self.bit;
		xo = x->vector.offset;
		if (type_of(y) != t_array)
			goto ERROR;
592
		if ((cl_elttype)y->array.elttype != aet_bit)
jjgarcia's avatar
jjgarcia committed
593 594 595 596 597 598 599 600 601 602 603 604 605
			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;
606
			if ((cl_elttype)r->array.elttype != aet_bit)
jjgarcia's avatar
jjgarcia committed
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
				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)) {
629
		  r = cl_alloc_object(t_array);
jjgarcia's avatar
jjgarcia committed
630 631 632 633
		  r->array.self.t = NULL;
		  r->array.displaced = Cnil;
		  r->array.rank = 1;
		  r->array.dims = NULL;
634
		  r->array.elttype = get_elttype(@'bit');
635
		  r->array.dims = (cl_index *)cl_alloc_atomic_align(x->array.dim * sizeof(cl_index), sizeof(cl_index));
jjgarcia's avatar
jjgarcia committed
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 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
		  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);
716
}