Sujet : Re: Optimization flag for unchecked fixnums in SBCL?
De : david (at) *nospam* harpegolden.net (David De La Harpe Golden)
Groupes : comp.lang.lispDate : 11. Aug 2024, 03:32:26
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v994bb$148i2$1@dont-email.me>
References : 1 2 3
User-Agent : Mozilla Thunderbird
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)