llvmpipe: Comparisons translation.
[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 #include "pipe/p_defines.h"
30 #include "lp_bld_type.h"
31 #include "lp_bld_intr.h"
32 #include "lp_bld_logic.h"
33
34
35 LLVMValueRef
36 lp_build_cmp(struct lp_build_context *bld,
37 unsigned func,
38 LLVMValueRef a,
39 LLVMValueRef b)
40 {
41 const union lp_type type = bld->type;
42 LLVMTypeRef vec_type = lp_build_vec_type(type);
43 LLVMTypeRef int_vec_type = lp_build_int_vec_type(type);
44 LLVMValueRef zeros = LLVMConstNull(int_vec_type);
45 LLVMValueRef ones = LLVMConstAllOnes(int_vec_type);
46 LLVMValueRef cond;
47
48 if(func == PIPE_FUNC_NEVER)
49 return zeros;
50 if(func == PIPE_FUNC_ALWAYS)
51 return ones;
52
53 /* TODO: optimize the constant case */
54
55 /* XXX: It is not clear if we should use the ordered or unordered operators */
56
57 #if defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)
58 if(type.width * type.length == 128) {
59 if(type.floating) {
60 LLVMValueRef args[3];
61 unsigned cc;
62 boolean swap;
63 LLVMValueRef res;
64
65 swap = FALSE;
66 switch(func) {
67 case PIPE_FUNC_EQUAL:
68 cc = 0;
69 break;
70 case PIPE_FUNC_NOTEQUAL:
71 cc = 4;
72 break;
73 case PIPE_FUNC_LESS:
74 cc = 1;
75 break;
76 case PIPE_FUNC_LEQUAL:
77 cc = 2;
78 break;
79 case PIPE_FUNC_GREATER:
80 cc = 1;
81 swap = TRUE;
82 break;
83 case PIPE_FUNC_GEQUAL:
84 cc = 2;
85 swap = TRUE;
86 break;
87 default:
88 assert(0);
89 return bld->undef;
90 }
91
92 if(swap) {
93 args[0] = b;
94 args[1] = a;
95 }
96 else {
97 args[0] = a;
98 args[1] = b;
99 }
100
101 args[2] = LLVMConstInt(LLVMInt8Type(), cc, 0);
102 res = lp_build_intrinsic(bld->builder,
103 "llvm.x86.sse.cmp.ps",
104 vec_type,
105 args, 3);
106 res = LLVMBuildBitCast(bld->builder, res, int_vec_type, "");
107 return res;
108 }
109 }
110 #endif
111
112 if(type.floating) {
113 LLVMRealPredicate op;
114 switch(func) {
115 case PIPE_FUNC_NEVER:
116 op = LLVMRealPredicateFalse;
117 break;
118 case PIPE_FUNC_ALWAYS:
119 op = LLVMRealPredicateTrue;
120 break;
121 case PIPE_FUNC_EQUAL:
122 op = LLVMRealUEQ;
123 break;
124 case PIPE_FUNC_NOTEQUAL:
125 op = LLVMRealUNE;
126 break;
127 case PIPE_FUNC_LESS:
128 op = LLVMRealULT;
129 break;
130 case PIPE_FUNC_LEQUAL:
131 op = LLVMRealULE;
132 break;
133 case PIPE_FUNC_GREATER:
134 op = LLVMRealUGT;
135 break;
136 case PIPE_FUNC_GEQUAL:
137 op = LLVMRealUGE;
138 break;
139 default:
140 assert(0);
141 return bld->undef;
142 }
143 cond = LLVMBuildFCmp(bld->builder, op, a, b, "");
144 }
145 else {
146 LLVMIntPredicate op;
147 switch(func) {
148 case PIPE_FUNC_EQUAL:
149 op = LLVMIntEQ;
150 break;
151 case PIPE_FUNC_NOTEQUAL:
152 op = LLVMIntNE;
153 break;
154 case PIPE_FUNC_LESS:
155 op = type.sign ? LLVMIntSLT : LLVMIntULT;
156 break;
157 case PIPE_FUNC_LEQUAL:
158 op = type.sign ? LLVMIntSLE : LLVMIntULE;
159 break;
160 case PIPE_FUNC_GREATER:
161 op = type.sign ? LLVMIntSGT : LLVMIntUGT;
162 break;
163 case PIPE_FUNC_GEQUAL:
164 op = type.sign ? LLVMIntSGE : LLVMIntUGE;
165 break;
166 default:
167 assert(0);
168 return bld->undef;
169 }
170 cond = LLVMBuildICmp(bld->builder, op, a, b, "");
171 }
172
173 return LLVMBuildSelect(bld->builder, cond, ones, zeros, "");
174 }