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

ruby-changes:41840

From: akr <ko1@a...>
Date: Wed, 24 Feb 2016 21:29:42 +0900 (JST)
Subject: [ruby-changes:41840] akr:r53914 (trunk): * random.c (limited_rand): Add a specialized path for the limit fits in 32 bit.

akr	2016-02-24 21:30:20 +0900 (Wed, 24 Feb 2016)

  New Revision: 53914

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=53914

  Log:
    * random.c (limited_rand): Add a specialized path for the limit fits in 32 bit.

  Modified files:
    trunk/ChangeLog
    trunk/random.c
Index: random.c
===================================================================
--- random.c	(revision 53913)
+++ random.c	(revision 53914)
@@ -826,21 +826,31 @@ static unsigned long https://github.com/ruby/ruby/blob/trunk/random.c#L826
 limited_rand(struct MT *mt, unsigned long limit)
 {
     /* mt must be initialized */
-    int i;
     unsigned long val, mask;
 
     if (!limit) return 0;
     mask = make_mask(limit);
-  retry:
-    val = 0;
-    for (i = SIZEOF_LONG/SIZEOF_INT32-1; 0 <= i; i--) {
-        if ((mask >> (i * 32)) & 0xffffffff) {
-            val |= (unsigned long)genrand_int32(mt) << (i * 32);
-            val &= mask;
-            if (limit < val)
-                goto retry;
+
+#if 4 < SIZEOF_LONG
+    if (0xffffffff < limit) {
+        int i;
+      retry:
+        val = 0;
+        for (i = SIZEOF_LONG/SIZEOF_INT32-1; 0 <= i; i--) {
+            if ((mask >> (i * 32)) & 0xffffffff) {
+                val |= (unsigned long)genrand_int32(mt) << (i * 32);
+                val &= mask;
+                if (limit < val)
+                    goto retry;
+            }
         }
+        return val;
     }
+#endif
+
+    do {
+        val = genrand_int32(mt) & mask;
+    } while (limit < val);
     return val;
 }
 
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 53913)
+++ ChangeLog	(revision 53914)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Feb 24 21:03:04 2016  Tanaka Akira  <akr@f...>
+
+	* random.c (limited_rand): Add a specialized path for the limit fits
+	  in 32 bit.
+
 Tue Feb 23 21:52:24 2016  Martin Duerst  <duerst@i...>
 
 	* enc/unicode/case-folding.rb, casefold.h: Outputting actual titlecase

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

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