-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathANKSigned+Subtraction.swift
83 lines (68 loc) · 3.2 KB
/
ANKSigned+Subtraction.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//=----------------------------------------------------------------------------=
// This source file is part of the AwesomeNumbersKit open source project.
//
// Copyright (c) 2022 Oscar Byström Ericsson
// Licensed under Apache License, Version 2.0
//
// See http://www.apache.org/licenses/LICENSE-2.0 for license information.
//=----------------------------------------------------------------------------=
import ANKCoreKit
//*============================================================================*
// MARK: * ANK x Signed x Subtraction
//*============================================================================*
extension ANKSigned {
//=------------------------------------------------------------------------=
// MARK: Transformations
//=------------------------------------------------------------------------=
@inlinable public static func -=(lhs: inout Self, rhs: Self) {
//=--------------------------------------=
if lhs.sign != rhs.sign {
lhs.magnitude += rhs.magnitude
//=--------------------------------------=
} else if lhs.magnitude >= rhs.magnitude {
lhs.magnitude -= rhs.magnitude
//=--------------------------------------=
} else {
lhs.sign.toggle()
lhs.magnitude = rhs.magnitude - lhs.magnitude
}
}
@_transparent public static func -(lhs: Self, rhs: Self) -> Self {
var lhs = lhs; lhs -= rhs; return lhs
}
}
//*============================================================================*
// MARK: * ANK x Signed x Fixed Width x Subtraction
//*============================================================================*
extension ANKSigned where Magnitude: ANKFixedWidthInteger {
//=------------------------------------------------------------------------=
// MARK: Transformations
//=------------------------------------------------------------------------=
@_transparent public static func &-=(lhs: inout Self, rhs: Self) {
_ = lhs.subtractReportingOverflow(rhs)
}
@_transparent public static func &-(lhs: Self, rhs: Self) -> Self {
lhs.subtractingReportingOverflow(rhs).partialValue
}
//=------------------------------------------------------------------------=
// MARK: Transformations
//=------------------------------------------------------------------------=
@inlinable public mutating func subtractReportingOverflow(_ other: Self) -> Bool {
//=--------------------------------------=
if self.sign != other.sign {
return self.magnitude.addReportingOverflow(other.magnitude)
}
//=--------------------------------------=
let magnitudeSubtractionOverflow = self.magnitude.subtractReportingOverflow(other.magnitude)
if magnitudeSubtractionOverflow {
self.sign.toggle()
self.magnitude.formTwosComplement()
}
return false
}
@inlinable public func subtractingReportingOverflow(_ other: Self) -> PVO<Self> {
var partialValue = self
let overflow: Bool = partialValue.subtractReportingOverflow(other)
return PVO(partialValue, overflow)
}
}