+(defun elapsed-cycles (h0 l0 h1 l1)
+ (declare (ignorable h0 l0 h1 l1))
+ #!+cycle-counter
+ (+ (ash (- h1 h0) 32)
+ (- l1 l0))
+ #!-cycle-counter
+ nil)
+(declaim (inline read-cycle-counter))
+(defun read-cycle-counter ()
+ #!+cycle-counter
+ (sb!vm::%read-cycle-counter)
+ #!-cycle-counter
+ (values 0 0))
+
+;;; This is so that we don't have to worry about the vagaries of
+;;; floating point printing, or about conversions to floats dropping
+;;; or introducing decimals, which are liable to imply wrong precision.
+(defun format-microseconds (stream usec &optional colonp atp)
+ (declare (ignore colonp))
+ (%format-decimal stream usec 6)
+ (unless atp
+ (write-string " seconds" stream)))
+
+(defun format-milliseconds (stream usec &optional colonp atp)
+ (declare (ignore colonp))
+ (%format-decimal stream usec 3)
+ (unless atp
+ (write-string " seconds" stream)))
+
+(defun %format-decimal (stream number power)
+ (declare (stream stream)
+ (integer number power))
+ (when (minusp number)
+ (write-char #\- stream)
+ (setf number (- number)))
+ (let ((scale (expt 10 power)))
+ (labels ((%fraction (fraction)
+ (if (zerop fraction)
+ (%zeroes)
+ (let ((scaled (* 10 fraction)))
+ (loop while (< scaled scale)
+ do (write-char #\0 stream)
+ (setf scaled (* scaled 10)))))
+ (format stream "~D" fraction))
+ (%zeroes ()
+ (let ((scaled (/ scale 10)))
+ (write-char #\0 stream)
+ (loop while (> scaled 1)
+ do (write-char #\0 stream)
+ (setf scaled (/ scaled 10))))))
+ (cond ((zerop number)
+ (write-string "0." stream)
+ (%zeroes))
+ ((< number scale)
+ (write-string "0." stream)
+ (%fraction number))
+ ((= number scale)
+ (write-string "1." stream)
+ (%zeroes))
+ ((> number scale)
+ (multiple-value-bind (whole fraction) (floor number scale)
+ (format stream "~D." whole)
+ (%fraction fraction))))))
+ nil)
+