nir: Add a ptr_as_array deref type
[mesa.git] / src / compiler / nir / nir_constant_expressions.py
1 from __future__ import print_function
2
3 import re
4 from nir_opcodes import opcodes
5 from nir_opcodes import type_has_size, type_size, type_sizes, type_base_type
6
7 def type_add_size(type_, size):
8 if type_has_size(type_):
9 return type_
10 return type_ + str(size)
11
12 def op_bit_sizes(op):
13 sizes = None
14 if not type_has_size(op.output_type):
15 sizes = set(type_sizes(op.output_type))
16
17 for input_type in op.input_types:
18 if not type_has_size(input_type):
19 if sizes is None:
20 sizes = set(type_sizes(input_type))
21 else:
22 sizes = sizes.intersection(set(type_sizes(input_type)))
23
24 return sorted(list(sizes)) if sizes is not None else None
25
26 def get_const_field(type_):
27 if type_size(type_) == 1:
28 return 'b'
29 elif type_base_type(type_) == 'bool':
30 return 'i' + str(type_size(type_))
31 elif type_ == "float16":
32 return "u16"
33 else:
34 return type_base_type(type_)[0] + str(type_size(type_))
35
36 template = """\
37 /*
38 * Copyright (C) 2014 Intel Corporation
39 *
40 * Permission is hereby granted, free of charge, to any person obtaining a
41 * copy of this software and associated documentation files (the "Software"),
42 * to deal in the Software without restriction, including without limitation
43 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
44 * and/or sell copies of the Software, and to permit persons to whom the
45 * Software is furnished to do so, subject to the following conditions:
46 *
47 * The above copyright notice and this permission notice (including the next
48 * paragraph) shall be included in all copies or substantial portions of the
49 * Software.
50 *
51 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
52 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
53 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
54 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
55 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
56 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
57 * IN THE SOFTWARE.
58 *
59 * Authors:
60 * Jason Ekstrand (jason@jlekstrand.net)
61 */
62
63 #include <math.h>
64 #include "util/rounding.h" /* for _mesa_roundeven */
65 #include "util/half_float.h"
66 #include "util/bigmath.h"
67 #include "nir_constant_expressions.h"
68
69 /**
70 * Evaluate one component of packSnorm4x8.
71 */
72 static uint8_t
73 pack_snorm_1x8(float x)
74 {
75 /* From section 8.4 of the GLSL 4.30 spec:
76 *
77 * packSnorm4x8
78 * ------------
79 * The conversion for component c of v to fixed point is done as
80 * follows:
81 *
82 * packSnorm4x8: round(clamp(c, -1, +1) * 127.0)
83 *
84 * We must first cast the float to an int, because casting a negative
85 * float to a uint is undefined.
86 */
87 return (uint8_t) (int)
88 _mesa_roundevenf(CLAMP(x, -1.0f, +1.0f) * 127.0f);
89 }
90
91 /**
92 * Evaluate one component of packSnorm2x16.
93 */
94 static uint16_t
95 pack_snorm_1x16(float x)
96 {
97 /* From section 8.4 of the GLSL ES 3.00 spec:
98 *
99 * packSnorm2x16
100 * -------------
101 * The conversion for component c of v to fixed point is done as
102 * follows:
103 *
104 * packSnorm2x16: round(clamp(c, -1, +1) * 32767.0)
105 *
106 * We must first cast the float to an int, because casting a negative
107 * float to a uint is undefined.
108 */
109 return (uint16_t) (int)
110 _mesa_roundevenf(CLAMP(x, -1.0f, +1.0f) * 32767.0f);
111 }
112
113 /**
114 * Evaluate one component of unpackSnorm4x8.
115 */
116 static float
117 unpack_snorm_1x8(uint8_t u)
118 {
119 /* From section 8.4 of the GLSL 4.30 spec:
120 *
121 * unpackSnorm4x8
122 * --------------
123 * The conversion for unpacked fixed-point value f to floating point is
124 * done as follows:
125 *
126 * unpackSnorm4x8: clamp(f / 127.0, -1, +1)
127 */
128 return CLAMP((int8_t) u / 127.0f, -1.0f, +1.0f);
129 }
130
131 /**
132 * Evaluate one component of unpackSnorm2x16.
133 */
134 static float
135 unpack_snorm_1x16(uint16_t u)
136 {
137 /* From section 8.4 of the GLSL ES 3.00 spec:
138 *
139 * unpackSnorm2x16
140 * ---------------
141 * The conversion for unpacked fixed-point value f to floating point is
142 * done as follows:
143 *
144 * unpackSnorm2x16: clamp(f / 32767.0, -1, +1)
145 */
146 return CLAMP((int16_t) u / 32767.0f, -1.0f, +1.0f);
147 }
148
149 /**
150 * Evaluate one component packUnorm4x8.
151 */
152 static uint8_t
153 pack_unorm_1x8(float x)
154 {
155 /* From section 8.4 of the GLSL 4.30 spec:
156 *
157 * packUnorm4x8
158 * ------------
159 * The conversion for component c of v to fixed point is done as
160 * follows:
161 *
162 * packUnorm4x8: round(clamp(c, 0, +1) * 255.0)
163 */
164 return (uint8_t) (int)
165 _mesa_roundevenf(CLAMP(x, 0.0f, 1.0f) * 255.0f);
166 }
167
168 /**
169 * Evaluate one component packUnorm2x16.
170 */
171 static uint16_t
172 pack_unorm_1x16(float x)
173 {
174 /* From section 8.4 of the GLSL ES 3.00 spec:
175 *
176 * packUnorm2x16
177 * -------------
178 * The conversion for component c of v to fixed point is done as
179 * follows:
180 *
181 * packUnorm2x16: round(clamp(c, 0, +1) * 65535.0)
182 */
183 return (uint16_t) (int)
184 _mesa_roundevenf(CLAMP(x, 0.0f, 1.0f) * 65535.0f);
185 }
186
187 /**
188 * Evaluate one component of unpackUnorm4x8.
189 */
190 static float
191 unpack_unorm_1x8(uint8_t u)
192 {
193 /* From section 8.4 of the GLSL 4.30 spec:
194 *
195 * unpackUnorm4x8
196 * --------------
197 * The conversion for unpacked fixed-point value f to floating point is
198 * done as follows:
199 *
200 * unpackUnorm4x8: f / 255.0
201 */
202 return (float) u / 255.0f;
203 }
204
205 /**
206 * Evaluate one component of unpackUnorm2x16.
207 */
208 static float
209 unpack_unorm_1x16(uint16_t u)
210 {
211 /* From section 8.4 of the GLSL ES 3.00 spec:
212 *
213 * unpackUnorm2x16
214 * ---------------
215 * The conversion for unpacked fixed-point value f to floating point is
216 * done as follows:
217 *
218 * unpackUnorm2x16: f / 65535.0
219 */
220 return (float) u / 65535.0f;
221 }
222
223 /**
224 * Evaluate one component of packHalf2x16.
225 */
226 static uint16_t
227 pack_half_1x16(float x)
228 {
229 return _mesa_float_to_half(x);
230 }
231
232 /**
233 * Evaluate one component of unpackHalf2x16.
234 */
235 static float
236 unpack_half_1x16(uint16_t u)
237 {
238 return _mesa_half_to_float(u);
239 }
240
241 /* Some typed vector structures to make things like src0.y work */
242 typedef int8_t int1_t;
243 typedef uint8_t uint1_t;
244 typedef float float16_t;
245 typedef float float32_t;
246 typedef double float64_t;
247 typedef bool bool1_t;
248 typedef bool bool8_t;
249 typedef bool bool16_t;
250 typedef bool bool32_t;
251 typedef bool bool64_t;
252 % for type in ["float", "int", "uint", "bool"]:
253 % for width in type_sizes(type):
254 struct ${type}${width}_vec {
255 ${type}${width}_t x;
256 ${type}${width}_t y;
257 ${type}${width}_t z;
258 ${type}${width}_t w;
259 };
260 % endfor
261 % endfor
262
263 <%def name="evaluate_op(op, bit_size)">
264 <%
265 output_type = type_add_size(op.output_type, bit_size)
266 input_types = [type_add_size(type_, bit_size) for type_ in op.input_types]
267 %>
268
269 ## For each non-per-component input, create a variable srcN that
270 ## contains x, y, z, and w elements which are filled in with the
271 ## appropriately-typed values.
272 % for j in range(op.num_inputs):
273 % if op.input_sizes[j] == 0:
274 <% continue %>
275 % elif "src" + str(j) not in op.const_expr:
276 ## Avoid unused variable warnings
277 <% continue %>
278 %endif
279
280 const struct ${input_types[j]}_vec src${j} = {
281 % for k in range(op.input_sizes[j]):
282 % if input_types[j] == "int1":
283 /* 1-bit integers use a 0/-1 convention */
284 -(int1_t)_src[${j}].b[${k}],
285 % elif input_types[j] == "float16":
286 _mesa_half_to_float(_src[${j}].u16[${k}]),
287 % else:
288 _src[${j}].${get_const_field(input_types[j])}[${k}],
289 % endif
290 % endfor
291 % for k in range(op.input_sizes[j], 4):
292 0,
293 % endfor
294 };
295 % endfor
296
297 % if op.output_size == 0:
298 ## For per-component instructions, we need to iterate over the
299 ## components and apply the constant expression one component
300 ## at a time.
301 for (unsigned _i = 0; _i < num_components; _i++) {
302 ## For each per-component input, create a variable srcN that
303 ## contains the value of the current (_i'th) component.
304 % for j in range(op.num_inputs):
305 % if op.input_sizes[j] != 0:
306 <% continue %>
307 % elif "src" + str(j) not in op.const_expr:
308 ## Avoid unused variable warnings
309 <% continue %>
310 % elif input_types[j] == "int1":
311 /* 1-bit integers use a 0/-1 convention */
312 const int1_t src${j} = -(int1_t)_src[${j}].b[_i];
313 % elif input_types[j] == "float16":
314 const float src${j} =
315 _mesa_half_to_float(_src[${j}].u16[_i]);
316 % else:
317 const ${input_types[j]}_t src${j} =
318 _src[${j}].${get_const_field(input_types[j])}[_i];
319 % endif
320 % endfor
321
322 ## Create an appropriately-typed variable dst and assign the
323 ## result of the const_expr to it. If const_expr already contains
324 ## writes to dst, just include const_expr directly.
325 % if "dst" in op.const_expr:
326 ${output_type}_t dst;
327
328 ${op.const_expr}
329 % else:
330 ${output_type}_t dst = ${op.const_expr};
331 % endif
332
333 ## Store the current component of the actual destination to the
334 ## value of dst.
335 % if output_type == "int1" or output_type == "uint1":
336 /* 1-bit integers get truncated */
337 _dst_val.b[_i] = dst & 1;
338 % elif output_type.startswith("bool"):
339 ## Sanitize the C value to a proper NIR 0/-1 bool
340 _dst_val.${get_const_field(output_type)}[_i] = -(int)dst;
341 % elif output_type == "float16":
342 _dst_val.u16[_i] = _mesa_float_to_half(dst);
343 % else:
344 _dst_val.${get_const_field(output_type)}[_i] = dst;
345 % endif
346 }
347 % else:
348 ## In the non-per-component case, create a struct dst with
349 ## appropriately-typed elements x, y, z, and w and assign the result
350 ## of the const_expr to all components of dst, or include the
351 ## const_expr directly if it writes to dst already.
352 struct ${output_type}_vec dst;
353
354 % if "dst" in op.const_expr:
355 ${op.const_expr}
356 % else:
357 ## Splat the value to all components. This way expressions which
358 ## write the same value to all components don't need to explicitly
359 ## write to dest. One such example is fnoise which has a
360 ## const_expr of 0.0f.
361 dst.x = dst.y = dst.z = dst.w = ${op.const_expr};
362 % endif
363
364 ## For each component in the destination, copy the value of dst to
365 ## the actual destination.
366 % for k in range(op.output_size):
367 % if output_type == "int1" or output_type == "uint1":
368 /* 1-bit integers get truncated */
369 _dst_val.b[${k}] = dst.${"xyzw"[k]} & 1;
370 % elif output_type.startswith("bool"):
371 ## Sanitize the C value to a proper NIR 0/-1 bool
372 _dst_val.${get_const_field(output_type)}[${k}] = -(int)dst.${"xyzw"[k]};
373 % elif output_type == "float16":
374 _dst_val.u16[${k}] = _mesa_float_to_half(dst.${"xyzw"[k]});
375 % else:
376 _dst_val.${get_const_field(output_type)}[${k}] = dst.${"xyzw"[k]};
377 % endif
378 % endfor
379 % endif
380 </%def>
381
382 % for name, op in sorted(opcodes.items()):
383 static nir_const_value
384 evaluate_${name}(MAYBE_UNUSED unsigned num_components,
385 ${"UNUSED" if op_bit_sizes(op) is None else ""} unsigned bit_size,
386 MAYBE_UNUSED nir_const_value *_src)
387 {
388 nir_const_value _dst_val = { {0, } };
389
390 % if op_bit_sizes(op) is not None:
391 switch (bit_size) {
392 % for bit_size in op_bit_sizes(op):
393 case ${bit_size}: {
394 ${evaluate_op(op, bit_size)}
395 break;
396 }
397 % endfor
398
399 default:
400 unreachable("unknown bit width");
401 }
402 % else:
403 ${evaluate_op(op, 0)}
404 % endif
405
406 return _dst_val;
407 }
408 % endfor
409
410 nir_const_value
411 nir_eval_const_opcode(nir_op op, unsigned num_components,
412 unsigned bit_width, nir_const_value *src)
413 {
414 switch (op) {
415 % for name in sorted(opcodes.keys()):
416 case nir_op_${name}:
417 return evaluate_${name}(num_components, bit_width, src);
418 % endfor
419 default:
420 unreachable("shouldn't get here");
421 }
422 }"""
423
424 from mako.template import Template
425
426 print(Template(template).render(opcodes=opcodes, type_sizes=type_sizes,
427 type_has_size=type_has_size,
428 type_add_size=type_add_size,
429 op_bit_sizes=op_bit_sizes,
430 get_const_field=get_const_field))