db5bde2b82d29edd9aaa2e7a36b957d14f97b4bd
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_
)]
16 elif type_
== 'float':
19 return [8, 16, 32, 64]
21 def type_add_size(type_
, size
):
22 if type_has_size(type_
):
24 return type_
+ str(size
)
28 if not type_has_size(op
.output_type
):
29 sizes
= set(type_sizes(op
.output_type
))
31 for input_type
in op
.input_types
:
32 if not type_has_size(input_type
):
34 sizes
= set(type_sizes(input_type
))
36 sizes
= sizes
.intersection(set(type_sizes(input_type
)))
38 return sorted(list(sizes
)) if sizes
is not None else None
40 def get_const_field(type_
):
43 elif type_
== "float16":
46 m
= type_split_re
.match(type_
)
48 raise Exception(str(type_
))
49 return m
.group('type')[0] + m
.group('bits')
53 * Copyright (C) 2014 Intel Corporation
55 * Permission is hereby granted, free of charge, to any person obtaining a
56 * copy of this software and associated documentation files (the "Software"),
57 * to deal in the Software without restriction, including without limitation
58 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
59 * and/or sell copies of the Software, and to permit persons to whom the
60 * Software is furnished to do so, subject to the following conditions:
62 * The above copyright notice and this permission notice (including the next
63 * paragraph) shall be included in all copies or substantial portions of the
66 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
67 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
68 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
69 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
70 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
71 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
75 * Jason Ekstrand (jason@jlekstrand.net)
79 #include "util/rounding.h" /* for _mesa_roundeven */
80 #include "util/half_float.h"
81 #include "nir_constant_expressions.h"
84 * Evaluate one component of packSnorm4x8.
87 pack_snorm_1x8(float x)
89 /* From section 8.4 of the GLSL 4.30 spec:
93 * The conversion for component c of v to fixed point is done as
96 * packSnorm4x8: round(clamp(c, -1, +1) * 127.0)
98 * We must first cast the float to an int, because casting a negative
99 * float to a uint is undefined.
101 return (uint8_t) (int)
102 _mesa_roundevenf(CLAMP(x, -1.0f, +1.0f) * 127.0f);
106 * Evaluate one component of packSnorm2x16.
109 pack_snorm_1x16(float x)
111 /* From section 8.4 of the GLSL ES 3.00 spec:
115 * The conversion for component c of v to fixed point is done as
118 * packSnorm2x16: round(clamp(c, -1, +1) * 32767.0)
120 * We must first cast the float to an int, because casting a negative
121 * float to a uint is undefined.
123 return (uint16_t) (int)
124 _mesa_roundevenf(CLAMP(x, -1.0f, +1.0f) * 32767.0f);
128 * Evaluate one component of unpackSnorm4x8.
131 unpack_snorm_1x8(uint8_t u)
133 /* From section 8.4 of the GLSL 4.30 spec:
137 * The conversion for unpacked fixed-point value f to floating point is
140 * unpackSnorm4x8: clamp(f / 127.0, -1, +1)
142 return CLAMP((int8_t) u / 127.0f, -1.0f, +1.0f);
146 * Evaluate one component of unpackSnorm2x16.
149 unpack_snorm_1x16(uint16_t u)
151 /* From section 8.4 of the GLSL ES 3.00 spec:
155 * The conversion for unpacked fixed-point value f to floating point is
158 * unpackSnorm2x16: clamp(f / 32767.0, -1, +1)
160 return CLAMP((int16_t) u / 32767.0f, -1.0f, +1.0f);
164 * Evaluate one component packUnorm4x8.
167 pack_unorm_1x8(float x)
169 /* From section 8.4 of the GLSL 4.30 spec:
173 * The conversion for component c of v to fixed point is done as
176 * packUnorm4x8: round(clamp(c, 0, +1) * 255.0)
178 return (uint8_t) (int)
179 _mesa_roundevenf(CLAMP(x, 0.0f, 1.0f) * 255.0f);
183 * Evaluate one component packUnorm2x16.
186 pack_unorm_1x16(float x)
188 /* From section 8.4 of the GLSL ES 3.00 spec:
192 * The conversion for component c of v to fixed point is done as
195 * packUnorm2x16: round(clamp(c, 0, +1) * 65535.0)
197 return (uint16_t) (int)
198 _mesa_roundevenf(CLAMP(x, 0.0f, 1.0f) * 65535.0f);
202 * Evaluate one component of unpackUnorm4x8.
205 unpack_unorm_1x8(uint8_t u)
207 /* From section 8.4 of the GLSL 4.30 spec:
211 * The conversion for unpacked fixed-point value f to floating point is
214 * unpackUnorm4x8: f / 255.0
216 return (float) u / 255.0f;
220 * Evaluate one component of unpackUnorm2x16.
223 unpack_unorm_1x16(uint16_t u)
225 /* From section 8.4 of the GLSL ES 3.00 spec:
229 * The conversion for unpacked fixed-point value f to floating point is
232 * unpackUnorm2x16: f / 65535.0
234 return (float) u / 65535.0f;
238 * Evaluate one component of packHalf2x16.
241 pack_half_1x16(float x)
243 return _mesa_float_to_half(x);
247 * Evaluate one component of unpackHalf2x16.
250 unpack_half_1x16(uint16_t u)
252 return _mesa_half_to_float(u);
255 /* Some typed vector structures to make things like src0.y work */
256 typedef float float16_t;
257 typedef float float32_t;
258 typedef double float64_t;
259 typedef bool bool32_t;
260 % for type in ["float", "int", "uint"]:
261 % for width in type_sizes(type):
262 struct ${type}${width}_vec {
278 <%def name="evaluate_op(op, bit_size)">
280 output_type = type_add_size(op.output_type, bit_size)
281 input_types = [type_add_size(type_, bit_size) for type_ in op.input_types]
284 ## For each non-per-component input, create a variable srcN that
285 ## contains x, y, z, and w elements which are filled in with the
286 ## appropriately-typed values.
287 % for j in range(op.num_inputs):
288 % if op.input_sizes[j] == 0:
290 % elif "src" + str(j) not in op.const_expr:
291 ## Avoid unused variable warnings
295 const struct ${input_types[j]}_vec src${j} = {
296 % for k in range(op.input_sizes[j]):
297 % if input_types[j] == "bool32":
298 _src[${j}].u32[${k}] != 0,
299 % elif input_types[j] == "float16":
300 _mesa_half_to_float(_src[${j}].u16[${k}]),
302 _src[${j}].${get_const_field(input_types[j])}[${k}],
305 % for k in range(op.input_sizes[j], 4):
311 % if op.output_size == 0:
312 ## For per-component instructions, we need to iterate over the
313 ## components and apply the constant expression one component
315 for (unsigned _i = 0; _i < num_components; _i++) {
316 ## For each per-component input, create a variable srcN that
317 ## contains the value of the current (_i'th) component.
318 % for j in range(op.num_inputs):
319 % if op.input_sizes[j] != 0:
321 % elif "src" + str(j) not in op.const_expr:
322 ## Avoid unused variable warnings
324 % elif input_types[j] == "bool32":
325 const bool src${j} = _src[${j}].u32[_i] != 0;
326 % elif input_types[j] == "float16":
327 const float src${j} =
328 _mesa_half_to_float(_src[${j}].u16[_i]);
330 const ${input_types[j]}_t src${j} =
331 _src[${j}].${get_const_field(input_types[j])}[_i];
335 ## Create an appropriately-typed variable dst and assign the
336 ## result of the const_expr to it. If const_expr already contains
337 ## writes to dst, just include const_expr directly.
338 % if "dst" in op.const_expr:
339 ${output_type}_t dst;
343 ${output_type}_t dst = ${op.const_expr};
346 ## Store the current component of the actual destination to the
348 % if output_type == "bool32":
349 ## Sanitize the C value to a proper NIR bool
350 _dst_val.u32[_i] = dst ? NIR_TRUE : NIR_FALSE;
351 % elif output_type == "float16":
352 _dst_val.u16[_i] = _mesa_float_to_half(dst);
354 _dst_val.${get_const_field(output_type)}[_i] = dst;
358 ## In the non-per-component case, create a struct dst with
359 ## appropriately-typed elements x, y, z, and w and assign the result
360 ## of the const_expr to all components of dst, or include the
361 ## const_expr directly if it writes to dst already.
362 struct ${output_type}_vec dst;
364 % if "dst" in op.const_expr:
367 ## Splat the value to all components. This way expressions which
368 ## write the same value to all components don't need to explicitly
369 ## write to dest. One such example is fnoise which has a
370 ## const_expr of 0.0f.
371 dst.x = dst.y = dst.z = dst.w = ${op.const_expr};
374 ## For each component in the destination, copy the value of dst to
375 ## the actual destination.
376 % for k in range(op.output_size):
377 % if output_type == "bool32":
378 ## Sanitize the C value to a proper NIR bool
379 _dst_val.u32[${k}] = dst.${"xyzw"[k]} ? NIR_TRUE : NIR_FALSE;
380 % elif output_type == "float16":
381 _dst_val.u16[${k}] = _mesa_float_to_half(dst.${"xyzw"[k]});
383 _dst_val.${get_const_field(output_type)}[${k}] = dst.${"xyzw"[k]};
389 % for name, op in sorted(opcodes.iteritems()):
390 static nir_const_value
391 evaluate_${name}(MAYBE_UNUSED unsigned num_components,
392 ${"UNUSED" if op_bit_sizes(op) is None else ""} unsigned bit_size,
393 MAYBE_UNUSED nir_const_value *_src)
395 nir_const_value _dst_val = { {0, } };
397 % if op_bit_sizes(op) is not None:
399 % for bit_size in op_bit_sizes(op):
401 ${evaluate_op(op, bit_size)}
407 unreachable("unknown bit width");
410 ${evaluate_op(op, 0)}
418 nir_eval_const_opcode(nir_op op, unsigned num_components,
419 unsigned bit_width, nir_const_value *src)
422 % for name in sorted(opcodes.iterkeys()):
424 return evaluate_${name}(num_components, bit_width, src);
427 unreachable("shouldn't get here");
431 from nir_opcodes
import opcodes
432 from mako
.template
import Template
434 print Template(template
).render(opcodes
=opcodes
, type_sizes
=type_sizes
,
435 type_has_size
=type_has_size
,
436 type_add_size
=type_add_size
,
437 op_bit_sizes
=op_bit_sizes
,
438 get_const_field
=get_const_field
)