... |
... |
@@ -2,3 +2,46 @@ |
2
|
2
|
;; *SOFTWARE-VERSION* from the LISP package to the SYSTEM package.
|
3
|
3
|
(ext:without-package-locks
|
4
|
4
|
(unintern 'lisp::*software-version* "LISP"))
|
|
5
|
+
|
|
6
|
+#+(or random-mt19937 random-xoroshiro)
|
|
7
|
+(in-package "C")
|
|
8
|
+#+(or random-mt19937 random-xoroshiro)
|
|
9
|
+(deftransform random ((num &optional state)
|
|
10
|
+ ((integer 1 #.(expt 2 32)) &optional *))
|
|
11
|
+ _N"use inline (unsigned-byte 32) operations"
|
|
12
|
+ (let* ((num-type (continuation-type num))
|
|
13
|
+ (num-high (cond ((numeric-type-p num-type)
|
|
14
|
+ (numeric-type-high num-type))
|
|
15
|
+ ((union-type-p num-type)
|
|
16
|
+ ;; Find the maximum of the union type. We
|
|
17
|
+ ;; know this works because if we're in this
|
|
18
|
+ ;; routine, NUM must be a subtype of
|
|
19
|
+ ;; (INTEGER 1 2^32), so each member of the
|
|
20
|
+ ;; union must be a subtype too.
|
|
21
|
+ (reduce #'max (union-type-types num-type)
|
|
22
|
+ :key #'numeric-type-high))
|
|
23
|
+ (t
|
|
24
|
+ (give-up)))))
|
|
25
|
+ ;; Rather than doing (rem (random-chunk) num-high), we do,
|
|
26
|
+ ;; essentially, (rem (* num-high (random-chunk)) #x100000000). I
|
|
27
|
+ ;; (rtoy) believe this approach doesn't have the bias issue with
|
|
28
|
+ ;; doing rem. This method works by treating (random-chunk) as if
|
|
29
|
+ ;; it were a 32-bit fraction between 0 and 1, exclusive. Multiply
|
|
30
|
+ ;; this by num-high to get a random number between 0 and num-high,
|
|
31
|
+ ;; This should have no bias.
|
|
32
|
+ (cond ((constant-continuation-p num)
|
|
33
|
+ (if (= num-high (expt 2 32))
|
|
34
|
+ '(random-chunk (or state *random-state*))
|
|
35
|
+ '(values (bignum::%multiply
|
|
36
|
+ (random-chunk (or state *random-state*))
|
|
37
|
+ num))))
|
|
38
|
+ ((< num-high (expt 2 32))
|
|
39
|
+ '(values (bignum::%multiply (random-chunk (or state *random-state*))
|
|
40
|
+ num)))
|
|
41
|
+ ((= num-high (expt 2 32))
|
|
42
|
+ '(if (= num (expt 2 32))
|
|
43
|
+ (random-chunk (or state *random-state*))
|
|
44
|
+ (values (bignum::%multiply (random-chunk (or state *random-state*))
|
|
45
|
+ num))))
|
|
46
|
+ (t
|
|
47
|
+ (error (intl:gettext "Shouldn't happen")))))) |