Skip to content

Commit b428fdb

Browse files
committed
Add some basic tests for confidence that we match existing generic implementations.
1 parent d71dd8c commit b428fdb

File tree

3 files changed

+251
-0
lines changed

3 files changed

+251
-0
lines changed

test/stdlib/SIMDConcreteFP.swift.gyb

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
//===--- SIMDConcreteFP.swift.gyb -----------------------------*- swift -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2021 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
// RUN: %empty-directory(%t)
13+
// RUN: %gyb -DCMAKE_SIZEOF_VOID_P=%target-ptrsize %s -o %t/SIMDConcreteFP.swift
14+
// RUN: %line-directive %t/SIMDConcreteFP.swift -- %target-build-swift %t/SIMDConcreteFP.swift -o %t/a.out
15+
// RUN: %target-codesign %t/a.out
16+
// RUN: %line-directive %t/SIMDConcreteFP.swift -- %target-run %t/a.out
17+
// REQUIRES: executable_test
18+
19+
import StdlibUnittest
20+
21+
func genericComparisons<T>(
22+
_ a: T, _ b: T
23+
) -> (
24+
eq: SIMDMask<T.MaskStorage>,
25+
ne: SIMDMask<T.MaskStorage>,
26+
lt: SIMDMask<T.MaskStorage>,
27+
le: SIMDMask<T.MaskStorage>,
28+
gt: SIMDMask<T.MaskStorage>,
29+
ge: SIMDMask<T.MaskStorage>
30+
) where T: SIMD, T.Scalar: Comparable {(
31+
eq: a .== b,
32+
ne: a .!= b,
33+
lt: a .< b,
34+
le: a .<= b,
35+
gt: a .> b,
36+
ge: a .>= b
37+
)}
38+
39+
%for (Scalar, bits) in [('Float16',16), ('Float',32), ('Double',64)]:
40+
% for n in [2,3,4,8,16,32,64]:
41+
% Vector = "SIMD" + str(n) + "<" + Scalar + ">"
42+
% storageN = 4 if n == 3 else n
43+
% Builtin = "Vec" + str(storageN) + "xFPIEEE" + str(bits)
44+
var ${Scalar}x${n}_TestSuite = TestSuite("${Scalar}x${n}")
45+
46+
% if bits == 16:
47+
#if !((os(macOS) || targetEnvironment(macCatalyst)) && arch(x86_64))
48+
% end
49+
${Scalar}x${n}_TestSuite.test("comparisons") {
50+
% if bits == 16:
51+
if #available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *) {
52+
% end
53+
let a = ${Vector}.random(in: -1 ..< 1)
54+
let ap1 = a + ${Vector}(repeating: 1)
55+
expectTrue(all(a .== a))
56+
expectFalse(any(a .!= a))
57+
expectFalse(any(a .== ap1))
58+
expectTrue(all(a .!= ap1))
59+
expectTrue(all(a .< ap1))
60+
expectTrue(all(a .<= ap1))
61+
expectFalse(any(a .> ap1))
62+
expectFalse(any(a .>= ap1))
63+
64+
let b = a.replacing(
65+
with: ${Vector}.random(in: -1 ..< 1),
66+
where: .random()
67+
)
68+
let (eq,ne,lt,le,gt,ge) = genericComparisons(a, b)
69+
expectEqual(eq, a .== b)
70+
expectEqual(ne, a .!= b)
71+
expectEqual(lt, a .< b)
72+
expectEqual(le, a .<= b)
73+
expectEqual(gt, a .> b)
74+
expectEqual(ge, a .>= b)
75+
% if bits == 16:
76+
}
77+
% end
78+
}
79+
% if bits == 16:
80+
#endif
81+
% end
82+
83+
% end
84+
%end
85+
86+
runAllTests()
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
//===--- SIMDConcreteIntegers.swift.gyb -----------------------*- swift -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2021 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
// RUN: %empty-directory(%t)
13+
// RUN: %gyb -DCMAKE_SIZEOF_VOID_P=%target-ptrsize %s -o %t/SIMDConcreteIntegers.swift
14+
// RUN: %line-directive %t/SIMDConcreteIntegers.swift -- %target-build-swift %t/SIMDConcreteIntegers.swift -o %t/a.out
15+
// RUN: %target-codesign %t/a.out
16+
// RUN: %line-directive %t/SIMDConcreteIntegers.swift -- %target-run %t/a.out
17+
// REQUIRES: executable_test
18+
19+
import StdlibUnittest
20+
21+
%{
22+
from __future__ import division
23+
from SwiftIntTypes import all_integer_types
24+
word_bits = int(CMAKE_SIZEOF_VOID_P) * 8
25+
storagescalarCounts = [2,4,8,16,32,64]
26+
vectorscalarCounts = storagescalarCounts + [3]
27+
}%
28+
29+
func genericComparisons<T>(
30+
_ a: T, _ b: T
31+
) -> (
32+
eq: SIMDMask<T.MaskStorage>,
33+
ne: SIMDMask<T.MaskStorage>,
34+
lt: SIMDMask<T.MaskStorage>,
35+
le: SIMDMask<T.MaskStorage>,
36+
gt: SIMDMask<T.MaskStorage>,
37+
ge: SIMDMask<T.MaskStorage>
38+
) where T: SIMD, T.Scalar: Comparable {(
39+
eq: a .== b,
40+
ne: a .!= b,
41+
lt: a .< b,
42+
le: a .<= b,
43+
gt: a .> b,
44+
ge: a .>= b
45+
)}
46+
47+
func genericArithmetic<T>(
48+
_ a: T, _ b: T
49+
) -> (
50+
add: T, sub: T, mul: T
51+
) where T: SIMD, T.Scalar: FixedWidthInteger {(
52+
add: a &+ b,
53+
sub: a &- b,
54+
mul: a &* b
55+
)}
56+
57+
%for int in all_integer_types(word_bits):
58+
% Scalar = int.stdlib_name
59+
% for n in vectorscalarCounts:
60+
% Vector = "SIMD" + str(n) + "<" + Scalar + ">"
61+
% storageN = 4 if n == 3 else n
62+
% s = "s" if int.is_signed else "u"
63+
% Builtin = "Vec" + str(storageN) + "xInt" + str(int.bits)
64+
var ${Scalar}x${n}_TestSuite = TestSuite("${Scalar}x${n}")
65+
66+
${Scalar}x${n}_TestSuite.test("comparisons") {
67+
let a = ${Vector}.random(in: ${Scalar}.min ... .max)
68+
expectTrue(all(a .== a))
69+
expectFalse(any(a .!= a))
70+
expectFalse(any(a .== a &+ ${Vector}(repeating: 1)))
71+
expectTrue(all(a .!= a &+ ${Vector}(repeating: 1)))
72+
73+
let b = a.replacing(
74+
with: ${Vector}.random(in: ${Scalar}.min ... .max),
75+
where: .random()
76+
)
77+
let (eq,ne,lt,le,gt,ge) = genericComparisons(a, b)
78+
expectEqual(eq, a .== b)
79+
expectEqual(ne, a .!= b)
80+
expectEqual(lt, a .< b)
81+
expectEqual(le, a .<= b)
82+
expectEqual(gt, a .> b)
83+
expectEqual(ge, a .>= b)
84+
}
85+
86+
${Scalar}x${n}_TestSuite.test("arithmetic") {
87+
let a = ${Vector}.random(in: ${Scalar}.min ... .max)
88+
let b = ${Vector}.random(in: ${Scalar}.min ... .max)
89+
let (add,sub,mul) = genericArithmetic(a, b)
90+
expectEqual(add, a &+ b)
91+
expectEqual(sub, a &- b)
92+
expectEqual(mul, a &* b)
93+
}
94+
95+
% end
96+
%end
97+
98+
runAllTests()
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//===--- SIMDConcreteMasks.swift.gyb --------------------------*- swift -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2021 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
// RUN: %empty-directory(%t)
13+
// RUN: %gyb -DCMAKE_SIZEOF_VOID_P=%target-ptrsize %s -o %t/SIMDConcreteMasks.swift
14+
// RUN: %line-directive %t/SIMDConcreteMasks.swift -- %target-build-swift %t/SIMDConcreteMasks.swift -o %t/a.out
15+
// RUN: %target-codesign %t/a.out
16+
// RUN: %line-directive %t/SIMDConcreteMasks.swift -- %target-run %t/a.out
17+
// REQUIRES: executable_test
18+
19+
import StdlibUnittest
20+
21+
%{
22+
from __future__ import division
23+
from SwiftIntTypes import all_integer_types
24+
word_bits = int(CMAKE_SIZEOF_VOID_P) * 8
25+
storagescalarCounts = [2,4,8,16,32,64]
26+
vectorscalarCounts = storagescalarCounts + [3]
27+
}%
28+
29+
func genericMaskOps<T>(
30+
_ a: SIMDMask<T>, _ b: SIMDMask<T>
31+
) -> (
32+
not: SIMDMask<T>,
33+
and: SIMDMask<T>,
34+
or: SIMDMask<T>,
35+
xor: SIMDMask<T>
36+
) where T: SIMD, T.Scalar: SignedInteger & FixedWidthInteger {(
37+
not: .!a,
38+
and: a .& b,
39+
or: a .| b,
40+
xor: a .^ b
41+
)}
42+
43+
%for int in all_integer_types(word_bits):
44+
% Scalar = int.stdlib_name
45+
% for n in vectorscalarCounts:
46+
% Vector = "SIMD" + str(n) + "<" + Scalar + ">"
47+
% storageN = 4 if n == 3 else n
48+
% s = "s" if int.is_signed else "u"
49+
% Builtin = "Vec" + str(storageN) + "xInt" + str(int.bits)
50+
% if int.is_signed:
51+
var ${Scalar}x${n}Mask_TestSuite = TestSuite("${Scalar}x${n}Mask")
52+
53+
${Scalar}x${n}Mask_TestSuite.test("boolean operators") {
54+
let a = SIMDMask<${Vector}>.random()
55+
let b = SIMDMask<${Vector}>.random()
56+
let (not,and,or,xor) = genericMaskOps(a, b)
57+
expectEqual(not, .!a)
58+
expectEqual(and, a .& b)
59+
expectEqual(or, a .| b)
60+
expectEqual(xor, a .^ b)
61+
}
62+
63+
% end
64+
% end
65+
%end
66+
67+
runAllTests()

0 commit comments

Comments
 (0)