[前][次][番号順一覧][スレッド一覧]

ruby-changes:29924

From: akr <ko1@a...>
Date: Mon, 15 Jul 2013 10:49:43 +0900 (JST)
Subject: [ruby-changes:29924] akr:r41976 (trunk): * bignum.c (nlz16): Use __builtin_clz if possible.

akr	2013-07-15 10:49:32 +0900 (Mon, 15 Jul 2013)

  New Revision: 41976

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=41976

  Log:
    * 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.

  Modified files:
    trunk/ChangeLog
    trunk/bignum.c
    trunk/configure.in

Index: configure.in
===================================================================
--- configure.in	(revision 41975)
+++ configure.in	(revision 41976)
@@ -1849,6 +1849,9 @@ if test "${AS_TR_SH(rb_cv_builtin_$1)}" https://github.com/ruby/ruby/blob/trunk/configure.in#L1849
 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
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 41975)
+++ ChangeLog	(revision 41976)
@@ -1,3 +1,13 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Mon Jul 15 10:45:09 2013  Tanaka Akira  <akr@f...>
+
+	* 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  <akr@f...>
 
 	* bignum.c (power_cache_get_power): Use bitsize instead of ceil_log2.
Index: bignum.c
===================================================================
--- bignum.c	(revision 41975)
+++ bignum.c	(revision 41976)
@@ -123,6 +123,10 @@ static void bigdivmod(VALUE x, VALUE y, https://github.com/ruby/ruby/blob/trunk/bignum.c#L123
 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) https://github.com/ruby/ruby/blob/trunk/bignum.c#L134
     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) https://github.com/ruby/ruby/blob/trunk/bignum.c#L155
     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) https://github.com/ruby/ruby/blob/trunk/bignum.c#L178
     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) https://github.com/ruby/ruby/blob/trunk/bignum.c#L186
 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) https://github.com/ruby/ruby/blob/trunk/bignum.c#L200
     y = x >>  2; if (y) {n -=  2; x = y;}
     y = x >>  1; if (y) {return n - 2;}
     return (int)(n - x);
+#endif
 }
 #endif
 

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]