Hi !
Consider the code given below. It adds a constant value to every point of an
image. There's one 1D array and one 2D array implementation of the same
process (don't worry about the offset trickery: it's to avoid cache
optimization artefacts).
I'm compiling with CMU-CL, and there are two things I don't understand:
1/ The compiler gives me the following note:
; File: /tmp/test.cl
; In: DEFUN TEST
; (TEST/SB-AREF IMAGE 42)
; --> LET DOTIMES DO BLOCK LET TAGBODY SETF PROGN SETF
; ==>
; (SETQ POS (MOD OFFSET SIZE))
; Note: Doing signed word to integer coercion (cost 20) to POS.
What I don't understand here is why I get this note, but none in test/mb on
the assignation to pos-i and pos-j. What's different ?
2/ Along with the same lines, it seems that the declarations A and B in
test/mb are necessary to optimize, but not declaration C in test/sb-aref.
Again, I don't see why the situation is different in these two cases.
Any hint would be appreciated.
Thanks !
;;; Code:
(eval-when (:compile-toplevel)
(declaim (optimize (speed 3)
(compilation-speed 0)
(safety 0)
(debug 0))))
(defmacro test/mb (image value)
"Add VALUE to every point of IMAGE (2D array implementation)."
`(let ((size (array-dimension ,image 0))
(offset-i 0)
(offset-j 0)
pos-i pos-j)
(declare (type fixnum offset-i))
(declare (type fixnum offset-j))
(dotimes (i size)
(declare (type fixnum i)) ;; A
(setf offset-i (+ offset-i 999983)
pos-i (mod offset-i size))
(dotimes (j size)
(declare (type fixnum j)) ;; B
(setf offset-j (+ offset-j 999983)
pos-j (mod offset-j size)
(aref ,image pos-i pos-j) (+ (aref ,image pos-i pos-j) ,value))
))))
(defmacro test/sb-aref (image value)
"Add VALUE to every point of IMAGE (1D array implementation)."
`(let ((size (array-dimension ,image 0))
(offset 0)
pos)
(declare (type fixnum offset))
(dotimes (i size)
;; (declare (type fixnum i)) ;; C. Seems to be unnecessary
(setf offset (+ offset 999983)
pos (mod offset size)
(aref ,image pos) (+ (aref ,image pos) ,value)))))
(defun test ()
(let ((image (make-array '(1024 1024)
:initial-element 0 :element-type 'fixnum)))
(test/mb image 42))
(let ((image (make-array (* 1024 1024)
:initial-element 0 :element-type 'fixnum)))
(test/sb-aref image 42)))
--
Didier Verna, ······@lrde.epita.fr, http://www.lrde.epita.fr/~didier
EPITA / LRDE, 14-16 rue Voltaire Tel.+33 (1) 44 08 01 85
94276 Le Kremlin-Bic�tre, France Fax.+33 (1) 53 14 59 22 ······@xemacs.org
From: Thomas A. Russ
Subject: Re: [Q] Type declaration mysteries with CMU CL
Date:
Message-ID: <ymiek3phzfq.fsf@sevak.isi.edu>
Didier Verna <······@lrde.epita.fr> writes:
> ; (SETQ POS (MOD OFFSET SIZE))
Also, another thing to be aware of is that lisp has both MOD and REM,
which are similar, but slightly different functions. The "mod" or "%"
operator in most other programming languages corresponds to REM rather
than MOD. It may be that REM is slightly more efficient.
--
Thomas A. Russ, USC/Information Sciences Institute
>>>>> "dvl" == Didier Verna <······@lrde.epita.fr> writes:
dvl> ; File: /tmp/test.cl
dvl> ; In: DEFUN TEST
dvl> ; (TEST/SB-AREF IMAGE 42)
dvl> ; --> LET DOTIMES DO BLOCK LET TAGBODY SETF PROGN SETF
dvl> ; ==>
dvl> ; (SETQ POS (MOD OFFSET SIZE))
dvl> ; Note: Doing signed word to integer coercion (cost 20) to POS.
dvl> What I don't understand here is why I get this note, but none in test/mb on
dvl> the assignation to pos-i and pos-j. What's different ?
Not sure why that is either, but you if you bind 0 to pos and declare
pos as a fixnum, the warnings go away.
dvl> 2/ Along with the same lines, it seems that the declarations A and B in
dvl> test/mb are necessary to optimize, but not declaration C in test/sb-aref.
dvl> Again, I don't see why the situation is different in these two cases.
I don't see why you the situation is different either, unfortunately.
Ray
Raymond Toy <···········@ericsson.com> wrote:
> dvl> What I don't understand here is why I get this note, but none in
> dvl> test/mb on the assignation to pos-i and pos-j. What's different ?
>
> Not sure why that is either, but you if you bind 0 to pos and declare
> pos as a fixnum, the warnings go away.
Of course. Otherwise the type should be (or nil ...).
Thanks.
--
Didier Verna, ······@lrde.epita.fr, http://www.lrde.epita.fr/~didier
EPITA / LRDE, 14-16 rue Voltaire Tel.+33 (1) 44 08 01 85
94276 Le Kremlin-Bic�tre, France Fax.+33 (1) 53 14 59 22 ······@xemacs.org