0001    //
0002    //  Bitwise Hacks.swift
0003    //  BigInt
0004    //
0005    //  Created by Károly Lőrentey on 2015-12-27.
0006    //  Copyright © 2016 Károly Lőrentey. All rights reserved.
0007    //
0008    
0009    import Foundation
0010    
0011    /// For all `i` less than 256, `leadingZeroesTable[i]` is the number of leading zero bits in `i`'s 8-bit representation.
0012    /// I.e., the minimum number of bits necessary to represent `i` is `8 - leadingZeroes[i]`.
0013    internal let leadingZeroesTable
Bitwise Hacks.swift:74
    internal var leadingZeroes: Int { return Int(leadingZeroesTable[Int(self)]) }
: [UInt8] = [ 0014 8, 0015 7, 0016 6, 6, 0017 5, 5, 5, 5, 0018 4, 4, 4, 4, 4, 4, 4, 4, 0019 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0020 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0021 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0022 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0023 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0024 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0025 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0026 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0027 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0028 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0029 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0030 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0031 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0032 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0033 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0034 ] 0035 /// For all `i` less than 256, `trailingZeroesTable[i]` is the number of trailing zero bits in `i`'s 8-bit representation. 0036 internal let trailingZeroesTable
Bitwise Hacks.swift:75
    internal var trailingZeroes: Int { return Int(trailingZeroesTable[Int(self)]) }
