gen_cl_int_builtins.py 14.7 KB
Newer Older
1
from __future__ import print_function, division
2
import os
3 4
from genclbuiltins import gen, DATA_SIZES, MAX_VALUES, MAX, MIN, BMIN, BMAX, \
                          SMIN, SMAX, UMIN, UMAX, TYPE, SIZE, T, U, B
5

6 7 8 9 10 11 12 13
# Builtins is a data structure of the following:
#  builtins = {
#    '{data_type}': { # data type is any of [u]char, [u]short, [u]int, [u]long
#        '{builtin_function_name}': {
#            'arg_types': ['{data_type}', '{data_type}', ...],
#            'function_type': 'ttt'|'tss',
#                           ttt = all arguments are same-length vectors
#                           tss = all arguments are either same-length vectors,
14
#                                  or a vector followed by scalars
15 16 17 18 19 20 21 22 23 24 25 26 27
#            'values': [
#                [array of test output (arg0) values],
#                [array of arg1 values],
#                [array of arg2 values],
#                ...
#            ]
#        },
#        '{next_function}': {...},
#        ...
#    },
#    '{next_type}': {...},
#    ...
#  }
28
#
29 30
#  The builtins_generic, builtins_signed/unsigned are the same, but lack the
#  {datatype} layer
31 32 33 34 35 36 37

SIGNED_TYPES = ['char', 'short', 'int', 'long']
UNSIGNED_TYPES = ['uchar', 'ushort', 'uint', 'ulong']
DATA_TYPES = SIGNED_TYPES + UNSIGNED_TYPES


CLC_VERSION_MIN = {
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    'abs': 10,
    'abs_diff': 10,
    'add_sat': 10,
    'hadd': 10,
    'rhadd': 10,
    'clz': 10,
    'clamp': 11,
    'mad_hi': 10,
    'mad_sat': 10,
    'max': 11,  # max/min are only same-size in CL1.0, but TSS in CL1.1
    'min': 11,
    'mul_hi': 10,
    'rotate': 10,
    'sub_sat': 10,
    'upsample': 10,
    'mad24': 10,
    'mul24': 10
55 56
}

57

58 59 60 61 62
def abs(val):
    if (val < 0):
        return val*-1
    return val

63

64 65 66
def add(val1, val2):
    return val1+val2

67 68

# Given a data type, return the next bigger type of given signedness.
69 70 71
def big(type):
    return B[type]

72

73 74 75 76
def clz(type, val):
    if (val < 0):
        return 0
    else:
77 78 79
        # Count the number of times that we can right shift before value = 0
        # then subtract that from (data_size - 1)
        count = 0
80 81 82 83 84 85
        while(val > 0):
            if (val > 0):
                val = val >> 1
                count = count + 1
        return DATA_SIZES[type] - count

86

87
def div(val1, val2):
88
    return val1 // val2
89

90

91 92 93
def mad_hi(x, y, z, type):
    res = (x*y) >> DATA_SIZES[type]
    res = res + z
94
    while (res > MAX_VALUES[type]):  # Emulate overflow... Necessary?
95 96 97
        res = res - (2**DATA_SIZES[type])
    return res

98

99 100 101
def mul(val1, val2):
    return val1 * val2

102 103

def mul_hi(x, y, type):
104 105 106
    res = (x*y) >> DATA_SIZES[type]
    return res

107 108 109
# def pop(val,type):
#     # TODO: Calculate number of non-zero bits in value (CLC 1.2)
#     return 0
110

111 112

def pow(val, pow):
113 114
    return val ** pow

115

116
def rotate_right(x, n, bits):
117
    # Find all bits that will wrap
118
    mask = (1 << n) - 1
119 120
    wrapped_bits = x & mask

121 122 123 124
    # sign extension needs to be masked out
    se_mask = (1 << (bits - n)) - 1
    unwrapped_bits = x >> n
    unwrapped_bits &= se_mask
125 126

    return unwrapped_bits | (wrapped_bits << (bits - n))
127

128

129 130 131
def rotate_left(x, n, bits):
    return rotate_right(x, bits - n, bits)

132

133 134 135 136 137 138
def rot(x, n, bits):
    if (n < 0):
        return rotate_right(x, -1*n, bits)
    else:
        return rotate_left(x, n, bits)

139

140 141 142
def sub(val1, val2):
    return val1-val2

143 144

