0f627d8ff8c3

Add some bit stuff, sort packages
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Mon, 26 Dec 2016 15:25:35 -0500
parents 04de8001e1e0
children 18e71146fb25
branches/tags (none)
files DOCUMENTATION.markdown losh.lisp make-docs.lisp package.lisp

Changes

--- a/DOCUMENTATION.markdown	Wed Dec 21 15:22:06 2016 -0500
+++ b/DOCUMENTATION.markdown	Mon Dec 26 15:25:35 2016 -0500
@@ -90,6 +90,98 @@
 
   
 
+## Package `LOSH.BITS`
+
+Utilities for low-level bit stuff.
+
+### `+/16` (function)
+
+    (+/16 X Y)
+
+Perform 16-bit signed addition of `x` and `y`.
+
+  Returns two values: the result and a boolean specifying whether
+  underflow/overflow occurred.
+
+  
+
+### `+/32` (function)
+
+    (+/32 X Y)
+
+Perform 32-bit signed addition of `x` and `y`.
+
+  Returns two values: the result and a boolean specifying whether
+  underflow/overflow occurred.
+
+  
+
+### `+/64` (function)
+
+    (+/64 X Y)
+
+Perform 64-bit signed addition of `x` and `y`.
+
+  Returns two values: the result and a boolean specifying whether
+  underflow/overflow occurred.
+
+  
+
+### `+/8` (function)
+
+    (+/8 X Y)
+
+Perform 8-bit signed addition of `x` and `y`.
+
+  Returns two values: the result and a boolean specifying whether
+  underflow/overflow occurred.
+
+  
+
+### `-/16` (function)
+
+    (-/16 X Y)
+
+Perform 16-bit signed subtraction of `x` and `y`.
+
+  Returns two values: the result and a boolean specifying whether
+  underflow/overflow occurred.
+
+  
+
+### `-/32` (function)
+
+    (-/32 X Y)
+
+Perform 32-bit signed subtraction of `x` and `y`.
+
+  Returns two values: the result and a boolean specifying whether
+  underflow/overflow occurred.
+
+  
+
+### `-/64` (function)
+
+    (-/64 X Y)
+
+Perform 64-bit signed subtraction of `x` and `y`.
+
+  Returns two values: the result and a boolean specifying whether
+  underflow/overflow occurred.
+
+  
+
+### `-/8` (function)
+
+    (-/8 X Y)
+
+Perform 8-bit signed subtraction of `x` and `y`.
+
+  Returns two values: the result and a boolean specifying whether
+  underflow/overflow occurred.
+
+  
+
 ## Package `LOSH.CHILI-DOGS`
 
 Gotta go FAST.
--- a/losh.lisp	Wed Dec 21 15:22:06 2016 -0500
+++ b/losh.lisp	Mon Dec 26 15:25:35 2016 -0500
@@ -182,6 +182,52 @@
     (mod <> base)))
 
 
