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
26from __future__ import print_function
27
28from nir_opcodes import opcodes, type_sizes
29from mako.template import Template
30
31template = Template("""
32#include "nir.h"
33
34nir_op
35nir_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_mov;
44   } else if (src == dst && src_base == nir_type_bool) {
45      return nir_op_mov;
46   } else if ((src_base == nir_type_int || src_base == nir_type_uint) &&
47              (dst_base == nir_type_int || dst_base == nir_type_uint) &&
48              src_bit_size == dst_bit_size) {
49      /* Integer <-> integer conversions with the same bit-size on both
50       * ends are just no-op moves.
51       */
52      return nir_op_mov;
53   }
54
55   switch (src_base) {
56%     for src_t in ['int', 'uint', 'float', 'bool']:
57      case nir_type_${src_t}:
58         switch (dst_base) {
59%           for dst_t in ['int', 'uint', 'float', 'bool']:
60            case nir_type_${dst_t}:
61%              if src_t in ['int', 'uint'] and dst_t in ['int', 'uint']:
62%                 if dst_t == 'int':
63<%                   continue %>
64%                 else:
65<%                   dst_t = src_t %>
66%                 endif
67%              elif src_t == 'bool' and dst_t in ['int', 'uint', 'bool']:
68%                 if dst_t == 'int':
69<%                   continue %>
70%                 else:
71<%                   dst_t = 'int' %>
72%                 endif
73%              elif src_t == 'uint' and dst_t == 'bool':
74<%                src_t = 'int' %>
75%              endif
76               switch (dst_bit_size) {
77%                 for dst_bits in type_sizes(dst_t):
78                  case ${dst_bits}:
79%                    if src_t == 'float' and dst_t == 'float' and dst_bits == 16:
80                     switch(rnd) {
81%                       for rnd_t in [('rtne', '_rtne'), ('rtz', '_rtz'), ('undef', '')]:
82                        case nir_rounding_mode_${rnd_t[0]}:
83                           return ${'nir_op_{0}2{1}{2}{3}'.format(src_t[0], dst_t[0],
84                                                                   dst_bits, rnd_t[1])};
85%                       endfor
86                        default:
87                           unreachable("Invalid 16-bit nir rounding mode");
88                     }
89%                    else:
90                     assert(rnd == nir_rounding_mode_undef);
91                     return ${'nir_op_{0}2{1}{2}'.format(src_t[0], dst_t[0], dst_bits)};
92%                    endif
93%                 endfor
94                  default:
95                     unreachable("Invalid nir alu bit size");
96               }
97%           endfor
98            default:
99               unreachable("Invalid nir alu base type");
100         }
101%     endfor
102      default:
103         unreachable("Invalid nir alu base type");
104   }
105}
106
107const nir_op_info nir_op_infos[nir_num_opcodes] = {
108% for name, opcode in sorted(opcodes.items()):
109{
110   .name = "${name}",
111   .num_inputs = ${opcode.num_inputs},
112   .output_size = ${opcode.output_size},
113   .output_type = ${"nir_type_" + opcode.output_type},
114   .input_sizes = {
115      ${ ", ".join(str(size) for size in opcode.input_sizes) }
116   },
117   .input_types = {
118      ${ ", ".join("nir_type_" + type for type in opcode.input_types) }
119   },
120   .is_conversion = ${"true" if opcode.is_conversion else "false"},
121   .algebraic_properties =
122      ${ "0" if opcode.algebraic_properties == "" else " | ".join(
123            "NIR_OP_IS_" + prop.upper() for prop in
124               opcode.algebraic_properties.strip().split(" ")) }
125},
126% endfor
127};
128""")
129
130print(template.render(opcodes=opcodes, type_sizes=type_sizes))
131