1 # 2 # Copyright (C) 2014 Connor Abbott 3 # 4 # Permission is hereby granted, free of charge, to any person obtaining a 5 # copy of this software and associated documentation files (the "Software"), 6 # to deal in the Software without restriction, including without limitation 7 # the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 # and/or sell copies of the Software, and to permit persons to whom the 9 # Software is furnished to do so, subject to the following conditions: 10 # 11 # The above copyright notice and this permission notice (including the next 12 # paragraph) shall be included in all copies or substantial portions of the 13 # Software. 14 # 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 # IN THE SOFTWARE. 22 # 23 # Authors: 24 # Connor Abbott (cwabbott0@gmail.com) 25 26 from nir_opcodes import opcodes, type_sizes 27 from mako.template import Template 28 29 template = Template(""" 30 #include "nir.h" 31 32 nir_op 33 nir_type_conversion_op(nir_alu_type src, nir_alu_type dst, nir_rounding_mode rnd) 34 { 35 nir_alu_type src_base = (nir_alu_type) nir_alu_type_get_base_type(src); 36 nir_alu_type dst_base = (nir_alu_type) nir_alu_type_get_base_type(dst); 37 unsigned src_bit_size = nir_alu_type_get_type_size(src); 38 unsigned dst_bit_size = nir_alu_type_get_type_size(dst); 39 40 if (src == dst && src_base == nir_type_float) { 41 return nir_op_mov; 42 } else if (src == dst && src_base == nir_type_bool) { 43 return nir_op_mov; 44 } else if ((src_base == nir_type_int || src_base == nir_type_uint) && 45 (dst_base == nir_type_int || dst_base == nir_type_uint) && 46 src_bit_size == dst_bit_size) { 47 /* Integer <-> integer conversions with the same bit-size on both 48 * ends are just no-op moves. 49 */ 50 return nir_op_mov; 51 } 52 53 /* f2b, i2b, and u2b do not exist. Use ine or fne (via nir_type_conversion) 54 * instead. 55 */ 56 assert(src_base == dst_base || dst_base != nir_type_bool); 57 58 switch (src_base) { 59 % for src_t in ['int', 'uint', 'float', 'bool']: 60 case nir_type_${src_t}: 61 switch (dst_base) { 62 % for dst_t in ['int', 'uint', 'float', 'bool']: 63 case nir_type_${dst_t}: 64 % if src_t in ['int', 'uint'] and dst_t in ['int', 'uint']: 65 % if dst_t == 'int': 66 <% continue %> 67 % else: 68 <% dst_t = src_t %> 69 % endif 70 % elif src_t == 'bool' and dst_t in ['int', 'uint']: 71 % if dst_t == 'int': 72 <% continue %> 73 % else: 74 <% dst_t = 'int' %> 75 % endif 76 % elif src_t != 'bool' and dst_t == 'bool': 77 <% continue %> 78 % endif 79 switch (dst_bit_size) { 80 % for dst_bits in type_sizes(dst_t): 81 case ${dst_bits}: 82 % if src_t == 'float' and dst_t == 'float' and dst_bits == 16: 83 switch(rnd) { 84 % for rnd_t in [('rtne', '_rtne'), ('rtz', '_rtz'), ('undef', '')]: 85 case nir_rounding_mode_${rnd_t[0]}: 86 return ${'nir_op_{0}2{1}{2}{3}'.format(src_t[0], dst_t[0], 87 dst_bits, rnd_t[1])}; 88 % endfor 89 default: 90 unreachable("Invalid 16-bit nir rounding mode"); 91 } 92 % else: 93 assert(rnd == nir_rounding_mode_undef); 94 return ${'nir_op_{0}2{1}{2}'.format(src_t[0], dst_t[0], dst_bits)}; 95 % endif 96 % endfor 97 default: 98 unreachable("Invalid nir alu bit size"); 99 } 100 % endfor 101 default: 102 unreachable("Invalid nir alu base type"); 103 } 104 % endfor 105 default: 106 unreachable("Invalid nir alu base type"); 107 } 108 } 109 110 const nir_op_info nir_op_infos[nir_num_opcodes] = { 111 % for name, opcode in sorted(opcodes.items()): 112 { 113 .name = "${name}", 114 .num_inputs = ${opcode.num_inputs}, 115 .output_size = ${opcode.output_size}, 116 .output_type = ${"nir_type_" + opcode.output_type}, 117 .input_sizes = { 118 ${ ", ".join(str(size) for size in opcode.input_sizes) } 119 }, 120 .input_types = { 121 ${ ", ".join("nir_type_" + type for type in opcode.input_types) } 122 }, 123 .is_conversion = ${"true" if opcode.is_conversion else "false"}, 124 .algebraic_properties = 125 ${ "0" if opcode.algebraic_properties == "" else " | ".join( 126 "NIR_OP_IS_" + prop.upper() for prop in 127 opcode.algebraic_properties.strip().split(" ")) } 128 }, 129 % endfor 130 }; 131 """) 132 133 print(template.render(opcodes=opcodes, type_sizes=type_sizes)) 134