gallivm: Use a more compact approach for lp_build_broadcast_scalar().
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_swizzle.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 swizzling/shuffling.
31 *
32 * @author Jose Fonseca <jfonseca@vmware.com>
33 */
34
35
36 #include "util/u_debug.h"
37
38 #include "lp_bld_type.h"
39 #include "lp_bld_const.h"
40 #include "lp_bld_logic.h"
41 #include "lp_bld_swizzle.h"
42
43
44 LLVMValueRef
45 lp_build_broadcast(LLVMBuilderRef builder,
46 LLVMTypeRef vec_type,
47 LLVMValueRef scalar)
48 {
49 const unsigned n = LLVMGetVectorSize(vec_type);
50 LLVMValueRef res;
51 unsigned i;
52
53 res = LLVMGetUndef(vec_type);
54 for(i = 0; i < n; ++i) {
55 LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), i, 0);
56 res = LLVMBuildInsertElement(builder, res, scalar, index, "");
57 }
58
59 return res;
60 }
61
62
63 /**
64 * Broadcast
65 */
66 LLVMValueRef
67 lp_build_broadcast_scalar(struct lp_build_context *bld,
68 LLVMValueRef scalar)
69 {
70 const struct lp_type type = bld->type;
71
72 assert(lp_check_elem_type(type, LLVMTypeOf(scalar)));
73
74 if (type.length == 1) {
75 return scalar;
76 }
77 else {
78 LLVMValueRef res;
79 res = LLVMBuildInsertElement(bld->builder, bld->undef, scalar,
80 LLVMConstInt(LLVMInt32Type(), 0, 0), "");
81 res = LLVMBuildShuffleVector(bld->builder, res, bld->undef,
82 lp_build_const_int_vec(type, 0), "");
83 return res;
84 }
85 }
86
87
88 LLVMValueRef
89 lp_build_broadcast_aos(struct lp_build_context *bld,
90 LLVMValueRef a,
91 unsigned channel)
92 {
93 const struct lp_type type = bld->type;
94 const unsigned n = type.length;
95 unsigned i, j;
96
97 if(a == bld->undef || a == bld->zero || a == bld->one)
98 return a;
99
100 /* XXX: SSE3 has PSHUFB which should be better than bitmasks, but forcing
101 * using shuffles here actually causes worst results. More investigation is
102 * needed. */
103 if (n <= 4) {
104 /*
105 * Shuffle.
106 */
107 LLVMTypeRef elem_type = LLVMInt32Type();
108 LLVMValueRef shuffles[LP_MAX_VECTOR_LENGTH];
109
110 for(j = 0; j < n; j += 4)
111 for(i = 0; i < 4; ++i)
112 shuffles[j + i] = LLVMConstInt(elem_type, j + channel, 0);
113
114 return LLVMBuildShuffleVector(bld->builder, a, bld->undef, LLVMConstVector(shuffles, n), "");
115 }
116 else {
117 /*
118 * Bit mask and recursive shifts
119 *
120 * XYZW XYZW .... XYZW <= input
121 * 0Y00 0Y00 .... 0Y00
122 * YY00 YY00 .... YY00
123 * YYYY YYYY .... YYYY <= output
124 */
125 struct lp_type type4 = type;
126 const char shifts[4][2] = {
127 { 1, 2},
128 {-1, 2},
129 { 1, -2},
130 {-1, -2}
131 };
132 boolean cond[4];
133 unsigned i;
134
135 memset(cond, 0, sizeof cond);
136 cond[channel] = 1;
137
138 a = LLVMBuildAnd(bld->builder, a, lp_build_const_mask_aos(type, cond), "");
139
140 type4.width *= 4;
141 type4.length /= 4;
142
143 a = LLVMBuildBitCast(bld->builder, a, lp_build_vec_type(type4), "");
144
145 for(i = 0; i < 2; ++i) {
146 LLVMValueRef tmp = NULL;
147 int shift = shifts[channel][i];
148
149 #ifdef PIPE_ARCH_LITTLE_ENDIAN
150 shift = -shift;
151 #endif
152
153 if(shift > 0)
154 tmp = LLVMBuildLShr(bld->builder, a, lp_build_const_int_vec(type4, shift*type.width), "");
155 if(shift < 0)
156 tmp = LLVMBuildShl(bld->builder, a, lp_build_const_int_vec(type4, -shift*type.width), "");
157
158 assert(tmp);
159 if(tmp)
160 a = LLVMBuildOr(bld->builder, a, tmp, "");
161 }
162
163 return LLVMBuildBitCast(bld->builder, a, lp_build_vec_type(type), "");
164 }
165 }
166
167
168 LLVMValueRef
169 lp_build_swizzle1_aos(struct lp_build_context *bld,
170 LLVMValueRef a,
171 const unsigned char swizzle[4])
172 {
173 const unsigned n = bld->type.length;
174 unsigned i, j;
175
176 if(a == bld->undef || a == bld->zero || a == bld->one)
177 return a;
178
179 if(swizzle[0] == swizzle[1] && swizzle[1] == swizzle[2] && swizzle[2] == swizzle[3])
180 return lp_build_broadcast_aos(bld, a, swizzle[0]);
181
182 {
183 /*
184 * Shuffle.
185 */
186 LLVMTypeRef elem_type = LLVMInt32Type();
187 LLVMValueRef shuffles[LP_MAX_VECTOR_LENGTH];
188
189 for(j = 0; j < n; j += 4)
190 for(i = 0; i < 4; ++i)
191 shuffles[j + i] = LLVMConstInt(elem_type, j + swizzle[i], 0);
192
193 return LLVMBuildShuffleVector(bld->builder, a, bld->undef, LLVMConstVector(shuffles, n), "");
194 }
195 }
196
197
198 LLVMValueRef
199 lp_build_swizzle2_aos(struct lp_build_context *bld,
200 LLVMValueRef a,
201 LLVMValueRef b,
202 const unsigned char swizzle[4])
203 {
204 const unsigned n = bld->type.length;
205 unsigned i, j;
206
207 if(swizzle[0] < 4 && swizzle[1] < 4 && swizzle[2] < 4 && swizzle[3] < 4)
208 return lp_build_swizzle1_aos(bld, a, swizzle);
209
210 if(a == b) {
211 unsigned char swizzle1[4];
212 swizzle1[0] = swizzle[0] % 4;
213 swizzle1[1] = swizzle[1] % 4;
214 swizzle1[2] = swizzle[2] % 4;
215 swizzle1[3] = swizzle[3] % 4;
216 return lp_build_swizzle1_aos(bld, a, swizzle1);
217 }
218
219 if(swizzle[0] % 4 == 0 &&
220 swizzle[1] % 4 == 1 &&
221 swizzle[2] % 4 == 2 &&
222 swizzle[3] % 4 == 3) {
223 boolean cond[4];
224 cond[0] = swizzle[0] / 4;
225 cond[1] = swizzle[1] / 4;
226 cond[2] = swizzle[2] / 4;
227 cond[3] = swizzle[3] / 4;
228 return lp_build_select_aos(bld, a, b, cond);
229 }
230
231 {
232 /*
233 * Shuffle.
234 */
235 LLVMTypeRef elem_type = LLVMInt32Type();
236 LLVMValueRef shuffles[LP_MAX_VECTOR_LENGTH];
237
238 for(j = 0; j < n; j += 4)
239 for(i = 0; i < 4; ++i)
240 shuffles[j + i] = LLVMConstInt(elem_type, j + (swizzle[i] % 4) + (swizzle[i] / 4 * n), 0);
241
242 return LLVMBuildShuffleVector(bld->builder, a, b, LLVMConstVector(shuffles, n), "");
243 }
244 }
245
246
247 /**
248 * Extended swizzle of a single channel of a SoA vector.
249 *
250 * @param bld building context
251 * @param unswizzled array with the 4 unswizzled values
252 * @param swizzle one of the PIPE_SWIZZLE_*
253 *
254 * @return the swizzled value.
255 */
256 LLVMValueRef
257 lp_build_swizzle_soa_channel(struct lp_build_context *bld,
258 const LLVMValueRef *unswizzled,
259 unsigned swizzle)
260 {
261 switch (swizzle) {
262 case PIPE_SWIZZLE_RED:
263 case PIPE_SWIZZLE_GREEN:
264 case PIPE_SWIZZLE_BLUE:
265 case PIPE_SWIZZLE_ALPHA:
266 return unswizzled[swizzle];
267 case PIPE_SWIZZLE_ZERO:
268 return bld->zero;
269 case PIPE_SWIZZLE_ONE:
270 return bld->one;
271 default:
272 assert(0);
273 return bld->undef;
274 }
275 }
276
277
278 /**
279 * Extended swizzle of a SoA vector.
280 *
281 * @param bld building context
282 * @param unswizzled array with the 4 unswizzled values
283 * @param swizzles array of PIPE_SWIZZLE_*
284 * @param swizzled output swizzled values
285 */
286 void
287 lp_build_swizzle_soa(struct lp_build_context *bld,
288 const LLVMValueRef *unswizzled,
289 const unsigned char swizzles[4],
290 LLVMValueRef *swizzled)
291 {
292 unsigned chan;
293
294 for (chan = 0; chan < 4; ++chan) {
295 swizzled[chan] = lp_build_swizzle_soa_channel(bld, unswizzled,
296 swizzles[chan]);
297 }
298 }
299
300
301 /**
302 * Do an extended swizzle of a SoA vector inplace.
303 *
304 * @param bld building context
305 * @param values intput/output array with the 4 values
306 * @param swizzles array of PIPE_SWIZZLE_*
307 */
308 void
309 lp_build_swizzle_soa_inplace(struct lp_build_context *bld,
310 LLVMValueRef *values,
311 const unsigned char swizzles[4])
312 {
313 LLVMValueRef unswizzled[4];
314 unsigned chan;
315
316 for (chan = 0; chan < 4; ++chan) {
317 unswizzled[chan] = values[chan];
318 }
319
320 lp_build_swizzle_soa(bld, unswizzled, swizzles, values);
321 }