ac: add ac_build_isign()
[mesa.git] / src / gallium / drivers / radeonsi / si_shader_tgsi_alu.c
1 /*
2 * Copyright 2016 Advanced Micro Devices, Inc.
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "si_shader_internal.h"
25 #include "gallivm/lp_bld_const.h"
26 #include "gallivm/lp_bld_intr.h"
27 #include "gallivm/lp_bld_gather.h"
28 #include "tgsi/tgsi_parse.h"
29 #include "amd/common/ac_llvm_build.h"
30
31 static void kill_if_fetch_args(struct lp_build_tgsi_context *bld_base,
32 struct lp_build_emit_data *emit_data)
33 {
34 const struct tgsi_full_instruction *inst = emit_data->inst;
35 struct si_shader_context *ctx = si_shader_context(bld_base);
36 LLVMBuilderRef builder = ctx->ac.builder;
37 unsigned i;
38 LLVMValueRef conds[TGSI_NUM_CHANNELS];
39
40 for (i = 0; i < TGSI_NUM_CHANNELS; i++) {
41 LLVMValueRef value = lp_build_emit_fetch(bld_base, inst, 0, i);
42 conds[i] = LLVMBuildFCmp(builder, LLVMRealOGE, value,
43 ctx->ac.f32_0, "");
44 }
45
46 /* And the conditions together */
47 for (i = TGSI_NUM_CHANNELS - 1; i > 0; i--) {
48 conds[i - 1] = LLVMBuildAnd(builder, conds[i], conds[i - 1], "");
49 }
50
51 emit_data->dst_type = ctx->voidt;
52 emit_data->arg_count = 1;
53 emit_data->args[0] = conds[0];
54 }
55
56 static void kil_emit(const struct lp_build_tgsi_action *action,
57 struct lp_build_tgsi_context *bld_base,
58 struct lp_build_emit_data *emit_data)
59 {
60 struct si_shader_context *ctx = si_shader_context(bld_base);
61 LLVMBuilderRef builder = ctx->ac.builder;
62 LLVMValueRef visible;
63
64 if (emit_data->inst->Instruction.Opcode == TGSI_OPCODE_KILL_IF) {
65 visible = emit_data->args[0];
66 } else {
67 assert(emit_data->inst->Instruction.Opcode == TGSI_OPCODE_KILL);
68 visible = LLVMConstInt(ctx->i1, false, 0);
69 }
70
71 if (ctx->shader->selector->force_correct_derivs_after_kill) {
72 /* LLVM 6.0 can kill immediately while maintaining WQM. */
73 if (HAVE_LLVM >= 0x0600) {
74 ac_build_kill_if_false(&ctx->ac,
75 ac_build_wqm_vote(&ctx->ac, visible));
76 }
77
78 LLVMValueRef mask = LLVMBuildLoad(builder, ctx->postponed_kill, "");
79 mask = LLVMBuildAnd(builder, mask, visible, "");
80 LLVMBuildStore(builder, mask, ctx->postponed_kill);
81 return;
82 }
83
84 ac_build_kill_if_false(&ctx->ac, visible);
85 }
86
87 static void emit_icmp(const struct lp_build_tgsi_action *action,
88 struct lp_build_tgsi_context *bld_base,
89 struct lp_build_emit_data *emit_data)
90 {
91 unsigned pred;
92 struct si_shader_context *ctx = si_shader_context(bld_base);
93
94 switch (emit_data->inst->Instruction.Opcode) {
95 case TGSI_OPCODE_USEQ:
96 case TGSI_OPCODE_U64SEQ: pred = LLVMIntEQ; break;
97 case TGSI_OPCODE_USNE:
98 case TGSI_OPCODE_U64SNE: pred = LLVMIntNE; break;
99 case TGSI_OPCODE_USGE:
100 case TGSI_OPCODE_U64SGE: pred = LLVMIntUGE; break;
101 case TGSI_OPCODE_USLT:
102 case TGSI_OPCODE_U64SLT: pred = LLVMIntULT; break;
103 case TGSI_OPCODE_ISGE:
104 case TGSI_OPCODE_I64SGE: pred = LLVMIntSGE; break;
105 case TGSI_OPCODE_ISLT:
106 case TGSI_OPCODE_I64SLT: pred = LLVMIntSLT; break;
107 default:
108 assert(!"unknown instruction");
109 pred = 0;
110 break;
111 }
112
113 LLVMValueRef v = LLVMBuildICmp(ctx->ac.builder, pred,
114 emit_data->args[0], emit_data->args[1],"");
115
116 v = LLVMBuildSExtOrBitCast(ctx->ac.builder, v, ctx->i32, "");
117
118 emit_data->output[emit_data->chan] = v;
119 }
120
121 static void emit_ucmp(const struct lp_build_tgsi_action *action,
122 struct lp_build_tgsi_context *bld_base,
123 struct lp_build_emit_data *emit_data)
124 {
125 struct si_shader_context *ctx = si_shader_context(bld_base);
126 LLVMValueRef arg0 = ac_to_integer(&ctx->ac, emit_data->args[0]);
127
128 LLVMValueRef v = LLVMBuildICmp(ctx->ac.builder, LLVMIntNE, arg0,
129 ctx->i32_0, "");
130
131 emit_data->output[emit_data->chan] =
132 LLVMBuildSelect(ctx->ac.builder, v, emit_data->args[1], emit_data->args[2], "");
133 }
134
135 static void emit_cmp(const struct lp_build_tgsi_action *action,
136 struct lp_build_tgsi_context *bld_base,
137 struct lp_build_emit_data *emit_data)
138 {
139 struct si_shader_context *ctx = si_shader_context(bld_base);
140 LLVMValueRef cond, *args = emit_data->args;
141
142 cond = LLVMBuildFCmp(ctx->ac.builder, LLVMRealOLT, args[0],
143 ctx->ac.f32_0, "");
144
145 emit_data->output[emit_data->chan] =
146 LLVMBuildSelect(ctx->ac.builder, cond, args[1], args[2], "");
147 }
148
149 static void emit_set_cond(const struct lp_build_tgsi_action *action,
150 struct lp_build_tgsi_context *bld_base,
151 struct lp_build_emit_data *emit_data)
152 {
153 struct si_shader_context *ctx = si_shader_context(bld_base);
154 LLVMRealPredicate pred;
155 LLVMValueRef cond;
156
157 /* Use ordered for everything but NE (which is usual for
158 * float comparisons)
159 */
160 switch (emit_data->inst->Instruction.Opcode) {
161 case TGSI_OPCODE_SGE: pred = LLVMRealOGE; break;
162 case TGSI_OPCODE_SEQ: pred = LLVMRealOEQ; break;
163 case TGSI_OPCODE_SLE: pred = LLVMRealOLE; break;
164 case TGSI_OPCODE_SLT: pred = LLVMRealOLT; break;
165 case TGSI_OPCODE_SNE: pred = LLVMRealUNE; break;
166 case TGSI_OPCODE_SGT: pred = LLVMRealOGT; break;
167 default: assert(!"unknown instruction"); pred = 0; break;
168 }
169
170 cond = LLVMBuildFCmp(ctx->ac.builder,
171 pred, emit_data->args[0], emit_data->args[1], "");
172
173 emit_data->output[emit_data->chan] = LLVMBuildSelect(ctx->ac.builder,
174 cond, ctx->ac.f32_1, ctx->ac.f32_0, "");
175 }
176
177 static void emit_fcmp(const struct lp_build_tgsi_action *action,
178 struct lp_build_tgsi_context *bld_base,
179 struct lp_build_emit_data *emit_data)
180 {
181 struct si_shader_context *ctx = si_shader_context(bld_base);
182 LLVMRealPredicate pred;
183
184 /* Use ordered for everything but NE (which is usual for
185 * float comparisons)
186 */
187 switch (emit_data->inst->Instruction.Opcode) {
188 case TGSI_OPCODE_FSEQ: pred = LLVMRealOEQ; break;
189 case TGSI_OPCODE_FSGE: pred = LLVMRealOGE; break;
190 case TGSI_OPCODE_FSLT: pred = LLVMRealOLT; break;
191 case TGSI_OPCODE_FSNE: pred = LLVMRealUNE; break;
192 default: assert(!"unknown instruction"); pred = 0; break;
193 }
194
195 LLVMValueRef v = LLVMBuildFCmp(ctx->ac.builder, pred,
196 emit_data->args[0], emit_data->args[1],"");
197
198 v = LLVMBuildSExtOrBitCast(ctx->ac.builder, v, ctx->i32, "");
199
200 emit_data->output[emit_data->chan] = v;
201 }
202
203 static void emit_dcmp(const struct lp_build_tgsi_action *action,
204 struct lp_build_tgsi_context *bld_base,
205 struct lp_build_emit_data *emit_data)
206 {
207 struct si_shader_context *ctx = si_shader_context(bld_base);
208 LLVMRealPredicate pred;
209
210 /* Use ordered for everything but NE (which is usual for
211 * float comparisons)
212 */
213 switch (emit_data->inst->Instruction.Opcode) {
214 case TGSI_OPCODE_DSEQ: pred = LLVMRealOEQ; break;
215 case TGSI_OPCODE_DSGE: pred = LLVMRealOGE; break;
216 case TGSI_OPCODE_DSLT: pred = LLVMRealOLT; break;
217 case TGSI_OPCODE_DSNE: pred = LLVMRealUNE; break;
218 default: assert(!"unknown instruction"); pred = 0; break;
219 }
220
221 LLVMValueRef v = LLVMBuildFCmp(ctx->ac.builder, pred,
222 emit_data->args[0], emit_data->args[1],"");
223
224 v = LLVMBuildSExtOrBitCast(ctx->ac.builder, v, ctx->i32, "");
225
226 emit_data->output[emit_data->chan] = v;
227 }
228
229 static void emit_not(const struct lp_build_tgsi_action *action,
230 struct lp_build_tgsi_context *bld_base,
231 struct lp_build_emit_data *emit_data)
232 {
233 struct si_shader_context *ctx = si_shader_context(bld_base);
234 LLVMValueRef v = ac_to_integer(&ctx->ac, emit_data->args[0]);
235 emit_data->output[emit_data->chan] = LLVMBuildNot(ctx->ac.builder, v, "");
236 }
237
238 static void emit_arl(const struct lp_build_tgsi_action *action,
239 struct lp_build_tgsi_context *bld_base,
240 struct lp_build_emit_data *emit_data)
241 {
242 struct si_shader_context *ctx = si_shader_context(bld_base);
243 LLVMValueRef floor_index = lp_build_emit_llvm_unary(bld_base, TGSI_OPCODE_FLR, emit_data->args[0]);
244 emit_data->output[emit_data->chan] = LLVMBuildFPToSI(ctx->ac.builder,
245 floor_index, ctx->i32, "");
246 }
247
248 static void emit_and(const struct lp_build_tgsi_action *action,
249 struct lp_build_tgsi_context *bld_base,
250 struct lp_build_emit_data *emit_data)
251 {
252 struct si_shader_context *ctx = si_shader_context(bld_base);
253 emit_data->output[emit_data->chan] = LLVMBuildAnd(ctx->ac.builder,
254 emit_data->args[0], emit_data->args[1], "");
255 }
256
257 static void emit_or(const struct lp_build_tgsi_action *action,
258 struct lp_build_tgsi_context *bld_base,
259 struct lp_build_emit_data *emit_data)
260 {
261 struct si_shader_context *ctx = si_shader_context(bld_base);
262 emit_data->output[emit_data->chan] = LLVMBuildOr(ctx->ac.builder,
263 emit_data->args[0], emit_data->args[1], "");
264 }
265
266 static void emit_uadd(const struct lp_build_tgsi_action *action,
267 struct lp_build_tgsi_context *bld_base,
268 struct lp_build_emit_data *emit_data)
269 {
270 struct si_shader_context *ctx = si_shader_context(bld_base);
271 emit_data->output[emit_data->chan] = LLVMBuildAdd(ctx->ac.builder,
272 emit_data->args[0], emit_data->args[1], "");
273 }
274
275 static void emit_udiv(const struct lp_build_tgsi_action *action,
276 struct lp_build_tgsi_context *bld_base,
277 struct lp_build_emit_data *emit_data)
278 {
279 struct si_shader_context *ctx = si_shader_context(bld_base);
280 emit_data->output[emit_data->chan] = LLVMBuildUDiv(ctx->ac.builder,
281 emit_data->args[0], emit_data->args[1], "");
282 }
283
284 static void emit_idiv(const struct lp_build_tgsi_action *action,
285 struct lp_build_tgsi_context *bld_base,
286 struct lp_build_emit_data *emit_data)
287 {
288 struct si_shader_context *ctx = si_shader_context(bld_base);
289 emit_data->output[emit_data->chan] = LLVMBuildSDiv(ctx->ac.builder,
290 emit_data->args[0], emit_data->args[1], "");
291 }
292
293 static void emit_mod(const struct lp_build_tgsi_action *action,
294 struct lp_build_tgsi_context *bld_base,
295 struct lp_build_emit_data *emit_data)
296 {
297 struct si_shader_context *ctx = si_shader_context(bld_base);
298 emit_data->output[emit_data->chan] = LLVMBuildSRem(ctx->ac.builder,
299 emit_data->args[0], emit_data->args[1], "");
300 }
301
302 static void emit_umod(const struct lp_build_tgsi_action *action,
303 struct lp_build_tgsi_context *bld_base,
304 struct lp_build_emit_data *emit_data)
305 {
306 struct si_shader_context *ctx = si_shader_context(bld_base);
307 emit_data->output[emit_data->chan] = LLVMBuildURem(ctx->ac.builder,
308 emit_data->args[0], emit_data->args[1], "");
309 }
310
311 static void emit_shl(const struct lp_build_tgsi_action *action,
312 struct lp_build_tgsi_context *bld_base,
313 struct lp_build_emit_data *emit_data)
314 {
315 struct si_shader_context *ctx = si_shader_context(bld_base);
316 emit_data->output[emit_data->chan] = LLVMBuildShl(ctx->ac.builder,
317 emit_data->args[0], emit_data->args[1], "");
318 }
319
320 static void emit_ushr(const struct lp_build_tgsi_action *action,
321 struct lp_build_tgsi_context *bld_base,
322 struct lp_build_emit_data *emit_data)
323 {
324 struct si_shader_context *ctx = si_shader_context(bld_base);
325 emit_data->output[emit_data->chan] = LLVMBuildLShr(ctx->ac.builder,
326 emit_data->args[0], emit_data->args[1], "");
327 }
328 static void emit_ishr(const struct lp_build_tgsi_action *action,
329 struct lp_build_tgsi_context *bld_base,
330 struct lp_build_emit_data *emit_data)
331 {
332 struct si_shader_context *ctx = si_shader_context(bld_base);
333 emit_data->output[emit_data->chan] = LLVMBuildAShr(ctx->ac.builder,
334 emit_data->args[0], emit_data->args[1], "");
335 }
336
337 static void emit_xor(const struct lp_build_tgsi_action *action,
338 struct lp_build_tgsi_context *bld_base,
339 struct lp_build_emit_data *emit_data)
340 {
341 struct si_shader_context *ctx = si_shader_context(bld_base);
342 emit_data->output[emit_data->chan] = LLVMBuildXor(ctx->ac.builder,
343 emit_data->args[0], emit_data->args[1], "");
344 }
345
346 static void emit_ssg(const struct lp_build_tgsi_action *action,
347 struct lp_build_tgsi_context *bld_base,
348 struct lp_build_emit_data *emit_data)
349 {
350 struct si_shader_context *ctx = si_shader_context(bld_base);
351 LLVMBuilderRef builder = ctx->ac.builder;
352
353 LLVMValueRef cmp, val;
354
355 if (emit_data->inst->Instruction.Opcode == TGSI_OPCODE_I64SSG) {
356 val = ac_build_isign(&ctx->ac, emit_data->args[0], 64);
357 } else if (emit_data->inst->Instruction.Opcode == TGSI_OPCODE_ISSG) {
358 val = ac_build_isign(&ctx->ac, emit_data->args[0], 32);
359 } else if (emit_data->inst->Instruction.Opcode == TGSI_OPCODE_DSSG) {
360 cmp = LLVMBuildFCmp(builder, LLVMRealOGT, emit_data->args[0], bld_base->dbl_bld.zero, "");
361 val = LLVMBuildSelect(builder, cmp, bld_base->dbl_bld.one, emit_data->args[0], "");
362 cmp = LLVMBuildFCmp(builder, LLVMRealOGE, val, bld_base->dbl_bld.zero, "");
363 val = LLVMBuildSelect(builder, cmp, val, LLVMConstReal(bld_base->dbl_bld.elem_type, -1), "");
364 } else { // float SSG
365 cmp = LLVMBuildFCmp(builder, LLVMRealOGT, emit_data->args[0], ctx->ac.f32_0, "");
366 val = LLVMBuildSelect(builder, cmp, ctx->ac.f32_1, emit_data->args[0], "");
367 cmp = LLVMBuildFCmp(builder, LLVMRealOGE, val, ctx->ac.f32_0, "");
368 val = LLVMBuildSelect(builder, cmp, val, LLVMConstReal(ctx->f32, -1), "");
369 }
370
371 emit_data->output[emit_data->chan] = val;
372 }
373
374 static void emit_ineg(const struct lp_build_tgsi_action *action,
375 struct lp_build_tgsi_context *bld_base,
376 struct lp_build_emit_data *emit_data)
377 {
378 struct si_shader_context *ctx = si_shader_context(bld_base);
379 emit_data->output[emit_data->chan] = LLVMBuildNeg(ctx->ac.builder,
380 emit_data->args[0], "");
381 }
382
383 static void emit_dneg(const struct lp_build_tgsi_action *action,
384 struct lp_build_tgsi_context *bld_base,
385 struct lp_build_emit_data *emit_data)
386 {
387 struct si_shader_context *ctx = si_shader_context(bld_base);
388 emit_data->output[emit_data->chan] = LLVMBuildFNeg(ctx->ac.builder,
389 emit_data->args[0], "");
390 }
391
392 static void emit_frac(const struct lp_build_tgsi_action *action,
393 struct lp_build_tgsi_context *bld_base,
394 struct lp_build_emit_data *emit_data)
395 {
396 struct si_shader_context *ctx = si_shader_context(bld_base);
397 unsigned bitsize;
398
399 if (emit_data->info->opcode == TGSI_OPCODE_FRC)
400 bitsize = 32;
401 else if (emit_data->info->opcode == TGSI_OPCODE_DFRAC)
402 bitsize = 64;
403 else {
404 assert(0);
405 return;
406 }
407
408 emit_data->output[emit_data->chan] =
409 ac_build_fract(&ctx->ac, emit_data->args[0], bitsize);
410 }
411
412 static void emit_f2i(const struct lp_build_tgsi_action *action,
413 struct lp_build_tgsi_context *bld_base,
414 struct lp_build_emit_data *emit_data)
415 {
416 struct si_shader_context *ctx = si_shader_context(bld_base);
417 emit_data->output[emit_data->chan] = LLVMBuildFPToSI(ctx->ac.builder,
418 emit_data->args[0], ctx->i32, "");
419 }
420
421 static void emit_f2u(const struct lp_build_tgsi_action *action,
422 struct lp_build_tgsi_context *bld_base,
423 struct lp_build_emit_data *emit_data)
424 {
425 struct si_shader_context *ctx = si_shader_context(bld_base);
426 emit_data->output[emit_data->chan] = LLVMBuildFPToUI(ctx->ac.builder,
427 emit_data->args[0], ctx->i32, "");
428 }
429
430 static void emit_i2f(const struct lp_build_tgsi_action *action,
431 struct lp_build_tgsi_context *bld_base,
432 struct lp_build_emit_data *emit_data)
433 {
434 struct si_shader_context *ctx = si_shader_context(bld_base);
435 emit_data->output[emit_data->chan] = LLVMBuildSIToFP(ctx->ac.builder,
436 emit_data->args[0], ctx->f32, "");
437 }
438
439 static void emit_u2f(const struct lp_build_tgsi_action *action,
440 struct lp_build_tgsi_context *bld_base,
441 struct lp_build_emit_data *emit_data)
442 {
443 struct si_shader_context *ctx = si_shader_context(bld_base);
444 emit_data->output[emit_data->chan] = LLVMBuildUIToFP(ctx->ac.builder,
445 emit_data->args[0], ctx->f32, "");
446 }
447
448 static void
449 build_tgsi_intrinsic_nomem(const struct lp_build_tgsi_action *action,
450 struct lp_build_tgsi_context *bld_base,
451 struct lp_build_emit_data *emit_data)
452 {
453 struct si_shader_context *ctx = si_shader_context(bld_base);
454 emit_data->output[emit_data->chan] =
455 lp_build_intrinsic(ctx->ac.builder, action->intr_name,
456 emit_data->dst_type, emit_data->args,
457 emit_data->arg_count, LP_FUNC_ATTR_READNONE);
458 }
459
460 static void emit_bfi(const struct lp_build_tgsi_action *action,
461 struct lp_build_tgsi_context *bld_base,
462 struct lp_build_emit_data *emit_data)
463 {
464 struct si_shader_context *ctx = si_shader_context(bld_base);
465 LLVMBuilderRef builder = ctx->ac.builder;
466 LLVMValueRef bfi_args[3];
467 LLVMValueRef bfi_sm5;
468 LLVMValueRef cond;
469
470 // Calculate the bitmask: (((1 << src3) - 1) << src2
471 bfi_args[0] = LLVMBuildShl(builder,
472 LLVMBuildSub(builder,
473 LLVMBuildShl(builder,
474 ctx->i32_1,
475 emit_data->args[3], ""),
476 ctx->i32_1, ""),
477 emit_data->args[2], "");
478
479 bfi_args[1] = LLVMBuildShl(builder, emit_data->args[1],
480 emit_data->args[2], "");
481
482 bfi_args[2] = emit_data->args[0];
483
484 /* Calculate:
485 * (arg0 & arg1) | (~arg0 & arg2) = arg2 ^ (arg0 & (arg1 ^ arg2)
486 * Use the right-hand side, which the LLVM backend can convert to V_BFI.
487 */
488 bfi_sm5 =
489 LLVMBuildXor(builder, bfi_args[2],
490 LLVMBuildAnd(builder, bfi_args[0],
491 LLVMBuildXor(builder, bfi_args[1], bfi_args[2],
492 ""), ""), "");
493
494 /* Since shifts of >= 32 bits are undefined in LLVM IR, the backend
495 * uses the convenient V_BFI lowering for the above, which follows SM5
496 * and disagrees with GLSL semantics when bits (src3) is 32.
497 */
498 cond = LLVMBuildICmp(builder, LLVMIntUGE, emit_data->args[3],
499 LLVMConstInt(ctx->i32, 32, 0), "");
500 emit_data->output[emit_data->chan] =
501 LLVMBuildSelect(builder, cond, emit_data->args[1], bfi_sm5, "");
502 }
503
504 static void emit_bfe(const struct lp_build_tgsi_action *action,
505 struct lp_build_tgsi_context *bld_base,
506 struct lp_build_emit_data *emit_data)
507 {
508 struct si_shader_context *ctx = si_shader_context(bld_base);
509 LLVMValueRef bfe_sm5;
510 LLVMValueRef cond;
511
512 bfe_sm5 = ac_build_bfe(&ctx->ac, emit_data->args[0],
513 emit_data->args[1], emit_data->args[2],
514 emit_data->info->opcode == TGSI_OPCODE_IBFE);
515
516 /* Correct for GLSL semantics. */
517 cond = LLVMBuildICmp(ctx->ac.builder, LLVMIntUGE, emit_data->args[2],
518 LLVMConstInt(ctx->i32, 32, 0), "");
519 emit_data->output[emit_data->chan] =
520 LLVMBuildSelect(ctx->ac.builder, cond, emit_data->args[0], bfe_sm5, "");
521 }
522
523 /* this is ffs in C */
524 static void emit_lsb(const struct lp_build_tgsi_action *action,
525 struct lp_build_tgsi_context *bld_base,
526 struct lp_build_emit_data *emit_data)
527 {
528 struct si_shader_context *ctx = si_shader_context(bld_base);
529
530 emit_data->output[emit_data->chan] = ac_find_lsb(&ctx->ac, emit_data->dst_type, emit_data->args[0]);
531 }
532
533 /* Find the last bit set. */
534 static void emit_umsb(const struct lp_build_tgsi_action *action,
535 struct lp_build_tgsi_context *bld_base,
536 struct lp_build_emit_data *emit_data)
537 {
538 struct si_shader_context *ctx = si_shader_context(bld_base);
539
540 emit_data->output[emit_data->chan] =
541 ac_build_umsb(&ctx->ac, emit_data->args[0], emit_data->dst_type);
542 }
543
544 /* Find the last bit opposite of the sign bit. */
545 static void emit_imsb(const struct lp_build_tgsi_action *action,
546 struct lp_build_tgsi_context *bld_base,
547 struct lp_build_emit_data *emit_data)
548 {
549 struct si_shader_context *ctx = si_shader_context(bld_base);
550 emit_data->output[emit_data->chan] =
551 ac_build_imsb(&ctx->ac, emit_data->args[0],
552 emit_data->dst_type);
553 }
554
555 static void emit_iabs(const struct lp_build_tgsi_action *action,
556 struct lp_build_tgsi_context *bld_base,
557 struct lp_build_emit_data *emit_data)
558 {
559 struct si_shader_context *ctx = si_shader_context(bld_base);
560
561 emit_data->output[emit_data->chan] =
562 lp_build_emit_llvm_binary(bld_base, TGSI_OPCODE_IMAX,
563 emit_data->args[0],
564 LLVMBuildNeg(ctx->ac.builder,
565 emit_data->args[0], ""));
566 }
567
568 static void emit_minmax_int(const struct lp_build_tgsi_action *action,
569 struct lp_build_tgsi_context *bld_base,
570 struct lp_build_emit_data *emit_data)
571 {
572 struct si_shader_context *ctx = si_shader_context(bld_base);
573 LLVMIntPredicate op;
574
575 switch (emit_data->info->opcode) {
576 default:
577 assert(0);
578 case TGSI_OPCODE_IMAX:
579 case TGSI_OPCODE_I64MAX:
580 op = LLVMIntSGT;
581 break;
582 case TGSI_OPCODE_IMIN:
583 case TGSI_OPCODE_I64MIN:
584 op = LLVMIntSLT;
585 break;
586 case TGSI_OPCODE_UMAX:
587 case TGSI_OPCODE_U64MAX:
588 op = LLVMIntUGT;
589 break;
590 case TGSI_OPCODE_UMIN:
591 case TGSI_OPCODE_U64MIN:
592 op = LLVMIntULT;
593 break;
594 }
595
596 emit_data->output[emit_data->chan] =
597 LLVMBuildSelect(ctx->ac.builder,
598 LLVMBuildICmp(ctx->ac.builder, op, emit_data->args[0],
599 emit_data->args[1], ""),
600 emit_data->args[0],
601 emit_data->args[1], "");
602 }
603
604 static void pk2h_fetch_args(struct lp_build_tgsi_context *bld_base,
605 struct lp_build_emit_data *emit_data)
606 {
607 emit_data->args[0] = lp_build_emit_fetch(bld_base, emit_data->inst,
608 0, TGSI_CHAN_X);
609 emit_data->args[1] = lp_build_emit_fetch(bld_base, emit_data->inst,
610 0, TGSI_CHAN_Y);
611 }
612
613 static void emit_pk2h(const struct lp_build_tgsi_action *action,
614 struct lp_build_tgsi_context *bld_base,
615 struct lp_build_emit_data *emit_data)
616 {
617 /* From the GLSL 4.50 spec:
618 * "The rounding mode cannot be set and is undefined."
619 *
620 * v_cvt_pkrtz_f16 rounds to zero, but it's fastest.
621 */
622 emit_data->output[emit_data->chan] =
623 ac_build_cvt_pkrtz_f16(&si_shader_context(bld_base)->ac,
624 emit_data->args);
625 }
626
627 static void up2h_fetch_args(struct lp_build_tgsi_context *bld_base,
628 struct lp_build_emit_data *emit_data)
629 {
630 emit_data->args[0] = lp_build_emit_fetch(bld_base, emit_data->inst,
631 0, TGSI_CHAN_X);
632 }
633
634 static void emit_up2h(const struct lp_build_tgsi_action *action,
635 struct lp_build_tgsi_context *bld_base,
636 struct lp_build_emit_data *emit_data)
637 {
638 struct si_shader_context *ctx = si_shader_context(bld_base);
639 LLVMTypeRef i16;
640 LLVMValueRef const16, input, val;
641 unsigned i;
642
643 i16 = LLVMInt16TypeInContext(ctx->ac.context);
644 const16 = LLVMConstInt(ctx->i32, 16, 0);
645 input = emit_data->args[0];
646
647 for (i = 0; i < 2; i++) {
648 val = i == 1 ? LLVMBuildLShr(ctx->ac.builder, input, const16, "") : input;
649 val = LLVMBuildTrunc(ctx->ac.builder, val, i16, "");
650 val = ac_to_float(&ctx->ac, val);
651 emit_data->output[i] = LLVMBuildFPExt(ctx->ac.builder, val, ctx->f32, "");
652 }
653 }
654
655 static void emit_fdiv(const struct lp_build_tgsi_action *action,
656 struct lp_build_tgsi_context *bld_base,
657 struct lp_build_emit_data *emit_data)
658 {
659 struct si_shader_context *ctx = si_shader_context(bld_base);
660
661 emit_data->output[emit_data->chan] =
662 ac_build_fdiv(&ctx->ac, emit_data->args[0], emit_data->args[1]);
663 }
664
665 /* 1/sqrt is translated to rsq for f32 if fp32 denormals are not enabled in
666 * the target machine. f64 needs global unsafe math flags to get rsq. */
667 static void emit_rsq(const struct lp_build_tgsi_action *action,
668 struct lp_build_tgsi_context *bld_base,
669 struct lp_build_emit_data *emit_data)
670 {
671 struct si_shader_context *ctx = si_shader_context(bld_base);
672
673 LLVMValueRef sqrt =
674 lp_build_emit_llvm_unary(bld_base, TGSI_OPCODE_SQRT,
675 emit_data->args[0]);
676
677 emit_data->output[emit_data->chan] =
678 lp_build_emit_llvm_binary(bld_base, TGSI_OPCODE_DIV,
679 ctx->ac.f32_1, sqrt);
680 }
681
682 static void dfracexp_fetch_args(struct lp_build_tgsi_context *bld_base,
683 struct lp_build_emit_data *emit_data)
684 {
685 emit_data->args[0] = lp_build_emit_fetch(bld_base, emit_data->inst, 0, TGSI_CHAN_X);
686 emit_data->arg_count = 1;
687 }
688
689 static void dfracexp_emit(const struct lp_build_tgsi_action *action,
690 struct lp_build_tgsi_context *bld_base,
691 struct lp_build_emit_data *emit_data)
692 {
693 struct si_shader_context *ctx = si_shader_context(bld_base);
694
695 emit_data->output[emit_data->chan] =
696 lp_build_intrinsic(ctx->ac.builder, "llvm.amdgcn.frexp.mant.f64",
697 ctx->ac.f64, &emit_data->args[0], 1, 0);
698 emit_data->output1[emit_data->chan] =
699 lp_build_intrinsic(ctx->ac.builder, "llvm.amdgcn.frexp.exp.i32.f64",
700 ctx->ac.i32, &emit_data->args[0], 1, 0);
701 }
702
703 void si_shader_context_init_alu(struct lp_build_tgsi_context *bld_base)
704 {
705 lp_set_default_actions(bld_base);
706
707 bld_base->op_actions[TGSI_OPCODE_AND].emit = emit_and;
708 bld_base->op_actions[TGSI_OPCODE_ARL].emit = emit_arl;
709 bld_base->op_actions[TGSI_OPCODE_BFI].emit = emit_bfi;
710 bld_base->op_actions[TGSI_OPCODE_BREV].emit = build_tgsi_intrinsic_nomem;
711 bld_base->op_actions[TGSI_OPCODE_BREV].intr_name = "llvm.bitreverse.i32";
712 bld_base->op_actions[TGSI_OPCODE_CEIL].emit = build_tgsi_intrinsic_nomem;
713 bld_base->op_actions[TGSI_OPCODE_CEIL].intr_name = "llvm.ceil.f32";
714 bld_base->op_actions[TGSI_OPCODE_CMP].emit = emit_cmp;
715 bld_base->op_actions[TGSI_OPCODE_COS].emit = build_tgsi_intrinsic_nomem;
716 bld_base->op_actions[TGSI_OPCODE_COS].intr_name = "llvm.cos.f32";
717 bld_base->op_actions[TGSI_OPCODE_DABS].emit = build_tgsi_intrinsic_nomem;
718 bld_base->op_actions[TGSI_OPCODE_DABS].intr_name = "llvm.fabs.f64";
719 bld_base->op_actions[TGSI_OPCODE_DCEIL].emit = build_tgsi_intrinsic_nomem;
720 bld_base->op_actions[TGSI_OPCODE_DCEIL].intr_name = "llvm.ceil.f64";
721 bld_base->op_actions[TGSI_OPCODE_DFLR].emit = build_tgsi_intrinsic_nomem;
722 bld_base->op_actions[TGSI_OPCODE_DFLR].intr_name = "llvm.floor.f64";
723 bld_base->op_actions[TGSI_OPCODE_DFMA].emit = build_tgsi_intrinsic_nomem;
724 bld_base->op_actions[TGSI_OPCODE_DFMA].intr_name = "llvm.fma.f64";
725 bld_base->op_actions[TGSI_OPCODE_DFRAC].emit = emit_frac;
726 bld_base->op_actions[TGSI_OPCODE_DIV].emit = emit_fdiv;
727 bld_base->op_actions[TGSI_OPCODE_DNEG].emit = emit_dneg;
728 bld_base->op_actions[TGSI_OPCODE_DROUND].emit = build_tgsi_intrinsic_nomem;
729 bld_base->op_actions[TGSI_OPCODE_DROUND].intr_name = "llvm.rint.f64";
730 bld_base->op_actions[TGSI_OPCODE_DSEQ].emit = emit_dcmp;
731 bld_base->op_actions[TGSI_OPCODE_DSGE].emit = emit_dcmp;
732 bld_base->op_actions[TGSI_OPCODE_DSLT].emit = emit_dcmp;
733 bld_base->op_actions[TGSI_OPCODE_DSNE].emit = emit_dcmp;
734 bld_base->op_actions[TGSI_OPCODE_DSSG].emit = emit_ssg;
735 bld_base->op_actions[TGSI_OPCODE_DRSQ].emit = build_tgsi_intrinsic_nomem;
736 bld_base->op_actions[TGSI_OPCODE_DRSQ].intr_name = "llvm.amdgcn.rsq.f64";
737 bld_base->op_actions[TGSI_OPCODE_DSQRT].emit = build_tgsi_intrinsic_nomem;
738 bld_base->op_actions[TGSI_OPCODE_DSQRT].intr_name = "llvm.sqrt.f64";
739 bld_base->op_actions[TGSI_OPCODE_DTRUNC].emit = build_tgsi_intrinsic_nomem;
740 bld_base->op_actions[TGSI_OPCODE_DTRUNC].intr_name = "llvm.trunc.f64";
741 bld_base->op_actions[TGSI_OPCODE_DFRACEXP].fetch_args = dfracexp_fetch_args;
742 bld_base->op_actions[TGSI_OPCODE_DFRACEXP].emit = dfracexp_emit;
743 bld_base->op_actions[TGSI_OPCODE_DLDEXP].emit = build_tgsi_intrinsic_nomem;
744 bld_base->op_actions[TGSI_OPCODE_DLDEXP].intr_name = "llvm.amdgcn.ldexp.f64";
745 bld_base->op_actions[TGSI_OPCODE_EX2].emit = build_tgsi_intrinsic_nomem;
746 bld_base->op_actions[TGSI_OPCODE_EX2].intr_name = "llvm.exp2.f32";
747 bld_base->op_actions[TGSI_OPCODE_FLR].emit = build_tgsi_intrinsic_nomem;
748 bld_base->op_actions[TGSI_OPCODE_FLR].intr_name = "llvm.floor.f32";
749 bld_base->op_actions[TGSI_OPCODE_FMA].emit =
750 bld_base->op_actions[TGSI_OPCODE_MAD].emit;
751 bld_base->op_actions[TGSI_OPCODE_FRC].emit = emit_frac;
752 bld_base->op_actions[TGSI_OPCODE_F2I].emit = emit_f2i;
753 bld_base->op_actions[TGSI_OPCODE_F2U].emit = emit_f2u;
754 bld_base->op_actions[TGSI_OPCODE_FSEQ].emit = emit_fcmp;
755 bld_base->op_actions[TGSI_OPCODE_FSGE].emit = emit_fcmp;
756 bld_base->op_actions[TGSI_OPCODE_FSLT].emit = emit_fcmp;
757 bld_base->op_actions[TGSI_OPCODE_FSNE].emit = emit_fcmp;
758 bld_base->op_actions[TGSI_OPCODE_IABS].emit = emit_iabs;
759 bld_base->op_actions[TGSI_OPCODE_IBFE].emit = emit_bfe;
760 bld_base->op_actions[TGSI_OPCODE_IDIV].emit = emit_idiv;
761 bld_base->op_actions[TGSI_OPCODE_IMAX].emit = emit_minmax_int;
762 bld_base->op_actions[TGSI_OPCODE_IMIN].emit = emit_minmax_int;
763 bld_base->op_actions[TGSI_OPCODE_IMSB].emit = emit_imsb;
764 bld_base->op_actions[TGSI_OPCODE_INEG].emit = emit_ineg;
765 bld_base->op_actions[TGSI_OPCODE_ISHR].emit = emit_ishr;
766 bld_base->op_actions[TGSI_OPCODE_ISGE].emit = emit_icmp;
767 bld_base->op_actions[TGSI_OPCODE_ISLT].emit = emit_icmp;
768 bld_base->op_actions[TGSI_OPCODE_ISSG].emit = emit_ssg;
769 bld_base->op_actions[TGSI_OPCODE_I2F].emit = emit_i2f;
770 bld_base->op_actions[TGSI_OPCODE_KILL_IF].fetch_args = kill_if_fetch_args;
771 bld_base->op_actions[TGSI_OPCODE_KILL_IF].emit = kil_emit;
772 bld_base->op_actions[TGSI_OPCODE_KILL].emit = kil_emit;
773 bld_base->op_actions[TGSI_OPCODE_LDEXP].emit = build_tgsi_intrinsic_nomem;
774 bld_base->op_actions[TGSI_OPCODE_LDEXP].intr_name = "llvm.amdgcn.ldexp.f32";
775 bld_base->op_actions[TGSI_OPCODE_LSB].emit = emit_lsb;
776 bld_base->op_actions[TGSI_OPCODE_LG2].emit = build_tgsi_intrinsic_nomem;
777 bld_base->op_actions[TGSI_OPCODE_LG2].intr_name = "llvm.log2.f32";
778 bld_base->op_actions[TGSI_OPCODE_MAX].emit = build_tgsi_intrinsic_nomem;
779 bld_base->op_actions[TGSI_OPCODE_MAX].intr_name = "llvm.maxnum.f32";
780 bld_base->op_actions[TGSI_OPCODE_MIN].emit = build_tgsi_intrinsic_nomem;
781 bld_base->op_actions[TGSI_OPCODE_MIN].intr_name = "llvm.minnum.f32";
782 bld_base->op_actions[TGSI_OPCODE_MOD].emit = emit_mod;
783 bld_base->op_actions[TGSI_OPCODE_UMSB].emit = emit_umsb;
784 bld_base->op_actions[TGSI_OPCODE_NOT].emit = emit_not;
785 bld_base->op_actions[TGSI_OPCODE_OR].emit = emit_or;
786 bld_base->op_actions[TGSI_OPCODE_PK2H].fetch_args = pk2h_fetch_args;
787 bld_base->op_actions[TGSI_OPCODE_PK2H].emit = emit_pk2h;
788 bld_base->op_actions[TGSI_OPCODE_POPC].emit = build_tgsi_intrinsic_nomem;
789 bld_base->op_actions[TGSI_OPCODE_POPC].intr_name = "llvm.ctpop.i32";
790 bld_base->op_actions[TGSI_OPCODE_POW].emit = build_tgsi_intrinsic_nomem;
791 bld_base->op_actions[TGSI_OPCODE_POW].intr_name = "llvm.pow.f32";
792 bld_base->op_actions[TGSI_OPCODE_ROUND].emit = build_tgsi_intrinsic_nomem;
793 bld_base->op_actions[TGSI_OPCODE_ROUND].intr_name = "llvm.rint.f32";
794 bld_base->op_actions[TGSI_OPCODE_RSQ].emit = emit_rsq;
795 bld_base->op_actions[TGSI_OPCODE_SGE].emit = emit_set_cond;
796 bld_base->op_actions[TGSI_OPCODE_SEQ].emit = emit_set_cond;
797 bld_base->op_actions[TGSI_OPCODE_SHL].emit = emit_shl;
798 bld_base->op_actions[TGSI_OPCODE_SLE].emit = emit_set_cond;
799 bld_base->op_actions[TGSI_OPCODE_SLT].emit = emit_set_cond;
800 bld_base->op_actions[TGSI_OPCODE_SNE].emit = emit_set_cond;
801 bld_base->op_actions[TGSI_OPCODE_SGT].emit = emit_set_cond;
802 bld_base->op_actions[TGSI_OPCODE_SIN].emit = build_tgsi_intrinsic_nomem;
803 bld_base->op_actions[TGSI_OPCODE_SIN].intr_name = "llvm.sin.f32";
804 bld_base->op_actions[TGSI_OPCODE_SQRT].emit = build_tgsi_intrinsic_nomem;
805 bld_base->op_actions[TGSI_OPCODE_SQRT].intr_name = "llvm.sqrt.f32";
806 bld_base->op_actions[TGSI_OPCODE_SSG].emit = emit_ssg;
807 bld_base->op_actions[TGSI_OPCODE_TRUNC].emit = build_tgsi_intrinsic_nomem;
808 bld_base->op_actions[TGSI_OPCODE_TRUNC].intr_name = "llvm.trunc.f32";
809 bld_base->op_actions[TGSI_OPCODE_UADD].emit = emit_uadd;
810 bld_base->op_actions[TGSI_OPCODE_UBFE].emit = emit_bfe;
811 bld_base->op_actions[TGSI_OPCODE_UDIV].emit = emit_udiv;
812 bld_base->op_actions[TGSI_OPCODE_UMAX].emit = emit_minmax_int;
813 bld_base->op_actions[TGSI_OPCODE_UMIN].emit = emit_minmax_int;
814 bld_base->op_actions[TGSI_OPCODE_UMOD].emit = emit_umod;
815 bld_base->op_actions[TGSI_OPCODE_USEQ].emit = emit_icmp;
816 bld_base->op_actions[TGSI_OPCODE_USGE].emit = emit_icmp;
817 bld_base->op_actions[TGSI_OPCODE_USHR].emit = emit_ushr;
818 bld_base->op_actions[TGSI_OPCODE_USLT].emit = emit_icmp;
819 bld_base->op_actions[TGSI_OPCODE_USNE].emit = emit_icmp;
820 bld_base->op_actions[TGSI_OPCODE_U2F].emit = emit_u2f;
821 bld_base->op_actions[TGSI_OPCODE_XOR].emit = emit_xor;
822 bld_base->op_actions[TGSI_OPCODE_UCMP].emit = emit_ucmp;
823 bld_base->op_actions[TGSI_OPCODE_UP2H].fetch_args = up2h_fetch_args;
824 bld_base->op_actions[TGSI_OPCODE_UP2H].emit = emit_up2h;
825
826 bld_base->op_actions[TGSI_OPCODE_I64MAX].emit = emit_minmax_int;
827 bld_base->op_actions[TGSI_OPCODE_I64MIN].emit = emit_minmax_int;
828 bld_base->op_actions[TGSI_OPCODE_U64MAX].emit = emit_minmax_int;
829 bld_base->op_actions[TGSI_OPCODE_U64MIN].emit = emit_minmax_int;
830 bld_base->op_actions[TGSI_OPCODE_I64ABS].emit = emit_iabs;
831 bld_base->op_actions[TGSI_OPCODE_I64SSG].emit = emit_ssg;
832 bld_base->op_actions[TGSI_OPCODE_I64NEG].emit = emit_ineg;
833
834 bld_base->op_actions[TGSI_OPCODE_U64SEQ].emit = emit_icmp;
835 bld_base->op_actions[TGSI_OPCODE_U64SNE].emit = emit_icmp;
836 bld_base->op_actions[TGSI_OPCODE_U64SGE].emit = emit_icmp;
837 bld_base->op_actions[TGSI_OPCODE_U64SLT].emit = emit_icmp;
838 bld_base->op_actions[TGSI_OPCODE_I64SGE].emit = emit_icmp;
839 bld_base->op_actions[TGSI_OPCODE_I64SLT].emit = emit_icmp;
840
841 bld_base->op_actions[TGSI_OPCODE_U64ADD].emit = emit_uadd;
842 bld_base->op_actions[TGSI_OPCODE_U64SHL].emit = emit_shl;
843 bld_base->op_actions[TGSI_OPCODE_U64SHR].emit = emit_ushr;
844 bld_base->op_actions[TGSI_OPCODE_I64SHR].emit = emit_ishr;
845
846 bld_base->op_actions[TGSI_OPCODE_U64MOD].emit = emit_umod;
847 bld_base->op_actions[TGSI_OPCODE_I64MOD].emit = emit_mod;
848 bld_base->op_actions[TGSI_OPCODE_U64DIV].emit = emit_udiv;
849 bld_base->op_actions[TGSI_OPCODE_I64DIV].emit = emit_idiv;
850 }