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 {
16241624 @inlinable
16251625 @inline ( __always)
16261626 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 {
16281632 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) )
16341635 }
1635- return self . magnitude < n. magnitude
1636- ? Self ( Int ( self ) + n)
1637- : self + Self( n)
16381636 }
16391637}
16401638
Original file line number Diff line number Diff line change @@ -852,6 +852,9 @@ tests.test("Strideable") {
852852 expectEqual( dist ( UInt8 . max, UInt8 . min) , - 255 )
853853 expectEqual( dist ( Int8 . min, Int8 . max) , 255 )
854854 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 )
855858}
856859
857860tests. test ( " signum/generic " ) {
You can’t perform that action at this time.
0 commit comments