num_log.d 18.4 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/ecl.h>
jjgarcia's avatar
jjgarcia committed
18
#include <stdlib.h>
19
#include <ecl/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
#ifdef WITH_GMP
35
	mpz_ior(i->big.big_num, i->big.big_num, j->big.big_num);
36 37 38
#else  /* WITH_GMP */
        i->big.big_num |= j->big.big_num;
#endif /* WITH_GMP */
jjgarcia's avatar
jjgarcia committed
39 40
}

41
static cl_fixnum
42
xor_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_xor_op(cl_object i, cl_object j)
jjgarcia's avatar
jjgarcia committed
49
{
50
#ifdef WITH_GMP
51
	mpz_xor(i->big.big_num, i->big.big_num, j->big.big_num);
52 53 54
#else  /* WITH_GMP */
        i->big.big_num ^= j->big.big_num;
#endif /* WITH_GMP */
jjgarcia's avatar
jjgarcia committed
55 56
}

57 58
static cl_fixnum
and_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
59 60 61 62
{
	return(i & j);
}

63 64 65
static void
mpz_and_op(cl_object i, cl_object j)
{
66
#ifdef WITH_GMP
67
	mpz_and(i->big.big_num, i->big.big_num, j->big.big_num);
68 69 70
#else  /* WITH_GMP */
        i->big.big_num &= j->big.big_num;
#endif /* WITH_GMP */
71 72
}

73 74
static cl_fixnum
eqv_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
75 76 77 78
{
	return(~(i ^ j));
}

79 80 81
static void
mpz_eqv_op(cl_object i, cl_object j)
{
82
#ifdef WITH_GMP
83 84
	mpz_xor(i->big.big_num, i->big.big_num, j->big.big_num);
	mpz_com(i->big.big_num, i->big.big_num);
85 86 87
#else  /* WITH_GMP */
        i->big.big_num = ~(i->big.big_num ^ j->big.big_num);
#endif /* WITH_GMP */
88 89
}

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
static void
mpz_nand_op(cl_object i, cl_object j)
{
99
#ifdef WITH_GMP
100 101
	mpz_and(i->big.big_num, i->big.big_num, j->big.big_num);
	mpz_com(i->big.big_num, i->big.big_num);
102 103 104
#else  /* WITH_GMP */
        i->big.big_num = ~(i->big.big_num & j->big.big_num);
#endif /* WITH_GMP */
105 106
}

107 108
static cl_fixnum
nor_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
109 110 111 112
{
	return(~(i | j));
}

113 114 115
static void
mpz_nor_op(cl_object i, cl_object j)
{
116
#ifdef WITH_GMP
117 118
	mpz_ior(i->big.big_num, i->big.big_num, j->big.big_num);
	mpz_com(i->big.big_num, i->big.big_num);
119 120 121
#else  /* WITH_GMP */
        i->big.big_num = ~(i->big.big_num | j->big.big_num);
#endif /* WITH_GMP */
122 123
}

124 125
static cl_fixnum
andc1_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
126 127 128 129
{
	return((~i) & j);
}

130 131 132
static void
mpz_andc1_op(cl_object i, cl_object j)
{
133
#ifdef WITH_GMP
134 135
	mpz_com(i->big.big_num, i->big.big_num);
	mpz_and(i->big.big_num, i->big.big_num, j->big.big_num);
136 137 138
#else  /* WITH_GMP */
        i->big.big_num = (~i->big.big_num) & (big_num_t)j;
#endif /* WITH_GMP */
139 140
}

141 142
static cl_fixnum
andc2_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
143 144 145 146
{
	return(i & (~j));
}

147 148 149 150 151
static void mpz_orc1_op(cl_object, cl_object);

static void
mpz_andc2_op(cl_object i, cl_object j)
{
152
#ifdef WITH_GMP
153 154 155
	/* (i & ~j) = ~((~i) | j) */
	mpz_orc1_op(i, j);
	mpz_com(i->big.big_num, i->big.big_num);
156 157 158
#else  /* WITH_GMP */
        i->big.big_num = i->big.big_num & (~j->big.big_num);
#endif /* WITH_GMP */
159 160
}

