llvmpipe: Fallback to element-wise comparisons when no comparison intrinsic is available.
[mesa.git] / src / gallium / drivers / llvmpipe / lp_bld_logic.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /**
29 * @file
30 * Helper functions for logical operations.
31 *
32 * @author Jose Fonseca <jfonseca@vmware.com>
33 */
34
35
36 #include "lp_bld_type.h"
37 #include "lp_bld_const.h"
38 #include "lp_bld_intr.h"
39 #include "lp_bld_logic.h"
40
41
42 LLVMValueRef
43 lp_build_cmp(struct lp_build_context *bld,
44 unsigned func,
45 LLVMValueRef a,
46 LLVMValueRef b)
47 {
48 const union lp_type type = bld->type;
49 LLVMTypeRef vec_type = lp_build_vec_type(type);
50 LLVMTypeRef int_vec_type = lp_build_int_vec_type(type);
51 LLVMValueRef zeros = LLVMConstNull(int_vec_type);
52 LLVMValueRef ones = LLVMConstAllOnes(int_vec_type);
53 LLVMValueRef cond;
54 LLVMValueRef res;
55 unsigned i;
56
57 if(func == PIPE_FUNC_NEVER)
58 return zeros;
59 if(func == PIPE_FUNC_ALWAYS)
60 return ones;
61
62 /* TODO: optimize the constant case */
63
64 /* XXX: It is not clear if we should use the ordered or unordered operators */
65
66 #if defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)
67 if(type.width * type.length == 128) {
68 if(type.floating) {
69 LLVMValueRef args[3];
70 unsigned cc;
71 boolean swap;
72
73 swap = FALSE;
74 switch(func) {
75 case PIPE_FUNC_EQUAL:
76 cc = 0;
77 break;
78 case PIPE_FUNC_NOTEQUAL:
79 cc = 4;
80 break;
81 case PIPE_FUNC_LESS:
82 cc = 1;
83 break;
84 case PIPE_FUNC_LEQUAL:
85 cc = 2;
86 break;
87 case PIPE_FUNC_GREATER:
88 cc = 1;
89 swap = TRUE;
90 break;
91 case PIPE_FUNC_GEQUAL:
92 cc = 2;
93 swap = TRUE;
94 break;
95 default:
96 assert(0);
97 return bld->undef;
98 }
99
100 if(swap) {
101 args[0] = b;
102 args[1] = a;
103 }
104 else {
105 args[0] = a;
106 args[1] = b;
107 }
108
109 args[2] = LLVMConstInt(LLVMInt8Type(), cc, 0);
110 res = lp_build_intrinsic(bld->builder,
111 "llvm.x86.sse.cmp.ps",
112 vec_type,
113 args, 3);
114 res = LLVMBuildBitCast(bld->builder, res, int_vec_type, "");
115 return res;
116 }
117 else {
118 static const struct {
119 unsigned swap:1;
120 unsigned eq:1;
121 unsigned gt:1;
122 unsigned not:1;
123 } table[] = {
124 {0, 0, 0, 1}, /* PIPE_FUNC_NEVER */
125 {1, 0, 1, 0}, /* PIPE_FUNC_LESS */
126 {0, 1, 0, 0}, /* PIPE_FUNC_EQUAL */
127 {0, 0, 1, 1}, /* PIPE_FUNC_LEQUAL */
128 {0, 0, 1, 0}, /* PIPE_FUNC_GREATER */
129 {0, 1, 0, 1}, /* PIPE_FUNC_NOTEQUAL */
130 {1, 0, 1, 1}, /* PIPE_FUNC_GEQUAL */
131 {0, 0, 0, 0} /* PIPE_FUNC_ALWAYS */
132 };
133 const char *pcmpeq;
134 const char *pcmpgt;
135 LLVMValueRef args[2];
136 LLVMValueRef res;
137
138 switch (type.width) {
139 case 8:
140 pcmpeq = "llvm.x86.sse2.pcmpeq.b";
141 pcmpgt = "llvm.x86.sse2.pcmpgt.b";
142 break;
143 case 16:
144 pcmpeq = "llvm.x86.sse2.pcmpeq.w";
145 pcmpgt = "llvm.x86.sse2.pcmpgt.w";
146 break;
147 case 32:
148 pcmpeq = "llvm.x86.sse2.pcmpeq.d";
149 pcmpgt = "llvm.x86.sse2.pcmpgt.d";
150 break;
151 default:
152 assert(0);
153 return bld->undef;
154 }
155
156 /* There are no signed byte and unsigned word/dword comparison
157 * instructions. So flip the sign bit so that the results match.
158 */
159 if(table[func].gt &&
160 ((type.width == 8 && type.sign) ||
161 (type.width != 8 && !type.sign))) {
162 LLVMValueRef msb = lp_build_int_const_scalar(type, (unsigned long long)1 << (type.width - 1));
163 a = LLVMBuildXor(bld->builder, a, msb, "");
164 b = LLVMBuildXor(bld->builder, b, msb, "");
165 }
166
167 if(table[func].swap) {
168 args[0] = b;
169 args[1] = a;
170 }
171 else {
172 args[0] = a;
173 args[1] = b;
174 }
175
176 if(table[func].eq)
177 res = lp_build_intrinsic(bld->builder, pcmpeq, vec_type, args, 2);
178 else if (table[func].gt)
179 res = lp_build_intrinsic(bld->builder, pcmpgt, vec_type, args, 2);
180 else
181 res = LLVMConstNull(vec_type);
182
183 if(table[func].not)
184 res = LLVMBuildNot(bld->builder, res, "");
185
186 return res;
187 }
188 }
189 #endif
190
191 if(type.floating) {
192 LLVMRealPredicate op;
193 switch(func) {
194 case PIPE_FUNC_NEVER:
195 op = LLVMRealPredicateFalse;
196 break;
197 case PIPE_FUNC_ALWAYS:
198 op = LLVMRealPredicateTrue;
199 break;
200 case PIPE_FUNC_EQUAL:
201 op = LLVMRealUEQ;
202 break;
203 case PIPE_FUNC_NOTEQUAL:
204 op = LLVMRealUNE;
205 break;
206 case PIPE_FUNC_LESS:
207 op = LLVMRealULT;
208 break;
209 case PIPE_FUNC_LEQUAL:
210 op = LLVMRealULE;
211 break;
212 case PIPE_FUNC_GREATER:
213 op = LLVMRealUGT;
214 break;
215 case PIPE_FUNC_GEQUAL:
216 op = LLVMRealUGE;
217 break;
218 default:
219 assert(0);
220 return bld->undef;
221 }
222
223 #if 0
224 /* XXX: Although valid IR, no LLVM target currently support this */
225 cond = LLVMBuildFCmp(bld->builder, op, a, b, "");
226 res = LLVMBuildSelect(bld->builder, cond, ones, zeros, "");
227 #else
228 debug_printf("%s: warning: using slow element-wise vector comparison\n",
229 __FUNCTION__);
230 res = LLVMGetUndef(int_vec_type);
231 for(i = 0; i < type.length; ++i) {
232 LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), i, 0);
233 cond = LLVMBuildFCmp(bld->builder, op,
234 LLVMBuildExtractElement(bld->builder, a, index, ""),
235 LLVMBuildExtractElement(bld->builder, b, index, ""),
236 "");
237 cond = LLVMBuildSelect(bld->builder, cond,
238 LLVMConstExtractElement(ones, index),
239 LLVMConstExtractElement(zeros, index),
240 "");
241 res = LLVMBuildInsertElement(bld->builder, res, cond, index, "");
242 }
243 #endif
244 }
245 else {
246 LLVMIntPredicate op;
247 switch(func) {
248 case PIPE_FUNC_EQUAL:
249 op = LLVMIntEQ;
250 break;
251 case PIPE_FUNC_NOTEQUAL:
252 op = LLVMIntNE;
253 break;
254 case PIPE_FUNC_LESS:
255 op = type.sign ? LLVMIntSLT : LLVMIntULT;
256 break;
257 case PIPE_FUNC_LEQUAL:
258 op = type.sign ? LLVMIntSLE : LLVMIntULE;
259 break;
260 case PIPE_FUNC_GREATER:
261 op = type.sign ? LLVMIntSGT : LLVMIntUGT;
262 break;
263 case PIPE_FUNC_GEQUAL:
264 op = type.sign ? LLVMIntSGE : LLVMIntUGE;
265 break;
266 default:
267 assert(0);
268 return bld->undef;
269 }
270
271 #if 0
272 /* XXX: Although valid IR, no LLVM target currently support this */
273 cond = LLVMBuildICmp(bld->builder, op, a, b, "");
274 res = LLVMBuildSelect(bld->builder, cond, ones, zeros, "");
275 #else
276 debug_printf("%s: warning: using slow element-wise vector comparison\n",
277 __FUNCTION__);
278 res = LLVMGetUndef(int_vec_type);
279 for(i = 0; i < type.length; ++i) {
280 LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), i, 0);
281 cond = LLVMBuildICmp(bld->builder, op,
282 LLVMBuildExtractElement(bld->builder, a, index, ""),
283 LLVMBuildExtractElement(bld->builder, b, index, ""),
284 "");
285 cond = LLVMBuildSelect(bld->builder, cond,
286 LLVMConstExtractElement(ones, index),
287 LLVMConstExtractElement(zeros, index),
288 "");
289 res = LLVMBuildInsertElement(bld->builder, res, cond, index, "");
290 }
291 #endif
292 }
293
294 return res;
295 }
296
297
298 LLVMValueRef
299 lp_build_select(struct lp_build_context *bld,
300 LLVMValueRef mask,
301 LLVMValueRef a,
302 LLVMValueRef b)
303 {
304 union lp_type type = bld->type;
305 LLVMValueRef res;
306
307 if(a == b)
308 return a;
309
310 if(type.floating) {
311 LLVMTypeRef int_vec_type = lp_build_int_vec_type(type);
312 a = LLVMBuildBitCast(bld->builder, a, int_vec_type, "");
313 b = LLVMBuildBitCast(bld->builder, b, int_vec_type, "");
314 }
315
316 /* TODO: On SSE4 we could do this with a single instruction -- PBLENDVB */
317
318 a = LLVMBuildAnd(bld->builder, a, mask, "");
319
320 /* This often gets translated to PANDN, but sometimes the NOT is
321 * pre-computed and stored in another constant. The best strategy depends
322 * on available registers, so it is not a big deal -- hopefully LLVM does
323 * the right decision attending the rest of the program.
324 */
325 b = LLVMBuildAnd(bld->builder, b, LLVMBuildNot(bld->builder, mask, ""), "");
326
327 res = LLVMBuildOr(bld->builder, a, b, "");
328
329 if(type.floating) {
330 LLVMTypeRef vec_type = lp_build_vec_type(type);
331 res = LLVMBuildBitCast(bld->builder, res, vec_type, "");
332 }
333
334 return res;
335 }
336
337
338 LLVMValueRef
339 lp_build_select_aos(struct lp_build_context *bld,
340 LLVMValueRef a,
341 LLVMValueRef b,
342 boolean cond[4])
343 {
344 const union lp_type type = bld->type;
345 const unsigned n = type.length;
346 unsigned i, j;
347
348 if(a == b)
349 return a;
350 if(cond[0] && cond[1] && cond[2] && cond[3])
351 return a;
352 if(!cond[0] && !cond[1] && !cond[2] && !cond[3])
353 return b;
354 if(a == bld->undef || b == bld->undef)
355 return bld->undef;
356
357 /*
358 * There are three major ways of accomplishing this:
359 * - with a shuffle,
360 * - with a select,
361 * - or with a bit mask.
362 *
363 * Select isn't supported for vector types yet.
364 * The flip between these is empirical and might need to be.
365 */
366 if (n <= 4) {
367 /*
368 * Shuffle.
369 */
370 LLVMTypeRef elem_type = LLVMInt32Type();
371 LLVMValueRef shuffles[LP_MAX_VECTOR_LENGTH];
372
373 for(j = 0; j < n; j += 4)
374 for(i = 0; i < 4; ++i)
375 shuffles[j + i] = LLVMConstInt(elem_type, (cond[i] ? 0 : n) + j + i, 0);
376
377 return LLVMBuildShuffleVector(bld->builder, a, b, LLVMConstVector(shuffles, n), "");
378 }
379 #if 0
380 else if(0) {
381 /* FIXME: Unfortunately select of vectors do not work */
382 /* Use a select */
383 LLVMTypeRef elem_type = LLVMInt1Type();
384 LLVMValueRef cond[LP_MAX_VECTOR_LENGTH];
385
386 for(j = 0; j < n; j += 4)
387 for(i = 0; i < 4; ++i)
388 cond[j + i] = LLVMConstInt(elem_type, cond[i] ? 1 : 0, 0);
389
390 return LLVMBuildSelect(bld->builder, LLVMConstVector(cond, n), a, b, "");
391 }
392 #endif
393 else {
394 LLVMValueRef mask = lp_build_const_mask_aos(type, cond);
395 return lp_build_select(bld, mask, a, b);
396 }
397 }