nir/opcodes: Pull in the type helpers from constant_expressions
[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 __future__ import print_function
27
28 from nir_opcodes import opcodes, type_sizes
29 from mako.template import Template
30
31 template = Template("""
32 #include "nir.h"
33
34 nir_op
35 nir_type_conversion_op(nir_alu_type src, nir_alu_type dst, nir_rounding_mode rnd)
36 {
37 nir_alu_type src_base = (nir_alu_type) nir_alu_type_get_base_type(src);
38 nir_alu_type dst_base = (nir_alu_type) nir_alu_type_get_base_type(dst);
39 unsigned src_bit_size = nir_alu_type_get_type_size(src);
40 unsigned dst_bit_size = nir_alu_type_get_type_size(dst);
41
42 if (src == dst && src_base == nir_type_float) {
43 return nir_op_fmov;
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_imov;
51 }
52
53 switch (src_base) {
54 % for src_t in ['int', 'uint', 'float']:
55 case nir_type_${src_t}:
56 switch (dst_base) {
57 % for dst_t in ['int', 'uint', 'float']:
58 case nir_type_${dst_t}:
59 % if src_t in ['int', 'uint'] and dst_t in ['int', 'uint']:
60 % if dst_t == 'int':
61 <% continue %>
62 % else:
63 <% dst_t = src_t %>
64 % endif
65 % endif
66 switch (dst_bit_size) {
67 % for dst_bits in type_sizes(dst_t):
68 case ${dst_bits}:
69 % if src_t == 'float' and dst_t == 'float' and dst_bits == 16:
70 switch(rnd) {
71 % for rnd_t in [('rtne', '_rtne'), ('rtz', '_rtz'), ('undef', '')]:
72 case nir_rounding_mode_${rnd_t[0]}:
73 return ${'nir_op_{0}2{1}{2}{3}'.format(src_t[0], dst_t[0],
74 dst_bits, rnd_t[1])};
75 % endfor
76 default:
77 unreachable("Invalid 16-bit nir rounding mode");
78 }
79 % else:
80 assert(rnd == nir_rounding_mode_undef);
81 return ${'nir_op_{0}2{1}{2}'.format(src_t[0], dst_t[0], dst_bits)};
82 % endif
83 % endfor
84 default:
85 unreachable("Invalid nir alu bit size");
86 }
87 % endfor
88 case nir_type_bool:
89 % if src_t == 'float':
90 return nir_op_f2b;
91 % else:
92 return nir_op_i2b;
93 % endif
94 default:
95 unreachable("Invalid nir alu base type");
96 }
97 % endfor
98 case nir_type_bool:
99 switch (dst_base) {
100 case nir_type_int:
101 case nir_type_uint:
102 return nir_op_b2i;
103 case nir_type_float:
104 return nir_op_b2f;
105 default:
106 unreachable("Invalid nir alu base type");
107 }
108 default:
109 unreachable("Invalid nir alu base type");
110 }
111 }
112
113 const nir_op_info nir_op_infos[nir_num_opcodes] = {
114 % for name, opcode in sorted(opcodes.items()):
115 {
116 .name = "${name}",
117 .num_inputs = ${opcode.num_inputs},
118 .output_size = ${opcode.output_size},
119 .output_type = ${"nir_type_" + opcode.output_type},
120 .input_sizes = {
121 ${ ", ".join(str(size) for size in opcode.input_sizes) }
122 },
123 .input_types = {
124 ${ ", ".join("nir_type_" + type for type in opcode.input_types) }
125 },
126 .algebraic_properties =
127 ${ "0" if opcode.algebraic_properties == "" else " | ".join(
128 "NIR_OP_IS_" + prop.upper() for prop in
129 opcode.algebraic_properties.strip().split(" ")) }
130 },
131 % endfor
132 };
133 """)
134
135 print(template.render(opcodes=opcodes, type_sizes=type_sizes))