swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.38k stars 10.34k forks source link

[16-bit AVR] `Builtin.IntLiteral` has limited bits #75652

Open benrimmington opened 1 month ago

benrimmington commented 1 month ago

Builtin.IntLiteral for 16-bit AVR has a maximum bitWidth of 255, unless the IntegerLiteralFlags::BitWidthShift is changed. ConstantIntegerLiteralMap::get() will build the flags word, without checking if the bitWidth was truncated.

let value = StaticBigInt(
  0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
)
value.bitWidth  //-> expected: 256 or 0x100, actual: 0

(StaticBigInt isn't part of the embedded Swift stdlib.)

Cc: @carlos4242 @kubamracek @phausler @rauhul

carlos4242 commented 1 month ago

Thoughts anyone? Do I need to try and write a fix?

benrimmington commented 1 month ago

We can increase the maximum bitWidth to 32767 (on 16-bit AVR) if we don't need to reserve space for other flags.

#if UINTPTR_MAX >= UINT32_MAX
    // Save some space for other flags.

    BitWidthShift = 8,
#else
    BitWidthShift = 1,
#endif