From 14956ea58f4382f50cd629829beae6bfedff6622 Mon Sep 17 00:00:00 2001 From: Justin Ethier Date: Fri, 10 Feb 2017 00:02:24 +0000 Subject: [PATCH] WIP, bignum integer comparisons --- runtime.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/runtime.c b/runtime.c index acaf1bf0..7e8a710e 100644 --- a/runtime.c +++ b/runtime.c @@ -1171,6 +1171,7 @@ typedef enum { int Cyc_bignum_cmp(void *data, bn_cmp_type type, object x, int tx, object y, int ty) { + mp_int tmp; int cmp; if (tx == bignum_tag && ty == bignum_tag) { @@ -1178,11 +1179,28 @@ int Cyc_bignum_cmp(void *data, bn_cmp_type type, object x, int tx, object y, int return (cmp == type) || ((type == CYC_BN_GTE && cmp > MP_LT) || (type == CYC_BN_LTE && cmp < MP_GT)); + } else if (tx == bignum_tag && ty == -1) { \ + // TODO: could consolidate with below + // TODO: probably possible to use sign and if different can avoid bignum alloc + mp_init(&tmp); + mp_set_int(&tmp, obj_obj2int(y)); + cmp = mp_cmp(&bignum_value(x), &tmp); + mp_clear(&tmp); + return (cmp == type) || + ((type == CYC_BN_GTE && cmp > MP_LT) || + (type == CYC_BN_LTE && cmp < MP_GT)); + } else if (tx == -1 && ty == bignum_tag) { \ + mp_init(&tmp); + mp_set_int(&tmp, obj_obj2int(x)); + cmp = mp_cmp(&tmp, &bignum_value(y)); + mp_clear(&tmp); + return (cmp == type) || + ((type == CYC_BN_GTE && cmp > MP_LT) || + (type == CYC_BN_LTE && cmp < MP_GT)); } + /* TODO: - } else if (tx == bignum_tag && ty == -1) { \ } else if (tx == bignum_tag && ty == double_tag) { \ - } else if (tx == -1 && ty == bignum_tag) { \ } else if (tx == double_tag && ty == bignum_tag) { \ */ { @@ -1218,10 +1236,12 @@ int FUNC_OP(void *data, object x, object y) { \ } else if (tx == double_tag && ty == double_tag) { \ result = (double_value(x)) OP (double_value(y)); \ } else if (tx == bignum_tag && ty == -1) { \ + result = Cyc_bignum_cmp(data, BN_CMP, x, tx, y, ty); \ } else if (tx == bignum_tag && ty == double_tag) { \ } else if (tx == bignum_tag && ty == bignum_tag) { \ result = Cyc_bignum_cmp(data, BN_CMP, x, tx, y, ty); \ } else if (tx == -1 && ty == bignum_tag) { \ + result = Cyc_bignum_cmp(data, BN_CMP, x, tx, y, ty); \ } else if (tx == double_tag && ty == bignum_tag) { \ } else { \ make_string(s, "Bad argument type"); \