-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathBitter.swift.gyb
More file actions
274 lines (250 loc) · 9.88 KB
/
Bitter.swift.gyb
File metadata and controls
274 lines (250 loc) · 9.88 KB
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
//
// Bitter.swift
// Bitter
//
// Created by Umberto Raimondi on 01/02/16.
// Copyright © 2016 Umberto Raimondi. All rights reserved.
//
// MARK: Int types extensions
%{
intTypes = [8,16,32,64]
}%
% for sign in ['','U']:
/**
Extension that adds a few additional functionalities to ${sign}Int:
- toIntN/toUIntN truncating bit pattern conversions
- allOnes
- size
- Byte indexed subscript
*/
extension ${sign}Int{
% for intType in intTypes[:3]:
/// Perform a bit pattern truncating conversion to UInt${intType}
public var toU${intType}: UInt${intType}{return UInt${intType}(truncatingIfNeeded:self)}
/// Perform a bit pattern truncating conversion to Int${intType}
public var to${intType}: Int${intType}{return Int${intType}(truncatingIfNeeded:self)}
%end
/// Perform a bit pattern truncating conversion to UInt64
public var toU64: UInt64{
return UInt64(self) //No difference if the platform is 32 or 64
}
/// Perform a bit pattern truncating conversion to Int64
public var to64: Int64{
return Int64(self) //No difference if the platform is 32 or 64
}
%if sign == '':
/// Perform a bit pattern truncating conversion to Int
public var toInt:Int{return self}
/// Perform a bit pattern truncating conversion to UInt
public var toUInt:UInt{return UInt(bitPattern:self)}
%else:
/// Perform a bit pattern truncating conversion to Int
public var toInt:Int{return Int(bitPattern:self)}
/// Perform a bit pattern truncating conversion to UInt
public var toUInt:UInt{return self}
%end
/// Create bits mask with one
public func mask(_ bits: ${sign}Int, _ msb: Bool) -> ${sign}Int {
if (bits >> 3) == ${sign}Int.size { return ${sign}Int.max }
let mask = ${sign}Int(truncating: NSDecimalNumber(decimal:(pow(2, bits.toInt) - 1)))
switch msb {
case false:
return mask
case true:
let shift = ${sign}Int(${sign}Int.size << 3) - bits
return mask << shift
}
}
/// Returns the size of this type (number of bytes)
public static var size:Int{return MemoryLayout<${sign}Int>.stride}
% for bitidx in [0,1,2,3,4,5,6,7]:
%if sign == '':
/// Get bit ${bitidx} value
public var b${bitidx}:Int{
return Int(bitPattern: ( UInt(bitPattern: self) & (0x1 << ${bitidx})) >> ${bitidx} )
}
/// Set bit ${bitidx} and return a new ${sign}Int
public func setb${bitidx}(_ bit:Int)->Int{
let nv: UInt = bit != 0 ? 1 : 0
return Int(bitPattern: ( UInt(bitPattern: self) & ~(0x1 << ${bitidx})) | (nv << ${bitidx}) )
}
%else:
/// Get bit ${bitidx} value
public var b${bitidx}:UInt{
return ( self & (0x1 << ${bitidx})) >> ${bitidx}
}
/// Set bit ${bitidx} and return a new ${sign}Int
public func setb${bitidx}(_ bit:Int)->UInt{
let nv:UInt = bit != 0 ? 1 : 0
return ( self & ~(0x1 << ${bitidx})) | (nv << ${bitidx})
}
%end
%end
/// Subscript that returns or set one of the bytes of a ${sign}Int
/// at the given index.
/// Trying to access an out of index byte will result in an error.
public subscript(index: Int) -> ${sign}Int {
%if sign == '':
get {
precondition(index<Int.size,"Byte set index out of range")
let idx: UInt = UInt(bitPattern: index) * 8
return Int(bitPattern: (UInt(bitPattern: self) & (0xFF << idx)) >> idx )
}
set(newValue) {
precondition(index<Int.size,"Byte set index out of range")
let idx: UInt = UInt(bitPattern: index) * 8
self = Int(bitPattern: (UInt(bitPattern: self) & ~(0xFF << idx)) | (UInt(bitPattern: newValue) << idx) )
}
%else:
get {
precondition(index<Int.size,"Byte set index out of range")
let idx: UInt = UInt(bitPattern: index) * 8
return (self & (0xFF << idx)) >> idx
}
set(newValue) {
precondition(index<Int.size,"Byte set index out of range")
let idx: UInt = UInt(bitPattern: index) * 8
self = (self & ~(0xFF << idx)) | (newValue << idx)
}
%end
}
}
%end
% for intType in intTypes:
% for sign in ['','U']:
/**
Extension that adds a few additional functionalities to ${sign}Int${intType}:
- toIntN/toUIntN truncating bit pattern conversions
- allOnes
- size
- Byte indexed subscript
*/
extension ${sign}Int${intType} {
% for aSize in intTypes:
%if intType > aSize:
/// Perform a bit pattern truncating conversion to UInt${aSize}
public var toU${aSize}: UInt${aSize}{return UInt${aSize}(truncatingIfNeeded:self)}
/// Perform a bit pattern truncating conversion to Int${aSize}
public var to${aSize}: Int${aSize}{return Int${aSize}(truncatingIfNeeded:self)}
%elif intType == aSize:
%if sign == '':
/// Perform a bit pattern truncating conversion to UInt${aSize}
public var toU${aSize}: UInt${aSize}{return UInt${aSize}(bitPattern:self)}
/// Perform a bit pattern truncating conversion to Int${aSize}
public var to${aSize}: Int${aSize}{return self}
%else:
/// Perform a bit pattern truncating conversion to UInt${aSize}
public var toU${aSize}: UInt${aSize}{return self}
/// Perform a bit pattern truncating conversion to Int${aSize}
public var to${aSize}: Int${aSize}{return Int${aSize}(bitPattern:self)}
%end
%else:
/// Perform a bit pattern truncating conversion to UInt${aSize}
public var toU${aSize}: UInt${aSize}{return UInt${aSize}(self)}
/// Perform a bit pattern truncating conversion to Int${aSize}
public var to${aSize}: Int${aSize}{return Int${aSize}(self)}
%end
%end
%if intType < 64:
/// Perform a bit pattern truncating conversion to Int
public var toInt:Int{return Int(bitPattern:UInt(self))}
/// Perform a bit pattern truncating conversion to UInt
public var toUInt:UInt{return UInt(self)}
%else:
/// Perform a bit pattern truncating conversion to Int
public var toInt:Int{return Int(truncatingIfNeeded:self)}
/// Perform a bit pattern truncating conversion to UInt
public var toUInt:UInt{return UInt(truncatingIfNeeded:self)}
%end
/// Create bits mask with one
public func mask(_ bits: ${sign}Int${intType}, _ msb: Bool) -> ${sign}Int${intType} {
if (bits >> 3) == ${sign}Int${intType}.size { return ${sign}Int${intType}.max }
let mask = ${sign}Int${intType}(truncating: NSDecimalNumber(decimal:(pow(2, bits.toInt) - 1)))
switch msb {
case false:
return mask
case true:
let shift = ${sign}Int${intType}(${sign}Int${intType}.size << 3) - bits
return mask << shift
}
}
/// Returns the size of this type (number of bytes)
public static var size:Int{return MemoryLayout<${sign}Int${intType}>.stride}
% for bitidx in [0,1,2,3,4,5,6,7]:
%if sign == '':
/// Get bit ${bitidx} value
public var b${bitidx}:${sign}Int${intType}{
return ( (self.toU${intType} & (0x1 << ${bitidx})) >> ${bitidx} ).to${intType}
}
/// Set bit ${bitidx} and return a new ${sign}Int
public func setb${bitidx}(_ bit:Int)->${sign}Int${intType}{
let nv = bit != 0 ? 1 : 0
return ( (self.toU${intType} & ~(0x1 << ${bitidx})) | (nv.toU${intType} << ${bitidx}) ).to${intType}
}
%else:
/// Get bit ${bitidx} value
public var b${bitidx}:${sign}Int${intType}{
return (self & (0x1 << ${bitidx})) >> ${bitidx}
}
/// Set bit ${bitidx} and return a new ${sign}Int
public func setb${bitidx}(_ bit:Int)->${sign}Int${intType}{
let nv = bit != 0 ? 1 : 0
return (self & ~(0x1 << ${bitidx})) | (nv.toU${intType} << ${bitidx})
}
%end
%end
%if intType == 8:
/// Subscript that returns or set one of the bytes of this integer
/// at the given index.
/// Trying to access an out of index byte will result in an error.
public subscript(index: Int) -> ${sign}Int8 {
get {
precondition(index<Int8.size,"Byte set index out of range")
return self
}
set(newValue) {
precondition(index<Int8.size,"Byte set index out of range")
self = newValue
}
}
%else:
/// Subscript that returns or set one of the bytes of a ${sign}Int${intType}
/// at the given index.
/// Trying to access an out of index byte will result in an error.
public subscript(index: Int) -> ${sign}Int${intType} {
%if sign == '':
get {
precondition(index<Int${intType}.size,"Byte set index out of range")
return ((self.toU${intType} & (0xFF << (index.toU${intType}*8))) >> (index.toU${intType}*8)).to${intType}
}
set(newValue) {
precondition(index<Int${intType}.size,"Byte set index out of range")
self = ( (self.toU${intType} & ~(0xFF << (index.toU${intType}*8))) | (newValue.toU${intType} << (index.toU${intType}*8)) ).to${intType}
}
%else:
get {
precondition(index<Int${intType}.size,"Byte set index out of range")
return (self & (0xFF << (index.toU${intType}*8))) >> (index.toU${intType}*8)
}
set(newValue) {
precondition(index<Int${intType}.size,"Byte set index out of range")
self = (self & ~(0xFF << (index.toU${intType}*8))) | (newValue.toU${intType} << (index.toU${intType}*8))
}
%end
}
%end
}
%end
%end
// MARK: Operators
/// Double negation operator
prefix operator ~~
% for intType in intTypes:
% for sign in ['','U']:
/// Double negation operator for ${sign}Int${intType}
/// Returns 1 if value is not equal to 0, 0 otherwise
prefix func ~~(value: ${sign}Int${intType})->${sign}Int${intType}{
return (value>0) ? 1 : 0
}
%end
%end