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

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: Use C99 instead of C11, and disable the test for GCC since 4.4 doesn't support the vector extension… 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
13 #include "native_client/src/include/nacl_macros.h"
14
15 #include <stdint.h>
16 #include <stdio.h>
17
nfullagar1 2014/04/16 23:04:57 can you add a description/goal of what this file i
JF 2014/04/17 00:05:44 Done.
18 /*
19 * Basic types that are supported inside vectors.
20 *
21 * TODO(jfb) Handle 64-bit int and double.
22 */
23 typedef int8_t I8;
24 typedef uint8_t U8;
25 typedef int16_t I16;
26 typedef uint16_t U16;
27 typedef int32_t I32;
28 typedef uint32_t U32;
29 typedef float F32;
30
31 /*
32 *
33 * The GCC/LLVM vector extensions represent the results of comparisons
34 * as a vector of all-ones or all-zeros with the same vector bit width
35 * and number of elements. They must be treated differently than their
36 * corresponding type because floating-point values change their bit
37 * representation through assignments when they hold NaN values.
38 */
39 typedef int8_t I8_BOOL;
40 typedef int8_t U8_BOOL;
41 typedef int16_t I16_BOOL;
42 typedef int16_t U16_BOOL;
43 typedef int32_t I32_BOOL;
44 typedef int32_t U32_BOOL;
45 typedef int32_t F32_BOOL;
46
47 #define I8_FMT "i"
48 #define U8_FMT "u"
49 #define I16_FMT "i"
50 #define U16_FMT "u"
51 #define I32_FMT "i"
52 #define U32_FMT "u"
53 #define F32_FMT "f"
54
55 /* All elements in a boolean vector should print as 0 or -1. */
56 #define I8_BOOL_FMT "i"
57 #define U8_BOOL_FMT "i"
58 #define I16_BOOL_FMT "i"
59 #define U16_BOOL_FMT "i"
60 #define I32_BOOL_FMT "i"
61 #define U32_BOOL_FMT "i"
62 #define F32_BOOL_FMT "i"
63
64 /* All supported vector types are currently 128-bit wide. */
65 #define VEC_BYTES 16
66
67 /* Vector types corresponding to each supported basic types. */
68 typedef I8 VI8 __attribute__((vector_size(VEC_BYTES)));
69 typedef U8 VU8 __attribute__((vector_size(VEC_BYTES)));
70 typedef I16 VI16 __attribute__((vector_size(VEC_BYTES)));
71 typedef U16 VU16 __attribute__((vector_size(VEC_BYTES)));
72 typedef I32 VI32 __attribute__((vector_size(VEC_BYTES)));
73 typedef U32 VU32 __attribute__((vector_size(VEC_BYTES)));
74 typedef F32 VF32 __attribute__((vector_size(VEC_BYTES)));
nfullagar1 2014/04/16 23:04:57 The names (ie 'I8') chosen here (and elsewhere) wi
JF 2014/04/17 00:05:44 Agreed, we'll need to add width to the name once w
75
76 /* Boolean vector types generate by comparisons on each vector type. */
77 typedef I8 VI8_BOOL __attribute__((vector_size(VEC_BYTES)));
78 typedef I8 VU8_BOOL __attribute__((vector_size(VEC_BYTES)));
79 typedef I16 VI16_BOOL __attribute__((vector_size(VEC_BYTES)));
80 typedef I16 VU16_BOOL __attribute__((vector_size(VEC_BYTES)));
81 typedef I32 VI32_BOOL __attribute__((vector_size(VEC_BYTES)));
82 typedef I32 VU32_BOOL __attribute__((vector_size(VEC_BYTES)));
83 typedef I32 VF32_BOOL __attribute__((vector_size(VEC_BYTES)));
84
85 #define PRINT(TYPE, VEC) \
86 do { \
87 NACL_COMPILE_TIME_ASSERT(sizeof(V##TYPE) == \
88 VEC_BYTES); /* Vector must be 128 bits. */ \
89 NACL_COMPILE_TIME_ASSERT(sizeof(TYPE) == \
90 sizeof(VEC[0])); /* Type must match. */ \
91 printf("{"); \
92 for (size_t i = 0; i != sizeof(V##TYPE) / sizeof(VEC[0]); ++i) \
93 printf("%" TYPE##_FMT ",", VEC[i]); \
94 printf("}"); \
95 } while (0)
96
97 #define TEST_BINARY(TYPE, LHS, OP, RHS) \
98 do { \
99 NACL_COMPILE_TIME_ASSERT(sizeof(TYPE) == \
100 sizeof(LHS[0])); /* Types must match. */ \
101 NACL_COMPILE_TIME_ASSERT(sizeof(TYPE) == \
102 sizeof(RHS[0])); /* Types must match. */ \
103 const V##TYPE result = LHS OP RHS; \
104 printf(#TYPE " "); \
105 PRINT(TYPE, LHS); \
106 printf(" %s ", #OP); \
107 PRINT(TYPE, RHS); \
108 printf(" = "); \
109 PRINT(TYPE, result); \
110 printf("\n"); \
111 } while (0)
112
113 #define TEST_BINARY_COMPARISON(TYPE, LHS, OP, RHS) \
114 do { \
115 NACL_COMPILE_TIME_ASSERT(sizeof(TYPE) == \
116 sizeof(LHS[0])); /* Types must match. */ \
117 NACL_COMPILE_TIME_ASSERT(sizeof(TYPE) == \
118 sizeof(RHS[0])); /* Types must match. */ \
119 const V##TYPE##_BOOL result = LHS OP RHS; \
120 printf(#TYPE " "); \
121 PRINT(TYPE, LHS); \
122 printf(" %s ", #OP); \
123 PRINT(TYPE, RHS); \
124 printf(" = "); \
125 PRINT(TYPE##_BOOL, result); \
126 printf("\n"); \
127 } while (0)
128
129 #define TEST_UNARY(TYPE, OP, VAL) \
130 do { \
131 NACL_COMPILE_TIME_ASSERT(sizeof(TYPE) == \
132 sizeof(VAL[0])); /* Types must match. */ \
133 const V##TYPE result = OP VAL; \
134 printf(#TYPE " %s ", #OP); \
135 PRINT(TYPE, VAL); \
136 printf(" = "); \
137 PRINT(TYPE, result); \
138 printf("\n"); \
139 } while (0)
140
141 #define TEST_BINARY_FP(TYPE, LHS, RHS) \
142 do { \
143 TEST_BINARY(TYPE, LHS, +, RHS); \
144 TEST_BINARY(TYPE, LHS, -, RHS); \
145 TEST_BINARY(TYPE, LHS, *, RHS); \
146 TEST_BINARY(TYPE, LHS, /, RHS); \
147 TEST_BINARY_COMPARISON(TYPE, LHS, ==, RHS); \
148 TEST_BINARY_COMPARISON(TYPE, LHS, !=, RHS); \
149 TEST_BINARY_COMPARISON(TYPE, LHS, <, RHS); \
150 TEST_BINARY_COMPARISON(TYPE, LHS, >, RHS); \
151 TEST_BINARY_COMPARISON(TYPE, LHS, <=, RHS); \
152 TEST_BINARY_COMPARISON(TYPE, LHS, >=, RHS); \
153 } while (0)
154
155 #define TEST_BINARY_INT(TYPE, LHS, RHS) \
156 do { \
157 TEST_BINARY_FP(TYPE, LHS, RHS); \
158 TEST_BINARY(TYPE, LHS, %, RHS); \
159 TEST_BINARY(TYPE, LHS, &, RHS); \
160 TEST_BINARY(TYPE, LHS, |, RHS); \
161 TEST_BINARY(TYPE, LHS, ^, RHS); \
162 TEST_BINARY(TYPE, LHS, <<, RHS); \
163 TEST_BINARY(TYPE, LHS, >>, RHS); \
164 } while (0)
165
166 /*
167 * TODO(jfb) Pre/post ++/-- don't seem to be supported. Neither does !.
168 */
169 #define TEST_UNARY_FP(TYPE, VAL) \
170 do { \
171 TEST_UNARY(TYPE, +, VAL); \
172 TEST_UNARY(TYPE, -, VAL); \
173 } while (0)
174
175 #define TEST_UNARY_INT(TYPE, VAL) \
176 do { \
177 TEST_UNARY_FP(TYPE, VAL); \
178 TEST_UNARY(TYPE, ~, VAL); \
179 } while (0)
180
181 /*
182 * Vector values used in tests.
183 *
184 * Initialize everything in a non-inlined function to make sure that
185 * nothing gets pre-computed.
186 */
187 VI8 vi8[2];
188 VU8 vu8[2];
189 VI16 vi16[2];
190 VU16 vu16[2];
191 VI32 vi32[2];
192 VU32 vu32[2];
193 VF32 vf32[2];
194 __attribute__((noinline)) void init(void) {
195 /*
196 * TODO(jfb) Test undefined behavior: shift bit bitwidth or larger,
197 * and divide by zero.
198 */
199 vi8[0] = (VI8) {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
200 vi8[1] = (VI8) {2, 1, 1, 2, 3, 4, 5, 6, 7, 7, 6, 5, 4, 3, 2, 1};
201
202 vu8[0] = (VU8) {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
203 vu8[1] = (VU8) {2, 1, 1, 2, 3, 4, 5, 6, 7, 7, 6, 5, 4, 3, 2, 1};
204
205 vi16[0] = (VI16) {1, 2, 3, 4, 5, 6, 7, 8};
206 vi16[1] = (VI16) {1, 15, 14, 13, 12, 11, 10, 9};
207
208 vu16[0] = (VU16) {1, 2, 3, 4, 5, 6, 7, 8};
209 vu16[1] = (VU16) {1, 15, 14, 13, 12, 11, 10, 9};
210
211 vi32[0] = (VI32) {1, 2, 3, 4};
212 vi32[1] = (VI32) {16, 15, 14, 13};
213
214 vu32[0] = (VU32) {1, 2, 3, 4};
215 vu32[1] = (VU32) {16, 15, 14, 13};
216
217 vf32[0] = (VF32) {1, 2, 3, 4};
218 vf32[1] = (VF32) {16, 15, 14, 13};
219 }
220
221 __attribute__((noinline)) void test(void) {
222 TEST_BINARY_INT(I8, vi8[0], vi8[1]);
223 TEST_BINARY_INT(U8, vu8[0], vu8[1]);
224 TEST_BINARY_INT(I16, vi16[0], vi16[1]);
225 TEST_BINARY_INT(U16, vu16[0], vu16[1]);
226 TEST_BINARY_INT(I32, vi32[0], vi32[1]);
227 TEST_BINARY_INT(U32, vu32[0], vu32[1]);
228 TEST_BINARY_FP(F32, vf32[0], vf32[1]);
229
230 TEST_UNARY_INT(I8, vi8[0]);
231 TEST_UNARY_INT(U8, vu8[0]);
232 TEST_UNARY_INT(I16, vi16[0]);
233 TEST_UNARY_INT(U16, vu16[0]);
234 TEST_UNARY_INT(I32, vi32[0]);
235 TEST_UNARY_INT(U32, vu32[0]);
236 TEST_UNARY_FP(F32, vf32[0]);
237 }
238
239 int main(void) {
240 init();
241 test();
242
243 return 0;
244 }
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