1# Copyright © 2024 Intel Corporation 2# SPDX-License-Identifier: MIT 3 4import argparse 5import sys 6from math import pi 7 8a = 'a' 9b = 'b' 10 11lower_fsign = [ 12 # The true branch of the fcsel elides ±1*X, so the pattern must be 13 # conditioned for that using is_only_used_as_float (see 14 # nir_opt_algebraic.py). The false branch elides 0*x, so the pattern must 15 # also be conditioned for that using either nsz,nnan or nsz with 16 # is_finite. 17 # 18 # NOTE: fcsel opcodes are currently limited to float32 in NIR. 19 (('fmul@32(is_only_used_as_float)', ('fsign(is_used_once)', 'a(is_not_negative)'), b), ('fcsel_gt', a , b , ('fmul', b, 0.0 ))), 20 (('~fmul@32', ('fsign(is_used_once)', 'a(is_not_negative)'), b), ('fcsel_gt', a , b , 0.0 )), 21 (('fmul@32(is_only_used_as_float)', ('fsign(is_used_once)', 'a(is_not_positive)'), b), ('fcsel_gt', ('fneg', a), ('fneg', b), ('fmul', b, 0x80000000))), 22 (('~fmul@32', ('fsign(is_used_once)', 'a(is_not_positive)'), b), ('fcsel_gt', ('fneg', a), ('fneg', b), 0x80000000 )), 23 24 (('fmul@16(is_only_used_as_float)', ('fsign(is_used_once)', 'a(is_not_negative)'), b), ('bcsel', ('!flt', 0, a ), b , ('fmul', b, 0.0 ))), 25 (('~fmul@16', ('fsign(is_used_once)', 'a(is_not_negative)'), b), ('bcsel', ('!flt', 0, a ), b , 0.0 )), 26 (('fmul@16(is_only_used_as_float)', ('fsign(is_used_once)', 'a(is_not_positive)'), b), ('bcsel', ('!flt', 0, ('fneg', a)), ('fneg', b), ('fmul', b, 0x8000))), 27 (('~fmul@16', ('fsign(is_used_once)', 'a(is_not_positive)'), b), ('bcsel', ('!flt', 0, ('fneg', a)), ('fneg', b), 0x8000 )), 28 29 (('fmul@32(is_only_used_as_float,nsz)', ('fsign(is_used_once)', a), 'b(is_finite)'), ('fcsel_gt', a, b, ('fcsel_gt', ('fneg', a), ('fneg', b), 0.0))), 30 (('fmul@32(is_only_used_as_float,nsz,nnan)', ('fsign(is_used_once)', a), b ), ('fcsel_gt', a, b, ('fcsel_gt', ('fneg', a), ('fneg', b), 0.0))), 31 (('~fmul@32', ('fsign(is_used_once)', a), b ), ('fcsel_gt', a, b, ('fcsel_gt', ('fneg', a), ('fneg', b), 0.0))), 32 33 # This is 99.99% strictly correct for OpenCL. It will provide correctly 34 # signed zero for ±0 inputs, and it will provide zero for NaN inputs. The 35 # only slight deviation is that it can provide -0 for some NaN inputs. 36 (('fsign@32', a), ('fcsel_gt', ('fabs', a) , ('ior', ('iand', a, 0x80000000), 0x3f800000), ('iand', a, 0x80000000))), 37 (('fsign@16', a), ('bcsel', ('!flt', 0, ('fabs', a)), ('ior', ('iand', a, 0x8000 ), 0x3c00 ), ('iand', a, 0x8000 ))), 38 39 # The only effect a*0.0 should have is when 'a' is infinity, -0.0 or NaN 40 (('fmul(nsz,nnan)', 'a', 0.0), 0.0), 41 (('fmul(nsz)', 'a(is_finite)', 0.0), 0.0), 42 (('fmul(nsz,nnan)', 'a@32', 0x80000000), 0.0), 43 (('fmul(nsz,nnan)', 'a@16', 0x8000 ), 0.0), 44] 45 46def main(): 47 parser = argparse.ArgumentParser() 48 parser.add_argument('-p', '--import-path', required=True) 49 args = parser.parse_args() 50 sys.path.insert(0, args.import_path) 51 run() 52 53 54def run(): 55 import nir_algebraic # pylint: disable=import-error 56 57 print('#include "brw_nir.h"') 58 59 print(nir_algebraic.AlgebraicPass("brw_nir_lower_fsign", lower_fsign).render()) 60 61 62if __name__ == '__main__': 63 main() 64