ruby-changes:30671
From: akr <ko1@a...>
Date: Sun, 1 Sep 2013 09:57:06 +0900 (JST)
Subject: [ruby-changes:30671] akr:r42750 (trunk): * internal.h (bit_length): Moved from bignum.c.
akr 2013-09-01 09:57:00 +0900 (Sun, 01 Sep 2013) New Revision: 42750 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=42750 Log: * internal.h (bit_length): Moved from bignum.c. (nlz_int): Ditto. (nlz_long): Ditto. (nlz_long_long): Ditto. (nlz_int128): Ditto. Modified files: trunk/ChangeLog trunk/bignum.c trunk/internal.h Index: ChangeLog =================================================================== --- ChangeLog (revision 42749) +++ ChangeLog (revision 42750) @@ -1,3 +1,11 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Sun Sep 1 09:55:45 2013 Tanaka Akira <akr@f...> + + * internal.h (bit_length): Moved from bignum.c. + (nlz_int): Ditto. + (nlz_long): Ditto. + (nlz_long_long): Ditto. + (nlz_int128): Ditto. + Sun Sep 1 03:32:22 2013 Tanaka Akira <akr@f...> * bignum.c (bit_length): Renamed from bitsize. Index: internal.h =================================================================== --- internal.h (revision 42749) +++ internal.h (revision 42750) @@ -106,6 +106,132 @@ extern "C" { https://github.com/ruby/ruby/blob/trunk/internal.h#L106 # endif #endif +static inline int +nlz_int(unsigned int x) +{ +#if defined(HAVE_BUILTIN___BUILTIN_CLZ) + if (x == 0) return SIZEOF_INT * CHAR_BIT; + return __builtin_clz(x); +#else + unsigned int y; +# if 64 < SIZEOF_INT * CHAR_BIT + int n = 128; +# elif 32 < SIZEOF_INT * CHAR_BIT + int n = 64; +# else + int n = 32; +# endif +# if 64 < SIZEOF_INT * CHAR_BIT + y = x >> 64; if (y) {n -= 64; x = y;} +# endif +# if 32 < SIZEOF_INT * CHAR_BIT + y = x >> 32; if (y) {n -= 32; x = y;} +# endif + y = x >> 16; if (y) {n -= 16; x = y;} + y = x >> 8; if (y) {n -= 8; x = y;} + y = x >> 4; if (y) {n -= 4; x = y;} + y = x >> 2; if (y) {n -= 2; x = y;} + y = x >> 1; if (y) {return n - 2;} + return (int)(n - x); +#endif +} + +static inline int +nlz_long(unsigned long x) +{ +#if defined(HAVE_BUILTIN___BUILTIN_CLZL) + if (x == 0) return SIZEOF_LONG * CHAR_BIT; + return __builtin_clzl(x); +#else + unsigned long y; +# if 64 < SIZEOF_LONG * CHAR_BIT + int n = 128; +# elif 32 < SIZEOF_LONG * CHAR_BIT + int n = 64; +# else + int n = 32; +# endif +# if 64 < SIZEOF_LONG * CHAR_BIT + y = x >> 64; if (y) {n -= 64; x = y;} +# endif +# if 32 < SIZEOF_LONG * CHAR_BIT + y = x >> 32; if (y) {n -= 32; x = y;} +# endif + y = x >> 16; if (y) {n -= 16; x = y;} + y = x >> 8; if (y) {n -= 8; x = y;} + y = x >> 4; if (y) {n -= 4; x = y;} + y = x >> 2; if (y) {n -= 2; x = y;} + y = x >> 1; if (y) {return n - 2;} + return (int)(n - x); +#endif +} + +#ifdef HAVE_LONG_LONG +static inline int +nlz_long_long(unsigned LONG_LONG x) +{ +#if defined(HAVE_BUILTIN___BUILTIN_CLZLL) + if (x == 0) return SIZEOF_LONG_LONG * CHAR_BIT; + return __builtin_clzll(x); +#else + unsigned LONG_LONG y; +# if 64 < SIZEOF_LONG_LONG * CHAR_BIT + int n = 128; +# elif 32 < SIZEOF_LONG_LONG * CHAR_BIT + int n = 64; +# else + int n = 32; +# endif +# if 64 < SIZEOF_LONG_LONG * CHAR_BIT + y = x >> 64; if (y) {n -= 64; x = y;} +# endif +# if 32 < SIZEOF_LONG_LONG * CHAR_BIT + y = x >> 32; if (y) {n -= 32; x = y;} +# endif + y = x >> 16; if (y) {n -= 16; x = y;} + y = x >> 8; if (y) {n -= 8; x = y;} + y = x >> 4; if (y) {n -= 4; x = y;} + y = x >> 2; if (y) {n -= 2; x = y;} + y = x >> 1; if (y) {return n - 2;} + return (int)(n - x); +#endif +} +#endif + +#ifdef HAVE_UINT128_T +static inline int +nlz_int128(uint128_t x) +{ + uint128_t y; + int n = 128; + y = x >> 64; if (y) {n -= 64; x = y;} + y = x >> 32; if (y) {n -= 32; x = y;} + y = x >> 16; if (y) {n -= 16; x = y;} + y = x >> 8; if (y) {n -= 8; x = y;} + y = x >> 4; if (y) {n -= 4; x = y;} + 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_UINT128_T) +# define bit_length(x) \ + (sizeof(x) <= SIZEOF_INT ? SIZEOF_INT * CHAR_BIT - nlz_int(x) : \ + sizeof(x) <= SIZEOF_LONG ? SIZEOF_LONG * CHAR_BIT - nlz_long(x) : \ + sizeof(x) <= SIZEOF_LONG_LONG ? SIZEOF_LONG_LONG * CHAR_BIT - nlz_long_long(x) : \ + SIZEOF_INT128_T * CHAR_BIT - nlz_int128(x)) +#elif defined(HAVE_LONG_LONG) +# define bit_length(x) \ + (sizeof(x) <= SIZEOF_INT ? SIZEOF_INT * CHAR_BIT - nlz_int(x) : \ + sizeof(x) <= SIZEOF_LONG ? SIZEOF_LONG * CHAR_BIT - nlz_long(x) : \ + SIZEOF_LONG_LONG * CHAR_BIT - nlz_long_long(x)) +#else +# define bit_length(x) \ + (sizeof(x) <= SIZEOF_INT ? SIZEOF_INT * CHAR_BIT - nlz_int(x) : \ + SIZEOF_LONG * CHAR_BIT - nlz_long(x)) +#endif + struct rb_deprecated_classext_struct { char conflict[sizeof(VALUE) * 3]; }; Index: bignum.c =================================================================== --- bignum.c (revision 42749) +++ bignum.c (revision 42750) @@ -154,115 +154,6 @@ static VALUE bigsq(VALUE x); https://github.com/ruby/ruby/blob/trunk/bignum.c#L154 static void bigdivmod(VALUE x, VALUE y, volatile VALUE *divp, volatile VALUE *modp); static inline VALUE power_cache_get_power(int base, int power_level, size_t *numdigits_ret); -static int -nlz_int(unsigned int x) -{ -#if defined(HAVE_BUILTIN___BUILTIN_CLZ) - if (x == 0) return SIZEOF_INT * CHAR_BIT; - return __builtin_clz(x); -#else - unsigned int y; -# if 64 < SIZEOF_INT * CHAR_BIT - int n = 128; -# elif 32 < SIZEOF_INT * CHAR_BIT - int n = 64; -# else - int n = 32; -# endif -# if 64 < SIZEOF_INT * CHAR_BIT - y = x >> 64; if (y) {n -= 64; x = y;} -# endif -# if 32 < SIZEOF_INT * CHAR_BIT - y = x >> 32; if (y) {n -= 32; x = y;} -# endif - y = x >> 16; if (y) {n -= 16; x = y;} - y = x >> 8; if (y) {n -= 8; x = y;} - y = x >> 4; if (y) {n -= 4; x = y;} - y = x >> 2; if (y) {n -= 2; x = y;} - y = x >> 1; if (y) {return n - 2;} - return (int)(n - x); -#endif -} - -static int -nlz_long(unsigned long x) -{ -#if defined(HAVE_BUILTIN___BUILTIN_CLZL) - if (x == 0) return SIZEOF_LONG * CHAR_BIT; - return __builtin_clzl(x); -#else - unsigned long y; -# if 64 < SIZEOF_LONG * CHAR_BIT - int n = 128; -# elif 32 < SIZEOF_LONG * CHAR_BIT - int n = 64; -# else - int n = 32; -# endif -# if 64 < SIZEOF_LONG * CHAR_BIT - y = x >> 64; if (y) {n -= 64; x = y;} -# endif -# if 32 < SIZEOF_LONG * CHAR_BIT - y = x >> 32; if (y) {n -= 32; x = y;} -# endif - y = x >> 16; if (y) {n -= 16; x = y;} - y = x >> 8; if (y) {n -= 8; x = y;} - y = x >> 4; if (y) {n -= 4; x = y;} - y = x >> 2; if (y) {n -= 2; x = y;} - y = x >> 1; if (y) {return n - 2;} - return (int)(n - x); -#endif -} - -#ifdef HAVE_LONG_LONG -static int -nlz_long_long(unsigned LONG_LONG x) -{ -#if defined(HAVE_BUILTIN___BUILTIN_CLZLL) - if (x == 0) return SIZEOF_LONG_LONG * CHAR_BIT; - return __builtin_clzll(x); -#else - unsigned LONG_LONG y; -# if 64 < SIZEOF_LONG_LONG * CHAR_BIT - int n = 128; -# elif 32 < SIZEOF_LONG_LONG * CHAR_BIT - int n = 64; -# else - int n = 32; -# endif -# if 64 < SIZEOF_LONG_LONG * CHAR_BIT - y = x >> 64; if (y) {n -= 64; x = y;} -# endif -# if 32 < SIZEOF_LONG_LONG * CHAR_BIT - y = x >> 32; if (y) {n -= 32; x = y;} -# endif - y = x >> 16; if (y) {n -= 16; x = y;} - y = x >> 8; if (y) {n -= 8; x = y;} - y = x >> 4; if (y) {n -= 4; x = y;} - y = x >> 2; if (y) {n -= 2; x = y;} - y = x >> 1; if (y) {return n - 2;} - return (int)(n - x); -#endif -} -#endif - -#ifdef HAVE_UINT128_T -static int -nlz_int128(uint128_t x) -{ - uint128_t y; - int n = 128; - y = x >> 64; if (y) {n -= 64; x = y;} - y = x >> 32; if (y) {n -= 32; x = y;} - y = x >> 16; if (y) {n -= 16; x = y;} - y = x >> 8; if (y) {n -= 8; x = y;} - y = x >> 4; if (y) {n -= 4; x = y;} - y = x >> 2; if (y) {n -= 2; x = y;} - y = x >> 1; if (y) {return n - 2;} - return (int)(n - x); -} -#endif - #if SIZEOF_BDIGITS <= SIZEOF_INT static int nlz(BDIGIT x) { return nlz_int((unsigned int)x) - (SIZEOF_INT-SIZEOF_BDIGITS) * CHAR_BIT; } #elif SIZEOF_BDIGITS <= SIZEOF_LONG @@ -273,23 +164,6 @@ static int nlz(BDIGIT x) { return nlz_lo https://github.com/ruby/ruby/blob/trunk/bignum.c#L164 static int nlz(BDIGIT x) { return nlz_int128((uint128_t)x) - (SIZEOF_INT128_T-SIZEOF_BDIGITS) * CHAR_BIT; } #endif -#if defined(HAVE_UINT128_T) -# define bit_length(x) \ - (sizeof(x) <= SIZEOF_INT ? SIZEOF_INT * CHAR_BIT - nlz_int(x) : \ - sizeof(x) <= SIZEOF_LONG ? SIZEOF_LONG * CHAR_BIT - nlz_long(x) : \ - sizeof(x) <= SIZEOF_LONG_LONG ? SIZEOF_LONG_LONG * CHAR_BIT - nlz_long_long(x) : \ - SIZEOF_INT128_T * CHAR_BIT - nlz_int128(x)) -#elif defined(HAVE_LONG_LONG) -# define bit_length(x) \ - (sizeof(x) <= SIZEOF_INT ? SIZEOF_INT * CHAR_BIT - nlz_int(x) : \ - sizeof(x) <= SIZEOF_LONG ? SIZEOF_LONG * CHAR_BIT - nlz_long(x) : \ - SIZEOF_LONG_LONG * CHAR_BIT - nlz_long_long(x)) -#else -# define bit_length(x) \ - (sizeof(x) <= SIZEOF_INT ? SIZEOF_INT * CHAR_BIT - nlz_int(x) : \ - SIZEOF_LONG * CHAR_BIT - nlz_long(x)) -#endif - #define U16(a) ((uint16_t)(a)) #define U32(a) ((uint32_t)(a)) #ifdef HAVE_UINT64_T -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/