gen_cl_int_builtins.py 14.9 KB
Newer Older
1
# coding=utf-8
2
from __future__ import print_function, division, absolute_import
3
import os
4 5
from genclbuiltins import gen, DATA_SIZES, MAX_VALUES, MAX, MIN, BMIN, BMAX, \
                          SMIN, SMAX, UMIN, UMAX, TYPE, SIZE, T, U, B
6

7 8 9 10 11 12 13 14
# 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,
15
#                                  or a vector followed by scalars
16 17 18 19 20 21 22 23 24 25 26 27 28
#            'values': [
#                [array of test output (arg0) values],
#                [array of arg1 values],
#                [array of arg2 values],
#                ...
#            ]
#        },
#        '{next_function}': {...},
#        ...
#    },
#    '{next_type}': {...},
#    ...
#  }
29
#
30 31
#  The builtins_generic, builtins_signed/unsigned are the same, but lack the
#  {datatype} layer
32 33 34 35 36 37 38

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


CLC_VERSION_MIN = {
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    '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,
Serge Martin's avatar
Serge Martin committed
54
    'popcount': 12,
55 56
    'mad24': 10,
    'mul24': 10
57 58
}

59

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

65

66 67 68
def add(val1, val2):
    return val1+val2

69 70

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

74

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

Serge Martin's avatar
Serge Martin committed
88 89
def popcount(val):
    return bin(val).count('1')
90

91
def div(val1, val2):
92
    return val1 // val2
93

94

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

102

103 104 105
def mul(val1, val2):
    return val1 * val2

106 107

def mul_hi(x, y, type):
108 109 110
    res = (x*y) >> DATA_SIZES[type]
    return res

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
        ]
Serge Martin's avatar
Serge Martin committed
301 302 303 304 305 306 307 308 309 310 311
    },
    'popcount': {
        'arg_types': [T, T],
        'function_type': 'ttt',
        'values': [
            [[popcount,   1],
             [popcount,   3],
             [popcount,   0],
             [popcount, MAX],
             [popcount, MIN]],
            [1, 3, 0, MAX, MIN]]
312 313 314
    }
}

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

}

437 438
# This list contains all numeric tests which never include negative integers
# that can't go into generic_tests.
439 440 441 442 443
unsigned_generic_tests = {
    'mad_sat': {
        'arg_types': [T, T, T, T],
        'function_type': 'ttt',
        'values': [
444
            [2,   MIN, MAX],
445
            [MIN, MIN, MIN],
446 447
            [2,   MIN, MAX],
            [2,   MIN, MAX]
448 449 450 451 452 453
        ]
    },
    'rotate': {
        'arg_types': [T, T, T],
        'function_type': 'ttt',
        'values': [
454 455
            [[div, [add, MAX, 1], 2], [div, [add, MAX, 1], 2]],
            [1, 1], [[sub, SIZE, 1], [sub, SIZE, 1]]
456 457 458 459
        ]
    },
}

460
# Hand-coded tests which are data type specific.
461 462
builtins = {
    'int': {
463
        'mad24': {
464 465 466
            'arg_types': [T, T, T, T],
            'function_type': 'ttt',
            'values': [
467 468 469 470
                [0, 2,  0, 520],
                [0, 1, -1,  32],
                [0, 1, -1,  16],
                [0, 1, -1,   8]
471 472
            ]
        },
473
        'mul24': {
474 475 476
            'arg_types': [T, T, T],
            'function_type': 'ttt',
            'values': [
477 478 479
                [0, 1,  1, 512, 4278190081],
                [0, 1, -1,  32,    2**23-1],
                [0, 1, -1,  16,    2**23-1]
480 481 482 483
            ]
        }
    },
    'uint': {
484
        'mad24': {
485 486 487 488 489 490 491 492 493
            '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]
            ]
        },
494
        'mul24': {
495 496 497
            'arg_types': [T, T, T],
            'function_type': 'ttt',
            'values': [
498 499 500
                [0, 1, 4261412865, 512],
                [0, 1,    2**24-1,  32],
                [0, 1,     2**24-1, 16]
501 502 503 504 505
            ]
        }
    }
}

506 507 508

# # # #  Define helper functions # # # #

509 510 511 512 513 514 515 516 517 518 519 520 521

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

522 523 524

# Given a data type and function name, create one set of combined applicable
# test definitions.
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
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

545

546 547 548 549 550 551 552 553 554
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()

555
    # Get the sorted unique set of function names
556 557
    return sorted(list(set(fnNames)))

558

559
def main():
560
    dirName = os.path.join("cl", "builtin", "int")
561

562 563 564
    testDefs = {}
    functions = getFnNames()

565 566
    for dataType in DATA_TYPES:
        for fnName in functions:
567 568
            if (fnName is 'upsample' and
                    (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)
572

573
    gen(DATA_TYPES, CLC_VERSION_MIN, functions, testDefs, dirName)
574

575 576 577

if __name__ == '__main__':
    main()