ad841e3d31118e2d535fda587c5f3d8b672a0982
4 type_split_re
= re
.compile(r
'(?P<type>[a-z]+)(?P<bits>\d+)')
6 def type_has_size(type_
):
7 return type_
[-1:].isdigit()
10 assert type_has_size(type_
)
11 return int(type_split_re
.match(type_
).group('bits'))
13 def type_sizes(type_
):
14 if type_has_size(type_
):
15 return [type_size(type_
)]
19 def type_add_size(type_
, size
):
20 if type_has_size(type_
):
22 return type_
+ str(size
)
25 sizes
= set([8, 16, 32, 64])
26 if not type_has_size(op
.output_type
):
27 sizes
= sizes
.intersection(set(type_sizes(op
.output_type
)))
28 for input_type
in op
.input_types
:
29 if not type_has_size(input_type
):
30 sizes
= sizes
.intersection(set(type_sizes(input_type
)))
31 return sorted(list(sizes
))
33 def get_const_field(type_
):
37 m
= type_split_re
.match(type_
)
39 raise Exception(str(type_
))
40 return m
.group('type')[0] + m
.group('bits')
44 * Copyright (C) 2014 Intel Corporation
46 * Permission is hereby granted, free of charge, to any person obtaining a
47 * copy of this software and associated documentation files (the "Software"),
48 * to deal in the Software without restriction, including without limitation
49 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
50 * and/or sell copies of the Software, and to permit persons to whom the
51 * Software is furnished to do so, subject to the following conditions:
53 * The above copyright notice and this permission notice (including the next
54 * paragraph) shall be included in all copies or substantial portions of the
57 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
58 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
59 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
60 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
61 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
62 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
66 * Jason Ekstrand (jason@jlekstrand.net)
70 #include "main/core.h"
71 #include "util/rounding.h" /* for _mesa_roundeven */
72 #include "util/half_float.h"
73 #include "nir_constant_expressions.h"
76 * Evaluate one component of packSnorm4x8.
79 pack_snorm_1x8(float x)
81 /* From section 8.4 of the GLSL 4.30 spec:
85 * The conversion for component c of v to fixed point is done as
88 * packSnorm4x8: round(clamp(c, -1, +1) * 127.0)
90 * We must first cast the float to an int, because casting a negative
91 * float to a uint is undefined.
93 return (uint8_t) (int)
94 _mesa_roundevenf(CLAMP(x, -1.0f, +1.0f) * 127.0f);
98 * Evaluate one component of packSnorm2x16.
101 pack_snorm_1x16(float x)
103 /* From section 8.4 of the GLSL ES 3.00 spec:
107 * The conversion for component c of v to fixed point is done as
110 * packSnorm2x16: round(clamp(c, -1, +1) * 32767.0)
112 * We must first cast the float to an int, because casting a negative
113 * float to a uint is undefined.
115 return (uint16_t) (int)
116 _mesa_roundevenf(CLAMP(x, -1.0f, +1.0f) * 32767.0f);
120 * Evaluate one component of unpackSnorm4x8.
123 unpack_snorm_1x8(uint8_t u)
125 /* From section 8.4 of the GLSL 4.30 spec:
129 * The conversion for unpacked fixed-point value f to floating point is
132 * unpackSnorm4x8: clamp(f / 127.0, -1, +1)
134 return CLAMP((int8_t) u / 127.0f, -1.0f, +1.0f);
138 * Evaluate one component of unpackSnorm2x16.
141 unpack_snorm_1x16(uint16_t u)
143 /* From section 8.4 of the GLSL ES 3.00 spec:
147 * The conversion for unpacked fixed-point value f to floating point is
150 * unpackSnorm2x16: clamp(f / 32767.0, -1, +1)
152 return CLAMP((int16_t) u / 32767.0f, -1.0f, +1.0f);
156 * Evaluate one component packUnorm4x8.
159 pack_unorm_1x8(float x)
161 /* From section 8.4 of the GLSL 4.30 spec:
165 * The conversion for component c of v to fixed point is done as
168 * packUnorm4x8: round(clamp(c, 0, +1) * 255.0)
170 return (uint8_t) (int)
171 _mesa_roundevenf(CLAMP(x, 0.0f, 1.0f) * 255.0f);
175 * Evaluate one component packUnorm2x16.
178 pack_unorm_1x16(float x)
180 /* From section 8.4 of the GLSL ES 3.00 spec:
184 * The conversion for component c of v to fixed point is done as
187 * packUnorm2x16: round(clamp(c, 0, +1) * 65535.0)
189 return (uint16_t) (int)
190 _mesa_roundevenf(CLAMP(x, 0.0f, 1.0f) * 65535.0f);
194 * Evaluate one component of unpackUnorm4x8.
197 unpack_unorm_1x8(uint8_t u)
199 /* From section 8.4 of the GLSL 4.30 spec:
203 * The conversion for unpacked fixed-point value f to floating point is
206 * unpackUnorm4x8: f / 255.0
208 return (float) u / 255.0f;
212 * Evaluate one component of unpackUnorm2x16.
215 unpack_unorm_1x16(uint16_t u)
217 /* From section 8.4 of the GLSL ES 3.00 spec:
221 * The conversion for unpacked fixed-point value f to floating point is
224 * unpackUnorm2x16: f / 65535.0
226 return (float) u / 65535.0f;
230 * Evaluate one component of packHalf2x16.
233 pack_half_1x16(float x)
235 return _mesa_float_to_half(x);
239 * Evaluate one component of unpackHalf2x16.
242 unpack_half_1x16(uint16_t u)
244 return _mesa_half_to_float(u);
247 /* Some typed vector structures to make things like src0.y work */
248 typedef float float32_t;
249 typedef double float64_t;
250 typedef bool bool32_t;
251 % for type in ["float", "int", "uint"]:
252 % for width in type_sizes(type):
253 struct ${type}${width}_vec {
269 <%def name="evaluate_op(op, bit_size)">
271 output_type = type_add_size(op.output_type, bit_size)
272 input_types = [type_add_size(type_, bit_size) for type_ in op.input_types]
275 ## For each non-per-component input, create a variable srcN that
276 ## contains x, y, z, and w elements which are filled in with the
277 ## appropriately-typed values.
278 % for j in range(op.num_inputs):
279 % if op.input_sizes[j] == 0:
281 % elif "src" + str(j) not in op.const_expr:
282 ## Avoid unused variable warnings
286 const struct ${input_types[j]}_vec src${j} = {
287 % for k in range(op.input_sizes[j]):
288 % if input_types[j] == "bool32":
289 _src[${j}].u32[${k}] != 0,
291 _src[${j}].${get_const_field(input_types[j])}[${k}],
294 % for k in range(op.input_sizes[j], 4):
300 % if op.output_size == 0:
301 ## For per-component instructions, we need to iterate over the
302 ## components and apply the constant expression one component
304 for (unsigned _i = 0; _i < num_components; _i++) {
305 ## For each per-component input, create a variable srcN that
306 ## contains the value of the current (_i'th) component.
307 % for j in range(op.num_inputs):
308 % if op.input_sizes[j] != 0:
310 % elif "src" + str(j) not in op.const_expr:
311 ## Avoid unused variable warnings
313 % elif input_types[j] == "bool32":
314 const bool src${j} = _src[${j}].u32[_i] != 0;
316 const ${input_types[j]}_t src${j} =
317 _src[${j}].${get_const_field(input_types[j])}[_i];
321 ## Create an appropriately-typed variable dst and assign the
322 ## result of the const_expr to it. If const_expr already contains
323 ## writes to dst, just include const_expr directly.
324 % if "dst" in op.const_expr:
325 ${output_type}_t dst;
329 ${output_type}_t dst = ${op.const_expr};
332 ## Store the current component of the actual destination to the
334 % if output_type == "bool32":
335 ## Sanitize the C value to a proper NIR bool
336 _dst_val.u32[_i] = dst ? NIR_TRUE : NIR_FALSE;
338 _dst_val.${get_const_field(output_type)}[_i] = dst;
342 ## In the non-per-component case, create a struct dst with
343 ## appropriately-typed elements x, y, z, and w and assign the result
344 ## of the const_expr to all components of dst, or include the
345 ## const_expr directly if it writes to dst already.
346 struct ${output_type}_vec dst;
348 % if "dst" in op.const_expr:
351 ## Splat the value to all components. This way expressions which
352 ## write the same value to all components don't need to explicitly
353 ## write to dest. One such example is fnoise which has a
354 ## const_expr of 0.0f.
355 dst.x = dst.y = dst.z = dst.w = ${op.const_expr};
358 ## For each component in the destination, copy the value of dst to
359 ## the actual destination.
360 % for k in range(op.output_size):
361 % if output_type == "bool32":
362 ## Sanitize the C value to a proper NIR bool
363 _dst_val.u32[${k}] = dst.${"xyzw"[k]} ? NIR_TRUE : NIR_FALSE;
365 _dst_val.${get_const_field(output_type)}[${k}] = dst.${"xyzw"[k]};
371 % for name, op in sorted(opcodes.iteritems()):
372 static nir_const_value
373 evaluate_${name}(MAYBE_UNUSED unsigned num_components, unsigned bit_size,
374 MAYBE_UNUSED nir_const_value *_src)
376 nir_const_value _dst_val = { {0, } };
379 % for bit_size in op_bit_sizes(op):
381 ${evaluate_op(op, bit_size)}
387 unreachable("unknown bit width");
395 nir_eval_const_opcode(nir_op op, unsigned num_components,
396 unsigned bit_width, nir_const_value *src)
399 % for name in sorted(opcodes.iterkeys()):
401 return evaluate_${name}(num_components, bit_width, src);
404 unreachable("shouldn't get here");
408 from nir_opcodes
import opcodes
409 from mako
.template
import Template
411 print Template(template
).render(opcodes
=opcodes
, type_sizes
=type_sizes
,
412 type_has_size
=type_has_size
,
413 type_add_size
=type_add_size
,
414 op_bit_sizes
=op_bit_sizes
,
415 get_const_field
=get_const_field
)