utils.lisp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. ;;; -*- Mode: Lisp; show-trailing-whitespace: t; Base: 10; indent-tabs: nil; Syntax: ANSI-Common-Lisp; Package: UTILS; -*-
  2. ;;; Copyright (c) 2013, Mark VandenBrink. All rights reserved.
  3. (in-package #:utils)
  4. (eval-when (:compile-toplevel :load-toplevel :execute)
  5. (pushnew :INSTRUMENT-MEMOIZED *features*)
  6. (defvar *standard-optimize-settings* '(optimize (speed 3) (safety 0) (space 0) (debug 0))))
  7. (defparameter *break-on-warn-user* nil "set to T if you'd like to stop in warn-user")
  8. (defun warn-user (format-string &rest args)
  9. "Print a warning error to *ERROR-OUTPUT* and continue"
  10. (when *break-on-warn-user* (break "Breaking in WARN-USER"))
  11. (format *error-output* "~&********************************************************************************~%")
  12. #+CCL (format *error-output* "~&WARNING in ~a:: " (ccl::%last-fn-on-stack 1))
  13. (apply #'format *error-output* format-string args)
  14. (format *error-output* "~&**********************************************************************************~%"))
  15. (defparameter *max-raw-bytes-print-len* 10 "Max number of octets to print from an array")
  16. (defun printable-array (array &optional (max-len *max-raw-bytes-print-len*))
  17. "Given an array, return a string of the first *MAX-RAW-BYTES-PRINT-LEN* bytes"
  18. (declare #.utils:*standard-optimize-settings*)
  19. (let* ((len (length array))
  20. (print-len (min len max-len))
  21. (printable-array (make-array print-len :displaced-to array)))
  22. (declare (fixnum max-len len)
  23. (type (array (unsigned-byte 8) 1) array))
  24. (format nil "[~:d of ~:d bytes] <~x>" print-len len printable-array)))
  25. (defmacro upto-null (string)
  26. "Trim STRING to end at first NULL found"
  27. `(subseq ,string 0 (position #\Null ,string)))
  28. (defun dump-data (file-name data)
  29. (with-open-file (f file-name :direction :output :if-exists :supersede :element-type '(unsigned-byte 8))
  30. (write-sequence data f)))
  31. (defmacro redirect (filename &rest body)
  32. "Temporarily set *STANDARD-OUTPUT* to FILENAME and execute BODY."
  33. `(let ((*standard-output* (open ,filename :direction :output :if-does-not-exist :create :if-exists :supersede)))
  34. ,@body
  35. (finish-output *standard-output*)))
  36. (defun get-bitmask(start width)
  37. "Create a bit mask that begins at bit START (31 is MSB) and is WIDTH bits wide.
  38. Example: (get-bitmask 31 11) -->> #xffe00000"
  39. (declare #.utils:*standard-optimize-settings*)
  40. (ash (- (ash 1 width) 1) (- (1+ start) width)))
  41. (defmacro get-bitfield (int start width)
  42. "Extract WIDTH bits from INT starting at START
  43. Example: (get-bitfield #xFFFBB240 31 11) -->> #x7ff.
  44. The above will expand to (ash (logand #xFFFBB240 #xFFE00000) -21) at COMPILE time."
  45. `(ash (logand ,int ,(utils::get-bitmask start width)) ,(- ( - start width -1))))
  46. ;;;;;;;;;;;;;;;;;;;; convenience macros ;;;;;;;;;;;;;;;;;;;;
  47. (defmacro with-gensyms (syms &body body)
  48. `(let ,(mapcar #'(lambda (s)
  49. `(,s (gensym)))
  50. syms)
  51. ,@body))
  52. (defun make-keyword (name)
  53. (intern (string name) :keyword))
  54. (defmacro while (test &body body)
  55. `(do ()
  56. ((not ,test))
  57. ,@body))
  58. (defmacro aif (test-form then-form &optional else-form)
  59. `(let ((it ,test-form))
  60. (if it ,then-form ,else-form)))
  61. (defmacro awhen (test-form &body body)
  62. `(aif ,test-form
  63. (progn ,@body)))
  64. #+INSTRUMENT-MEMOIZED (progn
  65. (defstruct memoized-funcs
  66. name
  67. table
  68. calls
  69. finds
  70. news)
  71. (defvar *memoized-funcs* nil))
  72. (defun mk-memoize (func-name)
  73. "Takes a normal function object and returns a memoized one"
  74. (let* ((func (symbol-function func-name))
  75. (hash-table (make-hash-table :test 'equal))
  76. #+INSTRUMENT-MEMOIZED (s (make-memoized-funcs :table hash-table :calls 0 :finds 0 :news 0 :name func-name))
  77. )
  78. #+INSTRUMENT-MEMOIZED (push s *memoized-funcs*)
  79. #'(lambda (arg)
  80. (multiple-value-bind (value foundp) (gethash arg hash-table)
  81. #+INSTRUMENT-MEMOIZED (incf (memoized-funcs-calls s))
  82. (if foundp
  83. (progn
  84. #+INSTRUMENT-MEMOIZED (incf (memoized-funcs-finds s))
  85. value)
  86. (progn
  87. #+INSTRUMENT-MEMOIZED (incf (memoized-funcs-news s))
  88. (setf (gethash arg hash-table) (funcall func arg))))))))
  89. (defmacro memoize (func-name)
  90. "Memoize function associated with Function-Name. Simplified version"
  91. `(setf (symbol-function ,func-name) (utils::mk-memoize ,func-name)))
  92. (defun timings (function)
  93. (let ((real-base (get-internal-real-time)))
  94. (funcall function)
  95. (float (/ (- (get-internal-real-time) real-base) internal-time-units-per-second))))