-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] Unsigned integer casting overflowing as if signed when using int()
or UInt32()
#3065
Comments
Just tested on nightly and behaviour is identical. |
So just to be clear: The So there might be two issues: the table one and the |
Smaller repro: fn main():
print(int(UInt8(128))) # prints -128 It's very curious that the following runs fine: fn main():
var n = UInt8(128)
print(n) # prints 128
print(int(n)) # prints 128 |
Similarly, running: var a: UInt32 = 129
print(int(a.cast[DType.uint8]())) in the REPL returns Running: def main():
var a: UInt32 = 129
print(int(a.cast[DType.uint8]()))
main() In the REPL returns |
Signed-off-by: martinvuyk <martin.vuyklop@gmail.com>
I have a bit of a quick and dirty solution proposal for this in #3172 . I would love some help since MLIR won't let me do rebind to another type and I'm out of my depth when it comes to IR and compiler stuff. As far as I understand we only have access to pop and index dialect and we'd need a way to bitcast the value itself and I've only found |
Signed-off-by: martinvuyk <martin.vuyklop@gmail.com>
I'm taking a look at this now: def main():
var b: UInt32 = 129
print(b.cast[DType.uint8]())
print(int(b.cast[DType.uint8]()))
print(UInt32(b.cast[DType.uint8]())) There is something flat out wrong with the conversion in |
In PR #3172 I thought about adding a constructor that casts WDYT? fn __init__[A: DType, //](inout self, value: SIMD[A, size]):
"""Cast the other SIMD vector into self.
Parameters:
A: The DType of the other SIMD.
Args:
value: The value to cast into self.
"""
self = value.cast[type]() |
I think we should make casting like this explicit. One way to do that is to make an always-failing constructor. I'm working on that now. |
@laszlokindrat I had the idea of adding a all-failing constructor to catch this case. However, with implicit conversion to fn f(x: SIMD) -> SIMD:
return x # We can only find out about the dtype/shape mismatch after the first compiling attempt |
The implicit casting part is traced in #3045. |
I agree this is not optimal, but the LSP currently has a general limitation that it doesn't work with |
The `pop.cast` op would sign extend when casting a smaller unsigned type to the `index` type, which would cause incorrect behavior for `int(UInt8(128))` and similar code. This patch fixes that by first upcasting to a larger unsigned scalar value and then converting to `Int`. Fixes #3065 MODULAR_ORIG_COMMIT_REV_ID: 111bb5545c02663ac0e7bf7cdb3678ea23c261fd
Bug description
Migrating this here after a bit of discussion in Discord.
It seems like casting to unsigned integers actually just casts to signed integers, but has different behaviour in different cases.
I.e. I would expect that if I took a
UInt32
value, set it to129
, cast it toUInt8
and then callint()
, I would still get 129.However, this seem to end up as
4294967169
, i.e.2**32 - 127
If I instead call
UInt32()
I get1
, so it overflows as2**32 + 1
? Not sure about the last one.So despite the fact that no overflow is expected (
129 < 255
), it seems to overflow, i.e. act like an signed integer.The same goes if I cast to
UInt16
, i.e. casting32768
(2**16/2 + 1
) toUInt16
and then callingint()
I get4294934528
i.e.2**32 - 2**16/2
So it looks like the cast to
UInt8
andUInt16
actually perform casts toInt8
andInt16
, and then overflow.Even more confusingly, this behaviour is different based on where the original
UInt32
is from, but behaviour differs when initialising usingvar
andalias
(see below).Steps to reproduce
Most minimal example
Produces:
Doesn't have the same effect in the REPL, but the REPL does seem to have a clue:
A little bit less of a minimal example:
The expected output would be:
But I get:
System information
The text was updated successfully, but these errors were encountered: