chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: "cannot coerce inexact literal to fixnum"


From: Peter Bex
Subject: Re: "cannot coerce inexact literal to fixnum"
Date: Sat, 10 Feb 2024 12:00:29 +0100

On Sat, Feb 10, 2024 at 08:12:38AM +0200, Al wrote:
> On 2024-02-10 02:42, Al wrote:
> 
> > ... if I enable fixnum, csc chokes on both the third and fourth
> > display's with: "Error: cannot coerce inexact literal `2147483647' to
> > fixnum". It compiles and runs fine if those lines are commented out (or
> > if fixnum is disabled).
> 
> So the error comes from a check for (big-fixnum?) in chicken-core/core.scm.
> It is defined in chicken-core/support.scm:
> 
>  (define (big-fixnum? x) ;; XXX: This should probably be in c-platform
>     (and (fixnum? x)
>          (feature? #:64bit)
>          (or (fx> x 1073741823)
>          (fx< x -1073741824) ) ) )
> 
>   (define (small-bignum? x) ;; XXX: This should probably be in c-platform
>     (and (bignum? x)
>          (not (feature? #:64bit))
>          (fx<= (integer-length x) 62) ) )
> 
> Maybe the condition in big-fixnum should be negated? Apply the restrictive
> #x3fffffff limit only when NOT (feature? #:64bit) ?

No, this code is correct.  It emits a literal into the C code.  That C code
must be able to be compiled on either 32-bit platforms or 64-bit platforms.

That's why on 64-bit platforms, we can't simply assume that a fixnum is
small enough to fit in a machine word - if the same code will be
compiled on a 32-bit machine it wouldn't fit in a fixnum.  These
so-called "big-fixnums" are compiled into a string literal which gets
decoded on-the-fly at runtime into either a fixnum (on 64-bit) or a
bignum (on 32-bit).

So you see, this wouldn't work with (declare fixnum) mode because that
must be able to assume fixnums throughout, regardless of target platform.

There's (currently) no option to force fixnum mode in a way that ignores
the existence 32-bit platforms.  Theoretically, it should be possible to
compile your code assuming fixnums (so it emits C integer literals) and
make it barf at compilation time if one tried to build for a 32-bit
platform using a #ifdef or something.  We just don't have the required
code to do this, and I'm not sure this is something we'd all want.

Cheers,
Peter

Attachment: signature.asc
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]