161 162
static cl_fixnum
orc1_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
163 164 165 166
{
	return((~i) | j);
}

167 168 169
static void
mpz_orc1_op(cl_object i, cl_object j)
{
170
#ifdef WITH_GMP
171 172
	mpz_com(i->big.big_num, i->big.big_num);
	mpz_ior(i->big.big_num, i->big.big_num, j->big.big_num);
173 174 175
#else  /* WITH_GMP */
        i->big.big_num = (~i->big.big_num) | j->big.big_num;
#endif /* WITH_GMP */
176 177
}

178 179
static cl_fixnum
orc2_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
180 181 182 183
{
	return(i | (~j));
}

184 185 186
static void
mpz_orc2_op(cl_object i, cl_object j)
{
187
#ifdef WITH_GMP
188 189 190
	/* (i | ~j) = ~((~i) & j) */
	mpz_andc1_op(i, j);
	mpz_com(i->big.big_num, i->big.big_num);
191 192 193
#else  /* WITH_GMP */
        i->big.big_num = i->big.big_num | (~j->big.big_num);
#endif /* WITH_GMP */
194 195
}

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

202 203 204
static void
mpz_b_clr_op(cl_object i, cl_object j)
{
205
#ifdef WITH_GMP
206
	mpz_set_si(i->big.big_num, 0);
207 208 209
#else  /* WITH_GMP */
        i->big.big_num = 0ll;
#endif /* WITH_GMP */
210 211
}

212 213
static cl_fixnum
b_set_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
214 215 216 217
{
	return(-1);
}

218 219 220
static void
mpz_b_set_op(cl_object i, cl_object j)
{
221
#ifdef WITH_GMP
222
	mpz_set_si(i->big.big_num, -1);
223 224 225
#else  /* WITH_GMP */
        i->big.big_num = -1ll;
#endif /* WITH_GMP */
226 227
}

228 229
static cl_fixnum
b_1_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
230 231 232 233
{
	return(i);
}

234 235 236 237 238
static void
mpz_b_1_op(cl_object i, cl_object j)
{
}

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

245 246 247
static void
mpz_b_2_op(cl_object i, cl_object j)
{
248
#ifdef WITH_GMP
249
	mpz_set(i->big.big_num, j->big.big_num);
250 251 252
#else  /* WITH_GMP */
        i->big.big_num = j->big.big_num;
#endif /* WITH_GMP */
253 254
}

255 256
static cl_fixnum
b_c1_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
257 258 259 260
{
	return(~i);
}

261 262 263
static void
mpz_b_c1_op(cl_object i, cl_object j)
{
264
#ifdef WITH_GMP
265
	mpz_com(i->big.big_num, i->big.big_num);
266 267 268
#else  /* WITH_GMP */
        i->big.big_num = ~i->big.big_num;
#endif /* WITH_GMP */
269 270
}

271 272
static cl_fixnum
b_c2_op(cl_fixnum i, cl_fixnum j)
jjgarcia's avatar
jjgarcia committed
273 274 275 276
{
	return(~j);
}

277 278 279
static void
mpz_b_c2_op(cl_object i, cl_object j)
{
280
#ifdef WITH_GMP
281
	mpz_com(i->big.big_num, j->big.big_num);
282 283 284
#else  /* WITH_GMP */
        i->big.big_num = ~j->big.big_num;
#endif /* WITH_GMP */
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
}

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
328
log_op(cl_narg narg, int op, cl_va_list ARGS)
329
{
330 331 332 333 334 335 336 337 338
#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);
339
			x = ecl_boole(op, x, y);
340 341 342 343
		} while (--narg);
	}
	return x;
#else
344 345 346
	cl_object x, numi;
	bit_operator fix_log_op;
	bignum_bit_operator big_log_op;
347 348
	int i = 1;
	cl_fixnum j;
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 395 396 397 398 399 400 401

	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));
402
#endif
403 404
}

