gen_cl_math_builtins.py 13.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# Copyright 2013 Advanced Micro Devices, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice (including the next
# paragraph) shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Authors: Tom Stellard <thomas.stellard@amd.com>
Aaron Watry's avatar
Aaron Watry committed
23
#          Aaron Watry  <awatry@gmail.com>
24 25
#

26
from __future__ import print_function
27 28
import os

Jan Vesely's avatar
Jan Vesely committed
29
from genclbuiltins import gen, NEGNAN
Meng, Mengmeng's avatar
Meng, Mengmeng committed
30
from math import acos, acosh, asin, asinh, atan, atan2, atanh, cos, cosh, exp
Jan Vesely's avatar
Jan Vesely committed
31
from math import fabs, fmod, log10, log1p, pi, pow, sin, sinh, sqrt, tan, tanh
32 33

CLC_VERSION_MIN = {
Aaron Watry's avatar
Aaron Watry committed
34
    'acos' : 10,
Aaron Watry's avatar
Aaron Watry committed
35
    'acosh' : 10,
Aaron Watry's avatar
Aaron Watry committed
36
    'asin' : 10,
Aaron Watry's avatar
Aaron Watry committed
37
    'asinh' : 10,
38
    'atan' : 10,
Aaron Watry's avatar
Aaron Watry committed
39
    'atan2' : 10,
Aaron Watry's avatar
Aaron Watry committed
40
    'atanh' : 10,
Zhu Bingbing's avatar
Zhu Bingbing committed
41
    'atanpi': 10,
Aaron Watry's avatar
Aaron Watry committed
42
    'cbrt' : 10,
43
    'ceil' : 10,
Jan Vesely's avatar
Jan Vesely committed
44
    'copysign' : 10,
Tom Stellard's avatar
Tom Stellard committed
45
    'cos' : 10,
Aaron Watry's avatar
Aaron Watry committed
46
    'cosh' : 10,
Aaron Watry's avatar
Aaron Watry committed
47
    'cospi' : 10,
Aaron Watry's avatar
Aaron Watry committed
48
    'erf' : 10,
Aaron Watry's avatar
Aaron Watry committed
49
    'erfc' : 10,
Meng, Mengmeng's avatar
Meng, Mengmeng committed
50
    'exp' : 10,
Aaron Watry's avatar
Aaron Watry committed
51
    'fabs' : 10,
52
    'floor' : 10,
Aaron Watry's avatar
Aaron Watry committed
53
    'fmod' : 10,
Aaron Watry's avatar
Aaron Watry committed
54
    'ldexp' : 10,
Jan Vesely's avatar
Jan Vesely committed
55
    'log10' : 10,
Aaron Watry's avatar
Aaron Watry committed
56
    'log1p' : 10,
Aaron Watry's avatar
Aaron Watry committed
57
    'mix' : 10,
58
    'nextafter' : 10,
59
    'round' : 10,
Tom Stellard's avatar
Tom Stellard committed
60
    'sign' : 10,
61
    'sin' : 10,
Aaron Watry's avatar
Aaron Watry committed
62
    'sinh' : 10,
Aaron Watry's avatar
Aaron Watry committed
63
    'sinpi' : 10,
Aaron Watry's avatar
Aaron Watry committed
64
    'tan' : 10,
Aaron Watry's avatar
Aaron Watry committed
65
    'tanh' : 10,
Aaron Watry's avatar
Aaron Watry committed
66
    'sqrt' : 10,
67
    'trunc' : 10
68 69 70 71 72 73 74 75
}

DATA_TYPES = ['float']

F = {
    'float' : 'float'
}

Aaron Watry's avatar
Aaron Watry committed
76 77 78 79
I = {
    'float' : 'int'
}

