The Computer Language
24.04 Benchmarks Game

fannkuch-redux Lisp SBCL #2 program

source code

;;   The Computer Language Benchmarks Game
;;   https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
;;;
;;; By Jon Smith
;;; Tranlated from OCaml implementation by ?, who translated it from
;;; the Scala implementation by Otto Bommer.
;;; 
;;; This is a single core implementation.
;;; I am sure that this program can be improved upon quite a lot.
;;; Most likely it will involve knowing how sbcl does its optimizations.
;;; As you can see, I simply used fixnums everywhere. There may be a better choice.
;;;
;;; To compile
;;; sbcl --load fannkuch.lisp --eval "(save-lisp-and-die \"fannkuch.core\" :purify t :toplevel (lambda () (main) (quit)))"
;;; To run
;;; sbcl --noinform --core fannkuch.core %A

(declaim (optimize (speed 3) (safety 0) (space 1) (debug 0)))

(defun fannkuch (n)
  (declare (type fixnum n))
  (let ((csum 0)
	(fmax 0))
    (declare (type fixnum fmax))
    (let ((perm (make-array n :element-type 'fixnum))
	  (copy (make-array n :element-type 'fixnum))
	  (num 0)) 

      (loop for i from 0 to (- n 1) do (setf (aref perm i) i))

      (labels ((do-iter (ht)
		 
		 (declare (type fixnum ht))
		 
		 (if (= ht 1)
		     (progn
		       (loop for i from 0 to (- n 1) do (setf (aref copy i) (aref perm i)))
		       (let ((c 0))
			 (declare (type fixnum c))
			 (let ((z (aref copy 0)))
			   (loop until (= z 0) do
				(progn
				  (loop for i from 0 to (ash z -1)
				     do (let ((temp (aref copy i))
					      (k (- z i)))
					  (setf (aref copy i) (aref copy k))
					  (setf (aref copy k) temp)))
				  (incf c)
				  (setf z (aref copy 0)))))
			 (setf csum (+ csum  (if (evenp num) c (- c))))
			 (when (> c fmax)
			   (setf fmax c)))
		       (incf num))
		     (loop for i from 1 to ht do
			  (progn (do-iter (- ht 1))
				 (let ((temp (aref perm 0))
				       (m (- ht 1)))
				   (loop for i from 1 to m do
					(setf (aref perm (- i 1)) (aref perm i)))
				   (setf (aref perm m) temp)))))))

	(do-iter n)))
    (format t "~s~%Pfannkuchen(~s) = ~s~%" csum n fmax)))


(defun main ()  
  (let* ((args (cdr sb-ext:*posix-argv*))
         (n (parse-integer (car args))))
    (fannkuch n)))
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
SBCL 2.4.2


 Mon, 04 Mar 2024 22:36:27 GMT

MAKE:
cp: 'fannkuchredux.sbcl-2.sbcl' and './fannkuchredux.sbcl-2.sbcl' are the same file
SBCL built with: /opt/src/sbcl-2.4.2/bin/sbcl --userinit /dev/null --batch --eval '(load "fannkuchredux.sbcl-2.sbcl_compile")'
### START fannkuchredux.sbcl-2.sbcl_compile
(handler-bind ((sb-ext:defconstant-uneql      (lambda (c) (abort c))))      (require :sb-concurrency)      (load (compile-file "fannkuchredux.sbcl-2.sbcl" ))) (save-lisp-and-die "sbcl.core" :purify t)
### END fannkuchredux.sbcl-2.sbcl_compile

; compiling file "/home/dunham/all-benchmarksgame/benchmarksgame_i53330/fannkuchredux/tmp/fannkuchredux.sbcl-2.sbcl" (written 26 APR 2018 12:47:38 PM):

; file: /home/dunham/all-benchmarksgame/benchmarksgame_i53330/fannkuchredux/tmp/fannkuchredux.sbcl-2.sbcl
; in: DEFUN FANNKUCH
;     (+ CSUM
;        (IF (EVENP NUM)
;            C
;            (- C)))
; 
; note: forced to do GENERIC-+ (cost 10)
;       unable to do inline fixnum arithmetic (cost 2) because:
;       The first argument is a INTEGER, not a FIXNUM.
;       The result is a (VALUES INTEGER &OPTIONAL), not a (VALUES FIXNUM
;                                                                 &OPTIONAL).
;       unable to do inline (unsigned-byte 64) arithmetic (cost 4) because:
;       The first argument is a INTEGER, not a (UNSIGNED-BYTE 64).
;       The second argument is a (INTEGER -4611686018427387903
;                                 4611686018427387903), not a (UNSIGNED-BYTE 64).
;       The result is a (VALUES INTEGER &OPTIONAL), not a (VALUES
;                                                          (UNSIGNED-BYTE 64)
;                                                          &OPTIONAL).
;       etc.

;     (INCF NUM)
; --> THE 
; ==>
;   (+ 1 NUM)
; 
; note: forced to do GENERIC-+ (cost 10)
;       unable to do inline fixnum arithmetic (cost 1) because:
;       The first argument is a UNSIGNED-BYTE, not a FIXNUM.
;       The result is a (VALUES (INTEGER 1) &OPTIONAL), not a (VALUES FIXNUM
;                                                                     &OPTIONAL).
;       unable to do inline fixnum arithmetic (cost 2) because:
;       The first argument is a UNSIGNED-BYTE, not a FIXNUM.
;       The result is a (VALUES (INTEGER 1) &OPTIONAL), not a (VALUES FIXNUM
;                                                                     &OPTIONAL).
;       etc.
; 
; compilation unit finished
;   printed 2 notes


; wrote /home/dunham/all-benchmarksgame/benchmarksgame_i53330/fannkuchredux/tmp/fannkuchredux.sbcl-2.fasl
; compilation finished in 0:00:00.072
### START fannkuchredux.sbcl-2.sbcl_run
(main) (quit)
### END fannkuchredux.sbcl-2.sbcl_run


3.40s to complete and log all make actions

COMMAND LINE:
 /opt/src/sbcl-2.4.2/bin/sbcl  --noinform --core sbcl.core --userinit /dev/null --load fannkuchredux.sbcl-2.sbcl_run 12

PROGRAM OUTPUT:
3968050
Pfannkuchen(12) = 65