Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(278)

Side by Side Diff: tests/simd/vector_extension.c

Issue 222483002: PNaCl: Test support for GCC/LLVM vector extensions (Closed) Base URL: svn://svn.chromium.org/native_client/trunk/src/native_client
Patch Set: Add TODOs. Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tests/simd/nacl.scons ('k') | tests/simd/vector_extension.stdout » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2014 The Native Client Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6
7 /*
8 * Test that the GCC/LLVM vector extensions can be used from C code.
9 * http://gcc.gnu.org/onlinedocs/gcc/Vector-Extensions.html
10 * http://clang.llvm.org/docs/LanguageExtensions.html
11 *
12 * This test is thorough feature-wise, but not thorough in testing the
13 * corner-case values. It tries to exercise all vector types and
14 * operations that are supported, and verifies that test values generate
15 * the right result by comparing to a golden output file. It does not
16 * test all the MIN/MAX values, nor does it test undefined behavior.
17 *
18 * TODO(jfb) Add testing for vector conversion.
19 * TODO(jfb) Add testing for vector shuffle once PNaCl supports it.
20 */
21
22 #include "native_client/src/include/nacl_macros.h"
23
24 #include <stdint.h>
25 #include <stdio.h>
26
27 /*
28 * Basic types that are supported inside vectors.
29 *
30 * TODO(jfb) Handle 64-bit int and double.
31 */
32 typedef int8_t I8;
33 typedef uint8_t U8;
34 typedef int16_t I16;
35 typedef uint16_t U16;
36 typedef int32_t I32;
37 typedef uint32_t U32;
38 typedef float F32;
39
40 /*
41 *
42 * The GCC/LLVM vector extensions represent the results of comparisons
43 * as a vector of all-ones or all-zeros with the same vector bit width
44 * and number of elements. They must be treated differently than their
45 * corresponding type because floating-point values change their bit
46 * representation through assignments when they hold NaN values.
47 */
48 typedef int8_t I8_BOOL;
49 typedef int8_t U8_BOOL;
50 typedef int16_t I16_BOOL;
51 typedef int16_t U16_BOOL;
52 typedef int32_t I32_BOOL;
53 typedef int32_t U32_BOOL;
54 typedef int32_t F32_BOOL;
55
56 #define I8_FMT "i"
57 #define U8_FMT "u"
58 #define I16_FMT "i"
59 #define U16_FMT "u"
60 #define I32_FMT "i"
61 #define U32_FMT "u"
62 #define F32_FMT "f"
63
64 /* All elements in a boolean vector should print as 0 or -1. */
65 #define I8_BOOL_FMT "i"
66 #define U8_BOOL_FMT "i"
67 #define I16_BOOL_FMT "i"
68 #define U16_BOOL_FMT "i"
69 #define I32_BOOL_FMT "i"
70 #define U32_BOOL_FMT "i"
71 #define F32_BOOL_FMT "i"
72
73 /* All supported vector types are currently 128-bit wide. */
74 #define VEC_BYTES 16
75
76 /* Vector types corresponding to each supported basic types. */
77 typedef I8 VI8 __attribute__((vector_size(VEC_BYTES)));
78 typedef U8 VU8 __attribute__((vector_size(VEC_BYTES)));
79 typedef I16 VI16 __attribute__((vector_size(VEC_BYTES)));
80 typedef U16 VU16 __attribute__((vector_size(VEC_BYTES)));
81 typedef I32 VI32 __attribute__((vector_size(VEC_BYTES)));
82 typedef U32 VU32 __attribute__((vector_size(VEC_BYTES)));
83 typedef F32 VF32 __attribute__((vector_size(VEC_BYTES)));
84
85 /* Boolean vector types generate by comparisons on each vector type. */
86 typedef I8 VI8_BOOL __attribute__((vector_size(VEC_BYTES)));
87 typedef I8 VU8_BOOL __attribute__((vector_size(VEC_BYTES)));
88 typedef I16 VI16_BOOL __attribute__((vector_size(VEC_BYTES)));
89 typedef I16 VU16_BOOL __attribute__((vector_size(VEC_BYTES)));
90 typedef I32 VI32_BOOL __attribute__((vector_size(VEC_BYTES)));
91 typedef I32 VU32_BOOL __attribute__((vector_size(VEC_BYTES)));
92 typedef I32 VF32_BOOL __attribute__((vector_size(VEC_BYTES)));
93
94 #define PRINT(TYPE, VEC) \
95 do { \
96 NACL_COMPILE_TIME_ASSERT(sizeof(V##TYPE) == \
97 VEC_BYTES); /* Vector must be 128 bits. */ \
98 NACL_COMPILE_TIME_ASSERT(sizeof(TYPE) == \
99 sizeof(VEC[0])); /* Type must match. */ \
100 printf("{"); \
101 for (size_t i = 0; i != sizeof(V##TYPE) / sizeof(VEC[0]); ++i) \
102 printf("%" TYPE##_FMT ",", VEC[i]); \
103 printf("}"); \
104 } while (0)
105
106 #define TEST_BINARY(TYPE, LHS, OP, RHS) \
107 do { \
108 NACL_COMPILE_TIME_ASSERT(sizeof(TYPE) == \
109 sizeof(LHS[0])); /* Types must match. */ \
110 NACL_COMPILE_TIME_ASSERT(sizeof(TYPE) == \
111 sizeof(RHS[0])); /* Types must match. */ \
112 const V##TYPE result = LHS OP RHS; \
113 printf(#TYPE " "); \
114 PRINT(TYPE, LHS); \
115 printf(" %s ", #OP); \
116 PRINT(TYPE, RHS); \
117 printf(" = "); \
118 PRINT(TYPE, result); \
119 printf("\n"); \
120 } while (0)
121
122 #define TEST_BINARY_COMPARISON(TYPE, LHS, OP, RHS) \
123 do { \
124 NACL_COMPILE_TIME_ASSERT(sizeof(TYPE) == \
125 sizeof(LHS[0])); /* Types must match. */ \
126 NACL_COMPILE_TIME_ASSERT(sizeof(TYPE) == \
127 sizeof(RHS[0])); /* Types must match. */ \
128 const V##TYPE##_BOOL result = LHS OP RHS; \
129 printf(#TYPE " "); \
130 PRINT(TYPE, LHS); \
131 printf(" %s ", #OP); \
132 PRINT(TYPE, RHS); \
133 printf(" = "); \
134 PRINT(TYPE##_BOOL, result); \
135 printf("\n"); \
136 } while (0)
137
138 #define TEST_UNARY(TYPE, OP, VAL) \
139 do { \
140 NACL_COMPILE_TIME_ASSERT(sizeof(TYPE) == \
141 sizeof(VAL[0])); /* Types must match. */ \
142 const V##TYPE result = OP VAL; \
143 printf(#TYPE " %s ", #OP); \
144 PRINT(TYPE, VAL); \
145 printf(" = "); \
146 PRINT(TYPE, result); \
147 printf("\n"); \
148 } while (0)
149
150 #define TEST_BINARY_FP(TYPE, LHS, RHS) \
151 do { \
152 TEST_BINARY(TYPE, LHS, +, RHS); \
153 TEST_BINARY(TYPE, LHS, -, RHS); \
154 TEST_BINARY(TYPE, LHS, *, RHS); \
155 TEST_BINARY(TYPE, LHS, /, RHS); \
156 TEST_BINARY_COMPARISON(TYPE, LHS, ==, RHS); \
157 TEST_BINARY_COMPARISON(TYPE, LHS, !=, RHS); \
158 TEST_BINARY_COMPARISON(TYPE, LHS, <, RHS); \
159 TEST_BINARY_COMPARISON(TYPE, LHS, >, RHS); \
160 TEST_BINARY_COMPARISON(TYPE, LHS, <=, RHS); \
161 TEST_BINARY_COMPARISON(TYPE, LHS, >=, RHS); \
162 } while (0)
163
164 #define TEST_BINARY_INT(TYPE, LHS, RHS) \
165 do { \
166 TEST_BINARY_FP(TYPE, LHS, RHS); \
167 TEST_BINARY(TYPE, LHS, %, RHS); \
168 TEST_BINARY(TYPE, LHS, &, RHS); \
169 TEST_BINARY(TYPE, LHS, |, RHS); \
170 TEST_BINARY(TYPE, LHS, ^, RHS); \
171 TEST_BINARY(TYPE, LHS, <<, RHS); \
172 TEST_BINARY(TYPE, LHS, >>, RHS); \
173 } while (0)
174
175 /*
176 * TODO(jfb) Pre/post ++/-- don't seem to be supported. Neither does !.
177 */
178 #define TEST_UNARY_FP(TYPE, VAL) \
179 do { \
180 TEST_UNARY(TYPE, +, VAL); \
181 TEST_UNARY(TYPE, -, VAL); \
182 } while (0)
183
184 #define TEST_UNARY_INT(TYPE, VAL) \
185 do { \
186 TEST_UNARY_FP(TYPE, VAL); \
187 TEST_UNARY(TYPE, ~, VAL); \
188 } while (0)
189
190 /*
191 * Vector values used in tests.
192 *
193 * Initialize everything in a non-inlined function to make sure that
194 * nothing gets pre-computed.
195 */
196 VI8 vi8[2];
197 VU8 vu8[2];
198 VI16 vi16[2];
199 VU16 vu16[2];
200 VI32 vi32[2];
201 VU32 vu32[2];
202 VF32 vf32[2];
203 __attribute__((noinline)) void init(void) {
204 /*
205 * TODO(jfb) Test undefined behavior: shift bit bitwidth or larger,
206 * and divide by zero.
207 */
208 vi8[0] = (VI8) {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
209 vi8[1] = (VI8) {2, 1, 1, 2, 3, 4, 5, 6, 7, 7, 6, 5, 4, 3, 2, 1};
210
211 vu8[0] = (VU8) {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
212 vu8[1] = (VU8) {2, 1, 1, 2, 3, 4, 5, 6, 7, 7, 6, 5, 4, 3, 2, 1};
213
214 vi16[0] = (VI16) {1, 2, 3, 4, 5, 6, 7, 8};
215 vi16[1] = (VI16) {1, 15, 14, 13, 12, 11, 10, 9};
216
217 vu16[0] = (VU16) {1, 2, 3, 4, 5, 6, 7, 8};
218 vu16[1] = (VU16) {1, 15, 14, 13, 12, 11, 10, 9};
219
220 vi32[0] = (VI32) {1, 2, 3, 4};
221 vi32[1] = (VI32) {16, 15, 14, 13};
222
223 vu32[0] = (VU32) {1, 2, 3, 4};
224 vu32[1] = (VU32) {16, 15, 14, 13};
225
226 vf32[0] = (VF32) {1, 2, 3, 4};
227 vf32[1] = (VF32) {16, 15, 14, 13};
228 }
229
230 __attribute__((noinline)) void test(void) {
231 TEST_BINARY_INT(I8, vi8[0], vi8[1]);
232 TEST_BINARY_INT(U8, vu8[0], vu8[1]);
233 TEST_BINARY_INT(I16, vi16[0], vi16[1]);
234 TEST_BINARY_INT(U16, vu16[0], vu16[1]);
235 TEST_BINARY_INT(I32, vi32[0], vi32[1]);
236 TEST_BINARY_INT(U32, vu32[0], vu32[1]);
237 TEST_BINARY_FP(F32, vf32[0], vf32[1]);
238
239 TEST_UNARY_INT(I8, vi8[0]);
240 TEST_UNARY_INT(U8, vu8[0]);
241 TEST_UNARY_INT(I16, vi16[0]);
242 TEST_UNARY_INT(U16, vu16[0]);
243 TEST_UNARY_INT(I32, vi32[0]);
244 TEST_UNARY_INT(U32, vu32[0]);
245 TEST_UNARY_FP(F32, vf32[0]);
246 }
247
248 int main(void) {
249 init();
250 test();
251
252 return 0;
253 }
OLDNEW
« no previous file with comments | « tests/simd/nacl.scons ('k') | tests/simd/vector_extension.stdout » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698