;;; Direct translation of first two versions of ;;; http://metamatix.org/~ocaml/price-of-abstraction.html ;;; to Common Lisp. (declaim (optimize (speed 3) (safety 0))) (defconstant +resolution+ 5000) (declaim (inline iters)) ;; Version using plain double floats (defun iters (max xc yc) (declare (double-float xc yc)) (let ((x xc) (y yc)) (declare (double-float x y) (fixnum max)) (loop for count from 0 below max do (when (>= (+ (* x x) (* y y)) 4.0d0) (return-from iters count)) (let ((tmp (+ (- (* x x) (* y y)) xc))) (setf y (+ (* 2.0d0 x y) yc) x tmp))) max)) ;; version using complex double floats #+nil (progn (declaim (inline norm-square)) (defun norm-square (c) (+ (* (realpart c) (realpart c)) (* (imagpart c) (imagpart c)))) (defun iters (max xc yc) (declare (double-float xc yc) (fixnum max)) (let ((c (complex xc yc)) (z (complex xc yc))) (declare (type (complex double-float) z c)) (loop for count from 0 below max do (when (>= (norm-square z) 4.0) (return-from iters2 count)) (setf z (+ (* z z) c))) max))) (defun main () (let* ((max (truncate +resolution+ 2)) (min (- max)) (mul (/ 2.0d0 max)) (count 0)) (declare (fixnum count)) (loop for i from min upto max do (loop for j from min upto max do (incf count (iters 100 (* mul i) (* mul j))))) (format t "result ~d~%" count))) #+nil (save-lisp-and-die "mandel" :executable t :toplevel #'main)