From 9162e3691399a1942259f1c7322469cf285d4cc4 Mon Sep 17 00:00:00 2001 From: Justin Ethier Date: Sat, 1 Apr 2017 01:02:41 -0400 Subject: [PATCH] Bignum NOT operation --- srfi/60.scm | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/srfi/60.scm b/srfi/60.scm index fc9d31b1..33533f96 100644 --- a/srfi/60.scm +++ b/srfi/60.scm @@ -121,13 +121,21 @@ (define-c lognot "(void* data, int argc, closure _, object k, object x)" "Cyc_check_int(data, x); + alloc_bignum(data, bn); if (Cyc_is_bignum(x) == boolean_t) { - // uh oh, libtommath doesn't provide this! - Cyc_rt_raise_msg(data, \"bignum negation not supported yet\"); + mp_copy(&bignum_value(x), &bignum_value(bn)); } else { - int result = ~((int)unbox_number(x)); - return_closcall1(data, k, obj_int2obj(result)); - }") + Cyc_int2bignum((int)unbox_number(x), &bignum_value(bn)); + } + + // From https://github.com/libtom/libtommath/issues/30 + /* A one's complement, aka bitwise NOT, is actually just -a - 1 */ + //CHECK_ERROR(mp_neg(&op->mp, &out->mp)); + //CHECK_ERROR(mp_sub_d(&out->mp, 1, &out->mp)); + mp_neg(&bignum_value(bn), &bignum_value(bn)); + mp_sub_d(&bignum_value(bn), 1, &bignum_value(bn)); + return_closcall1(data, k, Cyc_bignum_normalize(data, bn)); + ") (define bitwise-not lognot)