commit 2fb4419de3975d0dfd4034b8dd1011c17c56cae7
parent f47f4092ca541473c32028e03c60309b9ee79db5
Author: Andres Navarro <canavarro82@gmail.com>
Date: Sun, 10 Apr 2011 13:59:50 -0300
Added support for bigints in even? & odd? applicatives.
Diffstat:
3 files changed, 44 insertions(+), 2 deletions(-)
diff --git a/src/kgnumbers.c b/src/kgnumbers.c
@@ -648,8 +648,34 @@ bool knegativep(TValue n)
}
}
-bool koddp(TValue n) { return (ivalue(n) & 1) != 0; }
-bool kevenp(TValue n) { return (ivalue(n) & 1) == 0; }
+/* n is finite */
+bool koddp(TValue n)
+{
+ switch (ttype(n)) {
+ case K_TFIXINT:
+ return (ivalue(n) & 1) != 0;
+ case K_TBIGINT:
+ return kbigint_oddp(n);
+ default:
+ /* shouldn't happen */
+ assert(0);
+ return false;
+ }
+}
+
+bool kevenp(TValue n)
+{
+ switch (ttype(n)) {
+ case K_TFIXINT:
+ return (ivalue(n) & 1) == 0;
+ case K_TBIGINT:
+ return kbigint_evenp(n);
+ default:
+ /* shouldn't happen */
+ assert(0);
+ return false;
+ }
+}
/* 12.5.12 abs */
void kabs(klisp_State *K, TValue *xparams, TValue ptree, TValue denv)
diff --git a/src/kinteger.c b/src/kinteger.c
@@ -166,6 +166,18 @@ bool kbigint_positivep(TValue tv_bigint)
return kbigint_posp(tv2bigint(tv_bigint));
}
+bool kbigint_oddp(TValue tv_bigint)
+{
+ Bigint *bigint = tv2bigint(tv_bigint);
+ return ((bigint->last->digit) & 1) != 0;
+}
+
+bool kbigint_evenp(TValue tv_bigint)
+{
+ Bigint *bigint = tv2bigint(tv_bigint);
+ return ((bigint->last->digit) & 1) == 0;
+}
+
/* Mutate the bigint to have the opposite sign, used in read */
void kbigint_invert_sign(TValue tv_bigint)
{
diff --git a/src/kinteger.h b/src/kinteger.h
@@ -50,6 +50,10 @@ bool kbigint_has_digits(klisp_State *K, TValue tv_bigint);
bool kbigint_negativep(TValue tv_bigint);
bool kbigint_positivep(TValue tv_bigint);
+
+bool kbigint_oddp(TValue tv_bigint);
+bool kbigint_evenp(TValue tv_bigint);
+
/* Mutate the bigint to have the opposite sign, used in read & write */
void kbigint_invert_sign(TValue tv_bigint);