1 from __future__
import print_function
4 from nir_opcodes
import opcodes
5 from nir_opcodes
import type_has_size
, type_size
, type_sizes
, type_base_type
7 def type_add_size(type_
, size
):
8 if type_has_size(type_
):
10 return type_
+ str(size
)
14 if not type_has_size(op
.output_type
):
15 sizes
= set(type_sizes(op
.output_type
))
17 for input_type
in op
.input_types
:
18 if not type_has_size(input_type
):
20 sizes
= set(type_sizes(input_type
))
22 sizes
= sizes
.intersection(set(type_sizes(input_type
)))
24 return sorted(list(sizes
)) if sizes
is not None else None
26 def get_const_field(type_
):
27 if type_size(type_
) == 1:
29 elif type_base_type(type_
) == 'bool':
30 return 'i' + str(type_size(type_
))
31 elif type_
== "float16":
34 return type_base_type(type_
)[0] + str(type_size(type_
))
38 * Copyright (C) 2014 Intel Corporation
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:
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
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
60 * Jason Ekstrand (jason@jlekstrand.net)
64 #include "util/rounding.h" /* for _mesa_roundeven */
65 #include "util/half_float.h"
66 #include "util/double.h"
67 #include "util/softfloat.h"
68 #include "util/bigmath.h"
69 #include "nir_constant_expressions.h"
71 #define MAX_UINT_FOR_SIZE(bits) (UINT64_MAX >> (64 - (bits)))
74 * \brief Checks if the provided value is a denorm and flushes it to zero.
77 constant_denorm_flush_to_zero(nir_const_value *value, unsigned bit_size)
81 if (0 == (value->u64 & 0x7ff0000000000000))
82 value->u64 &= 0x8000000000000000;
85 if (0 == (value->u32 & 0x7f800000))
86 value->u32 &= 0x80000000;
89 if (0 == (value->u16 & 0x7c00))
95 * Evaluate one component of packSnorm4x8.
98 pack_snorm_1x8(float x)
100 /* From section 8.4 of the GLSL 4.30 spec:
104 * The conversion for component c of v to fixed point is done as
107 * packSnorm4x8: round(clamp(c, -1, +1) * 127.0)
109 * We must first cast the float to an int, because casting a negative
110 * float to a uint is undefined.
112 return (uint8_t) (int)
113 _mesa_roundevenf(CLAMP(x, -1.0f, +1.0f) * 127.0f);
117 * Evaluate one component of packSnorm2x16.
120 pack_snorm_1x16(float x)
122 /* From section 8.4 of the GLSL ES 3.00 spec:
126 * The conversion for component c of v to fixed point is done as
129 * packSnorm2x16: round(clamp(c, -1, +1) * 32767.0)
131 * We must first cast the float to an int, because casting a negative
132 * float to a uint is undefined.
134 return (uint16_t) (int)
135 _mesa_roundevenf(CLAMP(x, -1.0f, +1.0f) * 32767.0f);
139 * Evaluate one component of unpackSnorm4x8.
142 unpack_snorm_1x8(uint8_t u)
144 /* From section 8.4 of the GLSL 4.30 spec:
148 * The conversion for unpacked fixed-point value f to floating point is
151 * unpackSnorm4x8: clamp(f / 127.0, -1, +1)
153 return CLAMP((int8_t) u / 127.0f, -1.0f, +1.0f);
157 * Evaluate one component of unpackSnorm2x16.
160 unpack_snorm_1x16(uint16_t u)
162 /* From section 8.4 of the GLSL ES 3.00 spec:
166 * The conversion for unpacked fixed-point value f to floating point is
169 * unpackSnorm2x16: clamp(f / 32767.0, -1, +1)
171 return CLAMP((int16_t) u / 32767.0f, -1.0f, +1.0f);
175 * Evaluate one component packUnorm4x8.
178 pack_unorm_1x8(float x)
180 /* From section 8.4 of the GLSL 4.30 spec:
184 * The conversion for component c of v to fixed point is done as
187 * packUnorm4x8: round(clamp(c, 0, +1) * 255.0)
189 return (uint8_t) (int)
190 _mesa_roundevenf(CLAMP(x, 0.0f, 1.0f) * 255.0f);
194 * Evaluate one component packUnorm2x16.
197 pack_unorm_1x16(float x)
199 /* From section 8.4 of the GLSL ES 3.00 spec:
203 * The conversion for component c of v to fixed point is done as
206 * packUnorm2x16: round(clamp(c, 0, +1) * 65535.0)
208 return (uint16_t) (int)
209 _mesa_roundevenf(CLAMP(x, 0.0f, 1.0f) * 65535.0f);
213 * Evaluate one component of unpackUnorm4x8.
216 unpack_unorm_1x8(uint8_t u)
218 /* From section 8.4 of the GLSL 4.30 spec:
222 * The conversion for unpacked fixed-point value f to floating point is
225 * unpackUnorm4x8: f / 255.0
227 return (float) u / 255.0f;
231 * Evaluate one component of unpackUnorm2x16.
234 unpack_unorm_1x16(uint16_t u)
236 /* From section 8.4 of the GLSL ES 3.00 spec:
240 * The conversion for unpacked fixed-point value f to floating point is
243 * unpackUnorm2x16: f / 65535.0
245 return (float) u / 65535.0f;
249 * Evaluate one component of packHalf2x16.
252 pack_half_1x16(float x)
254 return _mesa_float_to_half(x);
258 * Evaluate one component of unpackHalf2x16.
261 unpack_half_1x16(uint16_t u)
263 return _mesa_half_to_float(u);
266 /* Some typed vector structures to make things like src0.y work */
267 typedef int8_t int1_t;
268 typedef uint8_t uint1_t;
269 typedef float float16_t;
270 typedef float float32_t;
271 typedef double float64_t;
272 typedef bool bool1_t;
273 typedef bool bool8_t;
274 typedef bool bool16_t;
275 typedef bool bool32_t;
276 typedef bool bool64_t;
277 % for type in ["float", "int", "uint", "bool"]:
278 % for width in type_sizes(type):
279 struct ${type}${width}_vec {
288 <%def name="evaluate_op(op, bit_size, execution_mode)">
290 output_type = type_add_size(op.output_type, bit_size)
291 input_types = [type_add_size(type_, bit_size) for type_ in op.input_types]
294 ## For each non-per-component input, create a variable srcN that
295 ## contains x, y, z, and w elements which are filled in with the
296 ## appropriately-typed values.
297 % for j in range(op.num_inputs):
298 % if op.input_sizes[j] == 0:
300 % elif "src" + str(j) not in op.const_expr:
301 ## Avoid unused variable warnings
305 const struct ${input_types[j]}_vec src${j} = {
306 % for k in range(op.input_sizes[j]):
307 % if input_types[j] == "int1":
308 /* 1-bit integers use a 0/-1 convention */
309 -(int1_t)_src[${j}][${k}].b,
310 % elif input_types[j] == "float16":
311 _mesa_half_to_float(_src[${j}][${k}].u16),
313 _src[${j}][${k}].${get_const_field(input_types[j])},
316 % for k in range(op.input_sizes[j], 4):
322 % if op.output_size == 0:
323 ## For per-component instructions, we need to iterate over the
324 ## components and apply the constant expression one component
326 for (unsigned _i = 0; _i < num_components; _i++) {
327 ## For each per-component input, create a variable srcN that
328 ## contains the value of the current (_i'th) component.
329 % for j in range(op.num_inputs):
330 % if op.input_sizes[j] != 0:
332 % elif "src" + str(j) not in op.const_expr:
333 ## Avoid unused variable warnings
335 % elif input_types[j] == "int1":
336 /* 1-bit integers use a 0/-1 convention */
337 const int1_t src${j} = -(int1_t)_src[${j}][_i].b;
338 % elif input_types[j] == "float16":
339 const float src${j} =
340 _mesa_half_to_float(_src[${j}][_i].u16);
342 const ${input_types[j]}_t src${j} =
343 _src[${j}][_i].${get_const_field(input_types[j])};
347 ## Create an appropriately-typed variable dst and assign the
348 ## result of the const_expr to it. If const_expr already contains
349 ## writes to dst, just include const_expr directly.
350 % if "dst" in op.const_expr:
351 ${output_type}_t dst;
355 ${output_type}_t dst = ${op.const_expr};
358 ## Store the current component of the actual destination to the
360 % if output_type == "int1" or output_type == "uint1":
361 /* 1-bit integers get truncated */
362 _dst_val[_i].b = dst & 1;
363 % elif output_type.startswith("bool"):
364 ## Sanitize the C value to a proper NIR 0/-1 bool
365 _dst_val[_i].${get_const_field(output_type)} = -(int)dst;
366 % elif output_type == "float16":
367 if (nir_is_rounding_mode_rtz(execution_mode, 16)) {
368 _dst_val[_i].u16 = _mesa_float_to_float16_rtz(dst);
370 _dst_val[_i].u16 = _mesa_float_to_float16_rtne(dst);
373 _dst_val[_i].${get_const_field(output_type)} = dst;
376 % if op.name != "fquantize2f16" and type_base_type(output_type) == "float":
377 % if type_has_size(output_type):
378 if (nir_is_denorm_flush_to_zero(execution_mode, ${type_size(output_type)})) {
379 constant_denorm_flush_to_zero(&_dst_val[_i], ${type_size(output_type)});
382 if (nir_is_denorm_flush_to_zero(execution_mode, ${bit_size})) {
383 constant_denorm_flush_to_zero(&_dst_val[i], bit_size);
389 ## In the non-per-component case, create a struct dst with
390 ## appropriately-typed elements x, y, z, and w and assign the result
391 ## of the const_expr to all components of dst, or include the
392 ## const_expr directly if it writes to dst already.
393 struct ${output_type}_vec dst;
395 % if "dst" in op.const_expr:
398 ## Splat the value to all components. This way expressions which
399 ## write the same value to all components don't need to explicitly
400 ## write to dest. One such example is fnoise which has a
401 ## const_expr of 0.0f.
402 dst.x = dst.y = dst.z = dst.w = ${op.const_expr};
405 ## For each component in the destination, copy the value of dst to
406 ## the actual destination.
407 % for k in range(op.output_size):
408 % if output_type == "int1" or output_type == "uint1":
409 /* 1-bit integers get truncated */
410 _dst_val[${k}].b = dst.${"xyzw"[k]} & 1;
411 % elif output_type.startswith("bool"):
412 ## Sanitize the C value to a proper NIR 0/-1 bool
413 _dst_val[${k}].${get_const_field(output_type)} = -(int)dst.${"xyzw"[k]};
414 % elif output_type == "float16":
415 if (nir_is_rounding_mode_rtz(execution_mode, 16)) {
416 _dst_val[${k}].u16 = _mesa_float_to_float16_rtz(dst.${"xyzw"[k]});
418 _dst_val[${k}].u16 = _mesa_float_to_float16_rtne(dst.${"xyzw"[k]});
421 _dst_val[${k}].${get_const_field(output_type)} = dst.${"xyzw"[k]};
424 % if op.name != "fquantize2f16" and type_base_type(output_type) == "float":
425 % if type_has_size(output_type):
426 if (nir_is_denorm_flush_to_zero(execution_mode, ${type_size(output_type)})) {
427 constant_denorm_flush_to_zero(&_dst_val[${k}], ${type_size(output_type)});
430 if (nir_is_denorm_flush_to_zero(execution_mode, ${bit_size})) {
431 constant_denorm_flush_to_zero(&_dst_val[${k}], bit_size);
439 % for name, op in sorted(opcodes.items()):
441 evaluate_${name}(nir_const_value *_dst_val,
442 UNUSED unsigned num_components,
443 ${"UNUSED" if op_bit_sizes(op) is None else ""} unsigned bit_size,
444 UNUSED nir_const_value **_src,
445 UNUSED unsigned execution_mode)
447 % if op_bit_sizes(op) is not None:
449 % for bit_size in op_bit_sizes(op):
451 ${evaluate_op(op, bit_size, execution_mode)}
457 unreachable("unknown bit width");
460 ${evaluate_op(op, 0, execution_mode)}
466 nir_eval_const_opcode(nir_op op, nir_const_value *dest,
467 unsigned num_components, unsigned bit_width,
468 nir_const_value **src,
469 unsigned float_controls_execution_mode)
472 % for name in sorted(opcodes.keys()):
474 evaluate_${name}(dest, num_components, bit_width, src, float_controls_execution_mode);
478 unreachable("shouldn't get here");
482 from mako
.template
import Template
484 print(Template(template
).render(opcodes
=opcodes
, type_sizes
=type_sizes
,
485 type_base_type
=type_base_type
,
487 type_has_size
=type_has_size
,
488 type_add_size
=type_add_size
,
489 op_bit_sizes
=op_bit_sizes
,
490 get_const_field
=get_const_field
))