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