# Tests which don't depend on the signedness or bit-width of the inputs
145 146 147 148 149
generic_tests = {
    'abs': {
        'arg_types': [U, T],
        'function_type': 'ttt',
        'values': [
150 151
            [0, 2, [abs, MIN], [abs, MAX]],
            [0, 2,        MIN,        MAX]
152 153 154
        ]
    },
    'abs_diff': {
155 156 157 158 159 160 161
        'arg_types': [U, T, T],
        'function_type': 'ttt',
        'values': [
            [0, 1, 1, UMAX, UMAX],
            [0, 1, 0,  MIN,  MAX],
            [0, 0, 1,  MAX,  MIN]
        ]
162 163 164 165 166
    },
    'add_sat': {
        'arg_types': [T, T, T],
        'function_type': 'ttt',
        'values': [
167 168 169
            [MAX,           MAX],
            [MAX, [sub, MAX, 1]],
            [64,             50]
170 171 172 173 174 175 176
        ]
    },
    'hadd': {
        'arg_types': [T, T, T],
        'function_type': 'ttt',
        'values': [
            [32, 0, 10, MAX, MIN,
177 178
             [div, [add, MAX, MIN], 2],
             [div, [add, MIN, MAX], 2]],
179 180 181 182 183 184 185 186
            [32, 1, 12, MAX, MIN, MAX, MIN],
            [33, 0,  8, MAX, MIN, MIN, MAX]
        ]
    },
    'clz': {
        'arg_types': [T, T],
        'function_type': 'ttt',
        'values': [
187 188 189 190 191 192
            [[clz, TYPE,   1],
             [clz, TYPE,  64],
             [clz, TYPE,   0],
             [clz, TYPE, MAX],
             [clz, TYPE, MIN]],
            [1, 64, 0, MAX, MIN]]
193 194 195 196 197 198 199
    },
    'clamp': {
        'arg_types': [T, T, T, T],
        'function_type': 'tss',
        'values': [
            [64, [div, MIN, 2],  1],
            [92,           MIN, 64],
200
            [0,  [div, MIN, 2],  0],
201 202 203 204 205 206 207
            [64,             0,  1]
        ]
    },
    'mad_hi': {
        'arg_types': [T, T, T, T],
        'function_type': 'ttt',
        'values': [
208 209 210 211 212 213 214 215 216
            [[mad_hi, [div, MAX, 2],   3,   1, TYPE],
             [mad_hi,           MIN,   2,   2, TYPE], 4, 1,
             [mad_hi,           MAX, MAX, MAX, TYPE],
             [mad_hi,           MIN, MIN, MIN, TYPE],
             [mad_hi,           MIN, MAX, MAX, TYPE],
             [mad_hi,           MAX,   2,   2, TYPE]],
            [[div, MAX, 2], MIN, 12, MAX, MAX, MIN, MIN, MAX],
            [            3,   2,  4,   1, MAX, MIN, MAX, 2],
            [            1,   2,  4,   1, MAX, MIN, MAX, 2]
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
        ]
    },
    'mad_sat': {
        'arg_types': [T, T, T, T],
        'function_type': 'ttt',
        'values': [
            [52, MAX, 93, 0, MAX, MAX],
            [12, MAX, 92, 0, MAX, MAX],
            [ 4,   1,  1, 0,   2, MAX],
            [ 4,   1,  1, 0,   2, MAX]
        ]
    },
    'max': {
        'arg_types': [T, T, T],
        'function_type': 'tss',
        'values': [
            [92,   2, 12, MAX,   1, MAX, MIN, MAX, MAX, 0],
            [92,   2, 12, MAX, MIN, MAX, MIN, MIN, MAX, 0],
            [ 2, MIN,  4,   1,   1, MAX, MIN, MAX, MIN, 0]
        ]
    },
    'min': {
        'arg_types': [T, T, T],
        'function_type': 'tss',
        'values': [
            [ 2,  1, MIN,  4,   1, MIN, MAX, MIN, MIN, 0, MAX],
            [92, 64,   2, 12, MAX, MIN, MAX, MIN, MIN, 0, MAX],
            [ 2,  1, MIN,  4,   1,   1, MAX, MIN, MAX, 0, MAX]
        ]
    },
    'mul_hi': {
        'arg_types': [T, T, T],
        'function_type': 'ttt',
        'values': [
251 252 253 254 255
            [0, 0, 0,
             [mul_hi, MAX, MAX, TYPE],
             [mul_hi, MIN, MIN, TYPE], 0,
             [mul_hi, MAX,   2, TYPE],
             [div, MIN, 2]
256 257
            ],
            [0, 92, MAX, MAX, MIN, 92, MAX, MIN],
258
            [0,  2,   1, MAX, MIN,  1,   2, MAX]
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
        ]
    },
    'rhadd': {
        'arg_types': [T, T, T],
        'function_type': 'ttt',
        'values': [
            [33, 1, 10],
            [32, 1, 12],
            [33, 0,  8]
        ]
    },
    'rotate': {
        'arg_types': [T, T, T],
        'function_type': 'ttt',
        'values': [
274 275 276 277 278
            [1, 8, 1, 2, 1],
            [1, 1, 1, 1, 1],
            [0, 3, SIZE,
             [add, SIZE,  1],
             [mul, SIZE, 10]]
279 280 281 282 283 284
        ]
    },
    'sub_sat': {
        'arg_types': [T, T, T],
        'function_type': 'ttt',
        'values': [
285 286 287
            [1, 25, MAX,   0,   0, MIN, MIN],
            [1, 57, MAX, MAX, MIN, MIN, [div,           MIN, 2]],
            [0, 32, MIN, MAX, MIN, MAX, [add, [div, MAX, 2], 1]]
288 289 290
        ]
    },
    'upsample': {
291
        'arg_types': [B, T, U],
292 293
        'function_type': 'ttt',
        'values': [
294 295 296 297 298 299
            [[pow,              2, SIZE],
             [add, [pow, 2, SIZE],    1],
             BMAX,              0,  MAX,
             [add, [pow, 2, SIZE],    7]],
            [1, 1,  MAX, 0,   0, 1],
            [0, 1, UMAX, 0, MAX, 7]
300 301 302 303
        ]
    }
}

