55// Copyright © 2020 SomeRandomiOSDev. All rights reserved.
66//
77
8- import Foundation
9-
108// MARK: Overflowing Addition
119
1210extension Complex where Scalar: FixedWidthInteger {
@@ -127,7 +125,7 @@ extension Complex where Scalar: FixedWidthInteger {
127125 //swiftlint:disable identifier_name
128126 public func dividedReportingOverflow( by rhs: Complex < Scalar > ) -> ( partialValue: Complex < Scalar > , overflow: Bool ) {
129127 // (a + bi) / (c + di) -> ((ac + bd) + (bc - ad)i) / (c^2 + d^2)
130- guard rhs. real != . zero || rhs. imaginary != . zero else { return ( rhs, true ) }
128+ guard rhs. real != 0 || rhs. imaginary != 0 else { return ( rhs, true ) }
131129
132130 let ac = self . real. multipliedFullWidth ( by: rhs. real)
133131 let ad = self . real. multipliedFullWidth ( by: rhs. imaginary)
@@ -162,12 +160,12 @@ extension Complex where Scalar: FixedWidthInteger, Scalar: SignedInteger {
162160 let a = ( high: ( high: dividend. high. high. real, low: dividend. high. low. real) , low: ( high: dividend. low. high. real, low: dividend. low. low. real) )
163161 let b = ( high: ( high: dividend. high. high. imaginary, low: dividend. high. low. imaginary) , low: ( high: dividend. low. high. imaginary, low: dividend. low. low. imaginary) )
164162
165- let ac = Complex< Scalar> . slowpathMultiply( real, a)
166- let ad = Complex< Scalar> . slowpathMultiply( imaginary, a)
167- let bc = Complex< Scalar> . slowpathMultiply( real, b)
168- let bd = Complex< Scalar> . slowpathMultiply( imaginary, b)
169- let cc = Complex< Scalar> . signExtend( real. multipliedFullWidth ( by: real) )
170- let dd = Complex< Scalar> . signExtend( imaginary. multipliedFullWidth ( by: imaginary) )
163+ let ac = Complex< Scalar> . slowpathMultiply( self . real, a)
164+ let ad = Complex< Scalar> . slowpathMultiply( self . imaginary, a)
165+ let bc = Complex< Scalar> . slowpathMultiply( self . real, b)
166+ let bd = Complex< Scalar> . slowpathMultiply( self . imaginary, b)
167+ let cc = Complex< Scalar> . signExtend( self . real. multipliedFullWidth ( by: self . real) )
168+ let dd = Complex< Scalar> . signExtend( self . imaginary. multipliedFullWidth ( by: self . imaginary) )
171169
172170 let real = Complex< Scalar> . add( ac, bd)
173171 let imaginary = Complex< Scalar> . subtract( bc, ad)
@@ -197,7 +195,7 @@ extension Complex where Scalar: FixedWidthInteger {
197195 //
198196
199197 internal static func signExtend( _ value: Scalar ) -> ExtendedScalar {
200- let highHigh = ( Scalar . isSigned && value. leadingZeroBitCount == 0 ) ? ~ Scalar. zero : Scalar . zero
198+ let highHigh = ( Scalar . isSigned && value. leadingZeroBitCount == 0 ) ? ~ Scalar( 0 ) : Scalar ( 0 )
201199 let highLow = Scalar . Magnitude ( truncatingIfNeeded: highHigh)
202200 let lowHigh = Scalar . Magnitude ( truncatingIfNeeded: highLow)
203201 let lowLow = Scalar . Magnitude ( truncatingIfNeeded: value)
@@ -206,7 +204,7 @@ extension Complex where Scalar: FixedWidthInteger {
206204 }
207205
208206 internal static func signExtend( _ value: ( high: Scalar , low: Scalar . Magnitude ) ) -> ExtendedScalar {
209- let highHigh = ( Scalar . isSigned && value. high. leadingZeroBitCount == 0 ) ? ~ Scalar. zero : Scalar . zero
207+ let highHigh = ( Scalar . isSigned && value. high. leadingZeroBitCount == 0 ) ? ~ Scalar( 0 ) : Scalar ( 0 )
210208 let highLow = Scalar . Magnitude ( truncatingIfNeeded: highHigh)
211209 let lowHigh = Scalar . Magnitude ( truncatingIfNeeded: value. high)
212210 let lowLow = value. low
@@ -276,10 +274,10 @@ extension Complex where Scalar: FixedWidthInteger {
276274 internal static func slowpathMultiply( _ lhs: ExtendedScalar , _ rhs: ExtendedScalar ) -> ExtendedScalar {
277275 var result : ExtendedScalar = ( ( 0 , 0 ) , ( 0 , 0 ) )
278276
279- let lhsIsNegative = Scalar . isSigned && lhs. high. high < . zero
277+ let lhsIsNegative = Scalar . isSigned && lhs. high. high < 0
280278 let left = lhsIsNegative ? twosComplement ( of: lhs) : lhs
281279
282- let rhsIsNegative = Scalar . isSigned && rhs. high. high < . zero
280+ let rhsIsNegative = Scalar . isSigned && rhs. high. high < 0
283281 let right = rhsIsNegative ? twosComplement ( of: rhs) : rhs
284282
285283 let ( leftIsPowerOfTwo, lhsShift) = isPowerOfTwo ( left)
@@ -295,7 +293,7 @@ extension Complex where Scalar: FixedWidthInteger {
295293 var shifted = right
296294 var bits = left
297295
298- while bits. low. low != . zero || bits. low. high != . zero || bits. high. low != . zero || bits. high. high != . zero {
296+ while bits. low. low != 0 || bits. low. high != 0 || bits. high. low != 0 || bits. high. high != 0 {
299297 if ( bits. low. low & 1 ) == 1 {
300298 shifted = leftShift ( shifted, by: shift - lastShift)
301299 result = add ( result, shifted)
@@ -316,7 +314,7 @@ extension Complex where Scalar: FixedWidthInteger {
316314 }
317315
318316 internal static func slowpathMultiply( _ lhs: Scalar , _ rhs: ExtendedScalar ) -> ExtendedScalar {
319- let lhsIsNegative = lhs < . zero
317+ let lhsIsNegative = lhs < 0
320318
321319 let extended = signExtend ( lhs)
322320 var result = slowpathMultiply ( lhsIsNegative ? twosComplement ( of: extended) : extended, rhs)
@@ -329,10 +327,10 @@ extension Complex where Scalar: FixedWidthInteger {
329327 }
330328
331329 internal static func slowpathDivide( _ lhs: ExtendedScalar , _ rhs: ExtendedScalar ) -> ( quotient: ExtendedScalar , remainder: ExtendedScalar ) {
332- let dividendIsNegative = Scalar . isSigned && lhs. high. high < . zero
330+ let dividendIsNegative = Scalar . isSigned && lhs. high. high < 0
333331 var dividend = dividendIsNegative ? twosComplement ( of: lhs) : lhs
334332
335- let divisorIsNegative = Scalar . isSigned && rhs. high. high < . zero
333+ let divisorIsNegative = Scalar . isSigned && rhs. high. high < 0
336334 var divisor = divisorIsNegative ? twosComplement ( of: rhs) : rhs
337335
338336 guard isLessThanOrEqual ( divisor, to: dividend) else { return ( quotient: ( ( 0 , 0 ) , ( 0 , 0 ) ) , remainder: lhs) }
@@ -469,7 +467,7 @@ extension Complex where Scalar: FixedWidthInteger {
469467 var bits = value
470468 var shift : Scalar . Magnitude = 0
471469
472- while bits. low. low > 1 || bits. low. high != . zero || bits. high. low != . zero || bits. high. high != . zero {
470+ while bits. low. low > 1 || bits. low. high != 0 || bits. high. low != 0 || bits. high. high != 0 {
473471 bits = rightShift ( bits, by: 1 )
474472 shift += 1
475473 }
@@ -520,13 +518,13 @@ extension Complex where Scalar: FixedWidthInteger {
520518 // non-zero or if the most significant bit of the least significant word
521519 // is 1.
522520
523- if value. high. high < . zero {
521+ if value. high. high < 0 {
524522 if value. high. high. nonzeroBitCount != Scalar . bitWidth || value. high. low. nonzeroBitCount != Scalar . bitWidth || value. low. high. nonzeroBitCount != Scalar . bitWidth {
525523 overflow = true
526524 } else if value. low. low. leadingZeroBitCount > 0 {
527525 overflow = true
528526 }
529- } else if value. high. high != . zero || value. high. low != . zero || value. low. high != . zero {
527+ } else if value. high. high != 0 || value. high. low != 0 || value. low. high != 0 {
530528 overflow = true
531529 } else if value. low. low. leadingZeroBitCount == 0 {
532530 overflow = true
@@ -535,7 +533,7 @@ extension Complex where Scalar: FixedWidthInteger {
535533 // For unsigned integers overflow occurs when the any of the three most significant
536534 // words are non-zero.
537535
538- overflow = ( value. high. high > . zero ) || ( value. high. low > . zero ) || ( value. low. high > . zero )
536+ overflow = ( value. high. high > 0 ) || ( value. high. low > 0 ) || ( value. low. high > 0 )
539537 }
540538
541539 return ( partialValue, overflow)
0 commit comments