1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 #include "Reactor.hpp"
17 #include "gtest/gtest.h"
21 int reference(int *p, int y)
26 for(int i = 0; i < 10; i++)
28 z += (2 << i) - (i / 3);
36 TEST(SubzeroReactorTest, Sample)
38 Routine *routine = nullptr;
41 Function<Int(Pointer<Int>, Int)> function;
43 Pointer<Int> p = function.Arg<0>();
45 Int y = function.Arg<1>();
48 For(Int i = 0, i < 10, i++)
50 z += (2 << i) - (i / 3);
55 z = As<Int>(Float(Float4(v.xzxx).y));
62 routine = function(L"one");
66 int (*callable)(int*, int) = (int(*)(int*,int))routine->getEntry();
68 int result = callable(&one[1], 2);
69 EXPECT_EQ(result, reference(&one[1], 2));
76 TEST(SubzeroReactorTest, SubVectorLoadStore)
78 Routine *routine = nullptr;
81 Function<Int(Pointer<Byte>, Pointer<Byte>)> function;
83 Pointer<Byte> in = function.Arg<0>();
84 Pointer<Byte> out = function.Arg<1>();
86 *Pointer<Int4>(out + 16 * 0) = *Pointer<Int4>(in + 16 * 0);
87 *Pointer<Short4>(out + 16 * 1) = *Pointer<Short4>(in + 16 * 1);
88 *Pointer<Byte8>(out + 16 * 2) = *Pointer<Byte8>(in + 16 * 2);
89 *Pointer<Byte4>(out + 16 * 3) = *Pointer<Byte4>(in + 16 * 3);
90 *Pointer<Short2>(out + 16 * 4) = *Pointer<Short2>(in + 16 * 4);
95 routine = function(L"one");
99 int8_t in[16 * 5] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
100 17, 18, 19, 20, 21, 22, 23, 24, 0, 0, 0, 0, 0, 0, 0, 0,
101 25, 26, 27, 28, 29, 30, 31, 32, 0, 0, 0, 0, 0, 0, 0, 0,
102 33, 34, 35, 36, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
103 37, 38, 39, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
105 int8_t out[16 * 5] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
106 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
107 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
108 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
109 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
111 int (*callable)(void*, void*) = (int(*)(void*,void*))routine->getEntry();
114 for(int row = 0; row < 5; row++)
116 for(int col = 0; col < 16; col++)
118 int i = row * 16 + col;
122 EXPECT_EQ(out[i], -1) << "Row " << row << " column " << col << " not left untouched.";
126 EXPECT_EQ(out[i], in[i]) << "Row " << row << " column " << col << " not equal to input.";
136 TEST(SubzeroReactorTest, VectorConstant)
138 Routine *routine = nullptr;
141 Function<Int(Pointer<Byte>)> function;
143 Pointer<Byte> out = function.Arg<0>();
145 *Pointer<Int4>(out + 16 * 0) = Int4(0x04030201, 0x08070605, 0x0C0B0A09, 0x100F0E0D);
146 *Pointer<Short4>(out + 16 * 1) = Short4(0x1211, 0x1413, 0x1615, 0x1817);
147 *Pointer<Byte8>(out + 16 * 2) = Byte8(0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20);
148 *Pointer<Int2>(out + 16 * 3) = Int2(0x24232221, 0x28272625);
153 routine = function(L"one");
157 int8_t out[16 * 4] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
158 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
159 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
160 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
162 int8_t exp[16 * 4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
163 17, 18, 19, 20, 21, 22, 23, 24, -1, -1, -1, -1, -1, -1, -1, -1,
164 25, 26, 27, 28, 29, 30, 31, 32, -1, -1, -1, -1, -1, -1, -1, -1,
165 33, 34, 35, 36, 37, 38, 39, 40, -1, -1, -1, -1, -1, -1, -1, -1};
167 int(*callable)(void*) = (int(*)(void*))routine->getEntry();
170 for(int row = 0; row < 4; row++)
172 for(int col = 0; col < 16; col++)
174 int i = row * 16 + col;
176 EXPECT_EQ(out[i], exp[i]);
185 TEST(SubzeroReactorTest, Swizzle)
187 Routine *routine = nullptr;
190 Function<Int(Pointer<Byte>)> function;
192 Pointer<Byte> out = function.Arg<0>();
194 for(int i = 0; i < 256; i++)
196 *Pointer<Float4>(out + 16 * i) = Swizzle(Float4(1.0f, 2.0f, 3.0f, 4.0f), i);
199 for(int i = 0; i < 256; i++)
201 *Pointer<Float4>(out + 16 * (256 + i)) = ShuffleLowHigh(Float4(1.0f, 2.0f, 3.0f, 4.0f), Float4(5.0f, 6.0f, 7.0f, 8.0f), i);
204 *Pointer<Float4>(out + 16 * (512 + 0)) = UnpackLow(Float4(1.0f, 2.0f, 3.0f, 4.0f), Float4(5.0f, 6.0f, 7.0f, 8.0f));
205 *Pointer<Float4>(out + 16 * (512 + 1)) = UnpackHigh(Float4(1.0f, 2.0f, 3.0f, 4.0f), Float4(5.0f, 6.0f, 7.0f, 8.0f));
210 routine = function(L"one");
214 float out[256 + 256 + 2][4];
215 memset(out, 0, sizeof(out));
217 float exp[256 + 256 + 2][4];
219 for(int i = 0; i < 256; i++)
221 exp[i][0] = float((i >> 0) & 0x03) + 1.0f;
222 exp[i][1] = float((i >> 2) & 0x03) + 1.0f;
223 exp[i][2] = float((i >> 4) & 0x03) + 1.0f;
224 exp[i][3] = float((i >> 6) & 0x03) + 1.0f;
227 for(int i = 0; i < 256; i++)
229 exp[256 + i][0] = float((i >> 0) & 0x03) + 1.0f;
230 exp[256 + i][1] = float((i >> 2) & 0x03) + 1.0f;
231 exp[256 + i][2] = float((i >> 4) & 0x03) + 5.0f;
232 exp[256 + i][3] = float((i >> 6) & 0x03) + 5.0f;
235 exp[512 + 0][0] = 1.0f;
236 exp[512 + 0][1] = 5.0f;
237 exp[512 + 0][2] = 2.0f;
238 exp[512 + 0][3] = 6.0f;
240 exp[512 + 1][0] = 3.0f;
241 exp[512 + 1][1] = 7.0f;
242 exp[512 + 1][2] = 4.0f;
243 exp[512 + 1][3] = 8.0f;
245 int(*callable)(void*) = (int(*)(void*))routine->getEntry();
248 for(int i = 0; i < 256 + 256 + 2; i++)
250 EXPECT_EQ(out[i][0], exp[i][0]);
251 EXPECT_EQ(out[i][1], exp[i][1]);
252 EXPECT_EQ(out[i][2], exp[i][2]);
253 EXPECT_EQ(out[i][3], exp[i][3]);
261 TEST(SubzeroReactorTest, Branching)
263 Routine *routine = nullptr;
266 Function<Int(Void)> function;
270 For(Int i = 0, i < 8, i++)
289 For(Int i = 0, i < 5, i++)
293 For(Int i = 0, i < 10, i++)
294 for(int i = 0; i < 10; i++)
295 For(Int i = 0, i < 10, i++)
300 For(Int i = 0, i < 2, i++)
312 routine = function(L"one");
316 int(*callable)() = (int(*)())routine->getEntry();
317 int result = callable();
319 EXPECT_EQ(result, 1000402222);
326 int main(int argc, char **argv)
328 ::testing::InitGoogleTest(&argc, argv);
329 return RUN_ALL_TESTS();