[PATCH] [4/15] Optimize mersenne twister

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Inline the MT fast path.
---
 mt.c |   21 ++++++++-------------
 mt.h |   21 +++++++++++++++++++--
 2 files changed, 27 insertions(+), 15 deletions(-)

Index: numactl-2.0.0/mt.c
===================================================================
--- numactl-2.0.0.orig/mt.c
+++ numactl-2.0.0/mt.c
@@ -1,9 +1,8 @@
 /* Mersenne twister implementation from Michael Brundage. Public Domain. 
    MT is a very fast pseudo random number generator. This version works
-   on 32bit words.  Minor changes by AK. */
+   on 32bit words.  Changes by AK. */
 #include <stdlib.h>
 #include "mt.h"
-#define MT_LEN       624
 
 int mt_index;
 unsigned int mt_buffer[MT_LEN];
@@ -25,15 +24,13 @@ void mt_init(void) 
 #define TWIST(b,i,j)    ((b)[i] & UPPER_MASK) | ((b)[j] & LOWER_MASK)
 #define MAGIC(s)        (((s)&1)*MATRIX_A)
 
-unsigned int mt_random(void) 
+void mt_refill(void)
 {
-    unsigned int * b = mt_buffer;
-    int idx = mt_index;
-    unsigned int s;
-    int i;
-	
-    if (idx == MT_LEN*sizeof(unsigned int)) {
-        idx = 0;
+	int i;
+	unsigned int s;
+	unsigned int * b = mt_buffer;
+
+	mt_index = 0;
         i = 0;
         for (; i < MT_IB; i++) {
             s = TWIST(b, i, i+1);
@@ -46,7 +43,5 @@ unsigned int mt_random(void) 
         
         s = TWIST(b, MT_LEN-1, 0);
         b[MT_LEN-1] = b[MT_IA-1] ^ (s >> 1) ^ MAGIC(s);
-    }
-    mt_index = idx + sizeof(unsigned int);
-    return *(unsigned int *)((unsigned char *)b + idx);
 }
+
Index: numactl-2.0.0/mt.h
===================================================================
--- numactl-2.0.0.orig/mt.h
+++ numactl-2.0.0/mt.h
@@ -1,3 +1,20 @@
-void mt_init(void);
-unsigned int mt_random(void);
+#define MT_LEN	     624
 
+extern void mt_init(void);
+extern void mt_refill();
+
+extern int mt_index;
+extern unsigned int mt_buffer[MT_LEN];
+
+static inline unsigned int mt_random(void)
+{
+    unsigned int * b = mt_buffer;
+    int idx = mt_index;
+
+    if (idx == MT_LEN*sizeof(unsigned int)) {
+	    mt_refill();
+	    idx = 0;
+    }
+    mt_index += sizeof(unsigned int);
+    return *(unsigned int *)((unsigned char *)b + idx);
+}
--
To unsubscribe from this list: send the line "unsubscribe linux-numa" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux SCSI]     [Devices]

  Powered by Linux