1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * Helper functions for logical operations.
32 * @author Jose Fonseca <jfonseca@vmware.com>
36 #include "pipe/p_defines.h"
37 #include "lp_bld_type.h"
38 #include "lp_bld_const.h"
39 #include "lp_bld_intr.h"
40 #include "lp_bld_logic.h"
44 lp_build_cmp(struct lp_build_context
*bld
,
49 const union lp_type type
= bld
->type
;
50 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
51 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
52 LLVMValueRef zeros
= LLVMConstNull(int_vec_type
);
53 LLVMValueRef ones
= LLVMConstAllOnes(int_vec_type
);
56 if(func
== PIPE_FUNC_NEVER
)
58 if(func
== PIPE_FUNC_ALWAYS
)
61 /* TODO: optimize the constant case */
63 /* XXX: It is not clear if we should use the ordered or unordered operators */
65 #if defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)
66 if(type
.width
* type
.length
== 128) {
78 case PIPE_FUNC_NOTEQUAL
:
84 case PIPE_FUNC_LEQUAL
:
87 case PIPE_FUNC_GREATER
:
91 case PIPE_FUNC_GEQUAL
:
109 args
[2] = LLVMConstInt(LLVMInt8Type(), cc
, 0);
110 res
= lp_build_intrinsic(bld
->builder
,
111 "llvm.x86.sse.cmp.ps",
114 res
= LLVMBuildBitCast(bld
->builder
, res
, int_vec_type
, "");
118 static const struct {
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 */
135 LLVMValueRef args
[2];
138 switch (type
.width
) {
140 pcmpeq
= "llvm.x86.sse2.pcmpeq.b";
141 pcmpgt
= "llvm.x86.sse2.pcmpgt.b";
144 pcmpeq
= "llvm.x86.sse2.pcmpeq.w";
145 pcmpgt
= "llvm.x86.sse2.pcmpgt.w";
148 pcmpeq
= "llvm.x86.sse2.pcmpeq.d";
149 pcmpgt
= "llvm.x86.sse2.pcmpgt.d";
156 /* There are no signed byte and unsigned word/dword comparison
157 * instructions. So flip the sign bit so that the results match.
160 ((type
.width
== 8 && type
.sign
) ||
161 (type
.width
!= 8 && !type
.sign
))) {
162 LLVMValueRef msb
= lp_build_int_const_uni(type
, (unsigned long long)1 << (type
.width
- 1));
163 a
= LLVMBuildXor(bld
->builder
, a
, msb
, "");
164 b
= LLVMBuildXor(bld
->builder
, b
, msb
, "");
167 if(table
[func
].swap
) {
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);
181 res
= LLVMConstNull(vec_type
);
184 res
= LLVMBuildNot(bld
->builder
, res
, "");
192 LLVMRealPredicate op
;
194 case PIPE_FUNC_NEVER
:
195 op
= LLVMRealPredicateFalse
;
197 case PIPE_FUNC_ALWAYS
:
198 op
= LLVMRealPredicateTrue
;
200 case PIPE_FUNC_EQUAL
:
203 case PIPE_FUNC_NOTEQUAL
:
209 case PIPE_FUNC_LEQUAL
:
212 case PIPE_FUNC_GREATER
:
215 case PIPE_FUNC_GEQUAL
:
222 cond
= LLVMBuildFCmp(bld
->builder
, op
, a
, b
, "");
227 case PIPE_FUNC_EQUAL
:
230 case PIPE_FUNC_NOTEQUAL
:
234 op
= type
.sign
? LLVMIntSLT
: LLVMIntULT
;
236 case PIPE_FUNC_LEQUAL
:
237 op
= type
.sign
? LLVMIntSLE
: LLVMIntULE
;
239 case PIPE_FUNC_GREATER
:
240 op
= type
.sign
? LLVMIntSGT
: LLVMIntUGT
;
242 case PIPE_FUNC_GEQUAL
:
243 op
= type
.sign
? LLVMIntSGE
: LLVMIntUGE
;
249 cond
= LLVMBuildICmp(bld
->builder
, op
, a
, b
, "");
252 return LLVMBuildSelect(bld
->builder
, cond
, ones
, zeros
, "");
257 lp_build_select(struct lp_build_context
*bld
,
262 union lp_type type
= bld
->type
;
269 LLVMTypeRef int_vec_type
= lp_build_int_vec_type(type
);
270 a
= LLVMBuildBitCast(bld
->builder
, a
, int_vec_type
, "");
271 b
= LLVMBuildBitCast(bld
->builder
, b
, int_vec_type
, "");
274 /* TODO: On SSE4 we could do this with a single instruction -- PBLENDVB */
276 a
= LLVMBuildAnd(bld
->builder
, a
, mask
, "");
278 /* This often gets translated to PANDN, but sometimes the NOT is
279 * pre-computed and stored in another constant. The best strategy depends
280 * on available registers, so it is not a big deal -- hopefully LLVM does
281 * the right decision attending the rest of the program.
283 b
= LLVMBuildAnd(bld
->builder
, b
, LLVMBuildNot(bld
->builder
, mask
, ""), "");
285 res
= LLVMBuildOr(bld
->builder
, a
, b
, "");
288 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
289 res
= LLVMBuildBitCast(bld
->builder
, res
, vec_type
, "");
297 lp_build_select_aos(struct lp_build_context
*bld
,
302 const union lp_type type
= bld
->type
;
303 const unsigned n
= type
.length
;
308 if(cond
[0] && cond
[1] && cond
[2] && cond
[3])
310 if(!cond
[0] && !cond
[1] && !cond
[2] && !cond
[3])
312 if(a
== bld
->undef
|| b
== bld
->undef
)
316 * There are three major ways of accomplishing this:
319 * - or with a bit mask.
321 * Select isn't supported for vector types yet.
322 * The flip between these is empirical and might need to be.
328 LLVMTypeRef elem_type
= LLVMInt32Type();
329 LLVMValueRef shuffles
[LP_MAX_VECTOR_LENGTH
];
331 for(j
= 0; j
< n
; j
+= 4)
332 for(i
= 0; i
< 4; ++i
)
333 shuffles
[j
+ i
] = LLVMConstInt(elem_type
, (cond
[i
] ? 0 : n
) + j
+ i
, 0);
335 return LLVMBuildShuffleVector(bld
->builder
, a
, b
, LLVMConstVector(shuffles
, n
), "");
339 /* FIXME: Unfortunately select of vectors do not work */
341 LLVMTypeRef elem_type
= LLVMInt1Type();
342 LLVMValueRef cond
[LP_MAX_VECTOR_LENGTH
];
344 for(j
= 0; j
< n
; j
+= 4)
345 for(i
= 0; i
< 4; ++i
)
346 cond
[j
+ i
] = LLVMConstInt(elem_type
, cond
[i
] ? 1 : 0, 0);
348 return LLVMBuildSelect(bld
->builder
, LLVMConstVector(cond
, n
), a
, b
, "");
352 LLVMValueRef mask
= lp_build_const_mask_aos(type
, cond
);
353 return lp_build_select(bld
, mask
, a
, b
);