-
Notifications
You must be signed in to change notification settings - Fork 1
/
ANKLargeBinaryInteger.swift
259 lines (227 loc) · 10.4 KB
/
ANKLargeBinaryInteger.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
252
253
254
255
256
257
258
259
//=----------------------------------------------------------------------------=
// 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 Binary Integer x Large
//*============================================================================*
/// An awesome, large, binary integer.
///
/// ### Two's Complement
///
/// Like `BinaryInteger`, its bitwise operations have two's complement semantics.
///
public protocol ANKLargeBinaryInteger<Digit>: ANKBinaryInteger where Magnitude: ANKUnsignedLargeBinaryInteger {
/// A machine word of some kind.
associatedtype Digit: ANKBinaryInteger
//=------------------------------------------------------------------------=
// MARK: Initializers
//=------------------------------------------------------------------------=
/// Creates a new instance from the given digit.
@inlinable init(digit: Digit)
//=------------------------------------------------------------------------=
// MARK: Details x Addition
//=------------------------------------------------------------------------=
/// Forms the sum of adding `rhs` to `lhs`.
///
/// ```swift
/// var a = Int256(1); a += Int(2) // a = Int256(3)
/// var b = Int256(2); b += Int(3) // b = Int256(5)
/// var c = Int256(3); c += Int(4) // c = Int256(7)
/// var d = Int256(4); d += Int(5) // d = Int256(9)
/// ```
///
@inlinable static func +=(lhs: inout Self, rhs: Digit)
/// Returns the sum of adding `rhs` to `lhs`.
///
/// ```swift
/// Int256(1) + Int(2) // Int256(3)
/// Int256(2) + Int(3) // Int256(5)
/// Int256(3) + Int(4) // Int256(7)
/// Int256(4) + Int(5) // Int256(9)
/// ```
///
@inlinable static func +(lhs: Self, rhs: Digit) -> Self
//=------------------------------------------------------------------------=
// MARK: Details x Division
//=------------------------------------------------------------------------=
/// Forms the quotient of dividing `lhs` by `rhs`.
///
/// ```swift
/// var a = Int256( 7); a /= Int( 3) // a = Int256( 2)
/// var b = Int256( 7); b /= Int(-3) // b = Int256(-2)
/// var c = Int256(-7); c /= Int( 3) // c = Int256(-2)
/// var d = Int256(-7); d /= Int(-3) // d = Int256( 2)
/// ```
///
@inlinable static func /=(lhs: inout Self, rhs: Digit)
/// Returns the quotient of dividing `lhs` by `rhs`.
///
/// ```swift
/// Int256( 7) / Int( 3) // Int256( 2)
/// Int256( 7) / Int(-3) // Int256(-2)
/// Int256(-7) / Int( 3) // Int256(-2)
/// Int256(-7) / Int(-3) // Int256( 2)
/// ```
///
@inlinable static func /(lhs: Self, rhs: Digit) -> Self
/// Forms the remainder of dividing `lhs` by `rhs`.
///
/// ```swift
/// var a = Int256( 7); a %= Int( 3) // a = Int256( 1)
/// var b = Int256( 7); b %= Int(-3) // b = Int256( 1)
/// var c = Int256(-7); c %= Int( 3) // c = Int256(-1)
/// var d = Int256(-7); d %= Int(-3) // d = Int256(-1)
/// ```
///
@inlinable static func %=(lhs: inout Self, rhs: Digit)
/// Returns the remainder of dividing `lhs` by `rhs`.
///
/// ```swift
/// Int256( 7) % Int( 3) // Int( 1)
/// Int256( 7) % Int(-3) // Int( 1)
/// Int256(-7) % Int( 3) // Int(-1)
/// Int256(-7) % Int(-3) // Int(-1)
/// ```
///
@inlinable static func %(lhs: Self, rhs: Digit) -> Digit
/// Forms the quotient of this value divided by the given value,
/// and returns a value indicating whether overflow occurred.
/// In the case of overflow, the result is either truncated or,
/// if undefined, this value.
///
/// ```swift
/// var a = Int256( 7); a.divideReportingOverflow(by: Int( 3)) // a = Int256( 2); -> false
/// var b = Int256.min; b.divideReportingOverflow(by: Int( 0)) // b = Int256.min; -> true
/// var c = Int256.min; c.divideReportingOverflow(by: Int(-1)) // c = Int256.min; -> true
/// ```
///
@inlinable mutating func divideReportingOverflow(by divisor: Digit) -> Bool
/// Returns the quotient of this value divided by the given value,
/// and returns a value indicating whether overflow occurred.
/// In the case of overflow, the result is either truncated or,
/// if undefined, this value.
///
/// ```swift
/// Int256( 7).dividedReportingOverflow(by: Int( 3)) // (partialValue: Int256( 2), overflow: false)
/// Int256.min.dividedReportingOverflow(by: Int( 0)) // (partialValue: Int256.min, overflow: true )
/// Int256.min.dividedReportingOverflow(by: Int(-1)) // (partialValue: Int256.min, overflow: true )
/// ```
///
@inlinable func dividedReportingOverflow(by divisor: Digit) -> PVO<Self>
/// Forms the remainder of this value divided by the given value,
/// and returns a value indicating whether overflow occurred.
/// In the case of overflow, the result is either the entire remainder or,
/// if undefined, zero.
///
/// ```swift
/// var a = Int256( 7); a.formRemainderReportingOverflow(dividingBy: Int( 3)) // a = Int256(1); -> false
/// var b = Int256.min; b.formRemainderReportingOverflow(dividingBy: Int( 0)) // b = Int256(0); -> true
/// var c = Int256.min; c.formRemainderReportingOverflow(dividingBy: Int(-1)) // c = Int256(0); -> true
/// ```
///
@inlinable mutating func formRemainderReportingOverflow(dividingBy divisor: Digit) -> Bool
/// Returns the remainder of this value divided by the given value,
/// and returns a value indicating whether overflow occurred.
/// In the case of overflow, the result is either the entire remainder
/// or, if undefined, zero.
///
/// ```swift
/// Int256( 7).remainderReportingOverflow(dividingBy: Int( 3)) // (partialValue: Int(1), overflow: false)
/// Int256.min.remainderReportingOverflow(dividingBy: Int( 0)) // (partialValue: Int(0), overflow: true )
/// Int256.min.remainderReportingOverflow(dividingBy: Int(-1)) // (partialValue: Int(0), overflow: true )
/// ```
///
@inlinable func remainderReportingOverflow(dividingBy divisor: Digit) -> PVO<Digit>
/// Returns the quotient and remainder of this value divided by the given value.
///
/// ```swift
/// Int256( 7).quotientAndRemainder(dividingBy: Int( 3)) // (quotient: Int256( 2), remainder: Int( 1))
/// Int256( 7).quotientAndRemainder(dividingBy: Int(-3)) // (quotient: Int256(-2), remainder: Int( 1))
/// Int256(-7).quotientAndRemainder(dividingBy: Int( 3)) // (quotient: Int256(-2), remainder: Int(-1))
/// Int256(-7).quotientAndRemainder(dividingBy: Int(-3)) // (quotient: Int256( 2), remainder: Int(-1))
/// ```
///
@inlinable func quotientAndRemainder(dividingBy divisor: Digit) -> QR<Self, Digit>
//=------------------------------------------------------------------------=
// MARK: Details x Multiplication
//=------------------------------------------------------------------------=
/// Forms the product of multiplying `lhs` by `rhs`.
///
/// ```swift
/// var a = Int256(1); a *= Int(2) // a = Int256( 2)
/// var b = Int256(2); b *= Int(3) // b = Int256( 6)
/// var c = Int256(3); c *= Int(4) // c = Int256(12)
/// var d = Int256(4); d *= Int(5) // d = Int256(20)
/// ```
///
@inlinable static func *=(lhs: inout Self, rhs: Digit)
/// Returns the product of multiplying `lhs` by `rhs`.
///
/// ```swift
/// Int256(1) * Int(2) // Int256( 2)
/// Int256(2) * Int(3) // Int256( 6)
/// Int256(3) * Int(4) // Int256(12)
/// Int256(4) * Int(5) // Int256(20)
/// ```
///
@inlinable static func *(lhs: Self, rhs: Digit) -> Self
//=------------------------------------------------------------------------=
// MARK: Details x Subtraction
//=------------------------------------------------------------------------=
/// Forms the difference of subtracting `rhs` from `lhs`.
///
/// ```swift
/// var a = Int256(3); a -= Int(2) // a = Int256(1)
/// var b = Int256(5); b -= Int(3) // b = Int256(2)
/// var c = Int256(7); c -= Int(4) // c = Int256(3)
/// var d = Int256(9); d -= Int(5) // d = Int256(4)
/// ```
///
@inlinable static func -=(lhs: inout Self, rhs: Digit)
/// Returns the difference of subtracting `rhs` from `lhs`.
///
/// ```swift
/// Int256(3) - Int(2) // Int256(1)
/// Int256(5) - Int(3) // Int256(2)
/// Int256(7) - Int(4) // Int256(3)
/// Int256(9) - Int(5) // Int256(4)
/// ```
///
@inlinable static func -(lhs: Self, rhs: Digit) -> Self
}
//=----------------------------------------------------------------------------=
// MARK: + Details where Digit == Self
//=----------------------------------------------------------------------------=
extension ANKLargeBinaryInteger where Digit == Self {
//=------------------------------------------------------------------------=
// MARK: Initializers
//=------------------------------------------------------------------------=
@_transparent public init(digit: Digit) { self = digit }
}
//*============================================================================*
// MARK: * ANK x Binary Integer x Large x Signed
//*============================================================================*
/// An awesome, signed, large, binary integer.
///
/// ### Two's Complement
///
/// Like `BinaryInteger`, its bitwise operations have two's complement semantics.
///
public protocol ANKSignedLargeBinaryInteger<Digit>: ANKLargeBinaryInteger,
ANKSignedInteger where Digit: ANKSignedInteger { }
//*============================================================================*
// MARK: * ANK x Binary Integer x Large x Unsigned
//*============================================================================*
/// An awesome, unsigned, large, binary integer.
///
/// ### Two's Complement
///
/// Like `BinaryInteger`, its bitwise operations have two's complement semantics.
///
public protocol ANKUnsignedLargeBinaryInteger<Digit>: ANKLargeBinaryInteger,
ANKUnsignedInteger where Digit: ANKUnsignedInteger, Digit.Magnitude == Digit { }