Loop Unrolling in Lisp


; unroll loop code: (dotimes (var n) code)
(defun unroll (docode)
  (let ((var (car (cadr docode)))
        (n   (cadr (cadr docode)))
        (code (caddr docode))
        result)
    (if (and (integerp n)
             (<  n 20))
        (cons 'progn
              (dotimes (i n (nreverse result))
                (push (subst i var code)
                      result) ) )
        docode) ))


> (unroll '(dotimes (i 3)
            (setf (aref x i) (aref y i))))
(PROGN
  (SETF (AREF X 0) (AREF Y 0))
  (SETF (AREF X 1) (AREF Y 1))
  (SETF (AREF X 2) (AREF Y 2)))

Contents    Page-10    Prev    Next    Page+10    Index