added moo_log2_for_pow2()

This commit is contained in:
hyunghwan.chung 2019-03-23 04:47:47 +00:00
parent 78970f370f
commit f126013747
4 changed files with 51 additions and 6 deletions

View File

@ -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 2**x = v
x = logv(2) = log(v) / log(2) x = log2(v)
x is the number of shift to make */ x is the number of shift to make */
TODO: TODO:
DO SHIFTING. how to get remainder.. 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; if (!z) return MOO_NULL;
rshift_unsigned_array (z, MOO_OBJ_GET_SIZE(z), log(v)/log(2)); rshift_unsigned_array (z, MOO_OBJ_GET_SIZE(z), log(v)/log(2));
} }
#ebduf #endif
} }
moo_pushvolat (moo, &x); moo_pushvolat (moo, &x);

View File

@ -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) #if defined(MOO_HAVE_UINT16_T)
MOO_EXPORT moo_uint16_t moo_ntoh16 ( MOO_EXPORT moo_uint16_t moo_ntoh16 (
moo_uint16_t x moo_uint16_t x

View File

@ -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; v = ((struct st_int32_t*)&rawptr[offset])->v;
break; 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: case 8:
v = ((struct st_int64_t*)&rawptr[offset])->v; v = ((struct st_int64_t*)&rawptr[offset])->v;
break; break;
#endif #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: case 16:
v = ((struct st_int128_t*)&rawptr[offset])->v; v = ((struct st_int128_t*)&rawptr[offset])->v;
break; 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; ((struct st_int32_t*)&rawptr[offset])->v = w;
return 0; 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: case 8:
((struct st_int64_t*)&rawptr[offset])->v = w; ((struct st_int64_t*)&rawptr[offset])->v = w;
return 0; return 0;
#endif #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: case 16:
((struct st_int128_t*)&rawptr[offset])->v = w; ((struct st_int128_t*)&rawptr[offset])->v = w;
return 0; return 0;

View File

@ -26,6 +26,45 @@
#include "moo-prv.h" #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) #if defined(MOO_HAVE_UINT16_T)