RandomRDRand.st
author Claus Gittinger <cg@exept.de>
Sat, 02 May 2020 21:40:13 +0200
changeset 5476 7355a4b11cb6
parent 5080 aeb43e5edb85
permissions -rw-r--r--
#FEATURE by cg class: Socket class added: #newTCPclientToHost:port:domain:domainOrder:withTimeout: changed: #newTCPclientToHost:port:domain:withTimeout:
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4598
409af0c80e09 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4595
diff changeset
     1
"{ Encoding: utf8 }"
409af0c80e09 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4595
diff changeset
     2
4322
97661b0035ed #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3416
diff changeset
     3
"
97661b0035ed #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3416
diff changeset
     4
 COPYRIGHT (c) 2014 Claus Gittinger
4667
35e1ebb8020e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4598
diff changeset
     5
	      All Rights Reserved
4322
97661b0035ed #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3416
diff changeset
     6
97661b0035ed #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3416
diff changeset
     7
 This software is furnished under a license and may be used
97661b0035ed #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3416
diff changeset
     8
 only in accordance with the terms of that license and with the
97661b0035ed #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3416
diff changeset
     9
 inclusion of the above copyright notice.   This software may not
97661b0035ed #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3416
diff changeset
    10
 be provided or otherwise made available to, or used by, any
97661b0035ed #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3416
diff changeset
    11
 other person.  No title to or ownership of the software is
97661b0035ed #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3416
diff changeset
    12
 hereby transferred.
97661b0035ed #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3416
diff changeset
    13
"
3405
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    14
"{ Package: 'stx:libbasic2' }"
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    15
4322
97661b0035ed #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3416
diff changeset
    16
"{ NameSpace: Smalltalk }"
97661b0035ed #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3416
diff changeset
    17
3405
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    18
Object subclass:#RandomRDRand
4926
0d707ba99ea5 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4925
diff changeset
    19
	instanceVariableNames:''
3405
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
	classVariableNames:''
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    21
	poolDictionaries:''
4595
39b2f30679c0 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4322
diff changeset
    22
	category:'Magnitude-Numbers-Random'
3405
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    23
!
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    25
!RandomRDRand class methodsFor:'documentation'!
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    26
4322
97661b0035ed #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3416
diff changeset
    27
copyright
97661b0035ed #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3416
diff changeset
    28
"
97661b0035ed #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3416
diff changeset
    29
 COPYRIGHT (c) 2014 Claus Gittinger
4667
35e1ebb8020e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4598
diff changeset
    30
	      All Rights Reserved
4322
97661b0035ed #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3416
diff changeset
    31
97661b0035ed #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3416
diff changeset
    32
 This software is furnished under a license and may be used
97661b0035ed #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3416
diff changeset
    33
 only in accordance with the terms of that license and with the
97661b0035ed #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3416
diff changeset
    34
 inclusion of the above copyright notice.   This software may not
97661b0035ed #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3416
diff changeset
    35
 be provided or otherwise made available to, or used by, any
97661b0035ed #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3416
diff changeset
    36
 other person.  No title to or ownership of the software is
97661b0035ed #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3416
diff changeset
    37
 hereby transferred.
97661b0035ed #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3416
diff changeset
    38
"
97661b0035ed #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3416
diff changeset
    39
!
97661b0035ed #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3416
diff changeset
    40
3405
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    41
documentation
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    42
"
4598
409af0c80e09 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4595
diff changeset
    43
    This generator uses the rdgen random generator which is built into modern intel chips.
409af0c80e09 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4595
diff changeset
    44
    Before using, you should check via the isSupported query.
409af0c80e09 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4595
diff changeset
    45
4667
35e1ebb8020e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4598
diff changeset
    46
    Warning:
5067
2fbc7ccd7e68 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4927
diff changeset
    47
        there have been discussions about the security of the intel rdgen instruction
2fbc7ccd7e68 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4927
diff changeset
    48
        and whether there are NSA backdoors built into it.
