From 150b2655024892d48ca6af5f70731e2ab68a491f Mon Sep 17 00:00:00 2001 From: akr Date: Mon, 15 Jul 2013 01:49:32 +0000 Subject: [PATCH] * bignum.c (nlz16): Use __builtin_clz if possible. (nlz32): Use __builtin_clz or __builtin_clzl if possible. (nlz64): Use __builtin_clzl or __builtin_clzll if possible. (nlz128): Use __builtin_clzll if possible. * configure.in: Check __builtin_clz, __builtin_clzl and __builtin_clzll. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41976 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 10 ++++++++++ bignum.c | 26 ++++++++++++++++++++++++++ configure.in | 3 +++ 3 files changed, 39 insertions(+) diff --git a/ChangeLog b/ChangeLog index 3be2ca8e1c..299b5ccae2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +Mon Jul 15 10:45:09 2013 Tanaka Akira + + * bignum.c (nlz16): Use __builtin_clz if possible. + (nlz32): Use __builtin_clz or __builtin_clzl if possible. + (nlz64): Use __builtin_clzl or __builtin_clzll if possible. + (nlz128): Use __builtin_clzll if possible. + + * configure.in: Check __builtin_clz, __builtin_clzl and + __builtin_clzll. + Mon Jul 15 09:39:07 2013 Tanaka Akira * bignum.c (power_cache_get_power): Use bitsize instead of ceil_log2. diff --git a/bignum.c b/bignum.c index fa8477e821..ca98c44837 100644 --- a/bignum.c +++ b/bignum.c @@ -123,6 +123,10 @@ static void bigdivmod(VALUE x, VALUE y, volatile VALUE *divp, volatile VALUE *mo static int nlz16(uint16_t x) { +#if defined(HAVE_BUILTIN___BUILTIN_CLZ) && 2 <= SIZEOF_INT + if (x == 0) return 16; + return __builtin_clz(x) - (SIZEOF_INT-2)*CHAR_BIT; +#else uint16_t y; int n = 16; y = x >> 8; if (y) {n -= 8; x = y;} @@ -130,11 +134,19 @@ nlz16(uint16_t x) y = x >> 2; if (y) {n -= 2; x = y;} y = x >> 1; if (y) {return n - 2;} return (int)(n - x); +#endif } static int nlz32(uint32_t x) { +#if defined(HAVE_BUILTIN___BUILTIN_CLZ) && 4 <= SIZEOF_INT + if (x == 0) return 32; + return __builtin_clz(x) - (SIZEOF_INT-4)*CHAR_BIT; +#elif defined(HAVE_BUILTIN___BUILTIN_CLZL) && 4 <= SIZEOF_LONG + if (x == 0) return 32; + return __builtin_clzl(x) - (SIZEOF_LONG-4)*CHAR_BIT; +#else uint32_t y; int n = 32; y = x >> 16; if (y) {n -= 16; x = y;} @@ -143,12 +155,20 @@ nlz32(uint32_t x) y = x >> 2; if (y) {n -= 2; x = y;} y = x >> 1; if (y) {return n - 2;} return (int)(n - x); +#endif } #if defined(HAVE_UINT64_T) static int nlz64(uint64_t x) { +#if defined(HAVE_BUILTIN___BUILTIN_CLZL) && 8 <= SIZEOF_LONG + if (x == 0) return 64; + return __builtin_clzl(x) - (SIZEOF_LONG-8)*CHAR_BIT; +#elif defined(HAVE_BUILTIN___BUILTIN_CLZLL) && 8 <= SIZEOF_LONG_LONG + if (x == 0) return 64; + return __builtin_clzll(x) - (SIZEOF_LONG_LONG-8)*CHAR_BIT; +#else uint64_t y; int n = 64; y = x >> 32; if (y) {n -= 32; x = y;} @@ -158,6 +178,7 @@ nlz64(uint64_t x) y = x >> 2; if (y) {n -= 2; x = y;} y = x >> 1; if (y) {return n - 2;} return (int)(n - x); +#endif } #endif @@ -165,6 +186,10 @@ nlz64(uint64_t x) static int nlz128(uint128_t x) { +#if defined(HAVE_BUILTIN___BUILTIN_CLZLL) && 16 <= SIZEOF_LONG_LONG + if (x == 0) return 128; + return __builtin_clzll(x) - (SIZEOF_LONG_LONG-16)*CHAR_BIT; +#else uint128_t y; int n = 128; y = x >> 64; if (y) {n -= 64; x = y;} @@ -175,6 +200,7 @@ nlz128(uint128_t x) y = x >> 2; if (y) {n -= 2; x = y;} y = x >> 1; if (y) {return n - 2;} return (int)(n - x); +#endif } #endif diff --git a/configure.in b/configure.in index 739d337072..48184e23cf 100644 --- a/configure.in +++ b/configure.in @@ -1849,6 +1849,9 @@ if test "${AS_TR_SH(rb_cv_builtin_$1)}" != no; then fi]) RUBY_CHECK_BUILTIN_FUNC(__builtin_bswap32, [__builtin_bswap32(0)]) RUBY_CHECK_BUILTIN_FUNC(__builtin_bswap64, [__builtin_bswap64(0)]) +RUBY_CHECK_BUILTIN_FUNC(__builtin_clz, [__builtin_clz(0)]) +RUBY_CHECK_BUILTIN_FUNC(__builtin_clzl, [__builtin_clzl(0)]) +RUBY_CHECK_BUILTIN_FUNC(__builtin_clzll, [__builtin_clzll(0)]) # Some platform neet -lrt for clock_gettime, but the other don't. if test x"$ac_cv_func_clock_gettime" != xyes; then