]> asedeno.scripts.mit.edu Git - cl-protobufs.git/commitdiff
wire-format: An ENCODE-INT that doesn't cons
authorAlejandro R Sedeño <asedeno@google.com>
Fri, 26 Apr 2013 15:49:03 +0000 (11:49 -0400)
committerAlejandro R Sedeño <asedeno@google.com>
Wed, 29 May 2013 22:02:07 +0000 (18:02 -0400)
wire-format.lisp

index 0c1341d320d9f38a096cfd77485649509b14bcb2..99fd18f933f5d2174aca2b5481fb0297db698eb5 100644 (file)
    Modifies the buffer, and returns the new index into the buffer.
    Watch out, this function turns off all type checking and array bounds checking."
   (declare #.$optimize-serialization)
-  (let ((val (ldb (byte 64 0) val))) ;clamp val to 64-bits maximum
-    (declare (type (unsigned-byte 64) val)
-             (type (simple-array (unsigned-byte 8)) buffer)
-             (type fixnum index))
-    ;; Seven bits at a time, least significant bits first
-    (loop do (let ((bits (ldb (byte 7 0) val)))
-               (declare (type (unsigned-byte 8) bits))
-               (setq val (ash val -7))
-               (setf (aref buffer index)
-                     (ilogior bits (if (zerop val) 0 128)))
-               (iincf index))
-          until (zerop val))
-    (values index buffer)))  ;return the buffer to improve 'trace'
+  (declare (type (simple-array (unsigned-byte 8)) buffer)
+           (type fixnum index))
+  ;; Seven bits at a time, least significant bits first
+  (loop repeat 9                ;up to 63 bits
+        do (setf (aref buffer index) (ldb (byte 7 0) val))
+           (setq val (ash val -7))
+        until (zerop val)
+        do (iincf (aref buffer index) #x80)
+           (iincf index)
+        finally (unless (zerop val)     ;take the 64th bit as needed
+                  (setf (aref buffer index) 1)
+                  (unless (= val -1)
+                    (serialization-error "Integer too large while encoding VarInt."))))
+  (values (iincf index) buffer))        ;return the buffer to improve 'trace'
 
 (defun encode-single (val buffer index)
   "Encodes the single float 'val' into the buffer at the given index.