Skip to content

Commit 735fe97

Browse files
committed
[stdlib] Fix sign extension in word -> multi-word integer conversions
This fixes integer conversion issues on 32-bit platforms.
1 parent e975445 commit 735fe97

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

stdlib/public/core/Integers.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2294,7 +2294,7 @@ ${unsafeOperationComment(x.operator)}
22942294

22952295
@inline(__always)
22962296
public init<T : BinaryInteger>(extendingOrTruncating source: T) {
2297-
if Self.bitWidth <= ${word_bits} || source.bitWidth <= ${word_bits} {
2297+
if Self.bitWidth <= ${word_bits} {
22982298
self = Self.init(_truncatingBits: source._lowWord)
22992299
}
23002300
else {

test/stdlib/Integers.swift.gyb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,18 @@ dwTests.test("inits") {
738738
_ = DWU16(UInt32.max)
739739
}
740740

741+
dwTests.test("TwoWords") {
742+
typealias DW = DoubleWidth<Int>
743+
744+
expectEqual(-1 as DW, DW(extendingOrTruncating: -1 as Int8))
745+
746+
expectNil(Int(exactly: DW(Int.min) - 1))
747+
expectNil(Int(exactly: DW(Int.max) + 1))
748+
749+
expectTrue(DW(Int.min) - 1 < Int.min)
750+
expectTrue(DW(Int.max) + 1 > Int.max)
751+
}
752+
741753
dwTests.test("Bitshifts") {
742754
typealias DWU16 = DoubleWidth<UInt8>
743755
typealias DWU32 = DoubleWidth<DWU16>

0 commit comments

Comments
 (0)