80
tests = {
Aaron Watry's avatar
Aaron Watry committed
81 82 83 84 85 86 87 88 89
    'acos' : {
        'arg_types' : [F, F],
        'function_type': 'ttt',
        'values' : [
            [ pi,  pi/2, 0.0, acos(0.12345), float("nan")], # Result
            [-1.0, 0.0,  1.0,      0.12345,  float("nan")]  # Arg0
        ],
        'tolerance' : 4
     },
Aaron Watry's avatar
Aaron Watry committed
90 91 92 93 94 95 96 97 98
    'acosh' : {
        'arg_types' : [F, F],
        'function_type': 'ttt',
        'values' : [
            [0.0, acosh(1.12345), float("nan"), acosh(123456789.01234)], #Result
            [1.0,       1.12345,  float("nan"),       123456789.01234 ]  #Arg0
        ],
        'tolerance' : 4
     },
Aaron Watry's avatar
Aaron Watry committed
99 100 101 102 103 104 105 106 107
    'asin' : {
        'arg_types' : [F, F],
        'function_type': 'ttt',
        'values' : [
            [-pi/2, 0.0, pi/2, asin(0.12345), float("nan")], # Result
            [-1.0,  0.0,  1.0,      0.12345,  float("nan")]  # Arg0
        ],
        'tolerance' : 4
     },
Aaron Watry's avatar
Aaron Watry committed
108 109 110 111 112 113 114 115 116
    'asinh' : {
        'arg_types' : [F, F],
        'function_type': 'ttt',
        'values' : [
            [0.0, asinh(1.0), asinh(-1.12345), float("nan"), asinh(123456789.01234)], #Result
            [0.0, 1.0,              -1.12345,  float("nan"),       123456789.01234 ]  #Arg0
        ],
        'tolerance' : 4
     },
117 118 119 120 121 122 123 124 125
    'atan' : {
        'arg_types' : [F, F],
        'function_type': 'ttt',
        'values' : [
            [atan(0.0), atan(0.12345), atan(3567147.0)], # Result
            [0.0,       0.12345,       3567147.0]# Arg0
        ],
        'tolerance' : 2
     },
Aaron Watry's avatar
Aaron Watry committed
126 127 128 129 130 131 132 133 134 135
    'atan2' : {
        'arg_types' : [F, F, F],
        'function_type': 'ttt',
        'values' : [
            [atan2(0.0, 0.0), atan2(1.2345, 10.0), atan2(35671470.0, 0.1)], # Result
            [0.0,             1.2345,              35671470.0            ], # Arg0
            [0.0,             10.0,                0.1                   ]  # Arg1
        ],
        'tolerance' : 6
     },
Aaron Watry's avatar
Aaron Watry committed
136 137 138 139 140 141 142 143 144
    'atanh' : {
        'arg_types' : [F, F],
        'function_type': 'ttt',
        'values' : [
            [0.0, float("inf"), float("-inf"), float("nan"), atanh(0.123456789)], #Result
            [0.0, 1.0,              -1.0,      float("nan"),       0.123456789 ]  #Arg0
        ],
        'tolerance' : 4
     },
Zhu Bingbing's avatar
Zhu Bingbing committed
145 146 147 148 149 150 151 152 153
    'atanpi' : {
    'arg_types' : [F, F],
    'function_type': 'ttt',
    'values' : [
        [0.0, -0.0, atan(1.02345)/pi, atan(-1.02345)/pi, float("nan"), 0.5,          -0.5          ],
        [0.0, -0.0, 1.02345,          -1.02345,          float("nan"), float("inf"), float("-inf") ]
    ],
    'tolerance' : 5
 },
Aaron Watry's avatar
Aaron Watry committed
154 155 156 157 158 159 160 161 162
    'cbrt' : {
        'arg_types': [F, F],
        'function_type': 'ttt',
        'values': [
            [3.0,  -1.0, float("nan"), float("inf"), 0.123456789**(1/3.0) ],
            [27.0, -1.0, float("nan"), float("inf"), 0.123456789 ]
        ],
        'tolerance' : 2
    },
163 164 165 166 167 168 169 170
    'ceil' : {
        'arg_types': [F, F],
        'function_type': 'ttt',
        'values': [
            [1.0,  0.0, 0.0, -0.0, float("nan"), -3.0],
            [0.5, -0.5, 0.0, -0.0, float("nan"), -3.99]
        ]
    },
Jan Vesely's avatar
Jan Vesely committed
171 172 173 174 175 176 177 178 179
    'copysign' : {
        'arg_types': [F, F, F],
        'function_type': 'ttt',
        'values': [
            [0.0, -0.0, 1.0, -1.0, float("nan"), float("nan"), NEGNAN,       float("-inf"), float("inf") ], # Result
            [0.0,  0.0, 1.0, -1.0, float("nan"), -4.0,         float("nan"), float("inf"),  float("-inf") ], # Arg0
            [1.0, -1.0, 2.0, -2.0, float("nan"), float("nan"), -4.0,         -3.0,          float("inf") ], # Arg1
        ]
    },
Tom Stellard's avatar
Tom Stellard committed
180 181 182 183
    'cos' : {
        'arg_types' : [F, F],
        'function_type': 'ttt',
        'values' : [
Jan Vesely's avatar
Jan Vesely committed
184 185
            [1.0, 0.0,    -1.0, 0.0,        1.0,    cos(1.12345), cos(7), cos(8), cos(pow(2,20)), cos(pow(2,24)), cos(pow(2,120)), float("nan")], # Result
            [0.0, pi / 2, pi,   3 * pi / 2, 2 * pi, 1.12345, 7, 8, pow(2,20), pow(2,24), pow(2,120), float("nan")] # Arg0
Tom Stellard's avatar
Tom Stellard committed
186 187 188
        ],
        'tolerance' : 2
    },
Aaron Watry's avatar
Aaron Watry committed
189 190 191 192 193 194 195 196 197
    'cosh' : {
        'arg_types' : [F, F],
        'function_type': 'ttt',
        'values' : [
            [1.0, cosh(0.123456789), float("inf"), float("inf"),  float("nan")],# Result
            [0.0, 0.123456789,       float("inf"), float("-inf"), float("nan")] # Arg0
        ],
        'tolerance' : 4
    },
Aaron Watry's avatar
Aaron Watry committed
198 199 200 201
    'cospi' : {
        'arg_types' : [F, F],
        'function_type': 'ttt',
        'values' : [
Aaron Watry's avatar
Aaron Watry committed
202 203
            [1.0, cos(pi*pi/2), cos(pi*3*pi/2), cos(2*pi*pi), cos(pi*1.12345), cos(pi*pow(2,20)), cos(pi*pow(2,24)), cos(pi*pow(2,120)), float("nan")], # Result
            [0.0, pi / 2,       3 * pi / 2,     2 * pi,       1.12345        , pow(2,20),         pow(2,24),         pow(2,120),         float("nan")]  # Arg0
Aaron Watry's avatar
Aaron Watry committed
204 205 206
        ],
        'tolerance' : 4
    },
Aaron Watry's avatar
Aaron Watry committed
207 208 209 210 211 212 213 214 215
    'erf' : {
        'arg_types' : [F, F],
        'function_type': 'ttt',
        'values' : [
            [0.0, 0.950004,        0.990005,        -0.994999475,  0.27219439321703e-29, 1, -1], # Result
            [0.0, 1.960/sqrt(2.0), 2.576/sqrt(2.0), -2.807/sqrt(2.0), 11.1, float("inf"), float("-inf")]  # Arg0
        ],
        'tolerance' : 16
    },
Aaron Watry's avatar
Aaron Watry committed
216 217 218 219 220 221 222 223 224
    'erfc' : {
        'arg_types' : [F, F],
        'function_type': 'ttt',
        'values' : [
            [1.0, 0.95, 0.009995, 1.994999, 0.0, 0.0, 2.0], # Result
            [0.0, 1.960/sqrt(2.0), 2.576/sqrt(2.0), -2.807/sqrt(2.0), 11.1, float("inf"), float("-inf")]  # Arg0
        ],
        'tolerance' : 16
    },
Meng, Mengmeng's avatar
Meng, Mengmeng committed
225 226 227 228 229 230 231 232 233
    'exp' : {
        'arg_types' : [F, F],
        'function_type': 'ttt',
        'values' : [
            [1.0, exp(0.95), exp(pi), exp(-pi), float("inf") ], # Result
            [0.0, 0.95, pi, -pi, float("inf")]  # Arg0
        ],
        'tolerance' : 3
    },
Aaron Watry's avatar
Aaron Watry committed
234 235 236 237 238 239 240 241 242
    'fabs' : {
        'arg_types' : [F, F],
        'function_type': 'ttt',
        'values' : [
            [0.0, pi/2,  pi,  0.0, float("inf"),  float("inf"), 1.12345 ], # Result
            [0.0, -pi/2, pi, -0.0, float("-inf"), float("inf"), -1.12345] # Arg0
        ],
        'tolerance' : 0
    },
243 244 245 246 247 248 249 250
    'floor' : {
        'arg_types': [F, F],
        'function_type': 'ttt',
        'values': [
            [0.0, -1.0, 0.0, -0.0, float("nan"), -4.0,  1.0],
            [0.5, -0.5, 0.0, -0.0, float("nan"), -3.99, 1.5]
        ]
    },
Aaron Watry's avatar
Aaron Watry committed
251 252 253 254 255 256 257 258 259 260
    'fmod' : {
        'arg_types': [F, F, F],
        'function_type': 'ttt',
        'values': [
            [float.fromhex("0x1.99998p-4"),  float("nan"), float.fromhex("-0x1.47aep-7"),   1.0, float("-nan"), float("nan")],
            [float.fromhex("0x1.466666p+2"), 0.0,          float.fromhex("-0x1p+2"),        1.0, 5.1,           3.0         ],
            [float.fromhex("0x1p-1"),        float("nan"), float.fromhex("-0x1.feb852p+1"), 1.5, 0.0,           float("inf")]
        ],
        'tolerance' : 0
    },
Aaron Watry's avatar
Aaron Watry committed
261 262 263 264 265 266 267 268 269 270
    'ldexp' : {
        'arg_types': [F, F, I],
        'function_type': 'tss',
        'values': [
            [0.0, 4.0, 15.2, 1.75, float("nan"), float("inf")],
            [0.0, 1.0, 0.95, 3.5,  float("nan"), 1.12312312],
            [0,   2,   4,    -1,   1,            2031231231]
        ],
        'tolerance' : 0
    },
Jan Vesely's avatar
Jan Vesely committed
271 272 273 274 275 276 277 278 279
    'log10' : {
        'arg_types': [F, F],
        'function_type': 'ttt',
        'values': [
            [log10(0.5), float("-inf"), log10(1.e-15), float("nan")],
            [0.5,        0.0,           1.e-15,        float("nan")]
        ],
        'tolerance' : 3
    },
Aaron Watry's avatar
Aaron Watry committed
280 281 282 283 284 285 286 287 288
    'log1p' : {
        'arg_types': [F, F],
        'function_type': 'ttt',
        'values': [
            [log1p(0.5), float("-inf"), log1p(1.e-15), float("nan")],
            [0.5,        -1.0,          1.e-15,        float("nan")]
        ],
        'tolerance' : 2
    },
Aaron Watry's avatar
Aaron Watry committed
289 290 291 292 293 294 295 296 297 298
    'mix' : { #x + (y - x) * a
        'arg_types': [F, F, F, F],
        'function_type': 'tts',
        'values': [
            [float("nan"), float("nan"), 1.0, 3.0, 10.0  ], # Result
            [1.0         , 1.0,          1.0, 4.0, 15.0 ], # Arg0
            [2.0         , float("nan"), 2.0, 2.0, 10.0 ], # Arg1
            [float("nan"), 0.0,          0.0, 0.5, 1.0  ], # Arg2
        ]
    },
299 300 301 302 303 304 305 306
    'nextafter' : {
        'arg_types': [F, F, F],
        'function_type': 'ttt',
        'values': [
            [1.401298e-45, -1.401298e-45, 1.00000011920928955078125, 0.999999940395355224609375, float("nan"), float("nan"), 5.0 ], # Result
            [0.0,           0.0         , 1.0, 1.0, float("nan"), 2.5, 5.0], # Arg0
            [1.0,          -1.0         , 2.0, 0.0, 3.4, float("nan"), 5.0], # Arg1
        ]
307
    },
308 309 310 311 312 313 314 315
    'round' : {
        'arg_types': [F, F],
        'function_type': 'ttt',
        'values': [
            [1.0, -1.0, 0.0, -0.0, float("nan"), -4.0,  2.0, 0.0, 1.0],
            [0.5, -0.5, 0.0, -0.0, float("nan"), -3.99, 1.5, 0.4, 0.6]
        ]
    },
316 317 318 319 320 321 322 323
    'sign' : { # This is really a Common function but it uses the same types
               # as a lot of the math functions.
        'arg_types': [F, F],
        'function_type': 'ttt',
        'values': [
            [1.0, -1.0, 0.0, -0.0, 0.0],
            [0.5, -0.5, 0.0, -0.0, float("nan")]
        ]
Tom Stellard's avatar
Tom Stellard committed
324 325 326 327 328
    },
    'sin' : {
        'arg_types' : [F, F],
        'function_type': 'ttt',
        'values' : [
Jan Vesely's avatar
Jan Vesely committed
329 330
            [0.0, 1.0,   0.0, -1.0,       0.0,    sin(2.234567), sin(7), sin(8), sin(pow(2,20)), sin(pow(2,24)), sin(pow(2,120)), float("nan")], # Result
            [0.0, pi / 2, pi, 3 * pi / 2, 2 * pi, 2.234567, 7, 8, pow(2,20), pow(2,24), pow(2,120), float("nan")] # Arg0
Tom Stellard's avatar
Tom Stellard committed
331 332
        ],
        'tolerance': 2
333
    },
Aaron Watry's avatar
Aaron Watry committed
334 335 336 337 338 339 340 341 342
    'sinh' : {
        'arg_types' : [F, F],
        'function_type': 'ttt',
        'values' : [
            [0.0, sinh(0.123456789), float("inf"), float("-inf"), float("nan")],# Result
            [0.0, 0.123456789,       float("inf"), float("-inf"), float("nan")] # Arg0
        ],
        'tolerance': 4
    },
Aaron Watry's avatar
Aaron Watry committed
343 344 345 346
    'sinpi' : {
        'arg_types' : [F, F],
        'function_type': 'ttt',
        'values' : [
Aaron Watry's avatar
Aaron Watry committed
347 348 349 350 351 352
            [0.0, 0.0, sin(pi*pi/2), sin(pi*3*pi/2), sin(2*pi*pi),
             sin(pi*1.12345), sin(pi*7), sin(pi*8), sin(pi*pow(2,20)),
             sin(pi*pow(2,24)), sin(pi*pow(2,120)), float("nan")],#Result
            [0.0, 1.0, pi / 2,       3 * pi / 2,     2 * pi,
             1.12345        , 7,         8,         pow(2,20),
             pow(2,24),         pow(2,120),         float("nan")] #Arg0
Aaron Watry's avatar
Aaron Watry committed
353 354 355
        ],
        'tolerance' : 4
    },
Aaron Watry's avatar
Aaron Watry committed
356 357 358 359 360 361 362 363 364
    'sqrt' : {
        'arg_types': [F, F],
        'function_type': 'ttt',
        'values': [
            [1.0, 2.0,  6.0, 2.5 , float("nan"), 4.0,  sqrt(0.0), sqrt(7.0), sqrt(pi)], # Result
            [1.0, 4.0, 36.0, 6.25, float("nan"), 16.0, 0.0, 7.0, pi], # Arg1
        ],
        'tolerance': 3
    },
Aaron Watry's avatar
Aaron Watry committed
365 366 367 368 369 370 371 372 373
    'tan' : {
        'arg_types': [F, F],
        'function_type': 'ttt',
        'values': [
            [0.0, 1.0,  0.0, sqrt(3), -1.0,   tan(2.234567), float("nan") ], # Result
            [0.0, pi/4, pi,  pi/3,    3*pi/4, 2.234567 ,     float("nan") ], # Arg1
        ],
        'tolerance': 5
    },
Aaron Watry's avatar
Aaron Watry committed
374 375 376 377 378 379 380 381 382
    'tanh' : {
        'arg_types' : [F, F],
        'function_type': 'ttt',
        'values' : [
            [0.0, tanh(0.123456789), tanh(15.123456789), 1.0,          -1.0         , float("nan")],# Result
            [0.0, 0.123456789,       15.123456789,       float("inf"), float("-inf"), float("nan")] # Arg0
        ],
        'tolerance': 5
    },
383 384 385 386 387 388 389
    'trunc' : {
        'arg_types': [F, F],
        'function_type': 'ttt',
        'values': [
            [0.0, -0.0, 0.0, -0.0, float("nan"), -3.0,  1.0],
            [0.5, -0.5, 0.0, -0.0, float("nan"), -3.99, 1.5]
        ]
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
    }
}


def main():
    dirName = os.path.join("cl", "builtin", "math")

    testDefs = {}
    functions = sorted(tests.keys())
    for dataType in DATA_TYPES:
        for fnName in functions:
            testDefs[(dataType, fnName)] = tests[fnName]

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


406 407
if __name__ == '__main__':
    main()