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(_MSC_VER) && (_MSC_VER < 1800)
35 static int isnormal(double x)
37 return _fpclass(x) == _FPCLASS_NN || _fpclass(x) == _FPCLASS_PN;
39 #elif defined(__SUNPRO_CC)
41 static int isnormal(double x)
43 return fpclass(x) == FP_NORMAL;
48 static double copysign(double x, double y)
50 return _copysign(x, y);
55 * Evaluate one component of packSnorm4x8.
58 pack_snorm_1x8(float x)
60 /* From section 8.4 of the GLSL 4.30 spec:
64 * The conversion for component c of v to fixed point is done as
67 * packSnorm4x8: round(clamp(c, -1, +1) * 127.0)
69 * We must first cast the float to an int, because casting a negative
70 * float to a uint is undefined.
72 return (uint8_t) (int)
73 _mesa_roundevenf(CLAMP(x, -1.0f, +1.0f) * 127.0f);
77 * Evaluate one component of packSnorm2x16.
80 pack_snorm_1x16(float x)
82 /* From section 8.4 of the GLSL ES 3.00 spec:
86 * The conversion for component c of v to fixed point is done as
89 * packSnorm2x16: round(clamp(c, -1, +1) * 32767.0)
91 * We must first cast the float to an int, because casting a negative
92 * float to a uint is undefined.
94 return (uint16_t) (int)
95 _mesa_roundevenf(CLAMP(x, -1.0f, +1.0f) * 32767.0f);
99 * Evaluate one component of unpackSnorm4x8.
102 unpack_snorm_1x8(uint8_t u)
104 /* From section 8.4 of the GLSL 4.30 spec:
108 * The conversion for unpacked fixed-point value f to floating point is
111 * unpackSnorm4x8: clamp(f / 127.0, -1, +1)
113 return CLAMP((int8_t) u / 127.0f, -1.0f, +1.0f);
117 * Evaluate one component of unpackSnorm2x16.
120 unpack_snorm_1x16(uint16_t u)
122 /* From section 8.4 of the GLSL ES 3.00 spec:
126 * The conversion for unpacked fixed-point value f to floating point is
129 * unpackSnorm2x16: clamp(f / 32767.0, -1, +1)
131 return CLAMP((int16_t) u / 32767.0f, -1.0f, +1.0f);
135 * Evaluate one component packUnorm4x8.
138 pack_unorm_1x8(float x)
140 /* From section 8.4 of the GLSL 4.30 spec:
144 * The conversion for component c of v to fixed point is done as
147 * packUnorm4x8: round(clamp(c, 0, +1) * 255.0)
149 return (uint8_t) (int)
150 _mesa_roundevenf(CLAMP(x, 0.0f, 1.0f) * 255.0f);
154 * Evaluate one component packUnorm2x16.
157 pack_unorm_1x16(float x)
159 /* From section 8.4 of the GLSL ES 3.00 spec:
163 * The conversion for component c of v to fixed point is done as
166 * packUnorm2x16: round(clamp(c, 0, +1) * 65535.0)
168 return (uint16_t) (int)
169 _mesa_roundevenf(CLAMP(x, 0.0f, 1.0f) * 65535.0f);
173 * Evaluate one component of unpackUnorm4x8.
176 unpack_unorm_1x8(uint8_t u)
178 /* From section 8.4 of the GLSL 4.30 spec:
182 * The conversion for unpacked fixed-point value f to floating point is
185 * unpackUnorm4x8: f / 255.0
187 return (float) u / 255.0f;
191 * Evaluate one component of unpackUnorm2x16.
194 unpack_unorm_1x16(uint16_t u)
196 /* From section 8.4 of the GLSL ES 3.00 spec:
200 * The conversion for unpacked fixed-point value f to floating point is
203 * unpackUnorm2x16: f / 65535.0
205 return (float) u / 65535.0f;
209 * Evaluate one component of packHalf2x16.
212 pack_half_1x16(float x)
214 return _mesa_float_to_half(x);
218 * Evaluate one component of unpackHalf2x16.
221 unpack_half_1x16(uint16_t u)
223 return _mesa_half_to_float(u);
226 /* Some typed vector structures to make things like src0.y work */
227 % for type in ["float", "int", "unsigned", "bool"]:
236 % for name, op in sorted(opcodes.iteritems()):
237 static nir_const_value
238 evaluate_${name}(unsigned num_components, nir_const_value *_src)
240 nir_const_value _dst_val = { { {0, 0, 0, 0} } };
242 ## For each non-per-component input, create a variable srcN that
243 ## contains x, y, z, and w elements which are filled in with the
244 ## appropriately-typed values.
245 % for j in range(op.num_inputs):
246 % if op.input_sizes[j] == 0:
248 % elif "src" + str(j) not in op.const_expr:
249 ## Avoid unused variable warnings
253 struct ${op.input_types[j]}_vec src${j} = {
254 % for k in range(op.input_sizes[j]):
255 % if op.input_types[j] == "bool":
256 _src[${j}].u[${k}] != 0,
258 _src[${j}].${op.input_types[j][:1]}[${k}],
264 % if op.output_size == 0:
265 ## For per-component instructions, we need to iterate over the
266 ## components and apply the constant expression one component
268 for (unsigned _i = 0; _i < num_components; _i++) {
269 ## For each per-component input, create a variable srcN that
270 ## contains the value of the current (_i'th) component.
271 % for j in range(op.num_inputs):
272 % if op.input_sizes[j] != 0:
274 % elif "src" + str(j) not in op.const_expr:
275 ## Avoid unused variable warnings
277 % elif op.input_types[j] == "bool":
278 bool src${j} = _src[${j}].u[_i] != 0;
280 ${op.input_types[j]} src${j} = _src[${j}].${op.input_types[j][:1]}[_i];
284 ## Create an appropriately-typed variable dst and assign the
285 ## result of the const_expr to it. If const_expr already contains
286 ## writes to dst, just include const_expr directly.
287 % if "dst" in op.const_expr:
288 ${op.output_type} dst;
291 ${op.output_type} dst = ${op.const_expr};
294 ## Store the current component of the actual destination to the
296 % if op.output_type == "bool":
297 ## Sanitize the C value to a proper NIR bool
298 _dst_val.u[_i] = dst ? NIR_TRUE : NIR_FALSE;
300 _dst_val.${op.output_type[:1]}[_i] = dst;
304 ## In the non-per-component case, create a struct dst with
305 ## appropriately-typed elements x, y, z, and w and assign the result
306 ## of the const_expr to all components of dst, or include the
307 ## const_expr directly if it writes to dst already.
308 struct ${op.output_type}_vec dst;
310 % if "dst" in op.const_expr:
313 ## Splat the value to all components. This way expressions which
314 ## write the same value to all components don't need to explicitly
315 ## write to dest. One such example is fnoise which has a
316 ## const_expr of 0.0f.
317 dst.x = dst.y = dst.z = dst.w = ${op.const_expr};
320 ## For each component in the destination, copy the value of dst to
321 ## the actual destination.
322 % for k in range(op.output_size):
323 % if op.output_type == "bool":
324 ## Sanitize the C value to a proper NIR bool
325 _dst_val.u[${k}] = dst.${"xyzw"[k]} ? NIR_TRUE : NIR_FALSE;
327 _dst_val.${op.output_type[:1]}[${k}] = dst.${"xyzw"[k]};
337 nir_eval_const_opcode(nir_op op, unsigned num_components,
338 nir_const_value *src)
341 % for name in sorted(opcodes.iterkeys()):
342 case nir_op_${name}: {
343 return evaluate_${name}(num_components, src);
348 unreachable("shouldn't get here");
352 from nir_opcodes
import opcodes
353 from mako
.template
import Template
355 print Template(template
).render(opcodes
=opcodes
)