2 def type_has_size(type_
):
3 return type_
[-1:].isdigit()
6 if type_
.endswith("8"):
8 elif type_
.endswith("16"):
10 elif type_
.endswith("32"):
12 elif type_
.endswith("64"):
17 def type_add_size(type_
, size
):
18 if type_has_size(type_
):
20 return type_
+ str(size
)
22 def get_const_field(type_
):
33 if type_
== "float32":
35 if type_
== "float64":
37 raise Exception(str(type_
))
42 * Copyright (C) 2014 Intel Corporation
44 * Permission is hereby granted, free of charge, to any person obtaining a
45 * copy of this software and associated documentation files (the "Software"),
46 * to deal in the Software without restriction, including without limitation
47 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
48 * and/or sell copies of the Software, and to permit persons to whom the
49 * Software is furnished to do so, subject to the following conditions:
51 * The above copyright notice and this permission notice (including the next
52 * paragraph) shall be included in all copies or substantial portions of the
55 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
56 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
57 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
58 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
59 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
60 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
64 * Jason Ekstrand (jason@jlekstrand.net)
68 #include "main/core.h"
69 #include "util/rounding.h" /* for _mesa_roundeven */
70 #include "util/half_float.h"
71 #include "nir_constant_expressions.h"
74 * Evaluate one component of packSnorm4x8.
77 pack_snorm_1x8(float x)
79 /* From section 8.4 of the GLSL 4.30 spec:
83 * The conversion for component c of v to fixed point is done as
86 * packSnorm4x8: round(clamp(c, -1, +1) * 127.0)
88 * We must first cast the float to an int, because casting a negative
89 * float to a uint is undefined.
91 return (uint8_t) (int)
92 _mesa_roundevenf(CLAMP(x, -1.0f, +1.0f) * 127.0f);
96 * Evaluate one component of packSnorm2x16.
99 pack_snorm_1x16(float x)
101 /* From section 8.4 of the GLSL ES 3.00 spec:
105 * The conversion for component c of v to fixed point is done as
108 * packSnorm2x16: round(clamp(c, -1, +1) * 32767.0)
110 * We must first cast the float to an int, because casting a negative
111 * float to a uint is undefined.
113 return (uint16_t) (int)
114 _mesa_roundevenf(CLAMP(x, -1.0f, +1.0f) * 32767.0f);
118 * Evaluate one component of unpackSnorm4x8.
121 unpack_snorm_1x8(uint8_t u)
123 /* From section 8.4 of the GLSL 4.30 spec:
127 * The conversion for unpacked fixed-point value f to floating point is
130 * unpackSnorm4x8: clamp(f / 127.0, -1, +1)
132 return CLAMP((int8_t) u / 127.0f, -1.0f, +1.0f);
136 * Evaluate one component of unpackSnorm2x16.
139 unpack_snorm_1x16(uint16_t u)
141 /* From section 8.4 of the GLSL ES 3.00 spec:
145 * The conversion for unpacked fixed-point value f to floating point is
148 * unpackSnorm2x16: clamp(f / 32767.0, -1, +1)
150 return CLAMP((int16_t) u / 32767.0f, -1.0f, +1.0f);
154 * Evaluate one component packUnorm4x8.
157 pack_unorm_1x8(float x)
159 /* From section 8.4 of the GLSL 4.30 spec:
163 * The conversion for component c of v to fixed point is done as
166 * packUnorm4x8: round(clamp(c, 0, +1) * 255.0)
168 return (uint8_t) (int)
169 _mesa_roundevenf(CLAMP(x, 0.0f, 1.0f) * 255.0f);
173 * Evaluate one component packUnorm2x16.
176 pack_unorm_1x16(float x)
178 /* From section 8.4 of the GLSL ES 3.00 spec:
182 * The conversion for component c of v to fixed point is done as
185 * packUnorm2x16: round(clamp(c, 0, +1) * 65535.0)
187 return (uint16_t) (int)
188 _mesa_roundevenf(CLAMP(x, 0.0f, 1.0f) * 65535.0f);
192 * Evaluate one component of unpackUnorm4x8.
195 unpack_unorm_1x8(uint8_t u)
197 /* From section 8.4 of the GLSL 4.30 spec:
201 * The conversion for unpacked fixed-point value f to floating point is
204 * unpackUnorm4x8: f / 255.0
206 return (float) u / 255.0f;
210 * Evaluate one component of unpackUnorm2x16.
213 unpack_unorm_1x16(uint16_t u)
215 /* From section 8.4 of the GLSL ES 3.00 spec:
219 * The conversion for unpacked fixed-point value f to floating point is
222 * unpackUnorm2x16: f / 65535.0
224 return (float) u / 65535.0f;
228 * Evaluate one component of packHalf2x16.
231 pack_half_1x16(float x)
233 return _mesa_float_to_half(x);
237 * Evaluate one component of unpackHalf2x16.
240 unpack_half_1x16(uint16_t u)
242 return _mesa_half_to_float(u);
245 /* Some typed vector structures to make things like src0.y work */
246 typedef float float32_t;
247 typedef double float64_t;
248 typedef bool bool32_t;
249 % for type in ["float", "int", "uint"]:
250 % for width in [32, 64]:
251 struct ${type}${width}_vec {
267 % for name, op in sorted(opcodes.iteritems()):
268 static nir_const_value
269 evaluate_${name}(MAYBE_UNUSED unsigned num_components, unsigned bit_size,
270 MAYBE_UNUSED nir_const_value *_src)
272 nir_const_value _dst_val = { {0, } };
275 % for bit_size in [32, 64]:
278 output_type = type_add_size(op.output_type, bit_size)
279 input_types = [type_add_size(type_, bit_size) for type_ in op.input_types]
282 ## For each non-per-component input, create a variable srcN that
283 ## contains x, y, z, and w elements which are filled in with the
284 ## appropriately-typed values.
285 % for j in range(op.num_inputs):
286 % if op.input_sizes[j] == 0:
288 % elif "src" + str(j) not in op.const_expr:
289 ## Avoid unused variable warnings
293 const struct ${input_types[j]}_vec src${j} = {
294 % for k in range(op.input_sizes[j]):
295 % if input_types[j] == "bool32":
296 _src[${j}].u32[${k}] != 0,
298 _src[${j}].${get_const_field(input_types[j])}[${k}],
301 % for k in range(op.input_sizes[j], 4):
307 % if op.output_size == 0:
308 ## For per-component instructions, we need to iterate over the
309 ## components and apply the constant expression one component
311 for (unsigned _i = 0; _i < num_components; _i++) {
312 ## For each per-component input, create a variable srcN that
313 ## contains the value of the current (_i'th) component.
314 % for j in range(op.num_inputs):
315 % if op.input_sizes[j] != 0:
317 % elif "src" + str(j) not in op.const_expr:
318 ## Avoid unused variable warnings
320 % elif input_types[j] == "bool32":
321 const bool src${j} = _src[${j}].u32[_i] != 0;
323 const ${input_types[j]}_t src${j} =
324 _src[${j}].${get_const_field(input_types[j])}[_i];
328 ## Create an appropriately-typed variable dst and assign the
329 ## result of the const_expr to it. If const_expr already contains
330 ## writes to dst, just include const_expr directly.
331 % if "dst" in op.const_expr:
332 ${output_type}_t dst;
336 ${output_type}_t dst = ${op.const_expr};
339 ## Store the current component of the actual destination to the
341 % if output_type == "bool32":
342 ## Sanitize the C value to a proper NIR bool
343 _dst_val.u32[_i] = dst ? NIR_TRUE : NIR_FALSE;
345 _dst_val.${get_const_field(output_type)}[_i] = dst;
349 ## In the non-per-component case, create a struct dst with
350 ## appropriately-typed elements x, y, z, and w and assign the result
351 ## of the const_expr to all components of dst, or include the
352 ## const_expr directly if it writes to dst already.
353 struct ${output_type}_vec dst;
355 % if "dst" in op.const_expr:
358 ## Splat the value to all components. This way expressions which
359 ## write the same value to all components don't need to explicitly
360 ## write to dest. One such example is fnoise which has a
361 ## const_expr of 0.0f.
362 dst.x = dst.y = dst.z = dst.w = ${op.const_expr};
365 ## For each component in the destination, copy the value of dst to
366 ## the actual destination.
367 % for k in range(op.output_size):
368 % if output_type == "bool32":
369 ## Sanitize the C value to a proper NIR bool
370 _dst_val.u32[${k}] = dst.${"xyzw"[k]} ? NIR_TRUE : NIR_FALSE;
372 _dst_val.${get_const_field(output_type)}[${k}] = dst.${"xyzw"[k]};
382 unreachable("unknown bit width");
390 nir_eval_const_opcode(nir_op op, unsigned num_components,
391 unsigned bit_width, nir_const_value *src)
394 % for name in sorted(opcodes.iterkeys()):
396 return evaluate_${name}(num_components, bit_width, src);
399 unreachable("shouldn't get here");
403 from nir_opcodes
import opcodes
404 from mako
.template
import Template
406 print Template(template
).render(opcodes
=opcodes
, type_sizes
=type_sizes
,
407 type_has_size
=type_has_size
,
408 type_add_size
=type_add_size
,
409 get_const_field
=get_const_field
)