2fbc7ccd7e68 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4927
diff changeset
    49
        Linus Torwalds refuses to use it for /dev/urandom in the linux kernel, for that very reason.
2fbc7ccd7e68 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4927
diff changeset
    50
        Be sure you know what you are doing, if you use this generator for sensitive cryptographic stuff.
2fbc7ccd7e68 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4927
diff changeset
    51
        We recommend using one of the libcrypt-based generators and use this only to get additional
2fbc7ccd7e68 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4927
diff changeset
    52
        entropy for the seed.
3405
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
5067
2fbc7ccd7e68 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4927
diff changeset
    54
    Warning2:
2fbc7ccd7e68 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4927
diff changeset
    55
        the initial release of the AMD Ryzen3000 (without BIOS update) has a bug in returning the same
2fbc7ccd7e68 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4927
diff changeset
    56
        (all 1s) random number every time.
2fbc7ccd7e68 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4927
diff changeset
    57
        The code here checks for that an raises an error in the constructor if that bug is detected.
2fbc7ccd7e68 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4927
diff changeset
    58
        Be sure to catch this exception in your code and fall back to another random generator if raised.
2fbc7ccd7e68 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4927
diff changeset
    59
        
3405
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    60
    NO WARRANTY
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    61
5080
aeb43e5edb85 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5067
diff changeset
    62
    [usage:]
aeb43e5edb85 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5067
diff changeset
    63
        RandomRDRand new nextInteger
3405
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    64
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    65
    [see also:]
5067
2fbc7ccd7e68 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4927
diff changeset
    66
        RandomGenerator - the default; uses the machine's /dev/random if available
2fbc7ccd7e68 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4927
diff changeset
    67
        Random  - fast, but generates less quality random numbers
2fbc7ccd7e68 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4927
diff changeset
    68
        RandomTT800 - another random generator
2fbc7ccd7e68 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4927
diff changeset
    69
        RandomParkMiller - another random generator
2fbc7ccd7e68 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4927
diff changeset
    70
        RandomMT19937 - another random generator
2fbc7ccd7e68 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4927
diff changeset
    71
        RandomKISS - another random generator
2fbc7ccd7e68 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4927
diff changeset
    72
        exept:libcrypt - a library containing more stuff based on hashes and cyphers
3405
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    73
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    74
    [author:]
5067
2fbc7ccd7e68 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4927
diff changeset
    75
        Claus Gittinger.
3405
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    76
"
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    77
! !
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    78
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    79
!RandomRDRand class methodsFor:'instance creation'!
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    80
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    81
new
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    82
    self isSupported ifFalse:[ self error:'this generator needs a cpu with rdgen instruction' ].
4598
409af0c80e09 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4595
diff changeset
    83
    ^ self basicNew initialize
409af0c80e09 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4595
diff changeset
    84
409af0c80e09 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4595
diff changeset
    85
    "
409af0c80e09 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4595
diff changeset
    86
     self new nextInteger
409af0c80e09 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4595
diff changeset
    87
    "
3405
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    88
!
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    90
new:seed
4598
409af0c80e09 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4595
diff changeset
    91
    "seed is actually ignored"
3405
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    92
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    93
    self isSupported ifFalse:[ self error:'this generator needs a cpu with rdgen instruction' ].
4598
409af0c80e09 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4595
diff changeset
    94
    ^ self basicNew initialize; seed:seed