304
# Any test that conceivably includes a negative number as input/output
305 306 307 308 309
signed_generic_tests = {
    'abs': {
        'arg_types': [U, T],
        'function_type': 'ttt',
        'values': [
310 311
            [ 1,  13],
            [-1, -13]
312 313 314
        ]
    },
    'abs_diff': {
315 316 317 318 319 320 321
        'arg_types': [U, T, T],
        'function_type': 'ttt',
        'values': [
            [1, 15],
            [0, -8],
            [1,  7]
        ]
322 323 324 325 326
    },
    'add_sat': {
        'arg_types': [T, T, T],
        'function_type': 'ttt',
        'values': [
327 328 329
            [  0, -2, [sub, MAX, 63], MIN,            MIN],
            [ 32, -8,            MAX, MIN, [add, MIN, 10]],
            [-32,  6,            -63, -12,            -50]
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
        ]
    },
    'clamp': {
        'arg_types': [T, T, T, T],
        'function_type': 'tss',
        'values': [
            [ -64,  0],
            [-128, -1],
            [ -64,  0],
            [   0,  1]
        ]
    },
    'mad_hi': {
        'arg_types': [T, T, T, T],
        'function_type': 'ttt',
        'values': [
            [MIN, -2],
            [ -1,  1],
            [MIN, -1],
            [MIN, -1]
        ]
    },
    'mad_sat': {
        'arg_types': [T, T, T, T],
        'function_type': 'ttt',
        'values': [
356 357 358 359
            [  0, MIN, MIN, MAX, MIN, -2],
            [ -1, MIN, MIN, MIN, MIN,  1],
            [MIN,   2,   1, MIN, MAX, -1],
            [MIN,   2,  -1, MIN, MAX, -1]
360 361 362 363 364 365
        ]
    },
    'max': {
        'arg_types': [T, T, T],
        'function_type': 'tss',
        'values': [
366 367 368
            [ -1,  1],
            [ -1,  1],
            [MIN, -1]
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
        ]
    },
    'min': {
        'arg_types': [T, T, T],
        'function_type': 'tss',
        'values': [
            [MIN, -1, MIN],
            [ -1,  1, MIN],
            [MIN, -1,  -1]
        ]
    },
    'mul_hi': {
        'arg_types': [T, T, T],
        'function_type': 'ttt',
        'values': [
384
            [  0 , -1,  -1, -1],
385 386 387 388 389 390 391
            [ -1, MIN, MIN,  1],
            [MIN,   2,   1, -1]
        ]
    },
    'rhadd': {
        'arg_types': [T, T, T],
        'function_type': 'ttt',
392
        'values': [[-32], [-33], [-32]]
393 394 395 396 397
    },
    'rotate': {
        'arg_types': [T, T, T],
        'function_type': 'ttt',
        'values': [
398 399 400 401 402
            [MIN, MIN, [rot, 1, -3, SIZE], 1, MIN, [pow, 2, [sub, SIZE, 2]],
             MIN, [rot, -2, -1, SIZE]], [1, 1, 1, 1, 1, 1, 1, -2],
            [[sub, SIZE, 1], -1, -3, [mul, SIZE, -1],
             [mul, [add, SIZE, 1], -1],
             [mul, [add, SIZE, 2], -1], [sub, SIZE, 1], -1]
403 404 405 406 407 408
        ]
    },
    'sub_sat': {
        'arg_types': [T, T, T],
        'function_type': 'ttt',
        'values': [
409 410 411
            [MAX,          81],
            [[sub, MAX, 8], 1],
            [-32,         -80]
412 413 414
        ]
    },
    'upsample': {
415
        'arg_types': [B, T, U],
416 417
        'function_type': 'ttt',
        'values': [
418 419 420
            [  -1, [mul, [pow, 2, SIZE], -1]],
            [  -1,                       -1],
            [UMAX,                        0]
421 422 423 424 425
        ]
    }

}

