gallivm: add no-signed-zeros-fp-math option to lp_create_builder (v2)
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_tgsi_action.c
1 /**************************************************************************
2 *
3 * Copyright 2011-2012 Advanced Micro Devices, Inc.
4 * Copyright 2009 VMware, Inc.
5 * Copyright 2007-2008 VMware, Inc.
6 * All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
18 * of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
23 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
24 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 *
28 **************************************************************************/
29
30 /**
31 * @file
32 * TGSI to LLVM IR translation.
33 *
34 * @author Jose Fonseca <jfonseca@vmware.com>
35 * @author Tom Stellard <thomas.stellard@amd.com>
36 *
37 * Based on tgsi_sse2.c code written by Michal Krol, Keith Whitwell,
38 * Brian Paul, and others.
39 */
40
41
42 #include "lp_bld_tgsi_action.h"
43
44 #include "lp_bld_tgsi.h"
45 #include "lp_bld_arit.h"
46 #include "lp_bld_bitarit.h"
47 #include "lp_bld_const.h"
48 #include "lp_bld_conv.h"
49 #include "lp_bld_gather.h"
50 #include "lp_bld_logic.h"
51 #include "lp_bld_pack.h"
52
53 #include "tgsi/tgsi_exec.h"
54
55 /* XXX: The CPU only defaults should be repaced by generic ones. In most
56 * cases, the CPU defaults are just wrappers around a function in
57 * lp_build_arit.c and these functions should be inlined here and the CPU
58 * generic code should be removed and placed elsewhere.
59 */
60
61 /* Default actions */
62
63 /* Generic fetch_arg functions */
64
65 static void scalar_unary_fetch_args(
66 struct lp_build_tgsi_context * bld_base,
67 struct lp_build_emit_data * emit_data)
68 {
69 /* src0.x */
70 emit_data->args[0] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, 0);
71 emit_data->arg_count = 1;
72 emit_data->dst_type = LLVMTypeOf(emit_data->args[0]);
73 }
74
75 static void scalar_binary_fetch_args(
76 struct lp_build_tgsi_context * bld_base,
77 struct lp_build_emit_data * emit_data)
78 {
79 /* src0.x */
80 emit_data->args[0] = lp_build_emit_fetch(bld_base, emit_data->inst,
81 0, TGSI_CHAN_X);
82 /* src1.x */
83 emit_data->args[1] = lp_build_emit_fetch(bld_base, emit_data->inst,
84 1, TGSI_CHAN_X);
85 emit_data->arg_count = 2;
86 emit_data->dst_type = LLVMTypeOf(emit_data->args[0]);
87 }
88
89 /* TGSI_OPCODE_ADD */
90 static void
91 add_emit(
92 const struct lp_build_tgsi_action * action,
93 struct lp_build_tgsi_context * bld_base,
94 struct lp_build_emit_data * emit_data)
95 {
96 emit_data->output[emit_data->chan] = LLVMBuildFAdd(
97 bld_base->base.gallivm->builder,
98 emit_data->args[0], emit_data->args[1], "");
99 }
100
101 /* TGSI_OPCODE_ARR */
102 static void
103 arr_emit(
104 const struct lp_build_tgsi_action * action,
105 struct lp_build_tgsi_context * bld_base,
106 struct lp_build_emit_data * emit_data)
107 {
108 LLVMValueRef tmp = lp_build_emit_llvm_unary(bld_base, TGSI_OPCODE_ROUND, emit_data->args[0]);
109 emit_data->output[emit_data->chan] = LLVMBuildFPToSI(bld_base->base.gallivm->builder, tmp,
110 bld_base->uint_bld.vec_type, "");
111 }
112
113 /* DP* Helper */
114
115 static void
116 dp_fetch_args(
117 struct lp_build_tgsi_context * bld_base,
118 struct lp_build_emit_data * emit_data,
119 unsigned dp_components)
120 {
121 unsigned chan, src;
122 for (src = 0; src < 2; src++) {
123 for (chan = 0; chan < dp_components; chan++) {
124 emit_data->args[(src * dp_components) + chan] =
125 lp_build_emit_fetch(bld_base, emit_data->inst, src, chan);
126 }
127 }
128 emit_data->dst_type = bld_base->base.elem_type;
129 }
130
131 /* TGSI_OPCODE_DP2 */
132 static void
133 dp2_fetch_args(
134 struct lp_build_tgsi_context * bld_base,
135 struct lp_build_emit_data * emit_data)
136 {
137 dp_fetch_args(bld_base, emit_data, 2);
138 }
139
140 static void
141 dp2_emit(
142 const struct lp_build_tgsi_action * action,
143 struct lp_build_tgsi_context * bld_base,
144 struct lp_build_emit_data * emit_data)
145 {
146 LLVMValueRef tmp0, tmp1;
147 tmp0 = lp_build_emit_llvm_binary(bld_base, TGSI_OPCODE_MUL,
148 emit_data->args[0] /* src0.x */,
149 emit_data->args[2] /* src1.x */);
150 tmp1 = lp_build_emit_llvm_binary(bld_base, TGSI_OPCODE_MUL,
151 emit_data->args[1] /* src0.y */,
152 emit_data->args[3] /* src1.y */);
153 emit_data->output[emit_data->chan] = lp_build_emit_llvm_binary(bld_base,
154 TGSI_OPCODE_ADD, tmp0, tmp1);
155 }
156
157 static struct lp_build_tgsi_action dp2_action = {
158 dp2_fetch_args, /* fetch_args */
159 dp2_emit /* emit */
160 };
161
162 /* TGSI_OPCODE_DP2A */
163 static void
164 dp2a_fetch_args(
165 struct lp_build_tgsi_context * bld_base,
166 struct lp_build_emit_data * emit_data)
167 {
168 dp_fetch_args(bld_base, emit_data, 2);
169 emit_data->args[5] = lp_build_emit_fetch(bld_base, emit_data->inst,
170 2, TGSI_CHAN_X);
171 }
172
173 static void
174 dp2a_emit(
175 const struct lp_build_tgsi_action * action,
176 struct lp_build_tgsi_context * bld_base,
177 struct lp_build_emit_data * emit_data)
178 {
179 LLVMValueRef tmp;
180 tmp = lp_build_emit_llvm(bld_base, TGSI_OPCODE_DP2, emit_data);
181 emit_data->output[emit_data->chan] = lp_build_emit_llvm_binary(bld_base, TGSI_OPCODE_ADD,
182 emit_data->args[5], tmp);
183 }
184
185 static struct lp_build_tgsi_action dp2a_action = {
186 dp2a_fetch_args, /* fetch_args */
187 dp2a_emit /* emit */
188 };
189
190 /* TGSI_OPCODE_DP3 */
191 static void
192 dp3_fetch_args(
193 struct lp_build_tgsi_context * bld_base,
194 struct lp_build_emit_data * emit_data)
195 {
196 dp_fetch_args(bld_base, emit_data, 3);
197 }
198
199 static void
200 dp3_emit(
201 const struct lp_build_tgsi_action * action,
202 struct lp_build_tgsi_context * bld_base,
203 struct lp_build_emit_data * emit_data)
204 {
205 LLVMValueRef tmp0, tmp1;
206 tmp0 = lp_build_emit_llvm_binary(bld_base, TGSI_OPCODE_MUL,
207 emit_data->args[0] /* src0.x */,
208 emit_data->args[3] /* src1.x */);
209 tmp1 = lp_build_emit_llvm_binary(bld_base, TGSI_OPCODE_MUL,
210 emit_data->args[1] /* src0.y */,
211 emit_data->args[4] /* src1.y */);
212 tmp0 = lp_build_emit_llvm_binary(bld_base, TGSI_OPCODE_ADD, tmp1, tmp0);
213 tmp1 = lp_build_emit_llvm_binary(bld_base, TGSI_OPCODE_MUL,
214 emit_data->args[2] /* src0.z */,
215 emit_data->args[5] /* src1.z */);
216 emit_data->output[emit_data->chan] = lp_build_emit_llvm_binary(bld_base,
217 TGSI_OPCODE_ADD, tmp0, tmp1);
218 }
219
220 static struct lp_build_tgsi_action dp3_action = {
221 dp3_fetch_args, /* fetch_args */
222 dp3_emit /* emit */
223 };
224
225 /* TGSI_OPCODDE_DP4 */
226
227 static void
228 dp4_fetch_args(
229 struct lp_build_tgsi_context * bld_base,
230 struct lp_build_emit_data * emit_data)
231 {
232 dp_fetch_args(bld_base, emit_data, 4);
233 }
234
235 static void
236 dp4_emit(
237 const struct lp_build_tgsi_action * action,
238 struct lp_build_tgsi_context * bld_base,
239 struct lp_build_emit_data * emit_data)
240 {
241 LLVMValueRef tmp0, tmp1;
242 tmp0 = lp_build_emit_llvm_binary(bld_base, TGSI_OPCODE_MUL,
243 emit_data->args[0] /* src0.x */,
244 emit_data->args[4] /* src1.x */);
245 tmp1 = lp_build_emit_llvm_binary(bld_base, TGSI_OPCODE_MUL,
246 emit_data->args[1] /* src0.y */,
247 emit_data->args[5] /* src1.y */);
248 tmp0 = lp_build_emit_llvm_binary(bld_base, TGSI_OPCODE_ADD, tmp0, tmp1);
249 tmp1 = lp_build_emit_llvm_binary(bld_base, TGSI_OPCODE_MUL,
250 emit_data->args[2] /* src0.z */,
251 emit_data->args[6] /* src1.z */);
252 tmp0 = lp_build_emit_llvm_binary(bld_base, TGSI_OPCODE_ADD, tmp0, tmp1);
253 tmp1 = lp_build_emit_llvm_binary(bld_base, TGSI_OPCODE_MUL,
254 emit_data->args[3] /* src0.w */,
255 emit_data->args[7] /* src1.w */);
256 emit_data->output[emit_data->chan] = lp_build_emit_llvm_binary(bld_base,
257 TGSI_OPCODE_ADD, tmp0, tmp1);
258 }
259
260 static struct lp_build_tgsi_action dp4_action = {
261 dp4_fetch_args, /* fetch_args */
262 dp4_emit /* emit */
263 };
264
265 /* TGSI_OPCODE_DPH */
266 static void
267 dph_fetch_args(
268 struct lp_build_tgsi_context * bld_base,
269 struct lp_build_emit_data * emit_data)
270 {
271 dp_fetch_args(bld_base, emit_data, 4);
272 /* src0.w */
273 emit_data->args[3] = bld_base->base.one;
274 }
275
276 const struct lp_build_tgsi_action dph_action = {
277 dph_fetch_args, /* fetch_args */
278 dp4_emit /* emit */
279 };
280
281 /* TGSI_OPCODE_DST */
282 static void
283 dst_fetch_args(
284 struct lp_build_tgsi_context * bld_base,
285 struct lp_build_emit_data * emit_data)
286 {
287 /* src0.y */
288 emit_data->args[0] = lp_build_emit_fetch(bld_base, emit_data->inst,
289 0, TGSI_CHAN_Y);
290 /* src0.z */
291 emit_data->args[1] = lp_build_emit_fetch(bld_base, emit_data->inst,
292 0, TGSI_CHAN_Z);
293 /* src1.y */
294 emit_data->args[2] = lp_build_emit_fetch(bld_base, emit_data->inst,
295 1, TGSI_CHAN_Y);
296 /* src1.w */
297 emit_data->args[3] = lp_build_emit_fetch(bld_base, emit_data->inst,
298 1, TGSI_CHAN_W);
299 }
300
301 static void
302 dst_emit(
303 const struct lp_build_tgsi_action * action,
304 struct lp_build_tgsi_context * bld_base,
305 struct lp_build_emit_data * emit_data)
306 {
307 /* dst.x */
308 emit_data->output[TGSI_CHAN_X] = bld_base->base.one;
309
310 /* dst.y */
311 emit_data->output[TGSI_CHAN_Y] = lp_build_emit_llvm_binary(bld_base,
312 TGSI_OPCODE_MUL,
313 emit_data->args[0] /* src0.y */,
314 emit_data->args[2] /* src1.y */);
315 /* dst.z */
316 emit_data->output[TGSI_CHAN_Z] = emit_data->args[1]; /* src0.z */
317
318 /* dst.w */
319 emit_data->output[TGSI_CHAN_W] = emit_data->args[3]; /* src1.w */
320 }
321
322 static struct lp_build_tgsi_action dst_action = {
323 dst_fetch_args, /* fetch_args */
324 dst_emit /* emit */
325 };
326
327 /* TGSI_OPCODE_END */
328 static void
329 end_emit(
330 const struct lp_build_tgsi_action * action,
331 struct lp_build_tgsi_context * bld_base,
332 struct lp_build_emit_data * emit_data)
333 {
334 bld_base->pc = -1;
335 }
336
337 /* TGSI_OPCODE_EXP */
338
339 static void
340 exp_emit(
341 const struct lp_build_tgsi_action * action,
342 struct lp_build_tgsi_context * bld_base,
343 struct lp_build_emit_data * emit_data)
344 {
345 LLVMValueRef floor_x;
346
347 /* floor( src0.x ) */
348 floor_x = lp_build_emit_llvm_unary(bld_base, TGSI_OPCODE_FLR,
349 emit_data->args[0]);
350
351 /* 2 ^ floor( src0.x ) */
352 emit_data->output[TGSI_CHAN_X] = lp_build_emit_llvm_unary(bld_base,
353 TGSI_OPCODE_EX2, floor_x);
354
355 /* src0.x - floor( src0.x ) */
356 emit_data->output[TGSI_CHAN_Y] =
357 lp_build_sub(&bld_base->base, emit_data->args[0] /* src0.x */, floor_x);
358
359 /* 2 ^ src0.x */
360 emit_data->output[TGSI_CHAN_Z] = lp_build_emit_llvm_unary(bld_base,
361 TGSI_OPCODE_EX2, emit_data->args[0] /* src0.x */);
362
363 emit_data->output[TGSI_CHAN_W] = bld_base->base.one;
364 }
365
366 const struct lp_build_tgsi_action exp_action = {
367 scalar_unary_fetch_args, /* fetch_args */
368 exp_emit /* emit */
369 };
370
371 /* TGSI_OPCODE_FRC */
372
373 static void
374 frc_emit(
375 const struct lp_build_tgsi_action * action,
376 struct lp_build_tgsi_context * bld_base,
377 struct lp_build_emit_data * emit_data)
378 {
379 LLVMValueRef tmp;
380 tmp = lp_build_emit_llvm_unary(bld_base, TGSI_OPCODE_FLR,
381 emit_data->args[0]);
382 emit_data->output[emit_data->chan] =
383 lp_build_sub(&bld_base->base, emit_data->args[0], tmp);
384 }
385
386 /* TGSI_OPCODE_KILL_IF */
387
388 static void
389 kil_fetch_args(
390 struct lp_build_tgsi_context * bld_base,
391 struct lp_build_emit_data * emit_data)
392 {
393 /* src0.x */
394 emit_data->args[0] = lp_build_emit_fetch(bld_base, emit_data->inst,
395 0, TGSI_CHAN_X);
396 /* src0.y */
397 emit_data->args[1] = lp_build_emit_fetch(bld_base, emit_data->inst,
398 0, TGSI_CHAN_Y);
399 /* src0.z */
400 emit_data->args[2] = lp_build_emit_fetch(bld_base, emit_data->inst,
401 0, TGSI_CHAN_Z);
402 /* src0.w */
403 emit_data->args[3] = lp_build_emit_fetch(bld_base, emit_data->inst,
404 0, TGSI_CHAN_W);
405 emit_data->arg_count = 4;
406 emit_data->dst_type = LLVMVoidTypeInContext(bld_base->base.gallivm->context);
407 }
408
409 /* TGSI_OPCODE_KILL */
410
411 static void
412 kilp_fetch_args(
413 struct lp_build_tgsi_context * bld_base,
414 struct lp_build_emit_data * emit_data)
415 {
416 emit_data->dst_type = LLVMVoidTypeInContext(bld_base->base.gallivm->context);
417 }
418
419 /* TGSI_OPCODE_LIT */
420
421 static void
422 lit_fetch_args(
423 struct lp_build_tgsi_context * bld_base,
424 struct lp_build_emit_data * emit_data)
425 {
426 /* src0.x */
427 emit_data->args[0] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_X);
428 /* src0.y */
429 emit_data->args[1] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_Y);
430 /* src0.w */
431 emit_data->args[2] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_W);
432 emit_data->arg_count = 3;
433 }
434
435 static void
436 lit_emit(
437 const struct lp_build_tgsi_action * action,
438 struct lp_build_tgsi_context * bld_base,
439 struct lp_build_emit_data * emit_data)
440 {
441 LLVMValueRef tmp0, tmp1, tmp2;
442
443 /* dst.x */
444 emit_data->output[TGSI_CHAN_X] = bld_base->base.one;
445
446 /* dst. y */
447 emit_data->output[TGSI_CHAN_Y] = lp_build_emit_llvm_binary(bld_base,
448 TGSI_OPCODE_MAX,
449 emit_data->args[0] /* src0.x */,
450 bld_base->base.zero);
451
452 /* dst.z */
453 /* XMM[1] = SrcReg[0].yyyy */
454 tmp1 = emit_data->args[1];
455 /* XMM[1] = max(XMM[1], 0) */
456 tmp1 = lp_build_emit_llvm_binary(bld_base, TGSI_OPCODE_MAX,
457 tmp1, bld_base->base.zero);
458 /* XMM[2] = SrcReg[0].wwww */
459 tmp2 = emit_data->args[2];
460 tmp1 = lp_build_emit_llvm_binary(bld_base, TGSI_OPCODE_POW,
461 tmp1, tmp2);
462 tmp0 = emit_data->args[0];
463 emit_data->output[TGSI_CHAN_Z] = lp_build_emit_llvm_ternary(bld_base,
464 TGSI_OPCODE_CMP,
465 tmp0, bld_base->base.zero, tmp1);
466 /* dst.w */
467 emit_data->output[TGSI_CHAN_W] = bld_base->base.one;
468 }
469
470 static struct lp_build_tgsi_action lit_action = {
471 lit_fetch_args, /* fetch_args */
472 lit_emit /* emit */
473 };
474
475 /* TGSI_OPCODE_LOG */
476
477 static void
478 log_emit(
479 const struct lp_build_tgsi_action * action,
480 struct lp_build_tgsi_context * bld_base,
481 struct lp_build_emit_data * emit_data)
482 {
483
484 LLVMValueRef abs_x, log_abs_x, flr_log_abs_x, ex2_flr_log_abs_x;
485
486 /* abs( src0.x) */
487 abs_x = lp_build_abs(&bld_base->base, emit_data->args[0] /* src0.x */);
488
489 /* log( abs( src0.x ) ) */
490 log_abs_x = lp_build_emit_llvm_unary(bld_base, TGSI_OPCODE_LG2,
491 abs_x);
492
493 /* floor( log( abs( src0.x ) ) ) */
494 flr_log_abs_x = lp_build_emit_llvm_unary(bld_base, TGSI_OPCODE_FLR,
495 log_abs_x);
496 /* dst.x */
497 emit_data->output[TGSI_CHAN_X] = flr_log_abs_x;
498
499 /* dst.y */
500 ex2_flr_log_abs_x = lp_build_emit_llvm_unary(bld_base, TGSI_OPCODE_EX2,
501 flr_log_abs_x);
502
503 /* abs( src0.x ) / 2^( floor( lg2( abs( src0.x ) ) ) ) */
504 emit_data->output[TGSI_CHAN_Y] = lp_build_emit_llvm_binary(bld_base,
505 TGSI_OPCODE_DIV, abs_x, ex2_flr_log_abs_x);
506
507 /* dst.x */
508 emit_data->output[TGSI_CHAN_Z] = log_abs_x;
509
510 /* dst.w */
511 emit_data->output[TGSI_CHAN_W] = bld_base->base.one;
512 }
513
514 static struct lp_build_tgsi_action log_action = {
515 scalar_unary_fetch_args, /* fetch_args */
516 log_emit /* emit */
517 };
518
519 /* TGSI_OPCODE_PK2H */
520
521 static void
522 pk2h_fetch_args(
523 struct lp_build_tgsi_context * bld_base,
524 struct lp_build_emit_data * emit_data)
525 {
526 /* src0.x */
527 emit_data->args[0] = lp_build_emit_fetch(bld_base, emit_data->inst,
528 0, TGSI_CHAN_X);
529 /* src0.y */
530 emit_data->args[1] = lp_build_emit_fetch(bld_base, emit_data->inst,
531 0, TGSI_CHAN_Y);
532 }
533
534 static void
535 pk2h_emit(
536 const struct lp_build_tgsi_action *action,
537 struct lp_build_tgsi_context *bld_base,
538 struct lp_build_emit_data *emit_data)
539 {
540 struct gallivm_state *gallivm = bld_base->base.gallivm;
541 struct lp_type f16i_t;
542 LLVMValueRef lo, hi, res;
543
544 f16i_t = lp_type_uint_vec(16, bld_base->base.type.length * 32);
545 lo = lp_build_float_to_half(gallivm, emit_data->args[0]);
546 hi = lp_build_float_to_half(gallivm, emit_data->args[1]);
547 /* maybe some interleave doubling vector width would be useful... */
548 lo = lp_build_pad_vector(gallivm, lo, bld_base->base.type.length * 2);
549 hi = lp_build_pad_vector(gallivm, hi, bld_base->base.type.length * 2);
550 res = lp_build_interleave2(gallivm, f16i_t, lo, hi, 0);
551
552 emit_data->output[emit_data->chan] = res;
553 }
554
555 static struct lp_build_tgsi_action pk2h_action = {
556 pk2h_fetch_args, /* fetch_args */
557 pk2h_emit /* emit */
558 };
559
560 /* TGSI_OPCODE_UP2H */
561
562 static void
563 up2h_emit(
564 const struct lp_build_tgsi_action *action,
565 struct lp_build_tgsi_context *bld_base,
566 struct lp_build_emit_data *emit_data)
567 {
568 struct gallivm_state *gallivm = bld_base->base.gallivm;
569 LLVMBuilderRef builder = gallivm->builder;
570 LLVMContextRef context = gallivm->context;
571 LLVMValueRef lo, hi, res[2], arg;
572 unsigned nr = bld_base->base.type.length;
573 LLVMTypeRef i16t = LLVMVectorType(LLVMInt16TypeInContext(context), nr * 2);
574
575 arg = LLVMBuildBitCast(builder, emit_data->args[0], i16t, "");
576 lo = lp_build_uninterleave1(gallivm, nr * 2, arg, 0);
577 hi = lp_build_uninterleave1(gallivm, nr * 2, arg, 1);
578 res[0] = lp_build_half_to_float(gallivm, lo);
579 res[1] = lp_build_half_to_float(gallivm, hi);
580
581 emit_data->output[0] = emit_data->output[2] = res[0];
582 emit_data->output[1] = emit_data->output[3] = res[1];
583 }
584
585 static struct lp_build_tgsi_action up2h_action = {
586 scalar_unary_fetch_args, /* fetch_args */
587 up2h_emit /* emit */
588 };
589
590 /* TGSI_OPCODE_LRP */
591
592 static void
593 lrp_emit(
594 const struct lp_build_tgsi_action * action,
595 struct lp_build_tgsi_context * bld_base,
596 struct lp_build_emit_data * emit_data)
597 {
598 struct lp_build_context *bld = &bld_base->base;
599 LLVMValueRef inv, a, b;
600
601 /* This uses the correct version: (1 - t)*a + t*b
602 *
603 * An alternative version is "a + t*(b-a)". The problem is this version
604 * doesn't return "b" for t = 1, because "a + (b-a)" isn't equal to "b"
605 * because of the floating-point rounding.
606 */
607 inv = lp_build_sub(bld, bld_base->base.one, emit_data->args[0]);
608 a = lp_build_mul(bld, emit_data->args[1], emit_data->args[0]);
609 b = lp_build_mul(bld, emit_data->args[2], inv);
610 emit_data->output[emit_data->chan] = lp_build_add(bld, a, b);
611 }
612
613 /* TGSI_OPCODE_MAD */
614
615 static void
616 mad_emit(
617 const struct lp_build_tgsi_action * action,
618 struct lp_build_tgsi_context * bld_base,
619 struct lp_build_emit_data * emit_data)
620 {
621 LLVMValueRef tmp;
622 tmp = lp_build_emit_llvm_binary(bld_base, TGSI_OPCODE_MUL,
623 emit_data->args[0],
624 emit_data->args[1]);
625 emit_data->output[emit_data->chan] = lp_build_emit_llvm_binary(bld_base,
626 TGSI_OPCODE_ADD, tmp, emit_data->args[2]);
627 }
628
629 /* TGSI_OPCODE_MOV */
630
631 static void
632 mov_emit(
633 const struct lp_build_tgsi_action * action,
634 struct lp_build_tgsi_context * bld_base,
635 struct lp_build_emit_data * emit_data)
636 {
637 emit_data->output[emit_data->chan] = emit_data->args[0];
638 }
639
640 /* TGSI_OPCODE_MUL */
641 static void
642 mul_emit(
643 const struct lp_build_tgsi_action * action,
644 struct lp_build_tgsi_context * bld_base,
645 struct lp_build_emit_data * emit_data)
646 {
647 emit_data->output[emit_data->chan] = LLVMBuildFMul(
648 bld_base->base.gallivm->builder,
649 emit_data->args[0], emit_data->args[1], "");
650 }
651
652 /*.TGSI_OPCODE_DIV.*/
653 static void fdiv_emit(
654 const struct lp_build_tgsi_action * action,
655 struct lp_build_tgsi_context * bld_base,
656 struct lp_build_emit_data * emit_data)
657 {
658 emit_data->output[emit_data->chan] = LLVMBuildFDiv(
659 bld_base->base.gallivm->builder,
660 emit_data->args[0], emit_data->args[1], "");
661 }
662
663 /*.TGSI_OPCODE_RCP.*/
664 static void rcp_emit(
665 const struct lp_build_tgsi_action * action,
666 struct lp_build_tgsi_context * bld_base,
667 struct lp_build_emit_data * emit_data)
668 {
669 LLVMValueRef one;
670 one = lp_build_const_float(bld_base->base.gallivm, 1.0f);
671 emit_data->output[emit_data->chan] = lp_build_emit_llvm_binary(bld_base,
672 TGSI_OPCODE_DIV, one, emit_data->args[0]);
673 }
674
675 /* TGSI_OPCODE_POW */
676
677 static void
678 pow_emit(
679 const struct lp_build_tgsi_action * action,
680 struct lp_build_tgsi_context * bld_base,
681 struct lp_build_emit_data * emit_data)
682 {
683 emit_data->output[emit_data->chan] = lp_build_pow(&bld_base->base,
684 emit_data->args[0], emit_data->args[1]);
685 }
686
687 static struct lp_build_tgsi_action pow_action = {
688 scalar_binary_fetch_args, /* fetch_args */
689 pow_emit /* emit */
690 };
691
692 /* TGSI_OPCODE_RSQ */
693
694 static void
695 rsq_emit(
696 const struct lp_build_tgsi_action * action,
697 struct lp_build_tgsi_context * bld_base,
698 struct lp_build_emit_data * emit_data)
699 {
700 if (bld_base->rsq_action.emit) {
701 bld_base->rsq_action.emit(&bld_base->rsq_action, bld_base, emit_data);
702 } else {
703 emit_data->output[emit_data->chan] = bld_base->base.undef;
704 }
705 }
706
707 const struct lp_build_tgsi_action rsq_action = {
708 scalar_unary_fetch_args, /* fetch_args */
709 rsq_emit /* emit */
710
711 };
712
713 /* TGSI_OPCODE_SQRT */
714
715 static void
716 sqrt_emit(
717 const struct lp_build_tgsi_action * action,
718 struct lp_build_tgsi_context * bld_base,
719 struct lp_build_emit_data * emit_data)
720 {
721 if (bld_base->sqrt_action.emit) {
722 bld_base->sqrt_action.emit(&bld_base->sqrt_action, bld_base, emit_data);
723 } else {
724 emit_data->output[emit_data->chan] = bld_base->base.undef;
725 }
726 }
727
728 const struct lp_build_tgsi_action sqrt_action = {
729 scalar_unary_fetch_args, /* fetch_args */
730 sqrt_emit /* emit */
731 };
732
733 /* TGSI_OPCODE_SCS */
734 static void
735 scs_emit(
736 const struct lp_build_tgsi_action * action,
737 struct lp_build_tgsi_context * bld_base,
738 struct lp_build_emit_data * emit_data)
739 {
740 /* dst.x */
741 emit_data->output[TGSI_CHAN_X] = lp_build_emit_llvm_unary(bld_base,
742 TGSI_OPCODE_COS, emit_data->args[0]);
743 /* dst.y */
744 emit_data->output[TGSI_CHAN_Y] = lp_build_emit_llvm_unary(bld_base,
745 TGSI_OPCODE_SIN, emit_data->args[0]);
746 /* dst.z */
747 emit_data->output[TGSI_CHAN_Z] = bld_base->base.zero;
748
749 /* dst.w */
750 emit_data->output[TGSI_CHAN_W] = bld_base->base.one;
751 }
752
753 const struct lp_build_tgsi_action scs_action = {
754 scalar_unary_fetch_args, /* fetch_args */
755 scs_emit /* emit */
756 };
757
758 /* TGSI_OPCODE_F2U */
759 static void
760 f2u_emit(
761 const struct lp_build_tgsi_action * action,
762 struct lp_build_tgsi_context * bld_base,
763 struct lp_build_emit_data * emit_data)
764 {
765 emit_data->output[emit_data->chan] =
766 LLVMBuildFPToUI(bld_base->base.gallivm->builder,
767 emit_data->args[0],
768 bld_base->base.int_vec_type, "");
769 }
770
771 /* TGSI_OPCODE_U2F */
772 static void
773 u2f_emit(
774 const struct lp_build_tgsi_action * action,
775 struct lp_build_tgsi_context * bld_base,
776 struct lp_build_emit_data * emit_data)
777 {
778 emit_data->output[emit_data->chan] =
779 LLVMBuildUIToFP(bld_base->base.gallivm->builder,
780 emit_data->args[0],
781 bld_base->base.vec_type, "");
782 }
783
784 static void
785 umad_emit(
786 const struct lp_build_tgsi_action * action,
787 struct lp_build_tgsi_context * bld_base,
788 struct lp_build_emit_data * emit_data)
789 {
790 LLVMValueRef tmp;
791 tmp = lp_build_emit_llvm_binary(bld_base, TGSI_OPCODE_UMUL,
792 emit_data->args[0],
793 emit_data->args[1]);
794 emit_data->output[emit_data->chan] = lp_build_emit_llvm_binary(bld_base,
795 TGSI_OPCODE_UADD, tmp, emit_data->args[2]);
796 }
797
798 /* TGSI_OPCODE_UMUL */
799 static void
800 umul_emit(
801 const struct lp_build_tgsi_action * action,
802 struct lp_build_tgsi_context * bld_base,
803 struct lp_build_emit_data * emit_data)
804 {
805 emit_data->output[emit_data->chan] = lp_build_mul(&bld_base->uint_bld,
806 emit_data->args[0], emit_data->args[1]);
807 }
808
809 /* TGSI_OPCODE_IMUL_HI */
810 static void
811 imul_hi_emit(
812 const struct lp_build_tgsi_action * action,
813 struct lp_build_tgsi_context * bld_base,
814 struct lp_build_emit_data * emit_data)
815 {
816 struct lp_build_context *int_bld = &bld_base->int_bld;
817 LLVMValueRef hi_bits;
818
819 assert(int_bld->type.width == 32);
820
821 /* low result bits are tossed away */
822 lp_build_mul_32_lohi(int_bld, emit_data->args[0],
823 emit_data->args[1], &hi_bits);
824 emit_data->output[emit_data->chan] = hi_bits;
825 }
826
827 static void
828 imul_hi_emit_cpu(
829 const struct lp_build_tgsi_action * action,
830 struct lp_build_tgsi_context * bld_base,
831 struct lp_build_emit_data * emit_data)
832 {
833 struct lp_build_context *int_bld = &bld_base->int_bld;
834 LLVMValueRef hi_bits;
835
836 assert(int_bld->type.width == 32);
837
838 /* low result bits are tossed away */
839 lp_build_mul_32_lohi_cpu(int_bld, emit_data->args[0],
840 emit_data->args[1], &hi_bits);
841 emit_data->output[emit_data->chan] = hi_bits;
842 }
843
844 /* TGSI_OPCODE_UMUL_HI */
845 static void
846 umul_hi_emit(
847 const struct lp_build_tgsi_action * action,
848 struct lp_build_tgsi_context * bld_base,
849 struct lp_build_emit_data * emit_data)
850 {
851 struct lp_build_context *uint_bld = &bld_base->uint_bld;
852 LLVMValueRef hi_bits;
853
854 assert(uint_bld->type.width == 32);
855
856 /* low result bits are tossed away */
857 lp_build_mul_32_lohi(uint_bld, emit_data->args[0],
858 emit_data->args[1], &hi_bits);
859 emit_data->output[emit_data->chan] = hi_bits;
860 }
861
862 static void
863 umul_hi_emit_cpu(
864 const struct lp_build_tgsi_action * action,
865 struct lp_build_tgsi_context * bld_base,
866 struct lp_build_emit_data * emit_data)
867 {
868 struct lp_build_context *uint_bld = &bld_base->uint_bld;
869 LLVMValueRef hi_bits;
870
871 assert(uint_bld->type.width == 32);
872
873 /* low result bits are tossed away */
874 lp_build_mul_32_lohi_cpu(uint_bld, emit_data->args[0],
875 emit_data->args[1], &hi_bits);
876 emit_data->output[emit_data->chan] = hi_bits;
877 }
878
879 /* TGSI_OPCODE_MAX */
880 static void fmax_emit(
881 const struct lp_build_tgsi_action * action,
882 struct lp_build_tgsi_context * bld_base,
883 struct lp_build_emit_data * emit_data)
884 {
885 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
886 emit_data->output[emit_data->chan] = LLVMBuildSelect(builder,
887 LLVMBuildFCmp(builder, LLVMRealUGE,
888 emit_data->args[0], emit_data->args[1], ""),
889 emit_data->args[0], emit_data->args[1], "");
890 }
891
892 /* TGSI_OPCODE_MIN */
893 static void fmin_emit(
894 const struct lp_build_tgsi_action * action,
895 struct lp_build_tgsi_context * bld_base,
896 struct lp_build_emit_data * emit_data)
897 {
898 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
899 emit_data->output[emit_data->chan] = LLVMBuildSelect(builder,
900 LLVMBuildFCmp(builder, LLVMRealUGE,
901 emit_data->args[0], emit_data->args[1], ""),
902 emit_data->args[1], emit_data->args[0], "");
903 }
904
905 /* TGSI_OPCODE_XPD */
906
907 static void
908 xpd_fetch_args(
909 struct lp_build_tgsi_context * bld_base,
910 struct lp_build_emit_data * emit_data)
911 {
912 dp_fetch_args(bld_base, emit_data, 3);
913 }
914
915 /**
916 * (a * b) - (c * d)
917 */
918 static LLVMValueRef
919 xpd_helper(
920 struct lp_build_tgsi_context * bld_base,
921 LLVMValueRef a,
922 LLVMValueRef b,
923 LLVMValueRef c,
924 LLVMValueRef d)
925 {
926 LLVMValueRef tmp0, tmp1;
927
928 tmp0 = lp_build_emit_llvm_binary(bld_base, TGSI_OPCODE_MUL, a, b);
929 tmp1 = lp_build_emit_llvm_binary(bld_base, TGSI_OPCODE_MUL, c, d);
930
931 return lp_build_sub(&bld_base->base, tmp0, tmp1);
932 }
933
934 static void
935 xpd_emit(
936 const struct lp_build_tgsi_action * action,
937 struct lp_build_tgsi_context * bld_base,
938 struct lp_build_emit_data * emit_data)
939 {
940 emit_data->output[TGSI_CHAN_X] = xpd_helper(bld_base,
941 emit_data->args[1] /* src0.y */, emit_data->args[5] /* src1.z */,
942 emit_data->args[4] /* src1.y */, emit_data->args[2] /* src0.z */);
943
944 emit_data->output[TGSI_CHAN_Y] = xpd_helper(bld_base,
945 emit_data->args[2] /* src0.z */, emit_data->args[3] /* src1.x */,
946 emit_data->args[5] /* src1.z */, emit_data->args[0] /* src0.x */);
947
948 emit_data->output[TGSI_CHAN_Z] = xpd_helper(bld_base,
949 emit_data->args[0] /* src0.x */, emit_data->args[4] /* src1.y */,
950 emit_data->args[3] /* src1.x */, emit_data->args[1] /* src0.y */);
951
952 emit_data->output[TGSI_CHAN_W] = bld_base->base.one;
953 }
954
955 const struct lp_build_tgsi_action xpd_action = {
956 xpd_fetch_args, /* fetch_args */
957 xpd_emit /* emit */
958 };
959
960 /* TGSI_OPCODE_D2F */
961 static void
962 d2f_emit(
963 const struct lp_build_tgsi_action * action,
964 struct lp_build_tgsi_context * bld_base,
965 struct lp_build_emit_data * emit_data)
966 {
967 emit_data->output[emit_data->chan] =
968 LLVMBuildFPTrunc(bld_base->base.gallivm->builder,
969 emit_data->args[0],
970 bld_base->base.vec_type, "");
971 }
972
973 /* TGSI_OPCODE_D2I */
974 static void
975 d2i_emit(
976 const struct lp_build_tgsi_action * action,
977 struct lp_build_tgsi_context * bld_base,
978 struct lp_build_emit_data * emit_data)
979 {
980 emit_data->output[emit_data->chan] =
981 LLVMBuildFPToSI(bld_base->base.gallivm->builder,
982 emit_data->args[0],
983 bld_base->base.int_vec_type, "");
984 }
985
986 /* TGSI_OPCODE_D2U */
987 static void
988 d2u_emit(
989 const struct lp_build_tgsi_action * action,
990 struct lp_build_tgsi_context * bld_base,
991 struct lp_build_emit_data * emit_data)
992 {
993 emit_data->output[emit_data->chan] =
994 LLVMBuildFPToUI(bld_base->base.gallivm->builder,
995 emit_data->args[0],
996 bld_base->base.int_vec_type, "");
997 }
998
999 /* TGSI_OPCODE_F2D */
1000 static void
1001 f2d_emit(
1002 const struct lp_build_tgsi_action * action,
1003 struct lp_build_tgsi_context * bld_base,
1004 struct lp_build_emit_data * emit_data)
1005 {
1006 emit_data->output[emit_data->chan] =
1007 LLVMBuildFPExt(bld_base->base.gallivm->builder,
1008 emit_data->args[0],
1009 bld_base->dbl_bld.vec_type, "");
1010 }
1011
1012 /* TGSI_OPCODE_U2D */
1013 static void
1014 u2d_emit(
1015 const struct lp_build_tgsi_action * action,
1016 struct lp_build_tgsi_context * bld_base,
1017 struct lp_build_emit_data * emit_data)
1018 {
1019 emit_data->output[emit_data->chan] =
1020 LLVMBuildUIToFP(bld_base->base.gallivm->builder,
1021 emit_data->args[0],
1022 bld_base->dbl_bld.vec_type, "");
1023 }
1024
1025 /* TGSI_OPCODE_I2D */
1026 static void
1027 i2d_emit(
1028 const struct lp_build_tgsi_action * action,
1029 struct lp_build_tgsi_context * bld_base,
1030 struct lp_build_emit_data * emit_data)
1031 {
1032 emit_data->output[emit_data->chan] =
1033 LLVMBuildSIToFP(bld_base->base.gallivm->builder,
1034 emit_data->args[0],
1035 bld_base->dbl_bld.vec_type, "");
1036 }
1037
1038 /* TGSI_OPCODE_DMAD */
1039 static void
1040 dmad_emit(
1041 const struct lp_build_tgsi_action * action,
1042 struct lp_build_tgsi_context * bld_base,
1043 struct lp_build_emit_data * emit_data)
1044 {
1045 LLVMValueRef tmp;
1046 tmp = lp_build_emit_llvm_binary(bld_base, TGSI_OPCODE_DMUL,
1047 emit_data->args[0],
1048 emit_data->args[1]);
1049 emit_data->output[emit_data->chan] = lp_build_emit_llvm_binary(bld_base,
1050 TGSI_OPCODE_DADD, tmp, emit_data->args[2]);
1051 }
1052
1053 /*.TGSI_OPCODE_DRCP.*/
1054 static void drcp_emit(
1055 const struct lp_build_tgsi_action * action,
1056 struct lp_build_tgsi_context * bld_base,
1057 struct lp_build_emit_data * emit_data)
1058 {
1059 LLVMValueRef one;
1060 one = lp_build_const_vec(bld_base->dbl_bld.gallivm, bld_base->dbl_bld.type, 1.0f);
1061 emit_data->output[emit_data->chan] = LLVMBuildFDiv(
1062 bld_base->base.gallivm->builder,
1063 one, emit_data->args[0], "");
1064 }
1065
1066 /* TGSI_OPCODE_DFRAC */
1067 static void dfrac_emit(
1068 const struct lp_build_tgsi_action * action,
1069 struct lp_build_tgsi_context * bld_base,
1070 struct lp_build_emit_data * emit_data)
1071 {
1072 LLVMValueRef tmp;
1073 tmp = lp_build_floor(&bld_base->dbl_bld,
1074 emit_data->args[0]);
1075 emit_data->output[emit_data->chan] = LLVMBuildFSub(bld_base->base.gallivm->builder,
1076 emit_data->args[0], tmp, "");
1077 }
1078
1079 static void
1080 u64mul_emit(
1081 const struct lp_build_tgsi_action * action,
1082 struct lp_build_tgsi_context * bld_base,
1083 struct lp_build_emit_data * emit_data)
1084 {
1085 emit_data->output[emit_data->chan] = lp_build_mul(&bld_base->uint64_bld,
1086 emit_data->args[0], emit_data->args[1]);
1087 }
1088
1089 static void
1090 u64mod_emit_cpu(
1091 const struct lp_build_tgsi_action * action,
1092 struct lp_build_tgsi_context * bld_base,
1093 struct lp_build_emit_data * emit_data)
1094 {
1095 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
1096 LLVMValueRef div_mask = lp_build_cmp(&bld_base->uint64_bld,
1097 PIPE_FUNC_EQUAL, emit_data->args[1],
1098 bld_base->uint64_bld.zero);
1099 /* We want to make sure that we never divide/mod by zero to not
1100 * generate sigfpe. We don't want to crash just because the
1101 * shader is doing something weird. */
1102 LLVMValueRef divisor = LLVMBuildOr(builder,
1103 div_mask,
1104 emit_data->args[1], "");
1105 LLVMValueRef result = lp_build_mod(&bld_base->uint64_bld,
1106 emit_data->args[0], divisor);
1107 /* umod by zero doesn't have a guaranteed return value chose -1 for now. */
1108 emit_data->output[emit_data->chan] = LLVMBuildOr(builder,
1109 div_mask,
1110 result, "");
1111 }
1112
1113 static void
1114 i64mod_emit_cpu(
1115 const struct lp_build_tgsi_action * action,
1116 struct lp_build_tgsi_context * bld_base,
1117 struct lp_build_emit_data * emit_data)
1118 {
1119 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
1120 LLVMValueRef div_mask = lp_build_cmp(&bld_base->uint64_bld,
1121 PIPE_FUNC_EQUAL, emit_data->args[1],
1122 bld_base->uint64_bld.zero);
1123 /* We want to make sure that we never divide/mod by zero to not
1124 * generate sigfpe. We don't want to crash just because the
1125 * shader is doing something weird. */
1126 LLVMValueRef divisor = LLVMBuildOr(builder,
1127 div_mask,
1128 emit_data->args[1], "");
1129 LLVMValueRef result = lp_build_mod(&bld_base->int64_bld,
1130 emit_data->args[0], divisor);
1131 /* umod by zero doesn't have a guaranteed return value chose -1 for now. */
1132 emit_data->output[emit_data->chan] = LLVMBuildOr(builder,
1133 div_mask,
1134 result, "");
1135 }
1136
1137 static void
1138 u64div_emit_cpu(
1139 const struct lp_build_tgsi_action * action,
1140 struct lp_build_tgsi_context * bld_base,
1141 struct lp_build_emit_data * emit_data)
1142 {
1143
1144 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
1145 LLVMValueRef div_mask = lp_build_cmp(&bld_base->uint64_bld,
1146 PIPE_FUNC_EQUAL, emit_data->args[1],
1147 bld_base->uint64_bld.zero);
1148 /* We want to make sure that we never divide/mod by zero to not
1149 * generate sigfpe. We don't want to crash just because the
1150 * shader is doing something weird. */
1151 LLVMValueRef divisor = LLVMBuildOr(builder,
1152 div_mask,
1153 emit_data->args[1], "");
1154 LLVMValueRef result = LLVMBuildUDiv(builder,
1155 emit_data->args[0], divisor, "");
1156 /* udiv by zero is guaranteed to return 0xffffffff at least with d3d10 */
1157 emit_data->output[emit_data->chan] = LLVMBuildOr(builder,
1158 div_mask,
1159 result, "");
1160 }
1161
1162 static void
1163 i64div_emit_cpu(
1164 const struct lp_build_tgsi_action * action,
1165 struct lp_build_tgsi_context * bld_base,
1166 struct lp_build_emit_data * emit_data)
1167 {
1168
1169 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
1170 LLVMValueRef div_mask = lp_build_cmp(&bld_base->int64_bld,
1171 PIPE_FUNC_EQUAL, emit_data->args[1],
1172 bld_base->int64_bld.zero);
1173 /* We want to make sure that we never divide/mod by zero to not
1174 * generate sigfpe. We don't want to crash just because the
1175 * shader is doing something weird. */
1176 LLVMValueRef divisor = LLVMBuildOr(builder,
1177 div_mask,
1178 emit_data->args[1], "");
1179 LLVMValueRef result = LLVMBuildSDiv(builder,
1180 emit_data->args[0], divisor, "");
1181 /* udiv by zero is guaranteed to return 0xffffffff at least with d3d10 */
1182 emit_data->output[emit_data->chan] = LLVMBuildOr(builder,
1183 div_mask,
1184 result, "");
1185 }
1186
1187 static void
1188 f2u64_emit(
1189 const struct lp_build_tgsi_action * action,
1190 struct lp_build_tgsi_context * bld_base,
1191 struct lp_build_emit_data * emit_data)
1192 {
1193 emit_data->output[emit_data->chan] =
1194 LLVMBuildFPToUI(bld_base->base.gallivm->builder,
1195 emit_data->args[0],
1196 bld_base->uint64_bld.vec_type, "");
1197 }
1198
1199 static void
1200 f2i64_emit(
1201 const struct lp_build_tgsi_action * action,
1202 struct lp_build_tgsi_context * bld_base,
1203 struct lp_build_emit_data * emit_data)
1204 {
1205 emit_data->output[emit_data->chan] =
1206 LLVMBuildFPToSI(bld_base->base.gallivm->builder,
1207 emit_data->args[0],
1208 bld_base->int64_bld.vec_type, "");
1209 }
1210
1211 static void
1212 u2i64_emit(
1213 const struct lp_build_tgsi_action * action,
1214 struct lp_build_tgsi_context * bld_base,
1215 struct lp_build_emit_data * emit_data)
1216 {
1217 emit_data->output[emit_data->chan] =
1218 LLVMBuildZExt(bld_base->base.gallivm->builder,
1219 emit_data->args[0],
1220 bld_base->uint64_bld.vec_type, "");
1221 }
1222
1223 static void
1224 i2i64_emit(
1225 const struct lp_build_tgsi_action * action,
1226 struct lp_build_tgsi_context * bld_base,
1227 struct lp_build_emit_data * emit_data)
1228 {
1229 emit_data->output[emit_data->chan] =
1230 LLVMBuildSExt(bld_base->base.gallivm->builder,
1231 emit_data->args[0],
1232 bld_base->int64_bld.vec_type, "");
1233 }
1234
1235 static void
1236 i642f_emit(
1237 const struct lp_build_tgsi_action * action,
1238 struct lp_build_tgsi_context * bld_base,
1239 struct lp_build_emit_data * emit_data)
1240 {
1241 emit_data->output[emit_data->chan] =
1242 LLVMBuildSIToFP(bld_base->base.gallivm->builder,
1243 emit_data->args[0],
1244 bld_base->base.vec_type, "");
1245 }
1246
1247 static void
1248 u642f_emit(
1249 const struct lp_build_tgsi_action * action,
1250 struct lp_build_tgsi_context * bld_base,
1251 struct lp_build_emit_data * emit_data)
1252 {
1253 emit_data->output[emit_data->chan] =
1254 LLVMBuildUIToFP(bld_base->base.gallivm->builder,
1255 emit_data->args[0],
1256 bld_base->base.vec_type, "");
1257 }
1258
1259 static void
1260 i642d_emit(
1261 const struct lp_build_tgsi_action * action,
1262 struct lp_build_tgsi_context * bld_base,
1263 struct lp_build_emit_data * emit_data)
1264 {
1265 emit_data->output[emit_data->chan] =
1266 LLVMBuildSIToFP(bld_base->base.gallivm->builder,
1267 emit_data->args[0],
1268 bld_base->dbl_bld.vec_type, "");
1269 }
1270
1271 static void
1272 u642d_emit(
1273 const struct lp_build_tgsi_action * action,
1274 struct lp_build_tgsi_context * bld_base,
1275 struct lp_build_emit_data * emit_data)
1276 {
1277 emit_data->output[emit_data->chan] =
1278 LLVMBuildUIToFP(bld_base->base.gallivm->builder,
1279 emit_data->args[0],
1280 bld_base->dbl_bld.vec_type, "");
1281 }
1282
1283 void
1284 lp_set_default_actions(struct lp_build_tgsi_context * bld_base)
1285 {
1286 bld_base->op_actions[TGSI_OPCODE_DP2] = dp2_action;
1287 bld_base->op_actions[TGSI_OPCODE_DP3] = dp3_action;
1288 bld_base->op_actions[TGSI_OPCODE_DP4] = dp4_action;
1289 bld_base->op_actions[TGSI_OPCODE_DP2A] = dp2a_action;
1290 bld_base->op_actions[TGSI_OPCODE_DPH] = dph_action;
1291 bld_base->op_actions[TGSI_OPCODE_DST] = dst_action;
1292 bld_base->op_actions[TGSI_OPCODE_EXP] = exp_action;
1293 bld_base->op_actions[TGSI_OPCODE_LIT] = lit_action;
1294 bld_base->op_actions[TGSI_OPCODE_LOG] = log_action;
1295 bld_base->op_actions[TGSI_OPCODE_PK2H] = pk2h_action;
1296 bld_base->op_actions[TGSI_OPCODE_RSQ] = rsq_action;
1297 bld_base->op_actions[TGSI_OPCODE_SQRT] = sqrt_action;
1298 bld_base->op_actions[TGSI_OPCODE_POW] = pow_action;
1299 bld_base->op_actions[TGSI_OPCODE_SCS] = scs_action;
1300 bld_base->op_actions[TGSI_OPCODE_UP2H] = up2h_action;
1301 bld_base->op_actions[TGSI_OPCODE_XPD] = xpd_action;
1302
1303 bld_base->op_actions[TGSI_OPCODE_BREAKC].fetch_args = scalar_unary_fetch_args;
1304 bld_base->op_actions[TGSI_OPCODE_SWITCH].fetch_args = scalar_unary_fetch_args;
1305 bld_base->op_actions[TGSI_OPCODE_CASE].fetch_args = scalar_unary_fetch_args;
1306 bld_base->op_actions[TGSI_OPCODE_COS].fetch_args = scalar_unary_fetch_args;
1307 bld_base->op_actions[TGSI_OPCODE_EX2].fetch_args = scalar_unary_fetch_args;
1308 bld_base->op_actions[TGSI_OPCODE_IF].fetch_args = scalar_unary_fetch_args;
1309 bld_base->op_actions[TGSI_OPCODE_UIF].fetch_args = scalar_unary_fetch_args;
1310 bld_base->op_actions[TGSI_OPCODE_KILL_IF].fetch_args = kil_fetch_args;
1311 bld_base->op_actions[TGSI_OPCODE_KILL].fetch_args = kilp_fetch_args;
1312 bld_base->op_actions[TGSI_OPCODE_RCP].fetch_args = scalar_unary_fetch_args;
1313 bld_base->op_actions[TGSI_OPCODE_SIN].fetch_args = scalar_unary_fetch_args;
1314 bld_base->op_actions[TGSI_OPCODE_LG2].fetch_args = scalar_unary_fetch_args;
1315
1316 bld_base->op_actions[TGSI_OPCODE_ADD].emit = add_emit;
1317 bld_base->op_actions[TGSI_OPCODE_ARR].emit = arr_emit;
1318 bld_base->op_actions[TGSI_OPCODE_END].emit = end_emit;
1319 bld_base->op_actions[TGSI_OPCODE_FRC].emit = frc_emit;
1320 bld_base->op_actions[TGSI_OPCODE_LRP].emit = lrp_emit;
1321 bld_base->op_actions[TGSI_OPCODE_MAD].emit = mad_emit;
1322 bld_base->op_actions[TGSI_OPCODE_MOV].emit = mov_emit;
1323 bld_base->op_actions[TGSI_OPCODE_MUL].emit = mul_emit;
1324 bld_base->op_actions[TGSI_OPCODE_DIV].emit = fdiv_emit;
1325 bld_base->op_actions[TGSI_OPCODE_RCP].emit = rcp_emit;
1326
1327 bld_base->op_actions[TGSI_OPCODE_UARL].emit = mov_emit;
1328 bld_base->op_actions[TGSI_OPCODE_F2U].emit = f2u_emit;
1329 bld_base->op_actions[TGSI_OPCODE_U2F].emit = u2f_emit;
1330 bld_base->op_actions[TGSI_OPCODE_UMAD].emit = umad_emit;
1331 bld_base->op_actions[TGSI_OPCODE_UMUL].emit = umul_emit;
1332 bld_base->op_actions[TGSI_OPCODE_IMUL_HI].emit = imul_hi_emit;
1333 bld_base->op_actions[TGSI_OPCODE_UMUL_HI].emit = umul_hi_emit;
1334
1335 bld_base->op_actions[TGSI_OPCODE_MAX].emit = fmax_emit;
1336 bld_base->op_actions[TGSI_OPCODE_MIN].emit = fmin_emit;
1337
1338 bld_base->op_actions[TGSI_OPCODE_DADD].emit = add_emit;
1339 bld_base->op_actions[TGSI_OPCODE_DMAX].emit = fmax_emit;
1340 bld_base->op_actions[TGSI_OPCODE_DMIN].emit = fmin_emit;
1341 bld_base->op_actions[TGSI_OPCODE_DMUL].emit = mul_emit;
1342 bld_base->op_actions[TGSI_OPCODE_DDIV].emit = fdiv_emit;
1343
1344 bld_base->op_actions[TGSI_OPCODE_D2F].emit = d2f_emit;
1345 bld_base->op_actions[TGSI_OPCODE_D2I].emit = d2i_emit;
1346 bld_base->op_actions[TGSI_OPCODE_D2U].emit = d2u_emit;
1347
1348 bld_base->op_actions[TGSI_OPCODE_F2D].emit = f2d_emit;
1349 bld_base->op_actions[TGSI_OPCODE_I2D].emit = i2d_emit;
1350 bld_base->op_actions[TGSI_OPCODE_U2D].emit = u2d_emit;
1351
1352 bld_base->op_actions[TGSI_OPCODE_DMAD].emit = dmad_emit;
1353
1354 bld_base->op_actions[TGSI_OPCODE_DRCP].emit = drcp_emit;
1355 bld_base->op_actions[TGSI_OPCODE_DFRAC].emit = dfrac_emit;
1356
1357 bld_base->op_actions[TGSI_OPCODE_U64MUL].emit = u64mul_emit;
1358
1359 bld_base->op_actions[TGSI_OPCODE_F2I64].emit = f2i64_emit;
1360 bld_base->op_actions[TGSI_OPCODE_F2U64].emit = f2u64_emit;
1361
1362 bld_base->op_actions[TGSI_OPCODE_D2I64].emit = f2i64_emit;
1363 bld_base->op_actions[TGSI_OPCODE_D2U64].emit = f2u64_emit;
1364
1365 bld_base->op_actions[TGSI_OPCODE_I2I64].emit = i2i64_emit;
1366 bld_base->op_actions[TGSI_OPCODE_U2I64].emit = u2i64_emit;
1367
1368 bld_base->op_actions[TGSI_OPCODE_I642F].emit = i642f_emit;
1369 bld_base->op_actions[TGSI_OPCODE_U642F].emit = u642f_emit;
1370
1371 bld_base->op_actions[TGSI_OPCODE_I642F].emit = i642f_emit;
1372 bld_base->op_actions[TGSI_OPCODE_U642F].emit = u642f_emit;
1373
1374 bld_base->op_actions[TGSI_OPCODE_I642D].emit = i642d_emit;
1375 bld_base->op_actions[TGSI_OPCODE_U642D].emit = u642d_emit;
1376
1377 }
1378
1379 /* CPU Only default actions */
1380
1381 /* These actions are CPU only, because they could potentially output SSE
1382 * intrinsics.
1383 */
1384
1385 /* TGSI_OPCODE_ADD (CPU Only) */
1386 static void
1387 add_emit_cpu(
1388 const struct lp_build_tgsi_action * action,
1389 struct lp_build_tgsi_context * bld_base,
1390 struct lp_build_emit_data * emit_data)
1391 {
1392 emit_data->output[emit_data->chan] = lp_build_add(&bld_base->base,
1393 emit_data->args[0], emit_data->args[1]);
1394 }
1395
1396 /* TGSI_OPCODE_AND (CPU Only) */
1397 static void
1398 and_emit_cpu(
1399 const struct lp_build_tgsi_action * action,
1400 struct lp_build_tgsi_context * bld_base,
1401 struct lp_build_emit_data * emit_data)
1402 {
1403 emit_data->output[emit_data->chan] = lp_build_and(&bld_base->uint_bld,
1404 emit_data->args[0], emit_data->args[1]);
1405 }
1406
1407 /* TGSI_OPCODE_ARL (CPU Only) */
1408 static void
1409 arl_emit_cpu(
1410 const struct lp_build_tgsi_action * action,
1411 struct lp_build_tgsi_context * bld_base,
1412 struct lp_build_emit_data * emit_data)
1413 {
1414 LLVMValueRef tmp;
1415 tmp = lp_build_floor(&bld_base->base,
1416 emit_data->args[0]);
1417 emit_data->output[emit_data->chan] = LLVMBuildFPToSI(bld_base->base.gallivm->builder, tmp,
1418 bld_base->uint_bld.vec_type, "");
1419 }
1420
1421 /* TGSI_OPCODE_ARR (CPU Only) */
1422 static void
1423 arr_emit_cpu(
1424 const struct lp_build_tgsi_action * action,
1425 struct lp_build_tgsi_context * bld_base,
1426 struct lp_build_emit_data * emit_data)
1427 {
1428 emit_data->output[emit_data->chan] = lp_build_iround(&bld_base->base, emit_data->args[0]);
1429 }
1430
1431 /* TGSI_OPCODE_CEIL (CPU Only) */
1432 static void
1433 ceil_emit_cpu(
1434 const struct lp_build_tgsi_action * action,
1435 struct lp_build_tgsi_context * bld_base,
1436 struct lp_build_emit_data * emit_data)
1437 {
1438 emit_data->output[emit_data->chan] = lp_build_ceil(&bld_base->base,
1439 emit_data->args[0]);
1440 }
1441
1442 /* TGSI_OPCODE_CMP (CPU Only) */
1443 static void
1444 cmp_emit_cpu(
1445 const struct lp_build_tgsi_action * action,
1446 struct lp_build_tgsi_context * bld_base,
1447 struct lp_build_emit_data * emit_data)
1448 {
1449 LLVMValueRef cond = lp_build_cmp(&bld_base->base, PIPE_FUNC_LESS,
1450 emit_data->args[0], bld_base->base.zero);
1451 emit_data->output[emit_data->chan] = lp_build_select(&bld_base->base,
1452 cond, emit_data->args[1], emit_data->args[2]);
1453 }
1454
1455 /* TGSI_OPCODE_UCMP (CPU Only) */
1456 static void
1457 ucmp_emit_cpu(
1458 const struct lp_build_tgsi_action * action,
1459 struct lp_build_tgsi_context * bld_base,
1460 struct lp_build_emit_data * emit_data)
1461 {
1462 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
1463 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1464 LLVMValueRef unsigned_cond =
1465 LLVMBuildBitCast(builder, emit_data->args[0], uint_bld->vec_type, "");
1466 LLVMValueRef cond = lp_build_cmp(uint_bld, PIPE_FUNC_NOTEQUAL,
1467 unsigned_cond,
1468 uint_bld->zero);
1469 emit_data->output[emit_data->chan] =
1470 lp_build_select(&bld_base->base,
1471 cond, emit_data->args[1], emit_data->args[2]);
1472 }
1473
1474 /* TGSI_OPCODE_COS (CPU Only) */
1475 static void
1476 cos_emit_cpu(
1477 const struct lp_build_tgsi_action * action,
1478 struct lp_build_tgsi_context * bld_base,
1479 struct lp_build_emit_data * emit_data)
1480 {
1481 emit_data->output[emit_data->chan] = lp_build_cos(&bld_base->base,
1482 emit_data->args[0]);
1483 }
1484
1485 /* TGSI_OPCODE_DIV (CPU Only) */
1486 static void
1487 div_emit_cpu(
1488 const struct lp_build_tgsi_action * action,
1489 struct lp_build_tgsi_context * bld_base,
1490 struct lp_build_emit_data * emit_data)
1491 {
1492 emit_data->output[emit_data->chan] = lp_build_div(&bld_base->base,
1493 emit_data->args[0], emit_data->args[1]);
1494 }
1495
1496 /* TGSI_OPCODE_EX2 (CPU Only) */
1497 static void
1498 ex2_emit_cpu(
1499 const struct lp_build_tgsi_action * action,
1500 struct lp_build_tgsi_context * bld_base,
1501 struct lp_build_emit_data * emit_data)
1502 {
1503 emit_data->output[emit_data->chan] = lp_build_exp2(&bld_base->base,
1504 emit_data->args[0]);
1505 }
1506
1507 /* TGSI_OPCODE_F2I (CPU Only) */
1508 static void
1509 f2i_emit_cpu(
1510 const struct lp_build_tgsi_action * action,
1511 struct lp_build_tgsi_context * bld_base,
1512 struct lp_build_emit_data * emit_data)
1513 {
1514 emit_data->output[emit_data->chan] = lp_build_itrunc(&bld_base->base,
1515 emit_data->args[0]);
1516 }
1517
1518 /* TGSI_OPCODE_FSET Helper (CPU Only) */
1519 static void
1520 fset_emit_cpu(
1521 const struct lp_build_tgsi_action * action,
1522 struct lp_build_tgsi_context * bld_base,
1523 struct lp_build_emit_data * emit_data,
1524 unsigned pipe_func)
1525 {
1526 LLVMValueRef cond;
1527
1528 if (pipe_func != PIPE_FUNC_NOTEQUAL) {
1529 cond = lp_build_cmp_ordered(&bld_base->base, pipe_func,
1530 emit_data->args[0], emit_data->args[1]);
1531 }
1532 else {
1533 cond = lp_build_cmp(&bld_base->base, pipe_func,
1534 emit_data->args[0], emit_data->args[1]);
1535
1536 }
1537 emit_data->output[emit_data->chan] = cond;
1538 }
1539
1540
1541 /* TGSI_OPCODE_FSEQ (CPU Only) */
1542 static void
1543 fseq_emit_cpu(
1544 const struct lp_build_tgsi_action * action,
1545 struct lp_build_tgsi_context * bld_base,
1546 struct lp_build_emit_data * emit_data)
1547 {
1548 fset_emit_cpu(action, bld_base, emit_data, PIPE_FUNC_EQUAL);
1549 }
1550
1551 /* TGSI_OPCODE_ISGE (CPU Only) */
1552 static void
1553 fsge_emit_cpu(
1554 const struct lp_build_tgsi_action * action,
1555 struct lp_build_tgsi_context * bld_base,
1556 struct lp_build_emit_data * emit_data)
1557 {
1558 fset_emit_cpu(action, bld_base, emit_data, PIPE_FUNC_GEQUAL);
1559 }
1560
1561 /* TGSI_OPCODE_ISLT (CPU Only) */
1562 static void
1563 fslt_emit_cpu(
1564 const struct lp_build_tgsi_action * action,
1565 struct lp_build_tgsi_context * bld_base,
1566 struct lp_build_emit_data * emit_data)
1567 {
1568 fset_emit_cpu(action, bld_base, emit_data, PIPE_FUNC_LESS);
1569 }
1570
1571 /* TGSI_OPCODE_USNE (CPU Only) */
1572
1573 static void
1574 fsne_emit_cpu(
1575 const struct lp_build_tgsi_action * action,
1576 struct lp_build_tgsi_context * bld_base,
1577 struct lp_build_emit_data * emit_data)
1578 {
1579 fset_emit_cpu(action, bld_base, emit_data, PIPE_FUNC_NOTEQUAL);
1580 }
1581
1582 /* TGSI_OPCODE_FLR (CPU Only) */
1583
1584 static void
1585 flr_emit_cpu(
1586 const struct lp_build_tgsi_action * action,
1587 struct lp_build_tgsi_context * bld_base,
1588 struct lp_build_emit_data * emit_data)
1589 {
1590 emit_data->output[emit_data->chan] = lp_build_floor(&bld_base->base,
1591 emit_data->args[0]);
1592 }
1593
1594 /* TGSI_OPCODE_I2F (CPU Only) */
1595 static void
1596 i2f_emit_cpu(
1597 const struct lp_build_tgsi_action * action,
1598 struct lp_build_tgsi_context * bld_base,
1599 struct lp_build_emit_data * emit_data)
1600 {
1601 emit_data->output[emit_data->chan] = lp_build_int_to_float(&bld_base->base,
1602 emit_data->args[0]);
1603 }
1604
1605 /* TGSI_OPCODE_IABS (CPU Only) */
1606 static void
1607 iabs_emit_cpu(
1608 const struct lp_build_tgsi_action * action,
1609 struct lp_build_tgsi_context * bld_base,
1610 struct lp_build_emit_data * emit_data)
1611 {
1612 emit_data->output[emit_data->chan] = lp_build_abs(&bld_base->int_bld,
1613 emit_data->args[0]);
1614 }
1615
1616 /* TGSI_OPCODE_IDIV (CPU Only) */
1617 static void
1618 idiv_emit_cpu(
1619 const struct lp_build_tgsi_action * action,
1620 struct lp_build_tgsi_context * bld_base,
1621 struct lp_build_emit_data * emit_data)
1622 {
1623 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
1624 LLVMValueRef div_mask = lp_build_cmp(&bld_base->uint_bld,
1625 PIPE_FUNC_EQUAL, emit_data->args[1],
1626 bld_base->uint_bld.zero);
1627 /* We want to make sure that we never divide/mod by zero to not
1628 * generate sigfpe. We don't want to crash just because the
1629 * shader is doing something weird. */
1630 LLVMValueRef divisor = LLVMBuildOr(builder,
1631 div_mask,
1632 emit_data->args[1], "");
1633 LLVMValueRef result = lp_build_div(&bld_base->int_bld,
1634 emit_data->args[0], divisor);
1635 LLVMValueRef not_div_mask = LLVMBuildNot(builder,
1636 div_mask,"");
1637 /* idiv by zero doesn't have a guaranteed return value chose 0 for now. */
1638 emit_data->output[emit_data->chan] = LLVMBuildAnd(builder,
1639 not_div_mask,
1640 result, "");
1641 }
1642
1643 /* TGSI_OPCODE_INEG (CPU Only) */
1644 static void
1645 ineg_emit_cpu(
1646 const struct lp_build_tgsi_action * action,
1647 struct lp_build_tgsi_context * bld_base,
1648 struct lp_build_emit_data * emit_data)
1649 {
1650 emit_data->output[emit_data->chan] = lp_build_sub(&bld_base->int_bld,
1651 bld_base->int_bld.zero,
1652 emit_data->args[0]);
1653 }
1654
1655 /* TGSI_OPCODE_ISET Helper (CPU Only) */
1656 static void
1657 iset_emit_cpu(
1658 const struct lp_build_tgsi_action * action,
1659 struct lp_build_tgsi_context * bld_base,
1660 struct lp_build_emit_data * emit_data,
1661 unsigned pipe_func)
1662 {
1663 LLVMValueRef cond = lp_build_cmp(&bld_base->int_bld, pipe_func,
1664 emit_data->args[0], emit_data->args[1]);
1665 emit_data->output[emit_data->chan] = cond;
1666 }
1667
1668 /* TGSI_OPCODE_IMAX (CPU Only) */
1669 static void
1670 imax_emit_cpu(
1671 const struct lp_build_tgsi_action * action,
1672 struct lp_build_tgsi_context * bld_base,
1673 struct lp_build_emit_data * emit_data)
1674 {
1675 emit_data->output[emit_data->chan] = lp_build_max(&bld_base->int_bld,
1676 emit_data->args[0], emit_data->args[1]);
1677 }
1678
1679 /* TGSI_OPCODE_IMIN (CPU Only) */
1680 static void
1681 imin_emit_cpu(
1682 const struct lp_build_tgsi_action * action,
1683 struct lp_build_tgsi_context * bld_base,
1684 struct lp_build_emit_data * emit_data)
1685 {
1686 emit_data->output[emit_data->chan] = lp_build_min(&bld_base->int_bld,
1687 emit_data->args[0], emit_data->args[1]);
1688 }
1689
1690 /* TGSI_OPCODE_ISGE (CPU Only) */
1691 static void
1692 isge_emit_cpu(
1693 const struct lp_build_tgsi_action * action,
1694 struct lp_build_tgsi_context * bld_base,
1695 struct lp_build_emit_data * emit_data)
1696 {
1697 iset_emit_cpu(action, bld_base, emit_data, PIPE_FUNC_GEQUAL);
1698 }
1699
1700 /* TGSI_OPCODE_ISHR (CPU Only) */
1701 static void
1702 ishr_emit_cpu(
1703 const struct lp_build_tgsi_action * action,
1704 struct lp_build_tgsi_context * bld_base,
1705 struct lp_build_emit_data * emit_data)
1706 {
1707 struct lp_build_context *int_bld = &bld_base->int_bld;
1708 LLVMValueRef mask = lp_build_const_vec(int_bld->gallivm, int_bld->type,
1709 int_bld->type.width - 1);
1710 LLVMValueRef masked_count = lp_build_and(int_bld, emit_data->args[1], mask);
1711 emit_data->output[emit_data->chan] = lp_build_shr(int_bld, emit_data->args[0],
1712 masked_count);
1713 }
1714
1715 /* TGSI_OPCODE_ISLT (CPU Only) */
1716 static void
1717 islt_emit_cpu(
1718 const struct lp_build_tgsi_action * action,
1719 struct lp_build_tgsi_context * bld_base,
1720 struct lp_build_emit_data * emit_data)
1721 {
1722 iset_emit_cpu(action, bld_base, emit_data, PIPE_FUNC_LESS);
1723 }
1724
1725
1726 /* TGSI_OPCODE_ISSG (CPU Only) */
1727 static void
1728 issg_emit_cpu(
1729 const struct lp_build_tgsi_action * action,
1730 struct lp_build_tgsi_context * bld_base,
1731 struct lp_build_emit_data * emit_data)
1732 {
1733 emit_data->output[emit_data->chan] = lp_build_sgn(&bld_base->int_bld,
1734 emit_data->args[0]);
1735 }
1736
1737 /* TGSI_OPCODE_LG2 (CPU Only) */
1738 static void
1739 lg2_emit_cpu(
1740 const struct lp_build_tgsi_action * action,
1741 struct lp_build_tgsi_context * bld_base,
1742 struct lp_build_emit_data * emit_data)
1743 {
1744 emit_data->output[emit_data->chan] = lp_build_log2_safe(&bld_base->base,
1745 emit_data->args[0]);
1746 }
1747
1748 /* TGSI_OPCODE_LOG (CPU Only) */
1749 static void
1750 log_emit_cpu(
1751 const struct lp_build_tgsi_action * action,
1752 struct lp_build_tgsi_context * bld_base,
1753 struct lp_build_emit_data * emit_data)
1754 {
1755 LLVMValueRef p_floor_log2;
1756 LLVMValueRef p_exp;
1757 LLVMValueRef p_log2;
1758 LLVMValueRef src0 = emit_data->args[0];
1759
1760 lp_build_log2_approx(&bld_base->base, src0,
1761 &p_exp, &p_floor_log2, &p_log2, FALSE);
1762
1763 emit_data->output[TGSI_CHAN_X] = p_floor_log2;
1764
1765 emit_data->output[TGSI_CHAN_Y] = lp_build_emit_llvm_binary(bld_base,
1766 TGSI_OPCODE_DIV,
1767 src0, p_exp);
1768 emit_data->output[TGSI_CHAN_Z] = p_log2;
1769
1770 emit_data->output[TGSI_CHAN_W] = bld_base->base.one;
1771
1772 }
1773
1774 /* TGSI_OPCODE_MAD (CPU Only) */
1775
1776 static void
1777 mad_emit_cpu(
1778 const struct lp_build_tgsi_action * action,
1779 struct lp_build_tgsi_context * bld_base,
1780 struct lp_build_emit_data * emit_data)
1781 {
1782 emit_data->output[emit_data->chan] =
1783 lp_build_mad(&bld_base->base,
1784 emit_data->args[0], emit_data->args[1], emit_data->args[2]);
1785 }
1786
1787 /* TGSI_OPCODE_MAX (CPU Only) */
1788
1789 static void
1790 max_emit_cpu(
1791 const struct lp_build_tgsi_action * action,
1792 struct lp_build_tgsi_context * bld_base,
1793 struct lp_build_emit_data * emit_data)
1794 {
1795 emit_data->output[emit_data->chan] =
1796 lp_build_max_ext(&bld_base->base,
1797 emit_data->args[0], emit_data->args[1],
1798 GALLIVM_NAN_RETURN_OTHER);
1799 }
1800
1801 /* TGSI_OPCODE_MIN (CPU Only) */
1802 static void
1803 min_emit_cpu(
1804 const struct lp_build_tgsi_action * action,
1805 struct lp_build_tgsi_context * bld_base,
1806 struct lp_build_emit_data * emit_data)
1807 {
1808 emit_data->output[emit_data->chan] =
1809 lp_build_min_ext(&bld_base->base,
1810 emit_data->args[0], emit_data->args[1],
1811 GALLIVM_NAN_RETURN_OTHER);
1812 }
1813
1814 /* TGSI_OPCODE_MOD (CPU Only) */
1815 static void
1816 mod_emit_cpu(
1817 const struct lp_build_tgsi_action * action,
1818 struct lp_build_tgsi_context * bld_base,
1819 struct lp_build_emit_data * emit_data)
1820 {
1821 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
1822 LLVMValueRef div_mask = lp_build_cmp(&bld_base->uint_bld,
1823 PIPE_FUNC_EQUAL, emit_data->args[1],
1824 bld_base->uint_bld.zero);
1825 /* We want to make sure that we never divide/mod by zero to not
1826 * generate sigfpe. We don't want to crash just because the
1827 * shader is doing something weird. */
1828 LLVMValueRef divisor = LLVMBuildOr(builder,
1829 div_mask,
1830 emit_data->args[1], "");
1831 LLVMValueRef result = lp_build_mod(&bld_base->int_bld,
1832 emit_data->args[0], divisor);
1833 /* umod by zero doesn't have a guaranteed return value chose -1 for now. */
1834 emit_data->output[emit_data->chan] = LLVMBuildOr(builder,
1835 div_mask,
1836 result, "");
1837 }
1838
1839 /* TGSI_OPCODE_NOT */
1840 static void
1841 not_emit_cpu(
1842 const struct lp_build_tgsi_action * action,
1843 struct lp_build_tgsi_context * bld_base,
1844 struct lp_build_emit_data * emit_data)
1845 {
1846 emit_data->output[emit_data->chan] = lp_build_not(&bld_base->uint_bld,
1847 emit_data->args[0]);
1848 }
1849
1850 /* TGSI_OPCODE_OR (CPU Only) */
1851 static void
1852 or_emit_cpu(
1853 const struct lp_build_tgsi_action * action,
1854 struct lp_build_tgsi_context * bld_base,
1855 struct lp_build_emit_data * emit_data)
1856 {
1857 emit_data->output[emit_data->chan] = lp_build_or(&bld_base->uint_bld,
1858 emit_data->args[0], emit_data->args[1]);
1859 }
1860
1861 /* TGSI_OPCODE_POW (CPU Only) */
1862 static void
1863 pow_emit_cpu(
1864 const struct lp_build_tgsi_action * action,
1865 struct lp_build_tgsi_context * bld_base,
1866 struct lp_build_emit_data * emit_data)
1867 {
1868 emit_data->output[emit_data->chan] = lp_build_pow(&bld_base->base,
1869 emit_data->args[0], emit_data->args[1]);
1870 }
1871
1872
1873 /* TGSI_OPCODE_RCP (CPU Only) */
1874
1875 static void
1876 rcp_emit_cpu(
1877 const struct lp_build_tgsi_action * action,
1878 struct lp_build_tgsi_context * bld_base,
1879 struct lp_build_emit_data * emit_data)
1880 {
1881 emit_data->output[emit_data->chan] = lp_build_rcp(&bld_base->base,
1882 emit_data->args[0]);
1883 }
1884
1885 /* Reciprical squareroot (CPU Only) */
1886 static void
1887 recip_sqrt_emit_cpu(
1888 const struct lp_build_tgsi_action * action,
1889 struct lp_build_tgsi_context * bld_base,
1890 struct lp_build_emit_data * emit_data)
1891 {
1892 emit_data->output[emit_data->chan] = lp_build_rsqrt(&bld_base->base,
1893 emit_data->args[0]);
1894 }
1895
1896 static void
1897 sqrt_emit_cpu(
1898 const struct lp_build_tgsi_action * action,
1899 struct lp_build_tgsi_context * bld_base,
1900 struct lp_build_emit_data * emit_data)
1901 {
1902 emit_data->output[emit_data->chan] = lp_build_sqrt(&bld_base->base,
1903 emit_data->args[0]);
1904 }
1905
1906
1907 /* TGSI_OPCODE_ROUND (CPU Only) */
1908 static void
1909 round_emit_cpu(
1910 const struct lp_build_tgsi_action * action,
1911 struct lp_build_tgsi_context * bld_base,
1912 struct lp_build_emit_data * emit_data)
1913 {
1914 emit_data->output[emit_data->chan] = lp_build_round(&bld_base->base,
1915 emit_data->args[0]);
1916 }
1917
1918 /* TGSI_OPCODE_SET Helper (CPU Only) */
1919
1920 static void
1921 set_emit_cpu(
1922 const struct lp_build_tgsi_action * action,
1923 struct lp_build_tgsi_context * bld_base,
1924 struct lp_build_emit_data * emit_data,
1925 unsigned pipe_func)
1926 {
1927 LLVMValueRef cond;
1928
1929 if (pipe_func != PIPE_FUNC_NOTEQUAL) {
1930 cond = lp_build_cmp_ordered(&bld_base->base, pipe_func,
1931 emit_data->args[0], emit_data->args[1]);
1932 }
1933 else {
1934 cond = lp_build_cmp(&bld_base->base, pipe_func,
1935 emit_data->args[0], emit_data->args[1]);
1936
1937 }
1938 emit_data->output[emit_data->chan] = lp_build_select(&bld_base->base,
1939 cond,
1940 bld_base->base.one,
1941 bld_base->base.zero);
1942 }
1943
1944 /* TGSI_OPCODE_SEQ (CPU Only) */
1945
1946 static void
1947 seq_emit_cpu(
1948 const struct lp_build_tgsi_action * action,
1949 struct lp_build_tgsi_context * bld_base,
1950 struct lp_build_emit_data * emit_data)
1951 {
1952 set_emit_cpu(action, bld_base, emit_data, PIPE_FUNC_EQUAL);
1953 }
1954
1955 /* TGSI_OPCODE_SGE (CPU Only) */
1956 static void
1957 sge_emit_cpu(
1958 const struct lp_build_tgsi_action * action,
1959 struct lp_build_tgsi_context * bld_base,
1960 struct lp_build_emit_data * emit_data)
1961 {
1962 set_emit_cpu(action, bld_base, emit_data, PIPE_FUNC_GEQUAL);
1963 }
1964
1965 /* TGSI_OPCODE_SGT (CPU Only)*/
1966
1967 static void
1968 sgt_emit_cpu(
1969 const struct lp_build_tgsi_action * action,
1970 struct lp_build_tgsi_context * bld_base,
1971 struct lp_build_emit_data * emit_data)
1972 {
1973 set_emit_cpu(action, bld_base, emit_data, PIPE_FUNC_GREATER);
1974 }
1975
1976 /* TGSI_OPCODE_SHL (CPU Only) */
1977 static void
1978 shl_emit_cpu(
1979 const struct lp_build_tgsi_action * action,
1980 struct lp_build_tgsi_context * bld_base,
1981 struct lp_build_emit_data * emit_data)
1982 {
1983 struct lp_build_context *uint_bld = &bld_base->uint_bld;
1984 LLVMValueRef mask = lp_build_const_vec(uint_bld->gallivm, uint_bld->type,
1985 uint_bld->type.width - 1);
1986 LLVMValueRef masked_count = lp_build_and(uint_bld, emit_data->args[1], mask);
1987 emit_data->output[emit_data->chan] = lp_build_shl(uint_bld, emit_data->args[0],
1988 masked_count);
1989 }
1990
1991 /* TGSI_OPCODE_SIN (CPU Only) */
1992 static void
1993 sin_emit_cpu(
1994 const struct lp_build_tgsi_action * action,
1995 struct lp_build_tgsi_context * bld_base,
1996 struct lp_build_emit_data * emit_data)
1997 {
1998 emit_data->output[emit_data->chan] = lp_build_sin(&bld_base->base,
1999 emit_data->args[0]);
2000 }
2001
2002 /* TGSI_OPCODE_SLE (CPU Only) */
2003 static void
2004 sle_emit_cpu(
2005 const struct lp_build_tgsi_action * action,
2006 struct lp_build_tgsi_context * bld_base,
2007 struct lp_build_emit_data * emit_data)
2008 {
2009 set_emit_cpu(action, bld_base, emit_data, PIPE_FUNC_LEQUAL);
2010 }
2011
2012 /* TGSI_OPCODE_SLT (CPU Only) */
2013 static void
2014 slt_emit_cpu(
2015 const struct lp_build_tgsi_action * action,
2016 struct lp_build_tgsi_context * bld_base,
2017 struct lp_build_emit_data * emit_data)
2018 {
2019 set_emit_cpu(action, bld_base, emit_data, PIPE_FUNC_LESS);
2020 }
2021
2022 /* TGSI_OPCODE_SNE (CPU Only) */
2023
2024 static void
2025 sne_emit_cpu(
2026 const struct lp_build_tgsi_action * action,
2027 struct lp_build_tgsi_context * bld_base,
2028 struct lp_build_emit_data * emit_data)
2029 {
2030 set_emit_cpu(action, bld_base, emit_data, PIPE_FUNC_NOTEQUAL);
2031 }
2032
2033 /* TGSI_OPCODE_SSG (CPU Only) */
2034
2035 static void
2036 ssg_emit_cpu(
2037 const struct lp_build_tgsi_action * action,
2038 struct lp_build_tgsi_context * bld_base,
2039 struct lp_build_emit_data * emit_data)
2040 {
2041 emit_data->output[emit_data->chan] = lp_build_sgn(&bld_base->base,
2042 emit_data->args[0]);
2043 }
2044
2045 /* TGSI_OPCODE_TRUNC (CPU Only) */
2046
2047 static void
2048 trunc_emit_cpu(
2049 const struct lp_build_tgsi_action * action,
2050 struct lp_build_tgsi_context * bld_base,
2051 struct lp_build_emit_data * emit_data)
2052 {
2053 emit_data->output[emit_data->chan] = lp_build_trunc(&bld_base->base,
2054 emit_data->args[0]);
2055 }
2056
2057 /* TGSI_OPCODE_UADD (CPU Only) */
2058 static void
2059 uadd_emit_cpu(
2060 const struct lp_build_tgsi_action * action,
2061 struct lp_build_tgsi_context * bld_base,
2062 struct lp_build_emit_data * emit_data)
2063 {
2064 emit_data->output[emit_data->chan] = lp_build_add(&bld_base->uint_bld,
2065 emit_data->args[0], emit_data->args[1]);
2066 }
2067
2068 /* TGSI_OPCODE_UDIV (CPU Only) */
2069 static void
2070 udiv_emit_cpu(
2071 const struct lp_build_tgsi_action * action,
2072 struct lp_build_tgsi_context * bld_base,
2073 struct lp_build_emit_data * emit_data)
2074 {
2075
2076 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
2077 LLVMValueRef div_mask = lp_build_cmp(&bld_base->uint_bld,
2078 PIPE_FUNC_EQUAL, emit_data->args[1],
2079 bld_base->uint_bld.zero);
2080 /* We want to make sure that we never divide/mod by zero to not
2081 * generate sigfpe. We don't want to crash just because the
2082 * shader is doing something weird. */
2083 LLVMValueRef divisor = LLVMBuildOr(builder,
2084 div_mask,
2085 emit_data->args[1], "");
2086 LLVMValueRef result = lp_build_div(&bld_base->uint_bld,
2087 emit_data->args[0], divisor);
2088 /* udiv by zero is guaranteed to return 0xffffffff at least with d3d10 */
2089 emit_data->output[emit_data->chan] = LLVMBuildOr(builder,
2090 div_mask,
2091 result, "");
2092 }
2093
2094 /* TGSI_OPCODE_UMAX (CPU Only) */
2095 static void
2096 umax_emit_cpu(
2097 const struct lp_build_tgsi_action * action,
2098 struct lp_build_tgsi_context * bld_base,
2099 struct lp_build_emit_data * emit_data)
2100 {
2101 emit_data->output[emit_data->chan] = lp_build_max(&bld_base->uint_bld,
2102 emit_data->args[0], emit_data->args[1]);
2103 }
2104
2105 /* TGSI_OPCODE_UMIN (CPU Only) */
2106 static void
2107 umin_emit_cpu(
2108 const struct lp_build_tgsi_action * action,
2109 struct lp_build_tgsi_context * bld_base,
2110 struct lp_build_emit_data * emit_data)
2111 {
2112 emit_data->output[emit_data->chan] = lp_build_min(&bld_base->uint_bld,
2113 emit_data->args[0], emit_data->args[1]);
2114 }
2115
2116 /* TGSI_OPCODE_UMOD (CPU Only) */
2117 static void
2118 umod_emit_cpu(
2119 const struct lp_build_tgsi_action * action,
2120 struct lp_build_tgsi_context * bld_base,
2121 struct lp_build_emit_data * emit_data)
2122 {
2123 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
2124 LLVMValueRef div_mask = lp_build_cmp(&bld_base->uint_bld,
2125 PIPE_FUNC_EQUAL, emit_data->args[1],
2126 bld_base->uint_bld.zero);
2127 /* We want to make sure that we never divide/mod by zero to not
2128 * generate sigfpe. We don't want to crash just because the
2129 * shader is doing something weird. */
2130 LLVMValueRef divisor = LLVMBuildOr(builder,
2131 div_mask,
2132 emit_data->args[1], "");
2133 LLVMValueRef result = lp_build_mod(&bld_base->uint_bld,
2134 emit_data->args[0], divisor);
2135 /* umod by zero is guaranteed to return 0xffffffff */
2136 emit_data->output[emit_data->chan] = LLVMBuildOr(builder,
2137 div_mask,
2138 result, "");
2139 }
2140
2141 /* TGSI_OPCODE_USET Helper (CPU Only) */
2142 static void
2143 uset_emit_cpu(
2144 const struct lp_build_tgsi_action * action,
2145 struct lp_build_tgsi_context * bld_base,
2146 struct lp_build_emit_data * emit_data,
2147 unsigned pipe_func)
2148 {
2149 LLVMValueRef cond = lp_build_cmp(&bld_base->uint_bld, pipe_func,
2150 emit_data->args[0], emit_data->args[1]);
2151 emit_data->output[emit_data->chan] = cond;
2152 }
2153
2154
2155 /* TGSI_OPCODE_USEQ (CPU Only) */
2156 static void
2157 useq_emit_cpu(
2158 const struct lp_build_tgsi_action * action,
2159 struct lp_build_tgsi_context * bld_base,
2160 struct lp_build_emit_data * emit_data)
2161 {
2162 uset_emit_cpu(action, bld_base, emit_data, PIPE_FUNC_EQUAL);
2163 }
2164
2165 /* TGSI_OPCODE_ISGE (CPU Only) */
2166 static void
2167 usge_emit_cpu(
2168 const struct lp_build_tgsi_action * action,
2169 struct lp_build_tgsi_context * bld_base,
2170 struct lp_build_emit_data * emit_data)
2171 {
2172 uset_emit_cpu(action, bld_base, emit_data, PIPE_FUNC_GEQUAL);
2173 }
2174
2175 /* TGSI_OPCODE_USHR (CPU Only) */
2176 static void
2177 ushr_emit_cpu(
2178 const struct lp_build_tgsi_action * action,
2179 struct lp_build_tgsi_context * bld_base,
2180 struct lp_build_emit_data * emit_data)
2181 {
2182 struct lp_build_context *uint_bld = &bld_base->uint_bld;
2183 LLVMValueRef mask = lp_build_const_vec(uint_bld->gallivm, uint_bld->type,
2184 uint_bld->type.width - 1);
2185 LLVMValueRef masked_count = lp_build_and(uint_bld, emit_data->args[1], mask);
2186 emit_data->output[emit_data->chan] = lp_build_shr(uint_bld, emit_data->args[0],
2187 masked_count);
2188 }
2189
2190 /* TGSI_OPCODE_ISLT (CPU Only) */
2191 static void
2192 uslt_emit_cpu(
2193 const struct lp_build_tgsi_action * action,
2194 struct lp_build_tgsi_context * bld_base,
2195 struct lp_build_emit_data * emit_data)
2196 {
2197 uset_emit_cpu(action, bld_base, emit_data, PIPE_FUNC_LESS);
2198 }
2199
2200 /* TGSI_OPCODE_USNE (CPU Only) */
2201
2202 static void
2203 usne_emit_cpu(
2204 const struct lp_build_tgsi_action * action,
2205 struct lp_build_tgsi_context * bld_base,
2206 struct lp_build_emit_data * emit_data)
2207 {
2208 uset_emit_cpu(action, bld_base, emit_data, PIPE_FUNC_NOTEQUAL);
2209 }
2210
2211 /* TGSI_OPCODE_XOR */
2212 static void
2213 xor_emit_cpu(
2214 const struct lp_build_tgsi_action * action,
2215 struct lp_build_tgsi_context * bld_base,
2216 struct lp_build_emit_data * emit_data)
2217 {
2218 emit_data->output[emit_data->chan] = lp_build_xor(&bld_base->uint_bld,
2219 emit_data->args[0],
2220 emit_data->args[1]);
2221 }
2222
2223 /* TGSI_OPCODE_DABS (CPU Only) */
2224 static void
2225 dabs_emit_cpu(
2226 const struct lp_build_tgsi_action * action,
2227 struct lp_build_tgsi_context * bld_base,
2228 struct lp_build_emit_data * emit_data)
2229 {
2230 emit_data->output[emit_data->chan] = lp_build_abs(&bld_base->dbl_bld,
2231 emit_data->args[0]);
2232 }
2233
2234 /* TGSI_OPCODE_DNEG (CPU Only) */
2235 static void
2236 dneg_emit_cpu(
2237 const struct lp_build_tgsi_action * action,
2238 struct lp_build_tgsi_context * bld_base,
2239 struct lp_build_emit_data * emit_data)
2240 {
2241 emit_data->output[emit_data->chan] = lp_build_sub(&bld_base->dbl_bld,
2242 bld_base->dbl_bld.zero,
2243 emit_data->args[0]);
2244 }
2245
2246 /* TGSI_OPCODE_DSET Helper (CPU Only) */
2247 static void
2248 dset_emit_cpu(
2249 const struct lp_build_tgsi_action * action,
2250 struct lp_build_tgsi_context * bld_base,
2251 struct lp_build_emit_data * emit_data,
2252 unsigned pipe_func)
2253 {
2254 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
2255 LLVMValueRef cond = lp_build_cmp(&bld_base->dbl_bld, pipe_func,
2256 emit_data->args[0], emit_data->args[1]);
2257 /* arguments were 64 bit but store as 32 bit */
2258 cond = LLVMBuildTrunc(builder, cond, bld_base->int_bld.int_vec_type, "");
2259 emit_data->output[emit_data->chan] = cond;
2260 }
2261
2262 /* TGSI_OPCODE_DSEQ (CPU Only) */
2263 static void
2264 dseq_emit_cpu(
2265 const struct lp_build_tgsi_action * action,
2266 struct lp_build_tgsi_context * bld_base,
2267 struct lp_build_emit_data * emit_data)
2268 {
2269 dset_emit_cpu(action, bld_base, emit_data, PIPE_FUNC_EQUAL);
2270 }
2271
2272 /* TGSI_OPCODE_DSGE (CPU Only) */
2273 static void
2274 dsge_emit_cpu(
2275 const struct lp_build_tgsi_action * action,
2276 struct lp_build_tgsi_context * bld_base,
2277 struct lp_build_emit_data * emit_data)
2278 {
2279 dset_emit_cpu(action, bld_base, emit_data, PIPE_FUNC_GEQUAL);
2280 }
2281
2282 /* TGSI_OPCODE_DSLT (CPU Only) */
2283 static void
2284 dslt_emit_cpu(
2285 const struct lp_build_tgsi_action * action,
2286 struct lp_build_tgsi_context * bld_base,
2287 struct lp_build_emit_data * emit_data)
2288 {
2289 dset_emit_cpu(action, bld_base, emit_data, PIPE_FUNC_LESS);
2290 }
2291
2292 /* TGSI_OPCODE_DSNE (CPU Only) */
2293 static void
2294 dsne_emit_cpu(
2295 const struct lp_build_tgsi_action * action,
2296 struct lp_build_tgsi_context * bld_base,
2297 struct lp_build_emit_data * emit_data)
2298 {
2299 dset_emit_cpu(action, bld_base, emit_data, PIPE_FUNC_NOTEQUAL);
2300 }
2301
2302 /* Double Reciprocal squareroot (CPU Only) */
2303 static void
2304 drecip_sqrt_emit_cpu(
2305 const struct lp_build_tgsi_action * action,
2306 struct lp_build_tgsi_context * bld_base,
2307 struct lp_build_emit_data * emit_data)
2308 {
2309 emit_data->output[emit_data->chan] = lp_build_rsqrt(&bld_base->dbl_bld,
2310 emit_data->args[0]);
2311 }
2312
2313 /* Double Squareroot (CPU Only) */
2314 static void
2315 dsqrt_emit_cpu(
2316 const struct lp_build_tgsi_action * action,
2317 struct lp_build_tgsi_context * bld_base,
2318 struct lp_build_emit_data * emit_data)
2319 {
2320 emit_data->output[emit_data->chan] = lp_build_sqrt(&bld_base->dbl_bld,
2321 emit_data->args[0]);
2322 }
2323
2324 static void
2325 i64abs_emit_cpu(
2326 const struct lp_build_tgsi_action * action,
2327 struct lp_build_tgsi_context * bld_base,
2328 struct lp_build_emit_data * emit_data)
2329 {
2330 emit_data->output[emit_data->chan] = lp_build_abs(&bld_base->int64_bld,
2331 emit_data->args[0]);
2332 }
2333
2334 static void
2335 i64ssg_emit_cpu(
2336 const struct lp_build_tgsi_action * action,
2337 struct lp_build_tgsi_context * bld_base,
2338 struct lp_build_emit_data * emit_data)
2339 {
2340 emit_data->output[emit_data->chan] = lp_build_sgn(&bld_base->int64_bld,
2341 emit_data->args[0]);
2342 }
2343
2344 static void
2345 i64neg_emit_cpu(
2346 const struct lp_build_tgsi_action * action,
2347 struct lp_build_tgsi_context * bld_base,
2348 struct lp_build_emit_data * emit_data)
2349 {
2350 emit_data->output[emit_data->chan] = lp_build_sub(&bld_base->int64_bld,
2351 bld_base->int64_bld.zero,
2352 emit_data->args[0]);
2353 }
2354
2355 static void
2356 u64set_emit_cpu(
2357 const struct lp_build_tgsi_action * action,
2358 struct lp_build_tgsi_context * bld_base,
2359 struct lp_build_emit_data * emit_data,
2360 unsigned pipe_func)
2361 {
2362 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
2363 LLVMValueRef cond = lp_build_cmp(&bld_base->uint64_bld, pipe_func,
2364 emit_data->args[0], emit_data->args[1]);
2365 /* arguments were 64 bit but store as 32 bit */
2366 cond = LLVMBuildTrunc(builder, cond, bld_base->int_bld.int_vec_type, "");
2367 emit_data->output[emit_data->chan] = cond;
2368 }
2369
2370 static void
2371 u64seq_emit_cpu(
2372 const struct lp_build_tgsi_action * action,
2373 struct lp_build_tgsi_context * bld_base,
2374 struct lp_build_emit_data * emit_data)
2375 {
2376 u64set_emit_cpu(action, bld_base, emit_data, PIPE_FUNC_EQUAL);
2377 }
2378
2379 static void
2380 u64sne_emit_cpu(
2381 const struct lp_build_tgsi_action * action,
2382 struct lp_build_tgsi_context * bld_base,
2383 struct lp_build_emit_data * emit_data)
2384 {
2385 u64set_emit_cpu(action, bld_base, emit_data, PIPE_FUNC_NOTEQUAL);
2386 }
2387
2388 static void
2389 u64slt_emit_cpu(
2390 const struct lp_build_tgsi_action * action,
2391 struct lp_build_tgsi_context * bld_base,
2392 struct lp_build_emit_data * emit_data)
2393 {
2394 u64set_emit_cpu(action, bld_base, emit_data, PIPE_FUNC_LESS);
2395 }
2396
2397 static void
2398 u64sge_emit_cpu(
2399 const struct lp_build_tgsi_action * action,
2400 struct lp_build_tgsi_context * bld_base,
2401 struct lp_build_emit_data * emit_data)
2402 {
2403 u64set_emit_cpu(action, bld_base, emit_data, PIPE_FUNC_GEQUAL);
2404 }
2405
2406 static void
2407 i64set_emit_cpu(
2408 const struct lp_build_tgsi_action * action,
2409 struct lp_build_tgsi_context * bld_base,
2410 struct lp_build_emit_data * emit_data,
2411 unsigned pipe_func)
2412 {
2413 LLVMBuilderRef builder = bld_base->base.gallivm->builder;
2414 LLVMValueRef cond = lp_build_cmp(&bld_base->int64_bld, pipe_func,
2415 emit_data->args[0], emit_data->args[1]);
2416 /* arguments were 64 bit but store as 32 bit */
2417 cond = LLVMBuildTrunc(builder, cond, bld_base->int_bld.int_vec_type, "");
2418 emit_data->output[emit_data->chan] = cond;
2419 }
2420
2421 static void
2422 i64slt_emit_cpu(
2423 const struct lp_build_tgsi_action * action,
2424 struct lp_build_tgsi_context * bld_base,
2425 struct lp_build_emit_data * emit_data)
2426 {
2427 i64set_emit_cpu(action, bld_base, emit_data, PIPE_FUNC_LESS);
2428 }
2429
2430 static void
2431 i64sge_emit_cpu(
2432 const struct lp_build_tgsi_action * action,
2433 struct lp_build_tgsi_context * bld_base,
2434 struct lp_build_emit_data * emit_data)
2435 {
2436 i64set_emit_cpu(action, bld_base, emit_data, PIPE_FUNC_GEQUAL);
2437 }
2438
2439 static void
2440 u64max_emit_cpu(
2441 const struct lp_build_tgsi_action * action,
2442 struct lp_build_tgsi_context * bld_base,
2443 struct lp_build_emit_data * emit_data)
2444 {
2445 emit_data->output[emit_data->chan] = lp_build_max(&bld_base->uint64_bld,
2446 emit_data->args[0], emit_data->args[1]);
2447 }
2448
2449 static void
2450 u64min_emit_cpu(
2451 const struct lp_build_tgsi_action * action,
2452 struct lp_build_tgsi_context * bld_base,
2453 struct lp_build_emit_data * emit_data)
2454 {
2455 emit_data->output[emit_data->chan] = lp_build_min(&bld_base->uint64_bld,
2456 emit_data->args[0], emit_data->args[1]);
2457 }
2458
2459 static void
2460 i64max_emit_cpu(
2461 const struct lp_build_tgsi_action * action,
2462 struct lp_build_tgsi_context * bld_base,
2463 struct lp_build_emit_data * emit_data)
2464 {
2465 emit_data->output[emit_data->chan] = lp_build_max(&bld_base->int64_bld,
2466 emit_data->args[0], emit_data->args[1]);
2467 }
2468
2469 static void
2470 i64min_emit_cpu(
2471 const struct lp_build_tgsi_action * action,
2472 struct lp_build_tgsi_context * bld_base,
2473 struct lp_build_emit_data * emit_data)
2474 {
2475 emit_data->output[emit_data->chan] = lp_build_min(&bld_base->int64_bld,
2476 emit_data->args[0], emit_data->args[1]);
2477 }
2478
2479 static void
2480 u64add_emit_cpu(
2481 const struct lp_build_tgsi_action * action,
2482 struct lp_build_tgsi_context * bld_base,
2483 struct lp_build_emit_data * emit_data)
2484 {
2485 emit_data->output[emit_data->chan] = lp_build_add(&bld_base->uint64_bld,
2486 emit_data->args[0], emit_data->args[1]);
2487 }
2488
2489 static void
2490 u64shl_emit_cpu(
2491 const struct lp_build_tgsi_action * action,
2492 struct lp_build_tgsi_context * bld_base,
2493 struct lp_build_emit_data * emit_data)
2494 {
2495 struct lp_build_context *uint_bld = &bld_base->uint64_bld;
2496 LLVMValueRef mask = lp_build_const_vec(uint_bld->gallivm, uint_bld->type,
2497 uint_bld->type.width - 1);
2498 LLVMValueRef masked_count = lp_build_and(uint_bld, emit_data->args[1], mask);
2499 emit_data->output[emit_data->chan] = lp_build_shl(uint_bld, emit_data->args[0],
2500 masked_count);
2501 }
2502
2503 static void
2504 i64shr_emit_cpu(
2505 const struct lp_build_tgsi_action * action,
2506 struct lp_build_tgsi_context * bld_base,
2507 struct lp_build_emit_data * emit_data)
2508 {
2509 struct lp_build_context *int_bld = &bld_base->int64_bld;
2510 LLVMValueRef mask = lp_build_const_vec(int_bld->gallivm, int_bld->type,
2511 int_bld->type.width - 1);
2512 LLVMValueRef masked_count = lp_build_and(int_bld, emit_data->args[1], mask);
2513 emit_data->output[emit_data->chan] = lp_build_shr(int_bld, emit_data->args[0],
2514 masked_count);
2515 }
2516
2517 static void
2518 u64shr_emit_cpu(
2519 const struct lp_build_tgsi_action * action,
2520 struct lp_build_tgsi_context * bld_base,
2521 struct lp_build_emit_data * emit_data)
2522 {
2523 struct lp_build_context *uint_bld = &bld_base->uint64_bld;
2524 LLVMValueRef mask = lp_build_const_vec(uint_bld->gallivm, uint_bld->type,
2525 uint_bld->type.width - 1);
2526 LLVMValueRef masked_count = lp_build_and(uint_bld, emit_data->args[1], mask);
2527 emit_data->output[emit_data->chan] = lp_build_shr(uint_bld, emit_data->args[0],
2528 masked_count);
2529 }
2530
2531 void
2532 lp_set_default_actions_cpu(
2533 struct lp_build_tgsi_context * bld_base)
2534 {
2535 lp_set_default_actions(bld_base);
2536 bld_base->op_actions[TGSI_OPCODE_ADD].emit = add_emit_cpu;
2537 bld_base->op_actions[TGSI_OPCODE_AND].emit = and_emit_cpu;
2538 bld_base->op_actions[TGSI_OPCODE_ARL].emit = arl_emit_cpu;
2539 bld_base->op_actions[TGSI_OPCODE_ARR].emit = arr_emit_cpu;
2540 bld_base->op_actions[TGSI_OPCODE_CEIL].emit = ceil_emit_cpu;
2541 bld_base->op_actions[TGSI_OPCODE_COS].emit = cos_emit_cpu;
2542 bld_base->op_actions[TGSI_OPCODE_CMP].emit = cmp_emit_cpu;
2543 bld_base->op_actions[TGSI_OPCODE_DIV].emit = div_emit_cpu;
2544 bld_base->op_actions[TGSI_OPCODE_EX2].emit = ex2_emit_cpu;
2545 bld_base->op_actions[TGSI_OPCODE_F2I].emit = f2i_emit_cpu;
2546 bld_base->op_actions[TGSI_OPCODE_FLR].emit = flr_emit_cpu;
2547 bld_base->op_actions[TGSI_OPCODE_FSEQ].emit = fseq_emit_cpu;
2548 bld_base->op_actions[TGSI_OPCODE_FSGE].emit = fsge_emit_cpu;
2549 bld_base->op_actions[TGSI_OPCODE_FSLT].emit = fslt_emit_cpu;
2550 bld_base->op_actions[TGSI_OPCODE_FSNE].emit = fsne_emit_cpu;
2551
2552 bld_base->op_actions[TGSI_OPCODE_I2F].emit = i2f_emit_cpu;
2553 bld_base->op_actions[TGSI_OPCODE_IABS].emit = iabs_emit_cpu;
2554 bld_base->op_actions[TGSI_OPCODE_IDIV].emit = idiv_emit_cpu;
2555 bld_base->op_actions[TGSI_OPCODE_INEG].emit = ineg_emit_cpu;
2556 bld_base->op_actions[TGSI_OPCODE_IMAX].emit = imax_emit_cpu;
2557 bld_base->op_actions[TGSI_OPCODE_IMIN].emit = imin_emit_cpu;
2558 bld_base->op_actions[TGSI_OPCODE_ISGE].emit = isge_emit_cpu;
2559 bld_base->op_actions[TGSI_OPCODE_ISHR].emit = ishr_emit_cpu;
2560 bld_base->op_actions[TGSI_OPCODE_ISLT].emit = islt_emit_cpu;
2561 bld_base->op_actions[TGSI_OPCODE_ISSG].emit = issg_emit_cpu;
2562 bld_base->op_actions[TGSI_OPCODE_IMUL_HI].emit = imul_hi_emit_cpu;
2563 bld_base->op_actions[TGSI_OPCODE_UMUL_HI].emit = umul_hi_emit_cpu;
2564
2565 bld_base->op_actions[TGSI_OPCODE_LG2].emit = lg2_emit_cpu;
2566 bld_base->op_actions[TGSI_OPCODE_LOG].emit = log_emit_cpu;
2567 bld_base->op_actions[TGSI_OPCODE_MAD].emit = mad_emit_cpu;
2568 bld_base->op_actions[TGSI_OPCODE_MAX].emit = max_emit_cpu;
2569 bld_base->op_actions[TGSI_OPCODE_MIN].emit = min_emit_cpu;
2570 bld_base->op_actions[TGSI_OPCODE_MOD].emit = mod_emit_cpu;
2571 bld_base->op_actions[TGSI_OPCODE_NOT].emit = not_emit_cpu;
2572 bld_base->op_actions[TGSI_OPCODE_OR].emit = or_emit_cpu;
2573 bld_base->op_actions[TGSI_OPCODE_POW].emit = pow_emit_cpu;
2574 bld_base->op_actions[TGSI_OPCODE_RCP].emit = rcp_emit_cpu;
2575 bld_base->op_actions[TGSI_OPCODE_ROUND].emit = round_emit_cpu;
2576 bld_base->op_actions[TGSI_OPCODE_SEQ].emit = seq_emit_cpu;
2577 bld_base->op_actions[TGSI_OPCODE_SGE].emit = sge_emit_cpu;
2578 bld_base->op_actions[TGSI_OPCODE_SGT].emit = sgt_emit_cpu;
2579 bld_base->op_actions[TGSI_OPCODE_SIN].emit = sin_emit_cpu;
2580 bld_base->op_actions[TGSI_OPCODE_SHL].emit = shl_emit_cpu;
2581 bld_base->op_actions[TGSI_OPCODE_SLE].emit = sle_emit_cpu;
2582 bld_base->op_actions[TGSI_OPCODE_SLT].emit = slt_emit_cpu;
2583 bld_base->op_actions[TGSI_OPCODE_SNE].emit = sne_emit_cpu;
2584 bld_base->op_actions[TGSI_OPCODE_SSG].emit = ssg_emit_cpu;
2585 bld_base->op_actions[TGSI_OPCODE_TRUNC].emit = trunc_emit_cpu;
2586
2587 bld_base->rsq_action.emit = recip_sqrt_emit_cpu;
2588 bld_base->sqrt_action.emit = sqrt_emit_cpu;
2589
2590 bld_base->op_actions[TGSI_OPCODE_UADD].emit = uadd_emit_cpu;
2591 bld_base->op_actions[TGSI_OPCODE_UCMP].emit = ucmp_emit_cpu;
2592 bld_base->op_actions[TGSI_OPCODE_UDIV].emit = udiv_emit_cpu;
2593 bld_base->op_actions[TGSI_OPCODE_UMAX].emit = umax_emit_cpu;
2594 bld_base->op_actions[TGSI_OPCODE_UMIN].emit = umin_emit_cpu;
2595 bld_base->op_actions[TGSI_OPCODE_UMOD].emit = umod_emit_cpu;
2596 bld_base->op_actions[TGSI_OPCODE_USEQ].emit = useq_emit_cpu;
2597 bld_base->op_actions[TGSI_OPCODE_USGE].emit = usge_emit_cpu;
2598 bld_base->op_actions[TGSI_OPCODE_USHR].emit = ushr_emit_cpu;
2599 bld_base->op_actions[TGSI_OPCODE_USLT].emit = uslt_emit_cpu;
2600 bld_base->op_actions[TGSI_OPCODE_USNE].emit = usne_emit_cpu;
2601
2602 bld_base->op_actions[TGSI_OPCODE_XOR].emit = xor_emit_cpu;
2603
2604 bld_base->op_actions[TGSI_OPCODE_DABS].emit = dabs_emit_cpu;
2605 bld_base->op_actions[TGSI_OPCODE_DNEG].emit = dneg_emit_cpu;
2606 bld_base->op_actions[TGSI_OPCODE_DSEQ].emit = dseq_emit_cpu;
2607 bld_base->op_actions[TGSI_OPCODE_DSGE].emit = dsge_emit_cpu;
2608 bld_base->op_actions[TGSI_OPCODE_DSLT].emit = dslt_emit_cpu;
2609 bld_base->op_actions[TGSI_OPCODE_DSNE].emit = dsne_emit_cpu;
2610
2611 bld_base->op_actions[TGSI_OPCODE_DRSQ].emit = drecip_sqrt_emit_cpu;
2612 bld_base->op_actions[TGSI_OPCODE_DSQRT].emit = dsqrt_emit_cpu;
2613
2614 bld_base->op_actions[TGSI_OPCODE_I64ABS].emit = i64abs_emit_cpu;
2615 bld_base->op_actions[TGSI_OPCODE_I64SSG].emit = i64ssg_emit_cpu;
2616 bld_base->op_actions[TGSI_OPCODE_I64NEG].emit = i64neg_emit_cpu;
2617
2618 bld_base->op_actions[TGSI_OPCODE_U64SEQ].emit = u64seq_emit_cpu;
2619 bld_base->op_actions[TGSI_OPCODE_U64SNE].emit = u64sne_emit_cpu;
2620 bld_base->op_actions[TGSI_OPCODE_U64SLT].emit = u64slt_emit_cpu;
2621 bld_base->op_actions[TGSI_OPCODE_U64SGE].emit = u64sge_emit_cpu;
2622 bld_base->op_actions[TGSI_OPCODE_I64SLT].emit = i64slt_emit_cpu;
2623 bld_base->op_actions[TGSI_OPCODE_I64SGE].emit = i64sge_emit_cpu;
2624
2625 bld_base->op_actions[TGSI_OPCODE_U64MIN].emit = u64min_emit_cpu;
2626 bld_base->op_actions[TGSI_OPCODE_U64MAX].emit = u64max_emit_cpu;
2627 bld_base->op_actions[TGSI_OPCODE_I64MIN].emit = i64min_emit_cpu;
2628 bld_base->op_actions[TGSI_OPCODE_I64MAX].emit = i64max_emit_cpu;
2629
2630 bld_base->op_actions[TGSI_OPCODE_U64ADD].emit = u64add_emit_cpu;
2631 bld_base->op_actions[TGSI_OPCODE_U64MOD].emit = u64mod_emit_cpu;
2632 bld_base->op_actions[TGSI_OPCODE_I64MOD].emit = i64mod_emit_cpu;
2633 bld_base->op_actions[TGSI_OPCODE_U64DIV].emit = u64div_emit_cpu;
2634 bld_base->op_actions[TGSI_OPCODE_I64DIV].emit = i64div_emit_cpu;
2635
2636 bld_base->op_actions[TGSI_OPCODE_U64SHL].emit = u64shl_emit_cpu;
2637 bld_base->op_actions[TGSI_OPCODE_I64SHR].emit = i64shr_emit_cpu;
2638 bld_base->op_actions[TGSI_OPCODE_U64SHR].emit = u64shr_emit_cpu;
2639 }