405 406
cl_object
ecl_boole(int op, cl_object x, cl_object y)
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
{
	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();
434
#ifdef WITH_GMP
435
			mpz_set_si(z->big.big_num, fix(y));
436 437 438
#else  /* WITH_GMP */
                        z->big.big_num = fix(y);
#endif /* WITH_GMP */
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
			(*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);
}

457 458 459
cl_object
cl_lognot(cl_object x)
{
460
	return @logxor(2,x,MAKE_FIXNUM(-1));
461
}
jjgarcia's avatar
jjgarcia committed
462

463
static cl_fixnum
jjgarcia's avatar
jjgarcia committed
464 465 466 467 468 469 470 471 472
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)
473
			if (j & 1) count++;
jjgarcia's avatar
jjgarcia committed
474 475 476
		break;
	}
	case t_bignum:
477
#ifdef WITH_GMP
478 479 480 481 482
		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);
483
			count = mpz_popcount(z->big.big_num);
484
			big_register_free(z);
jjgarcia's avatar
jjgarcia committed
485
		}
486 487 488 489 490 491 492 493 494
#else  /* WITH_GMP */
                {
                     big_num_t i = x->big.big_num;
                     if ( i<0 ) 
                          i = ~i;
                     for ( count=0 ; i ; i >>= 1 )
                          if ( i&1 ) count++;
                }
#endif /* WITH_GMP */
jjgarcia's avatar
jjgarcia committed
495 496 497 498 499 500 501 502 503 504 505
		break;
	default:
		FEtype_error_integer(x);
	}
	return count;
}

/*
   Left shift if w > 0, right shift if w < 0.
 */
cl_object
506
ecl_ash(cl_object x, cl_fixnum w)
jjgarcia's avatar
jjgarcia committed
507 508
{
	cl_object y;
509 510 511

	if (w == 0)
		return(x);
jjgarcia's avatar
jjgarcia committed
512 513
	y = big_register0_get();
	if (w < 0) {
514
		cl_index bits = -w;
515
		if (FIXNUMP(x)) {
516 517 518 519 520 521 522
			/* 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 :-/
			 */
523
			cl_fixnum y = fix(x);
524 525 526 527 528
			if (bits >= FIXNUM_BITS) {
				y = (y < 0)? -1 : 0;
			} else {
				y >>= bits;
			}
529 530
			return MAKE_FIXNUM(y);
		}
531
#ifdef WITH_GMP
532
		mpz_div_2exp(y->big.big_num, x->big.big_num, bits);
533 534 535
#else  /* WITH_GMP */
                y->big.big_num = x->big.big_num >> bits;
#endif /* WITH_GMP */
jjgarcia's avatar
jjgarcia committed
536
	} else {
537
#ifdef WITH_GMP
538 539 540 541
		if (FIXNUMP(x)) {
			mpz_set_si(y->big.big_num, fix(x));
			x = y;
		}
542
		mpz_mul_2exp(y->big.big_num, x->big.big_num, (unsigned long)w);
543 544 545 546
#else  /* WITH_GMP */
                y->big.big_num = FIXNUMP(x) ? fix(x) : x->big.big_num;
                y->big.big_num <<= w;
#endif /* WITH_GMP */
jjgarcia's avatar
jjgarcia committed
547 548 549 550
	}
	return(big_register_normalize(y));
}

551 552
int
ecl_fixnum_bit_length(cl_fixnum i)
jjgarcia's avatar
jjgarcia committed
553
{
554 555 556 557 558 559
	int count;
	if (i < 0)
		i = ~i;
	for (count = 0; i && (count < FIXNUM_BITS); i >>= 1, count++)
		;
	return count;
jjgarcia's avatar
jjgarcia committed
560 561 562 563 564 565
}

@(defun logior (&rest nums)
@
	if (narg == 0)
		@(return MAKE_FIXNUM(0))
566
	/* INV: log_op() checks types and outputs first argument as default. */
567
	@(return log_op(narg, ECL_BOOLIOR, nums))
jjgarcia's avatar
jjgarcia committed
568 569 570 571 572 573
@)

