Deutsch   English   Français   Italiano  
<v994bb$148i2$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!news.nobody.at!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: David De La Harpe Golden <david@harpegolden.net>
Newsgroups: comp.lang.lisp
Subject: Re: Optimization flag for unchecked fixnums in SBCL?
Date: Sun, 11 Aug 2024 02:32:26 +0100
Organization: A noiseless patient Spider
Lines: 73
Message-ID: <v994bb$148i2$1@dont-email.me>
References: <87h6bwrufd.fsf@nightsong.com> <v916b5$3g9mg$1@dont-email.me>
 <87zfplhyjg.fsf@nightsong.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 11 Aug 2024 03:32:27 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="dcb1311a197eeb2b316c7dbcaa60d1d0";
	logging-data="1188418"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19YiLXd213Tlxyjdo50w1Onk++sKGzf+RE6bpjKqYR1bQ=="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:/3fQHkj4IB5/ypPsgEaVEltERc0=
In-Reply-To: <87zfplhyjg.fsf@nightsong.com>
Content-Language: en-US
Bytes: 3319

On 10/08/2024 04:00, Paul Rubin wrote:


> The "trick" of this Euler problem is that a few (only a few) of
> the intermediate values will overflow a 32 bit word.  But, everyone uses
> 64 bit machines these days, so that part works anyway.

Ah, good point. (erm, last (signed-byte 29) part my prev post probably 
only worked because - at (safety 0) - it was indeed generating code 
using unchecked >32-bit signed arithmetic ops on my 64-bit machine anyway)

Worth noting in context that if you use sbcl with (safety 3) ON and use 
(signed-byte 32) say, though, then the type-declarations-as-assertions 
nicely catches the problem (at runtime with dynamic check)

     $ head -n2 collatz-signed-byte-32-inline-safe.lisp
     (declaim (optimize (speed 0) (safety 3) (debug 3)))
     (declaim (ftype (function ((signed-byte 32)) (values (signed-byte 
32) &optional)) collatz))


     $ sbcl --control-stack-size 1024 --dynamic-space-size 65536
     * (compile-file "collatz-signed-byte-32-inline-safe.lisp")
     [...]
     * (load "collatz-signed-byte-32-inline-safe.fasl")

     debugger invoked on a TYPE-ERROR @535D60FF in thread
     #<THREAD "main thread" RUNNING {1001348003}>:
       The value
         2482111348
       is not of type
         (SIGNED-BYTE 32)
      from the function type declaration.

     Type HELP for debugger help, or (SB-EXT:EXIT) to exit from SBCL.

     restarts (invokable by number or by possibly-abbreviated name):
       0: [ABORT] Exit debugger, returning to top level.

     (COLLATZ 827370449)


Playing about for n 1000000, apparently (signed-byte 37) enough...


     $ sbcl --control-stack-size 2048 --dynamic-space-size 65536 
--script collatz-signed-byte-36-inline-safe.lisp
     Unhandled TYPE-ERROR in thread #<SB-THREAD:THREAD "main thread" RUNNING
                                       {1001348003}>:
       The value
         34988856874
       is not of type
         (SIGNED-BYTE 36)
       from the function type declaration.
     [...]


     $ sbcl --control-stack-size 2048 --dynamic-space-size 65536 
--script collatz-signed-byte-37-inline-safe.lisp

     (837799 525)