* bignum.c (nlz16): Removed.

(nlz32): Ditto.
  (nlz64): Ditto.
  (nlz128): Ditto.
  (nlz_int): New function.
  (nlz_long): New function.
  (nlz_long_long): New function.
  (nlz_int128): New function.
  (nlz): Follow above changes.
  (bitsize): Follow above changes.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42413 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2013-08-06 22:43:01 +00:00
parent 0650ecc395
commit d35596c61d
2 changed files with 110 additions and 72 deletions

View file

@ -1,3 +1,16 @@
Wed Aug 7 07:38:39 2013 Tanaka Akira <akr@fsij.org>
* bignum.c (nlz16): Removed.
(nlz32): Ditto.
(nlz64): Ditto.
(nlz128): Ditto.
(nlz_int): New function.
(nlz_long): New function.
(nlz_long_long): New function.
(nlz_int128): New function.
(nlz): Follow above changes.
(bitsize): Follow above changes.
Tue Aug 6 22:38:15 2013 Zachary Scott <e@zzak.io> Tue Aug 6 22:38:15 2013 Zachary Scott <e@zzak.io>
* time.c: [DOC] Typo in Time overview by @sparr [Fixes GH-374] * time.c: [DOC] Typo in Time overview by @sparr [Fixes GH-374]

169
bignum.c
View file

@ -149,57 +149,87 @@ static void bigdivmod(VALUE x, VALUE y, volatile VALUE *divp, volatile VALUE *mo
static inline VALUE power_cache_get_power(int base, int power_level, size_t *numdigits_ret); static inline VALUE power_cache_get_power(int base, int power_level, size_t *numdigits_ret);
static int static int
nlz16(uint16_t x) nlz_int(unsigned int x)
{ {
#if defined(HAVE_BUILTIN___BUILTIN_CLZ) && 2 <= SIZEOF_INT #if defined(HAVE_BUILTIN___BUILTIN_CLZ)
if (x == 0) return 16; if (x == 0) return SIZEOF_INT * CHAR_BIT;
return __builtin_clz(x) - (SIZEOF_INT-2)*CHAR_BIT; return __builtin_clz(x);
#else #else
uint16_t y; unsigned int y;
int n = 16; # if 64 < SIZEOF_INT * CHAR_BIT
y = x >> 8; if (y) {n -= 8; x = y;} int n = 128;
y = x >> 4; if (y) {n -= 4; x = y;} # elif 32 < SIZEOF_INT * CHAR_BIT
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;}
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_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; 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;} 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 >> 16; if (y) {n -= 16; x = y;}
y = x >> 8; if (y) {n -= 8; x = y;} y = x >> 8; if (y) {n -= 8; x = y;}
y = x >> 4; if (y) {n -= 4; x = y;} y = x >> 4; if (y) {n -= 4; x = y;}
@ -210,14 +240,10 @@ nlz64(uint64_t x)
} }
#endif #endif
#if defined(HAVE_UINT128_T) #ifdef HAVE_UINT128_T
static int static int
nlz128(uint128_t x) nlz_int128(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; uint128_t y;
int n = 128; int n = 128;
y = x >> 64; if (y) {n -= 64; x = y;} y = x >> 64; if (y) {n -= 64; x = y;}
@ -228,35 +254,34 @@ nlz128(uint128_t x)
y = x >> 2; if (y) {n -= 2; x = y;} y = x >> 2; if (y) {n -= 2; x = y;}
y = x >> 1; if (y) {return n - 2;} y = x >> 1; if (y) {return n - 2;}
return (int)(n - x); return (int)(n - x);
#endif
} }
#endif #endif
#if SIZEOF_BDIGITS == 2 #if SIZEOF_BDIGITS <= SIZEOF_INT
static int nlz(BDIGIT x) { return nlz16((uint16_t)x); } static int nlz(BDIGIT x) { return nlz_int((unsigned int)x) - (SIZEOF_INT-SIZEOF_BDIGITS) * CHAR_BIT; }
#elif SIZEOF_BDIGITS == 4 #elif SIZEOF_BDIGITS <= SIZEOF_LONG
static int nlz(BDIGIT x) { return nlz32((uint32_t)x); } static int nlz(BDIGIT x) { return nlz_long((unsigned long)x) - (SIZEOF_LONG-SIZEOF_BDIGITS) * CHAR_BIT; }
#elif SIZEOF_BDIGITS == 8 #elif SIZEOF_BDIGITS <= SIZEOF_LONG_LONG
static int nlz(BDIGIT x) { return nlz64((uint64_t)x); } static int nlz(BDIGIT x) { return nlz_long_long((unsigned LONG_LONG)x) - (SIZEOF_LONG_LONG-SIZEOF_BDIGITS) * CHAR_BIT; }
#elif SIZEOF_BDIGITS == 16 #elif SIZEOF_BDIGITS <= SIZEOF_INT128_T
static int nlz(BDIGIT x) { return nlz128((uint128_t)x); } static int nlz(BDIGIT x) { return nlz_int128((uint128_t)x) - (SIZEOF_INT128_T-SIZEOF_BDIGITS) * CHAR_BIT; }
#endif #endif
#if defined(HAVE_UINT128_T) #if defined(HAVE_UINT128_T)
# define bitsize(x) \ # define bitsize(x) \
(sizeof(x) <= 2 ? 16 - nlz16(x) : \ (sizeof(x) <= SIZEOF_INT ? SIZEOF_INT * CHAR_BIT - nlz_int(x) : \
sizeof(x) <= 4 ? 32 - nlz32(x) : \ sizeof(x) <= SIZEOF_LONG ? SIZEOF_LONG * CHAR_BIT - nlz_long(x) : \
sizeof(x) <= 8 ? 64 - nlz64(x) : \ sizeof(x) <= SIZEOF_LONG_LONG ? SIZEOF_LONG_LONG * CHAR_BIT - nlz_long_long(x) : \
128 - nlz128(x)) SIZEOF_INT128_T * CHAR_BIT - nlz_int128(x))
#elif defined(HAVE_UINT64_T) #elif defined(HAVE_LONG_LONG)
# define bitsize(x) \ # define bitsize(x) \
(sizeof(x) <= 2 ? 16 - nlz16(x) : \ (sizeof(x) <= SIZEOF_INT ? SIZEOF_INT * CHAR_BIT - nlz_int(x) : \
sizeof(x) <= 4 ? 32 - nlz32(x) : \ sizeof(x) <= SIZEOF_LONG ? SIZEOF_LONG * CHAR_BIT - nlz_long(x) : \
64 - nlz64(x)) SIZEOF_LONG_LONG * CHAR_BIT - nlz_long_long(x))
#else #else
# define bitsize(x) \ # define bitsize(x) \
(sizeof(x) <= 2 ? 16 - nlz16(x) : \ (sizeof(x) <= SIZEOF_INT ? SIZEOF_INT * CHAR_BIT - nlz_int(x) : \
32 - nlz32(x)) sizeof(x) <= SIZEOF_LONG ? SIZEOF_LONG * CHAR_BIT - nlz_long(x))
#endif #endif
#define U16(a) ((uint16_t)(a)) #define U16(a) ((uint16_t)(a))