anchor.diff:            (declare (type fixnum b n)
                      (dynamic-extent b n))
             (cond ((zerop n))
  -                ((>= (plus n *prev-length*) 8)
  -                 (let* ((result (ash *prev-byte* (minus 8 *prev-length*)))
  -                        (total-length (plus n *prev-length*))
  +                ((>= (plus n (write-bits-prev-length write-bits-state)) 8)
  +                 (let* ((result (ash (write-bits-prev-byte write-bits-state)
  +                                     (minus 8 (write-bits-prev-length write-bits-state))))
  +                        (total-length (plus n (write-bits-prev-length write-bits-state)))
                           (overflow (minus total-length 8)))
                      (declare (type fixnum overflow total-length result)
                               (dynamic-extent overflow total-length result))
  -                   (setf *prev-byte* (ldb (byte overflow 0) b))
  +                   (setf (write-bits-prev-byte write-bits-state) (ldb (byte overflow 0) b))
                      (write-stuffed (deposit-field
                                      (ldb (byte (minus n overflow) overflow) b)
  -                                   (byte (minus 8 *prev-length*) 0)
  +                                   (byte (minus 8 (write-bits-prev-length write-bits-state)) 0)
                                      result)
                                     s)
  -                   (setf *prev-length* overflow)))
  -                (t  (setf *prev-byte* (deposit-field b (byte n 0) (ash *prev-byte* n)))
  -                    (incf *prev-length* n))))))
  +                   (setf (write-bits-prev-length write-bits-state) overflow)))
  +                (t  (setf (write-bits-prev-byte write-bits-state)
  +                          (deposit-field b
  +                                         (byte n 0)
  +                                         (ash (write-bits-prev-byte write-bits-state) n)))
  +                    (incf (write-bits-prev-length write-bits-state) n))))))
   
   ;;; Encodes block using specified huffman tables, returns new pred (DC prediction value)
   ;;; and last code written to stream for padding
  -(defun encode-block (block tables pred s)
  +(defun encode-block (block tables pred s write-bits-state)
     (declare #.*optimize* (type fixnum pred)
              (type sint16-array block))
     (let* ((ehufsi-dc (first (first tables)))
  
positive.code:                            (crunch sampled-buf pos q-tab)
                             (setf (aref preds cn)
                                   (encode-block (zigzag (aref sampled-buf pos) zz-result)
                                                 hufftabs (aref preds cn) out-stream)))))))))
      (unless (zerop *prev-length*)
        (write-stuffed (deposit-field #xff ; byte padding & flushing
                                      (byte (minus 8 *prev-length*) 0)
                                      (the fixnum (ash (the uint8 *prev-byte*) (the uint8 (minus 8 *prev-length*)))))
                       out-stream))
      (write-marker +M_EOI+ out-stream)))
  
  
negative.code: ;;; NB: probably it's a good idea to encapsulate this behavior into a class, but I'm
  ;;; afraid that method dispatch would be too slow
  
  (defstruct (write-bits-state (:conc-name write-bits-))
    (prev-byte 0 :type uint8)
    (prev-length 0 :type fixnum))
  
  (defun write-bits (bi ni s write-bits-state)
    (declare #.*optimize*
             (type fixnum bi ni)
             (type stream s))
  