-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathDoubleWidth.swift.gyb
623 lines (527 loc) · 18.5 KB
/
DoubleWidth.swift.gyb
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
//===--- DoubleWidth.swift.gyb --------------------------------*- swift -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
/// A fixed-width integer that is twice the size of its base type.
public struct DoubleWidth<Base : FixedWidthInteger> :
FixedWidthInteger, _ExpressibleByBuiltinIntegerLiteral
where Base.Words : Collection, Base.Magnitude.Words : Collection {
public typealias High = Base
public typealias Low = Base.Magnitude
internal var _storage: (high: Base, low: Base.Magnitude)
public // @testable
init(_ _value: (High, Low)) {
self._storage = (high: _value.0, low: _value.1)
}
public var high: High {
return _storage.high
}
public var low: Low {
return _storage.low
}
// Numeric
//
public init() {
self.init((0, 0))
}
// BinaryInteger
//
public var magnitude: DoubleWidth<Low> {
if Base.isSigned && _storage.high < (0 as High) {
return self == .min
? DoubleWidth.max.magnitude &+ 1
: (0 - self).magnitude
}
return DoubleWidth<Low>((
_storage.high.magnitude, _storage.low.magnitude))
}
internal init(_ _magnitude: Magnitude) {
self.init((High(_magnitude._storage.high), _magnitude._storage.low))
}
public static func ==(lhs: DoubleWidth, rhs: DoubleWidth) -> Bool {
return (lhs._storage.high == rhs._storage.high) &&
(lhs._storage.low == rhs._storage.low)
}
public static func <(lhs: DoubleWidth, rhs: DoubleWidth) -> Bool {
if lhs._storage.high < rhs._storage.high {
return true
}
if lhs._storage.high > rhs._storage.high {
return false
}
return lhs._storage.low < rhs._storage.low
}
public init<T : BinaryInteger>(_ source: T) {
self.init(exactly: source)!
}
public init?<T : BinaryInteger>(exactly source: T) {
// Can't represent a negative 'source' if Base is unsigned
guard source >= 0 || DoubleWidth.isSigned else { return nil }
// Is 'source' is entirely representable in Low?
if let low = Low(exactly: source.magnitude) {
if source < (0 as T) {
self.init((~0, ~low + 1))
} else {
self.init((0, low))
}
} else {
// At this point we know source's bitWidth > Base.bitWidth, or else
// we would've taken the first branch.
let lowInT = source & T(~0 as Low)
let highInT = source &>> numericCast(High.bitWidth)
let low = Low(lowInT.magnitude)
guard let high = High(exactly: highInT) else { return nil }
self.init((high, low))
}
}
public init<T : FloatingPoint>(_ source: T) {
fatalError()
}
public init?<T : FloatingPoint>(exactly source: T) {
fatalError()
}
public init<T : BinaryFloatingPoint>(_ source: T)
where T.RawSignificand : FixedWidthInteger
{
_precondition(source.isFinite, "Can't create a DoubleWidth from a non-finite value")
self.init(exactly: source.rounded(.towardZero))!
}
public init?<T : BinaryFloatingPoint>(exactly source: T)
where T.RawSignificand : FixedWidthInteger
{
// Need a finite value
guard source.isFinite else { return nil }
// Don't need to go further with zero.
if source.isZero {
self.init(0)
return
}
// Need a value with a non-negative exponent
guard source.exponent >= 0 else { return nil }
typealias Raw = T.RawSignificand
let bitPattern = source.significandBitPattern |
((1 as Raw) &<< Raw(T.significandBitCount))
let offset = T.significandBitCount - Int(source.exponent)
// FIXME: spurious compile error when 'where' clause above is removed:
// error: non-nominal type 'T.RawSignificand' does not support explicit initialization
let fractionPart: Raw = bitPattern &<< Raw(Raw.bitWidth - offset)
guard fractionPart == (0 as Raw) else {
return nil
}
let integerPart: Raw = bitPattern &>> Raw(offset)
// Should have caught any actual zero values above
_sanityCheck(integerPart > (0 as Raw))
if source.sign == .minus {
if !DoubleWidth.isSigned || integerPart &- 1 > DoubleWidth.max {
return nil
}
// Have to juggle, or else the intermediate step of creating a value
// with Self.min's magnitude will overflow. It's okay to use wrapping
// subtraction because integerPart > 0.
self.init(integerPart &- 1)
self = 0 &- self &- 1
} else {
self.init(exactly: integerPart)
}
}
public struct Words : Collection {
public enum _IndexValue {
case low(Base.Magnitude.Words.Index)
case high(Base.Words.Index)
}
public struct Index : Comparable {
public var _value: _IndexValue
public init(_ _value: _IndexValue) { self._value = _value }
public static func ==(lhs: Index, rhs: Index) -> Bool {
switch (lhs._value, rhs._value) {
case let (.low(l), .low(r)): return l == r
case let (.high(l), .high(r)): return l == r
default: return false
}
}
public static func <(lhs: Index, rhs: Index) -> Bool {
switch (lhs._value, rhs._value) {
case let (.low(l), .low(r)): return l < r
case (.low(_), .high(_)): return true
case (.high(_), .low(_)): return false
case let (.high(l), .high(r)): return l < r
}
}
}
public var _high: Base.Words
public var _low: Base.Magnitude.Words
public init(_ value: DoubleWidth<Base>) {
// multiples of word-size only
guard Base.bitWidth == Base.Magnitude.bitWidth &&
(UInt.bitWidth % Base.bitWidth == 0 ||
Base.bitWidth % UInt.bitWidth == 0) else {
fatalError("Access to words is not supported on this type")
}
self._high = value._storage.high.words
self._low = value._storage.low.words
_sanityCheck(!_low.isEmpty)
}
public var startIndex: Index {
return Index(.low(_low.startIndex))
}
public var endIndex: Index {
return Index(.high(_high.endIndex))
}
public var count: Int {
if Base.bitWidth < UInt.bitWidth { return 1 }
return Int(_low.count) + Int(_high.count)
}
public func index(after i: Index) -> Index {
switch i._value {
case let .low(li):
if Base.bitWidth < UInt.bitWidth {
return Index(.high(_high.endIndex))
}
let next = _low.index(after: li)
if next == _low.endIndex {
return Index(.high(_high.startIndex))
}
return Index(.low(next))
case let .high(hi):
return Index(.high(_high.index(after: hi)))
}
}
public subscript(_ i: Index) -> UInt {
if Base.bitWidth < UInt.bitWidth {
_precondition(i == Index(.low(_low.startIndex)), "Invalid index")
_sanityCheck(2 * Base.bitWidth <= UInt.bitWidth)
return _low.first! | (_high.first! &<< Base.bitWidth._lowWord)
}
switch i._value {
case let .low(li): return _low[li]
case let .high(hi): return _high[hi]
}
}
}
public var words: Words {
return Words(self)
}
public static var isSigned: Bool {
return Base.isSigned
}
// fixed width
//
public static var max: DoubleWidth {
return self.init((High.max, Low.max))
}
public static var min: DoubleWidth {
return self.init((High.min, Low.min))
}
public static var bitWidth: Int {
return 2 * Base.bitWidth
}
% for (operator, name) in [('+', 'adding'), ('-', 'subtracting')]:
% highAffectedByLowOverflow = 'Base.max' if operator == '+' else 'Base.min'
public func ${name}ReportingOverflow(_ rhs: DoubleWidth)
-> (partialValue: DoubleWidth, overflow: ArithmeticOverflow) {
let (low, lowOverflow) =
_storage.low.${name}ReportingOverflow(rhs._storage.low)
let (high, highOverflow) =
_storage.high.${name}ReportingOverflow(rhs._storage.high)
let isLowOverflow = lowOverflow == .overflow
let result = (high &${operator} (isLowOverflow ? 1 : 0), low)
let overflow = ArithmeticOverflow(
highOverflow == .overflow ||
high == ${highAffectedByLowOverflow} && isLowOverflow
)
return (partialValue: DoubleWidth(result),
overflow: overflow)
}
% end
public func multipliedReportingOverflow(
by rhs: DoubleWidth
) -> (partialValue: DoubleWidth, overflow: ArithmeticOverflow) {
let (carry, product) = multipliedFullWidth(by: rhs)
let result = DoubleWidth(extendingOrTruncating: product)
let isNegative = (self < (0 as DoubleWidth)) != (rhs < (0 as DoubleWidth))
let didCarry = isNegative
? carry != ~(0 as DoubleWidth)
: carry != (0 as DoubleWidth)
let hadPositiveOverflow = !isNegative &&
DoubleWidth.isSigned && product.leadingZeroBitCount == 0
return (result, ArithmeticOverflow(didCarry || hadPositiveOverflow))
}
public func quotientAndRemainder(dividingBy other: DoubleWidth)
-> (quotient: DoubleWidth, remainder: DoubleWidth) {
let isNegative = (self < (0 as DoubleWidth)) != (other < (0 as DoubleWidth))
let rhs = other.magnitude
var q = self.magnitude
// Bail if |other| > |self|
if rhs.leadingZeroBitCount < q.leadingZeroBitCount {
return (0, self)
}
// Calculate the number of bits before q and rhs line up,
// we can skip that many bits of iteration.
let initialOffset = q.leadingZeroBitCount +
(DoubleWidth.bitWidth - rhs.leadingZeroBitCount) - 1
// TODO(performance): Use &>> instead here?
// Start with remainder capturing the high bits of q.
var r = q >> Magnitude(DoubleWidth.bitWidth - initialOffset)
q <<= Magnitude(initialOffset)
let highBit = ~(~0 >> 1) as Magnitude
for _ in initialOffset..<DoubleWidth.bitWidth {
r <<= 1
if q & highBit != (0 as Magnitude) {
r += 1 as Magnitude
}
q <<= 1
if r >= rhs {
q |= 1
r -= rhs
}
}
// Sign of remainder matches dividend
let remainder = self < (0 as DoubleWidth)
? 0 - DoubleWidth(r)
: DoubleWidth(r)
if isNegative {
return (0 - DoubleWidth(q), remainder)
} else {
return (DoubleWidth(q), remainder)
}
}
public func dividedReportingOverflow(by other: DoubleWidth)
-> (partialValue: DoubleWidth, overflow: ArithmeticOverflow) {
if other == (0 as DoubleWidth) ||
(DoubleWidth.isSigned && other == (-1 as Int) && self == .min)
{
return (self, .overflow)
}
return (quotientAndRemainder(dividingBy: other).quotient, .none)
}
public func remainderReportingOverflow(dividingBy other: DoubleWidth)
-> (partialValue: DoubleWidth, overflow: ArithmeticOverflow) {
if other == 0 ||
(DoubleWidth.isSigned && other == -1 && self == .min)
{
return (self, .overflow)
}
return (quotientAndRemainder(dividingBy: other).remainder, .none)
}
public func multipliedFullWidth(by other: DoubleWidth)
-> (high: DoubleWidth, low: DoubleWidth.Magnitude) {
let isNegative = DoubleWidth.isSigned &&
(self < (0 as DoubleWidth)) != (other < (0 as DoubleWidth))
func mul(_ x: Low, _ y: Low) -> (partial: Low, carry: Low) {
let (high, low) = x.multipliedFullWidth(by: y)
return (low, high)
}
func sum(_ x: Low, _ y: Low, _ z: Low) -> (partial: Low, carry: Low) {
let (sum1, overflow1) = x.addingReportingOverflow(y)
let (sum2, overflow2) = sum1.addingReportingOverflow(z)
let carry: Low = (overflow1 == .overflow ? 1 : 0) +
(overflow2 == .overflow ? 1 : 0)
return (sum2, carry)
}
let lhs = self.magnitude
let rhs = other.magnitude
let a = mul(rhs._storage.low, lhs._storage.low)
let b = mul(rhs._storage.low, lhs._storage.high)
let c = mul(rhs._storage.high, lhs._storage.low)
let d = mul(rhs._storage.high, lhs._storage.high)
let mid1 = sum(a.carry, b.partial, c.partial)
let mid2 = sum(b.carry, c.carry, d.partial)
let low = DoubleWidth<Low>((mid1.partial, a.partial))
let high = DoubleWidth(
(High(mid2.carry + d.carry), mid1.carry + mid2.partial))
if isNegative {
let (lowComplement, overflow) = (~low).addingReportingOverflow(1)
return (~high + (overflow == .overflow ? 1 : 0), lowComplement)
} else {
return (high, low)
}
}
public func dividingFullWidth(
_ dividend: (high: DoubleWidth, low: DoubleWidth.Magnitude)
) -> (quotient: DoubleWidth, remainder: DoubleWidth) {
let lhs = DoubleWidth<DoubleWidth<Base>>(dividend)
let rhs = DoubleWidth<DoubleWidth<Base>>(self)
let (quotient, remainder) = lhs.quotientAndRemainder(dividingBy: rhs)
// FIXME(integers): check for overflow of quotient and remainder
return (DoubleWidth(quotient.low), DoubleWidth(remainder.low))
}
% for operator in ['&', '|', '^']:
public static func ${operator}=(
lhs: inout DoubleWidth, rhs: DoubleWidth
) {
lhs._storage.low ${operator}= rhs._storage.low
lhs._storage.high ${operator}= rhs._storage.high
}
% end
public static func <<=(lhs: inout DoubleWidth, rhs: DoubleWidth) {
if rhs < (0 as DoubleWidth) {
lhs >>= 0 - rhs
return
}
if rhs._storage.high != (0 as High) ||
rhs._storage.low >= DoubleWidth.bitWidth
{
lhs = 0
return
}
// Shift is exactly the width of `Base`, so low -> high.
if rhs._storage.low == Base.bitWidth {
lhs = DoubleWidth((High(extendingOrTruncating: lhs._storage.low), 0))
return
}
lhs &<<= rhs
}
public static func >>=(lhs: inout DoubleWidth, rhs: DoubleWidth) {
if rhs < (0 as DoubleWidth) {
lhs <<= 0 - rhs
return
}
// Shift is larger than this type's bit width.
if rhs._storage.high != (0 as High) ||
rhs._storage.low >= DoubleWidth.bitWidth
{
lhs = lhs < (0 as DoubleWidth) ? ~0 : 0
return
}
// Shift is exactly the width of `Base`, so high -> low.
if rhs._storage.low == Base.bitWidth {
lhs = DoubleWidth((
lhs < (0 as DoubleWidth) ? ~0 : 0,
Low(extendingOrTruncating: lhs._storage.high)
))
return
}
lhs &>>= rhs
}
public static func &<<=(lhs: inout DoubleWidth, rhs: DoubleWidth) {
let rhs = rhs & DoubleWidth(DoubleWidth.bitWidth - 1)
lhs._storage.high <<= High(rhs._storage.low)
if Base.bitWidth > rhs._storage.low {
lhs._storage.high |= High(extendingOrTruncating: lhs._storage.low >>
(numericCast(Base.bitWidth) - rhs._storage.low))
} else {
lhs._storage.high |= High(extendingOrTruncating: lhs._storage.low <<
(rhs._storage.low - numericCast(Base.bitWidth)))
}
lhs._storage.low <<= rhs._storage.low
}
public static func &>>=(lhs: inout DoubleWidth, rhs: DoubleWidth) {
let rhs = rhs & DoubleWidth(DoubleWidth.bitWidth - 1)
lhs._storage.low >>= rhs._storage.low
if Base.bitWidth > rhs._storage.low {
lhs._storage.low |= Low(extendingOrTruncating: lhs._storage.high <<
numericCast(numericCast(Base.bitWidth) - rhs._storage.low))
} else {
lhs._storage.low |= Low(extendingOrTruncating: lhs._storage.high >>
numericCast(rhs._storage.low - numericCast(Base.bitWidth)))
}
lhs._storage.high >>= High(extendingOrTruncating: rhs._storage.low)
}
%{
binaryOperators = [
('+', 'adding', '_', '+'),
('-', 'subtracting', '_', '-'),
('*', 'multiplied', 'by', '*'),
('/', 'divided', 'by', '/'),
('%', 'remainder', 'dividingBy', '/'),
]
}%
% for (operator, name, firstArg, kind) in binaryOperators:
// FIXME(integers): remove this once the operators are back to Numeric
public static func ${operator} (
lhs: DoubleWidth, rhs: DoubleWidth
) -> DoubleWidth {
var lhs = lhs
lhs ${operator}= rhs
return lhs
}
% argumentLabel = (firstArg + ':') if firstArg != '_' else ''
public static func ${operator}=(
lhs: inout DoubleWidth, rhs: DoubleWidth
) {
let (result, overflow) = lhs.${name}ReportingOverflow(${argumentLabel}rhs)
_precondition(overflow == .none, "Overflow in ${operator}=")
lhs = result
}
% end
public init(_truncatingBits bits: UInt) {
_storage.low = Low(_truncatingBits: bits)
_storage.high = High(_truncatingBits: bits >> UInt(Base.bitWidth))
}
// other
//
public init(_builtinIntegerLiteral x: _MaxBuiltinIntegerType) {
// FIXME: This won't work if `x` is out of range for `Int64`
self.init(Int64(_builtinIntegerLiteral: x))
}
public var description: String {
return "(\(_storage.high), \(_storage.low))"
}
public var leadingZeroBitCount: Int {
return high == (0 as High)
? Base.bitWidth + low.leadingZeroBitCount
: high.leadingZeroBitCount
}
public var trailingZeroBitCount: Int {
return low == (0 as Low)
? Base.bitWidth + high.trailingZeroBitCount
: low.trailingZeroBitCount
}
public var nonzeroBitCount: Int {
return high.nonzeroBitCount + low.nonzeroBitCount
}
public var hashValue: Int {
return _mixInt(high.hashValue) ^ low.hashValue
}
@_transparent
public var byteSwapped: DoubleWidth {
return DoubleWidth((High(extendingOrTruncating: low.byteSwapped),
Low(extendingOrTruncating: high.byteSwapped)))
}
}
// FIXME(ABI) (Conditional Conformance):
// DoubleWidth should conform to SignedInteger where Base : SignedInteger
extension DoubleWidth where Base : SignedInteger {
/// Returns the additive inverse of the specified value.
///
/// The negation operator (prefix `-`) returns the additive inverse of its
/// argument.
///
/// let x = 21 as DoubleWidth<Int>
/// let y = -x
/// // y == -21
///
/// The resulting value must be representable in the same type as the
/// argument. In particular, negating a signed, fixed-width integer type's
/// minimum results in a value that cannot be represented.
///
/// let z = -DoubleWidth<Int>.min
/// // Overflow error
///
/// - Returns: The additive inverse of this value.
///
/// - SeeAlso: `negate()`
public static prefix func - (_ operand: DoubleWidth) -> DoubleWidth {
return 0 - operand
}
/// Replaces this value with its additive inverse.
///
/// The following example uses the `negate()` method to negate the value of
/// an integer `x`:
///
/// var x = 21 as DoubleWidth<Int>
/// x.negate()
/// // x == -21
///
/// - SeeAlso: The unary minus operator (`-`).
public mutating func negate() {
self = 0 - self
}
}