-
Notifications
You must be signed in to change notification settings - Fork 3
/
NBKCoreInteger.swift
251 lines (205 loc) · 9.68 KB
/
NBKCoreInteger.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//=----------------------------------------------------------------------------=
// This source file is part of the Numberick open source project.
//
// Copyright (c) 2023 Oscar Byström Ericsson
// Licensed under Apache License, Version 2.0
//
// See http://www.apache.org/licenses/LICENSE-2.0 for license information.
//=----------------------------------------------------------------------------=
//*============================================================================*
// MARK: * NBK 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 NBKCoreInteger<Magnitude>: NBKFixedWidthInteger where
BitPattern == Magnitude, Digit == Self, Magnitude: NBKCoreInteger { }
//=----------------------------------------------------------------------------=
// MARK: + Details
//=----------------------------------------------------------------------------=
extension NBKCoreInteger {
//=------------------------------------------------------------------------=
// MARK: Details x Comparisons
//=------------------------------------------------------------------------=
@inlinable public func signum() -> Int {
Int(bit: self.isMoreThanZero) - Int(bit: self.isLessThanZero)
}
//=------------------------------------------------------------------------=
// MARK: Details x Text
//=------------------------------------------------------------------------=
@inlinable public func description(radix: Int, uppercase: Bool) -> String {
String(NBK.someSwiftBinaryInteger(self), radix: radix, uppercase: uppercase)
}
//=------------------------------------------------------------------------=
// MARK: Details x Words
//=------------------------------------------------------------------------=
@inlinable public init?(words: some RandomAccessCollection<UInt>) {
self.init(words: words, isSigned: Self.isSigned)
}
@inlinable public init?(words: some RandomAccessCollection<UInt>, isSigned: Bool) {
let chunks = NBKChunkedInt(words, isSigned: isSigned, as: Magnitude.self)
self = Self(bitPattern: chunks[0 as Int]) // with sign extension in case it is empty
guard self.isLessThanZero != chunks.sign.isZero else { return nil }
for index in chunks.indices.dropFirst() where chunks[index] != chunks.sign { return nil }
}
//=------------------------------------------------------------------------=
// 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 One's Complement
//=------------------------------------------------------------------------=
@inlinable public mutating func formOnesComplement() {
self = self.onesComplement()
}
@inlinable public func onesComplement() -> Self {
~self
}
//=------------------------------------------------------------------------=
// MARK: Details x Two's Complement
//=------------------------------------------------------------------------=
@inlinable public mutating func formTwosComplementReportingOverflow() -> Bool {
self.formTwosComplementSubsequence(true)
}
@inlinable public func twosComplementReportingOverflow() -> PVO<Self> {
self.twosComplementSubsequence(true)
}
@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: NBKSignedInteger {
let lhsIsLessThanZero: Bool = other.high.isLessThanZero
let rhsIsLessThanZero: Bool = /*--*/self.isLessThanZero
let minus: Bool = lhsIsLessThanZero != rhsIsLessThanZero
//=--------------------------------------=
var qro = NBK.bitCast(self.magnitude.dividingFullWidthReportingOverflow(NBK.TBI.magnitude(of: other))) 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: NBKUnsignedInteger {
//=--------------------------------------=
// divisor is zero
//=--------------------------------------=
if self.isZero {
return NBK.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: * NBK x Core Integer x Swift
//*============================================================================*
extension Int: NBKCoreInteger, NBKSignedInteger {
public typealias BitPattern = Magnitude
}
extension Int8: NBKCoreInteger, NBKSignedInteger {
public typealias BitPattern = Magnitude
}
extension Int16: NBKCoreInteger, NBKSignedInteger {
public typealias BitPattern = Magnitude
}
extension Int32: NBKCoreInteger, NBKSignedInteger {
public typealias BitPattern = Magnitude
}
extension Int64: NBKCoreInteger, NBKSignedInteger {
public typealias BitPattern = Magnitude
}
extension UInt: NBKCoreInteger, NBKUnsignedInteger {
public typealias BitPattern = Magnitude
}
extension UInt8: NBKCoreInteger, NBKUnsignedInteger {
public typealias BitPattern = Magnitude
}
extension UInt16: NBKCoreInteger, NBKUnsignedInteger {
public typealias BitPattern = Magnitude
}
extension UInt32: NBKCoreInteger, NBKUnsignedInteger {
public typealias BitPattern = Magnitude
}
extension UInt64: NBKCoreInteger, NBKUnsignedInteger {
public typealias BitPattern = Magnitude
}