4 * Copyright (C) 2014 Intel Corporation
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26 * Jason Ekstrand (jason@jlekstrand.net)
30 #include "main/core.h"
31 #include "util/rounding.h" /* for _mesa_roundeven */
32 #include "nir_constant_expressions.h"
34 #if defined(__SUNPRO_CC)
36 static int isnormal(double x)
38 return fpclass(x) == FP_NORMAL;
43 * Evaluate one component of packSnorm4x8.
46 pack_snorm_1x8(float x)
48 /* From section 8.4 of the GLSL 4.30 spec:
52 * The conversion for component c of v to fixed point is done as
55 * packSnorm4x8: round(clamp(c, -1, +1) * 127.0)
57 * We must first cast the float to an int, because casting a negative
58 * float to a uint is undefined.
60 return (uint8_t) (int)
61 _mesa_roundevenf(CLAMP(x, -1.0f, +1.0f) * 127.0f);
65 * Evaluate one component of packSnorm2x16.
68 pack_snorm_1x16(float x)
70 /* From section 8.4 of the GLSL ES 3.00 spec:
74 * The conversion for component c of v to fixed point is done as
77 * packSnorm2x16: round(clamp(c, -1, +1) * 32767.0)
79 * We must first cast the float to an int, because casting a negative
80 * float to a uint is undefined.
82 return (uint16_t) (int)
83 _mesa_roundevenf(CLAMP(x, -1.0f, +1.0f) * 32767.0f);
87 * Evaluate one component of unpackSnorm4x8.
90 unpack_snorm_1x8(uint8_t u)
92 /* From section 8.4 of the GLSL 4.30 spec:
96 * The conversion for unpacked fixed-point value f to floating point is
99 * unpackSnorm4x8: clamp(f / 127.0, -1, +1)
101 return CLAMP((int8_t) u / 127.0f, -1.0f, +1.0f);
105 * Evaluate one component of unpackSnorm2x16.
108 unpack_snorm_1x16(uint16_t u)
110 /* From section 8.4 of the GLSL ES 3.00 spec:
114 * The conversion for unpacked fixed-point value f to floating point is
117 * unpackSnorm2x16: clamp(f / 32767.0, -1, +1)
119 return CLAMP((int16_t) u / 32767.0f, -1.0f, +1.0f);
123 * Evaluate one component packUnorm4x8.
126 pack_unorm_1x8(float x)
128 /* From section 8.4 of the GLSL 4.30 spec:
132 * The conversion for component c of v to fixed point is done as
135 * packUnorm4x8: round(clamp(c, 0, +1) * 255.0)
137 return (uint8_t) (int)
138 _mesa_roundevenf(CLAMP(x, 0.0f, 1.0f) * 255.0f);
142 * Evaluate one component packUnorm2x16.
145 pack_unorm_1x16(float x)
147 /* From section 8.4 of the GLSL ES 3.00 spec:
151 * The conversion for component c of v to fixed point is done as
154 * packUnorm2x16: round(clamp(c, 0, +1) * 65535.0)
156 return (uint16_t) (int)
157 _mesa_roundevenf(CLAMP(x, 0.0f, 1.0f) * 65535.0f);
161 * Evaluate one component of unpackUnorm4x8.
164 unpack_unorm_1x8(uint8_t u)
166 /* From section 8.4 of the GLSL 4.30 spec:
170 * The conversion for unpacked fixed-point value f to floating point is
173 * unpackUnorm4x8: f / 255.0
175 return (float) u / 255.0f;
179 * Evaluate one component of unpackUnorm2x16.
182 unpack_unorm_1x16(uint16_t u)
184 /* From section 8.4 of the GLSL ES 3.00 spec:
188 * The conversion for unpacked fixed-point value f to floating point is
191 * unpackUnorm2x16: f / 65535.0
193 return (float) u / 65535.0f;
197 * Evaluate one component of packHalf2x16.
200 pack_half_1x16(float x)
202 return _mesa_float_to_half(x);
206 * Evaluate one component of unpackHalf2x16.
209 unpack_half_1x16(uint16_t u)
211 return _mesa_half_to_float(u);
214 /* Some typed vector structures to make things like src0.y work */
215 % for type in ["float", "int", "unsigned", "bool"]:
224 % for name, op in sorted(opcodes.iteritems()):
225 static nir_const_value
226 evaluate_${name}(unsigned num_components, nir_const_value *_src)
228 nir_const_value _dst_val = { { {0, 0, 0, 0} } };
230 ## For each non-per-component input, create a variable srcN that
231 ## contains x, y, z, and w elements which are filled in with the
232 ## appropriately-typed values.
233 % for j in range(op.num_inputs):
234 % if op.input_sizes[j] == 0:
236 % elif "src" + str(j) not in op.const_expr:
237 ## Avoid unused variable warnings
241 struct ${op.input_types[j]}_vec src${j} = {
242 % for k in range(op.input_sizes[j]):
243 % if op.input_types[j] == "bool":
244 _src[${j}].u[${k}] != 0,
246 _src[${j}].${op.input_types[j][:1]}[${k}],
252 % if op.output_size == 0:
253 ## For per-component instructions, we need to iterate over the
254 ## components and apply the constant expression one component
256 for (unsigned _i = 0; _i < num_components; _i++) {
257 ## For each per-component input, create a variable srcN that
258 ## contains the value of the current (_i'th) component.
259 % for j in range(op.num_inputs):
260 % if op.input_sizes[j] != 0:
262 % elif "src" + str(j) not in op.const_expr:
263 ## Avoid unused variable warnings
265 % elif op.input_types[j] == "bool":
266 bool src${j} = _src[${j}].u[_i] != 0;
268 ${op.input_types[j]} src${j} = _src[${j}].${op.input_types[j][:1]}[_i];
272 ## Create an appropriately-typed variable dst and assign the
273 ## result of the const_expr to it. If const_expr already contains
274 ## writes to dst, just include const_expr directly.
275 % if "dst" in op.const_expr:
276 ${op.output_type} dst;
279 ${op.output_type} dst = ${op.const_expr};
282 ## Store the current component of the actual destination to the
284 % if op.output_type == "bool":
285 ## Sanitize the C value to a proper NIR bool
286 _dst_val.u[_i] = dst ? NIR_TRUE : NIR_FALSE;
288 _dst_val.${op.output_type[:1]}[_i] = dst;
292 ## In the non-per-component case, create a struct dst with
293 ## appropriately-typed elements x, y, z, and w and assign the result
294 ## of the const_expr to all components of dst, or include the
295 ## const_expr directly if it writes to dst already.
296 struct ${op.output_type}_vec dst;
298 % if "dst" in op.const_expr:
301 ## Splat the value to all components. This way expressions which
302 ## write the same value to all components don't need to explicitly
303 ## write to dest. One such example is fnoise which has a
304 ## const_expr of 0.0f.
305 dst.x = dst.y = dst.z = dst.w = ${op.const_expr};
308 ## For each component in the destination, copy the value of dst to
309 ## the actual destination.
310 % for k in range(op.output_size):
311 % if op.output_type == "bool":
312 ## Sanitize the C value to a proper NIR bool
313 _dst_val.u[${k}] = dst.${"xyzw"[k]} ? NIR_TRUE : NIR_FALSE;
315 _dst_val.${op.output_type[:1]}[${k}] = dst.${"xyzw"[k]};
325 nir_eval_const_opcode(nir_op op, unsigned num_components,
326 nir_const_value *src)
329 % for name in sorted(opcodes.iterkeys()):
330 case nir_op_${name}: {
331 return evaluate_${name}(num_components, src);
336 unreachable("shouldn't get here");
340 from nir_opcodes
import opcodes
341 from mako
.template
import Template
343 print Template(template
).render(opcodes
=opcodes
)