glsl: Use _mesa_bitcount to implement constant ir_unop_bit_count
[mesa.git] / src / compiler / glsl / ir_constant_expression.cpp
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 /**
25 * \file ir_constant_expression.cpp
26 * Evaluate and process constant valued expressions
27 *
28 * In GLSL, constant valued expressions are used in several places. These
29 * must be processed and evaluated very early in the compilation process.
30 *
31 * * Sizes of arrays
32 * * Initializers for uniforms
33 * * Initializers for \c const variables
34 */
35
36 #include <math.h>
37 #include "main/core.h" /* for MAX2, MIN2, CLAMP */
38 #include "util/rounding.h" /* for _mesa_roundeven */
39 #include "util/half_float.h"
40 #include "ir.h"
41 #include "compiler/glsl_types.h"
42 #include "program/hash_table.h"
43
44 static float
45 dot_f(ir_constant *op0, ir_constant *op1)
46 {
47 assert(op0->type->is_float() && op1->type->is_float());
48
49 float result = 0;
50 for (unsigned c = 0; c < op0->type->components(); c++)
51 result += op0->value.f[c] * op1->value.f[c];
52
53 return result;
54 }
55
56 static double
57 dot_d(ir_constant *op0, ir_constant *op1)
58 {
59 assert(op0->type->is_double() && op1->type->is_double());
60
61 double result = 0;
62 for (unsigned c = 0; c < op0->type->components(); c++)
63 result += op0->value.d[c] * op1->value.d[c];
64
65 return result;
66 }
67
68 /* This method is the only one supported by gcc. Unions in particular
69 * are iffy, and read-through-converted-pointer is killed by strict
70 * aliasing. OTOH, the compiler sees through the memcpy, so the
71 * resulting asm is reasonable.
72 */
73 static float
74 bitcast_u2f(unsigned int u)
75 {
76 assert(sizeof(float) == sizeof(unsigned int));
77 float f;
78 memcpy(&f, &u, sizeof(f));
79 return f;
80 }
81
82 static unsigned int
83 bitcast_f2u(float f)
84 {
85 assert(sizeof(float) == sizeof(unsigned int));
86 unsigned int u;
87 memcpy(&u, &f, sizeof(f));
88 return u;
89 }
90
91 /**
92 * Evaluate one component of a floating-point 4x8 unpacking function.
93 */
94 typedef uint8_t
95 (*pack_1x8_func_t)(float);
96
97 /**
98 * Evaluate one component of a floating-point 2x16 unpacking function.
99 */
100 typedef uint16_t
101 (*pack_1x16_func_t)(float);
102
103 /**
104 * Evaluate one component of a floating-point 4x8 unpacking function.
105 */
106 typedef float
107 (*unpack_1x8_func_t)(uint8_t);
108
109 /**
110 * Evaluate one component of a floating-point 2x16 unpacking function.
111 */
112 typedef float
113 (*unpack_1x16_func_t)(uint16_t);
114
115 /**
116 * Evaluate a 2x16 floating-point packing function.
117 */
118 static uint32_t
119 pack_2x16(pack_1x16_func_t pack_1x16,
120 float x, float y)
121 {
122 /* From section 8.4 of the GLSL ES 3.00 spec:
123 *
124 * packSnorm2x16
125 * -------------
126 * The first component of the vector will be written to the least
127 * significant bits of the output; the last component will be written to
128 * the most significant bits.
129 *
130 * The specifications for the other packing functions contain similar
131 * language.
132 */
133 uint32_t u = 0;
134 u |= ((uint32_t) pack_1x16(x) << 0);
135 u |= ((uint32_t) pack_1x16(y) << 16);
136 return u;
137 }
138
139 /**
140 * Evaluate a 4x8 floating-point packing function.
141 */
142 static uint32_t
143 pack_4x8(pack_1x8_func_t pack_1x8,
144 float x, float y, float z, float w)
145 {
146 /* From section 8.4 of the GLSL 4.30 spec:
147 *
148 * packSnorm4x8
149 * ------------
150 * The first component of the vector will be written to the least
151 * significant bits of the output; the last component will be written to
152 * the most significant bits.
153 *
154 * The specifications for the other packing functions contain similar
155 * language.
156 */
157 uint32_t u = 0;
158 u |= ((uint32_t) pack_1x8(x) << 0);
159 u |= ((uint32_t) pack_1x8(y) << 8);
160 u |= ((uint32_t) pack_1x8(z) << 16);
161 u |= ((uint32_t) pack_1x8(w) << 24);
162 return u;
163 }
164
165 /**
166 * Evaluate a 2x16 floating-point unpacking function.
167 */
168 static void
169 unpack_2x16(unpack_1x16_func_t unpack_1x16,
170 uint32_t u,
171 float *x, float *y)
172 {
173 /* From section 8.4 of the GLSL ES 3.00 spec:
174 *
175 * unpackSnorm2x16
176 * ---------------
177 * The first component of the returned vector will be extracted from
178 * the least significant bits of the input; the last component will be
179 * extracted from the most significant bits.
180 *
181 * The specifications for the other unpacking functions contain similar
182 * language.
183 */
184 *x = unpack_1x16((uint16_t) (u & 0xffff));
185 *y = unpack_1x16((uint16_t) (u >> 16));
186 }
187
188 /**
189 * Evaluate a 4x8 floating-point unpacking function.
190 */
191 static void
192 unpack_4x8(unpack_1x8_func_t unpack_1x8, uint32_t u,
193 float *x, float *y, float *z, float *w)
194 {
195 /* From section 8.4 of the GLSL 4.30 spec:
196 *
197 * unpackSnorm4x8
198 * --------------
199 * The first component of the returned vector will be extracted from
200 * the least significant bits of the input; the last component will be
201 * extracted from the most significant bits.
202 *
203 * The specifications for the other unpacking functions contain similar
204 * language.
205 */
206 *x = unpack_1x8((uint8_t) (u & 0xff));
207 *y = unpack_1x8((uint8_t) (u >> 8));
208 *z = unpack_1x8((uint8_t) (u >> 16));
209 *w = unpack_1x8((uint8_t) (u >> 24));
210 }
211
212 /**
213 * Evaluate one component of packSnorm4x8.
214 */
215 static uint8_t
216 pack_snorm_1x8(float x)
217 {
218 /* From section 8.4 of the GLSL 4.30 spec:
219 *
220 * packSnorm4x8
221 * ------------
222 * The conversion for component c of v to fixed point is done as
223 * follows:
224 *
225 * packSnorm4x8: round(clamp(c, -1, +1) * 127.0)
226 */
227 return (uint8_t)
228 _mesa_lroundevenf(CLAMP(x, -1.0f, +1.0f) * 127.0f);
229 }
230
231 /**
232 * Evaluate one component of packSnorm2x16.
233 */
234 static uint16_t
235 pack_snorm_1x16(float x)
236 {
237 /* From section 8.4 of the GLSL ES 3.00 spec:
238 *
239 * packSnorm2x16
240 * -------------
241 * The conversion for component c of v to fixed point is done as
242 * follows:
243 *
244 * packSnorm2x16: round(clamp(c, -1, +1) * 32767.0)
245 */
246 return (uint16_t)
247 _mesa_lroundevenf(CLAMP(x, -1.0f, +1.0f) * 32767.0f);
248 }
249
250 /**
251 * Evaluate one component of unpackSnorm4x8.
252 */
253 static float
254 unpack_snorm_1x8(uint8_t u)
255 {
256 /* From section 8.4 of the GLSL 4.30 spec:
257 *
258 * unpackSnorm4x8
259 * --------------
260 * The conversion for unpacked fixed-point value f to floating point is
261 * done as follows:
262 *
263 * unpackSnorm4x8: clamp(f / 127.0, -1, +1)
264 */
265 return CLAMP((int8_t) u / 127.0f, -1.0f, +1.0f);
266 }
267
268 /**
269 * Evaluate one component of unpackSnorm2x16.
270 */
271 static float
272 unpack_snorm_1x16(uint16_t u)
273 {
274 /* From section 8.4 of the GLSL ES 3.00 spec:
275 *
276 * unpackSnorm2x16
277 * ---------------
278 * The conversion for unpacked fixed-point value f to floating point is
279 * done as follows:
280 *
281 * unpackSnorm2x16: clamp(f / 32767.0, -1, +1)
282 */
283 return CLAMP((int16_t) u / 32767.0f, -1.0f, +1.0f);
284 }
285
286 /**
287 * Evaluate one component packUnorm4x8.
288 */
289 static uint8_t
290 pack_unorm_1x8(float x)
291 {
292 /* From section 8.4 of the GLSL 4.30 spec:
293 *
294 * packUnorm4x8
295 * ------------
296 * The conversion for component c of v to fixed point is done as
297 * follows:
298 *
299 * packUnorm4x8: round(clamp(c, 0, +1) * 255.0)
300 */
301 return (uint8_t) (int) _mesa_roundevenf(CLAMP(x, 0.0f, 1.0f) * 255.0f);
302 }
303
304 /**
305 * Evaluate one component packUnorm2x16.
306 */
307 static uint16_t
308 pack_unorm_1x16(float x)
309 {
310 /* From section 8.4 of the GLSL ES 3.00 spec:
311 *
312 * packUnorm2x16
313 * -------------
314 * The conversion for component c of v to fixed point is done as
315 * follows:
316 *
317 * packUnorm2x16: round(clamp(c, 0, +1) * 65535.0)
318 */
319 return (uint16_t) (int)
320 _mesa_roundevenf(CLAMP(x, 0.0f, 1.0f) * 65535.0f);
321 }
322
323 /**
324 * Evaluate one component of unpackUnorm4x8.
325 */
326 static float
327 unpack_unorm_1x8(uint8_t u)
328 {
329 /* From section 8.4 of the GLSL 4.30 spec:
330 *
331 * unpackUnorm4x8
332 * --------------
333 * The conversion for unpacked fixed-point value f to floating point is
334 * done as follows:
335 *
336 * unpackUnorm4x8: f / 255.0
337 */
338 return (float) u / 255.0f;
339 }
340
341 /**
342 * Evaluate one component of unpackUnorm2x16.
343 */
344 static float
345 unpack_unorm_1x16(uint16_t u)
346 {
347 /* From section 8.4 of the GLSL ES 3.00 spec:
348 *
349 * unpackUnorm2x16
350 * ---------------
351 * The conversion for unpacked fixed-point value f to floating point is
352 * done as follows:
353 *
354 * unpackUnorm2x16: f / 65535.0
355 */
356 return (float) u / 65535.0f;
357 }
358
359 /**
360 * Evaluate one component of packHalf2x16.
361 */
362 static uint16_t
363 pack_half_1x16(float x)
364 {
365 return _mesa_float_to_half(x);
366 }
367
368 /**
369 * Evaluate one component of unpackHalf2x16.
370 */
371 static float
372 unpack_half_1x16(uint16_t u)
373 {
374 return _mesa_half_to_float(u);
375 }
376
377 /**
378 * Get the constant that is ultimately referenced by an r-value, in a constant
379 * expression evaluation context.
380 *
381 * The offset is used when the reference is to a specific column of a matrix.
382 */
383 static bool
384 constant_referenced(const ir_dereference *deref,
385 struct hash_table *variable_context,
386 ir_constant *&store, int &offset)
387 {
388 store = NULL;
389 offset = 0;
390
391 if (variable_context == NULL)
392 return false;
393
394 switch (deref->ir_type) {
395 case ir_type_dereference_array: {
396 const ir_dereference_array *const da =
397 (const ir_dereference_array *) deref;
398
399 ir_constant *const index_c =
400 da->array_index->constant_expression_value(variable_context);
401
402 if (!index_c || !index_c->type->is_scalar() || !index_c->type->is_integer())
403 break;
404
405 const int index = index_c->type->base_type == GLSL_TYPE_INT ?
406 index_c->get_int_component(0) :
407 index_c->get_uint_component(0);
408
409 ir_constant *substore;
410 int suboffset;
411
412 const ir_dereference *const deref = da->array->as_dereference();
413 if (!deref)
414 break;
415
416 if (!constant_referenced(deref, variable_context, substore, suboffset))
417 break;
418
419 const glsl_type *const vt = da->array->type;
420 if (vt->is_array()) {
421 store = substore->get_array_element(index);
422 offset = 0;
423 } else if (vt->is_matrix()) {
424 store = substore;
425 offset = index * vt->vector_elements;
426 } else if (vt->is_vector()) {
427 store = substore;
428 offset = suboffset + index;
429 }
430
431 break;
432 }
433
434 case ir_type_dereference_record: {
435 const ir_dereference_record *const dr =
436 (const ir_dereference_record *) deref;
437
438 const ir_dereference *const deref = dr->record->as_dereference();
439 if (!deref)
440 break;
441
442 ir_constant *substore;
443 int suboffset;
444
445 if (!constant_referenced(deref, variable_context, substore, suboffset))
446 break;
447
448 /* Since we're dropping it on the floor...
449 */
450 assert(suboffset == 0);
451
452 store = substore->get_record_field(dr->field);
453 break;
454 }
455
456 case ir_type_dereference_variable: {
457 const ir_dereference_variable *const dv =
458 (const ir_dereference_variable *) deref;
459
460 store = (ir_constant *) hash_table_find(variable_context, dv->var);
461 break;
462 }
463
464 default:
465 assert(!"Should not get here.");
466 break;
467 }
468
469 return store != NULL;
470 }
471
472
473 ir_constant *
474 ir_rvalue::constant_expression_value(struct hash_table *)
475 {
476 assert(this->type->is_error());
477 return NULL;
478 }
479
480 ir_constant *
481 ir_expression::constant_expression_value(struct hash_table *variable_context)
482 {
483 if (this->type->is_error())
484 return NULL;
485
486 ir_constant *op[ARRAY_SIZE(this->operands)] = { NULL, };
487 ir_constant_data data;
488
489 memset(&data, 0, sizeof(data));
490
491 for (unsigned operand = 0; operand < this->get_num_operands(); operand++) {
492 op[operand] = this->operands[operand]->constant_expression_value(variable_context);
493 if (!op[operand])
494 return NULL;
495 }
496
497 if (op[1] != NULL)
498 switch (this->operation) {
499 case ir_binop_lshift:
500 case ir_binop_rshift:
501 case ir_binop_ldexp:
502 case ir_binop_interpolate_at_offset:
503 case ir_binop_interpolate_at_sample:
504 case ir_binop_vector_extract:
505 case ir_triop_csel:
506 case ir_triop_bitfield_extract:
507 break;
508
509 default:
510 assert(op[0]->type->base_type == op[1]->type->base_type);
511 break;
512 }
513
514 bool op0_scalar = op[0]->type->is_scalar();
515 bool op1_scalar = op[1] != NULL && op[1]->type->is_scalar();
516
517 /* When iterating over a vector or matrix's components, we want to increase
518 * the loop counter. However, for scalars, we want to stay at 0.
519 */
520 unsigned c0_inc = op0_scalar ? 0 : 1;
521 unsigned c1_inc = op1_scalar ? 0 : 1;
522 unsigned components;
523 if (op1_scalar || !op[1]) {
524 components = op[0]->type->components();
525 } else {
526 components = op[1]->type->components();
527 }
528
529 void *ctx = ralloc_parent(this);
530
531 /* Handle array operations here, rather than below. */
532 if (op[0]->type->is_array()) {
533 assert(op[1] != NULL && op[1]->type->is_array());
534 switch (this->operation) {
535 case ir_binop_all_equal:
536 return new(ctx) ir_constant(op[0]->has_value(op[1]));
537 case ir_binop_any_nequal:
538 return new(ctx) ir_constant(!op[0]->has_value(op[1]));
539 default:
540 break;
541 }
542 return NULL;
543 }
544
545 switch (this->operation) {
546 case ir_unop_bit_not:
547 switch (op[0]->type->base_type) {
548 case GLSL_TYPE_INT:
549 for (unsigned c = 0; c < components; c++)
550 data.i[c] = ~ op[0]->value.i[c];
551 break;
552 case GLSL_TYPE_UINT:
553 for (unsigned c = 0; c < components; c++)
554 data.u[c] = ~ op[0]->value.u[c];
555 break;
556 default:
557 assert(0);
558 }
559 break;
560
561 case ir_unop_logic_not:
562 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
563 for (unsigned c = 0; c < op[0]->type->components(); c++)
564 data.b[c] = !op[0]->value.b[c];
565 break;
566
567 case ir_unop_f2i:
568 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
569 for (unsigned c = 0; c < op[0]->type->components(); c++) {
570 data.i[c] = (int) op[0]->value.f[c];
571 }
572 break;
573 case ir_unop_f2u:
574 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
575 for (unsigned c = 0; c < op[0]->type->components(); c++) {
576 data.u[c] = (unsigned) op[0]->value.f[c];
577 }
578 break;
579 case ir_unop_i2f:
580 assert(op[0]->type->base_type == GLSL_TYPE_INT);
581 for (unsigned c = 0; c < op[0]->type->components(); c++) {
582 data.f[c] = (float) op[0]->value.i[c];
583 }
584 break;
585 case ir_unop_u2f:
586 assert(op[0]->type->base_type == GLSL_TYPE_UINT);
587 for (unsigned c = 0; c < op[0]->type->components(); c++) {
588 data.f[c] = (float) op[0]->value.u[c];
589 }
590 break;
591 case ir_unop_b2f:
592 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
593 for (unsigned c = 0; c < op[0]->type->components(); c++) {
594 data.f[c] = op[0]->value.b[c] ? 1.0F : 0.0F;
595 }
596 break;
597 case ir_unop_f2b:
598 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
599 for (unsigned c = 0; c < op[0]->type->components(); c++) {
600 data.b[c] = op[0]->value.f[c] != 0.0F ? true : false;
601 }
602 break;
603 case ir_unop_b2i:
604 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
605 for (unsigned c = 0; c < op[0]->type->components(); c++) {
606 data.i[c] = op[0]->value.b[c] ? 1 : 0;
607 }
608 break;
609 case ir_unop_i2b:
610 assert(op[0]->type->is_integer());
611 for (unsigned c = 0; c < op[0]->type->components(); c++) {
612 data.b[c] = op[0]->value.u[c] ? true : false;
613 }
614 break;
615 case ir_unop_u2i:
616 assert(op[0]->type->base_type == GLSL_TYPE_UINT);
617 for (unsigned c = 0; c < op[0]->type->components(); c++) {
618 data.i[c] = op[0]->value.u[c];
619 }
620 break;
621 case ir_unop_i2u:
622 assert(op[0]->type->base_type == GLSL_TYPE_INT);
623 for (unsigned c = 0; c < op[0]->type->components(); c++) {
624 data.u[c] = op[0]->value.i[c];
625 }
626 break;
627 case ir_unop_bitcast_i2f:
628 assert(op[0]->type->base_type == GLSL_TYPE_INT);
629 for (unsigned c = 0; c < op[0]->type->components(); c++) {
630 data.f[c] = bitcast_u2f(op[0]->value.i[c]);
631 }
632 break;
633 case ir_unop_bitcast_f2i:
634 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
635 for (unsigned c = 0; c < op[0]->type->components(); c++) {
636 data.i[c] = bitcast_f2u(op[0]->value.f[c]);
637 }
638 break;
639 case ir_unop_bitcast_u2f:
640 assert(op[0]->type->base_type == GLSL_TYPE_UINT);
641 for (unsigned c = 0; c < op[0]->type->components(); c++) {
642 data.f[c] = bitcast_u2f(op[0]->value.u[c]);
643 }
644 break;
645 case ir_unop_bitcast_f2u:
646 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
647 for (unsigned c = 0; c < op[0]->type->components(); c++) {
648 data.u[c] = bitcast_f2u(op[0]->value.f[c]);
649 }
650 break;
651 case ir_unop_d2f:
652 assert(op[0]->type->base_type == GLSL_TYPE_DOUBLE);
653 for (unsigned c = 0; c < op[0]->type->components(); c++) {
654 data.f[c] = op[0]->value.d[c];
655 }
656 break;
657 case ir_unop_f2d:
658 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
659 for (unsigned c = 0; c < op[0]->type->components(); c++) {
660 data.d[c] = op[0]->value.f[c];
661 }
662 break;
663 case ir_unop_d2i:
664 assert(op[0]->type->base_type == GLSL_TYPE_DOUBLE);
665 for (unsigned c = 0; c < op[0]->type->components(); c++) {
666 data.i[c] = op[0]->value.d[c];
667 }
668 break;
669 case ir_unop_i2d:
670 assert(op[0]->type->base_type == GLSL_TYPE_INT);
671 for (unsigned c = 0; c < op[0]->type->components(); c++) {
672 data.d[c] = op[0]->value.i[c];
673 }
674 break;
675 case ir_unop_d2u:
676 assert(op[0]->type->base_type == GLSL_TYPE_DOUBLE);
677 for (unsigned c = 0; c < op[0]->type->components(); c++) {
678 data.u[c] = op[0]->value.d[c];
679 }
680 break;
681 case ir_unop_u2d:
682 assert(op[0]->type->base_type == GLSL_TYPE_UINT);
683 for (unsigned c = 0; c < op[0]->type->components(); c++) {
684 data.d[c] = op[0]->value.u[c];
685 }
686 break;
687 case ir_unop_d2b:
688 assert(op[0]->type->base_type == GLSL_TYPE_DOUBLE);
689 for (unsigned c = 0; c < op[0]->type->components(); c++) {
690 data.b[c] = op[0]->value.d[c] != 0.0;
691 }
692 break;
693 case ir_unop_trunc:
694 for (unsigned c = 0; c < op[0]->type->components(); c++) {
695 if (op[0]->type->base_type == GLSL_TYPE_DOUBLE)
696 data.d[c] = trunc(op[0]->value.d[c]);
697 else
698 data.f[c] = truncf(op[0]->value.f[c]);
699 }
700 break;
701
702 case ir_unop_round_even:
703 for (unsigned c = 0; c < op[0]->type->components(); c++) {
704 if (op[0]->type->base_type == GLSL_TYPE_DOUBLE)
705 data.d[c] = _mesa_roundeven(op[0]->value.d[c]);
706 else
707 data.f[c] = _mesa_roundevenf(op[0]->value.f[c]);
708 }
709 break;
710
711 case ir_unop_ceil:
712 for (unsigned c = 0; c < op[0]->type->components(); c++) {
713 if (op[0]->type->base_type == GLSL_TYPE_DOUBLE)
714 data.d[c] = ceil(op[0]->value.d[c]);
715 else
716 data.f[c] = ceilf(op[0]->value.f[c]);
717 }
718 break;
719
720 case ir_unop_floor:
721 for (unsigned c = 0; c < op[0]->type->components(); c++) {
722 if (op[0]->type->base_type == GLSL_TYPE_DOUBLE)
723 data.d[c] = floor(op[0]->value.d[c]);
724 else
725 data.f[c] = floorf(op[0]->value.f[c]);
726 }
727 break;
728
729 case ir_unop_fract:
730 for (unsigned c = 0; c < op[0]->type->components(); c++) {
731 switch (this->type->base_type) {
732 case GLSL_TYPE_FLOAT:
733 data.f[c] = op[0]->value.f[c] - floor(op[0]->value.f[c]);
734 break;
735 case GLSL_TYPE_DOUBLE:
736 data.d[c] = op[0]->value.d[c] - floor(op[0]->value.d[c]);
737 break;
738 default:
739 assert(0);
740 }
741 }
742 break;
743
744 case ir_unop_sin:
745 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
746 for (unsigned c = 0; c < op[0]->type->components(); c++) {
747 data.f[c] = sinf(op[0]->value.f[c]);
748 }
749 break;
750
751 case ir_unop_cos:
752 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
753 for (unsigned c = 0; c < op[0]->type->components(); c++) {
754 data.f[c] = cosf(op[0]->value.f[c]);
755 }
756 break;
757
758 case ir_unop_neg:
759 for (unsigned c = 0; c < op[0]->type->components(); c++) {
760 switch (this->type->base_type) {
761 case GLSL_TYPE_UINT:
762 data.u[c] = -((int) op[0]->value.u[c]);
763 break;
764 case GLSL_TYPE_INT:
765 data.i[c] = -op[0]->value.i[c];
766 break;
767 case GLSL_TYPE_FLOAT:
768 data.f[c] = -op[0]->value.f[c];
769 break;
770 case GLSL_TYPE_DOUBLE:
771 data.d[c] = -op[0]->value.d[c];
772 break;
773 default:
774 assert(0);
775 }
776 }
777 break;
778
779 case ir_unop_abs:
780 for (unsigned c = 0; c < op[0]->type->components(); c++) {
781 switch (this->type->base_type) {
782 case GLSL_TYPE_INT:
783 data.i[c] = op[0]->value.i[c];
784 if (data.i[c] < 0)
785 data.i[c] = -data.i[c];
786 break;
787 case GLSL_TYPE_FLOAT:
788 data.f[c] = fabs(op[0]->value.f[c]);
789 break;
790 case GLSL_TYPE_DOUBLE:
791 data.d[c] = fabs(op[0]->value.d[c]);
792 break;
793 default:
794 assert(0);
795 }
796 }
797 break;
798
799 case ir_unop_sign:
800 for (unsigned c = 0; c < op[0]->type->components(); c++) {
801 switch (this->type->base_type) {
802 case GLSL_TYPE_INT:
803 data.i[c] = (op[0]->value.i[c] > 0) - (op[0]->value.i[c] < 0);
804 break;
805 case GLSL_TYPE_FLOAT:
806 data.f[c] = float((op[0]->value.f[c] > 0)-(op[0]->value.f[c] < 0));
807 break;
808 case GLSL_TYPE_DOUBLE:
809 data.d[c] = double((op[0]->value.d[c] > 0)-(op[0]->value.d[c] < 0));
810 break;
811 default:
812 assert(0);
813 }
814 }
815 break;
816
817 case ir_unop_rcp:
818 for (unsigned c = 0; c < op[0]->type->components(); c++) {
819 switch (this->type->base_type) {
820 case GLSL_TYPE_FLOAT:
821 if (op[0]->value.f[c] != 0.0)
822 data.f[c] = 1.0F / op[0]->value.f[c];
823 break;
824 case GLSL_TYPE_DOUBLE:
825 if (op[0]->value.d[c] != 0.0)
826 data.d[c] = 1.0 / op[0]->value.d[c];
827 break;
828 default:
829 assert(0);
830 }
831 }
832 break;
833
834 case ir_unop_rsq:
835 for (unsigned c = 0; c < op[0]->type->components(); c++) {
836 if (op[0]->type->base_type == GLSL_TYPE_DOUBLE)
837 data.d[c] = 1.0 / sqrt(op[0]->value.d[c]);
838 else
839 data.f[c] = 1.0F / sqrtf(op[0]->value.f[c]);
840 }
841 break;
842
843 case ir_unop_sqrt:
844 for (unsigned c = 0; c < op[0]->type->components(); c++) {
845 if (op[0]->type->base_type == GLSL_TYPE_DOUBLE)
846 data.d[c] = sqrt(op[0]->value.d[c]);
847 else
848 data.f[c] = sqrtf(op[0]->value.f[c]);
849 }
850 break;
851
852 case ir_unop_exp:
853 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
854 for (unsigned c = 0; c < op[0]->type->components(); c++) {
855 data.f[c] = expf(op[0]->value.f[c]);
856 }
857 break;
858
859 case ir_unop_exp2:
860 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
861 for (unsigned c = 0; c < op[0]->type->components(); c++) {
862 data.f[c] = exp2f(op[0]->value.f[c]);
863 }
864 break;
865
866 case ir_unop_log:
867 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
868 for (unsigned c = 0; c < op[0]->type->components(); c++) {
869 data.f[c] = logf(op[0]->value.f[c]);
870 }
871 break;
872
873 case ir_unop_log2:
874 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
875 for (unsigned c = 0; c < op[0]->type->components(); c++) {
876 data.f[c] = log2f(op[0]->value.f[c]);
877 }
878 break;
879
880 case ir_unop_dFdx:
881 case ir_unop_dFdx_coarse:
882 case ir_unop_dFdx_fine:
883 case ir_unop_dFdy:
884 case ir_unop_dFdy_coarse:
885 case ir_unop_dFdy_fine:
886 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
887 for (unsigned c = 0; c < op[0]->type->components(); c++) {
888 data.f[c] = 0.0;
889 }
890 break;
891
892 case ir_unop_pack_snorm_2x16:
893 assert(op[0]->type == glsl_type::vec2_type);
894 data.u[0] = pack_2x16(pack_snorm_1x16,
895 op[0]->value.f[0],
896 op[0]->value.f[1]);
897 break;
898 case ir_unop_pack_snorm_4x8:
899 assert(op[0]->type == glsl_type::vec4_type);
900 data.u[0] = pack_4x8(pack_snorm_1x8,
901 op[0]->value.f[0],
902 op[0]->value.f[1],
903 op[0]->value.f[2],
904 op[0]->value.f[3]);
905 break;
906 case ir_unop_unpack_snorm_2x16:
907 assert(op[0]->type == glsl_type::uint_type);
908 unpack_2x16(unpack_snorm_1x16,
909 op[0]->value.u[0],
910 &data.f[0], &data.f[1]);
911 break;
912 case ir_unop_unpack_snorm_4x8:
913 assert(op[0]->type == glsl_type::uint_type);
914 unpack_4x8(unpack_snorm_1x8,
915 op[0]->value.u[0],
916 &data.f[0], &data.f[1], &data.f[2], &data.f[3]);
917 break;
918 case ir_unop_pack_unorm_2x16:
919 assert(op[0]->type == glsl_type::vec2_type);
920 data.u[0] = pack_2x16(pack_unorm_1x16,
921 op[0]->value.f[0],
922 op[0]->value.f[1]);
923 break;
924 case ir_unop_pack_unorm_4x8:
925 assert(op[0]->type == glsl_type::vec4_type);
926 data.u[0] = pack_4x8(pack_unorm_1x8,
927 op[0]->value.f[0],
928 op[0]->value.f[1],
929 op[0]->value.f[2],
930 op[0]->value.f[3]);
931 break;
932 case ir_unop_unpack_unorm_2x16:
933 assert(op[0]->type == glsl_type::uint_type);
934 unpack_2x16(unpack_unorm_1x16,
935 op[0]->value.u[0],
936 &data.f[0], &data.f[1]);
937 break;
938 case ir_unop_unpack_unorm_4x8:
939 assert(op[0]->type == glsl_type::uint_type);
940 unpack_4x8(unpack_unorm_1x8,
941 op[0]->value.u[0],
942 &data.f[0], &data.f[1], &data.f[2], &data.f[3]);
943 break;
944 case ir_unop_pack_half_2x16:
945 assert(op[0]->type == glsl_type::vec2_type);
946 data.u[0] = pack_2x16(pack_half_1x16,
947 op[0]->value.f[0],
948 op[0]->value.f[1]);
949 break;
950 case ir_unop_unpack_half_2x16:
951 assert(op[0]->type == glsl_type::uint_type);
952 unpack_2x16(unpack_half_1x16,
953 op[0]->value.u[0],
954 &data.f[0], &data.f[1]);
955 break;
956 case ir_binop_pow:
957 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
958 for (unsigned c = 0; c < op[0]->type->components(); c++) {
959 data.f[c] = powf(op[0]->value.f[c], op[1]->value.f[c]);
960 }
961 break;
962
963 case ir_binop_dot:
964 if (op[0]->type->base_type == GLSL_TYPE_DOUBLE)
965 data.d[0] = dot_d(op[0], op[1]);
966 else
967 data.f[0] = dot_f(op[0], op[1]);
968 break;
969
970 case ir_binop_min:
971 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
972 for (unsigned c = 0, c0 = 0, c1 = 0;
973 c < components;
974 c0 += c0_inc, c1 += c1_inc, c++) {
975
976 switch (op[0]->type->base_type) {
977 case GLSL_TYPE_UINT:
978 data.u[c] = MIN2(op[0]->value.u[c0], op[1]->value.u[c1]);
979 break;
980 case GLSL_TYPE_INT:
981 data.i[c] = MIN2(op[0]->value.i[c0], op[1]->value.i[c1]);
982 break;
983 case GLSL_TYPE_FLOAT:
984 data.f[c] = MIN2(op[0]->value.f[c0], op[1]->value.f[c1]);
985 break;
986 case GLSL_TYPE_DOUBLE:
987 data.d[c] = MIN2(op[0]->value.d[c0], op[1]->value.d[c1]);
988 break;
989 default:
990 assert(0);
991 }
992 }
993
994 break;
995 case ir_binop_max:
996 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
997 for (unsigned c = 0, c0 = 0, c1 = 0;
998 c < components;
999 c0 += c0_inc, c1 += c1_inc, c++) {
1000
1001 switch (op[0]->type->base_type) {
1002 case GLSL_TYPE_UINT:
1003 data.u[c] = MAX2(op[0]->value.u[c0], op[1]->value.u[c1]);
1004 break;
1005 case GLSL_TYPE_INT:
1006 data.i[c] = MAX2(op[0]->value.i[c0], op[1]->value.i[c1]);
1007 break;
1008 case GLSL_TYPE_FLOAT:
1009 data.f[c] = MAX2(op[0]->value.f[c0], op[1]->value.f[c1]);
1010 break;
1011 case GLSL_TYPE_DOUBLE:
1012 data.d[c] = MAX2(op[0]->value.d[c0], op[1]->value.d[c1]);
1013 break;
1014 default:
1015 assert(0);
1016 }
1017 }
1018 break;
1019
1020 case ir_binop_add:
1021 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
1022 for (unsigned c = 0, c0 = 0, c1 = 0;
1023 c < components;
1024 c0 += c0_inc, c1 += c1_inc, c++) {
1025
1026 switch (op[0]->type->base_type) {
1027 case GLSL_TYPE_UINT:
1028 data.u[c] = op[0]->value.u[c0] + op[1]->value.u[c1];
1029 break;
1030 case GLSL_TYPE_INT:
1031 data.i[c] = op[0]->value.i[c0] + op[1]->value.i[c1];
1032 break;
1033 case GLSL_TYPE_FLOAT:
1034 data.f[c] = op[0]->value.f[c0] + op[1]->value.f[c1];
1035 break;
1036 case GLSL_TYPE_DOUBLE:
1037 data.d[c] = op[0]->value.d[c0] + op[1]->value.d[c1];
1038 break;
1039 default:
1040 assert(0);
1041 }
1042 }
1043
1044 break;
1045 case ir_binop_sub:
1046 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
1047 for (unsigned c = 0, c0 = 0, c1 = 0;
1048 c < components;
1049 c0 += c0_inc, c1 += c1_inc, c++) {
1050
1051 switch (op[0]->type->base_type) {
1052 case GLSL_TYPE_UINT:
1053 data.u[c] = op[0]->value.u[c0] - op[1]->value.u[c1];
1054 break;
1055 case GLSL_TYPE_INT:
1056 data.i[c] = op[0]->value.i[c0] - op[1]->value.i[c1];
1057 break;
1058 case GLSL_TYPE_FLOAT:
1059 data.f[c] = op[0]->value.f[c0] - op[1]->value.f[c1];
1060 break;
1061 case GLSL_TYPE_DOUBLE:
1062 data.d[c] = op[0]->value.d[c0] - op[1]->value.d[c1];
1063 break;
1064 default:
1065 assert(0);
1066 }
1067 }
1068
1069 break;
1070 case ir_binop_mul:
1071 /* Check for equal types, or unequal types involving scalars */
1072 if ((op[0]->type == op[1]->type && !op[0]->type->is_matrix())
1073 || op0_scalar || op1_scalar) {
1074 for (unsigned c = 0, c0 = 0, c1 = 0;
1075 c < components;
1076 c0 += c0_inc, c1 += c1_inc, c++) {
1077
1078 switch (op[0]->type->base_type) {
1079 case GLSL_TYPE_UINT:
1080 data.u[c] = op[0]->value.u[c0] * op[1]->value.u[c1];
1081 break;
1082 case GLSL_TYPE_INT:
1083 data.i[c] = op[0]->value.i[c0] * op[1]->value.i[c1];
1084 break;
1085 case GLSL_TYPE_FLOAT:
1086 data.f[c] = op[0]->value.f[c0] * op[1]->value.f[c1];
1087 break;
1088 case GLSL_TYPE_DOUBLE:
1089 data.d[c] = op[0]->value.d[c0] * op[1]->value.d[c1];
1090 break;
1091 default:
1092 assert(0);
1093 }
1094 }
1095 } else {
1096 assert(op[0]->type->is_matrix() || op[1]->type->is_matrix());
1097
1098 /* Multiply an N-by-M matrix with an M-by-P matrix. Since either
1099 * matrix can be a GLSL vector, either N or P can be 1.
1100 *
1101 * For vec*mat, the vector is treated as a row vector. This
1102 * means the vector is a 1-row x M-column matrix.
1103 *
1104 * For mat*vec, the vector is treated as a column vector. Since
1105 * matrix_columns is 1 for vectors, this just works.
1106 */
1107 const unsigned n = op[0]->type->is_vector()
1108 ? 1 : op[0]->type->vector_elements;
1109 const unsigned m = op[1]->type->vector_elements;
1110 const unsigned p = op[1]->type->matrix_columns;
1111 for (unsigned j = 0; j < p; j++) {
1112 for (unsigned i = 0; i < n; i++) {
1113 for (unsigned k = 0; k < m; k++) {
1114 if (op[0]->type->base_type == GLSL_TYPE_DOUBLE)
1115 data.d[i+n*j] += op[0]->value.d[i+n*k]*op[1]->value.d[k+m*j];
1116 else
1117 data.f[i+n*j] += op[0]->value.f[i+n*k]*op[1]->value.f[k+m*j];
1118 }
1119 }
1120 }
1121 }
1122
1123 break;
1124 case ir_binop_div:
1125 /* FINISHME: Emit warning when division-by-zero is detected. */
1126 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
1127 for (unsigned c = 0, c0 = 0, c1 = 0;
1128 c < components;
1129 c0 += c0_inc, c1 += c1_inc, c++) {
1130
1131 switch (op[0]->type->base_type) {
1132 case GLSL_TYPE_UINT:
1133 if (op[1]->value.u[c1] == 0) {
1134 data.u[c] = 0;
1135 } else {
1136 data.u[c] = op[0]->value.u[c0] / op[1]->value.u[c1];
1137 }
1138 break;
1139 case GLSL_TYPE_INT:
1140 if (op[1]->value.i[c1] == 0) {
1141 data.i[c] = 0;
1142 } else {
1143 data.i[c] = op[0]->value.i[c0] / op[1]->value.i[c1];
1144 }
1145 break;
1146 case GLSL_TYPE_FLOAT:
1147 data.f[c] = op[0]->value.f[c0] / op[1]->value.f[c1];
1148 break;
1149 case GLSL_TYPE_DOUBLE:
1150 data.d[c] = op[0]->value.d[c0] / op[1]->value.d[c1];
1151 break;
1152 default:
1153 assert(0);
1154 }
1155 }
1156
1157 break;
1158 case ir_binop_mod:
1159 /* FINISHME: Emit warning when division-by-zero is detected. */
1160 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
1161 for (unsigned c = 0, c0 = 0, c1 = 0;
1162 c < components;
1163 c0 += c0_inc, c1 += c1_inc, c++) {
1164
1165 switch (op[0]->type->base_type) {
1166 case GLSL_TYPE_UINT:
1167 if (op[1]->value.u[c1] == 0) {
1168 data.u[c] = 0;
1169 } else {
1170 data.u[c] = op[0]->value.u[c0] % op[1]->value.u[c1];
1171 }
1172 break;
1173 case GLSL_TYPE_INT:
1174 if (op[1]->value.i[c1] == 0) {
1175 data.i[c] = 0;
1176 } else {
1177 data.i[c] = op[0]->value.i[c0] % op[1]->value.i[c1];
1178 }
1179 break;
1180 case GLSL_TYPE_FLOAT:
1181 /* We don't use fmod because it rounds toward zero; GLSL specifies
1182 * the use of floor.
1183 */
1184 data.f[c] = op[0]->value.f[c0] - op[1]->value.f[c1]
1185 * floorf(op[0]->value.f[c0] / op[1]->value.f[c1]);
1186 break;
1187 case GLSL_TYPE_DOUBLE:
1188 /* We don't use fmod because it rounds toward zero; GLSL specifies
1189 * the use of floor.
1190 */
1191 data.d[c] = op[0]->value.d[c0] - op[1]->value.d[c1]
1192 * floor(op[0]->value.d[c0] / op[1]->value.d[c1]);
1193 break;
1194 default:
1195 assert(0);
1196 }
1197 }
1198
1199 break;
1200
1201 case ir_binop_logic_and:
1202 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
1203 for (unsigned c = 0; c < op[0]->type->components(); c++)
1204 data.b[c] = op[0]->value.b[c] && op[1]->value.b[c];
1205 break;
1206 case ir_binop_logic_xor:
1207 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
1208 for (unsigned c = 0; c < op[0]->type->components(); c++)
1209 data.b[c] = op[0]->value.b[c] ^ op[1]->value.b[c];
1210 break;
1211 case ir_binop_logic_or:
1212 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
1213 for (unsigned c = 0; c < op[0]->type->components(); c++)
1214 data.b[c] = op[0]->value.b[c] || op[1]->value.b[c];
1215 break;
1216
1217 case ir_binop_less:
1218 assert(op[0]->type == op[1]->type);
1219 for (unsigned c = 0; c < op[0]->type->components(); c++) {
1220 switch (op[0]->type->base_type) {
1221 case GLSL_TYPE_UINT:
1222 data.b[c] = op[0]->value.u[c] < op[1]->value.u[c];
1223 break;
1224 case GLSL_TYPE_INT:
1225 data.b[c] = op[0]->value.i[c] < op[1]->value.i[c];
1226 break;
1227 case GLSL_TYPE_FLOAT:
1228 data.b[c] = op[0]->value.f[c] < op[1]->value.f[c];
1229 break;
1230 case GLSL_TYPE_DOUBLE:
1231 data.b[c] = op[0]->value.d[c] < op[1]->value.d[c];
1232 break;
1233 default:
1234 assert(0);
1235 }
1236 }
1237 break;
1238 case ir_binop_greater:
1239 assert(op[0]->type == op[1]->type);
1240 for (unsigned c = 0; c < op[0]->type->components(); c++) {
1241 switch (op[0]->type->base_type) {
1242 case GLSL_TYPE_UINT:
1243 data.b[c] = op[0]->value.u[c] > op[1]->value.u[c];
1244 break;
1245 case GLSL_TYPE_INT:
1246 data.b[c] = op[0]->value.i[c] > op[1]->value.i[c];
1247 break;
1248 case GLSL_TYPE_FLOAT:
1249 data.b[c] = op[0]->value.f[c] > op[1]->value.f[c];
1250 break;
1251 case GLSL_TYPE_DOUBLE:
1252 data.b[c] = op[0]->value.d[c] > op[1]->value.d[c];
1253 break;
1254 default:
1255 assert(0);
1256 }
1257 }
1258 break;
1259 case ir_binop_lequal:
1260 assert(op[0]->type == op[1]->type);
1261 for (unsigned c = 0; c < op[0]->type->components(); c++) {
1262 switch (op[0]->type->base_type) {
1263 case GLSL_TYPE_UINT:
1264 data.b[c] = op[0]->value.u[c] <= op[1]->value.u[c];
1265 break;
1266 case GLSL_TYPE_INT:
1267 data.b[c] = op[0]->value.i[c] <= op[1]->value.i[c];
1268 break;
1269 case GLSL_TYPE_FLOAT:
1270 data.b[c] = op[0]->value.f[c] <= op[1]->value.f[c];
1271 break;
1272 case GLSL_TYPE_DOUBLE:
1273 data.b[c] = op[0]->value.d[c] <= op[1]->value.d[c];
1274 break;
1275 default:
1276 assert(0);
1277 }
1278 }
1279 break;
1280 case ir_binop_gequal:
1281 assert(op[0]->type == op[1]->type);
1282 for (unsigned c = 0; c < op[0]->type->components(); c++) {
1283 switch (op[0]->type->base_type) {
1284 case GLSL_TYPE_UINT:
1285 data.b[c] = op[0]->value.u[c] >= op[1]->value.u[c];
1286 break;
1287 case GLSL_TYPE_INT:
1288 data.b[c] = op[0]->value.i[c] >= op[1]->value.i[c];
1289 break;
1290 case GLSL_TYPE_FLOAT:
1291 data.b[c] = op[0]->value.f[c] >= op[1]->value.f[c];
1292 break;
1293 case GLSL_TYPE_DOUBLE:
1294 data.b[c] = op[0]->value.d[c] >= op[1]->value.d[c];
1295 break;
1296 default:
1297 assert(0);
1298 }
1299 }
1300 break;
1301 case ir_binop_equal:
1302 assert(op[0]->type == op[1]->type);
1303 for (unsigned c = 0; c < components; c++) {
1304 switch (op[0]->type->base_type) {
1305 case GLSL_TYPE_UINT:
1306 data.b[c] = op[0]->value.u[c] == op[1]->value.u[c];
1307 break;
1308 case GLSL_TYPE_INT:
1309 data.b[c] = op[0]->value.i[c] == op[1]->value.i[c];
1310 break;
1311 case GLSL_TYPE_FLOAT:
1312 data.b[c] = op[0]->value.f[c] == op[1]->value.f[c];
1313 break;
1314 case GLSL_TYPE_BOOL:
1315 data.b[c] = op[0]->value.b[c] == op[1]->value.b[c];
1316 break;
1317 case GLSL_TYPE_DOUBLE:
1318 data.b[c] = op[0]->value.d[c] == op[1]->value.d[c];
1319 break;
1320 default:
1321 assert(0);
1322 }
1323 }
1324 break;
1325 case ir_binop_nequal:
1326 assert(op[0]->type == op[1]->type);
1327 for (unsigned c = 0; c < components; c++) {
1328 switch (op[0]->type->base_type) {
1329 case GLSL_TYPE_UINT:
1330 data.b[c] = op[0]->value.u[c] != op[1]->value.u[c];
1331 break;
1332 case GLSL_TYPE_INT:
1333 data.b[c] = op[0]->value.i[c] != op[1]->value.i[c];
1334 break;
1335 case GLSL_TYPE_FLOAT:
1336 data.b[c] = op[0]->value.f[c] != op[1]->value.f[c];
1337 break;
1338 case GLSL_TYPE_BOOL:
1339 data.b[c] = op[0]->value.b[c] != op[1]->value.b[c];
1340 break;
1341 case GLSL_TYPE_DOUBLE:
1342 data.b[c] = op[0]->value.d[c] != op[1]->value.d[c];
1343 break;
1344 default:
1345 assert(0);
1346 }
1347 }
1348 break;
1349 case ir_binop_all_equal:
1350 data.b[0] = op[0]->has_value(op[1]);
1351 break;
1352 case ir_binop_any_nequal:
1353 data.b[0] = !op[0]->has_value(op[1]);
1354 break;
1355
1356 case ir_binop_lshift:
1357 for (unsigned c = 0, c0 = 0, c1 = 0;
1358 c < components;
1359 c0 += c0_inc, c1 += c1_inc, c++) {
1360
1361 if (op[0]->type->base_type == GLSL_TYPE_INT &&
1362 op[1]->type->base_type == GLSL_TYPE_INT) {
1363 data.i[c] = op[0]->value.i[c0] << op[1]->value.i[c1];
1364
1365 } else if (op[0]->type->base_type == GLSL_TYPE_INT &&
1366 op[1]->type->base_type == GLSL_TYPE_UINT) {
1367 data.i[c] = op[0]->value.i[c0] << op[1]->value.u[c1];
1368
1369 } else if (op[0]->type->base_type == GLSL_TYPE_UINT &&
1370 op[1]->type->base_type == GLSL_TYPE_INT) {
1371 data.u[c] = op[0]->value.u[c0] << op[1]->value.i[c1];
1372
1373 } else if (op[0]->type->base_type == GLSL_TYPE_UINT &&
1374 op[1]->type->base_type == GLSL_TYPE_UINT) {
1375 data.u[c] = op[0]->value.u[c0] << op[1]->value.u[c1];
1376 }
1377 }
1378 break;
1379
1380 case ir_binop_rshift:
1381 for (unsigned c = 0, c0 = 0, c1 = 0;
1382 c < components;
1383 c0 += c0_inc, c1 += c1_inc, c++) {
1384
1385 if (op[0]->type->base_type == GLSL_TYPE_INT &&
1386 op[1]->type->base_type == GLSL_TYPE_INT) {
1387 data.i[c] = op[0]->value.i[c0] >> op[1]->value.i[c1];
1388
1389 } else if (op[0]->type->base_type == GLSL_TYPE_INT &&
1390 op[1]->type->base_type == GLSL_TYPE_UINT) {
1391 data.i[c] = op[0]->value.i[c0] >> op[1]->value.u[c1];
1392
1393 } else if (op[0]->type->base_type == GLSL_TYPE_UINT &&
1394 op[1]->type->base_type == GLSL_TYPE_INT) {
1395 data.u[c] = op[0]->value.u[c0] >> op[1]->value.i[c1];
1396
1397 } else if (op[0]->type->base_type == GLSL_TYPE_UINT &&
1398 op[1]->type->base_type == GLSL_TYPE_UINT) {
1399 data.u[c] = op[0]->value.u[c0] >> op[1]->value.u[c1];
1400 }
1401 }
1402 break;
1403
1404 case ir_binop_bit_and:
1405 for (unsigned c = 0, c0 = 0, c1 = 0;
1406 c < components;
1407 c0 += c0_inc, c1 += c1_inc, c++) {
1408
1409 switch (op[0]->type->base_type) {
1410 case GLSL_TYPE_INT:
1411 data.i[c] = op[0]->value.i[c0] & op[1]->value.i[c1];
1412 break;
1413 case GLSL_TYPE_UINT:
1414 data.u[c] = op[0]->value.u[c0] & op[1]->value.u[c1];
1415 break;
1416 default:
1417 assert(0);
1418 }
1419 }
1420 break;
1421
1422 case ir_binop_bit_or:
1423 for (unsigned c = 0, c0 = 0, c1 = 0;
1424 c < components;
1425 c0 += c0_inc, c1 += c1_inc, c++) {
1426
1427 switch (op[0]->type->base_type) {
1428 case GLSL_TYPE_INT:
1429 data.i[c] = op[0]->value.i[c0] | op[1]->value.i[c1];
1430 break;
1431 case GLSL_TYPE_UINT:
1432 data.u[c] = op[0]->value.u[c0] | op[1]->value.u[c1];
1433 break;
1434 default:
1435 assert(0);
1436 }
1437 }
1438 break;
1439
1440 case ir_binop_vector_extract: {
1441 const int c = CLAMP(op[1]->value.i[0], 0,
1442 (int) op[0]->type->vector_elements - 1);
1443
1444 switch (op[0]->type->base_type) {
1445 case GLSL_TYPE_UINT:
1446 data.u[0] = op[0]->value.u[c];
1447 break;
1448 case GLSL_TYPE_INT:
1449 data.i[0] = op[0]->value.i[c];
1450 break;
1451 case GLSL_TYPE_FLOAT:
1452 data.f[0] = op[0]->value.f[c];
1453 break;
1454 case GLSL_TYPE_DOUBLE:
1455 data.d[0] = op[0]->value.d[c];
1456 break;
1457 case GLSL_TYPE_BOOL:
1458 data.b[0] = op[0]->value.b[c];
1459 break;
1460 default:
1461 assert(0);
1462 }
1463 break;
1464 }
1465
1466 case ir_binop_bit_xor:
1467 for (unsigned c = 0, c0 = 0, c1 = 0;
1468 c < components;
1469 c0 += c0_inc, c1 += c1_inc, c++) {
1470
1471 switch (op[0]->type->base_type) {
1472 case GLSL_TYPE_INT:
1473 data.i[c] = op[0]->value.i[c0] ^ op[1]->value.i[c1];
1474 break;
1475 case GLSL_TYPE_UINT:
1476 data.u[c] = op[0]->value.u[c0] ^ op[1]->value.u[c1];
1477 break;
1478 default:
1479 assert(0);
1480 }
1481 }
1482 break;
1483
1484 case ir_unop_bitfield_reverse:
1485 /* http://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious */
1486 for (unsigned c = 0; c < components; c++) {
1487 unsigned int v = op[0]->value.u[c]; // input bits to be reversed
1488 unsigned int r = v; // r will be reversed bits of v; first get LSB of v
1489 int s = sizeof(v) * CHAR_BIT - 1; // extra shift needed at end
1490
1491 for (v >>= 1; v; v >>= 1) {
1492 r <<= 1;
1493 r |= v & 1;
1494 s--;
1495 }
1496 r <<= s; // shift when v's highest bits are zero
1497
1498 data.u[c] = r;
1499 }
1500 break;
1501
1502 case ir_unop_bit_count:
1503 for (unsigned c = 0; c < components; c++)
1504 data.i[c] = _mesa_bitcount(op[0]->value.u[c]);
1505 break;
1506
1507 case ir_unop_find_msb:
1508 for (unsigned c = 0; c < components; c++) {
1509 int v = op[0]->value.i[c];
1510
1511 if (v == 0 || (op[0]->type->base_type == GLSL_TYPE_INT && v == -1))
1512 data.i[c] = -1;
1513 else {
1514 int count = 0;
1515 unsigned top_bit = op[0]->type->base_type == GLSL_TYPE_UINT
1516 ? 0 : v & (1u << 31);
1517
1518 while (((v & (1u << 31)) == top_bit) && count != 32) {
1519 count++;
1520 v <<= 1;
1521 }
1522
1523 data.i[c] = 31 - count;
1524 }
1525 }
1526 break;
1527
1528 case ir_unop_find_lsb:
1529 for (unsigned c = 0; c < components; c++) {
1530 if (op[0]->value.i[c] == 0)
1531 data.i[c] = -1;
1532 else {
1533 unsigned pos = 0;
1534 unsigned v = op[0]->value.u[c];
1535
1536 for (; !(v & 1); v >>= 1) {
1537 pos++;
1538 }
1539 data.u[c] = pos;
1540 }
1541 }
1542 break;
1543
1544 case ir_unop_saturate:
1545 for (unsigned c = 0; c < components; c++) {
1546 data.f[c] = CLAMP(op[0]->value.f[c], 0.0f, 1.0f);
1547 }
1548 break;
1549 case ir_unop_pack_double_2x32:
1550 /* XXX needs to be checked on big-endian */
1551 memcpy(&data.d[0], &op[0]->value.u[0], sizeof(double));
1552 break;
1553 case ir_unop_unpack_double_2x32:
1554 /* XXX needs to be checked on big-endian */
1555 memcpy(&data.u[0], &op[0]->value.d[0], sizeof(double));
1556 break;
1557
1558 case ir_triop_bitfield_extract: {
1559 for (unsigned c = 0; c < components; c++) {
1560 int offset = op[1]->value.i[c];
1561 int bits = op[2]->value.i[c];
1562
1563 if (bits == 0)
1564 data.u[c] = 0;
1565 else if (offset < 0 || bits < 0)
1566 data.u[c] = 0; /* Undefined, per spec. */
1567 else if (offset + bits > 32)
1568 data.u[c] = 0; /* Undefined, per spec. */
1569 else {
1570 if (op[0]->type->base_type == GLSL_TYPE_INT) {
1571 /* int so that the right shift will sign-extend. */
1572 int value = op[0]->value.i[c];
1573 value <<= 32 - bits - offset;
1574 value >>= 32 - bits;
1575 data.i[c] = value;
1576 } else {
1577 unsigned value = op[0]->value.u[c];
1578 value <<= 32 - bits - offset;
1579 value >>= 32 - bits;
1580 data.u[c] = value;
1581 }
1582 }
1583 }
1584 break;
1585 }
1586
1587 case ir_binop_ldexp:
1588 for (unsigned c = 0; c < components; c++) {
1589 if (op[0]->type->base_type == GLSL_TYPE_DOUBLE) {
1590 data.d[c] = ldexp(op[0]->value.d[c], op[1]->value.i[c]);
1591 /* Flush subnormal values to zero. */
1592 if (!isnormal(data.d[c]))
1593 data.d[c] = copysign(0.0, op[0]->value.d[c]);
1594 } else {
1595 data.f[c] = ldexpf(op[0]->value.f[c], op[1]->value.i[c]);
1596 /* Flush subnormal values to zero. */
1597 if (!isnormal(data.f[c]))
1598 data.f[c] = copysignf(0.0f, op[0]->value.f[c]);
1599 }
1600 }
1601 break;
1602
1603 case ir_triop_fma:
1604 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT ||
1605 op[0]->type->base_type == GLSL_TYPE_DOUBLE);
1606 assert(op[1]->type->base_type == GLSL_TYPE_FLOAT ||
1607 op[1]->type->base_type == GLSL_TYPE_DOUBLE);
1608 assert(op[2]->type->base_type == GLSL_TYPE_FLOAT ||
1609 op[2]->type->base_type == GLSL_TYPE_DOUBLE);
1610
1611 for (unsigned c = 0; c < components; c++) {
1612 if (op[0]->type->base_type == GLSL_TYPE_DOUBLE)
1613 data.d[c] = op[0]->value.d[c] * op[1]->value.d[c]
1614 + op[2]->value.d[c];
1615 else
1616 data.f[c] = op[0]->value.f[c] * op[1]->value.f[c]
1617 + op[2]->value.f[c];
1618 }
1619 break;
1620
1621 case ir_triop_lrp: {
1622 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT ||
1623 op[0]->type->base_type == GLSL_TYPE_DOUBLE);
1624 assert(op[1]->type->base_type == GLSL_TYPE_FLOAT ||
1625 op[1]->type->base_type == GLSL_TYPE_DOUBLE);
1626 assert(op[2]->type->base_type == GLSL_TYPE_FLOAT ||
1627 op[2]->type->base_type == GLSL_TYPE_DOUBLE);
1628
1629 unsigned c2_inc = op[2]->type->is_scalar() ? 0 : 1;
1630 for (unsigned c = 0, c2 = 0; c < components; c2 += c2_inc, c++) {
1631 if (op[0]->type->base_type == GLSL_TYPE_DOUBLE)
1632 data.d[c] = op[0]->value.d[c] * (1.0 - op[2]->value.d[c2]) +
1633 (op[1]->value.d[c] * op[2]->value.d[c2]);
1634 else
1635 data.f[c] = op[0]->value.f[c] * (1.0f - op[2]->value.f[c2]) +
1636 (op[1]->value.f[c] * op[2]->value.f[c2]);
1637 }
1638 break;
1639 }
1640
1641 case ir_triop_csel:
1642 for (unsigned c = 0; c < components; c++) {
1643 if (op[1]->type->base_type == GLSL_TYPE_DOUBLE)
1644 data.d[c] = op[0]->value.b[c] ? op[1]->value.d[c]
1645 : op[2]->value.d[c];
1646 else
1647 data.u[c] = op[0]->value.b[c] ? op[1]->value.u[c]
1648 : op[2]->value.u[c];
1649 }
1650 break;
1651
1652 case ir_triop_vector_insert: {
1653 const unsigned idx = op[2]->value.u[0];
1654
1655 memcpy(&data, &op[0]->value, sizeof(data));
1656
1657 switch (this->type->base_type) {
1658 case GLSL_TYPE_INT:
1659 data.i[idx] = op[1]->value.i[0];
1660 break;
1661 case GLSL_TYPE_UINT:
1662 data.u[idx] = op[1]->value.u[0];
1663 break;
1664 case GLSL_TYPE_FLOAT:
1665 data.f[idx] = op[1]->value.f[0];
1666 break;
1667 case GLSL_TYPE_BOOL:
1668 data.b[idx] = op[1]->value.b[0];
1669 break;
1670 case GLSL_TYPE_DOUBLE:
1671 data.d[idx] = op[1]->value.d[0];
1672 break;
1673 default:
1674 assert(!"Should not get here.");
1675 break;
1676 }
1677 break;
1678 }
1679
1680 case ir_quadop_bitfield_insert: {
1681 for (unsigned c = 0; c < components; c++) {
1682 int offset = op[2]->value.i[c];
1683 int bits = op[3]->value.i[c];
1684
1685 if (bits == 0)
1686 data.u[c] = op[0]->value.u[c];
1687 else if (offset < 0 || bits < 0)
1688 data.u[c] = 0; /* Undefined, per spec. */
1689 else if (offset + bits > 32)
1690 data.u[c] = 0; /* Undefined, per spec. */
1691 else {
1692 unsigned insert_mask = ((1ull << bits) - 1) << offset;
1693
1694 unsigned insert = op[1]->value.u[c];
1695 insert <<= offset;
1696 insert &= insert_mask;
1697
1698 unsigned base = op[0]->value.u[c];
1699 base &= ~insert_mask;
1700
1701 data.u[c] = base | insert;
1702 }
1703 }
1704 break;
1705 }
1706
1707 case ir_quadop_vector:
1708 for (unsigned c = 0; c < this->type->vector_elements; c++) {
1709 switch (this->type->base_type) {
1710 case GLSL_TYPE_INT:
1711 data.i[c] = op[c]->value.i[0];
1712 break;
1713 case GLSL_TYPE_UINT:
1714 data.u[c] = op[c]->value.u[0];
1715 break;
1716 case GLSL_TYPE_FLOAT:
1717 data.f[c] = op[c]->value.f[0];
1718 break;
1719 case GLSL_TYPE_DOUBLE:
1720 data.d[c] = op[c]->value.d[0];
1721 break;
1722 case GLSL_TYPE_BOOL:
1723 data.b[c] = op[c]->value.b[0];
1724 break;
1725 default:
1726 assert(0);
1727 }
1728 }
1729 break;
1730
1731 default:
1732 /* FINISHME: Should handle all expression types. */
1733 return NULL;
1734 }
1735
1736 return new(ctx) ir_constant(this->type, &data);
1737 }
1738
1739
1740 ir_constant *
1741 ir_texture::constant_expression_value(struct hash_table *)
1742 {
1743 /* texture lookups aren't constant expressions */
1744 return NULL;
1745 }
1746
1747
1748 ir_constant *
1749 ir_swizzle::constant_expression_value(struct hash_table *variable_context)
1750 {
1751 ir_constant *v = this->val->constant_expression_value(variable_context);
1752
1753 if (v != NULL) {
1754 ir_constant_data data = { { 0 } };
1755
1756 const unsigned swiz_idx[4] = {
1757 this->mask.x, this->mask.y, this->mask.z, this->mask.w
1758 };
1759
1760 for (unsigned i = 0; i < this->mask.num_components; i++) {
1761 switch (v->type->base_type) {
1762 case GLSL_TYPE_UINT:
1763 case GLSL_TYPE_INT: data.u[i] = v->value.u[swiz_idx[i]]; break;
1764 case GLSL_TYPE_FLOAT: data.f[i] = v->value.f[swiz_idx[i]]; break;
1765 case GLSL_TYPE_BOOL: data.b[i] = v->value.b[swiz_idx[i]]; break;
1766 case GLSL_TYPE_DOUBLE:data.d[i] = v->value.d[swiz_idx[i]]; break;
1767 default: assert(!"Should not get here."); break;
1768 }
1769 }
1770
1771 void *ctx = ralloc_parent(this);
1772 return new(ctx) ir_constant(this->type, &data);
1773 }
1774 return NULL;
1775 }
1776
1777
1778 ir_constant *
1779 ir_dereference_variable::constant_expression_value(struct hash_table *variable_context)
1780 {
1781 assert(var);
1782
1783 /* Give priority to the context hashtable, if it exists */
1784 if (variable_context) {
1785 ir_constant *value = (ir_constant *)hash_table_find(variable_context, var);
1786 if(value)
1787 return value;
1788 }
1789
1790 /* The constant_value of a uniform variable is its initializer,
1791 * not the lifetime constant value of the uniform.
1792 */
1793 if (var->data.mode == ir_var_uniform)
1794 return NULL;
1795
1796 if (!var->constant_value)
1797 return NULL;
1798
1799 return var->constant_value->clone(ralloc_parent(var), NULL);
1800 }
1801
1802
1803 ir_constant *
1804 ir_dereference_array::constant_expression_value(struct hash_table *variable_context)
1805 {
1806 ir_constant *array = this->array->constant_expression_value(variable_context);
1807 ir_constant *idx = this->array_index->constant_expression_value(variable_context);
1808
1809 if ((array != NULL) && (idx != NULL)) {
1810 void *ctx = ralloc_parent(this);
1811 if (array->type->is_matrix()) {
1812 /* Array access of a matrix results in a vector.
1813 */
1814 const unsigned column = idx->value.u[0];
1815
1816 const glsl_type *const column_type = array->type->column_type();
1817
1818 /* Offset in the constant matrix to the first element of the column
1819 * to be extracted.
1820 */
1821 const unsigned mat_idx = column * column_type->vector_elements;
1822
1823 ir_constant_data data = { { 0 } };
1824
1825 switch (column_type->base_type) {
1826 case GLSL_TYPE_UINT:
1827 case GLSL_TYPE_INT:
1828 for (unsigned i = 0; i < column_type->vector_elements; i++)
1829 data.u[i] = array->value.u[mat_idx + i];
1830
1831 break;
1832
1833 case GLSL_TYPE_FLOAT:
1834 for (unsigned i = 0; i < column_type->vector_elements; i++)
1835 data.f[i] = array->value.f[mat_idx + i];
1836
1837 break;
1838
1839 case GLSL_TYPE_DOUBLE:
1840 for (unsigned i = 0; i < column_type->vector_elements; i++)
1841 data.d[i] = array->value.d[mat_idx + i];
1842
1843 break;
1844
1845 default:
1846 assert(!"Should not get here.");
1847 break;
1848 }
1849
1850 return new(ctx) ir_constant(column_type, &data);
1851 } else if (array->type->is_vector()) {
1852 const unsigned component = idx->value.u[0];
1853
1854 return new(ctx) ir_constant(array, component);
1855 } else {
1856 const unsigned index = idx->value.u[0];
1857 return array->get_array_element(index)->clone(ctx, NULL);
1858 }
1859 }
1860 return NULL;
1861 }
1862
1863
1864 ir_constant *
1865 ir_dereference_record::constant_expression_value(struct hash_table *)
1866 {
1867 ir_constant *v = this->record->constant_expression_value();
1868
1869 return (v != NULL) ? v->get_record_field(this->field) : NULL;
1870 }
1871
1872
1873 ir_constant *
1874 ir_assignment::constant_expression_value(struct hash_table *)
1875 {
1876 /* FINISHME: Handle CEs involving assignment (return RHS) */
1877 return NULL;
1878 }
1879
1880
1881 ir_constant *
1882 ir_constant::constant_expression_value(struct hash_table *)
1883 {
1884 return this;
1885 }
1886
1887
1888 ir_constant *
1889 ir_call::constant_expression_value(struct hash_table *variable_context)
1890 {
1891 return this->callee->constant_expression_value(&this->actual_parameters, variable_context);
1892 }
1893
1894
1895 bool ir_function_signature::constant_expression_evaluate_expression_list(const struct exec_list &body,
1896 struct hash_table *variable_context,
1897 ir_constant **result)
1898 {
1899 foreach_in_list(ir_instruction, inst, &body) {
1900 switch(inst->ir_type) {
1901
1902 /* (declare () type symbol) */
1903 case ir_type_variable: {
1904 ir_variable *var = inst->as_variable();
1905 hash_table_insert(variable_context, ir_constant::zero(this, var->type), var);
1906 break;
1907 }
1908
1909 /* (assign [condition] (write-mask) (ref) (value)) */
1910 case ir_type_assignment: {
1911 ir_assignment *asg = inst->as_assignment();
1912 if (asg->condition) {
1913 ir_constant *cond = asg->condition->constant_expression_value(variable_context);
1914 if (!cond)
1915 return false;
1916 if (!cond->get_bool_component(0))
1917 break;
1918 }
1919
1920 ir_constant *store = NULL;
1921 int offset = 0;
1922
1923 if (!constant_referenced(asg->lhs, variable_context, store, offset))
1924 return false;
1925
1926 ir_constant *value = asg->rhs->constant_expression_value(variable_context);
1927
1928 if (!value)
1929 return false;
1930
1931 store->copy_masked_offset(value, offset, asg->write_mask);
1932 break;
1933 }
1934
1935 /* (return (expression)) */
1936 case ir_type_return:
1937 assert (result);
1938 *result = inst->as_return()->value->constant_expression_value(variable_context);
1939 return *result != NULL;
1940
1941 /* (call name (ref) (params))*/
1942 case ir_type_call: {
1943 ir_call *call = inst->as_call();
1944
1945 /* Just say no to void functions in constant expressions. We
1946 * don't need them at that point.
1947 */
1948
1949 if (!call->return_deref)
1950 return false;
1951
1952 ir_constant *store = NULL;
1953 int offset = 0;
1954
1955 if (!constant_referenced(call->return_deref, variable_context,
1956 store, offset))
1957 return false;
1958
1959 ir_constant *value = call->constant_expression_value(variable_context);
1960
1961 if(!value)
1962 return false;
1963
1964 store->copy_offset(value, offset);
1965 break;
1966 }
1967
1968 /* (if condition (then-instructions) (else-instructions)) */
1969 case ir_type_if: {
1970 ir_if *iif = inst->as_if();
1971
1972 ir_constant *cond = iif->condition->constant_expression_value(variable_context);
1973 if (!cond || !cond->type->is_boolean())
1974 return false;
1975
1976 exec_list &branch = cond->get_bool_component(0) ? iif->then_instructions : iif->else_instructions;
1977
1978 *result = NULL;
1979 if (!constant_expression_evaluate_expression_list(branch, variable_context, result))
1980 return false;
1981
1982 /* If there was a return in the branch chosen, drop out now. */
1983 if (*result)
1984 return true;
1985
1986 break;
1987 }
1988
1989 /* Every other expression type, we drop out. */
1990 default:
1991 return false;
1992 }
1993 }
1994
1995 /* Reaching the end of the block is not an error condition */
1996 if (result)
1997 *result = NULL;
1998
1999 return true;
2000 }
2001
2002 ir_constant *
2003 ir_function_signature::constant_expression_value(exec_list *actual_parameters, struct hash_table *variable_context)
2004 {
2005 const glsl_type *type = this->return_type;
2006 if (type == glsl_type::void_type)
2007 return NULL;
2008
2009 /* From the GLSL 1.20 spec, page 23:
2010 * "Function calls to user-defined functions (non-built-in functions)
2011 * cannot be used to form constant expressions."
2012 */
2013 if (!this->is_builtin())
2014 return NULL;
2015
2016 /*
2017 * Of the builtin functions, only the texture lookups and the noise
2018 * ones must not be used in constant expressions. They all include
2019 * specific opcodes so they don't need to be special-cased at this
2020 * point.
2021 */
2022
2023 /* Initialize the table of dereferencable names with the function
2024 * parameters. Verify their const-ness on the way.
2025 *
2026 * We expect the correctness of the number of parameters to have
2027 * been checked earlier.
2028 */
2029 hash_table *deref_hash = hash_table_ctor(8, hash_table_pointer_hash,
2030 hash_table_pointer_compare);
2031
2032 /* If "origin" is non-NULL, then the function body is there. So we
2033 * have to use the variable objects from the object with the body,
2034 * but the parameter instanciation on the current object.
2035 */
2036 const exec_node *parameter_info = origin ? origin->parameters.get_head_raw() : parameters.get_head_raw();
2037
2038 foreach_in_list(ir_rvalue, n, actual_parameters) {
2039 ir_constant *constant = n->constant_expression_value(variable_context);
2040 if (constant == NULL) {
2041 hash_table_dtor(deref_hash);
2042 return NULL;
2043 }
2044
2045
2046 ir_variable *var = (ir_variable *)parameter_info;
2047 hash_table_insert(deref_hash, constant, var);
2048
2049 parameter_info = parameter_info->next;
2050 }
2051
2052 ir_constant *result = NULL;
2053
2054 /* Now run the builtin function until something non-constant
2055 * happens or we get the result.
2056 */
2057 if (constant_expression_evaluate_expression_list(origin ? origin->body : body, deref_hash, &result) && result)
2058 result = result->clone(ralloc_parent(this), NULL);
2059
2060 hash_table_dtor(deref_hash);
2061
2062 return result;
2063 }