Re: [Q] type promotion in * and +

David Tolpin <[email protected]> Fri, 7 Apr 2006 18:13:11 +0500
Newsgroups gmane.lisp.allegro
Message-ID <[email protected]>
On 09/08/5766, at 17:53, Didier Verna wrote:

> (defun my+ (a b)
>   (declare (:explain :boxing :calls :types))
>   (declare (type fixnum a))
>   (declare (type fixnum b))
>   (+ a b))

Hi Didier,

this is affected by

COMPILER:DECLARED-FIXNUMS-REMAIN-FIXNUMS-SWITCH
COMPILER:TRUST-DECLARATIONS-SWITCH

they are both nil for default optimization settings, and I get (ACL  
7.0 Trial):

> CL-USER(3): (compile 'my+)
> ;Examining a call to +_2OP with arguments:
> ;  symeval A type in fixnum range (INTEGER -536870912 536870911)
> ;     VARIABLE-information: LEXICAL: ((TYPE
>                                        (INTEGER -536870912  
> 536870911)))
> ;  symeval B type in fixnum range (INTEGER -536870912 536870911)
> ;     VARIABLE-information: LEXICAL: ((TYPE
>                                        (INTEGER -536870912  
> 536870911)))
> ; which returns a value of type (INTEGER -1073741824 1073741822)
>
> (my+ most-positive-fixnum most-positive-fixnum )
> 1073741822

If I set the first one to true, I'll get the following explanations  
from the optimizer:

> CL-USER(9): (compile 'my+)
> ;Examining a call to +_2OP with arguments:
> ;  symeval A type in fixnum range (INTEGER -536870912 536870911)
> ;     VARIABLE-information: LEXICAL: ((TYPE
>                                        (INTEGER -536870912  
> 536870911)))
> ;  symeval B type in fixnum range (INTEGER -536870912 536870911)
> ;     VARIABLE-information: LEXICAL: ((TYPE
>                                        (INTEGER -536870912  
> 536870911)))
> ; which returns a value in fixnum range of type (INTEGER
>                                                  -536870912
>                                                  536870911)
> MY+

but the compiler won't trust itself, and will give the correct result:

> CL-USER(10): (my+ most-positive-fixnum most-positive-fixnum)
> 1073741822


When, however, I set both to true, the result will match the  
explanation:

> CL-USER(14): (compile 'my+)
> ;Examining a call to +_2OP with arguments:
> ;  symeval A type in fixnum range (INTEGER -536870912 536870911)
> ;     VARIABLE-information: LEXICAL: ((TYPE
>                                        (INTEGER -536870912  
> 536870911)))
> ;  symeval B type in fixnum range (INTEGER -536870912 536870911)
> ;     VARIABLE-information: LEXICAL: ((TYPE
>                                        (INTEGER -536870912  
> 536870911)))
> ; which returns a value in fixnum range of type (INTEGER
>                                                  -536870912
>                                                  536870911)
> MY+
> NIL
> NIL
> CL-USER(15): (my+ most-positive-fixnum 1)
> -536870912
> CL-USER(16): (my+ most-positive-fixnum most-positive-fixnum)
> -2

David