gallivm: Remove unnecessary headers.
[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 #if HAVE_LLVM >= 0x207
80 res = LLVMBuildInsertElement(bld->builder, bld->undef, scalar,
81 LLVMConstInt(LLVMInt32Type(), 0, 0), "");
82 res = LLVMBuildShuffleVector(bld->builder, res, bld->undef,
83 lp_build_const_int_vec(type, 0), "");
84 #else
85 /* XXX: The above path provokes a bug in LLVM 2.6 */
86 unsigned i;
87 res = bld->undef;
88 for(i = 0; i < type.length; ++i) {
89 LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), i, 0);
90 res = LLVMBuildInsertElement(bld->builder, res, scalar, index, "");
91 }
92 #endif
93 return res;
94 }
95 }
96
97
98 /**
99 * Swizzle one channel into all other three channels.
100 */
101 LLVMValueRef
102 lp_build_swizzle_scalar_aos(struct lp_build_context *bld,
103 LLVMValueRef a,
104 unsigned channel)
105 {
106 const struct lp_type type = bld->type;
107 const unsigned n = type.length;
108 unsigned i, j;
109
110 if(a == bld->undef || a == bld->zero || a == bld->one)
111 return a;
112
113 /* XXX: SSE3 has PSHUFB which should be better than bitmasks, but forcing
114 * using shuffles here actually causes worst results. More investigation is
115 * needed. */
116 if (type.width >= 16) {
117 /*
118 * Shuffle.
119 */
120 LLVMTypeRef elem_type = LLVMInt32Type();
121 LLVMValueRef shuffles[LP_MAX_VECTOR_LENGTH];
122
123 for(j = 0; j < n; j += 4)
124 for(i = 0; i < 4; ++i)
125 shuffles[j + i] = LLVMConstInt(elem_type, j + channel, 0);
126
127 return LLVMBuildShuffleVector(bld->builder, a, bld->undef, LLVMConstVector(shuffles, n), "");
128 }
129 else {
130 /*
131 * Bit mask and recursive shifts
132 *
133 * XYZW XYZW .... XYZW <= input
134 * 0Y00 0Y00 .... 0Y00
135 * YY00 YY00 .... YY00
136 * YYYY YYYY .... YYYY <= output
137 */
138 struct lp_type type4;
139 const char shifts[4][2] = {
140 { 1, 2},
141 {-1, 2},
142 { 1, -2},
143 {-1, -2}
144 };
145 unsigned i;
146
147 a = LLVMBuildAnd(bld->builder, a,
148 lp_build_const_mask_aos(type, 1 << channel), "");
149
150 /*
151 * Build a type where each element is an integer that cover the four
152 * channels.
153 */
154
155 type4 = type;
156 type4.floating = FALSE;
157 type4.width *= 4;
158 type4.length /= 4;
159
160 a = LLVMBuildBitCast(bld->builder, a, lp_build_vec_type(type4), "");
161
162 for(i = 0; i < 2; ++i) {
163 LLVMValueRef tmp = NULL;
164 int shift = shifts[channel][i];
165
166 #ifdef PIPE_ARCH_LITTLE_ENDIAN
167 shift = -shift;
168 #endif
169
170 if(shift > 0)
171 tmp = LLVMBuildLShr(bld->builder, a, lp_build_const_int_vec(type4, shift*type.width), "");
172 if(shift < 0)
173 tmp = LLVMBuildShl(bld->builder, a, lp_build_const_int_vec(type4, -shift*type.width), "");
174
175 assert(tmp);
176 if(tmp)
177 a = LLVMBuildOr(bld->builder, a, tmp, "");
178 }
179
180 return LLVMBuildBitCast(bld->builder, a, lp_build_vec_type(type), "");
181 }
182 }
183
184
185 LLVMValueRef
186 lp_build_swizzle_aos(struct lp_build_context *bld,
187 LLVMValueRef a,
188 const unsigned char swizzles[4])
189 {
190 const struct lp_type type = bld->type;
191 const unsigned n = type.length;
192 unsigned i, j;
193
194 if (swizzles[0] == PIPE_SWIZZLE_RED &&
195 swizzles[1] == PIPE_SWIZZLE_GREEN &&
196 swizzles[2] == PIPE_SWIZZLE_BLUE &&
197 swizzles[3] == PIPE_SWIZZLE_ALPHA) {
198 return a;
199 }
200
201 if (swizzles[0] == swizzles[1] &&
202 swizzles[1] == swizzles[2] &&
203 swizzles[2] == swizzles[3]) {
204 switch (swizzles[0]) {
205 case PIPE_SWIZZLE_RED:
206 case PIPE_SWIZZLE_GREEN:
207 case PIPE_SWIZZLE_BLUE:
208 case PIPE_SWIZZLE_ALPHA:
209 return lp_build_swizzle_scalar_aos(bld, a, swizzles[0]);
210 case PIPE_SWIZZLE_ZERO:
211 return bld->zero;
212 case PIPE_SWIZZLE_ONE:
213 return bld->one;
214 default:
215 assert(0);
216 return bld->undef;
217 }
218 }
219
220 if (type.width >= 16) {
221 /*
222 * Shuffle.
223 */
224 LLVMValueRef undef = LLVMGetUndef(lp_build_elem_type(type));
225 LLVMTypeRef i32t = LLVMInt32Type();
226 LLVMValueRef shuffles[LP_MAX_VECTOR_LENGTH];
227 LLVMValueRef aux[LP_MAX_VECTOR_LENGTH];
228
229 memset(aux, 0, sizeof aux);
230
231 for(j = 0; j < n; j += 4) {
232 for(i = 0; i < 4; ++i) {
233 unsigned shuffle;
234 switch (swizzles[i]) {
235 default:
236 assert(0);
237 /* fall through */
238 case PIPE_SWIZZLE_RED:
239 case PIPE_SWIZZLE_GREEN:
240 case PIPE_SWIZZLE_BLUE:
241 case PIPE_SWIZZLE_ALPHA:
242 shuffle = j + swizzles[i];
243 break;
244 case PIPE_SWIZZLE_ZERO:
245 shuffle = type.length + 0;
246 if (!aux[0]) {
247 aux[0] = lp_build_const_elem(type, 0.0);
248 }
249 break;
250 case PIPE_SWIZZLE_ONE:
251 shuffle = type.length + 1;
252 if (!aux[1]) {
253 aux[1] = lp_build_const_elem(type, 1.0);
254 }
255 break;
256 }
257 shuffles[j + i] = LLVMConstInt(i32t, shuffle, 0);
258 }
259 }
260
261 for (i = 0; i < n; ++i) {
262 if (!aux[i]) {
263 aux[i] = undef;
264 }
265 }
266
267 return LLVMBuildShuffleVector(bld->builder, a,
268 LLVMConstVector(aux, n),
269 LLVMConstVector(shuffles, n), "");
270 } else {
271 /*
272 * Bit mask and shifts.
273 *
274 * For example, this will convert BGRA to RGBA by doing
275 *
276 * rgba = (bgra & 0x00ff0000) >> 16
277 * | (bgra & 0xff00ff00)
278 * | (bgra & 0x000000ff) << 16
279 *
280 * This is necessary not only for faster cause, but because X86 backend
281 * will refuse shuffles of <4 x i8> vectors
282 */
283 LLVMValueRef res;
284 struct lp_type type4;
285 unsigned cond = 0;
286 unsigned chan;
287 int shift;
288
289 /*
290 * Start with a mixture of 1 and 0.
291 */
292 for (chan = 0; chan < 4; ++chan) {
293 if (swizzles[chan] == PIPE_SWIZZLE_ONE) {
294 cond |= 1 << chan;
295 }
296 }
297 res = lp_build_select_aos(bld, cond, bld->one, bld->zero);
298
299 /*
300 * Build a type where each element is an integer that cover the four
301 * channels.
302 */
303 type4 = type;
304 type4.floating = FALSE;
305 type4.width *= 4;
306 type4.length /= 4;
307
308 a = LLVMBuildBitCast(bld->builder, a, lp_build_vec_type(type4), "");
309 res = LLVMBuildBitCast(bld->builder, res, lp_build_vec_type(type4), "");
310
311 /*
312 * Mask and shift the channels, trying to group as many channels in the
313 * same shift as possible
314 */
315 for (shift = -3; shift <= 3; ++shift) {
316 unsigned long long mask = 0;
317
318 assert(type4.width <= sizeof(mask)*8);
319
320 for (chan = 0; chan < 4; ++chan) {
321 /* FIXME: big endian */
322 if (swizzles[chan] < 4 &&
323 chan - swizzles[chan] == shift) {
324 mask |= ((1ULL << type.width) - 1) << (swizzles[chan] * type.width);
325 }
326 }
327
328 if (mask) {
329 LLVMValueRef masked;
330 LLVMValueRef shifted;
331
332 if (0)
333 debug_printf("shift = %i, mask = 0x%08llx\n", shift, mask);
334
335 masked = LLVMBuildAnd(bld->builder, a,
336 lp_build_const_int_vec(type4, mask), "");
337 if (shift > 0) {
338 shifted = LLVMBuildShl(bld->builder, masked,
339 lp_build_const_int_vec(type4, shift*type.width), "");
340 } else if (shift < 0) {
341 shifted = LLVMBuildLShr(bld->builder, masked,
342 lp_build_const_int_vec(type4, -shift*type.width), "");
343 } else {
344 shifted = masked;
345 }
346
347 res = LLVMBuildOr(bld->builder, res, shifted, "");
348 }
349 }
350
351 return LLVMBuildBitCast(bld->builder, res, lp_build_vec_type(type), "");
352 }
353 }
354
355
356 /**
357 * Extended swizzle of a single channel of a SoA vector.
358 *
359 * @param bld building context
360 * @param unswizzled array with the 4 unswizzled values
361 * @param swizzle one of the PIPE_SWIZZLE_*
362 *
363 * @return the swizzled value.
364 */
365 LLVMValueRef
366 lp_build_swizzle_soa_channel(struct lp_build_context *bld,
367 const LLVMValueRef *unswizzled,
368 unsigned swizzle)
369 {
370 switch (swizzle) {
371 case PIPE_SWIZZLE_RED:
372 case PIPE_SWIZZLE_GREEN:
373 case PIPE_SWIZZLE_BLUE:
374 case PIPE_SWIZZLE_ALPHA:
375 return unswizzled[swizzle];
376 case PIPE_SWIZZLE_ZERO:
377 return bld->zero;
378 case PIPE_SWIZZLE_ONE:
379 return bld->one;
380 default:
381 assert(0);
382 return bld->undef;
383 }
384 }
385
386
387 /**
388 * Extended swizzle of a SoA vector.
389 *
390 * @param bld building context
391 * @param unswizzled array with the 4 unswizzled values
392 * @param swizzles array of PIPE_SWIZZLE_*
393 * @param swizzled output swizzled values
394 */
395 void
396 lp_build_swizzle_soa(struct lp_build_context *bld,
397 const LLVMValueRef *unswizzled,
398 const unsigned char swizzles[4],
399 LLVMValueRef *swizzled)
400 {
401 unsigned chan;
402
403 for (chan = 0; chan < 4; ++chan) {
404 swizzled[chan] = lp_build_swizzle_soa_channel(bld, unswizzled,
405 swizzles[chan]);
406 }
407 }
408
409
410 /**
411 * Do an extended swizzle of a SoA vector inplace.
412 *
413 * @param bld building context
414 * @param values intput/output array with the 4 values
415 * @param swizzles array of PIPE_SWIZZLE_*
416 */
417 void
418 lp_build_swizzle_soa_inplace(struct lp_build_context *bld,
419 LLVMValueRef *values,
420 const unsigned char swizzles[4])
421 {
422 LLVMValueRef unswizzled[4];
423 unsigned chan;
424
425 for (chan = 0; chan < 4; ++chan) {
426 unswizzled[chan] = values[chan];
427 }
428
429 lp_build_swizzle_soa(bld, unswizzled, swizzles, values);
430 }