-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathANKCoreInteger.swift
209 lines (174 loc) · 7.97 KB
/
ANKCoreInteger.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//=----------------------------------------------------------------------------=
// 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.
//=----------------------------------------------------------------------------=
//*============================================================================*
// MARK: * ANK x Core Integer
//*============================================================================*
/// A fixed-width, binary, integer that is up to 64 bits wide.
///
/// Only the following types in the standard library may conform to this protocol:
///
/// - `Int`
/// - `Int8`
/// - `Int16`
/// - `Int32`
/// - `Int64`
///
/// - `UInt`
/// - `UInt8`
/// - `UInt16`
/// - `UInt32`
/// - `UInt64`
///
public protocol ANKCoreInteger<Magnitude>: ANKFixedWidthInteger where
BitPattern == Magnitude, Digit == Self, Magnitude: ANKCoreInteger { }
//=----------------------------------------------------------------------------=
// MARK: + Details
//=----------------------------------------------------------------------------=
extension ANKCoreInteger {
//=------------------------------------------------------------------------=
// MARK: Details x Bit Pattern
//=------------------------------------------------------------------------=
@inlinable public init(bitPattern: BitPattern) {
self = Swift.unsafeBitCast(bitPattern, to: Self.self)
}
@inlinable public var bitPattern: BitPattern {
Swift.unsafeBitCast(self, to: BitPattern.self)
}
//=------------------------------------------------------------------------=
// MARK: Details x Two's Complement
//=------------------------------------------------------------------------=
@inlinable public mutating func formTwosComplementSubsequence(_ carry: Bool) -> Bool {
let pvo: PVO<Self> = self.twosComplementSubsequence(carry)
self = pvo.partialValue
return pvo.overflow as Bool
}
@inlinable public func twosComplementSubsequence(_ carry: Bool) -> PVO<Self> {
var partialValue = ~self
let overflow: Bool = carry && partialValue.addReportingOverflow(1 as Self)
return PVO(partialValue, overflow)
}
//=------------------------------------------------------------------------=
// MARK: Details x Addition, Subtraction, Multiplication, Division
//=------------------------------------------------------------------------=
@inlinable public mutating func addReportingOverflow(_ other: Self) -> Bool {
let pvo: PVO<Self> = self.addingReportingOverflow(other)
self = pvo.partialValue
return pvo.overflow as Bool
}
@inlinable public mutating func subtractReportingOverflow(_ other: Self) -> Bool {
let pvo: PVO<Self> = self.subtractingReportingOverflow(other)
self = pvo.partialValue
return pvo.overflow as Bool
}
@inlinable public mutating func multiplyReportingOverflow(by other: Self) -> Bool {
let pvo: PVO<Self> = self.multipliedReportingOverflow(by: other)
self = pvo.partialValue
return pvo.overflow as Bool
}
@inlinable public mutating func multiplyFullWidth(by other: Self) -> Self {
let product: HL<Self, Magnitude> = self.multipliedFullWidth(by: other)
self = Self(bitPattern: product.low)
return product.high as Self
}
@inlinable public mutating func divideReportingOverflow(by other: Self) -> Bool {
let pvo: PVO<Self> = self.dividedReportingOverflow(by: other)
self = pvo.partialValue
return pvo.overflow as Bool
}
@inlinable public mutating func formRemainderReportingOverflow(dividingBy other: Self) -> Bool {
let pvo: PVO<Self> = self.remainderReportingOverflow(dividingBy: other)
self = pvo.partialValue
return pvo.overflow as Bool
}
@inlinable public func quotientAndRemainderReportingOverflow(dividingBy other: Self) -> PVO<QR<Self, Self>> {
let quotient: PVO<Self> = self.dividedReportingOverflow(by: other)
let remainder: PVO<Self> = self.remainderReportingOverflow(dividingBy: other)
assert(quotient.overflow == remainder.overflow)
return PVO(QR(quotient.partialValue, remainder.partialValue), quotient.overflow)
}
//=------------------------------------------------------------------------=
// MARK: Division x Full Width
//=------------------------------------------------------------------------=
@inlinable public func dividingFullWidthReportingOverflow(_ other: HL<Self, Magnitude>) -> PVO<QR<Self, Self>> where Self: ANKSignedInteger {
let lhsIsLessThanZero: Bool = other.high.isLessThanZero
let rhsIsLessThanZero: Bool = /*--*/self.isLessThanZero
let minus: Bool = lhsIsLessThanZero != rhsIsLessThanZero
//=--------------------------------------=
var lhsMagnitude = ANK.bitCast(other) as HL<Magnitude, Magnitude>
if lhsIsLessThanZero {
var carry = true
carry = lhsMagnitude.low .formTwosComplementSubsequence(carry)
carry = lhsMagnitude.high.formTwosComplementSubsequence(carry)
}
let rhsMagnitude = self.magnitude as Magnitude
//=--------------------------------------=
var qro = ANK.bitCast(rhsMagnitude.dividingFullWidthReportingOverflow(lhsMagnitude)) as PVO<QR<Self, Self>>
//=--------------------------------------=
if minus {
qro.partialValue.quotient.formTwosComplement()
}
if lhsIsLessThanZero {
qro.partialValue.remainder.formTwosComplement()
}
if minus != qro.partialValue.quotient.isLessThanZero {
qro.overflow = qro.overflow || !(minus && qro.partialValue.quotient.isZero)
}
//=--------------------------------------=
return qro as PVO<QR<Self, Self>>
}
@inlinable public func dividingFullWidthReportingOverflow(_ other: HL<Self, Magnitude>) -> PVO<QR<Self, Self>> where Self: ANKUnsignedInteger {
//=--------------------------------------=
// divisor is zero
//=--------------------------------------=
if self.isZero {
return ANK.bitCast(PVO(QR(other.low, other.low), true))
}
//=--------------------------------------=
// quotient does not fit in two halves
//=--------------------------------------=
if self <= other.high {
return PVO(self.dividingFullWidth(HL(other.high % self, other.low)), true) // stdlib
}
//=--------------------------------------=
return PVO(self.dividingFullWidth(other), false) // stdlib
}
}
//*============================================================================*
// MARK: * ANK x Core Integer x Swift
//*============================================================================*
extension Int: ANKCoreInteger, ANKSignedInteger {
public typealias BitPattern = Magnitude
}
extension Int8: ANKCoreInteger, ANKSignedInteger {
public typealias BitPattern = Magnitude
}
extension Int16: ANKCoreInteger, ANKSignedInteger {
public typealias BitPattern = Magnitude
}
extension Int32: ANKCoreInteger, ANKSignedInteger {
public typealias BitPattern = Magnitude
}
extension Int64: ANKCoreInteger, ANKSignedInteger {
public typealias BitPattern = Magnitude
}
extension UInt: ANKCoreInteger, ANKUnsignedInteger {
public typealias BitPattern = Magnitude
}
extension UInt8: ANKCoreInteger, ANKUnsignedInteger {
public typealias BitPattern = Magnitude
}
extension UInt16: ANKCoreInteger, ANKUnsignedInteger {
public typealias BitPattern = Magnitude
}
extension UInt32: ANKCoreInteger, ANKUnsignedInteger {
public typealias BitPattern = Magnitude
}
extension UInt64: ANKCoreInteger, ANKUnsignedInteger {
public typealias BitPattern = Magnitude
}