glsl/lower_instructions: Use float16 constants when appropriate
[mesa.git] / src / compiler / glsl / lower_instructions.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 lower_instructions.cpp
26 *
27 * Many GPUs lack native instructions for certain expression operations, and
28 * must replace them with some other expression tree. This pass lowers some
29 * of the most common cases, allowing the lowering code to be implemented once
30 * rather than in each driver backend.
31 *
32 * Currently supported transformations:
33 * - SUB_TO_ADD_NEG
34 * - DIV_TO_MUL_RCP
35 * - INT_DIV_TO_MUL_RCP
36 * - EXP_TO_EXP2
37 * - POW_TO_EXP2
38 * - LOG_TO_LOG2
39 * - MOD_TO_FLOOR
40 * - LDEXP_TO_ARITH
41 * - DFREXP_TO_ARITH
42 * - CARRY_TO_ARITH
43 * - BORROW_TO_ARITH
44 * - SAT_TO_CLAMP
45 * - DOPS_TO_DFRAC
46 *
47 * SUB_TO_ADD_NEG:
48 * ---------------
49 * Breaks an ir_binop_sub expression down to add(op0, neg(op1))
50 *
51 * This simplifies expression reassociation, and for many backends
52 * there is no subtract operation separate from adding the negation.
53 * For backends with native subtract operations, they will probably
54 * want to recognize add(op0, neg(op1)) or the other way around to
55 * produce a subtract anyway.
56 *
57 * FDIV_TO_MUL_RCP, DDIV_TO_MUL_RCP, and INT_DIV_TO_MUL_RCP:
58 * ---------------------------------------------------------
59 * Breaks an ir_binop_div expression down to op0 * (rcp(op1)).
60 *
61 * Many GPUs don't have a divide instruction (945 and 965 included),
62 * but they do have an RCP instruction to compute an approximate
63 * reciprocal. By breaking the operation down, constant reciprocals
64 * can get constant folded.
65 *
66 * FDIV_TO_MUL_RCP only lowers single-precision floating point division;
67 * DDIV_TO_MUL_RCP only lowers double-precision floating point division.
68 * DIV_TO_MUL_RCP is a convenience macro that sets both flags.
69 * INT_DIV_TO_MUL_RCP handles the integer case, converting to and from floating
70 * point so that RCP is possible.
71 *
72 * EXP_TO_EXP2 and LOG_TO_LOG2:
73 * ----------------------------
74 * Many GPUs don't have a base e log or exponent instruction, but they
75 * do have base 2 versions, so this pass converts exp and log to exp2
76 * and log2 operations.
77 *
78 * POW_TO_EXP2:
79 * -----------
80 * Many older GPUs don't have an x**y instruction. For these GPUs, convert
81 * x**y to 2**(y * log2(x)).
82 *
83 * MOD_TO_FLOOR:
84 * -------------
85 * Breaks an ir_binop_mod expression down to (op0 - op1 * floor(op0 / op1))
86 *
87 * Many GPUs don't have a MOD instruction (945 and 965 included), and
88 * if we have to break it down like this anyway, it gives an
89 * opportunity to do things like constant fold the (1.0 / op1) easily.
90 *
91 * Note: before we used to implement this as op1 * fract(op / op1) but this
92 * implementation had significant precision errors.
93 *
94 * LDEXP_TO_ARITH:
95 * -------------
96 * Converts ir_binop_ldexp to arithmetic and bit operations for float sources.
97 *
98 * DFREXP_DLDEXP_TO_ARITH:
99 * ---------------
100 * Converts ir_binop_ldexp, ir_unop_frexp_sig, and ir_unop_frexp_exp to
101 * arithmetic and bit ops for double arguments.
102 *
103 * CARRY_TO_ARITH:
104 * ---------------
105 * Converts ir_carry into (x + y) < x.
106 *
107 * BORROW_TO_ARITH:
108 * ----------------
109 * Converts ir_borrow into (x < y).
110 *
111 * SAT_TO_CLAMP:
112 * -------------
113 * Converts ir_unop_saturate into min(max(x, 0.0), 1.0)
114 *
115 * DOPS_TO_DFRAC:
116 * --------------
117 * Converts double trunc, ceil, floor, round to fract
118 */
119
120 #include "c99_math.h"
121 #include "program/prog_instruction.h" /* for swizzle */
122 #include "compiler/glsl_types.h"
123 #include "ir.h"
124 #include "ir_builder.h"
125 #include "ir_optimization.h"
126 #include "util/half_float.h"
127
128 using namespace ir_builder;
129
130 namespace {
131
132 class lower_instructions_visitor : public ir_hierarchical_visitor {
133 public:
134 lower_instructions_visitor(unsigned lower)
135 : progress(false), lower(lower) { }
136
137 ir_visitor_status visit_leave(ir_expression *);
138
139 bool progress;
140
141 private:
142 unsigned lower; /** Bitfield of which operations to lower */
143
144 void sub_to_add_neg(ir_expression *);
145 void div_to_mul_rcp(ir_expression *);
146 void int_div_to_mul_rcp(ir_expression *);
147 void mod_to_floor(ir_expression *);
148 void exp_to_exp2(ir_expression *);
149 void pow_to_exp2(ir_expression *);
150 void log_to_log2(ir_expression *);
151 void ldexp_to_arith(ir_expression *);
152 void dldexp_to_arith(ir_expression *);
153 void dfrexp_sig_to_arith(ir_expression *);
154 void dfrexp_exp_to_arith(ir_expression *);
155 void carry_to_arith(ir_expression *);
156 void borrow_to_arith(ir_expression *);
157 void sat_to_clamp(ir_expression *);
158 void double_dot_to_fma(ir_expression *);
159 void double_lrp(ir_expression *);
160 void dceil_to_dfrac(ir_expression *);
161 void dfloor_to_dfrac(ir_expression *);
162 void dround_even_to_dfrac(ir_expression *);
163 void dtrunc_to_dfrac(ir_expression *);
164 void dsign_to_csel(ir_expression *);
165 void bit_count_to_math(ir_expression *);
166 void extract_to_shifts(ir_expression *);
167 void insert_to_shifts(ir_expression *);
168 void reverse_to_shifts(ir_expression *ir);
169 void find_lsb_to_float_cast(ir_expression *ir);
170 void find_msb_to_float_cast(ir_expression *ir);
171 void imul_high_to_mul(ir_expression *ir);
172 void sqrt_to_abs_sqrt(ir_expression *ir);
173 void mul64_to_mul_and_mul_high(ir_expression *ir);
174
175 ir_expression *_carry(operand a, operand b);
176
177 static ir_constant *_imm_fp(void *mem_ctx,
178 const glsl_type *type,
179 double f,
180 unsigned vector_elements=1);
181 };
182
183 } /* anonymous namespace */
184
185 /**
186 * Determine if a particular type of lowering should occur
187 */
188 #define lowering(x) (this->lower & x)
189
190 bool
191 lower_instructions(exec_list *instructions, unsigned what_to_lower)
192 {
193 lower_instructions_visitor v(what_to_lower);
194
195 visit_list_elements(&v, instructions);
196 return v.progress;
197 }
198
199 void
200 lower_instructions_visitor::sub_to_add_neg(ir_expression *ir)
201 {
202 ir->operation = ir_binop_add;
203 ir->init_num_operands();
204 ir->operands[1] = new(ir) ir_expression(ir_unop_neg, ir->operands[1]->type,
205 ir->operands[1], NULL);
206 this->progress = true;
207 }
208
209 void
210 lower_instructions_visitor::div_to_mul_rcp(ir_expression *ir)
211 {
212 assert(ir->operands[1]->type->is_float() || ir->operands[1]->type->is_double());
213
214 /* New expression for the 1.0 / op1 */
215 ir_rvalue *expr;
216 expr = new(ir) ir_expression(ir_unop_rcp,
217 ir->operands[1]->type,
218 ir->operands[1]);
219
220 /* op0 / op1 -> op0 * (1.0 / op1) */
221 ir->operation = ir_binop_mul;
222 ir->init_num_operands();
223 ir->operands[1] = expr;
224
225 this->progress = true;
226 }
227
228 void
229 lower_instructions_visitor::int_div_to_mul_rcp(ir_expression *ir)
230 {
231 assert(ir->operands[1]->type->is_integer_32());
232
233 /* Be careful with integer division -- we need to do it as a
234 * float and re-truncate, since rcp(n > 1) of an integer would
235 * just be 0.
236 */
237 ir_rvalue *op0, *op1;
238 const struct glsl_type *vec_type;
239
240 vec_type = glsl_type::get_instance(GLSL_TYPE_FLOAT,
241 ir->operands[1]->type->vector_elements,
242 ir->operands[1]->type->matrix_columns);
243
244 if (ir->operands[1]->type->base_type == GLSL_TYPE_INT)
245 op1 = new(ir) ir_expression(ir_unop_i2f, vec_type, ir->operands[1], NULL);
246 else
247 op1 = new(ir) ir_expression(ir_unop_u2f, vec_type, ir->operands[1], NULL);
248
249 op1 = new(ir) ir_expression(ir_unop_rcp, op1->type, op1, NULL);
250
251 vec_type = glsl_type::get_instance(GLSL_TYPE_FLOAT,
252 ir->operands[0]->type->vector_elements,
253 ir->operands[0]->type->matrix_columns);
254
255 if (ir->operands[0]->type->base_type == GLSL_TYPE_INT)
256 op0 = new(ir) ir_expression(ir_unop_i2f, vec_type, ir->operands[0], NULL);
257 else
258 op0 = new(ir) ir_expression(ir_unop_u2f, vec_type, ir->operands[0], NULL);
259
260 vec_type = glsl_type::get_instance(GLSL_TYPE_FLOAT,
261 ir->type->vector_elements,
262 ir->type->matrix_columns);
263
264 op0 = new(ir) ir_expression(ir_binop_mul, vec_type, op0, op1);
265
266 if (ir->operands[1]->type->base_type == GLSL_TYPE_INT) {
267 ir->operation = ir_unop_f2i;
268 ir->operands[0] = op0;
269 } else {
270 ir->operation = ir_unop_i2u;
271 ir->operands[0] = new(ir) ir_expression(ir_unop_f2i, op0);
272 }
273 ir->init_num_operands();
274 ir->operands[1] = NULL;
275
276 this->progress = true;
277 }
278
279 void
280 lower_instructions_visitor::exp_to_exp2(ir_expression *ir)
281 {
282 ir_constant *log2_e = _imm_fp(ir, ir->type, M_LOG2E);
283
284 ir->operation = ir_unop_exp2;
285 ir->init_num_operands();
286 ir->operands[0] = new(ir) ir_expression(ir_binop_mul, ir->operands[0]->type,
287 ir->operands[0], log2_e);
288 this->progress = true;
289 }
290
291 void
292 lower_instructions_visitor::pow_to_exp2(ir_expression *ir)
293 {
294 ir_expression *const log2_x =
295 new(ir) ir_expression(ir_unop_log2, ir->operands[0]->type,
296 ir->operands[0]);
297
298 ir->operation = ir_unop_exp2;
299 ir->init_num_operands();
300 ir->operands[0] = new(ir) ir_expression(ir_binop_mul, ir->operands[1]->type,
301 ir->operands[1], log2_x);
302 ir->operands[1] = NULL;
303 this->progress = true;
304 }
305
306 void
307 lower_instructions_visitor::log_to_log2(ir_expression *ir)
308 {
309 ir->operation = ir_binop_mul;
310 ir->init_num_operands();
311 ir->operands[0] = new(ir) ir_expression(ir_unop_log2, ir->operands[0]->type,
312 ir->operands[0], NULL);
313 ir->operands[1] = _imm_fp(ir, ir->operands[0]->type, 1.0 / M_LOG2E);
314 this->progress = true;
315 }
316
317 void
318 lower_instructions_visitor::mod_to_floor(ir_expression *ir)
319 {
320 ir_variable *x = new(ir) ir_variable(ir->operands[0]->type, "mod_x",
321 ir_var_temporary);
322 ir_variable *y = new(ir) ir_variable(ir->operands[1]->type, "mod_y",
323 ir_var_temporary);
324 this->base_ir->insert_before(x);
325 this->base_ir->insert_before(y);
326
327 ir_assignment *const assign_x =
328 new(ir) ir_assignment(new(ir) ir_dereference_variable(x),
329 ir->operands[0]);
330 ir_assignment *const assign_y =
331 new(ir) ir_assignment(new(ir) ir_dereference_variable(y),
332 ir->operands[1]);
333
334 this->base_ir->insert_before(assign_x);
335 this->base_ir->insert_before(assign_y);
336
337 ir_expression *const div_expr =
338 new(ir) ir_expression(ir_binop_div, x->type,
339 new(ir) ir_dereference_variable(x),
340 new(ir) ir_dereference_variable(y));
341
342 /* Don't generate new IR that would need to be lowered in an additional
343 * pass.
344 */
345 if ((lowering(FDIV_TO_MUL_RCP) && ir->type->is_float()) ||
346 (lowering(DDIV_TO_MUL_RCP) && ir->type->is_double()))
347 div_to_mul_rcp(div_expr);
348
349 ir_expression *const floor_expr =
350 new(ir) ir_expression(ir_unop_floor, x->type, div_expr);
351
352 if (lowering(DOPS_TO_DFRAC) && ir->type->is_double())
353 dfloor_to_dfrac(floor_expr);
354
355 ir_expression *const mul_expr =
356 new(ir) ir_expression(ir_binop_mul,
357 new(ir) ir_dereference_variable(y),
358 floor_expr);
359
360 ir->operation = ir_binop_sub;
361 ir->init_num_operands();
362 ir->operands[0] = new(ir) ir_dereference_variable(x);
363 ir->operands[1] = mul_expr;
364 this->progress = true;
365 }
366
367 void
368 lower_instructions_visitor::ldexp_to_arith(ir_expression *ir)
369 {
370 /* Translates
371 * ir_binop_ldexp x exp
372 * into
373 *
374 * extracted_biased_exp = rshift(bitcast_f2i(abs(x)), exp_shift);
375 * resulting_biased_exp = min(extracted_biased_exp + exp, 255);
376 *
377 * if (extracted_biased_exp >= 255)
378 * return x; // +/-inf, NaN
379 *
380 * sign_mantissa = bitcast_f2u(x) & sign_mantissa_mask;
381 *
382 * if (min(resulting_biased_exp, extracted_biased_exp) < 1)
383 * resulting_biased_exp = 0;
384 * if (resulting_biased_exp >= 255 ||
385 * min(resulting_biased_exp, extracted_biased_exp) < 1) {
386 * sign_mantissa &= sign_mask;
387 * }
388 *
389 * return bitcast_u2f(sign_mantissa |
390 * lshift(i2u(resulting_biased_exp), exp_shift));
391 *
392 * which we can't actually implement as such, since the GLSL IR doesn't
393 * have vectorized if-statements. We actually implement it without branches
394 * using conditional-select:
395 *
396 * extracted_biased_exp = rshift(bitcast_f2i(abs(x)), exp_shift);
397 * resulting_biased_exp = min(extracted_biased_exp + exp, 255);
398 *
399 * sign_mantissa = bitcast_f2u(x) & sign_mantissa_mask;
400 *
401 * flush_to_zero = lequal(min(resulting_biased_exp, extracted_biased_exp), 0);
402 * resulting_biased_exp = csel(flush_to_zero, 0, resulting_biased_exp)
403 * zero_mantissa = logic_or(flush_to_zero,
404 * gequal(resulting_biased_exp, 255));
405 * sign_mantissa = csel(zero_mantissa, sign_mantissa & sign_mask, sign_mantissa);
406 *
407 * result = sign_mantissa |
408 * lshift(i2u(resulting_biased_exp), exp_shift));
409 *
410 * return csel(extracted_biased_exp >= 255, x, bitcast_u2f(result));
411 *
412 * The definition of ldexp in the GLSL spec says:
413 *
414 * "If this product is too large to be represented in the
415 * floating-point type, the result is undefined."
416 *
417 * However, the definition of ldexp in the GLSL ES spec does not contain
418 * this sentence, so we do need to handle overflow correctly.
419 *
420 * There is additional language limiting the defined range of exp, but this
421 * is merely to allow implementations that store 2^exp in a temporary
422 * variable.
423 */
424
425 const unsigned vec_elem = ir->type->vector_elements;
426
427 /* Types */
428 const glsl_type *ivec = glsl_type::get_instance(GLSL_TYPE_INT, vec_elem, 1);
429 const glsl_type *uvec = glsl_type::get_instance(GLSL_TYPE_UINT, vec_elem, 1);
430 const glsl_type *bvec = glsl_type::get_instance(GLSL_TYPE_BOOL, vec_elem, 1);
431
432 /* Temporary variables */
433 ir_variable *x = new(ir) ir_variable(ir->type, "x", ir_var_temporary);
434 ir_variable *exp = new(ir) ir_variable(ivec, "exp", ir_var_temporary);
435 ir_variable *result = new(ir) ir_variable(uvec, "result", ir_var_temporary);
436
437 ir_variable *extracted_biased_exp =
438 new(ir) ir_variable(ivec, "extracted_biased_exp", ir_var_temporary);
439 ir_variable *resulting_biased_exp =
440 new(ir) ir_variable(ivec, "resulting_biased_exp", ir_var_temporary);
441
442 ir_variable *sign_mantissa =
443 new(ir) ir_variable(uvec, "sign_mantissa", ir_var_temporary);
444
445 ir_variable *flush_to_zero =
446 new(ir) ir_variable(bvec, "flush_to_zero", ir_var_temporary);
447 ir_variable *zero_mantissa =
448 new(ir) ir_variable(bvec, "zero_mantissa", ir_var_temporary);
449
450 ir_instruction &i = *base_ir;
451
452 /* Copy <x> and <exp> arguments. */
453 i.insert_before(x);
454 i.insert_before(assign(x, ir->operands[0]));
455 i.insert_before(exp);
456 i.insert_before(assign(exp, ir->operands[1]));
457
458 /* Extract the biased exponent from <x>. */
459 i.insert_before(extracted_biased_exp);
460 i.insert_before(assign(extracted_biased_exp,
461 rshift(bitcast_f2i(abs(x)),
462 new(ir) ir_constant(23, vec_elem))));
463
464 /* The definition of ldexp in the GLSL 4.60 spec says:
465 *
466 * "If exp is greater than +128 (single-precision) or +1024
467 * (double-precision), the value returned is undefined. If exp is less
468 * than -126 (single-precision) or -1022 (double-precision), the value
469 * returned may be flushed to zero."
470 *
471 * So we do not have to guard against the possibility of addition overflow,
472 * which could happen when exp is close to INT_MAX. Addition underflow
473 * cannot happen (the worst case is 0 + (-INT_MAX)).
474 */
475 i.insert_before(resulting_biased_exp);
476 i.insert_before(assign(resulting_biased_exp,
477 min2(add(extracted_biased_exp, exp),
478 new(ir) ir_constant(255, vec_elem))));
479
480 i.insert_before(sign_mantissa);
481 i.insert_before(assign(sign_mantissa,
482 bit_and(bitcast_f2u(x),
483 new(ir) ir_constant(0x807fffffu, vec_elem))));
484
485 /* We flush to zero if the original or resulting biased exponent is 0,
486 * indicating a +/-0.0 or subnormal input or output.
487 *
488 * The mantissa is set to 0 if the resulting biased exponent is 255, since
489 * an overflow should produce a +/-inf result.
490 *
491 * Note that NaN inputs are handled separately.
492 */
493 i.insert_before(flush_to_zero);
494 i.insert_before(assign(flush_to_zero,
495 lequal(min2(resulting_biased_exp,
496 extracted_biased_exp),
497 ir_constant::zero(ir, ivec))));
498 i.insert_before(assign(resulting_biased_exp,
499 csel(flush_to_zero,
500 ir_constant::zero(ir, ivec),
501 resulting_biased_exp)));
502
503 i.insert_before(zero_mantissa);
504 i.insert_before(assign(zero_mantissa,
505 logic_or(flush_to_zero,
506 equal(resulting_biased_exp,
507 new(ir) ir_constant(255, vec_elem)))));
508 i.insert_before(assign(sign_mantissa,
509 csel(zero_mantissa,
510 bit_and(sign_mantissa,
511 new(ir) ir_constant(0x80000000u, vec_elem)),
512 sign_mantissa)));
513
514 /* Don't generate new IR that would need to be lowered in an additional
515 * pass.
516 */
517 i.insert_before(result);
518 if (!lowering(INSERT_TO_SHIFTS)) {
519 i.insert_before(assign(result,
520 bitfield_insert(sign_mantissa,
521 i2u(resulting_biased_exp),
522 new(ir) ir_constant(23u, vec_elem),
523 new(ir) ir_constant(8u, vec_elem))));
524 } else {
525 i.insert_before(assign(result,
526 bit_or(sign_mantissa,
527 lshift(i2u(resulting_biased_exp),
528 new(ir) ir_constant(23, vec_elem)))));
529 }
530
531 ir->operation = ir_triop_csel;
532 ir->init_num_operands();
533 ir->operands[0] = gequal(extracted_biased_exp,
534 new(ir) ir_constant(255, vec_elem));
535 ir->operands[1] = new(ir) ir_dereference_variable(x);
536 ir->operands[2] = bitcast_u2f(result);
537
538 this->progress = true;
539 }
540
541 void
542 lower_instructions_visitor::dldexp_to_arith(ir_expression *ir)
543 {
544 /* See ldexp_to_arith for structure. Uses frexp_exp to extract the exponent
545 * from the significand.
546 */
547
548 const unsigned vec_elem = ir->type->vector_elements;
549
550 /* Types */
551 const glsl_type *ivec = glsl_type::get_instance(GLSL_TYPE_INT, vec_elem, 1);
552 const glsl_type *bvec = glsl_type::get_instance(GLSL_TYPE_BOOL, vec_elem, 1);
553
554 /* Constants */
555 ir_constant *zeroi = ir_constant::zero(ir, ivec);
556
557 ir_constant *sign_mask = new(ir) ir_constant(0x80000000u);
558
559 ir_constant *exp_shift = new(ir) ir_constant(20u);
560 ir_constant *exp_width = new(ir) ir_constant(11u);
561 ir_constant *exp_bias = new(ir) ir_constant(1022, vec_elem);
562
563 /* Temporary variables */
564 ir_variable *x = new(ir) ir_variable(ir->type, "x", ir_var_temporary);
565 ir_variable *exp = new(ir) ir_variable(ivec, "exp", ir_var_temporary);
566
567 ir_variable *zero_sign_x = new(ir) ir_variable(ir->type, "zero_sign_x",
568 ir_var_temporary);
569
570 ir_variable *extracted_biased_exp =
571 new(ir) ir_variable(ivec, "extracted_biased_exp", ir_var_temporary);
572 ir_variable *resulting_biased_exp =
573 new(ir) ir_variable(ivec, "resulting_biased_exp", ir_var_temporary);
574
575 ir_variable *is_not_zero_or_underflow =
576 new(ir) ir_variable(bvec, "is_not_zero_or_underflow", ir_var_temporary);
577
578 ir_instruction &i = *base_ir;
579
580 /* Copy <x> and <exp> arguments. */
581 i.insert_before(x);
582 i.insert_before(assign(x, ir->operands[0]));
583 i.insert_before(exp);
584 i.insert_before(assign(exp, ir->operands[1]));
585
586 ir_expression *frexp_exp = expr(ir_unop_frexp_exp, x);
587 if (lowering(DFREXP_DLDEXP_TO_ARITH))
588 dfrexp_exp_to_arith(frexp_exp);
589
590 /* Extract the biased exponent from <x>. */
591 i.insert_before(extracted_biased_exp);
592 i.insert_before(assign(extracted_biased_exp, add(frexp_exp, exp_bias)));
593
594 i.insert_before(resulting_biased_exp);
595 i.insert_before(assign(resulting_biased_exp,
596 add(extracted_biased_exp, exp)));
597
598 /* Test if result is ±0.0, subnormal, or underflow by checking if the
599 * resulting biased exponent would be less than 0x1. If so, the result is
600 * 0.0 with the sign of x. (Actually, invert the conditions so that
601 * immediate values are the second arguments, which is better for i965)
602 * TODO: Implement in a vector fashion.
603 */
604 i.insert_before(zero_sign_x);
605 for (unsigned elem = 0; elem < vec_elem; elem++) {
606 ir_variable *unpacked =
607 new(ir) ir_variable(glsl_type::uvec2_type, "unpacked", ir_var_temporary);
608 i.insert_before(unpacked);
609 i.insert_before(
610 assign(unpacked,
611 expr(ir_unop_unpack_double_2x32, swizzle(x, elem, 1))));
612 i.insert_before(assign(unpacked, bit_and(swizzle_y(unpacked), sign_mask->clone(ir, NULL)),
613 WRITEMASK_Y));
614 i.insert_before(assign(unpacked, ir_constant::zero(ir, glsl_type::uint_type), WRITEMASK_X));
615 i.insert_before(assign(zero_sign_x,
616 expr(ir_unop_pack_double_2x32, unpacked),
617 1 << elem));
618 }
619 i.insert_before(is_not_zero_or_underflow);
620 i.insert_before(assign(is_not_zero_or_underflow,
621 gequal(resulting_biased_exp,
622 new(ir) ir_constant(0x1, vec_elem))));
623 i.insert_before(assign(x, csel(is_not_zero_or_underflow,
624 x, zero_sign_x)));
625 i.insert_before(assign(resulting_biased_exp,
626 csel(is_not_zero_or_underflow,
627 resulting_biased_exp, zeroi)));
628
629 /* We could test for overflows by checking if the resulting biased exponent
630 * would be greater than 0xFE. Turns out we don't need to because the GLSL
631 * spec says:
632 *
633 * "If this product is too large to be represented in the
634 * floating-point type, the result is undefined."
635 */
636
637 ir_rvalue *results[4] = {NULL};
638 for (unsigned elem = 0; elem < vec_elem; elem++) {
639 ir_variable *unpacked =
640 new(ir) ir_variable(glsl_type::uvec2_type, "unpacked", ir_var_temporary);
641 i.insert_before(unpacked);
642 i.insert_before(
643 assign(unpacked,
644 expr(ir_unop_unpack_double_2x32, swizzle(x, elem, 1))));
645
646 ir_expression *bfi = bitfield_insert(
647 swizzle_y(unpacked),
648 i2u(swizzle(resulting_biased_exp, elem, 1)),
649 exp_shift->clone(ir, NULL),
650 exp_width->clone(ir, NULL));
651
652 i.insert_before(assign(unpacked, bfi, WRITEMASK_Y));
653
654 results[elem] = expr(ir_unop_pack_double_2x32, unpacked);
655 }
656
657 ir->operation = ir_quadop_vector;
658 ir->init_num_operands();
659 ir->operands[0] = results[0];
660 ir->operands[1] = results[1];
661 ir->operands[2] = results[2];
662 ir->operands[3] = results[3];
663
664 /* Don't generate new IR that would need to be lowered in an additional
665 * pass.
666 */
667
668 this->progress = true;
669 }
670
671 void
672 lower_instructions_visitor::dfrexp_sig_to_arith(ir_expression *ir)
673 {
674 const unsigned vec_elem = ir->type->vector_elements;
675 const glsl_type *bvec = glsl_type::get_instance(GLSL_TYPE_BOOL, vec_elem, 1);
676
677 /* Double-precision floating-point values are stored as
678 * 1 sign bit;
679 * 11 exponent bits;
680 * 52 mantissa bits.
681 *
682 * We're just extracting the significand here, so we only need to modify
683 * the upper 32-bit uint. Unfortunately we must extract each double
684 * independently as there is no vector version of unpackDouble.
685 */
686
687 ir_instruction &i = *base_ir;
688
689 ir_variable *is_not_zero =
690 new(ir) ir_variable(bvec, "is_not_zero", ir_var_temporary);
691 ir_rvalue *results[4] = {NULL};
692
693 ir_constant *dzero = new(ir) ir_constant(0.0, vec_elem);
694 i.insert_before(is_not_zero);
695 i.insert_before(
696 assign(is_not_zero,
697 nequal(abs(ir->operands[0]->clone(ir, NULL)), dzero)));
698
699 /* TODO: Remake this as more vector-friendly when int64 support is
700 * available.
701 */
702 for (unsigned elem = 0; elem < vec_elem; elem++) {
703 ir_constant *zero = new(ir) ir_constant(0u, 1);
704 ir_constant *sign_mantissa_mask = new(ir) ir_constant(0x800fffffu, 1);
705
706 /* Exponent of double floating-point values in the range [0.5, 1.0). */
707 ir_constant *exponent_value = new(ir) ir_constant(0x3fe00000u, 1);
708
709 ir_variable *bits =
710 new(ir) ir_variable(glsl_type::uint_type, "bits", ir_var_temporary);
711 ir_variable *unpacked =
712 new(ir) ir_variable(glsl_type::uvec2_type, "unpacked", ir_var_temporary);
713
714 ir_rvalue *x = swizzle(ir->operands[0]->clone(ir, NULL), elem, 1);
715
716 i.insert_before(bits);
717 i.insert_before(unpacked);
718 i.insert_before(assign(unpacked, expr(ir_unop_unpack_double_2x32, x)));
719
720 /* Manipulate the high uint to remove the exponent and replace it with
721 * either the default exponent or zero.
722 */
723 i.insert_before(assign(bits, swizzle_y(unpacked)));
724 i.insert_before(assign(bits, bit_and(bits, sign_mantissa_mask)));
725 i.insert_before(assign(bits, bit_or(bits,
726 csel(swizzle(is_not_zero, elem, 1),
727 exponent_value,
728 zero))));
729 i.insert_before(assign(unpacked, bits, WRITEMASK_Y));
730 results[elem] = expr(ir_unop_pack_double_2x32, unpacked);
731 }
732
733 /* Put the dvec back together */
734 ir->operation = ir_quadop_vector;
735 ir->init_num_operands();
736 ir->operands[0] = results[0];
737 ir->operands[1] = results[1];
738 ir->operands[2] = results[2];
739 ir->operands[3] = results[3];
740
741 this->progress = true;
742 }
743
744 void
745 lower_instructions_visitor::dfrexp_exp_to_arith(ir_expression *ir)
746 {
747 const unsigned vec_elem = ir->type->vector_elements;
748 const glsl_type *bvec = glsl_type::get_instance(GLSL_TYPE_BOOL, vec_elem, 1);
749 const glsl_type *uvec = glsl_type::get_instance(GLSL_TYPE_UINT, vec_elem, 1);
750
751 /* Double-precision floating-point values are stored as
752 * 1 sign bit;
753 * 11 exponent bits;
754 * 52 mantissa bits.
755 *
756 * We're just extracting the exponent here, so we only care about the upper
757 * 32-bit uint.
758 */
759
760 ir_instruction &i = *base_ir;
761
762 ir_variable *is_not_zero =
763 new(ir) ir_variable(bvec, "is_not_zero", ir_var_temporary);
764 ir_variable *high_words =
765 new(ir) ir_variable(uvec, "high_words", ir_var_temporary);
766 ir_constant *dzero = new(ir) ir_constant(0.0, vec_elem);
767 ir_constant *izero = new(ir) ir_constant(0, vec_elem);
768
769 ir_rvalue *absval = abs(ir->operands[0]);
770
771 i.insert_before(is_not_zero);
772 i.insert_before(high_words);
773 i.insert_before(assign(is_not_zero, nequal(absval->clone(ir, NULL), dzero)));
774
775 /* Extract all of the upper uints. */
776 for (unsigned elem = 0; elem < vec_elem; elem++) {
777 ir_rvalue *x = swizzle(absval->clone(ir, NULL), elem, 1);
778
779 i.insert_before(assign(high_words,
780 swizzle_y(expr(ir_unop_unpack_double_2x32, x)),
781 1 << elem));
782
783 }
784 ir_constant *exponent_shift = new(ir) ir_constant(20, vec_elem);
785 ir_constant *exponent_bias = new(ir) ir_constant(-1022, vec_elem);
786
787 /* For non-zero inputs, shift the exponent down and apply bias. */
788 ir->operation = ir_triop_csel;
789 ir->init_num_operands();
790 ir->operands[0] = new(ir) ir_dereference_variable(is_not_zero);
791 ir->operands[1] = add(exponent_bias, u2i(rshift(high_words, exponent_shift)));
792 ir->operands[2] = izero;
793
794 this->progress = true;
795 }
796
797 void
798 lower_instructions_visitor::carry_to_arith(ir_expression *ir)
799 {
800 /* Translates
801 * ir_binop_carry x y
802 * into
803 * sum = ir_binop_add x y
804 * bcarry = ir_binop_less sum x
805 * carry = ir_unop_b2i bcarry
806 */
807
808 ir_rvalue *x_clone = ir->operands[0]->clone(ir, NULL);
809 ir->operation = ir_unop_i2u;
810 ir->init_num_operands();
811 ir->operands[0] = b2i(less(add(ir->operands[0], ir->operands[1]), x_clone));
812 ir->operands[1] = NULL;
813
814 this->progress = true;
815 }
816
817 void
818 lower_instructions_visitor::borrow_to_arith(ir_expression *ir)
819 {
820 /* Translates
821 * ir_binop_borrow x y
822 * into
823 * bcarry = ir_binop_less x y
824 * carry = ir_unop_b2i bcarry
825 */
826
827 ir->operation = ir_unop_i2u;
828 ir->init_num_operands();
829 ir->operands[0] = b2i(less(ir->operands[0], ir->operands[1]));
830 ir->operands[1] = NULL;
831
832 this->progress = true;
833 }
834
835 void
836 lower_instructions_visitor::sat_to_clamp(ir_expression *ir)
837 {
838 /* Translates
839 * ir_unop_saturate x
840 * into
841 * ir_binop_min (ir_binop_max(x, 0.0), 1.0)
842 */
843
844 ir->operation = ir_binop_min;
845 ir->init_num_operands();
846
847 ir_constant *zero = _imm_fp(ir, ir->operands[0]->type, 0.0);
848 ir->operands[0] = new(ir) ir_expression(ir_binop_max, ir->operands[0]->type,
849 ir->operands[0], zero);
850 ir->operands[1] = _imm_fp(ir, ir->operands[0]->type, 1.0);
851
852 this->progress = true;
853 }
854
855 void
856 lower_instructions_visitor::double_dot_to_fma(ir_expression *ir)
857 {
858 ir_variable *temp = new(ir) ir_variable(ir->operands[0]->type->get_base_type(), "dot_res",
859 ir_var_temporary);
860 this->base_ir->insert_before(temp);
861
862 int nc = ir->operands[0]->type->components();
863 for (int i = nc - 1; i >= 1; i--) {
864 ir_assignment *assig;
865 if (i == (nc - 1)) {
866 assig = assign(temp, mul(swizzle(ir->operands[0]->clone(ir, NULL), i, 1),
867 swizzle(ir->operands[1]->clone(ir, NULL), i, 1)));
868 } else {
869 assig = assign(temp, fma(swizzle(ir->operands[0]->clone(ir, NULL), i, 1),
870 swizzle(ir->operands[1]->clone(ir, NULL), i, 1),
871 temp));
872 }
873 this->base_ir->insert_before(assig);
874 }
875
876 ir->operation = ir_triop_fma;
877 ir->init_num_operands();
878 ir->operands[0] = swizzle(ir->operands[0], 0, 1);
879 ir->operands[1] = swizzle(ir->operands[1], 0, 1);
880 ir->operands[2] = new(ir) ir_dereference_variable(temp);
881
882 this->progress = true;
883
884 }
885
886 void
887 lower_instructions_visitor::double_lrp(ir_expression *ir)
888 {
889 int swizval;
890 ir_rvalue *op0 = ir->operands[0], *op2 = ir->operands[2];
891 ir_constant *one = new(ir) ir_constant(1.0, op2->type->vector_elements);
892
893 switch (op2->type->vector_elements) {
894 case 1:
895 swizval = SWIZZLE_XXXX;
896 break;
897 default:
898 assert(op0->type->vector_elements == op2->type->vector_elements);
899 swizval = SWIZZLE_XYZW;
900 break;
901 }
902
903 ir->operation = ir_triop_fma;
904 ir->init_num_operands();
905 ir->operands[0] = swizzle(op2, swizval, op0->type->vector_elements);
906 ir->operands[2] = mul(sub(one, op2->clone(ir, NULL)), op0);
907
908 this->progress = true;
909 }
910
911 void
912 lower_instructions_visitor::dceil_to_dfrac(ir_expression *ir)
913 {
914 /*
915 * frtemp = frac(x);
916 * temp = sub(x, frtemp);
917 * result = temp + ((frtemp != 0.0) ? 1.0 : 0.0);
918 */
919 ir_instruction &i = *base_ir;
920 ir_constant *zero = new(ir) ir_constant(0.0, ir->operands[0]->type->vector_elements);
921 ir_constant *one = new(ir) ir_constant(1.0, ir->operands[0]->type->vector_elements);
922 ir_variable *frtemp = new(ir) ir_variable(ir->operands[0]->type, "frtemp",
923 ir_var_temporary);
924
925 i.insert_before(frtemp);
926 i.insert_before(assign(frtemp, fract(ir->operands[0])));
927
928 ir->operation = ir_binop_add;
929 ir->init_num_operands();
930 ir->operands[0] = sub(ir->operands[0]->clone(ir, NULL), frtemp);
931 ir->operands[1] = csel(nequal(frtemp, zero), one, zero->clone(ir, NULL));
932
933 this->progress = true;
934 }
935
936 void
937 lower_instructions_visitor::dfloor_to_dfrac(ir_expression *ir)
938 {
939 /*
940 * frtemp = frac(x);
941 * result = sub(x, frtemp);
942 */
943 ir->operation = ir_binop_sub;
944 ir->init_num_operands();
945 ir->operands[1] = fract(ir->operands[0]->clone(ir, NULL));
946
947 this->progress = true;
948 }
949 void
950 lower_instructions_visitor::dround_even_to_dfrac(ir_expression *ir)
951 {
952 /*
953 * insane but works
954 * temp = x + 0.5;
955 * frtemp = frac(temp);
956 * t2 = sub(temp, frtemp);
957 * if (frac(x) == 0.5)
958 * result = frac(t2 * 0.5) == 0 ? t2 : t2 - 1;
959 * else
960 * result = t2;
961
962 */
963 ir_instruction &i = *base_ir;
964 ir_variable *frtemp = new(ir) ir_variable(ir->operands[0]->type, "frtemp",
965 ir_var_temporary);
966 ir_variable *temp = new(ir) ir_variable(ir->operands[0]->type, "temp",
967 ir_var_temporary);
968 ir_variable *t2 = new(ir) ir_variable(ir->operands[0]->type, "t2",
969 ir_var_temporary);
970 ir_constant *p5 = new(ir) ir_constant(0.5, ir->operands[0]->type->vector_elements);
971 ir_constant *one = new(ir) ir_constant(1.0, ir->operands[0]->type->vector_elements);
972 ir_constant *zero = new(ir) ir_constant(0.0, ir->operands[0]->type->vector_elements);
973
974 i.insert_before(temp);
975 i.insert_before(assign(temp, add(ir->operands[0], p5)));
976
977 i.insert_before(frtemp);
978 i.insert_before(assign(frtemp, fract(temp)));
979
980 i.insert_before(t2);
981 i.insert_before(assign(t2, sub(temp, frtemp)));
982
983 ir->operation = ir_triop_csel;
984 ir->init_num_operands();
985 ir->operands[0] = equal(fract(ir->operands[0]->clone(ir, NULL)),
986 p5->clone(ir, NULL));
987 ir->operands[1] = csel(equal(fract(mul(t2, p5->clone(ir, NULL))),
988 zero),
989 t2,
990 sub(t2, one));
991 ir->operands[2] = new(ir) ir_dereference_variable(t2);
992
993 this->progress = true;
994 }
995
996 void
997 lower_instructions_visitor::dtrunc_to_dfrac(ir_expression *ir)
998 {
999 /*
1000 * frtemp = frac(x);
1001 * temp = sub(x, frtemp);
1002 * result = x >= 0 ? temp : temp + (frtemp == 0.0) ? 0 : 1;
1003 */
1004 ir_rvalue *arg = ir->operands[0];
1005 ir_instruction &i = *base_ir;
1006
1007 ir_constant *zero = new(ir) ir_constant(0.0, arg->type->vector_elements);
1008 ir_constant *one = new(ir) ir_constant(1.0, arg->type->vector_elements);
1009 ir_variable *frtemp = new(ir) ir_variable(arg->type, "frtemp",
1010 ir_var_temporary);
1011 ir_variable *temp = new(ir) ir_variable(ir->operands[0]->type, "temp",
1012 ir_var_temporary);
1013
1014 i.insert_before(frtemp);
1015 i.insert_before(assign(frtemp, fract(arg)));
1016 i.insert_before(temp);
1017 i.insert_before(assign(temp, sub(arg->clone(ir, NULL), frtemp)));
1018
1019 ir->operation = ir_triop_csel;
1020 ir->init_num_operands();
1021 ir->operands[0] = gequal(arg->clone(ir, NULL), zero);
1022 ir->operands[1] = new (ir) ir_dereference_variable(temp);
1023 ir->operands[2] = add(temp,
1024 csel(equal(frtemp, zero->clone(ir, NULL)),
1025 zero->clone(ir, NULL),
1026 one));
1027
1028 this->progress = true;
1029 }
1030
1031 void
1032 lower_instructions_visitor::dsign_to_csel(ir_expression *ir)
1033 {
1034 /*
1035 * temp = x > 0.0 ? 1.0 : 0.0;
1036 * result = x < 0.0 ? -1.0 : temp;
1037 */
1038 ir_rvalue *arg = ir->operands[0];
1039 ir_constant *zero = new(ir) ir_constant(0.0, arg->type->vector_elements);
1040 ir_constant *one = new(ir) ir_constant(1.0, arg->type->vector_elements);
1041 ir_constant *neg_one = new(ir) ir_constant(-1.0, arg->type->vector_elements);
1042
1043 ir->operation = ir_triop_csel;
1044 ir->init_num_operands();
1045 ir->operands[0] = less(arg->clone(ir, NULL),
1046 zero->clone(ir, NULL));
1047 ir->operands[1] = neg_one;
1048 ir->operands[2] = csel(greater(arg, zero),
1049 one,
1050 zero->clone(ir, NULL));
1051
1052 this->progress = true;
1053 }
1054
1055 void
1056 lower_instructions_visitor::bit_count_to_math(ir_expression *ir)
1057 {
1058 /* For more details, see:
1059 *
1060 * http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetPaallel
1061 */
1062 const unsigned elements = ir->operands[0]->type->vector_elements;
1063 ir_variable *temp = new(ir) ir_variable(glsl_type::uvec(elements), "temp",
1064 ir_var_temporary);
1065 ir_constant *c55555555 = new(ir) ir_constant(0x55555555u);
1066 ir_constant *c33333333 = new(ir) ir_constant(0x33333333u);
1067 ir_constant *c0F0F0F0F = new(ir) ir_constant(0x0F0F0F0Fu);
1068 ir_constant *c01010101 = new(ir) ir_constant(0x01010101u);
1069 ir_constant *c1 = new(ir) ir_constant(1u);
1070 ir_constant *c2 = new(ir) ir_constant(2u);
1071 ir_constant *c4 = new(ir) ir_constant(4u);
1072 ir_constant *c24 = new(ir) ir_constant(24u);
1073
1074 base_ir->insert_before(temp);
1075
1076 if (ir->operands[0]->type->base_type == GLSL_TYPE_UINT) {
1077 base_ir->insert_before(assign(temp, ir->operands[0]));
1078 } else {
1079 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
1080 base_ir->insert_before(assign(temp, i2u(ir->operands[0])));
1081 }
1082
1083 /* temp = temp - ((temp >> 1) & 0x55555555u); */
1084 base_ir->insert_before(assign(temp, sub(temp, bit_and(rshift(temp, c1),
1085 c55555555))));
1086
1087 /* temp = (temp & 0x33333333u) + ((temp >> 2) & 0x33333333u); */
1088 base_ir->insert_before(assign(temp, add(bit_and(temp, c33333333),
1089 bit_and(rshift(temp, c2),
1090 c33333333->clone(ir, NULL)))));
1091
1092 /* int(((temp + (temp >> 4) & 0xF0F0F0Fu) * 0x1010101u) >> 24); */
1093 ir->operation = ir_unop_u2i;
1094 ir->init_num_operands();
1095 ir->operands[0] = rshift(mul(bit_and(add(temp, rshift(temp, c4)), c0F0F0F0F),
1096 c01010101),
1097 c24);
1098
1099 this->progress = true;
1100 }
1101
1102 void
1103 lower_instructions_visitor::extract_to_shifts(ir_expression *ir)
1104 {
1105 ir_variable *bits =
1106 new(ir) ir_variable(ir->operands[0]->type, "bits", ir_var_temporary);
1107
1108 base_ir->insert_before(bits);
1109 base_ir->insert_before(assign(bits, ir->operands[2]));
1110
1111 if (ir->operands[0]->type->base_type == GLSL_TYPE_UINT) {
1112 ir_constant *c1 =
1113 new(ir) ir_constant(1u, ir->operands[0]->type->vector_elements);
1114 ir_constant *c32 =
1115 new(ir) ir_constant(32u, ir->operands[0]->type->vector_elements);
1116 ir_constant *cFFFFFFFF =
1117 new(ir) ir_constant(0xFFFFFFFFu, ir->operands[0]->type->vector_elements);
1118
1119 /* At least some hardware treats (x << y) as (x << (y%32)). This means
1120 * we'd get a mask of 0 when bits is 32. Special case it.
1121 *
1122 * mask = bits == 32 ? 0xffffffff : (1u << bits) - 1u;
1123 */
1124 ir_expression *mask = csel(equal(bits, c32),
1125 cFFFFFFFF,
1126 sub(lshift(c1, bits), c1->clone(ir, NULL)));
1127
1128 /* Section 8.8 (Integer Functions) of the GLSL 4.50 spec says:
1129 *
1130 * If bits is zero, the result will be zero.
1131 *
1132 * Since (1 << 0) - 1 == 0, we don't need to bother with the conditional
1133 * select as in the signed integer case.
1134 *
1135 * (value >> offset) & mask;
1136 */
1137 ir->operation = ir_binop_bit_and;
1138 ir->init_num_operands();
1139 ir->operands[0] = rshift(ir->operands[0], ir->operands[1]);
1140 ir->operands[1] = mask;
1141 ir->operands[2] = NULL;
1142 } else {
1143 ir_constant *c0 =
1144 new(ir) ir_constant(int(0), ir->operands[0]->type->vector_elements);
1145 ir_constant *c32 =
1146 new(ir) ir_constant(int(32), ir->operands[0]->type->vector_elements);
1147 ir_variable *temp =
1148 new(ir) ir_variable(ir->operands[0]->type, "temp", ir_var_temporary);
1149
1150 /* temp = 32 - bits; */
1151 base_ir->insert_before(temp);
1152 base_ir->insert_before(assign(temp, sub(c32, bits)));
1153
1154 /* expr = value << (temp - offset)) >> temp; */
1155 ir_expression *expr =
1156 rshift(lshift(ir->operands[0], sub(temp, ir->operands[1])), temp);
1157
1158 /* Section 8.8 (Integer Functions) of the GLSL 4.50 spec says:
1159 *
1160 * If bits is zero, the result will be zero.
1161 *
1162 * Due to the (x << (y%32)) behavior mentioned before, the (value <<
1163 * (32-0)) doesn't "erase" all of the data as we would like, so finish
1164 * up with:
1165 *
1166 * (bits == 0) ? 0 : e;
1167 */
1168 ir->operation = ir_triop_csel;
1169 ir->init_num_operands();
1170 ir->operands[0] = equal(c0, bits);
1171 ir->operands[1] = c0->clone(ir, NULL);
1172 ir->operands[2] = expr;
1173 }
1174
1175 this->progress = true;
1176 }
1177
1178 void
1179 lower_instructions_visitor::insert_to_shifts(ir_expression *ir)
1180 {
1181 ir_constant *c1;
1182 ir_constant *c32;
1183 ir_constant *cFFFFFFFF;
1184 ir_variable *offset =
1185 new(ir) ir_variable(ir->operands[0]->type, "offset", ir_var_temporary);
1186 ir_variable *bits =
1187 new(ir) ir_variable(ir->operands[0]->type, "bits", ir_var_temporary);
1188 ir_variable *mask =
1189 new(ir) ir_variable(ir->operands[0]->type, "mask", ir_var_temporary);
1190
1191 if (ir->operands[0]->type->base_type == GLSL_TYPE_INT) {
1192 c1 = new(ir) ir_constant(int(1), ir->operands[0]->type->vector_elements);
1193 c32 = new(ir) ir_constant(int(32), ir->operands[0]->type->vector_elements);
1194 cFFFFFFFF = new(ir) ir_constant(int(0xFFFFFFFF), ir->operands[0]->type->vector_elements);
1195 } else {
1196 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
1197
1198 c1 = new(ir) ir_constant(1u, ir->operands[0]->type->vector_elements);
1199 c32 = new(ir) ir_constant(32u, ir->operands[0]->type->vector_elements);
1200 cFFFFFFFF = new(ir) ir_constant(0xFFFFFFFFu, ir->operands[0]->type->vector_elements);
1201 }
1202
1203 base_ir->insert_before(offset);
1204 base_ir->insert_before(assign(offset, ir->operands[2]));
1205
1206 base_ir->insert_before(bits);
1207 base_ir->insert_before(assign(bits, ir->operands[3]));
1208
1209 /* At least some hardware treats (x << y) as (x << (y%32)). This means
1210 * we'd get a mask of 0 when bits is 32. Special case it.
1211 *
1212 * mask = (bits == 32 ? 0xffffffff : (1u << bits) - 1u) << offset;
1213 *
1214 * Section 8.8 (Integer Functions) of the GLSL 4.50 spec says:
1215 *
1216 * The result will be undefined if offset or bits is negative, or if the
1217 * sum of offset and bits is greater than the number of bits used to
1218 * store the operand.
1219 *
1220 * Since it's undefined, there are a couple other ways this could be
1221 * implemented. The other way that was considered was to put the csel
1222 * around the whole thing:
1223 *
1224 * final_result = bits == 32 ? insert : ... ;
1225 */
1226 base_ir->insert_before(mask);
1227
1228 base_ir->insert_before(assign(mask, csel(equal(bits, c32),
1229 cFFFFFFFF,
1230 lshift(sub(lshift(c1, bits),
1231 c1->clone(ir, NULL)),
1232 offset))));
1233
1234 /* (base & ~mask) | ((insert << offset) & mask) */
1235 ir->operation = ir_binop_bit_or;
1236 ir->init_num_operands();
1237 ir->operands[0] = bit_and(ir->operands[0], bit_not(mask));
1238 ir->operands[1] = bit_and(lshift(ir->operands[1], offset), mask);
1239 ir->operands[2] = NULL;
1240 ir->operands[3] = NULL;
1241
1242 this->progress = true;
1243 }
1244
1245 void
1246 lower_instructions_visitor::reverse_to_shifts(ir_expression *ir)
1247 {
1248 /* For more details, see:
1249 *
1250 * http://graphics.stanford.edu/~seander/bithacks.html#ReverseParallel
1251 */
1252 ir_constant *c1 =
1253 new(ir) ir_constant(1u, ir->operands[0]->type->vector_elements);
1254 ir_constant *c2 =
1255 new(ir) ir_constant(2u, ir->operands[0]->type->vector_elements);
1256 ir_constant *c4 =
1257 new(ir) ir_constant(4u, ir->operands[0]->type->vector_elements);
1258 ir_constant *c8 =
1259 new(ir) ir_constant(8u, ir->operands[0]->type->vector_elements);
1260 ir_constant *c16 =
1261 new(ir) ir_constant(16u, ir->operands[0]->type->vector_elements);
1262 ir_constant *c33333333 =
1263 new(ir) ir_constant(0x33333333u, ir->operands[0]->type->vector_elements);
1264 ir_constant *c55555555 =
1265 new(ir) ir_constant(0x55555555u, ir->operands[0]->type->vector_elements);
1266 ir_constant *c0F0F0F0F =
1267 new(ir) ir_constant(0x0F0F0F0Fu, ir->operands[0]->type->vector_elements);
1268 ir_constant *c00FF00FF =
1269 new(ir) ir_constant(0x00FF00FFu, ir->operands[0]->type->vector_elements);
1270 ir_variable *temp =
1271 new(ir) ir_variable(glsl_type::uvec(ir->operands[0]->type->vector_elements),
1272 "temp", ir_var_temporary);
1273 ir_instruction &i = *base_ir;
1274
1275 i.insert_before(temp);
1276
1277 if (ir->operands[0]->type->base_type == GLSL_TYPE_UINT) {
1278 i.insert_before(assign(temp, ir->operands[0]));
1279 } else {
1280 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
1281 i.insert_before(assign(temp, i2u(ir->operands[0])));
1282 }
1283
1284 /* Swap odd and even bits.
1285 *
1286 * temp = ((temp >> 1) & 0x55555555u) | ((temp & 0x55555555u) << 1);
1287 */
1288 i.insert_before(assign(temp, bit_or(bit_and(rshift(temp, c1), c55555555),
1289 lshift(bit_and(temp, c55555555->clone(ir, NULL)),
1290 c1->clone(ir, NULL)))));
1291 /* Swap consecutive pairs.
1292 *
1293 * temp = ((temp >> 2) & 0x33333333u) | ((temp & 0x33333333u) << 2);
1294 */
1295 i.insert_before(assign(temp, bit_or(bit_and(rshift(temp, c2), c33333333),
1296 lshift(bit_and(temp, c33333333->clone(ir, NULL)),
1297 c2->clone(ir, NULL)))));
1298
1299 /* Swap nibbles.
1300 *
1301 * temp = ((temp >> 4) & 0x0F0F0F0Fu) | ((temp & 0x0F0F0F0Fu) << 4);
1302 */
1303 i.insert_before(assign(temp, bit_or(bit_and(rshift(temp, c4), c0F0F0F0F),
1304 lshift(bit_and(temp, c0F0F0F0F->clone(ir, NULL)),
1305 c4->clone(ir, NULL)))));
1306
1307 /* The last step is, basically, bswap. Swap the bytes, then swap the
1308 * words. When this code is run through GCC on x86, it does generate a
1309 * bswap instruction.
1310 *
1311 * temp = ((temp >> 8) & 0x00FF00FFu) | ((temp & 0x00FF00FFu) << 8);
1312 * temp = ( temp >> 16 ) | ( temp << 16);
1313 */
1314 i.insert_before(assign(temp, bit_or(bit_and(rshift(temp, c8), c00FF00FF),
1315 lshift(bit_and(temp, c00FF00FF->clone(ir, NULL)),
1316 c8->clone(ir, NULL)))));
1317
1318 if (ir->operands[0]->type->base_type == GLSL_TYPE_UINT) {
1319 ir->operation = ir_binop_bit_or;
1320 ir->init_num_operands();
1321 ir->operands[0] = rshift(temp, c16);
1322 ir->operands[1] = lshift(temp, c16->clone(ir, NULL));
1323 } else {
1324 ir->operation = ir_unop_u2i;
1325 ir->init_num_operands();
1326 ir->operands[0] = bit_or(rshift(temp, c16),
1327 lshift(temp, c16->clone(ir, NULL)));
1328 }
1329
1330 this->progress = true;
1331 }
1332
1333 void
1334 lower_instructions_visitor::find_lsb_to_float_cast(ir_expression *ir)
1335 {
1336 /* For more details, see:
1337 *
1338 * http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightFloatCast
1339 */
1340 const unsigned elements = ir->operands[0]->type->vector_elements;
1341 ir_constant *c0 = new(ir) ir_constant(unsigned(0), elements);
1342 ir_constant *cminus1 = new(ir) ir_constant(int(-1), elements);
1343 ir_constant *c23 = new(ir) ir_constant(int(23), elements);
1344 ir_constant *c7F = new(ir) ir_constant(int(0x7F), elements);
1345 ir_variable *temp =
1346 new(ir) ir_variable(glsl_type::ivec(elements), "temp", ir_var_temporary);
1347 ir_variable *lsb_only =
1348 new(ir) ir_variable(glsl_type::uvec(elements), "lsb_only", ir_var_temporary);
1349 ir_variable *as_float =
1350 new(ir) ir_variable(glsl_type::vec(elements), "as_float", ir_var_temporary);
1351 ir_variable *lsb =
1352 new(ir) ir_variable(glsl_type::ivec(elements), "lsb", ir_var_temporary);
1353
1354 ir_instruction &i = *base_ir;
1355
1356 i.insert_before(temp);
1357
1358 if (ir->operands[0]->type->base_type == GLSL_TYPE_INT) {
1359 i.insert_before(assign(temp, ir->operands[0]));
1360 } else {
1361 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
1362 i.insert_before(assign(temp, u2i(ir->operands[0])));
1363 }
1364
1365 /* The int-to-float conversion is lossless because (value & -value) is
1366 * either a power of two or zero. We don't use the result in the zero
1367 * case. The uint() cast is necessary so that 0x80000000 does not
1368 * generate a negative value.
1369 *
1370 * uint lsb_only = uint(value & -value);
1371 * float as_float = float(lsb_only);
1372 */
1373 i.insert_before(lsb_only);
1374 i.insert_before(assign(lsb_only, i2u(bit_and(temp, neg(temp)))));
1375
1376 i.insert_before(as_float);
1377 i.insert_before(assign(as_float, u2f(lsb_only)));
1378
1379 /* This is basically an open-coded frexp. Implementations that have a
1380 * native frexp instruction would be better served by that. This is
1381 * optimized versus a full-featured open-coded implementation in two ways:
1382 *
1383 * - We don't care about a correct result from subnormal numbers (including
1384 * 0.0), so the raw exponent can always be safely unbiased.
1385 *
1386 * - The value cannot be negative, so it does not need to be masked off to
1387 * extract the exponent.
1388 *
1389 * int lsb = (floatBitsToInt(as_float) >> 23) - 0x7f;
1390 */
1391 i.insert_before(lsb);
1392 i.insert_before(assign(lsb, sub(rshift(bitcast_f2i(as_float), c23), c7F)));
1393
1394 /* Use lsb_only in the comparison instead of temp so that the & (far above)
1395 * can possibly generate the result without an explicit comparison.
1396 *
1397 * (lsb_only == 0) ? -1 : lsb;
1398 *
1399 * Since our input values are all integers, the unbiased exponent must not
1400 * be negative. It will only be negative (-0x7f, in fact) if lsb_only is
1401 * 0. Instead of using (lsb_only == 0), we could use (lsb >= 0). Which is
1402 * better is likely GPU dependent. Either way, the difference should be
1403 * small.
1404 */
1405 ir->operation = ir_triop_csel;
1406 ir->init_num_operands();
1407 ir->operands[0] = equal(lsb_only, c0);
1408 ir->operands[1] = cminus1;
1409 ir->operands[2] = new(ir) ir_dereference_variable(lsb);
1410
1411 this->progress = true;
1412 }
1413
1414 void
1415 lower_instructions_visitor::find_msb_to_float_cast(ir_expression *ir)
1416 {
1417 /* For more details, see:
1418 *
1419 * http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightFloatCast
1420 */
1421 const unsigned elements = ir->operands[0]->type->vector_elements;
1422 ir_constant *c0 = new(ir) ir_constant(int(0), elements);
1423 ir_constant *cminus1 = new(ir) ir_constant(int(-1), elements);
1424 ir_constant *c23 = new(ir) ir_constant(int(23), elements);
1425 ir_constant *c7F = new(ir) ir_constant(int(0x7F), elements);
1426 ir_constant *c000000FF = new(ir) ir_constant(0x000000FFu, elements);
1427 ir_constant *cFFFFFF00 = new(ir) ir_constant(0xFFFFFF00u, elements);
1428 ir_variable *temp =
1429 new(ir) ir_variable(glsl_type::uvec(elements), "temp", ir_var_temporary);
1430 ir_variable *as_float =
1431 new(ir) ir_variable(glsl_type::vec(elements), "as_float", ir_var_temporary);
1432 ir_variable *msb =
1433 new(ir) ir_variable(glsl_type::ivec(elements), "msb", ir_var_temporary);
1434
1435 ir_instruction &i = *base_ir;
1436
1437 i.insert_before(temp);
1438
1439 if (ir->operands[0]->type->base_type == GLSL_TYPE_UINT) {
1440 i.insert_before(assign(temp, ir->operands[0]));
1441 } else {
1442 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
1443
1444 /* findMSB(uint(abs(some_int))) almost always does the right thing.
1445 * There are two problem values:
1446 *
1447 * * 0x80000000. Since abs(0x80000000) == 0x80000000, findMSB returns
1448 * 31. However, findMSB(int(0x80000000)) == 30.
1449 *
1450 * * 0xffffffff. Since abs(0xffffffff) == 1, findMSB returns
1451 * 31. Section 8.8 (Integer Functions) of the GLSL 4.50 spec says:
1452 *
1453 * For a value of zero or negative one, -1 will be returned.
1454 *
1455 * For all negative number cases, including 0x80000000 and 0xffffffff,
1456 * the correct value is obtained from findMSB if instead of negating the
1457 * (already negative) value the logical-not is used. A conditonal
1458 * logical-not can be achieved in two instructions.
1459 */
1460 ir_variable *as_int =
1461 new(ir) ir_variable(glsl_type::ivec(elements), "as_int", ir_var_temporary);
1462 ir_constant *c31 = new(ir) ir_constant(int(31), elements);
1463
1464 i.insert_before(as_int);
1465 i.insert_before(assign(as_int, ir->operands[0]));
1466 i.insert_before(assign(temp, i2u(expr(ir_binop_bit_xor,
1467 as_int,
1468 rshift(as_int, c31)))));
1469 }
1470
1471 /* The int-to-float conversion is lossless because bits are conditionally
1472 * masked off the bottom of temp to ensure the value has at most 24 bits of
1473 * data or is zero. We don't use the result in the zero case. The uint()
1474 * cast is necessary so that 0x80000000 does not generate a negative value.
1475 *
1476 * float as_float = float(temp > 255 ? temp & ~255 : temp);
1477 */
1478 i.insert_before(as_float);
1479 i.insert_before(assign(as_float, u2f(csel(greater(temp, c000000FF),
1480 bit_and(temp, cFFFFFF00),
1481 temp))));
1482
1483 /* This is basically an open-coded frexp. Implementations that have a
1484 * native frexp instruction would be better served by that. This is
1485 * optimized versus a full-featured open-coded implementation in two ways:
1486 *
1487 * - We don't care about a correct result from subnormal numbers (including
1488 * 0.0), so the raw exponent can always be safely unbiased.
1489 *
1490 * - The value cannot be negative, so it does not need to be masked off to
1491 * extract the exponent.
1492 *
1493 * int msb = (floatBitsToInt(as_float) >> 23) - 0x7f;
1494 */
1495 i.insert_before(msb);
1496 i.insert_before(assign(msb, sub(rshift(bitcast_f2i(as_float), c23), c7F)));
1497
1498 /* Use msb in the comparison instead of temp so that the subtract can
1499 * possibly generate the result without an explicit comparison.
1500 *
1501 * (msb < 0) ? -1 : msb;
1502 *
1503 * Since our input values are all integers, the unbiased exponent must not
1504 * be negative. It will only be negative (-0x7f, in fact) if temp is 0.
1505 */
1506 ir->operation = ir_triop_csel;
1507 ir->init_num_operands();
1508 ir->operands[0] = less(msb, c0);
1509 ir->operands[1] = cminus1;
1510 ir->operands[2] = new(ir) ir_dereference_variable(msb);
1511
1512 this->progress = true;
1513 }
1514
1515 ir_expression *
1516 lower_instructions_visitor::_carry(operand a, operand b)
1517 {
1518 if (lowering(CARRY_TO_ARITH))
1519 return i2u(b2i(less(add(a, b),
1520 a.val->clone(ralloc_parent(a.val), NULL))));
1521 else
1522 return carry(a, b);
1523 }
1524
1525 ir_constant *
1526 lower_instructions_visitor::_imm_fp(void *mem_ctx,
1527 const glsl_type *type,
1528 double f,
1529 unsigned vector_elements)
1530 {
1531 switch (type->base_type) {
1532 case GLSL_TYPE_FLOAT:
1533 return new(mem_ctx) ir_constant((float) f, vector_elements);
1534 case GLSL_TYPE_DOUBLE:
1535 return new(mem_ctx) ir_constant((double) f, vector_elements);
1536 case GLSL_TYPE_FLOAT16:
1537 return new(mem_ctx) ir_constant(float16_t(f), vector_elements);
1538 default:
1539 assert(!"unknown float type for immediate");
1540 return NULL;
1541 }
1542 }
1543
1544 void
1545 lower_instructions_visitor::imul_high_to_mul(ir_expression *ir)
1546 {
1547 /* ABCD
1548 * * EFGH
1549 * ======
1550 * (GH * CD) + (GH * AB) << 16 + (EF * CD) << 16 + (EF * AB) << 32
1551 *
1552 * In GLSL, (a * b) becomes
1553 *
1554 * uint m1 = (a & 0x0000ffffu) * (b & 0x0000ffffu);
1555 * uint m2 = (a & 0x0000ffffu) * (b >> 16);
1556 * uint m3 = (a >> 16) * (b & 0x0000ffffu);
1557 * uint m4 = (a >> 16) * (b >> 16);
1558 *
1559 * uint c1;
1560 * uint c2;
1561 * uint lo_result;
1562 * uint hi_result;
1563 *
1564 * lo_result = uaddCarry(m1, m2 << 16, c1);
1565 * hi_result = m4 + c1;
1566 * lo_result = uaddCarry(lo_result, m3 << 16, c2);
1567 * hi_result = hi_result + c2;
1568 * hi_result = hi_result + (m2 >> 16) + (m3 >> 16);
1569 */
1570 const unsigned elements = ir->operands[0]->type->vector_elements;
1571 ir_variable *src1 =
1572 new(ir) ir_variable(glsl_type::uvec(elements), "src1", ir_var_temporary);
1573 ir_variable *src1h =
1574 new(ir) ir_variable(glsl_type::uvec(elements), "src1h", ir_var_temporary);
1575 ir_variable *src1l =
1576 new(ir) ir_variable(glsl_type::uvec(elements), "src1l", ir_var_temporary);
1577 ir_variable *src2 =
1578 new(ir) ir_variable(glsl_type::uvec(elements), "src2", ir_var_temporary);
1579 ir_variable *src2h =
1580 new(ir) ir_variable(glsl_type::uvec(elements), "src2h", ir_var_temporary);
1581 ir_variable *src2l =
1582 new(ir) ir_variable(glsl_type::uvec(elements), "src2l", ir_var_temporary);
1583 ir_variable *t1 =
1584 new(ir) ir_variable(glsl_type::uvec(elements), "t1", ir_var_temporary);
1585 ir_variable *t2 =
1586 new(ir) ir_variable(glsl_type::uvec(elements), "t2", ir_var_temporary);
1587 ir_variable *lo =
1588 new(ir) ir_variable(glsl_type::uvec(elements), "lo", ir_var_temporary);
1589 ir_variable *hi =
1590 new(ir) ir_variable(glsl_type::uvec(elements), "hi", ir_var_temporary);
1591 ir_variable *different_signs = NULL;
1592 ir_constant *c0000FFFF = new(ir) ir_constant(0x0000FFFFu, elements);
1593 ir_constant *c16 = new(ir) ir_constant(16u, elements);
1594
1595 ir_instruction &i = *base_ir;
1596
1597 i.insert_before(src1);
1598 i.insert_before(src2);
1599 i.insert_before(src1h);
1600 i.insert_before(src2h);
1601 i.insert_before(src1l);
1602 i.insert_before(src2l);
1603
1604 if (ir->operands[0]->type->base_type == GLSL_TYPE_UINT) {
1605 i.insert_before(assign(src1, ir->operands[0]));
1606 i.insert_before(assign(src2, ir->operands[1]));
1607 } else {
1608 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
1609
1610 ir_variable *itmp1 =
1611 new(ir) ir_variable(glsl_type::ivec(elements), "itmp1", ir_var_temporary);
1612 ir_variable *itmp2 =
1613 new(ir) ir_variable(glsl_type::ivec(elements), "itmp2", ir_var_temporary);
1614 ir_constant *c0 = new(ir) ir_constant(int(0), elements);
1615
1616 i.insert_before(itmp1);
1617 i.insert_before(itmp2);
1618 i.insert_before(assign(itmp1, ir->operands[0]));
1619 i.insert_before(assign(itmp2, ir->operands[1]));
1620
1621 different_signs =
1622 new(ir) ir_variable(glsl_type::bvec(elements), "different_signs",
1623 ir_var_temporary);
1624
1625 i.insert_before(different_signs);
1626 i.insert_before(assign(different_signs, expr(ir_binop_logic_xor,
1627 less(itmp1, c0),
1628 less(itmp2, c0->clone(ir, NULL)))));
1629
1630 i.insert_before(assign(src1, i2u(abs(itmp1))));
1631 i.insert_before(assign(src2, i2u(abs(itmp2))));
1632 }
1633
1634 i.insert_before(assign(src1l, bit_and(src1, c0000FFFF)));
1635 i.insert_before(assign(src2l, bit_and(src2, c0000FFFF->clone(ir, NULL))));
1636 i.insert_before(assign(src1h, rshift(src1, c16)));
1637 i.insert_before(assign(src2h, rshift(src2, c16->clone(ir, NULL))));
1638
1639 i.insert_before(lo);
1640 i.insert_before(hi);
1641 i.insert_before(t1);
1642 i.insert_before(t2);
1643
1644 i.insert_before(assign(lo, mul(src1l, src2l)));
1645 i.insert_before(assign(t1, mul(src1l, src2h)));
1646 i.insert_before(assign(t2, mul(src1h, src2l)));
1647 i.insert_before(assign(hi, mul(src1h, src2h)));
1648
1649 i.insert_before(assign(hi, add(hi, _carry(lo, lshift(t1, c16->clone(ir, NULL))))));
1650 i.insert_before(assign(lo, add(lo, lshift(t1, c16->clone(ir, NULL)))));
1651
1652 i.insert_before(assign(hi, add(hi, _carry(lo, lshift(t2, c16->clone(ir, NULL))))));
1653 i.insert_before(assign(lo, add(lo, lshift(t2, c16->clone(ir, NULL)))));
1654
1655 if (different_signs == NULL) {
1656 assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
1657
1658 ir->operation = ir_binop_add;
1659 ir->init_num_operands();
1660 ir->operands[0] = add(hi, rshift(t1, c16->clone(ir, NULL)));
1661 ir->operands[1] = rshift(t2, c16->clone(ir, NULL));
1662 } else {
1663 assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
1664
1665 i.insert_before(assign(hi, add(add(hi, rshift(t1, c16->clone(ir, NULL))),
1666 rshift(t2, c16->clone(ir, NULL)))));
1667
1668 /* For channels where different_signs is set we have to perform a 64-bit
1669 * negation. This is *not* the same as just negating the high 32-bits.
1670 * Consider -3 * 2. The high 32-bits is 0, but the desired result is
1671 * -1, not -0! Recall -x == ~x + 1.
1672 */
1673 ir_variable *neg_hi =
1674 new(ir) ir_variable(glsl_type::ivec(elements), "neg_hi", ir_var_temporary);
1675 ir_constant *c1 = new(ir) ir_constant(1u, elements);
1676
1677 i.insert_before(neg_hi);
1678 i.insert_before(assign(neg_hi, add(bit_not(u2i(hi)),
1679 u2i(_carry(bit_not(lo), c1)))));
1680
1681 ir->operation = ir_triop_csel;
1682 ir->init_num_operands();
1683 ir->operands[0] = new(ir) ir_dereference_variable(different_signs);
1684 ir->operands[1] = new(ir) ir_dereference_variable(neg_hi);
1685 ir->operands[2] = u2i(hi);
1686 }
1687 }
1688
1689 void
1690 lower_instructions_visitor::sqrt_to_abs_sqrt(ir_expression *ir)
1691 {
1692 ir->operands[0] = new(ir) ir_expression(ir_unop_abs, ir->operands[0]);
1693 this->progress = true;
1694 }
1695
1696 void
1697 lower_instructions_visitor::mul64_to_mul_and_mul_high(ir_expression *ir)
1698 {
1699 /* Lower 32x32-> 64 to
1700 * msb = imul_high(x_lo, y_lo)
1701 * lsb = mul(x_lo, y_lo)
1702 */
1703 const unsigned elements = ir->operands[0]->type->vector_elements;
1704
1705 const ir_expression_operation operation =
1706 ir->type->base_type == GLSL_TYPE_UINT64 ? ir_unop_pack_uint_2x32
1707 : ir_unop_pack_int_2x32;
1708
1709 const glsl_type *var_type = ir->type->base_type == GLSL_TYPE_UINT64
1710 ? glsl_type::uvec(elements)
1711 : glsl_type::ivec(elements);
1712
1713 const glsl_type *ret_type = ir->type->base_type == GLSL_TYPE_UINT64
1714 ? glsl_type::uvec2_type
1715 : glsl_type::ivec2_type;
1716
1717 ir_instruction &i = *base_ir;
1718
1719 ir_variable *msb =
1720 new(ir) ir_variable(var_type, "msb", ir_var_temporary);
1721 ir_variable *lsb =
1722 new(ir) ir_variable(var_type, "lsb", ir_var_temporary);
1723 ir_variable *x =
1724 new(ir) ir_variable(var_type, "x", ir_var_temporary);
1725 ir_variable *y =
1726 new(ir) ir_variable(var_type, "y", ir_var_temporary);
1727
1728 i.insert_before(x);
1729 i.insert_before(assign(x, ir->operands[0]));
1730 i.insert_before(y);
1731 i.insert_before(assign(y, ir->operands[1]));
1732 i.insert_before(msb);
1733 i.insert_before(lsb);
1734
1735 i.insert_before(assign(msb, imul_high(x, y)));
1736 i.insert_before(assign(lsb, mul(x, y)));
1737
1738 ir_rvalue *result[4] = {NULL};
1739 for (unsigned elem = 0; elem < elements; elem++) {
1740 ir_rvalue *val = new(ir) ir_expression(ir_quadop_vector, ret_type,
1741 swizzle(lsb, elem, 1),
1742 swizzle(msb, elem, 1), NULL, NULL);
1743 result[elem] = expr(operation, val);
1744 }
1745
1746 ir->operation = ir_quadop_vector;
1747 ir->init_num_operands();
1748 ir->operands[0] = result[0];
1749 ir->operands[1] = result[1];
1750 ir->operands[2] = result[2];
1751 ir->operands[3] = result[3];
1752
1753 this->progress = true;
1754 }
1755
1756 ir_visitor_status
1757 lower_instructions_visitor::visit_leave(ir_expression *ir)
1758 {
1759 switch (ir->operation) {
1760 case ir_binop_dot:
1761 if (ir->operands[0]->type->is_double())
1762 double_dot_to_fma(ir);
1763 break;
1764 case ir_triop_lrp:
1765 if (ir->operands[0]->type->is_double())
1766 double_lrp(ir);
1767 break;
1768 case ir_binop_sub:
1769 if (lowering(SUB_TO_ADD_NEG))
1770 sub_to_add_neg(ir);
1771 break;
1772
1773 case ir_binop_div:
1774 if (ir->operands[1]->type->is_integer_32() && lowering(INT_DIV_TO_MUL_RCP))
1775 int_div_to_mul_rcp(ir);
1776 else if ((ir->operands[1]->type->is_float() && lowering(FDIV_TO_MUL_RCP)) ||
1777 (ir->operands[1]->type->is_double() && lowering(DDIV_TO_MUL_RCP)))
1778 div_to_mul_rcp(ir);
1779 break;
1780
1781 case ir_unop_exp:
1782 if (lowering(EXP_TO_EXP2))
1783 exp_to_exp2(ir);
1784 break;
1785
1786 case ir_unop_log:
1787 if (lowering(LOG_TO_LOG2))
1788 log_to_log2(ir);
1789 break;
1790
1791 case ir_binop_mod:
1792 if (lowering(MOD_TO_FLOOR) && (ir->type->is_float() || ir->type->is_double()))
1793 mod_to_floor(ir);
1794 break;
1795
1796 case ir_binop_pow:
1797 if (lowering(POW_TO_EXP2))
1798 pow_to_exp2(ir);
1799 break;
1800
1801 case ir_binop_ldexp:
1802 if (lowering(LDEXP_TO_ARITH) && ir->type->is_float())
1803 ldexp_to_arith(ir);
1804 if (lowering(DFREXP_DLDEXP_TO_ARITH) && ir->type->is_double())
1805 dldexp_to_arith(ir);
1806 break;
1807
1808 case ir_unop_frexp_exp:
1809 if (lowering(DFREXP_DLDEXP_TO_ARITH) && ir->operands[0]->type->is_double())
1810 dfrexp_exp_to_arith(ir);
1811 break;
1812
1813 case ir_unop_frexp_sig:
1814 if (lowering(DFREXP_DLDEXP_TO_ARITH) && ir->operands[0]->type->is_double())
1815 dfrexp_sig_to_arith(ir);
1816 break;
1817
1818 case ir_binop_carry:
1819 if (lowering(CARRY_TO_ARITH))
1820 carry_to_arith(ir);
1821 break;
1822
1823 case ir_binop_borrow:
1824 if (lowering(BORROW_TO_ARITH))
1825 borrow_to_arith(ir);
1826 break;
1827
1828 case ir_unop_saturate:
1829 if (lowering(SAT_TO_CLAMP))
1830 sat_to_clamp(ir);
1831 break;
1832
1833 case ir_unop_trunc:
1834 if (lowering(DOPS_TO_DFRAC) && ir->type->is_double())
1835 dtrunc_to_dfrac(ir);
1836 break;
1837
1838 case ir_unop_ceil:
1839 if (lowering(DOPS_TO_DFRAC) && ir->type->is_double())
1840 dceil_to_dfrac(ir);
1841 break;
1842
1843 case ir_unop_floor:
1844 if (lowering(DOPS_TO_DFRAC) && ir->type->is_double())
1845 dfloor_to_dfrac(ir);
1846 break;
1847
1848 case ir_unop_round_even:
1849 if (lowering(DOPS_TO_DFRAC) && ir->type->is_double())
1850 dround_even_to_dfrac(ir);
1851 break;
1852
1853 case ir_unop_sign:
1854 if (lowering(DOPS_TO_DFRAC) && ir->type->is_double())
1855 dsign_to_csel(ir);
1856 break;
1857
1858 case ir_unop_bit_count:
1859 if (lowering(BIT_COUNT_TO_MATH))
1860 bit_count_to_math(ir);
1861 break;
1862
1863 case ir_triop_bitfield_extract:
1864 if (lowering(EXTRACT_TO_SHIFTS))
1865 extract_to_shifts(ir);
1866 break;
1867
1868 case ir_quadop_bitfield_insert:
1869 if (lowering(INSERT_TO_SHIFTS))
1870 insert_to_shifts(ir);
1871 break;
1872
1873 case ir_unop_bitfield_reverse:
1874 if (lowering(REVERSE_TO_SHIFTS))
1875 reverse_to_shifts(ir);
1876 break;
1877
1878 case ir_unop_find_lsb:
1879 if (lowering(FIND_LSB_TO_FLOAT_CAST))
1880 find_lsb_to_float_cast(ir);
1881 break;
1882
1883 case ir_unop_find_msb:
1884 if (lowering(FIND_MSB_TO_FLOAT_CAST))
1885 find_msb_to_float_cast(ir);
1886 break;
1887
1888 case ir_binop_imul_high:
1889 if (lowering(IMUL_HIGH_TO_MUL))
1890 imul_high_to_mul(ir);
1891 break;
1892
1893 case ir_binop_mul:
1894 if (lowering(MUL64_TO_MUL_AND_MUL_HIGH) &&
1895 (ir->type->base_type == GLSL_TYPE_INT64 ||
1896 ir->type->base_type == GLSL_TYPE_UINT64) &&
1897 (ir->operands[0]->type->base_type == GLSL_TYPE_INT ||
1898 ir->operands[1]->type->base_type == GLSL_TYPE_UINT))
1899 mul64_to_mul_and_mul_high(ir);
1900 break;
1901
1902 case ir_unop_rsq:
1903 case ir_unop_sqrt:
1904 if (lowering(SQRT_TO_ABS_SQRT))
1905 sqrt_to_abs_sqrt(ir);
1906 break;
1907
1908 default:
1909 return visit_continue;
1910 }
1911
1912 return visit_continue;
1913 }