-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNBKDoubleWidth+Endianness.swift
63 lines (53 loc) · 1.89 KB
/
NBKDoubleWidth+Endianness.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
//=----------------------------------------------------------------------------=
// 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 Endianness
//*============================================================================*
extension NBKDoubleWidth {
//=------------------------------------------------------------------------=
// MARK: Initializers
//=------------------------------------------------------------------------=
@inlinable public init(bigEndian value: Self) {
#if _endian(big)
self = value
#else
self = value.byteSwapped
#endif
}
@inlinable public init(littleEndian value: Self) {
#if _endian(big)
self = value.byteSwapped
#else
self = value
#endif
}
//=------------------------------------------------------------------------=
// MARK: Transformations
//=------------------------------------------------------------------------=
@inlinable public var bigEndian: Self {
#if _endian(big)
return self
#else
return self.byteSwapped
#endif
}
@inlinable public var littleEndian: Self {
#if _endian(big)
return self.byteSwapped
#else
return self
#endif
}
@inlinable public var byteSwapped: Self {
let high = High(bitPattern: self.low .byteSwapped)
let low = Low (bitPattern: self.high.byteSwapped)
return Self(high: high, low: low)
}
}