@@ -535,14 +535,14 @@ extension FixedWidthIntegerType {
535
535
536
536
@_transparent
537
537
public func uword(n: Word) -> UWord {
538
- var n = n
539
538
_precondition(n >= 0, "Negative word index")
540
- var x = self
541
- while n > 0 {
542
- x &>>= Swift.min(Self(_truncatingBits: UWord(Self.bitWidth._storage)) &- 1, ${word_bits})
543
- n -= 1
539
+ if _fastPath(n < countRepresentedWords) {
540
+ let shift = UWord(n._storage) &* ${word_bits}
541
+ let bitWidth = UWord(Self.bitWidth._storage)
542
+ _sanityCheck(shift < bitWidth)
543
+ return (self &>> Self(_truncatingBits: shift))._lowUWord
544
544
}
545
- return x._lowUWord
545
+ return self < 0 ? ~0 : 0
546
546
}
547
547
548
548
public var countRepresentedWords: Word {
@@ -1302,6 +1302,20 @@ tests.test("Basics") {
1302
1302
expectEqual(32, Int32.bitWidth)
1303
1303
}
1304
1304
1305
+ tests.test("uword") {
1306
+ let x = UDWord(Word.max)
1307
+ expectEqual(Word.max._lowUWord, x.uword(0))
1308
+ expectEqual(0, x.uword(1))
1309
+
1310
+ let y = DWord(Word.min)
1311
+ expectEqual(Word.min._lowUWord, y.uword(0))
1312
+ expectEqual(~0, y.uword(1))
1313
+
1314
+ let z = UWord(~Word.min) + 1
1315
+ expectEqual(Word.min._lowUWord, z.uword(0))
1316
+ expectEqual(0, z.uword(1))
1317
+ }
1318
+
1305
1319
tests.test("Multiprecision/+/DWord") {
1306
1320
var x = DWord.max - 2
1307
1321
x += (1 as UInt8)
0 commit comments