ext_gpu_shader4: add compiler tests for everything
[piglit.git] / generated_tests / gen_cl_int_builtins.py
blob28c441ee62c90c8269f8afa1f49684e04ee03fb3
1 # coding=utf-8
2 from __future__ import print_function, division, absolute_import
3 import os
4 from genclbuiltins import gen, DATA_SIZES, MAX_VALUES, MAX, MIN, BMIN, BMAX, \
5 SMIN, SMAX, UMIN, UMAX, TYPE, SIZE, T, U, B
7 # Builtins is a data structure of the following:
8 # builtins = {
9 # '{data_type}': { # data type is any of [u]char, [u]short, [u]int, [u]long
10 # '{builtin_function_name}': {
11 # 'arg_types': ['{data_type}', '{data_type}', ...],
12 # 'function_type': 'ttt'|'tss',
13 # ttt = all arguments are same-length vectors
14 # tss = all arguments are either same-length vectors,
15 # or a vector followed by scalars
16 # 'values': [
17 # [array of test output (arg0) values],
18 # [array of arg1 values],
19 # [array of arg2 values],
20 # ...
21 # ]
22 # },
23 # '{next_function}': {...},
24 # ...
25 # },
26 # '{next_type}': {...},
27 # ...
28 # }
30 # The builtins_generic, builtins_signed/unsigned are the same, but lack the
31 # {datatype} layer
33 SIGNED_TYPES = ['char', 'short', 'int', 'long']
34 UNSIGNED_TYPES = ['uchar', 'ushort', 'uint', 'ulong']
35 DATA_TYPES = SIGNED_TYPES + UNSIGNED_TYPES
38 CLC_VERSION_MIN = {
39 'abs': 10,
40 'abs_diff': 10,
41 'add_sat': 10,
42 'hadd': 10,
43 'rhadd': 10,
44 'clz': 10,
45 'clamp': 11,
46 'mad_hi': 10,
47 'mad_sat': 10,
48 'max': 11, # max/min are only same-size in CL1.0, but TSS in CL1.1
49 'min': 11,
50 'mul_hi': 10,
51 'rotate': 10,
52 'sub_sat': 10,
53 'upsample': 10,
54 'popcount': 12,
55 'mad24': 10,
56 'mul24': 10
60 def abs(val):
61 if (val < 0):
62 return val*-1
63 return val
66 def add(val1, val2):
67 return val1+val2
70 # Given a data type, return the next bigger type of given signedness.
71 def big(type):
72 return B[type]
75 def clz(type, val):
76 if (val < 0):
77 return 0
78 else:
79 # Count the number of times that we can right shift before value = 0
80 # then subtract that from (data_size - 1)
81 count = 0
82 while(val > 0):
83 if (val > 0):
84 val = val >> 1
85 count = count + 1
86 return DATA_SIZES[type] - count
88 def popcount(val):
89 return bin(val).count('1')
91 def div(val1, val2):
92 return val1 // val2
95 def mad_hi(x, y, z, type):
96 res = (x*y) >> DATA_SIZES[type]
97 res = res + z
98 while (res > MAX_VALUES[type]): # Emulate overflow... Necessary?
99 res = res - (2**DATA_SIZES[type])
100 return res
103 def mul(val1, val2):
104 return val1 * val2
107 def mul_hi(x, y, type):
108 res = (x*y) >> DATA_SIZES[type]
109 return res
112 def pow(val, pow):
113 return val ** pow
116 def rotate_right(x, n, bits):
117 # Find all bits that will wrap
118 mask = (1 << n) - 1
119 wrapped_bits = x & mask
121 # sign extension needs to be masked out
122 se_mask = (1 << (bits - n)) - 1
123 unwrapped_bits = x >> n
124 unwrapped_bits &= se_mask
126 return unwrapped_bits | (wrapped_bits << (bits - n))
129 def rotate_left(x, n, bits):
130 return rotate_right(x, bits - n, bits)
133 def rot(x, n, bits):
134 if (n < 0):
135 return rotate_right(x, -1*n, bits)
136 else:
137 return rotate_left(x, n, bits)
140 def sub(val1, val2):
141 return val1-val2
144 # Tests which don't depend on the signedness or bit-width of the inputs
145 generic_tests = {
146 'abs': {
147 'arg_types': [U, T],
148 'function_type': 'ttt',
149 'values': [
150 [0, 2, [abs, MIN], [abs, MAX]],
151 [0, 2, MIN, MAX]
154 'abs_diff': {
155 'arg_types': [U, T, T],
156 'function_type': 'ttt',
157 'values': [
158 [0, 1, 1, UMAX, UMAX],
159 [0, 1, 0, MIN, MAX],
160 [0, 0, 1, MAX, MIN]
163 'add_sat': {
164 'arg_types': [T, T, T],
165 'function_type': 'ttt',
166 'values': [
167 [MAX, MAX],
168 [MAX, [sub, MAX, 1]],
169 [64, 50]
172 'hadd': {
173 'arg_types': [T, T, T],
174 'function_type': 'ttt',
175 'values': [
176 [32, 0, 10, MAX, MIN,
177 [div, [add, MAX, MIN], 2],
178 [div, [add, MIN, MAX], 2]],
179 [32, 1, 12, MAX, MIN, MAX, MIN],
180 [33, 0, 8, MAX, MIN, MIN, MAX]
183 'clz': {
184 'arg_types': [T, T],
185 'function_type': 'ttt',
186 'values': [
187 [[clz, TYPE, 1],
188 [clz, TYPE, 64],
189 [clz, TYPE, 0],
190 [clz, TYPE, MAX],
191 [clz, TYPE, MIN]],
192 [1, 64, 0, MAX, MIN]]
194 'clamp': {
195 'arg_types': [T, T, T, T],
196 'function_type': 'tss',
197 'values': [
198 [64, [div, MIN, 2], 1],
199 [92, MIN, 64],
200 [0, [div, MIN, 2], 0],
201 [64, 0, 1]
204 'mad_hi': {
205 'arg_types': [T, T, T, T],
206 'function_type': 'ttt',
207 'values': [
208 [[mad_hi, [div, MAX, 2], 3, 1, TYPE],
209 [mad_hi, MIN, 2, 2, TYPE], 4, 1,
210 [mad_hi, MAX, MAX, MAX, TYPE],
211 [mad_hi, MIN, MIN, MIN, TYPE],
212 [mad_hi, MIN, MAX, MAX, TYPE],
213 [mad_hi, MAX, 2, 2, TYPE]],
214 [[div, MAX, 2], MIN, 12, MAX, MAX, MIN, MIN, MAX],
215 [ 3, 2, 4, 1, MAX, MIN, MAX, 2],
216 [ 1, 2, 4, 1, MAX, MIN, MAX, 2]
219 'mad_sat': {
220 'arg_types': [T, T, T, T],
221 'function_type': 'ttt',
222 'values': [
223 [52, MAX, 93, 0, MAX, MAX],
224 [12, MAX, 92, 0, MAX, MAX],
225 [ 4, 1, 1, 0, 2, MAX],
226 [ 4, 1, 1, 0, 2, MAX]
229 'max': {
230 'arg_types': [T, T, T],
231 'function_type': 'tss',
232 'values': [
233 [92, 2, 12, MAX, 1, MAX, MIN, MAX, MAX, 0],
234 [92, 2, 12, MAX, MIN, MAX, MIN, MIN, MAX, 0],
235 [ 2, MIN, 4, 1, 1, MAX, MIN, MAX, MIN, 0]
238 'min': {
239 'arg_types': [T, T, T],
240 'function_type': 'tss',
241 'values': [
242 [ 2, 1, MIN, 4, 1, MIN, MAX, MIN, MIN, 0, MAX],
243 [92, 64, 2, 12, MAX, MIN, MAX, MIN, MIN, 0, MAX],
244 [ 2, 1, MIN, 4, 1, 1, MAX, MIN, MAX, 0, MAX]
247 'mul_hi': {
248 'arg_types': [T, T, T],
249 'function_type': 'ttt',
250 'values': [
251 [0, 0, 0,
252 [mul_hi, MAX, MAX, TYPE],
253 [mul_hi, MIN, MIN, TYPE], 0,
254 [mul_hi, MAX, 2, TYPE],
255 [div, MIN, 2]
257 [0, 92, MAX, MAX, MIN, 92, MAX, MIN],
258 [0, 2, 1, MAX, MIN, 1, 2, MAX]
261 'rhadd': {
262 'arg_types': [T, T, T],
263 'function_type': 'ttt',
264 'values': [
265 [33, 1, 10],
266 [32, 1, 12],
267 [33, 0, 8]
270 'rotate': {
271 'arg_types': [T, T, T],
272 'function_type': 'ttt',
273 'values': [
274 [1, 8, 1, 2, 1],
275 [1, 1, 1, 1, 1],
276 [0, 3, SIZE,
277 [add, SIZE, 1],
278 [mul, SIZE, 10]]
281 'sub_sat': {
282 'arg_types': [T, T, T],
283 'function_type': 'ttt',
284 'values': [
285 [1, 25, MAX, 0, 0, MIN, MIN],
286 [1, 57, MAX, MAX, MIN, MIN, [div, MIN, 2]],
287 [0, 32, MIN, MAX, MIN, MAX, [add, [div, MAX, 2], 1]]
290 'upsample': {
291 'arg_types': [B, T, U],
292 'function_type': 'ttt',
293 'values': [
294 [[pow, 2, SIZE],
295 [add, [pow, 2, SIZE], 1],
296 BMAX, 0, MAX,
297 [add, [pow, 2, SIZE], 7]],
298 [1, 1, MAX, 0, 0, 1],
299 [0, 1, UMAX, 0, MAX, 7]
302 'popcount': {
303 'arg_types': [T, T],
304 'function_type': 'ttt',
305 'values': [
306 [[popcount, 1],
307 [popcount, 3],
308 [popcount, 0],
309 [popcount, MAX],
310 [popcount, MIN]],
311 [1, 3, 0, MAX, MIN]]
315 # Any test that conceivably includes a negative number as input/output
316 signed_generic_tests = {
317 'abs': {
318 'arg_types': [U, T],
319 'function_type': 'ttt',
320 'values': [
321 [ 1, 13],
322 [-1, -13]
325 'abs_diff': {
326 'arg_types': [U, T, T],
327 'function_type': 'ttt',
328 'values': [
329 [1, 15],
330 [0, -8],
331 [1, 7]
334 'add_sat': {
335 'arg_types': [T, T, T],
336 'function_type': 'ttt',
337 'values': [
338 [ 0, -2, [sub, MAX, 63], MIN, MIN],
339 [ 32, -8, MAX, MIN, [add, MIN, 10]],
340 [-32, 6, -63, -12, -50]
343 'clamp': {
344 'arg_types': [T, T, T, T],
345 'function_type': 'tss',
346 'values': [
347 [ -64, 0],
348 [-128, -1],
349 [ -64, 0],
350 [ 0, 1]
353 'mad_hi': {
354 'arg_types': [T, T, T, T],
355 'function_type': 'ttt',
356 'values': [
357 [MIN, -2],
358 [ -1, 1],
359 [MIN, -1],
360 [MIN, -1]
363 'mad_sat': {
364 'arg_types': [T, T, T, T],
365 'function_type': 'ttt',
366 'values': [
367 [ 0, MIN, MIN, MAX, MIN, -2],
368 [ -1, MIN, MIN, MIN, MIN, 1],
369 [MIN, 2, 1, MIN, MAX, -1],
370 [MIN, 2, -1, MIN, MAX, -1]
373 'max': {
374 'arg_types': [T, T, T],
375 'function_type': 'tss',
376 'values': [
377 [ -1, 1],
378 [ -1, 1],
379 [MIN, -1]
382 'min': {
383 'arg_types': [T, T, T],
384 'function_type': 'tss',
385 'values': [
386 [MIN, -1, MIN],
387 [ -1, 1, MIN],
388 [MIN, -1, -1]
391 'mul_hi': {
392 'arg_types': [T, T, T],
393 'function_type': 'ttt',
394 'values': [
395 [ 0 , -1, -1, -1],
396 [ -1, MIN, MIN, 1],
397 [MIN, 2, 1, -1]
400 'rhadd': {
401 'arg_types': [T, T, T],
402 'function_type': 'ttt',
403 'values': [[-32], [-33], [-32]]
405 'rotate': {
406 'arg_types': [T, T, T],
407 'function_type': 'ttt',
408 'values': [
409 [MIN, MIN, [rot, 1, -3, SIZE], 1, MIN, [pow, 2, [sub, SIZE, 2]],
410 MIN, [rot, -2, -1, SIZE]], [1, 1, 1, 1, 1, 1, 1, -2],
411 [[sub, SIZE, 1], -1, -3, [mul, SIZE, -1],
412 [mul, [add, SIZE, 1], -1],
413 [mul, [add, SIZE, 2], -1], [sub, SIZE, 1], -1]
416 'sub_sat': {
417 'arg_types': [T, T, T],
418 'function_type': 'ttt',
419 'values': [
420 [MAX, 81],
421 [[sub, MAX, 8], 1],
422 [-32, -80]
425 'upsample': {
426 'arg_types': [B, T, U],
427 'function_type': 'ttt',
428 'values': [
429 [ -1, [mul, [pow, 2, SIZE], -1]],
430 [ -1, -1],
431 [UMAX, 0]
437 # This list contains all numeric tests which never include negative integers
438 # that can't go into generic_tests.
439 unsigned_generic_tests = {
440 'mad_sat': {
441 'arg_types': [T, T, T, T],
442 'function_type': 'ttt',
443 'values': [
444 [2, MIN, MAX],
445 [MIN, MIN, MIN],
446 [2, MIN, MAX],
447 [2, MIN, MAX]
450 'rotate': {
451 'arg_types': [T, T, T],
452 'function_type': 'ttt',
453 'values': [
454 [[div, [add, MAX, 1], 2], [div, [add, MAX, 1], 2]],
455 [1, 1], [[sub, SIZE, 1], [sub, SIZE, 1]]
460 # Hand-coded tests which are data type specific.
461 builtins = {
462 'int': {
463 'mad24': {
464 'arg_types': [T, T, T, T],
465 'function_type': 'ttt',
466 'values': [
467 [0, 2, 0, 520],
468 [0, 1, -1, 32],
469 [0, 1, -1, 16],
470 [0, 1, -1, 8]
473 'mul24': {
474 'arg_types': [T, T, T],
475 'function_type': 'ttt',
476 'values': [
477 [0, 1, 1, 512, 4278190081],
478 [0, 1, -1, 32, 2**23-1],
479 [0, 1, -1, 16, 2**23-1]
483 'uint': {
484 'mad24': {
485 'arg_types': [T, T, T, T],
486 'function_type': 'ttt',
487 'values': [
488 [0, 2, 4278190080, 520],
489 [0, 1, 2**24-1, 32],
490 [0, 1, 2**24-1, 16],
491 [0, 1, 2**24-1, 8]
494 'mul24': {
495 'arg_types': [T, T, T],
496 'function_type': 'ttt',
497 'values': [
498 [0, 1, 4261412865, 512],
499 [0, 1, 2**24-1, 32],
500 [0, 1, 2**24-1, 16]
507 # # # # Define helper functions # # # #
510 def addTestValues(origDef, origValues):
511 fnDef = dict(origDef)
512 values = list(origValues)
513 if (not 'values' in fnDef):
514 fnDef['values'] = []
515 for idx in range(0, len(values)):
516 fnDef['values'].append(list(values[idx]))
517 else:
518 for arg in range(0, len(values)):
519 fnDef['values'][arg] += values[arg]
520 return fnDef
523 # Given a data type and function name, create one set of combined applicable
524 # test definitions.
525 def mergedTestDefinition(dataType, fnName):
526 mergedTest = dict()
528 testLists = [generic_tests]
529 if (dataType in SIGNED_TYPES):
530 testLists += [signed_generic_tests]
531 if (dataType in UNSIGNED_TYPES):
532 testLists += [unsigned_generic_tests]
533 if (dataType in builtins):
534 testLists += [builtins[dataType]]
536 for testList in testLists:
537 if (fnName in testList):
538 fnDef = dict(testList[fnName])
539 if (not 'arg_types' in mergedTest):
540 mergedTest['arg_types'] = list(fnDef['arg_types'])
541 mergedTest['function_type'] = fnDef['function_type']
542 mergedTest = addTestValues(dict(mergedTest), list(fnDef['values']))
543 return mergedTest
546 def getFnNames():
547 fnNames = []
548 fnNames += generic_tests.keys()
549 fnNames += signed_generic_tests.keys()
550 fnNames += unsigned_generic_tests.keys()
551 for type in DATA_TYPES:
552 if (type in builtins):
553 fnNames += builtins[type].keys()
555 # Get the sorted unique set of function names
556 return sorted(list(set(fnNames)))
559 def main():
560 dirName = os.path.join("cl", "builtin", "int")
562 testDefs = {}
563 functions = getFnNames()
565 for dataType in DATA_TYPES:
566 for fnName in functions:
567 if (fnName is 'upsample' and
568 (dataType is 'long' or dataType is 'ulong')):
569 continue
570 # Merge all of the generic/signed/unsigned/custom test definitions
571 testDefs[(dataType, fnName)] = mergedTestDefinition(dataType, fnName)
573 gen(DATA_TYPES, CLC_VERSION_MIN, functions, testDefs, dirName)
576 if __name__ == '__main__':
577 main()