Refactor uint Decoders

Instead bit-shifting to 'place' the bits where we want them to be and
adding Lisp provides the DPB function that allows us to deposit bits
where we want them in an integer.
This commit is contained in:
Javier Olaechea 2019-03-24 16:43:20 -05:00 committed by nik gaffney
parent 2924fe75e0
commit f8cb331753
Signed by: nik
GPG key ID: 989F5E6EDB478160

View file

@ -352,19 +352,7 @@ with the current time use (encode-timetag :time)."
(ldb (byte 16 0) (decode-int32 s))) (ldb (byte 16 0) (decode-int32 s)))
#-(or sbcl cmucl openmcl allegro) (error "cant decode floats using this implementation")) #-(or sbcl cmucl openmcl allegro) (error "cant decode floats using this implementation"))
<<<<<<< HEAD
(defun decode-int32 (s)
"4 byte -> 32 bit int -> two's compliment (in network byte order)"
(let ((i (+ (ash (elt s 0) 24)
(ash (elt s 1) 16)
(ash (elt s 2) 8)
(elt s 3))))
(if (>= i #x7fffffff)
(- 0 (- #x100000000 i))
i)))
=======
>>>>>>> d130e45 (Reuse uint decoders when possible)
(defun decode-uint32 (s) (defun decode-uint32 (s)
"4 byte -> 32 bit unsigned int" "4 byte -> 32 bit unsigned int"
(let ((i (+ (ash (elt s 0) 24) (let ((i (+ (ash (elt s 0) 24)
@ -373,18 +361,6 @@ with the current time use (encode-timetag :time)."
(elt s 3)))) (elt s 3))))
i)) i))
(defun decode-uint64 (s)
"8 byte -> 64 bit unsigned int"
(let ((i (+ (ash (elt s 0) 56)
(ash (elt s 1) 48)
(ash (elt s 2) 40)
(ash (elt s 3) 32)
(ash (elt s 4) 24)
(ash (elt s 5) 16)
(ash (elt s 6) 8)
(elt s 7))))
i))
(defmacro defint-encoder (num-of-octets &optional docstring) (defmacro defint-encoder (num-of-octets &optional docstring)
(let ((enc-name (intern (format nil "~:@(encode-int~)~D" (* 8 num-of-octets)))) (let ((enc-name (intern (format nil "~:@(encode-int~)~D" (* 8 num-of-octets))))
(buf (gensym)) (buf (gensym))
@ -400,33 +376,9 @@ with the current time use (encode-timetag :time)."
,int))) ,int)))
,buf)))) ,buf))))
(defint-encoder 4 "Convert an integer into a sequence of 4 bytes in network byte order.") (defint-encoder 4 "Convert an integer into a sequence of 4 bytes in network byte order (32 bit).")
(defint-encoder 8 "Convert an integer into a sequence of 8 bytes in network byte order.") (defint-encoder 8 "Convert an integer into a sequence of 8 bytes in network byte order (64 bit).")
<<<<<<< HEAD
(defun decode-uint64 (s)
"8 byte -> 64 bit unsigned int"
(let ((i (+ (ash (elt s 0) 56)
(ash (elt s 1) 48)
(ash (elt s 2) 40)
(ash (elt s 3) 32)
(ash (elt s 4) 24)
(ash (elt s 5) 16)
(ash (elt s 6) 8)
(elt s 7))))
i))
(defun decode-int64 (s)
"8 byte -> 64 bit int -> two's compliment (in network byte order)"
(let ((i (+ (ash (elt s 0) 56)
(ash (elt s 1) 48)
(ash (elt s 2) 40)
(ash (elt s 3) 32)
(ash (elt s 4) 24)
(ash (elt s 5) 16)
(ash (elt s 6) 8)
(elt s 7))))
=======
(defun decode-int32 (s) (defun decode-int32 (s)
"4 byte -> 32 bit int -> two's complement (in network byte order)" "4 byte -> 32 bit int -> two's complement (in network byte order)"
(let ((i (decode-uint32 s))) (let ((i (decode-uint32 s)))
@ -437,26 +389,16 @@ with the current time use (encode-timetag :time)."
(defun decode-int64 (s) (defun decode-int64 (s)
"8 byte -> 64 bit int -> two's complement (in network byte order)" "8 byte -> 64 bit int -> two's complement (in network byte order)"
(let ((i (decode-uint64 s))) (let ((i (decode-uint64 s)))
>>>>>>> d130e45 (Reuse uint decoders when possible)
(if (>= i #x7fffffffffffffff) (if (>= i #x7fffffffffffffff)
(- 0 (- #x10000000000000000 i)) (- 0 (- #x10000000000000000 i))
i))) i)))
;; osc-strings are unsigned bytes, padded to a 4 byte boundary ;; osc-strings are unsigned bytes, padded to a 4 byte boundary
(defun encode-string (string) (defun encode-string (string)
"encodes a string as a vector of character-codes, padded to 4 byte boundary" "encodes a string as a vector of character-codes, padded to 4 byte boundary"
(cat (map 'vector #'char-code string) (cat (map 'vector #'char-code string)
(string-padding string))) (string-padding string)))
(ash (elt s 1) 48)
(ash (elt s 2) 40)
(ash (elt s 3) 32)
(ash (elt s 4) 24)
(ash (elt s 5) 16)
(ash (elt s 6) 8)
(elt s 7))))
i))
(defun decode-string (data) (defun decode-string (data)
"converts a binary vector to a string and removes trailing #\nul characters" "converts a binary vector to a string and removes trailing #\nul characters"
@ -505,4 +447,5 @@ i))
(make-array n :initial-element 0 :fill-pointer n)) (make-array n :initial-element 0 :fill-pointer n))
(provide :osc) (provide :osc)
;; end ;; end