common-lispsbclinteger-arithmetic

Signed Word to Integer Conversion in Lisp


I'd like some help in understanding and fixing an SBCL compiler note that says:

; in: DEFUN PRINT-SEARCH-PROGRESS-GRAPH
;     (- (1+ WOULDWORK-PKG::*N*)
;        (LENGTH WOULDWORK-PKG::*L*))
;
; note: doing signed word to integer coercion (cost 20), for:
;       the first result of inline (signed-byte 64) arithmetic

Some possibly relevant background info:

(optimize (debug 3))

(defparameter *N* 0)
(declaim (fixnum *N*))
    
(defparameter *L* nil)
(declaim (list *L*))
    
(type-of *N*) => BIT

Solution

  • You declared *N* to be a FIXNUM, which is a fixed-size integer; it's equivalent to (SIGNED-BYTE 64) in 64-bit SBCL. But if its value happens to be MOST-POSITIVE-FIXNUM, (1+ *n*) could produce a value that's doesn't fit in 64 bits, so it will have to convert to INTEGER. The note is warning you that this conversion may take place.

    If you know that it will never be so high that this overflow will be necessary, you can use THE:

    (- (the fixnum (1+ *n*)) (length *l*))