nir: support converting to 8-bit integers in nir_type_conversion_op
[mesa.git] / src / compiler / nir / nir_opcodes_c.py
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
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_fmov;
42 } else if ((src_base == nir_type_int || src_base == nir_type_uint) &&
43 (dst_base == nir_type_int || dst_base == nir_type_uint) &&
44 src_bit_size == dst_bit_size) {
45 /* Integer <-> integer conversions with the same bit-size on both
46 * ends are just no-op moves.
47 */
48 return nir_op_imov;
49 }
50
51 switch (src_base) {
52 % for src_t in ['int', 'uint', 'float']:
53 case nir_type_${src_t}:
54 switch (dst_base) {
55 % for dst_t in ['int', 'uint', 'float']:
56 case nir_type_${dst_t}:
57 % if src_t in ['int', 'uint'] and dst_t in ['int', 'uint']:
58 % if dst_t == 'int':
59 <% continue %>
60 % else:
61 <% dst_t = src_t %>
62 % endif
63 % endif
64 switch (dst_bit_size) {
65 % if dst_t == 'float':
66 <% bit_sizes = [16, 32, 64] %>
67 % else:
68 <% bit_sizes = [8, 16, 32, 64] %>
69 % endif
70 % for dst_bits in bit_sizes:
71 case ${dst_bits}:
72 % if src_t == 'float' and dst_t == 'float' and dst_bits == 16:
73 switch(rnd) {
74 % for rnd_t in ['rtne', 'rtz', 'undef']:
75 case nir_rounding_mode_${rnd_t}:
76 return ${'nir_op_{0}2{1}{2}_{3}'.format(src_t[0], dst_t[0],
77 dst_bits, rnd_t)};
78 % endfor
79 default:
80 unreachable("Invalid 16-bit nir rounding mode");
81 }
82 % else:
83 assert(rnd == nir_rounding_mode_undef);
84 return ${'nir_op_{0}2{1}{2}'.format(src_t[0], dst_t[0], dst_bits)};
85 % endif
86 % endfor
87 default:
88 unreachable("Invalid nir alu bit size");
89 }
90 % endfor
91 case nir_type_bool:
92 % if src_t == 'float':
93 return nir_op_f2b;
94 % else:
95 return nir_op_i2b;
96 % endif
97 default:
98 unreachable("Invalid nir alu base type");
99 }
100 % endfor
101 case nir_type_bool:
102 switch (dst_base) {
103 case nir_type_int:
104 case nir_type_uint:
105 return nir_op_b2i;
106 case nir_type_float:
107 return nir_op_b2f;
108 default:
109 unreachable("Invalid nir alu base type");
110 }
111 default:
112 unreachable("Invalid nir alu base type");
113 }
114 }
115
116 const nir_op_info nir_op_infos[nir_num_opcodes] = {
117 % for name, opcode in sorted(opcodes.iteritems()):
118 {
119 .name = "${name}",
120 .num_inputs = ${opcode.num_inputs},
121 .output_size = ${opcode.output_size},
122 .output_type = ${"nir_type_" + opcode.output_type},
123 .input_sizes = {
124 ${ ", ".join(str(size) for size in opcode.input_sizes) }
125 },
126 .input_types = {
127 ${ ", ".join("nir_type_" + type for type in opcode.input_types) }
128 },
129 .algebraic_properties =
130 ${ "0" if opcode.algebraic_properties == "" else " | ".join(
131 "NIR_OP_IS_" + prop.upper() for prop in
132 opcode.algebraic_properties.strip().split(" ")) }
133 },
134 % endfor
135 };
136 """)
137
138 print template.render(opcodes=opcodes)