2 from __future__
import print_function
, division
, absolute_import
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:
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
17 # [array of test output (arg0) values],
18 # [array of arg1 values],
19 # [array of arg2 values],
23 # '{next_function}': {...},
26 # '{next_type}': {...},
30 # The builtins_generic, builtins_signed/unsigned are the same, but lack the
33 SIGNED_TYPES
= ['char', 'short', 'int', 'long']
34 UNSIGNED_TYPES
= ['uchar', 'ushort', 'uint', 'ulong']
35 DATA_TYPES
= SIGNED_TYPES
+ UNSIGNED_TYPES
48 'max': 11, # max/min are only same-size in CL1.0, but TSS in CL1.1
70 # Given a data type, return the next bigger type of given signedness.
79 # Count the number of times that we can right shift before value = 0
80 # then subtract that from (data_size - 1)
86 return DATA_SIZES
[type] - count
89 return bin(val
).count('1')
95 def mad_hi(x
, y
, z
, type):
96 res
= (x
*y
) >> DATA_SIZES
[type]
98 while (res
> MAX_VALUES
[type]): # Emulate overflow... Necessary?
99 res
= res
- (2**DATA_SIZES
[type])
107 def mul_hi(x
, y
, type):
108 res
= (x
*y
) >> DATA_SIZES
[type]
116 def rotate_right(x
, n
, bits
):
117 # Find all bits that will wrap
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
)
135 return rotate_right(x
, -1*n
, bits
)
137 return rotate_left(x
, n
, bits
)
144 # Tests which don't depend on the signedness or bit-width of the inputs
148 'function_type': 'ttt',
150 [0, 2, [abs, MIN
], [abs, MAX
]],
155 'arg_types': [U
, T
, T
],
156 'function_type': 'ttt',
158 [0, 1, 1, UMAX
, UMAX
],
164 'arg_types': [T
, T
, T
],
165 'function_type': 'ttt',
168 [MAX
, [sub
, MAX
, 1]],
173 'arg_types': [T
, T
, T
],
174 'function_type': 'ttt',
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
]
185 'function_type': 'ttt',
192 [1, 64, 0, MAX
, MIN
]]
195 'arg_types': [T
, T
, T
, T
],
196 'function_type': 'tss',
198 [64, [div
, MIN
, 2], 1],
200 [0, [div
, MIN
, 2], 0],
205 'arg_types': [T
, T
, T
, T
],
206 'function_type': 'ttt',
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]
220 'arg_types': [T
, T
, T
, T
],
221 'function_type': 'ttt',
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
]
230 'arg_types': [T
, T
, T
],
231 'function_type': 'tss',
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]
239 'arg_types': [T
, T
, T
],
240 'function_type': 'tss',
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
]
248 'arg_types': [T
, T
, T
],
249 'function_type': 'ttt',
252 [mul_hi
, MAX
, MAX
, TYPE
],
253 [mul_hi
, MIN
, MIN
, TYPE
], 0,
254 [mul_hi
, MAX
, 2, TYPE
],
257 [0, 92, MAX
, MAX
, MIN
, 92, MAX
, MIN
],
258 [0, 2, 1, MAX
, MIN
, 1, 2, MAX
]
262 'arg_types': [T
, T
, T
],
263 'function_type': 'ttt',
271 'arg_types': [T
, T
, T
],
272 'function_type': 'ttt',
282 'arg_types': [T
, T
, T
],
283 'function_type': 'ttt',
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]]
291 'arg_types': [B
, T
, U
],
292 'function_type': 'ttt',
295 [add
, [pow, 2, SIZE
], 1],
297 [add
, [pow, 2, SIZE
], 7]],
298 [1, 1, MAX
, 0, 0, 1],
299 [0, 1, UMAX
, 0, MAX
, 7]
304 'function_type': 'ttt',
315 # Any test that conceivably includes a negative number as input/output
316 signed_generic_tests
= {
319 'function_type': 'ttt',
326 'arg_types': [U
, T
, T
],
327 'function_type': 'ttt',
335 'arg_types': [T
, T
, T
],
336 'function_type': 'ttt',
338 [ 0, -2, [sub
, MAX
, 63], MIN
, MIN
],
339 [ 32, -8, MAX
, MIN
, [add
, MIN
, 10]],
340 [-32, 6, -63, -12, -50]
344 'arg_types': [T
, T
, T
, T
],
345 'function_type': 'tss',
354 'arg_types': [T
, T
, T
, T
],
355 'function_type': 'ttt',
364 'arg_types': [T
, T
, T
, T
],
365 'function_type': 'ttt',
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]
374 'arg_types': [T
, T
, T
],
375 'function_type': 'tss',
383 'arg_types': [T
, T
, T
],
384 'function_type': 'tss',
392 'arg_types': [T
, T
, T
],
393 'function_type': 'ttt',
401 'arg_types': [T
, T
, T
],
402 'function_type': 'ttt',
403 'values': [[-32], [-33], [-32]]
406 'arg_types': [T
, T
, T
],
407 'function_type': 'ttt',
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]
417 'arg_types': [T
, T
, T
],
418 'function_type': 'ttt',
426 'arg_types': [B
, T
, U
],
427 'function_type': 'ttt',
429 [ -1, [mul
, [pow, 2, SIZE
], -1]],
437 # This list contains all numeric tests which never include negative integers
438 # that can't go into generic_tests.
439 unsigned_generic_tests
= {
441 'arg_types': [T
, T
, T
, T
],
442 'function_type': 'ttt',
451 'arg_types': [T
, T
, T
],
452 'function_type': 'ttt',
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.
464 'arg_types': [T
, T
, T
, T
],
465 'function_type': 'ttt',
474 'arg_types': [T
, T
, T
],
475 'function_type': 'ttt',
477 [0, 1, 1, 512, 4278190081],
478 [0, 1, -1, 32, 2**23-1],
479 [0, 1, -1, 16, 2**23-1]
485 'arg_types': [T
, T
, T
, T
],
486 'function_type': 'ttt',
488 [0, 2, 4278190080, 520],
495 'arg_types': [T
, T
, T
],
496 'function_type': 'ttt',
498 [0, 1, 4261412865, 512],
507 # # # # Define helper functions # # # #
510 def addTestValues(origDef
, origValues
):
511 fnDef
= dict(origDef
)
512 values
= list(origValues
)
513 if (not 'values' in fnDef
):
515 for idx
in range(0, len(values
)):
516 fnDef
['values'].append(list(values
[idx
]))
518 for arg
in range(0, len(values
)):
519 fnDef
['values'][arg
] += values
[arg
]
523 # Given a data type and function name, create one set of combined applicable
525 def mergedTestDefinition(dataType
, fnName
):
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']))
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
)))
560 dirName
= os
.path
.join("cl", "builtin", "int")
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')):
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__':