From 1300a27dca36c28e25df98707ef7032074bd6c28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Lindqvist?= Date: Sat, 7 Jun 2014 13:09:00 +0200 Subject: [PATCH] VM: better version of bignum_to_fixnum_strict that doesn't allocate --- vm/bignum.cpp | 11 ++++++----- vm/math.cpp | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/vm/bignum.cpp b/vm/bignum.cpp index b2b1109183..799099e3c6 100644 --- a/vm/bignum.cpp +++ b/vm/bignum.cpp @@ -383,14 +383,15 @@ BIGNUM_TO_FOO(fixnum, fixnum, fixnum, cell) BIGNUM_TO_FOO(long_long, int64_t, int64_t, uint64_t) BIGNUM_TO_FOO(ulong_long, uint64_t, int64_t, uint64_t) -/* does allocate memory */ +/* cannot allocate memory */ fixnum factor_vm::bignum_to_fixnum_strict(bignum* bignum_in) { - fixnum fix = bignum_to_fixnum(bignum_in); - bignum* bignum_out = fixnum_to_bignum(fix); - GC_BIGNUM(bignum_out); - if (bignum_compare(bignum_in, bignum_out) != bignum_comparison_equal) { + fixnum len = BIGNUM_LENGTH(bignum_in); + bignum_digit_type *digits = BIGNUM_START_PTR(bignum_in); + if ((len == 1 && digits[0] > fixnum_max) || (len > 1)) { general_error(ERROR_OUT_OF_FIXNUM_RANGE, tag(bignum_in), false_object); } + fixnum fix = bignum_to_fixnum(bignum_in); + FACTOR_ASSERT(fix <= fixnum_max && fix >= fixnum_min); return fix; } diff --git a/vm/math.cpp b/vm/math.cpp index 71ba13b803..e88145a2f2 100644 --- a/vm/math.cpp +++ b/vm/math.cpp @@ -277,7 +277,7 @@ void factor_vm::primitive_bits_double() { ctx->push(allot_float(bits_double(to_unsigned_8(ctx->pop())))); } -/* Allocates memory */ +/* Cannot allocate */ fixnum factor_vm::to_fixnum(cell tagged) { switch (TAG(tagged)) { case FIXNUM_TYPE: