Menu

#783 Numbers > 2^63 not always converting correctly to ulongint

closed
dkl
None
compiler
2015-09-19
2015-08-20
Matthew
No

In the example below, both columns should contain the same values. But when casting the variable to ulongint, it may be truncated to 2^63.

const c = 1e19
var v = c

print c, v
print culngint(c), culngint(v)
print hex(c), hex(v)

Testing in the 64-bit fbc for linux-x86_64, I see this truncation effect.
I implemented a conversion in the compiler and x86 emitter, which subtracts 2^63 if the value exceeds this, and re-adds it after the conversion. This ensures values between 2^63 and 2^64-1 are converted correctly. But it seems the conversion in GCC doesn't (necessarily) do this. Perhaps we need a function in the rtlib to do it.

Discussion

  • dkl

    dkl - 2015-09-19

    Hi,

    This should be fixed in Git now: [baec86]

    The C backend used a float-to-int64 conversion plus a cast to uint64 to implement the float-to-uint64 conversion. That meant that values would be truncated to int64.

    I've changed it to do something more sane now, and it at least fixed the problem with the above test case. Although I only tested on 32bit for now, hopefully it works on 64bit just as well.

     

    Related

    Commit: [baec86]

  • dkl

    dkl - 2015-09-19
    • status: open --> closed
    • assigned_to: dkl
     

Log in to post a comment.

MongoDB Logo MongoDB