File: basic.lisp

package info (click to toggle)
acl2 8.6%2Bdfsg-2
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 1,111,420 kB
  • sloc: lisp: 17,818,294; java: 125,359; python: 28,122; javascript: 23,458; cpp: 18,851; ansic: 11,569; perl: 7,678; xml: 5,591; sh: 3,976; makefile: 3,833; ruby: 2,633; yacc: 1,126; ml: 763; awk: 295; csh: 233; lex: 197; php: 178; tcl: 49; asm: 23; haskell: 17
file content (67 lines) | stat: -rw-r--r-- 1,831 bytes parent folder | download | duplicates (3)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
(in-package :fast-io.test)

(defmacro wo (&body body)
  `(with-fast-output (b)
     ,@body))

(defmacro wi (seq &body body)
  `(with-fast-input (b (octets-from ,seq))
     ,@body))

(defmacro write-all (bits)
  (let ((fun (mapcar (lambda (m) (symbolicate (format nil m bits)))
                     '("WRITE~A-BE" "WRITEU~A-BE"
                       "WRITE~A-LE" "WRITEU~A-LE")))
        (unsigned (- (expt 2 bits) 2))
        (signed (- (truncate (expt 2 bits) 2))))
    `(values
      (wo (,(first fun) ,signed b))
      (wo (,(second fun) ,unsigned b))
      (wo (,(third fun) ,signed b))
      (wo (,(fourth fun) ,unsigned b)))))

(defmacro read-all (bits)
  (let ((fun (mapcar (lambda (m) (symbolicate (format nil m bits)))
                     '("READ~A-BE" "READU~A-BE"
                       "READ~A-LE" "READU~A-LE"))))
    `(let ((bytes (multiple-value-list (write-all ,bits))))
         (values
          (wi (first bytes) (,(first fun) b))
          (wi (second bytes) (,(second fun) b))
          (wi (third bytes) (,(third fun) b))
          (wi (fourth bytes) (,(fourth fun) b))))))

(check (:name :octets)
  (results
   (make-octet-vector 4)
   (octets-from '(1 2 3 4))
   (octets-from #(4 3 2 1))))

(check (:name :write-bytes :output-p t)
  (results
   (wo (fast-write-byte 1 b))
   (wo (fast-write-sequence (octets-from '(1 2 3 4)) b))))

(check (:name :write-endian :output-p t)
  (results
   (write-all 8)
   (write-all 16)
   (write-all 32)
   (write-all 64)
   (write-all 128)))

(check (:name :read-bytes :output-p t)
  (results
   (wi '(1) (fast-read-byte b))
   (wi '(1 2 3 4)
     (let ((vec (make-octet-vector 4)))
       (fast-read-sequence vec b)
       vec))))

(check (:name :read-endian :output-p t)
  (results
   (read-all 8)
   (read-all 16)
   (read-all 32)
   (read-all 64)
   (read-all 128)))