-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNBKDoubleWidth+Complements.swift
78 lines (61 loc) · 2.89 KB
/
NBKDoubleWidth+Complements.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
//=----------------------------------------------------------------------------=
// 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 Complements
//*============================================================================*
extension NBKDoubleWidth {
//=------------------------------------------------------------------------=
// 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 Magnitude
//=------------------------------------------------------------------------=
@inlinable public var magnitude: Magnitude {
var value = self
if value.isLessThanZero {
value.formTwosComplement()
}
return Magnitude(bitPattern: value)
}
//=------------------------------------------------------------------------=
// MARK: Details x One's Complement
//=------------------------------------------------------------------------=
@inlinable public mutating func formOnesComplement() {
self.low .formOnesComplement()
self.high.formOnesComplement()
}
@inlinable public func onesComplement() -> Self {
Self(high: ~self.high, low: ~self.low)
}
//=------------------------------------------------------------------------=
// 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 carry = self.low .formTwosComplementSubsequence(carry)
return /**/ self.high.formTwosComplementSubsequence(carry)
}
@inlinable public func twosComplementSubsequence(_ carry: Bool) -> PVO<Self> {
var partialValue = self
let overflow: Bool = partialValue.formTwosComplementSubsequence(carry)
return PVO(partialValue, overflow)
}
}