@(defun logxor (&rest nums)
@
	if (narg == 0)
		@(return MAKE_FIXNUM(0))
574
	/* INV: log_op() checks types and outputs first argument as default. */
575
	@(return log_op(narg, ECL_BOOLXOR, nums))
jjgarcia's avatar
jjgarcia committed
576 577 578 579 580 581
@)

@(defun logand (&rest nums)
@
	if (narg == 0)
		@(return MAKE_FIXNUM(-1))
582
	/* INV: log_op() checks types and outputs first argument as default. */
583
	@(return log_op(narg, ECL_BOOLAND, nums))
jjgarcia's avatar
jjgarcia committed
584 585 586 587 588 589
@)

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

594 595 596
cl_object
cl_lognand(cl_object x, cl_object y)
{
597
	@(return ecl_boole(ECL_BOOLNAND, x, y))
598
}
599

600 601 602
cl_object
cl_lognor(cl_object x, cl_object y)
{
603
	@(return ecl_boole(ECL_BOOLNOR, x, y))
604
}
605

606 607 608
cl_object
cl_logandc1(cl_object x, cl_object y)
{
609
	@(return ecl_boole(ECL_BOOLANDC1, x, y))
610
}
611

612 613 614
cl_object
cl_logandc2(cl_object x, cl_object y)
{
615
	@(return ecl_boole(ECL_BOOLANDC2, x, y))
616
}
617

618 619 620
cl_object
cl_logorc1(cl_object x, cl_object y)
{
621
	@(return ecl_boole(ECL_BOOLORC1, x, y))
622
}
623

624 625 626
cl_object
cl_logorc2(cl_object x, cl_object y)
{
627
	@(return ecl_boole(ECL_BOOLORC2, x, y))
628 629 630 631 632 633 634
}

static int
coerce_to_logical_operator(cl_object o)
{
	cl_fixnum op;
	op = fixint(o);
635
	if (op < 0 || op > ECL_BOOLSET)
636 637
		FEerror("~S is an invalid logical operator.", 1, o);
	return op;
638
}
639

640 641 642
cl_object
cl_boole(cl_object o, cl_object x, cl_object y)
{
643
	/* INV: log_op2() checks types */
644
	@(return ecl_boole(coerce_to_logical_operator(o), x, y))
645
}
jjgarcia's avatar
jjgarcia committed
646

647 648 649
cl_object
cl_logbitp(cl_object p, cl_object x)
{
650
	bool i;
651

jjgarcia's avatar
jjgarcia committed
652
	assert_type_integer(x);
653
	if (FIXNUMP(p)) {
654
		cl_index n = fixnnint(p);
655 656 657 658 659 660 661 662
		if (FIXNUMP(x)) {
			cl_fixnum y = fix(x);
			if (n >= FIXNUM_BITS) {
				i = (y < 0);
			} else {
				i = ((y >> n) & 1);
			}
		} else {
663
#ifdef WITH_GMP
664
			i = mpz_tstbit(x->big.big_num, n);
665 666 667 668 669 670 671
#else  /* WITH_GMP */
                        if ( n >= 8*sizeof(big_num_t) ) {
                                i = (x->big.big_num < 0);
                        } else {
                                i = (x->big.big_num >> n) & 1;
                        }
#endif /* WITH_GMP */
672
		}
673 674 675 676 677 678 679
	} 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
680
	@(return (i ? Ct : Cnil))
681
}
jjgarcia's avatar
jjgarcia committed
682

683 684 685
cl_object
cl_ash(cl_object x, cl_object y)
{
jjgarcia's avatar
jjgarcia committed
686 687
	cl_object r;
	int sign_x;
688

jjgarcia's avatar
jjgarcia committed
689 690 691
        assert_type_integer(x);
	assert_type_integer(y);
	if (FIXNUMP(y))
692
	  r = ecl_ash(x, fix(y));
jjgarcia's avatar
jjgarcia committed
693 694 695 696 697 698 699
	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))
700 701 702
	    if (FIXNUM_MINUSP(x))
	      sign_x = -1;
	    else if (x == MAKE_FIXNUM(0))
