-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathFixedPoint.swift.gyb
434 lines (336 loc) · 10.5 KB
/
FixedPoint.swift.gyb
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
// RUN: %target-run-simple-swiftgyb
// REQUIRES: executable_test
import StdlibUnittest
var FixedPoint = TestSuite("FixedPoint")
%{
import gyb
test_bit_patterns = [
0x0000000000000000,
0x0000000000000001,
0xffffffffffffffff,
0x00ffffffffffffff,
0x0000ffffffffffff,
0x000000ffffffffff,
0x00000000ffffffff,
0x0000000000ffffff,
0x000000000000ffff,
0x00000000000000ff,
0xfe123456789abcde,
0x00fe123456789abc,
0x0000fe123456789a,
0x000000fe12345678,
0x00000000fe123456,
0x0000000000fe1234,
0x000000000000fe12,
0x00000000000000fe,
0x7f123456789abcde,
0x007f123456789abc,
0x00007f123456789a,
0x0000007f12345678,
0x000000007f123456,
0x00000000007f1234,
0x0000000000007f12,
0x000000000000007f,
]
def prepare_bit_pattern(bit_pattern, dst_bits, dst_signed):
mask = ((1 << dst_bits) - 1)
dst = bit_pattern & mask
if not dst_signed:
return dst
if dst <= ((1 << (dst_bits - 1)) - 1):
return dst
return dst - mask - 1
def get_fixed_point_hash(bit_pattern, src_bits, word_bits):
if src_bits <= word_bits:
bit_pattern = prepare_bit_pattern(bit_pattern, src_bits, True)
return prepare_bit_pattern(bit_pattern, word_bits, True)
if src_bits == word_bits * 2:
return prepare_bit_pattern(
bit_pattern ^ (bit_pattern >> 32), word_bits, True)
}%
//===----------------------------------------------------------------------===//
// 'Int(truncatingBitPattern:)' initializer
//===----------------------------------------------------------------------===//
%{
truncating_bit_pattern_test_template = gyb.parse_template("truncating_bit_pattern",
"""
% from SwiftIntTypes import all_integer_types, should_define_truncating_bit_pattern_init
% for dst_ty in all_integer_types(word_bits):
% Dst = dst_ty.stdlib_name
% for src_ty in all_integer_types(word_bits):
% Src = src_ty.stdlib_name
% if should_define_truncating_bit_pattern_init(src_ty=src_ty, dst_ty=dst_ty):
%
% for bit_pattern in test_bit_patterns:
FixedPoint.test("${Dst}(truncatingBitPattern:) from ${Src}(${bit_pattern})") {
% input = prepare_bit_pattern(bit_pattern, src_ty.bits, src_ty.is_signed)
let input = get${Src}(${input})
% input = prepare_bit_pattern(input, src_ty.bits, False)
let output = get${Dst}(${Dst}(truncatingBitPattern: input))
let expected = get${Dst}(${prepare_bit_pattern(input, dst_ty.bits, dst_ty.is_signed)})
expectEqual(expected, output)
}
% end
// This comment prevents gyb from miscompiling this file.
// <rdar://problem/17548877> gyb miscompiles a certain for loop
% end
// This comment prevents gyb from miscompiling this file.
// <rdar://problem/17548877> gyb miscompiles a certain for loop
% end
// This comment prevents gyb from miscompiling this file.
// <rdar://problem/17548877> gyb miscompiles a certain for loop
% end
""")
}%
#if arch(i386) || arch(arm)
${gyb.execute_template(
truncating_bit_pattern_test_template,
prepare_bit_pattern=prepare_bit_pattern,
test_bit_patterns=test_bit_patterns,
word_bits=32)}
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
${gyb.execute_template(
truncating_bit_pattern_test_template,
prepare_bit_pattern=prepare_bit_pattern,
test_bit_patterns=test_bit_patterns,
word_bits=64)}
#else
_UnimplementedError()
#endif
//===----------------------------------------------------------------------===//
// 'Int(bitPattern:)' initializer
//===----------------------------------------------------------------------===//
%{
bit_pattern_test_template = gyb.parse_template("bit_pattern",
"""
% from SwiftIntTypes import all_integer_types
% for dst_ty in all_integer_types(word_bits):
% Dst = dst_ty.stdlib_name
% # Source is the same as destination, but of different signedness.
% src_ty = dst_ty.get_opposite_signedness()
% Src = src_ty.stdlib_name
% for bit_pattern in test_bit_patterns:
FixedPoint.test("${Dst}(bitPattern:) from ${Src}(${bit_pattern})") {
do {
% input = prepare_bit_pattern(bit_pattern, src_ty.bits, src_ty.is_signed)
let input = get${Src}(${input})
% input = prepare_bit_pattern(input, src_ty.bits, False)
let output = get${Dst}(${Dst}(bitPattern: input))
let expected = get${Dst}(${prepare_bit_pattern(input, dst_ty.bits, dst_ty.is_signed)})
expectEqual(expected, output)
}
do {
% input = prepare_bit_pattern(bit_pattern, src_ty.bits, src_ty.is_signed)
let input = get${Src}(${input})
// Pass a literal directly.
let literalOutput = get${Dst}(${Dst}(bitPattern: ${input}))
let output = get${Dst}(${Dst}(bitPattern: input))
% input = prepare_bit_pattern(input, src_ty.bits, False)
let expected = get${Dst}(${prepare_bit_pattern(input, dst_ty.bits, dst_ty.is_signed)})
expectEqual(expected, literalOutput)
expectEqual(expected, output)
}
}
% end
% end
""")
}%
#if arch(i386) || arch(arm)
${gyb.execute_template(
bit_pattern_test_template,
prepare_bit_pattern=prepare_bit_pattern,
test_bit_patterns=test_bit_patterns,
word_bits=32)}
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
${gyb.execute_template(
bit_pattern_test_template,
prepare_bit_pattern=prepare_bit_pattern,
test_bit_patterns=test_bit_patterns,
word_bits=64)}
#else
_UnimplementedError()
#endif
//===----------------------------------------------------------------------===//
// 'Int.hashValue' property
//===----------------------------------------------------------------------===//
%{
hash_value_test_template = gyb.parse_template("hash_value",
"""
% from SwiftIntTypes import all_integer_types
% for self_ty in all_integer_types(word_bits):
% Self = self_ty.stdlib_name
FixedPoint.test("${Self}.hashValue") {
% for bit_pattern in test_bit_patterns:
do {
% input = prepare_bit_pattern(bit_pattern, self_ty.bits, self_ty.is_signed)
let input = get${Self}(${input})
let output = getInt(input.hashValue)
let expected = getInt(${get_fixed_point_hash(input, self_ty.bits, word_bits)})
expectEqual(expected, output)
}
% end
}
% end
""")
}%
#if arch(i386) || arch(arm)
${gyb.execute_template(
hash_value_test_template,
prepare_bit_pattern=prepare_bit_pattern,
get_fixed_point_hash=get_fixed_point_hash,
test_bit_patterns=test_bit_patterns,
word_bits=32)}
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
${gyb.execute_template(
hash_value_test_template,
prepare_bit_pattern=prepare_bit_pattern,
get_fixed_point_hash=get_fixed_point_hash,
test_bit_patterns=test_bit_patterns,
word_bits=64)}
#else
_UnimplementedError()
#endif
//===----------------------------------------------------------------------===//
// Misc tests that should be expanded to cover all types (FIXME)
//===----------------------------------------------------------------------===//
func testBitwiseOperationsImpl<T : UnsignedInteger>(_: T.Type) {
var x = numericCast(0b11_1010_00) as T
var y = numericCast(0b10_1100_10) as T
expectEqual(0b10_1000_00, x & y)
expectEqual(0b11_1110_10, x | y)
expectEqual(0b01_0110_10, x ^ y)
expectEqual(0b00_0101_11, (~x) & 0xff)
var z = T.allZeros
expectEqual(x, x | z)
expectEqual(x, x ^ z)
expectEqual(z, x & z)
expectEqual(x, x & ~z)
}
FixedPoint.test("BitwiseOperations/UInt8") {
testBitwiseOperationsImpl(UInt8.self)
}
FixedPoint.test("BitwiseOperations/UInt16") {
testBitwiseOperationsImpl(UInt16.self)
}
FixedPoint.test("BitwiseOperations/UInt32") {
testBitwiseOperationsImpl(UInt32.self)
}
FixedPoint.test("BitwiseOperations/UInt64") {
testBitwiseOperationsImpl(UInt64.self)
}
FixedPoint.test("OverflowCheck") {
expectEqual(Int8.addWithOverflow(4, 5), (9, false))
expectEqual(Int8.addWithOverflow(1, 127), (-128, true))
expectEqual(UInt8.multiplyWithOverflow(2,128), (0, true))
}
FixedPoint.test("String.init") {
let x: UInt32 = 0xdeadbeef
#if _endian(little)
expectEqual("efbeadde", String(x.bigEndian, radix: 16))
let y = UInt32(bigEndian: 0xdeadbeef)
expectEqual("deadbeef", String(y.bigEndian, radix: 16))
#else
expectEqual("efbeadde", String(x.littleEndian, radix: 16))
let y = UInt32(littleEndian: 0xdeadbeef)
expectEqual("deadbeef", String(y.littleEndian, radix: 16))
#endif
}
FixedPoint.test("byteSwapped") {
expectEqual(288230376151711744, Int64(4).byteSwapped)
}
//===----------------------------------------------------------------------===//
// 'Bool' type
//===----------------------------------------------------------------------===//
var BoolTestSuite = TestSuite("Bool")
BoolTestSuite.test("literals") {
do {
var v = false
expectType(Bool.self, &v)
}
do {
var v = true
expectType(Bool.self, &v)
}
}
BoolTestSuite.test("init()") {
let v = Bool()
expectFalse(v)
}
struct Booleanish : Boolean {
let boolValue: Bool
}
BoolTestSuite.test("init<T : Boolean>(_:)") {
do {
let v = Bool(Booleanish(boolValue: false))
expectFalse(v)
}
do {
let v = Bool(Booleanish(boolValue: true))
expectTrue(v)
}
}
BoolTestSuite.test("Boolean") {
do {
var v: Bool = false
expectIsBooleanType(&v)
expectFalse(v.boolValue)
}
do {
var v: Bool = true
expectTrue(v.boolValue)
}
}
BoolTestSuite.test("CustomStringConvertible") {
do {
let v: Bool = false
expectEqual("false", String(v))
}
do {
let v: Bool = true
expectEqual("true", String(v))
}
}
BoolTestSuite.test("Equatable,Hashable") {
checkHashable(true, false, false)
checkHashable(false, false, true)
checkHashable(true, true, true)
expectNotEqual(false.hashValue, true.hashValue)
}
BoolTestSuite.test("!") {
do {
let v: Bool = false
var r = !v
expectType(Bool.self, &r)
expectTrue(r)
}
do {
let v: Bool = true
var r = !v
expectType(Bool.self, &r)
expectFalse(r)
}
}
BoolTestSuite.test("==,!=") {
let v: Bool = false
do {
var r = (v == v)
expectType(Bool.self, &r)
}
do {
var r = (v != v)
expectType(Bool.self, &r)
}
}
BoolTestSuite.test("&&") {
expectTrue(getBool(true) && getBool(true))
expectFalse(getBool(true) && getBool(false))
expectFalse(getBool(false) && getBool(true))
expectFalse(getBool(false) && getBool(false))
}
BoolTestSuite.test("||") {
expectTrue(getBool(true) || getBool(true))
expectTrue(getBool(true) || getBool(false))
expectTrue(getBool(false) || getBool(true))
expectFalse(getBool(false) || getBool(false))
}
runAllTests()