swiftlang / swift

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

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

Open benrimmington opened 4 months ago

benrimmington commented 4 months 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 4 months ago

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

benrimmington commented 4 months 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