jjgarcia's avatar
jjgarcia committed
703 704
	      sign_x = 0;
	    else
705
	      sign_x = 1;
jjgarcia's avatar
jjgarcia committed
706 707 708 709 710 711 712 713 714 715 716 717 718
	  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)
719
}
jjgarcia's avatar
jjgarcia committed
720

721 722 723
cl_object
cl_logcount(cl_object x)
{
jjgarcia's avatar
jjgarcia committed
724
	@(return MAKE_FIXNUM(count_bits(x)))
725
}
jjgarcia's avatar
jjgarcia committed
726

727 728 729
cl_object
cl_integer_length(cl_object x)
{
730 731
	int count;
	cl_fixnum i;
732

jjgarcia's avatar
jjgarcia committed
733 734 735
	switch (type_of(x)) {
	case t_fixnum:
		i = fix(x);
736
		count = ecl_fixnum_bit_length(i);
jjgarcia's avatar
jjgarcia committed
737
		break;
738
	case t_bignum:
739
		if (big_sign(x) < 0)
740
			x = cl_lognot(x);
741
#ifdef WITH_GMP
742
		count = mpz_sizeinbase(x->big.big_num, 2);
743 744 745 746 747 748 749
#else  /* WITH_GMP */
                for ( i=(8*sizeof(big_num_t))-1 ; i>0 ; i-- )
                        if ( (x->big.big_num >> i) & 1 ) {
                                count = i;
                                break;
                        }
#endif /* WITH_GMP */
jjgarcia's avatar
jjgarcia committed
750 751 752 753 754
		break;
	default:
		FEtype_error_integer(x);
	}
	@(return MAKE_FIXNUM(count))
755
}
jjgarcia's avatar
jjgarcia committed
756

757 758 759
cl_object
si_bit_array_op(cl_object o, cl_object x, cl_object y, cl_object r)
{
jjgarcia's avatar
jjgarcia committed
760 761
	cl_fixnum i, j, n, d;
	cl_object r0;
762
	bit_operator op;
jjgarcia's avatar
jjgarcia committed
763 764 765 766
	bool replace = FALSE;
	int xi, yi, ri;
	byte *xp, *yp, *rp;
	int xo, yo, ro;
767

jjgarcia's avatar
jjgarcia committed
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
	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)) {
801
			r = si_make_vector(@'bit', MAKE_FIXNUM(d), Cnil, Cnil, Cnil, Cnil);
jjgarcia's avatar
jjgarcia committed
802 803 804 805
		}
	} else {
		if (type_of(x) != t_array)
			goto ERROR;
806
		if ((cl_elttype)x->array.elttype != aet_bit)
jjgarcia's avatar
jjgarcia committed
807 808 809 810 811 812
			goto ERROR;
		d = x->array.dim;
		xp = x->vector.self.bit;
		xo = x->vector.offset;
		if (type_of(y) != t_array)
			goto ERROR;
813
		if ((cl_elttype)y->array.elttype != aet_bit)
jjgarcia's avatar
jjgarcia committed
814 815 816 817 818 819 820 821 822 823 824 825 826
			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;
827
			if ((cl_elttype)r->array.elttype != aet_bit)
jjgarcia's avatar
jjgarcia committed
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
				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)) {
850
		  r = cl_alloc_object(t_array);
jjgarcia's avatar
jjgarcia committed
851 852
		  r->array.self.t = NULL;
		  r->array.displaced = Cnil;
jjgarcia's avatar
jjgarcia committed
853 854 855
		  r->array.rank = x->array.rank;
		  r->array.dims = x->array.dims;
		  r->array.elttype = aet_bit;
jjgarcia's avatar
jjgarcia committed
856 857 858 859 860 861 862
		  r->array.dim = x->array.dim;
		  r->array.adjustable = FALSE;
		  array_allocself(r);
		}
	}
	rp = r->vector.self.bit;
	ro = r->vector.offset;
863
	op = fixnum_operations[coerce_to_logical_operator(o)];
jjgarcia's avatar
jjgarcia committed
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916

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