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

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

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


CLC_VERSION_MIN = {
37 38 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,
    'mad24': 10,
    'mul24': 10
54 55
}

56

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

62

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

66 67

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

71

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

85

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

89

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

97

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

101 102

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

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

110 111

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

114

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

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

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

127

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

131

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

138

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

142 143

# Tests which don't depend on the signedness or bit-width of the inputs
144 145 146 147 148
generic_tests = {
    'abs': {
        'arg_types': [U, T],
        'function_type': 'ttt',
        'values': [
149 150
            [0, 2, [abs, MIN], [abs, MAX]],
            [0, 2,        MIN,        MAX]
151 152 153
        ]
    },
    'abs_diff': {
154 155 156 157 158 159 160
        'arg_types': [U, T, T],
        'function_type': 'ttt',
        'values': [
            [0, 1, 1, UMAX, UMAX],
            [0, 1, 0,  MIN,  MAX],
            [0, 0, 1,  MAX,  MIN]
        ]
161 162 163 164 165
    },
    'add_sat': {
        'arg_types': [T, T, T],
        'function_type': 'ttt',
        'values': [
166 167 168
            [MAX,           MAX],
            [MAX, [sub, MAX, 1]],
            [64,             50]
169 170 171 172 173 174 175
        ]
    },
    'hadd': {
        'arg_types': [T, T, T],
        'function_type': 'ttt',
        'values': [
            [32, 0, 10, MAX, MIN,
176 177
             [div, [add, MAX, MIN], 2],
             [div, [add, MIN, MAX], 2]],
178 179 180 181 182 183 184 185
            [32, 1, 12, MAX, MIN, MAX, MIN],
            [33, 0,  8, MAX, MIN, MIN, MAX]
        ]
    },
    'clz': {
        'arg_types': [T, T],
        'function_type': 'ttt',
        'values': [
186 187 188 189 190 191
            [[clz, TYPE,   1],
             [clz, TYPE,  64],
             [clz, TYPE,   0],
             [clz, TYPE, MAX],
             [clz, TYPE, MIN]],
            [1, 64, 0, MAX, MIN]]
192 193 194 195 196 197 198
    },
    'clamp': {
        'arg_types': [T, T, T, T],
        'function_type': 'tss',
        'values': [
            [64, [div, MIN, 2],  1],
            [92,           MIN, 64],
199
            [0,  [div, MIN, 2],  0],
200 201 202 203 204 205 206
            [64,             0,  1]
        ]
    },
    'mad_hi': {
        'arg_types': [T, T, T, T],
        'function_type': 'ttt',
        'values': [
207 208 209 210 211 212 213 214 215
            [[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]
216 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
        ]
    },
    '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': [
250 251 252 253 254
            [0, 0, 0,
             [mul_hi, MAX, MAX, TYPE],
             [mul_hi, MIN, MIN, TYPE], 0,
             [mul_hi, MAX,   2, TYPE],
             [div, MIN, 2]
255 256
            ],
            [0, 92, MAX, MAX, MIN, 92, MAX, MIN],
257
            [0,  2,   1, MAX, MIN,  1,   2, MAX]
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
        ]
    },
    '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': [
273 274 275 276 277
            [1, 8, 1, 2, 1],
            [1, 1, 1, 1, 1],
            [0, 3, SIZE,
             [add, SIZE,  1],
             [mul, SIZE, 10]]
278 279 280 281 282 283
        ]
    },
    'sub_sat': {
        'arg_types': [T, T, T],
        'function_type': 'ttt',
        'values': [
284 285 286
            [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]]
287 288 289
        ]
    },
    'upsample': {
290
        'arg_types': [B, T, U],
291 292
        'function_type': 'ttt',
        'values': [
293 294 295 296 297 298
            [[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]
299 300 301 302
        ]
    }
}

303
# Any test that conceivably includes a negative number as input/output
304 305 306 307 308
signed_generic_tests = {
    'abs': {
        'arg_types': [U, T],
        'function_type': 'ttt',
        'values': [
309 310
            [ 1,  13],
            [-1, -13]
311 312 313
        ]
    },
    'abs_diff': {
314 315 316 317 318 319 320
        'arg_types': [U, T, T],
        'function_type': 'ttt',
        'values': [
            [1, 15],
            [0, -8],
            [1,  7]
        ]
321 322 323 324 325
    },
    'add_sat': {
        'arg_types': [T, T, T],
        'function_type': 'ttt',
        'values': [
326 327 328
            [  0, -2, [sub, MAX, 63], MIN,            MIN],
            [ 32, -8,            MAX, MIN, [add, MIN, 10]],
            [-32,  6,            -63, -12,            -50]
329 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
        ]
    },
    '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': [
355 356 357 358
            [  0, MIN, MIN, MAX, MIN, -2],
            [ -1, MIN, MIN, MIN, MIN,  1],
            [MIN,   2,   1, MIN, MAX, -1],
            [MIN,   2,  -1, MIN, MAX, -1]
359 360 361 362 363 364
        ]
    },
    'max': {
        'arg_types': [T, T, T],
        'function_type': 'tss',
        'values': [
365 366 367
            [ -1,  1],
            [ -1,  1],
            [MIN, -1]
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
        ]
    },
    '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': [
383
            [  0 , -1,  -1, -1],
384 385 386 387 388 389 390
            [ -1, MIN, MIN,  1],
            [MIN,   2,   1, -1]
        ]
    },
    'rhadd': {
        'arg_types': [T, T, T],
        'function_type': 'ttt',
391
        'values': [[-32], [-33], [-32]]
392 393 394 395 396
    },
    'rotate': {
        'arg_types': [T, T, T],
        'function_type': 'ttt',
        'values': [
397 398 399 400 401
            [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]
402 403 404 405 406 407
        ]
    },
    'sub_sat': {
        'arg_types': [T, T, T],
        'function_type': 'ttt',
        'values': [
408 409 410
            [MAX,          81],
            [[sub, MAX, 8], 1],
            [-32,         -80]
411 412 413
        ]
    },
    'upsample': {
414
        'arg_types': [B, T, U],
415 416
        'function_type': 'ttt',
        'values': [
417 418 419
            [  -1, [mul, [pow, 2, SIZE], -1]],
            [  -1,                       -1],
            [UMAX,                        0]
420 421 422 423 424
        ]
    }

}

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

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

494 495 496

# # # #  Define helper functions # # # #

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

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

510 511 512

# Given a data type and function name, create one set of combined applicable
# test definitions.
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
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

533

534 535 536 537 538 539 540 541 542
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()

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

546

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

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

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

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

563 564 565

if __name__ == '__main__':
    main()