+;;;; Bits ---------------------------------------------------------------------
+(eval-when (:compile-toplevel :load-toplevel :execute)
+  (defparameter *signed-add-docstring-template*
+    "Perform ~D-bit signed addition of `x` and `y`.
+
+  Returns two values: the result and a boolean specifying whether
+  underflow/overflow occurred.
+
+  ")
+
+  (defparameter *signed-sub-docstring-template*
+    "Perform ~D-bit signed subtraction of `x` and `y`.
+
+  Returns two values: the result and a boolean specifying whether
+  underflow/overflow occurred.
+
+  "))
+
+(macrolet
+    ((define-ops (size)
+       (let ((overflow (symb 'overflow- size)))
+         `(progn
+           (defun-inline ,overflow (value)
+             (cond ((> value (1- (expt 2 ,(1- size))))
+                    (values (- value (expt 2 ,size)) t))
+                   ((< value (- (expt 2 ,(1- size))))
+                    (values (+ value (expt 2 ,size)) t))
+                   (t (values value nil))))
+
+           (defun-inlineable ,(symb '+/ size) (x y)
+             ,(format nil *signed-add-docstring-template* size)
+             (declare (optimize speed)
+                      (type (signed-byte ,size) x y))
+             (,overflow (+ x y)))
+
+           (defun-inlineable ,(symb '-/ size) (x y)
+             ,(format nil *signed-sub-docstring-template* size)
+             (declare (optimize speed)
+                      (type (signed-byte ,size) x y))
+             (,overflow (- x y)))))))
+  (define-ops 8)
+  (define-ops 16)
+  (define-ops 32)
+  (define-ops 64))
+
+
 ;;;; Random -------------------------------------------------------------------
 (defun-inlineable randomp (&optional (chance 0.5))
   "Return a random boolean with `chance` probability of `t`."
--- a/make-docs.lisp	Wed Dec 21 15:22:06 2016 -0500
+++ b/make-docs.lisp	Mon Dec 26 15:25:35 2016 -0500
@@ -4,6 +4,7 @@
   (list "LOSH"
 
         "LOSH.ARRAYS"
+        "LOSH.BITS"
         "LOSH.CHILI-DOGS"
         "LOSH.CONTROL-FLOW"
         "LOSH.DEBUGGING"
--- a/package.lisp	Wed Dec 21 15:22:06 2016 -0500
+++ b/package.lisp	Mon Dec 26 15:25:35 2016 -0500
@@ -19,6 +19,150 @@
              `(:export ,@(external-symbols parent-package)))))
 
 
+(defpackage :losh.arrays
+  (:documentation "Utilities related to arrays.")
+  (:export
+    :do-array
+    :fill-multidimensional-array
+    :fill-multidimensional-array-t
+    :fill-multidimensional-array-fixnum
+    :fill-multidimensional-array-single-float))
+
+(defpackage :losh.bits
+  (:documentation "Utilities for low-level bit stuff.")
+  (:export
+    :+/8
+    :-/8
+    :+/16
+    :-/16
+    :+/32
+    :-/32
+    :+/64
+    :-/64))
+
+(defpackage :losh.chili-dogs
+  (:documentation "Gotta go FAST.")
+  (:export
+    :defun-inline
+    :defun-inlineable))
+
+(defpackage :losh.control-flow
+  (:documentation "Utilities for managing control flow.")
+  (:export
+    :recursively
+    :recur
+    :when-found
+    :if-found
+    :gathering
+    :gather
+    :when-let*
+    :multiple-value-bind*))
+
+(defpackage :losh.debugging
+  (:documentation "Utilities for figuring out what the hell is going on.")
+  (:export
+    :pr
+    :prl
+    :bits
+    :shut-up
+    :dis
+    :comment
+    :aesthetic-string
+    :structural-string
+    #+sbcl :start-profiling
+    #+sbcl :stop-profiling
+    :print-table
+    :print-hash-table))
+
+(defpackage :losh.eldritch-horrors
+  (:documentation "Abandon all hope, ye who enter here.")
+  (:export
+    :dlambda
+    :define-with-macro))
+
+(defpackage :losh.functions
+  (:documentation "Utilities for working with higher-order functions.")
+  (:export
+    :juxt
+    :nullary
+    :fixed-point))
+
+(defpackage :losh.gnuplot
+  (:documentation "Utilities for plotting data with gnuplot.")
+  (:export
+    :gnuplot
+    :gnuplot-args
+    :gnuplot-expr
+    :gnuplot-function
+    :x))
+
+(defpackage :losh.hash-sets
+  (:documentation "Simple hash set implementation.")
+  (:export
+    :hash-set
+    :make-hash-set
+    :copy-hash-set
+
+    :hset-empty-p
+    :hset-contains-p
+    :hset-elements
+    :hset-count
+
+    :hset-insert!
+    :hset-remove!
+    :hset-pop!
+    :hset-clear!
+
+    :hset=
+
+    :hset-union
+    :hset-union!
+    :hset-intersection
+    :hset-intersection!
+    :hset-difference
+    :hset-difference!
+    :hset-filter
+    :hset-filter!
+    :hset-map
+    :hset-map!))
+
+(defpackage :losh.hash-tables
+  (:documentation "Utilities for operating on hash tables.")
+  (:export
+    :mutate-hash-values))
+
+(defpackage :losh.iterate
+  (:use :iterate) ; need this for iterate's `for` symbol fuckery
+  (:documentation "Custom `iterate` drivers and clauses.")
+  (:export
+    :across-flat-array
+    :averaging
+    :cycling
+    :every-nth
+    :for-nested
+    :in-array
+    :in-lists
+    :in-sequences
+    :in-whatever
+    :index-of-flat-array
+    :into
+    :macroexpand-iterate
+    :modulo
+    :pairs-of-list
+    :per-iteration-into
+    :real-time
+    :run-time
+    :since-start-into
+    :skip-origin
+    :timing
+    :within-radius
+    ))
+
+(defpackage :losh.licensing
+  (:documentation "Utilities related to open source licenses.")
+  (:export
+    :print-licenses))
+
 (defpackage :losh.math
   (:documentation "Utilities related to math and numbers.")
   (:export
@@ -50,37 +194,6 @@
     :square
     :digit))
 
-(defpackage :losh.random
-  (:documentation "Utilities related to randomness.")
-  (:export
-    :randomp
-    :random-elt
-    :random-range
-    :random-range-exclusive
-    :random-around
-    :random-gaussian
-    :random-gaussian-integer
-    :d))
-
-(defpackage :losh.functions
-  (:documentation "Utilities for working with higher-order functions.")
-  (:export
-    :juxt
-    :nullary
-    :fixed-point))
-
-(defpackage :losh.control-flow
-  (:documentation "Utilities for managing control flow.")
-  (:export
-    :recursively
-    :recur
-    :when-found
-    :if-found
-    :gathering
-    :gather
-    :when-let*
-    :multiple-value-bind*))
-
 (defpackage :losh.mutation
   (:documentation "Utilities for mutating places in-place.")
   (:export
@@ -95,15 +208,6 @@
     :notf
     :callf))
 
-(defpackage :losh.arrays
-  (:documentation "Utilities related to arrays.")
-  (:export
-    :do-array
-    :fill-multidimensional-array
-    :fill-multidimensional-array-t
-    :fill-multidimensional-array-fixnum
-    :fill-multidimensional-array-single-float))
-
 (defpackage :losh.queues
   (:documentation "A simple queue implementation.")
   (:export
@@ -116,67 +220,17 @@
     :dequeue
     :queue-append))
 
-(defpackage :losh.iterate
-  (:use :iterate) ; need this for iterate's `for` symbol fuckery
-  (:documentation "Custom `iterate` drivers and clauses.")
-  (:export
-    :across-flat-array
-    :averaging
-    :cycling
-    :every-nth
-    :for-nested
-    :in-array
-    :in-lists
-    :in-sequences
-    :in-whatever
-    :index-of-flat-array
-    :into
-    :macroexpand-iterate
-    :modulo
-    :pairs-of-list
-    :per-iteration-into
-    :real-time
-    :run-time
-    :since-start-into
-    :skip-origin
-    :timing
-    :within-radius
-    ))
-
-(defpackage :losh.hash-tables
-  (:documentation "Utilities for operating on hash tables.")
+(defpackage :losh.random
+  (:documentation "Utilities related to randomness.")
   (:export
-    :mutate-hash-values))
-
-(defpackage :losh.hash-sets
-  (:documentation "Simple hash set implementation.")
-  (:export
-    :hash-set
-    :make-hash-set
-    :copy-hash-set
-
-    :hset-empty-p
-    :hset-contains-p
-    :hset-elements
-    :hset-count
-
-    :hset-insert!
-    :hset-remove!
-    :hset-pop!
-    :hset-clear!
-
-    :hset=
-
-    :hset-union
-    :hset-union!
-    :hset-intersection
-    :hset-intersection!
-    :hset-difference
-    :hset-difference!
-    :hset-filter
-    :hset-filter!
-    :hset-map
-    :hset-map!))
+    :randomp
+    :random-elt
+    :random-range
+    :random-range-exclusive
+    :random-around
+    :random-gaussian
+    :random-gaussian-integer
+    :d))
 
 (defpackage :losh.sequences
   (:documentation "Utilities for operating on sequences.")
@@ -188,22 +242,6 @@
     :take
     :drop))
 
-(defpackage :losh.debugging
-  (:documentation "Utilities for figuring out what the hell is going on.")
-  (:export
-    :pr
-    :prl
-    :bits
-    :shut-up
-    :dis
-    :comment
-    :aesthetic-string
-    :structural-string
-    #+sbcl :start-profiling
-    #+sbcl :stop-profiling
-    :print-table
-    :print-hash-table))
-
 (defpackage :losh.weightlists
   (:documentation
     "A simple data structure for choosing random items with weighted probabilities.")
@@ -214,36 +252,11 @@
     :make-weightlist
     :weightlist-random))
 
-(defpackage :losh.licensing
-  (:documentation "Utilities related to open source licenses.")
-  (:export
-    :print-licenses))
-
-(defpackage :losh.gnuplot
-  (:documentation "Utilities for plotting data with gnuplot.")
-  (:export
-    :gnuplot
-    :gnuplot-args
-    :gnuplot-expr
-    :gnuplot-function
-    :x))
-
-(defpackage :losh.eldritch-horrors
-  (:documentation "Abandon all hope, ye who enter here.")
-  (:export
-    :dlambda
-    :define-with-macro))
-
-(defpackage :losh.chili-dogs
-  (:documentation "Gotta go FAST.")
-  (:export
-    :defun-inline
-    :defun-inlineable))
-
 
 (defpackage-inheriting :losh
   (:losh.arrays
 
+   :losh.bits
    :losh.chili-dogs
    :losh.control-flow
    :losh.debugging