From e519c5078ece126369b5a03199657076eee9b939 Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Tue, 10 Nov 2015 14:29:32 +0000 Subject: [PATCH] relocated bigint data table initialization to stix_init() --- stix/lib/bigint.c | 26 +++----------------------- stix/lib/stix.c | 25 +++++++++++++++++++++++++ stix/lib/stix.h | 2 +- 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/stix/lib/bigint.c b/stix/lib/bigint.c index 72c5e04..611f7a1 100644 --- a/stix/lib/bigint.c +++ b/stix/lib/bigint.c @@ -654,35 +654,15 @@ stix_oop_t stix_strtoint (stix_t* stix, const stix_ooch_t* str, stix_oow_t len, } else { - stix_oow_t r1, r2, ub; + stix_oow_t r1, r2; stix_oohw_t multiplier; int dg, i, safe_ndigits; w = 0; ptr = start; - if (stix->bigint[radix].safe_ndigits > 0) - { - /* if the table entry has been initialized properly, - * take it instead of recalculation. */ - safe_ndigits = stix->bigint[radix].safe_ndigits; - multiplier = stix->bigint[radix].multiplier; - } - else - { - r1 = 0; - ub = STIX_TYPE_MAX(stix_oohw_t) / radix - (radix - 1); - multiplier = 1; - for (safe_ndigits = 0; r1 <= ub; safe_ndigits++) - { - r1 = r1 * radix + (radix - 1); - multiplier *= radix; - } - /* safe_ndigits contains the number of digits that never - * cause overflow when computed normally with a native type. */ - stix->bigint[radix].safe_ndigits = safe_ndigits; - stix->bigint[radix].multiplier = multiplier; - } + safe_ndigits = stix->bigint[radix].safe_ndigits; + multiplier = stix->bigint[radix].multiplier; outlen = (end - str) / safe_ndigits + 1; if (outlen > STIX_COUNTOF(hw)) diff --git a/stix/lib/stix.c b/stix/lib/stix.c index 51e7a16..1148e35 100644 --- a/stix/lib/stix.c +++ b/stix/lib/stix.c @@ -56,6 +56,29 @@ void stix_close (stix_t* stix) STIX_MMGR_FREE (stix->mmgr, stix); } +static void fill_bigint_tables (stix_t* stix) +{ + int radix, safe_ndigits; + stix_oow_t multiplier, ub, w; + + for (radix = 2; radix <= 36; radix++) + { + w = 0; + ub = (stix_oow_t)STIX_TYPE_MAX(stix_oohw_t) / radix - (radix - 1); + multiplier = 1; + for (safe_ndigits = 0; w <= ub; safe_ndigits++) + { + w = w * radix + (radix - 1); + multiplier *= radix; + } + + /* safe_ndigits contains the number of digits that never + * cause overflow when computed normally with a native type. */ + stix->bigint[radix].safe_ndigits = safe_ndigits; + stix->bigint[radix].multiplier = multiplier; + } +} + int stix_init (stix_t* stix, stix_mmgr_t* mmgr, stix_size_t heapsz, const stix_vmprim_t* vmprim) { STIX_MEMSET (stix, 0, STIX_SIZEOF(*stix)); @@ -71,6 +94,8 @@ int stix_init (stix_t* stix, stix_mmgr_t* mmgr, stix_size_t heapsz, const stix_v if (stix_rbt_init (&stix->pmtable, mmgr, STIX_SIZEOF(stix_ooch_t), 1) <= -1) goto oops; stix_rbt_setstyle (&stix->pmtable, stix_getrbtstyle(STIX_RBT_STYLE_INLINE_COPIERS)); + + fill_bigint_tables (stix); return 0; oops: diff --git a/stix/lib/stix.h b/stix/lib/stix.h index ee4cab3..279e0e8 100644 --- a/stix/lib/stix.h +++ b/stix/lib/stix.h @@ -704,7 +704,7 @@ struct stix_t { int safe_ndigits; stix_oow_t multiplier; - } bigint[36]; + } bigint[37]; /* == END BIGINT CONVERSION == */ #if defined(STIX_INCLUDE_COMPILER)