3405
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    95
! !
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    96
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    97
!RandomRDRand class methodsFor:'queries'!
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    98
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    99
isSupported
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   100
    "true if this architecture supports hardware random numbers"
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   101
4925
3b6b894d2664 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4887
diff changeset
   102
    ^ (OperatingSystem getSystemInfo at:#extendedInstructions ifAbsent:#())
3b6b894d2664 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4887
diff changeset
   103
              includes:#rdmd
3405
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   104
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   105
    "
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   106
     self isSupported
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   107
    "
4925
3b6b894d2664 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4887
diff changeset
   108
3b6b894d2664 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4887
diff changeset
   109
    "Modified: / 28-03-2019 / 10:59:32 / Stefan Vogel"
3405
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   110
! !
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   111
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   112
!RandomRDRand methodsFor:'initialization'!
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   113
4926
0d707ba99ea5 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4925
diff changeset
   114
initialize
5067
2fbc7ccd7e68 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4927
diff changeset
   115
    "check for Ryzen RDRAND bug (returning the same all-ones everytime)"
2fbc7ccd7e68 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4927
diff changeset
   116
2fbc7ccd7e68 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4927
diff changeset
   117
    |i1 i2|
2fbc7ccd7e68 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4927
diff changeset
   118
2fbc7ccd7e68 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4927
diff changeset
   119
    i1 := self nextInteger.
2fbc7ccd7e68 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4927
diff changeset
   120
    i2 := self nextInteger.
2fbc7ccd7e68 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4927
diff changeset
   121
    (i1 = i2) ifTrue:[
2fbc7ccd7e68 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4927
diff changeset
   122
        self error:'Ryzen RDRand bug detected'
2fbc7ccd7e68 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4927
diff changeset
   123
    ].
2fbc7ccd7e68 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4927
diff changeset
   124
2fbc7ccd7e68 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4927
diff changeset
   125
    "
2fbc7ccd7e68 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4927
diff changeset
   126
     self new
2fbc7ccd7e68 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4927
diff changeset
   127
    "
2fbc7ccd7e68 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4927
diff changeset
   128
2fbc7ccd7e68 #QUALITY by cg
Claus Gittinger <cg@exept.de>
parents: 4927
diff changeset
   129
    "Modified (comment): / 25-07-2019 / 13:30:28 / Claus Gittinger"
4926
0d707ba99ea5 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4925
diff changeset
   130
!
0d707ba99ea5 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4925
diff changeset
   131
3405
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   132
seed:seed
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   133
    "/ ignored
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   134
! !
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   135
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   136
!RandomRDRand methodsFor:'random numbers'!
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   137
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   138
nextBoolean
3416
a71369580f84 comments
Claus Gittinger <cg@exept.de>
parents: 3405
diff changeset
   139
    "generates a boolean random"
3405
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   140
4927
8e944da8fc20 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 4926
diff changeset
   141
    ^ self nextInteger bitTest:1
4926
0d707ba99ea5 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4925
diff changeset
   142
0d707ba99ea5 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4925
diff changeset
   143
    "
0d707ba99ea5 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4925
diff changeset
   144
        |bag rng|
0d707ba99ea5 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4925
diff changeset
   145
0d707ba99ea5 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4925
diff changeset
   146
        rng := self new.
0d707ba99ea5 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4925
diff changeset
   147
        bag := Bag new.
0d707ba99ea5 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4925
diff changeset
   148
        100000 timesRepeat:[
0d707ba99ea5 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4925
diff changeset
   149
            bag add:rng nextBoolean.
0d707ba99ea5 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4925
diff changeset
   150
        ].
0d707ba99ea5 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4925
diff changeset
   151
        bag
0d707ba99ea5 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4925
diff changeset
   152
    "
3405
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   153
!
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   154
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   155
nextInteger
4926
0d707ba99ea5 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4925
diff changeset
   156
    "generates the next integer in 0..MAXINT.
3405
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   157
     Notice, it may raise an illegal instruction exception on some cpu chips,
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   158
     even though the cpuid instruction says that it is available"
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   159
4667
35e1ebb8020e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4598
diff changeset
   160
    |cfStillSet|
35e1ebb8020e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4598
diff changeset
   161
3405
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   162
%{
4598
409af0c80e09 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4595
diff changeset
   163
    unsigned INT r = 0;
4926
0d707ba99ea5 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4925
diff changeset
   164
    unsigned char cf;
4667
35e1ebb8020e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4598
diff changeset
   165
    int count = 50;
3405
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   166
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   167
    do {
4598
409af0c80e09 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4595
diff changeset
   168
#ifdef USE_DRAND64
4887
8c49f1189e7c #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 4667
diff changeset
   169
        cf = _rdrand64_step(&r);
4925
3b6b894d2664 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4887
diff changeset
   170
#elif defined(USE_DRAND32)
4887
8c49f1189e7c #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 4667
diff changeset
   171
        cf = _rdrand32_step(&r);
4925
3b6b894d2664 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4887
diff changeset
   172
#elif defined(__i386__) && defined(__GNUC__) && (__GNUC__ >= 2)
4887
8c49f1189e7c #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 4667
diff changeset
   173
        // Encoding of rdrand %eax
4926
0d707ba99ea5 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4925
diff changeset
   174
        asm(".byte 0x0F, 0xC7, 0xF0; setc %1"
4887
8c49f1189e7c #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 4667
diff changeset
   175
            : "=a" (r), "=r" (cf)
8c49f1189e7c #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 4667
diff changeset
   176
            : "0" (r), "1" (cf)
8c49f1189e7c #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 4667
diff changeset
   177
            : "cc");
4598
409af0c80e09 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 4595
diff changeset
   178
4925
3b6b894d2664 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4887
diff changeset
   179
#elif defined(__x86_64__) && defined(__GNUC__) && (__GNUC__ >= 2)
4887
8c49f1189e7c #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 4667
diff changeset
   180
        // Encoding of rdrand %rax
4926
0d707ba99ea5 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4925
diff changeset
   181
        asm(".byte 0x48, 0x0F, 0xC7, 0xF0; setc %1"
4887
8c49f1189e7c #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 4667
diff changeset
   182
            : "=a" (r), "=r" (cf)
8c49f1189e7c #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 4667
diff changeset
   183
            : "0" (r), "1" (cf)
8c49f1189e7c #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 4667
diff changeset
   184
            : "cc");
4667
35e1ebb8020e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4598
diff changeset
   185
4925
3b6b894d2664 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4887
diff changeset
   186
#else
4887
8c49f1189e7c #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 4667
diff changeset
   187
        goto unsupported;
3405
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   188
#endif
4926
0d707ba99ea5 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4925
diff changeset
   189
    } while ((cf == 0) && (--count > 0));
0d707ba99ea5 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4925
diff changeset
   190
    if (cf != 0) {
4887
8c49f1189e7c #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 4667
diff changeset
   191
        RETURN (__MKUINT(r));
4667
35e1ebb8020e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4598
diff changeset
   192
    }
35e1ebb8020e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4598
diff changeset
   193
    cfStillSet = true;
3405
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   194
unsupported: ;
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   195
%}.
4887
8c49f1189e7c #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 4667
diff changeset
   196
    cfStillSet == true ifTrue:[
4927
8e944da8fc20 #QUALITY by stefan
Stefan Vogel <sv@exept.de>
parents: 4926
diff changeset
   197
        self primitiveFailed:'carry flag not set after 50 tries'
4667
35e1ebb8020e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4598
diff changeset
   198
    ].
3405
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   199
    self primitiveFailed:'unsupported on this architecture'
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   200
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   201
    "
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   202
     self new nextInteger
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   203
    "
4925
3b6b894d2664 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4887
diff changeset
   204
3b6b894d2664 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4887
diff changeset
   205
    "Modified: / 28-03-2019 / 10:46:29 / Stefan Vogel"
3405
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   206
! !
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   207
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   208
!RandomRDRand class methodsFor:'documentation'!
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   209
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   210
version
4322
97661b0035ed #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3416
diff changeset
   211
    ^ '$Header$'
3405
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   212
!
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   213
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   214
version_CVS
4322
97661b0035ed #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3416
diff changeset
   215
    ^ '$Header$'
3405
84393adea27c initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   216
! !
4887
8c49f1189e7c #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 4667
diff changeset
   217