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:
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
16 # [array of test output (arg0) values],
17 # [array of arg1 values],
18 # [array of arg2 values],
22 # '{next_function}': {...},
25 # '{next_type}': {...},
29 # The builtins_generic, builtins_signed/unsigned are the same, but lack the
32 SIGNED_TYPES
= ['char', 'short', 'int', 'long']
33 UNSIGNED_TYPES
= ['uchar', 'ushort', 'uint', 'ulong']
34 DATA_TYPES
= SIGNED_TYPES
+ UNSIGNED_TYPES
47 'max': 11, # max/min are only same-size in CL1.0, but TSS in CL1.1
69 # Given a data type, return the next bigger type of given signedness.
78 # Count the number of times that we can right shift before value = 0
79 # then subtract that from (data_size - 1)
85 return DATA_SIZES
[type] - count
88 return bin(val
).count('1')
94 def mad_hi(x
, y
, z
, type):
95 res
= (x
*y
) >> DATA_SIZES
[type]
97 while (res
> MAX_VALUES
[type]): # Emulate overflow... Necessary?
98 res
= res
- (2**DATA_SIZES
[type])
106 def mul_hi(x
, y
, type):
107 res
= (x
*y
) >> DATA_SIZES
[type]
115 def rotate_right(x
, n
, bits
):
116 # Find all bits that will wrap
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
)
134 return rotate_right(x
, -1*n
, bits
)
136 return rotate_left(x
, n
, bits
)
143 # Tests which don't depend on the signedness or bit-width of the inputs
147 'function_type': 'ttt',
149 [0, 2, [abs, MIN
], [abs, MAX
]],
154 'arg_types': [U
, T
, T
],
155 'function_type': 'ttt',
157 [0, 1, 1, UMAX
, UMAX
],
163 'arg_types': [T
, T
, T
],
164 'function_type': 'ttt',
167 [MAX
, [sub
, MAX
, 1]],
172 'arg_types': [T
, T
, T
],
173 'function_type': 'ttt',
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
]
184 'function_type': 'ttt',
191 [1, 64, 0, MAX
, MIN
]]
194 'arg_types': [T
, T
, T
, T
],
195 'function_type': 'tss',
197 [64, [div
, MIN
, 2], 1],
199 [0, [div
, MIN
, 2], 0],
204 'arg_types': [T
, T
, T
, T
],
205 'function_type': 'ttt',
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]
219 'arg_types': [T
, T
, T
, T
],
220 'function_type': 'ttt',
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
]
229 'arg_types': [T
, T
, T
],
230 'function_type': 'tss',
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]
238 'arg_types': [T
, T
, T
],
239 'function_type': 'tss',
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
]
247 'arg_types': [T
, T
, T
],
248 'function_type': 'ttt',
251 [mul_hi
, MAX
, MAX
, TYPE
],
252 [mul_hi
, MIN
, MIN
, TYPE
], 0,
253 [mul_hi
, MAX
, 2, TYPE
],
256 [0, 92, MAX
, MAX
, MIN
, 92, MAX
, MIN
],
257 [0, 2, 1, MAX
, MIN
, 1, 2, MAX
]
261 'arg_types': [T
, T
, T
],
262 'function_type': 'ttt',
270 'arg_types': [T
, T
, T
],
271 'function_type': 'ttt',
281 'arg_types': [T
, T
, T
],
282 'function_type': 'ttt',
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]]
290 'arg_types': [B
, T
, U
],
291 'function_type': 'ttt',
294 [add
, [pow, 2, SIZE
], 1],
296 [add
, [pow, 2, SIZE
], 7]],
297 [1, 1, MAX
, 0, 0, 1],
298 [0, 1, UMAX
, 0, MAX
, 7]
303 'function_type': 'ttt',
314 # Any test that conceivably includes a negative number as input/output
315 signed_generic_tests
= {
318 'function_type': 'ttt',
325 'arg_types': [U
, T
, T
],
326 'function_type': 'ttt',
334 'arg_types': [T
, T
, T
],
335 'function_type': 'ttt',
337 [ 0, -2, [sub
, MAX
, 63], MIN
, MIN
],
338 [ 32, -8, MAX
, MIN
, [add
, MIN
, 10]],
339 [-32, 6, -63, -12, -50]
343 'arg_types': [T
, T
, T
, T
],
344 'function_type': 'tss',
353 'arg_types': [T
, T
, T
, T
],
354 'function_type': 'ttt',
363 'arg_types': [T
, T
, T
, T
],
364 'function_type': 'ttt',
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]
373 'arg_types': [T
, T
, T
],
374 'function_type': 'tss',
382 'arg_types': [T
, T
, T
],
383 'function_type': 'tss',
391 'arg_types': [T
, T
, T
],
392 'function_type': 'ttt',
400 'arg_types': [T
, T
, T
],
401 'function_type': 'ttt',
402 'values': [[-32], [-33], [-32]]
405 'arg_types': [T
, T
, T
],
406 'function_type': 'ttt',
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]
416 'arg_types': [T
, T
, T
],
417 'function_type': 'ttt',
425 'arg_types': [B
, T
, U
],
426 'function_type': 'ttt',
428 [ -1, [mul
, [pow, 2, SIZE
], -1]],
436 # This list contains all numeric tests which never include negative integers
437 # that can't go into generic_tests.
438 unsigned_generic_tests
= {
440 'arg_types': [T
, T
, T
, T
],
441 'function_type': 'ttt',
450 'arg_types': [T
, T
, T
],
451 'function_type': 'ttt',
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.
463 'arg_types': [T
, T
, T
, T
],
464 'function_type': 'ttt',
473 'arg_types': [T
, T
, T
],
474 'function_type': 'ttt',
476 [0, 1, 1, 512, 4278190081],
477 [0, 1, -1, 32, 2**23-1],
478 [0, 1, -1, 16, 2**23-1]
484 'arg_types': [T
, T
, T
, T
],
485 'function_type': 'ttt',
487 [0, 2, 4278190080, 520],
494 'arg_types': [T
, T
, T
],
495 'function_type': 'ttt',
497 [0, 1, 4261412865, 512],
506 # # # # Define helper functions # # # #
509 def addTestValues(origDef
, origValues
):
510 fnDef
= dict(origDef
)
511 values
= list(origValues
)
512 if (not 'values' in fnDef
):
514 for idx
in range(0, len(values
)):
515 fnDef
['values'].append(list(values
[idx
]))
517 for arg
in range(0, len(values
)):
518 fnDef
['values'][arg
] += values
[arg
]
522 # Given a data type and function name, create one set of combined applicable
524 def mergedTestDefinition(dataType
, fnName
):
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']))
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
)))
559 dirName
= os
.path
.join("cl", "builtin", "int")
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')):
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__':