426 427
# This list contains all numeric tests which never include negative integers
# that can't go into generic_tests.
428 429 430 431 432
unsigned_generic_tests = {
    'mad_sat': {
        'arg_types': [T, T, T, T],
        'function_type': 'ttt',
        'values': [
433
            [2,   MIN, MAX],
434
            [MIN, MIN, MIN],
435 436
            [2,   MIN, MAX],
            [2,   MIN, MAX]
437 438 439 440 441 442
        ]
    },
    'rotate': {
        'arg_types': [T, T, T],
        'function_type': 'ttt',
        'values': [
443 444
            [[div, [add, MAX, 1], 2], [div, [add, MAX, 1], 2]],
            [1, 1], [[sub, SIZE, 1], [sub, SIZE, 1]]
445 446 447 448
        ]
    },
}

449
# Hand-coded tests which are data type specific.
450 451
builtins = {
    'int': {
452
        'mad24': {
453 454 455
            'arg_types': [T, T, T, T],
            'function_type': 'ttt',
            'values': [
456 457 458 459
                [0, 2,  0, 520],
                [0, 1, -1,  32],
                [0, 1, -1,  16],
                [0, 1, -1,   8]
460 461
            ]
        },
462
        'mul24': {
463 464 465
            'arg_types': [T, T, T],
            'function_type': 'ttt',
            'values': [
466 467 468
                [0, 1,  1, 512, 4278190081],
                [0, 1, -1,  32,    2**23-1],
                [0, 1, -1,  16,    2**23-1]
469 470 471 472
            ]
        }
    },
    'uint': {
473
        'mad24': {
474 475 476 477 478 479 480 481 482
            'arg_types': [T, T, T, T],
            'function_type': 'ttt',
            'values': [
                [0, 2, 4278190080, 520],
                [0, 1,    2**24-1,  32],
                [0, 1,    2**24-1,  16],
                [0, 1,    2**24-1,   8]
            ]
        },
483
        'mul24': {
484 485 486
            'arg_types': [T, T, T],
            'function_type': 'ttt',
            'values': [
487 488 489
                [0, 1, 4261412865, 512],
                [0, 1,    2**24-1,  32],
                [0, 1,     2**24-1, 16]
490 491 492 493 494
            ]
        }
    }
}

495 496 497

# # # #  Define helper functions # # # #

498 499 500 501 502 503 504 505 506 507 508 509 510

def addTestValues(origDef, origValues):
    fnDef = dict(origDef)
    values = list(origValues)
    if (not 'values' in fnDef):
        fnDef['values'] = []
        for idx in range(0, len(values)):
            fnDef['values'].append(list(values[idx]))
    else:
        for arg in range(0, len(values)):
            fnDef['values'][arg] += values[arg]
    return fnDef

511 512 513

# Given a data type and function name, create one set of combined applicable
# test definitions.
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
def mergedTestDefinition(dataType, fnName):
    mergedTest = dict()

    testLists = [generic_tests]
    if (dataType in SIGNED_TYPES):
        testLists += [signed_generic_tests]
    if (dataType in UNSIGNED_TYPES):
        testLists += [unsigned_generic_tests]
    if (dataType in builtins):
        testLists += [builtins[dataType]]

    for testList in testLists:
        if (fnName in testList):
            fnDef = dict(testList[fnName])
            if (not 'arg_types' in mergedTest):
                mergedTest['arg_types'] = list(fnDef['arg_types'])
                mergedTest['function_type'] = fnDef['function_type']
            mergedTest = addTestValues(dict(mergedTest), list(fnDef['values']))
    return mergedTest

534

535 536 537 538 539 540 541 542 543
def getFnNames():
    fnNames = []
    fnNames += generic_tests.keys()
    fnNames += signed_generic_tests.keys()
    fnNames += unsigned_generic_tests.keys()
    for type in DATA_TYPES:
        if (type in builtins):
            fnNames += builtins[type].keys()

544
    # Get the sorted unique set of function names
545 546
    return sorted(list(set(fnNames)))

547

548
def main():
549
    dirName = os.path.join("cl", "builtin", "int")
550

551 552 553
    testDefs = {}
    functions = getFnNames()

554 555
    for dataType in DATA_TYPES:
        for fnName in functions:
556 557
            if (fnName is 'upsample' and
                    (dataType is 'long' or dataType is 'ulong')):
558
                continue
559
            # Merge all of the generic/signed/unsigned/custom test definitions
560
            testDefs[(dataType, fnName)] = mergedTestDefinition(dataType, fnName)
561

562
    gen(DATA_TYPES, CLC_VERSION_MIN, functions, testDefs, dirName)
563

564 565 566

if __name__ == '__main__':
    main()