File tree Expand file tree Collapse file tree 2 files changed +10
-9
lines changed Expand file tree Collapse file tree 2 files changed +10
-9
lines changed Original file line number Diff line number Diff line change @@ -1624,17 +1624,15 @@ extension BinaryInteger {
1624
1624
@inlinable
1625
1625
@inline ( __always)
1626
1626
public func advanced( by n: Int ) -> Self {
1627
- if !Self. isSigned {
1627
+ if Self . isSigned {
1628
+ return self . bitWidth < n. bitWidth
1629
+ ? Self ( Int ( truncatingIfNeeded: self ) + n)
1630
+ : self + Self( truncatingIfNeeded: n)
1631
+ } else {
1628
1632
return n < ( 0 as Int )
1629
- ? self - Self( - n)
1630
- : self + Self( n)
1631
- }
1632
- if ( self < ( 0 as Self ) ) == ( n < ( 0 as Self ) ) {
1633
- return self + Self( n)
1633
+ ? self - Self( UInt ( bitPattern: ~ n &+ 1 ) )
1634
+ : self + Self( UInt ( bitPattern: n) )
1634
1635
}
1635
- return self . magnitude < n. magnitude
1636
- ? Self ( Int ( self ) + n)
1637
- : self + Self( n)
1638
1636
}
1639
1637
}
1640
1638
Original file line number Diff line number Diff line change @@ -852,6 +852,9 @@ tests.test("Strideable") {
852
852
expectEqual( dist ( UInt8 . max, UInt8 . min) , - 255 )
853
853
expectEqual( dist ( Int8 . min, Int8 . max) , 255 )
854
854
expectEqual( dist ( Int8 . max, Int8 . min) , - 255 )
855
+
856
+ expectEqual( Int8 . min. advanced ( by: Int ( Int8 . max) + 1 ) , 0 )
857
+ expectEqual( UInt . max. advanced ( by: Int . min) , UInt . max / 2 )
855
858
}
856
859
857
860
tests. test ( " signum/generic " ) {
You can’t perform that action at this time.
0 commit comments