-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNBKDoubleWidth+Bits.swift
67 lines (53 loc) · 2.43 KB
/
NBKDoubleWidth+Bits.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
//=----------------------------------------------------------------------------=
// 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.
//=----------------------------------------------------------------------------=
import NBKCoreKit
//*============================================================================*
// MARK: * NBK x Double Width x Bits
//*============================================================================*
extension NBKDoubleWidth {
//=------------------------------------------------------------------------=
// MARK: Accessors
//=------------------------------------------------------------------------=
@inlinable public static var bitWidth: Int {
assert(MemoryLayout<Self>.size / MemoryLayout<UInt>.stride >= 2)
assert(MemoryLayout<Self>.size % MemoryLayout<UInt>.stride == 0)
return MemoryLayout<Self>.size * 8
}
//=------------------------------------------------------------------------=
// MARK: Initializers
//=------------------------------------------------------------------------=
@inlinable public init(bit: Bool) {
self.init(low: Low(bit: bit))
}
@inlinable public init(repeating bit: Bool) {
self.init(bitPattern: bit ? Magnitude.max : Magnitude.min)
}
//=------------------------------------------------------------------------=
// MARK: Accessors
//=------------------------------------------------------------------------=
@inlinable public var nonzeroBitCount: Int {
self.low.nonzeroBitCount &+ self.high.nonzeroBitCount
}
@inlinable public var leadingZeroBitCount: Int {
let result = self.high.leadingZeroBitCount
guard result == High.bitWidth else { return result }
return result &+ self.low .leadingZeroBitCount
}
@inlinable public var trailingZeroBitCount: Int {
let result = self.low .trailingZeroBitCount
guard result == Low .bitWidth else { return result }
return result &+ self.high.trailingZeroBitCount
}
@inlinable public var mostSignificantBit: Bool {
self.high.mostSignificantBit
}
@inlinable public var leastSignificantBit: Bool {
self.low.leastSignificantBit
}
}