diff --git a/moo/lib/bigint.c b/moo/lib/bigint.c index 9cc5d04..4468afb 100644 --- a/moo/lib/bigint.c +++ b/moo/lib/bigint.c @@ -2107,7 +2107,7 @@ moo_oop_t moo_divints (moo_t* moo, moo_oop_t x, moo_oop_t y, int modulo, moo_oop { /* 2**x = v - x = logv(2) = log(v) / log(2) + x = log2(v) x is the number of shift to make */ TODO: DO SHIFTING. how to get remainder.. @@ -2117,7 +2117,7 @@ moo_oop_t moo_divints (moo_t* moo, moo_oop_t x, moo_oop_t y, int modulo, moo_oop if (!z) return MOO_NULL; rshift_unsigned_array (z, MOO_OBJ_GET_SIZE(z), log(v)/log(2)); } -#ebduf +#endif } moo_pushvolat (moo, &x); diff --git a/moo/lib/moo-utl.h b/moo/lib/moo-utl.h index 83c6b7c..5b3b4cf 100644 --- a/moo/lib/moo-utl.h +++ b/moo/lib/moo-utl.h @@ -640,6 +640,12 @@ MOO_EXPORT moo_oow_t moo_utf16_to_uc ( /* ------------------------------------------------------------------------- */ +MOO_EXPORT int moo_log2_for_pow2 ( + moo_oow_t pow2v +); + +/* ------------------------------------------------------------------------- */ + #if defined(MOO_HAVE_UINT16_T) MOO_EXPORT moo_uint16_t moo_ntoh16 ( moo_uint16_t x diff --git a/moo/lib/pf-sys.c b/moo/lib/pf-sys.c index a09eacd..78fb792 100644 --- a/moo/lib/pf-sys.c +++ b/moo/lib/pf-sys.c @@ -184,13 +184,13 @@ static MOO_INLINE moo_oop_t _fetch_raw_int (moo_t* moo, moo_int8_t* rawptr, moo_ v = ((struct st_int32_t*)&rawptr[offset])->v; break; - #if defined(MOO_HAVE_INT64_T) && (MOO_SIZEOF_OOW_T >= MOO_SIZEOF_INT64_T) + #if defined(MOO_HAVE_INT64_T) && (MOO_SIZEOF_OOW_T >= 8) case 8: v = ((struct st_int64_t*)&rawptr[offset])->v; break; #endif - #if defined(MOO_HAVE_INT128_T) && (MOO_SIZEOF_OOW_T >= MOO_SIZEOF_INT128_T) + #if defined(MOO_HAVE_INT128_T) && (MOO_SIZEOF_OOW_T >= 16) case 16: v = ((struct st_int128_t*)&rawptr[offset])->v; break; @@ -279,13 +279,13 @@ static MOO_INLINE int _store_raw_int (moo_t* moo, moo_uint8_t* rawptr, moo_oow_t ((struct st_int32_t*)&rawptr[offset])->v = w; return 0; - #if defined(MOO_HAVE_INT64_T) && (MOO_SIZEOF_OOW_T >= MOO_SIZEOF_INT64_T) + #if defined(MOO_HAVE_INT64_T) && (MOO_SIZEOF_OOW_T >= 8) case 8: ((struct st_int64_t*)&rawptr[offset])->v = w; return 0; #endif - #if defined(MOO_HAVE_INT128_T) && (MOO_SIZEOF_OOW_T >= MOO_SIZEOF_INT128_T) + #if defined(MOO_HAVE_INT128_T) && (MOO_SIZEOF_OOW_T >= 16) case 16: ((struct st_int128_t*)&rawptr[offset])->v = w; return 0; diff --git a/moo/lib/utl.c b/moo/lib/utl.c index 774e549..a6bb754 100644 --- a/moo/lib/utl.c +++ b/moo/lib/utl.c @@ -26,6 +26,45 @@ #include "moo-prv.h" +/* ----------------------------------------------------------------------- */ +int moo_log2_for_pow2 (moo_oow_t pow2v) +{ +#if defined(MOO_HAVE_UINT32_T) && (MOO_SIZEOF_OOW_T == 4) + + static const int debruijn[32] = + { + 0, 1, 28, 2, 29, 14, 24, 3, + 30, 22, 20, 15, 25, 17, 4, 8, + 31, 27, 13, 23, 21, 19, 16, 7, + 26, 12, 18, 6, 11, 5, 10, 9 + }; + + return debruijn[(moo_uint32_t)(pow2v * 0x077CB531u) >> 27]; + +#elif defined(MOO_HAVE_UINT64_T) && (MOO_SIZEOF_OOW_T == 8) + + static const int debruijn[64] = + { + 0, 1, 2, 53, 3, 7, 54, 27, + 4, 38, 41, 8, 34, 55, 48, 28, + 62, 5, 39, 46, 44, 42, 22, 9, + 24, 35, 59, 56, 49, 18, 29, 11, + 63, 52, 6, 26, 37, 40, 33, 47, + 61, 45, 43, 21, 23, 58, 17, 10, + 51, 25, 36, 32, 60, 20, 57, 16, + 50, 31, 19, 15, 30, 14, 13, 12 + }; + + return debruijn[(moo_uint64_t)(pow2v * 0x022fdd63cc95386dllu) >> 58]; + +#else + int i; + for (i = -1; pow2v; i++) pow2v >>= 1; + return (i == -1) ? 0 : i; + +#endif +} + /* ----------------------------------------------------------------------- */ #if defined(MOO_HAVE_UINT16_T)