From 2cd60cbed879acb2df2ff2b257b20aa36a7c8e10 Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Mon, 12 Aug 2019 02:32:04 +0000 Subject: [PATCH] added moo_uintmaxtoint() --- moo/bin/main.c | 6 +++++- moo/lib/bigint.c | 46 ++++++++++++++++++++++++++++++++++++++++------ moo/lib/moo.h | 4 ++-- 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/moo/bin/main.c b/moo/bin/main.c index 52cf176..230f7a6 100644 --- a/moo/bin/main.c +++ b/moo/bin/main.c @@ -291,7 +291,11 @@ int main (int argc, char* argv[]) } } -/*MOO_DEBUG2 (moo, "XXXXXXXXXXXXXXXXXXXXX %O %ld\n", moo_ooitoint(moo, MOO_TYPE_MIN(moo_ooi_t)), MOO_TYPE_MIN(moo_ooi_t));*/ +/*MOO_DEBUG2 (moo, "XXXXXXXXXXXXXXXXXXXXX %O %zd\n", moo_ooitoint(moo, MOO_TYPE_MIN(moo_ooi_t)), MOO_TYPE_MIN(moo_ooi_t)); +MOO_DEBUG2 (moo, "XXXXXXXXXXXXXXXXXXXXX %O %jd\n", moo_intmaxtoint(moo, MOO_TYPE_MIN(moo_intmax_t)), MOO_TYPE_MIN(moo_intmax_t)); +MOO_DEBUG2 (moo, "XXXXXXXXXXXXXXXXXXXXX %O %ju\n", moo_uintmaxtoint(moo, MOO_TYPE_MAX(moo_uintmax_t)), MOO_TYPE_MAX(moo_uintmax_t)); +MOO_DEBUG2 (moo, "XXXXXXXXXXXXXXXXXXXXX %O %zu\n", moo_oowtoint(moo, MOO_TYPE_MAX(moo_oow_t)), MOO_TYPE_MAX(moo_oow_t));*/ + MOO_DEBUG0 (moo, "COMPILE OK. STARTING EXECUTION...\n"); xret = 0; diff --git a/moo/lib/bigint.c b/moo/lib/bigint.c index 3a177b5..0428f90 100644 --- a/moo/lib/bigint.c +++ b/moo/lib/bigint.c @@ -625,13 +625,19 @@ static MOO_INLINE moo_oop_t make_bigint_with_intmax (moo_t* moo, moo_intmax_t v) moo_oow_t len; moo_liw_t buf[MOO_SIZEOF_INTMAX_T / MOO_SIZEOF_LIW_T]; moo_uintmax_t ui; + moo_oop_class_t _class; -/*TODO: enhance to support MOO_TYPE_MIN(moo_intmax_t) */ - /* this is not a generic function. it can't handle v - * if it's MOO_TYPE_MIN(moo_intmax_t) */ - MOO_ASSERT (moo, v > MOO_TYPE_MIN(moo_intmax_t)); + if (v >= 0) + { + ui = v; + _class = moo->_large_positive_integer; + } + else + { + ui = (v == MOO_TYPE_MIN(moo_intmax_t))? ((moo_uintmax_t)MOO_TYPE_MAX(moo_intmax_t) + 1): -v; + _class = moo->_large_negative_integer; + } - ui = (v >= 0)? v: -v; len = 0; do { @@ -640,7 +646,23 @@ static MOO_INLINE moo_oop_t make_bigint_with_intmax (moo_t* moo, moo_intmax_t v) } while (ui > 0); - return moo_instantiate(moo, ((v >= 0)? moo->_large_positive_integer: moo->_large_negative_integer), buf, len); + return moo_instantiate(moo, _class, buf, len); +} + +static MOO_INLINE moo_oop_t make_bigint_with_uintmax (moo_t* moo, moo_uintmax_t ui) +{ + moo_oow_t len; + moo_liw_t buf[MOO_SIZEOF_INTMAX_T / MOO_SIZEOF_LIW_T]; + + len = 0; + do + { + buf[len++] = (moo_liw_t)ui; + ui = ui >> MOO_LIW_BITS; + } + while (ui > 0); + + return moo_instantiate(moo, moo->_large_positive_integer, buf, len); } moo_oop_t moo_oowtoint (moo_t* moo, moo_oow_t w) @@ -681,6 +703,18 @@ moo_oop_t moo_intmaxtoint (moo_t* moo, moo_intmax_t i) } } +moo_oop_t moo_uintmaxtoint (moo_t* moo, moo_uintmax_t i) +{ + if (MOO_IN_SMOOI_RANGE(i)) + { + return MOO_SMOOI_TO_OOP(i); + } + else + { + return make_bigint_with_uintmax(moo, i); + } +} + static MOO_INLINE moo_oop_t expand_bigint (moo_t* moo, moo_oop_t oop, moo_oow_t inc) { moo_oop_t z; diff --git a/moo/lib/moo.h b/moo/lib/moo.h index a79974e..9ed5587 100644 --- a/moo/lib/moo.h +++ b/moo/lib/moo.h @@ -2221,10 +2221,10 @@ MOO_EXPORT moo_oop_t moo_intmaxtoint ( moo_intmax_t i ); -/*MOO_EXPORT moo_oop_t moo_uintmaxtoint ( +MOO_EXPORT moo_oop_t moo_uintmaxtoint ( moo_t* moo, moo_uintmax_t i -);*/ +); MOO_EXPORT int moo_inttouintmax ( moo_t* moo,