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/bigmath.h"
67 #include "nir_constant_expressions.h"
70 * Evaluate one component of packSnorm4x8.
73 pack_snorm_1x8(float x)
75 /* From section 8.4 of the GLSL 4.30 spec:
79 * The conversion for component c of v to fixed point is done as
82 * packSnorm4x8: round(clamp(c, -1, +1) * 127.0)
84 * We must first cast the float to an int, because casting a negative
85 * float to a uint is undefined.
87 return (uint8_t) (int)
88 _mesa_roundevenf(CLAMP(x, -1.0f, +1.0f) * 127.0f);
92 * Evaluate one component of packSnorm2x16.
95 pack_snorm_1x16(float x)
97 /* From section 8.4 of the GLSL ES 3.00 spec:
101 * The conversion for component c of v to fixed point is done as
104 * packSnorm2x16: round(clamp(c, -1, +1) * 32767.0)
106 * We must first cast the float to an int, because casting a negative
107 * float to a uint is undefined.
109 return (uint16_t) (int)
110 _mesa_roundevenf(CLAMP(x, -1.0f, +1.0f) * 32767.0f);
114 * Evaluate one component of unpackSnorm4x8.
117 unpack_snorm_1x8(uint8_t u)
119 /* From section 8.4 of the GLSL 4.30 spec:
123 * The conversion for unpacked fixed-point value f to floating point is
126 * unpackSnorm4x8: clamp(f / 127.0, -1, +1)
128 return CLAMP((int8_t) u / 127.0f, -1.0f, +1.0f);
132 * Evaluate one component of unpackSnorm2x16.
135 unpack_snorm_1x16(uint16_t u)
137 /* From section 8.4 of the GLSL ES 3.00 spec:
141 * The conversion for unpacked fixed-point value f to floating point is
144 * unpackSnorm2x16: clamp(f / 32767.0, -1, +1)
146 return CLAMP((int16_t) u / 32767.0f, -1.0f, +1.0f);
150 * Evaluate one component packUnorm4x8.
153 pack_unorm_1x8(float x)
155 /* From section 8.4 of the GLSL 4.30 spec:
159 * The conversion for component c of v to fixed point is done as
162 * packUnorm4x8: round(clamp(c, 0, +1) * 255.0)
164 return (uint8_t) (int)
165 _mesa_roundevenf(CLAMP(x, 0.0f, 1.0f) * 255.0f);
169 * Evaluate one component packUnorm2x16.
172 pack_unorm_1x16(float x)
174 /* From section 8.4 of the GLSL ES 3.00 spec:
178 * The conversion for component c of v to fixed point is done as
181 * packUnorm2x16: round(clamp(c, 0, +1) * 65535.0)
183 return (uint16_t) (int)
184 _mesa_roundevenf(CLAMP(x, 0.0f, 1.0f) * 65535.0f);
188 * Evaluate one component of unpackUnorm4x8.
191 unpack_unorm_1x8(uint8_t u)
193 /* From section 8.4 of the GLSL 4.30 spec:
197 * The conversion for unpacked fixed-point value f to floating point is
200 * unpackUnorm4x8: f / 255.0
202 return (float) u / 255.0f;
206 * Evaluate one component of unpackUnorm2x16.
209 unpack_unorm_1x16(uint16_t u)
211 /* From section 8.4 of the GLSL ES 3.00 spec:
215 * The conversion for unpacked fixed-point value f to floating point is
218 * unpackUnorm2x16: f / 65535.0
220 return (float) u / 65535.0f;
224 * Evaluate one component of packHalf2x16.
227 pack_half_1x16(float x)
229 return _mesa_float_to_half(x);
233 * Evaluate one component of unpackHalf2x16.
236 unpack_half_1x16(uint16_t u)
238 return _mesa_half_to_float(u);
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 {
263 <%def name="evaluate_op(op, bit_size)">
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]
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:
275 % elif "src" + str(j) not in op.const_expr:
276 ## Avoid unused variable warnings
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}]),
288 _src[${j}].${get_const_field(input_types[j])}[${k}],
291 % for k in range(op.input_sizes[j], 4):
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
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:
307 % elif "src" + str(j) not in op.const_expr:
308 ## Avoid unused variable warnings
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]);
317 const ${input_types[j]}_t src${j} =
318 _src[${j}].${get_const_field(input_types[j])}[_i];
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;
330 ${output_type}_t dst = ${op.const_expr};
333 ## Store the current component of the actual destination to the
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);
344 _dst_val.${get_const_field(output_type)}[_i] = dst;
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;
354 % if "dst" in op.const_expr:
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};
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]});
376 _dst_val.${get_const_field(output_type)}[${k}] = dst.${"xyzw"[k]};
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)
388 nir_const_value _dst_val = { {0, } };
390 % if op_bit_sizes(op) is not None:
392 % for bit_size in op_bit_sizes(op):
394 ${evaluate_op(op, bit_size)}
400 unreachable("unknown bit width");
403 ${evaluate_op(op, 0)}
411 nir_eval_const_opcode(nir_op op, unsigned num_components,
412 unsigned bit_width, nir_const_value *src)
415 % for name in sorted(opcodes.keys()):
417 return evaluate_${name}(num_components, bit_width, src);
420 unreachable("shouldn't get here");
424 from mako
.template
import Template
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
))