build/pcg.ros @ cd52795d9ecd default tip

Update URLs
author Steve Losh <steve@stevelosh.com>
date Tue, 14 Jan 2020 19:53:05 -0500
parents d997d6e268a3
children (none)
#!/bin/sh
#|-*- mode:lisp -*-|#
#|
exec ros -Q -- $0 "$@"
|#


(unless (find-package :pcg)
  (ql:quickload '(:cl-pcg) :silent t))

(unless (find-package :cl-variates)
  (ql:quickload '(:cl-variates) :silent t))

(require 'sb-rotate-byte)

(declaim (optimize (debug 0) (safety 0) (speed 3)))

(let ((*standard-output* (make-broadcast-stream)) ; shut
      (*error-output* (make-broadcast-stream))) ; up
  (asdf:load-system 'cl-pcg :force t)
  (asdf:load-system 'cl-variates :force t))


(deftype u32 () '(unsigned-byte 32))
(defconstant +buffer-size+ (* 1 1024))

(defmacro define-dumper (name (generator-name make-generator-form) generate-form)
  (pcg.quickutils:with-gensyms
    (output-stream buffer i)
    `(defun ,name ()
       (with-open-file (,output-stream "/dev/stdout"
                                       :direction :output
                                       :if-exists :append
                                       :element-type 'u32)
         (loop
           :with ,generator-name = ,make-generator-form
           :with ,buffer = (make-array +buffer-size+
                                       :element-type t
                                       :initial-element 0)
           :do (loop :for ,i :from 0 :below +buffer-size+
                     :do (setf (aref ,buffer ,i) ,generate-form))
           (write-sequence ,buffer ,output-stream))))))

(define-dumper variates-dump-data
    (gen (cl-variates:make-random-number-generator (random (expt 2 32))))
  (cl-variates:integer-random gen 0 (expt 2 32)) )

(define-dumper pcg-dump-data
    (pcg (pcg::make-pcg :seed (random (expt 2 32)) :stream-id 0))
  (pcg::pcg-random% pcg))

(define-dumper random-dump-data
    (rs (make-random-state t))
  (random (expt 2 32) rs))

(defun main (&optional generator &rest argv)
  (declare (ignore argv))
  (setf *random-state* (make-random-state t))
  (handler-case
      (cond
        ((string= generator "random") (random-dump-data))
        ((string= generator "variates") (variates-dump-data))
        (t (pcg-dump-data)))
    (stream-error () t))
  t)