Raymond Toy pushed to branch rtoy-xoro at cmucl / cmucl

Commits:

1 changed file:

Changes:

  • src/code/rand-xoroshiro.lisp
    ... ... @@ -79,6 +79,7 @@
    79 79
     
    
    80 80
     (defstruct (xoro-random-state
    
    81 81
     	     (:constructor make-xoroshiro-object)
    
    82
    +	     (:print-function %print-xoro-state)
    
    82 83
     	     (:make-load-form-fun :just-dump-it-normally))
    
    83 84
       ;; The state of the RNG.  The actual algorithm uses 2 64-bit words
    
    84 85
       ;; of state.  To reduce consing, we use an array of double-float's
    
    ... ... @@ -95,6 +96,46 @@
    95 96
       ;; generate a new 64-bit result.
    
    96 97
       (cached-p nil :type (member t nil)))
    
    97 98
     
    
    99
    +(defun %print-xoro-state (rng-state stream depth)
    
    100
    +  (declare (ignore depth))
    
    101
    +  ;; Basically the same as the default structure printer, but we want
    
    102
    +  ;; to print the state as an array of integers instead of doubles,
    
    103
    +  ;; because it's a bit confusing to see the state as doubles.
    
    104
    +  (let ((state (xoro-random-state-state rng-state)))
    
    105
    +    (pprint-logical-block (stream nil :prefix "#S(" :suffix ")")
    
    106
    +      (prin1 'xoro-random-state stream)
    
    107
    +      (write-char #\space stream)
    
    108
    +      (pprint-indent :block 2 stream)
    
    109
    +      (pprint-newline :linear stream)
    
    110
    +      (prin1 :state stream)
    
    111
    +      (write-char #\space stream)
    
    112
    +      (pprint-newline :miser stream)
    
    113
    +      (pprint-logical-block (stream nil :prefix "#.(" :suffix ")")
    
    114
    +	(prin1 'init-xoro-state stream)
    
    115
    +	(write-char #\space stream)
    
    116
    +	(prin1 (make-array 4 :element-type '(unsigned-byte 32)
    
    117
    +			 :initial-contents (list (ldb (byte 32 0)
    
    118
    +						      (double-float-high-bits (aref state 0)))
    
    119
    +						 (double-float-low-bits (aref state 0))
    
    120
    +						 (ldb (byte 32 0)
    
    121
    +						      (double-float-high-bits (aref state 1)))
    
    122
    +						 (double-float-low-bits (aref state 1))))
    
    123
    +	       stream))
    
    124
    +      (write-char #\space stream)
    
    125
    +      (pprint-newline :linear stream)
    
    126
    +
    
    127
    +      (prin1 :rand stream)
    
    128
    +      (write-char #\space stream)
    
    129
    +      (pprint-newline :miser stream)
    
    130
    +      (prin1 (xoro-random-state-rand rng-state) stream)
    
    131
    +      (write-char #\space stream)
    
    132
    +      (pprint-newline :linear stream)
    
    133
    +
    
    134
    +      (prin1 :cached-p stream)
    
    135
    +      (write-char #\space stream)
    
    136
    +      (pprint-newline :miser stream)
    
    137
    +      (prin1 (xoro-random-state-cached-p rng-state) stream))))
    
    138
    +
    
    98 139
     (defvar *xoro-random-state*
    
    99 140
       (make-xoroshiro-object))
    
    100 141
     
    
    ... ... @@ -396,11 +437,3 @@
    396 437
           (setf (aref state 1) (convert s1-1 s1-0)))
    
    397 438
           rng-state))
    
    398 439
     
    399
    -(defun print-xoro-state (rng-state)
    
    400
    -  (let ((state (xoro-random-state-state rng-state)))
    
    401
    -    (flet ((v (x)
    
    402
    -	     (multiple-value-bind (hi lo)
    
    403
    -		 (kernel:double-float-bits x)
    
    404
    -	       (logior (ash (ldb (byte 32 0) hi) 32)
    
    405
    -		       lo))))
    
    406
    -      (format t "~16,'0x ~16,'0x" (v (aref state 0)) (v (aref state 1))))))
    \ No newline at end of file