: [UInt8] = [ 0037 /*0*/ 8, 0, 0038 /*1*/ 1, 0, 0039 /*2*/ 2, 0, /*1*/ 1, 0, 0040 /*3*/ 3, 0, /*1*/ 1, 0, 0041 /*2*/ 2, 0, 1, 0, 0042 /*4*/ 4, 0, /*1*/ 1, 0, 0043 /*2*/ 2, 0, 1, 0, 0044 /*3*/ 3, 0, 1, 0, 2, 0, 1, 0, 0045 /*5*/ 5, 0, /*1*/ 1, 0, 0046 /*2*/ 2, 0, 1, 0, 0047 /*3*/ 3, 0, 1, 0, 2, 0, 1, 0, 0048 /*4*/ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 0049 /*6*/ 6, 0, /*1*/ 1, 0, 0050 /*2*/ 2, 0, 1, 0, 0051 /*3*/ 3, 0, 1, 0, 2, 0, 1, 0, 0052 /*4*/ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 0053 /*5*/ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 0054 0055 /*7*/ 7, 0, /*1*/ 1, 0, 0056 /*2*/ 2, 0, 1, 0, 0057 /*3*/ 3, 0, 1, 0, 2, 0, 1, 0, 0058 /*4*/ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 0059 /*5*/ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 0060 /*6*/ 6, 0, 1, 0, 0061 2, 0, 1, 0, 0062 3, 0, 1, 0, 2, 0, 1, 0, 0063 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 0064 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 0065 ] 0066 0067 extension UInt8 { 0068 internal static var width: Int { return 8 } 0069 0070 internal var low
Bitwise Hacks.swift:72
    internal var split: (high: UInt8, low: UInt8) { return (high, low) }
: UInt8 { return self & 0xF } 0071 internal var high
Bitwise Hacks.swift:72
    internal var split: (high: UInt8, low: UInt8) { return (high, low) }
: UInt8 { return self >> 4 } 0072 internal var split: (high: UInt8, low: UInt8) { return (high, low) } 0073 0074 internal var leadingZeroes
Bitwise Hacks.swift:84
    internal var leadingZeroes: Int { return high == 0 ? 8 + UInt8(low).leadingZeroes : UInt8(high).leadingZeroes }
Bitwise Hacks.swift:84
    internal var leadingZeroes: Int { return high == 0 ? 8 + UInt8(low).leadingZeroes : UInt8(high).leadingZeroes }
: Int { return Int(leadingZeroesTable[Int(self)]) } 0075 internal var trailingZeroes
Bitwise Hacks.swift:85
    internal var trailingZeroes: Int { return low == 0 ? 8 + UInt8(high).trailingZeroes : UInt8(low).trailingZeroes }
Bitwise Hacks.swift:85
    internal var trailingZeroes: Int { return low == 0 ? 8 + UInt8(high).trailingZeroes : UInt8(low).trailingZeroes }
: Int { return Int(trailingZeroesTable[Int(self)]) } 0076 } 0077 extension UInt16 { 0078 internal static var width: Int { return 16 } 0079 0080 internal var low
Bitwise Hacks.swift:82
    internal var split: (high: UInt16, low: UInt16) { return (high, low) }
Bitwise Hacks.swift:84
    internal var leadingZeroes: Int { return high == 0 ? 8 + UInt8(low).leadingZeroes : UInt8(high).leadingZeroes }
Bitwise Hacks.swift:85
    internal var trailingZeroes: Int { return low == 0 ? 8 + UInt8(high).trailingZeroes : UInt8(low).trailingZeroes }
Bitwise Hacks.swift:85
    internal var trailingZeroes: Int { return low == 0 ? 8 + UInt8(high).trailingZeroes : UInt8(low).trailingZeroes }
: UInt16 { return self & 0xFF } 0081 internal var high
Bitwise Hacks.swift:82
    internal var split: (high: UInt16, low: UInt16) { return (high, low) }
Bitwise Hacks.swift:84
    internal var leadingZeroes: Int { return high == 0 ? 8 + UInt8(low).leadingZeroes : UInt8(high).leadingZeroes }
Bitwise Hacks.swift:84
    internal var leadingZeroes: Int { return high == 0 ? 8 + UInt8(low).leadingZeroes : UInt8(high).leadingZeroes }
Bitwise Hacks.swift:85
    internal var trailingZeroes: Int { return low == 0 ? 8 + UInt8(high).trailingZeroes : UInt8(low).trailingZeroes }
: UInt16 { return self >> 8 } 0082 internal var split: (high: UInt16, low: UInt16) { return (high, low) } 0083 0084 internal var leadingZeroes
Bitwise Hacks.swift:94
    internal var leadingZeroes: Int { return high == 0 ? 16 + UInt16(low).leadingZeroes : UInt16(high).leadingZeroes }
Bitwise Hacks.swift:94
    internal var leadingZeroes: Int { return high == 0 ? 16 + UInt16(low).leadingZeroes : UInt16(high).leadingZeroes }
: Int { return high == 0 ? 8 + UInt8(low).leadingZeroes : UInt8(high).leadingZeroes } 0085 internal var trailingZeroes
Bitwise Hacks.swift:95
    internal var trailingZeroes: Int { return low == 0 ? 16 + UInt16(high).trailingZeroes : UInt16(low).trailingZeroes }
Bitwise Hacks.swift:95
    internal var trailingZeroes: Int { return low == 0 ? 16 + UInt16(high).trailingZeroes : UInt16(low).trailingZeroes }
: Int { return low == 0 ? 8 + UInt8(high).trailingZeroes : UInt8(low).trailingZeroes } 0086 } 0087 extension UInt32 { 0088 internal static var width: Int { return 32 } 0089 0090 internal var low
Bitwise Hacks.swift:92
    internal var split: (high: UInt32, low: UInt32) { return (high, low) }
Bitwise Hacks.swift:94
    internal var leadingZeroes: Int { return high == 0 ? 16 + UInt16(low).leadingZeroes : UInt16(high).leadingZeroes }
Bitwise Hacks.swift:95
    internal var trailingZeroes: Int { return low == 0 ? 16 + UInt16(high).trailingZeroes : UInt16(low).trailingZeroes }
Bitwise Hacks.swift:95
    internal var trailingZeroes: Int { return low == 0 ? 16 + UInt16(high).trailingZeroes : UInt16(low).trailingZeroes }
: UInt32 { return self & 0xFFFF } 0091 internal var high
Bitwise Hacks.swift:92
    internal var split: (high: UInt32, low: UInt32) { return (high, low) }
Bitwise Hacks.swift:94
    internal var leadingZeroes: Int { return high == 0 ? 16 + UInt16(low).leadingZeroes : UInt16(high).leadingZeroes }
Bitwise Hacks.swift:94
    internal var leadingZeroes: Int { return high == 0 ? 16 + UInt16(low).leadingZeroes : UInt16(high).leadingZeroes }
Bitwise Hacks.swift:95
    internal var trailingZeroes: Int { return low == 0 ? 16 + UInt16(high).trailingZeroes : UInt16(low).trailingZeroes }
: UInt32 { return self >> 16 } 0092 internal var split: (high: UInt32, low: UInt32) { return (high, low) } 0093 0094 internal var leadingZeroes
Bitwise Hacks.swift:104
    internal var leadingZeroes: Int { return high == 0 ? 32 + UInt32(low).leadingZeroes : UInt32(high).leadingZeroes }
Bitwise Hacks.swift:104
    internal var leadingZeroes: Int { return high == 0 ? 32 + UInt32(low).leadingZeroes : UInt32(high).leadingZeroes }
Bitwise Hacks.swift:120
    internal var leadingZeroes: Int { return high == 0 ? 32 + UInt32(low).leadingZeroes : UInt32(high).leadingZeroes }
Bitwise Hacks.swift:120
    internal var leadingZeroes: Int { return high == 0 ? 32 + UInt32(low).leadingZeroes : UInt32(high).leadingZeroes }
: Int { return high == 0 ? 16 + UInt16(low).leadingZeroes : UInt16(high).leadingZeroes } 0095 internal var trailingZeroes
Bitwise Hacks.swift:105
    internal var trailingZeroes: Int { return low == 0 ? 32 + UInt32(high).trailingZeroes : UInt32(low).trailingZeroes }
Bitwise Hacks.swift:105
    internal var trailingZeroes: Int { return low == 0 ? 32 + UInt32(high).trailingZeroes : UInt32(low).trailingZeroes }
Bitwise Hacks.swift:121
    internal var trailingZeroes: Int { return low == 0 ? 32 + UInt32(high).trailingZeroes : UInt32(low).trailingZeroes }
Bitwise Hacks.swift:121
    internal var trailingZeroes: Int { return low == 0 ? 32 + UInt32(high).trailingZeroes : UInt32(low).trailingZeroes }
: Int { return low == 0 ? 16 + UInt16(high).trailingZeroes : UInt16(low).trailingZeroes } 0096 } 0097 extension UInt64 { 0098 internal static var width
BigDigit.swift:75
        var width = UIntMax.width - remaining.leadingZeroes
BigDigit.swift:91
        var width = UIntMax.width - remaining.leadingZeroes
BigUInt Bitwise Ops.swift:21
        return count * Digit.width - self[count - 1].leadingZeroes
BigUInt Bitwise Ops.swift:44
        return i * Digit.width + self[i].trailingZeroes
BigUInt Data.swift:18
        precondition(Digit.width % 8 == 0)
BigUInt Data.swift:24
        let bytesPerDigit = Digit.width / 8
BigUInt Data.swift:53
        precondition(Digit.width % 8 == 0)
BigUInt Data.swift:63
            for _ in 0 ..< Digit.width / 8 {
BigUInt Random.swift:44
        result[(width - 1) / Digit.width] |= 1 << Digit((width - 1) % Digit.width)
BigUInt Random.swift:44
        result[(width - 1) / Digit.width] |= 1 << Digit((width - 1) % Digit.width)
BigUInt Shifts.swift:22
    let ext = amount / Digit.width // External shift amount (new digits)
BigUInt Shifts.swift:23
    let up = Digit(amount % Digit.width) // Internal shift amount (subdigit shift)
BigUInt Shifts.swift:24
    let down = Digit(Digit.width) - up
BigUInt Shifts.swift:54
    let ext = amount / Digit.width // External shift amount (new digits)
BigUInt Shifts.swift:55
    let up = Digit(amount % Digit.width) // Internal shift amount (subdigit shift)
BigUInt Shifts.swift:56
    let down = Digit(Digit.width) - up
BigUInt Shifts.swift:86
    let ext = amount / Digit.width // External shift amount (new digits)
BigUInt Shifts.swift:87
    let down = Digit(amount % Digit.width) // Internal shift amount (subdigit shift)
BigUInt Shifts.swift:88
    let up = Digit(Digit.width) - down
BigUInt Shifts.swift:125
    let ext = amount / Digit.width // External shift amount (new digits)
BigUInt Shifts.swift:126
    let down = Digit(amount % Digit.width) // Internal shift amount (subdigit shift)
BigUInt Shifts.swift:127
    let up = Digit(Digit.width) - down
: Int { return 64 } 0099 0100 internal var low
BigDigit.swift:52
    internal static func digitsFromUIntMax(i: UIntMax) -> [UInt32] { return [UInt32(i.low), UInt32(i.high)] }
BigDigit.swift:59
        return (UInt32(p.high), UInt32(p.low))
Bitwise Hacks.swift:102
    internal var split: (high: UInt64, low: UInt64) { return (high, low) }
Bitwise Hacks.swift:104
    internal var leadingZeroes: Int { return high == 0 ? 32 + UInt32(low).leadingZeroes : UInt32(high).leadingZeroes }
Bitwise Hacks.swift:105
    internal var trailingZeroes: Int { return low == 0 ? 32 + UInt32(high).trailingZeroes : UInt32(low).trailingZeroes }
Bitwise Hacks.swift:105
    internal var trailingZeroes: Int { return low == 0 ? 32 + UInt32(high).trailingZeroes : UInt32(low).trailingZeroes }
: UInt64 { return self & 0xFFFFFFFF } 0101 internal var high
BigDigit.swift:52
    internal static func digitsFromUIntMax(i: UIntMax) -> [UInt32] { return [UInt32(i.low), UInt32(i.high)] }
BigDigit.swift:59
        return (UInt32(p.high), UInt32(p.low))
BigUInt Division.swift:132
        assert(divisor.count == y.count && divisor.last!.high > 0)
Bitwise Hacks.swift:102
    internal var split: (high: UInt64, low: UInt64) { return (high, low) }
Bitwise Hacks.swift:104
    internal var leadingZeroes: Int { return high == 0 ? 32 + UInt32(low).leadingZeroes : UInt32(high).leadingZeroes }
Bitwise Hacks.swift:104
    internal var leadingZeroes: Int { return high == 0 ? 32 + UInt32(low).leadingZeroes : UInt32(high).leadingZeroes }
Bitwise Hacks.swift:105
    internal var trailingZeroes: Int { return low == 0 ? 32 + UInt32(high).trailingZeroes : UInt32(low).trailingZeroes }
: UInt64 { return self >> 32 } 0102 internal var split: (high: UInt64, low: UInt64) { return (high, low) } 0103 0104 internal var leadingZeroes
BigDigit.swift:75
        var width = UIntMax.width - remaining.leadingZeroes
BigDigit.swift:91
        var width = UIntMax.width - remaining.leadingZeroes
BigUInt Bitwise Ops.swift:21
        return count * Digit.width - self[count - 1].leadingZeroes
BigUInt Bitwise Ops.swift:33
        return self[count - 1].leadingZeroes
: Int { return high == 0 ? 32 + UInt32(low).leadingZeroes : UInt32(high).leadingZeroes } 0105 internal var trailingZeroes
BigUInt Bitwise Ops.swift:44
        return i * Digit.width + self[i].trailingZeroes
: Int { return low == 0 ? 32 + UInt32(high).trailingZeroes : UInt32(low).trailingZeroes } 0106 } 0107 0108 extension UInt { 0109 internal var low
Bitwise Hacks.swift:117
    internal var split: (high: UInt, low: UInt) { return (high, low) }
Bitwise Hacks.swift:120
    internal var leadingZeroes: Int { return high == 0 ? 32 + UInt32(low).leadingZeroes : UInt32(high).leadingZeroes }
Bitwise Hacks.swift:121
    internal var trailingZeroes: Int { return low == 0 ? 32 + UInt32(high).trailingZeroes : UInt32(low).trailingZeroes }
Bitwise Hacks.swift:121
    internal var trailingZeroes: Int { return low == 0 ? 32 + UInt32(high).trailingZeroes : UInt32(low).trailingZeroes }
: UInt { 0110 precondition(sizeof(UInt) == 8) 0111 return self & 0xFFFFFFFF 0112 } 0113 internal var high
Bitwise Hacks.swift:117
    internal var split: (high: UInt, low: UInt) { return (high, low) }
Bitwise Hacks.swift:120
    internal var leadingZeroes: Int { return high == 0 ? 32 + UInt32(low).leadingZeroes : UInt32(high).leadingZeroes }
Bitwise Hacks.swift:120
    internal var leadingZeroes: Int { return high == 0 ? 32 + UInt32(low).leadingZeroes : UInt32(high).leadingZeroes }
Bitwise Hacks.swift:121
    internal var trailingZeroes: Int { return low == 0 ? 32 + UInt32(high).trailingZeroes : UInt32(low).trailingZeroes }
: UInt { 0114 precondition(sizeof(UInt) == 8) 0115 return self >> 32 0116 } 0117 internal var split: (high: UInt, low: UInt) { return (high, low) } 0118 internal static let halfShift: UInt = 32 0119 0120 internal var leadingZeroes: Int { return high == 0 ? 32 + UInt32(low).leadingZeroes : UInt32(high).leadingZeroes } 0121 internal var trailingZeroes: Int { return low == 0 ? 32 + UInt32(high).trailingZeroes : UInt32(low).trailingZeroes } 0122 } 0123