From 4ca60174520d321a19183e6c5d8f829b7c7d1e4e Mon Sep 17 00:00:00 2001 From: Vincent St-Amour Date: Fri, 27 Feb 2009 12:06:07 -0500 Subject: [PATCH] Added bitwise ior and xor for bignums. --- bignums.c | 43 +++++++++++++++++++++++-------------------- primitives.c | 6 +++++- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/bignums.c b/bignums.c index 8c5ff8f..1d81be2 100644 --- a/bignums.c +++ b/bignums.c @@ -8,13 +8,7 @@ #ifdef INFINITE_PRECISION_BIGNUMS -// TODO clean up all this - integer make_integer (digit lo, integer hi) { -/* if(!hi && lo <= MAX_FIXNUM && lo >= MIN_FIXNUM) */ -/* return ENCODE_FIXNUM(lo);o */ - // TODO won't work, and the bignum functions are unaware of fixnums - // TODO if I uncomment, segfaults every time this is called. no ill effect is noticed without it, but kept just in case return alloc_ram_cell_init (BIGNUM_FIELD0 | (hi >> 8), hi, lo >> 8, lo); } @@ -388,26 +382,35 @@ integer divnonneg (integer x, integer y) { integer bitwise_ior (integer x, integer y) { /* returns the bitwise inclusive or of x and y */ - obj result = ZERO; - printf("START\n"); + obj result = NIL; for (;;){ - printf("LOOP\n"); - if (obj_eq(x, ZERO)){ - printf("ZERO\n"); - return make_integer(y, result); - } - if (obj_eq(x, NEG1)){ - printf("NEG1\n"); - return norm(x, result); - } - result = make_integer(integer_lo(x) | integer_lo(y), // TODO a l'envers + if (obj_eq(x, ZERO)) + return norm(result, y); + if (obj_eq(x, NEG1)) + return norm(result, x); + result = make_integer(integer_lo(x) | integer_lo(y), + result); + x = integer_hi(x); + y = integer_hi(y); + } +} + +integer bitwise_xor (integer x, integer y) { // TODO similar to ior (only diff is the test), abstract ? + /* returns the bitwise inclusive or of x and y */ + + obj result = NIL; + + for (;;){ + if (obj_eq(x, ZERO)) + return norm(result, y); + if (obj_eq(x, NEG1)) + return norm(result, x); + result = make_integer(integer_lo(x) ^ integer_lo(y), result); x = integer_hi(x); y = integer_hi(y); - printf("END-LOOP\n"); } - // TODO test for fixnums } // used only in primitives that use small numbers only diff --git a/primitives.c b/primitives.c index f15c4c9..8efb243 100644 --- a/primitives.c +++ b/primitives.c @@ -234,7 +234,7 @@ void prim_geq (void) { arg2 = OBJ_FALSE; } -void prim_ior (void) { // TODO FOOBIGNUMS these have not been implemented with bignums, do it +void prim_ior (void) { #ifdef INFINITE_PRECISION_BIGNUMS arg1 = bitwise_ior(arg1, arg2); #else @@ -245,8 +245,12 @@ void prim_ior (void) { // TODO FOOBIGNUMS these have not been implemented with b } void prim_xor (void) { +#ifdef INFINITE_PRECISION_BIGNUMS + arg1 = bitwise_xor(arg1, arg2); +#else decode_2_int_args (); // TODO is the function call overhead worth it ? arg1 = encode_int (a1 ^ a2); +#endif arg2 = OBJ_FALSE; } -- 2.11.4.GIT