-
Notifications
You must be signed in to change notification settings - Fork 0
/
num.lisp
42 lines (36 loc) · 900 Bytes
/
num.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
;; (C) 2011 Pierre-Yves Baccou
(provide "num")
(defun uint16 (seq &optional (endian :lsb))
(ecase endian
(:lsb (+ (ash (elt seq 0) 0)
(ash (elt seq 1) 8)))
(:msb (+ (ash (elt seq 1) 0)
(ash (elt seq 0) 8)))))
(defun uint32 (seq &optional (endian :lsb))
(ecase endian
(:lsb (+ (ash (elt seq 0) 0)
(ash (elt seq 1) 8)
(ash (elt seq 2) 16)
(ash (elt seq 3) 24)))
(:msb (+ (ash (elt seq 3) 0)
(ash (elt seq 2) 8)
(ash (elt seq 1) 16)
(ash (elt seq 0) 24)))))
(defun int16 (seq &optional (endian :lsb))
(let ((i (uint16 seq endian)))
(if (= (ash i -15) 1)
(- i #x10000)
i)))
(defun int32 (seq &optional (endian :lsb))
(let ((i (uint32 seq endian)))
(if (= (ash i -31) 1)
(- i #x100000000)
i)))
(defun pad (n)
(let ((n (if (numberp n)
n
(length n))))
(- (* 4
(ceiling n
4))
n)))