git.fiddlerwoaroof.com
Browse code

Remove bit-smasher

The file delta.lisp used two functions from the bit-smasher library,
int->bits and bits->int. However this system was not specified as a
dependency for cl-git. Instead of adding a dependency, implement the
functions as they are straight-forward.

Note that bit-smasher 'reversed' the bit-vectors so we had to flip the
arrays to preserve that behavior. Because bit-vectors are arrays and
arrays are printed from low-to-high but octets are normally printed
from high-to-low (little-endian). If the bit-vectors were to use
big-endian instead the code for the helper could be simplified further.

Javier Olaechea authored on 08/11/2020 01:08:06
Showing 1 changed files
... ...
@@ -30,13 +30,31 @@
30 30
 (defun make-ref-delta (base commands repository)
31 31
   (fw.lu:new 'ofs-delta base commands repository))
32 32
 
33
+(defun int->bit-vector (n)
34
+  (let* ((integer-length (integer-length n))
35
+         (bv-size (* 8 (ceiling integer-length 8)))
36
+         (bv (make-array bv-size :element-type 'bit)))
37
+    (loop :for ix :below integer-length
38
+          :do (setf (aref bv (- bv-size 1 ix))
39
+                    (if (logbitp ix n)
40
+                        1
41
+                        0)))
42
+    bv))
43
+
44
+(defun bit-vector->int (bv)
45
+  (let ((bv-size (array-total-size bv)))
46
+    (loop :for ix :from (1- bv-size) :downto 0
47
+          :for n :from 0
48
+          :unless (zerop (aref bv ix))
49
+            :sum (expt 2 n))))
50
+
33 51
 (defun partition-commands (data)
34 52
   (let ((idx 0))
35 53
     (labels ((advance ()
36 54
                (prog1 (elt data idx)
37 55
                  (incf idx)))
38 56
              (get-command ()
39
-               (let* ((bv (bit-smasher:int->bits (elt data idx)))
57
+               (let* ((bv (int->bit-vector (elt data idx)))
40 58
                       (discriminator (elt bv 0))
41 59
                       (insts (subseq bv 1)))
42 60
                  (incf idx)
... ...
@@ -46,7 +64,7 @@
46 64
                            (coerce (loop repeat (count 1 insts) collect (advance))
47 65
                                    '(vector (unsigned-byte 8))))
48 66
                      (list :add
49
-                           (coerce (loop repeat (1- (bit-smasher:bits->int (reverse insts)))
67
+                           (coerce (loop repeat (1- (bit-vector->int (reverse insts)))
50 68
                                          collect (advance))
51 69
                                    '(vector (unsigned-byte 8))))))))
52 70
       (loop while (< idx (length data))