Skip to content
Snippets Groups Projects
Commit 9cd66071 authored by Raymond Toy's avatar Raymond Toy
Browse files

Document the jump function and add test.

* rand-xoroshiro.lisp:
  * Rename xoroshiro-jump to random-state-jump
  * Add documentation/comments.
* tests/rng.lisp
  * Add tests for the RNG jump function.
parent 96c90caf
No related branches found
No related tags found
No related merge requests found
...@@ -18,7 +18,8 @@ ...@@ -18,7 +18,8 @@
make-random-state)) make-random-state))
(in-package "KERNEL") (in-package "KERNEL")
(export '(%random-single-float %random-double-float random-chunk init-random-state)) (export '(%random-single-float %random-double-float random-chunk init-random-state
random-state-jump))
(sys:register-lisp-feature :random-xoroshiro) (sys:register-lisp-feature :random-xoroshiro)
...@@ -470,7 +471,12 @@ ...@@ -470,7 +471,12 @@
:format-control (intl:gettext "Argument is not a positive integer or a positive float: ~S") :format-control (intl:gettext "Argument is not a positive integer or a positive float: ~S")
:format-arguments (list arg))))) :format-arguments (list arg)))))
(defun xoroshiro-jump (rng-state) ;; Jump function for the generator. See the jump function in
;; http://xoroshiro.di.unimi.it/xoroshiro128plus.c
(defun random-state-jump (&optional (rng-state *random-state*))
"Jump the RNG-STATE. This is equivalent to 2^64 calls to the
xoroshiro128+ generator. It can be used to generate 2^64
non-overlapping subsequences for parallel computations."
(declare (type random-state rng-state)) (declare (type random-state rng-state))
(let ((state (random-state-state rng-state)) (let ((state (random-state-state rng-state))
(s0-0 0) (s0-0 0)
...@@ -493,7 +499,6 @@ ...@@ -493,7 +499,6 @@
(kernel:double-float-bits (aref state 1)) (kernel:double-float-bits (aref state 1))
(setf s1-1 (logxor s1-1 (ldb (byte 32 0) x1)) (setf s1-1 (logxor s1-1 (ldb (byte 32 0) x1))
s1-0 (logxor s1-0 x0)))) s1-0 (logxor s1-0 x0))))
(format t "jump: ~D s0, s1 = ~X~8,'0X ~X~8,'0X~%" b s0-1 s0-0 s1-1 s1-0)
(xoroshiro-gen state))) (xoroshiro-gen state)))
(flet ((convert (x1 x0) (flet ((convert (x1 x0)
...@@ -504,4 +509,3 @@ ...@@ -504,4 +509,3 @@
(setf (aref state 0) (convert s0-1 s0-0)) (setf (aref state 0) (convert s0-1 s0-0))
(setf (aref state 1) (convert s1-1 s1-0))) (setf (aref state 1) (convert s1-1 s1-0)))
rng-state)) rng-state))
...@@ -55,3 +55,16 @@ ...@@ -55,3 +55,16 @@
item item
(assert-equal value (64-bit-value *test-state*)) (assert-equal value (64-bit-value *test-state*))
(assert-equal state (multiple-value-list (64-bit-rng-state *test-state*)))))) (assert-equal state (multiple-value-list (64-bit-rng-state *test-state*))))))
(define-test rng.jump
(setf *test-state*
(kernel::make-random-object :state (kernel::init-random-state #x12345678)
:rand 0
:cached-p nil))
(dolist (result '((#x291ddf8e6f6a7b67 #x1f9018a12f9e031f)
(#x88a7aa12158558d0 #xe264d785ab1472d9)
(#x207e16f73c51e7ba #x999c8a0a9a8d87c0)
(#x28f8959d3bcf5ff1 #x38091e563ab6eb98)))
(kernel:random-state-jump *test-state*)
(assert-equal result (multiple-value-list
(64-bit-rng-state *test-state*)))))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment