-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathANKSigned+Addition+Digit.swift
86 lines (71 loc) · 3.38 KB
/
ANKSigned+Addition+Digit.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
84
85
86
//=----------------------------------------------------------------------------=
// 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 Addition x Digit
//*============================================================================*
extension ANKSigned {
//=------------------------------------------------------------------------=
// MARK: Transformations
//=------------------------------------------------------------------------=
@_disfavoredOverload @inlinable public static func +=(lhs: inout Self, rhs: Digit) {
//=--------------------------------------=
if lhs.sign == rhs.sign {
lhs.magnitude += rhs.magnitude
return
}
//=--------------------------------------=
let extended = Magnitude(digit: rhs.magnitude)
if lhs.magnitude >= extended {
lhs.magnitude -= rhs.magnitude
//=--------------------------------------=
} else {
lhs.sign = rhs.sign
lhs.magnitude = extended - lhs.magnitude
}
}
@_disfavoredOverload @_transparent public static func +(lhs: Self, rhs: Digit) -> Self {
var lhs = lhs; lhs += rhs; return lhs
}
}
//*============================================================================*
// MARK: * ANK x Signed x Fixed Width x Addition x Digit
//*============================================================================*
extension ANKSigned where Magnitude: ANKFixedWidthInteger {
//=------------------------------------------------------------------------=
// MARK: Transformations
//=------------------------------------------------------------------------=
@_disfavoredOverload @_transparent public static func &+=(lhs: inout Self, rhs: Digit) {
_ = lhs.addReportingOverflow(rhs)
}
@_disfavoredOverload @_transparent public static func &+(lhs: Self, rhs: Digit) -> Self {
lhs.addingReportingOverflow(rhs).partialValue
}
//=------------------------------------------------------------------------=
// MARK: Transformations
//=------------------------------------------------------------------------=
@_disfavoredOverload @inlinable public mutating func addReportingOverflow(_ other: Digit) -> Bool {
//=--------------------------------------=
if self.sign == other.sign {
return self.magnitude.addReportingOverflow(other.magnitude)
}
//=--------------------------------------=
let magnitudeSubtractionOverflow = self.magnitude.subtractReportingOverflow(other.magnitude)
if magnitudeSubtractionOverflow {
self.sign = other.sign
self.magnitude.formTwosComplement()
}
return false
}
@_disfavoredOverload @inlinable public func addingReportingOverflow(_ other: Digit) -> PVO<Self> {
var partialValue = self
let overflow: Bool = partialValue.addReportingOverflow(other)
return PVO(partialValue, overflow)
}
}