#REFACTORING by cg
authorClaus Gittinger <cg@exept.de>
Fri, 02 Mar 2018 14:15:56 +0100
changeset 4598 409af0c80e09
parent 4597 42570a22d794
child 4599 169bd0f282fb
#REFACTORING by cg class: RandomRDRand changed: #nextInteger class: RandomRDRand class comment/format in: #documentation #new #new:
RandomRDRand.st
--- a/RandomRDRand.st	Fri Mar 02 14:04:32 2018 +0100
+++ b/RandomRDRand.st	Fri Mar 02 14:15:56 2018 +0100
@@ -1,3 +1,5 @@
+"{ Encoding: utf8 }"
+
 "
  COPYRIGHT (c) 2014 Claus Gittinger
               All Rights Reserved
@@ -38,18 +40,19 @@
 
 documentation
 "
-    Warning: there have been discussions about the security of the intel rdgen instruction
-    and whether there are NSA backdoors built into it.
-    Linus Torwalds refuses to use it for /dev/urandom in the linux kernel, for that very reason.
-    Be sure you know what you are doing, if you use this generator for sensitive cryptographic stuff.
-    We recommend using one of the libcrypt-based generators and use this only to get additional
-    entropy for the seed.
+    This generator uses the rdgen random generator which is built into modern intel chips.
+    Before using, you should check via the isSupported query.
+
+    Warning: 
+        there have been discussions about the security of the intel rdgen instruction
+        and whether there are NSA backdoors built into it.
+        Linus Torwalds refuses to use it for /dev/urandom in the linux kernel, for that very reason.
+        Be sure you know what you are doing, if you use this generator for sensitive cryptographic stuff.
+        We recommend using one of the libcrypt-based generators and use this only to get additional
+        entropy for the seed.
 
     NO WARRANTY
 
-    This generator uses the rdgen random generator which is built into modern intel chips.
-    Before using, you should check via the isSupported query.
-
     RandomRDGen new nextInteger
 
     [see also:]
@@ -70,17 +73,18 @@
 
 new
     self isSupported ifFalse:[ self error:'this generator needs a cpu with rdgen instruction' ].
-    ^ self basicNew 
-        initialize
+    ^ self basicNew initialize
+
+    "
+     self new nextInteger
+    "
 !
 
 new:seed
-    "seed is actualy ignored"
+    "seed is actually ignored"
 
     self isSupported ifFalse:[ self error:'this generator needs a cpu with rdgen instruction' ].
-    ^ self basicNew 
-        initialize;
-        seed:seed
+    ^ self basicNew initialize; seed:seed
 ! !
 
 !RandomRDRand class methodsFor:'queries'!
@@ -120,20 +124,26 @@
      even though the cpuid instruction says that it is available"
 
 %{
-    unsigned int r = 0;
+    unsigned INT r = 0;
     int cf;
 
     do {
-#if defined(__i386__) && defined(__GNUC__) && (__GNUC__ >= 2)
-        // Encoding of rdrand %eax
-        asm(".byte 0x0F, 0xC7, 0xF0; adcl $0,%1" :
-            "=a" (r), "=r" (cf) : "0" (r), "1" (cf) : "cc");
-
+#ifdef USE_DRAND64
+        cf = _rdrand64_step(&r);
 #else
 # ifdef USE_DRAND32
         cf = _rdrand32_step(&r);
 # else
+#  if defined(__x86__) && defined(__GNUC__) && (__GNUC__ >= 2)
+        // Encoding of rdrand %eax
+        asm(".byte 0x0F, 0xC7, 0xF0; adcl $0,%1" 
+            : "=a" (r), "=r" (cf) 
+            : "0" (r), "1" (cf) 
+            : "cc");
+
+#  else
         goto unsupported;
+#  endif
 # endif
 #endif
     } while (cf != 0);