Debian Codegraph @

src/gallium/auxiliary/gallivm/lp_bld_swizzle.c

raw symbols SCIP
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 <[email protected]>
33 */
34
35 #include <inttypes.h> /* for PRIx64 macro */
36 #include "util/u_debug.h"
37
38 #include "lp_bld_type.h"
39 #include "lp_bld_const.h"
40 #include "lp_bld_init.h"
41 #include "lp_bld_logic.h"
42 #include "lp_bld_swizzle.h"
43 #include "lp_bld_pack.h"
44
45
46 LLVMValueRef
47 lp_build_broadcast(struct gallivm_state *gallivm,
48 LLVMTypeRef vec_type,
49 LLVMValueRef scalar)
50 {
51 LLVMValueRef res;
52
53 if (LLVMGetTypeKind(vec_type) != LLVMVectorTypeKind) {
54 /* scalar */
55 assert(vec_type == LLVMTypeOf(scalar));
56 res = scalar;
57 } else {
58 LLVMBuilderRef builder = gallivm->builder;
59 const unsigned length = LLVMGetVectorSize(vec_type);
60 LLVMValueRef undef = LLVMGetUndef(vec_type);
61 /* The shuffle vector is always made of int32 elements */
62 LLVMTypeRef i32_type = LLVMInt32TypeInContext(gallivm->context);
63 LLVMTypeRef i32_vec_type = LLVMVectorType(i32_type, length);
64
65 assert(LLVM_VERSION_MAJOR >= 15 || LLVMGetElementType(vec_type) == LLVMTypeOf(scalar));
66
67 res = LLVMBuildInsertElement(builder, undef, scalar, LLVMConstNull(i32_type), "");
68 res = LLVMBuildShuffleVector(builder, res, undef, LLVMConstNull(i32_vec_type), "");
69 }
70
71 return res;
72 }
73
74
75 /**
76 * Broadcast
77 */
78 LLVMValueRef
79 lp_build_broadcast_scalar(struct lp_build_context *bld,
80 LLVMValueRef scalar)
81 {
82 assert(lp_check_elem_type(bld->type, LLVMTypeOf(scalar)));
83
84 return lp_build_broadcast(bld->gallivm, bld->vec_type, scalar);
85 }
86
87
88 /**
89 * Combined extract and broadcast (mere shuffle in most cases)
90 */
91 LLVMValueRef
92 lp_build_extract_broadcast(struct gallivm_state *gallivm,
93 struct lp_type src_type,
94 struct lp_type dst_type,
95 LLVMValueRef vector,
96 LLVMValueRef index)
97 {
98 LLVMTypeRef i32t = LLVMInt32TypeInContext(gallivm->context);
99 LLVMValueRef res;
100
101 assert(src_type.floating == dst_type.floating);
102 assert(src_type.width == dst_type.width);
103
104 assert(lp_check_value(src_type, vector));
105 assert(LLVMTypeOf(index) == i32t);
106
107 if (src_type.length == 1) {
108 if (dst_type.length == 1) {
109 /*
110 * Trivial scalar -> scalar.
111 */
112 res = vector;
113 } else {
114 /*
115 * Broadcast scalar -> vector.
116 */
117 res = lp_build_broadcast(gallivm,
118 lp_build_vec_type(gallivm, dst_type),
119 vector);
120 }
121 } else {
122 if (dst_type.length > 1) {
123 /*
124 * shuffle - result can be of different length.
125 */
126 LLVMValueRef shuffle;
127 shuffle = lp_build_broadcast(gallivm,
128 LLVMVectorType(i32t, dst_type.length),
129 index);
130 res = LLVMBuildShuffleVector(gallivm->builder, vector,
131 LLVMGetUndef(lp_build_vec_type(gallivm, src_type)),
132 shuffle, "");
133 } else {
134 /*
135 * Trivial extract scalar from vector.
136 */
137 res = LLVMBuildExtractElement(gallivm->builder, vector, index, "");
138 }
139 }
140
141 return res;
142 }
143
144
145 /**
146 * Swizzle one channel into other channels.
147 */
148 LLVMValueRef
149 lp_build_swizzle_scalar_aos(struct lp_build_context *bld,
150 LLVMValueRef a,
151 unsigned channel,
152 unsigned num_channels)
153 {
154 LLVMBuilderRef builder = bld->gallivm->builder;
155 const struct lp_type type = bld->type;
156 const unsigned n = type.length;
157
158 if (a == bld->undef || a == bld->zero || a == bld->one || num_channels == 1)
159 return a;
160
161 assert(num_channels == 2 || num_channels == 4);
162
163 /* XXX: SSE3 has PSHUFB which should be better than bitmasks, but forcing
164 * using shuffles here actually causes worst results. More investigation is
165 * needed. */
166 if (LLVMIsConstant(a) || type.width >= 16) {
167 /*
168 * Shuffle.
169 */
170 LLVMTypeRef elem_type = LLVMInt32TypeInContext(bld->gallivm->context);
171 LLVMValueRef shuffles[LP_MAX_VECTOR_LENGTH];
172
173 for (unsigned j = 0; j < n; j += num_channels)
174 for (unsigned i = 0; i < num_channels; ++i)
175 shuffles[j + i] = LLVMConstInt(elem_type, j + channel, 0);
176
177 return LLVMBuildShuffleVector(builder, a, bld->undef, LLVMConstVector(shuffles, n), "");
178 } else if (num_channels == 2) {
179 /*
180 * Bit mask and shifts
181 *
182 * XY XY .... XY <= input
183 * 0Y 0Y .... 0Y
184 * YY YY .... YY
185 * YY YY .... YY <= output
186 */
187 struct lp_type type2;
188 LLVMValueRef tmp = NULL;
189 int shift;
190
191 a = LLVMBuildAnd(builder, a,
192 lp_build_const_mask_aos(bld->gallivm,
193 type, 1 << channel, num_channels), "");
194
195 type2 = type;
196 type2.floating = false;
197 type2.width *= 2;
198 type2.length /= 2;
199
200 a = LLVMBuildBitCast(builder, a, lp_build_vec_type(bld->gallivm, type2), "");
201
202 /*
203 * Vector element 0 is always channel X.
204 *
205 * 76 54 32 10 (array numbering)
206 * Little endian reg in: YX YX YX YX
207 * Little endian reg out: YY YY YY YY if shift right (shift == -1)
208 * XX XX XX XX if shift left (shift == 1)
209 *
210 * 01 23 45 67 (array numbering)
211 * Big endian reg in: XY XY XY XY
212 * Big endian reg out: YY YY YY YY if shift left (shift == 1)
213 * XX XX XX XX if shift right (shift == -1)
214 *
215 */
216 #if UTIL_ARCH_LITTLE_ENDIAN
217 shift = channel == 0 ? 1 : -1;
218 #else
219 shift = channel == 0 ? -1 : 1;
220 #endif
221
222 if (shift > 0) {
223 tmp = LLVMBuildShl(builder, a, lp_build_const_int_vec(bld->gallivm, type2, shift * type.width), "");
224 } else if (shift < 0) {
225 tmp = LLVMBuildLShr(builder, a, lp_build_const_int_vec(bld->gallivm, type2, -shift * type.width), "");
226 }
227
228 assert(tmp);
229 if (tmp) {
230 a = LLVMBuildOr(builder, a, tmp, "");
231 }
232
233 return LLVMBuildBitCast(builder, a, lp_build_vec_type(bld->gallivm, type), "");
234 } else {
235 /*
236 * Bit mask and recursive shifts
237 *
238 * Little-endian registers:
239 *
240 * 7654 3210
241 * WZYX WZYX .... WZYX <= input
242 * 00Y0 00Y0 .... 00Y0 <= mask
243 * 00YY 00YY .... 00YY <= shift right 1 (shift amount -1)
244 * YYYY YYYY .... YYYY <= shift left 2 (shift amount 2)
245 *
246 * Big-endian registers:
247 *
248 * 0123 4567
249 * XYZW XYZW .... XYZW <= input
250 * 0Y00 0Y00 .... 0Y00 <= mask
251 * YY00 YY00 .... YY00 <= shift left 1 (shift amount 1)
252 * YYYY YYYY .... YYYY <= shift right 2 (shift amount -2)
253 *
254 * shifts[] gives little-endian shift amounts; we need to negate for big-endian.
255 */
256 static const int shifts[4][2] = {
257 { 1, 2},
258 {-1, 2},
259 { 1, -2},
260 {-1, -2}
261 };
262
263 a = LLVMBuildAnd(builder, a,
264 lp_build_const_mask_aos(bld->gallivm,
265 type, 1 << channel, 4), "");
266
267 /*
268 * Build a type where each element is an integer that cover the four
269 * channels.
270 */
271
272 struct lp_type type4 = type;
273 type4.floating = false;
274 type4.width *= 4;
275 type4.length /= 4;
276
277 a = LLVMBuildBitCast(builder, a, lp_build_vec_type(bld->gallivm, type4), "");
278
279 for (unsigned i = 0; i < 2; ++i) {
280 LLVMValueRef tmp = NULL;
281 int shift = shifts[channel][i];
282
283 /* See endianness diagram above */
284 #if UTIL_ARCH_BIG_ENDIAN
285 shift = -shift;
286 #endif
287
288 if (shift > 0)
289 tmp = LLVMBuildShl(builder, a, lp_build_const_int_vec(bld->gallivm, type4, shift*type.width), "");
290 if (shift < 0)
291 tmp = LLVMBuildLShr(builder, a, lp_build_const_int_vec(bld->gallivm, type4, -shift*type.width), "");
292
293 assert(tmp);
294 if (tmp)
295 a = LLVMBuildOr(builder, a, tmp, "");
296 }
297
298 return LLVMBuildBitCast(builder, a, lp_build_vec_type(bld->gallivm, type), "");
299 }
300 }
301
302
303 /**
304 * Swizzle a vector consisting of an array of XYZW structs.
305 *
306 * This fills a vector of dst_len length with the swizzled channels from src.
307 * Swizzle values are repeated as many times as necessary to fill the dst_len.
308 * The "stride" value is used to increment a base index every time the list of
309 * swizzle repeats. Once its value equals or exceeds the source length, all
310 * further values are "undef".
311 *
312 * e.g. with swizzles = { 2, 1, 0 }, stride = 0, dst_len = 6 results in
313 * R1G1B1A1 R2G2B2A2 = B1G1R1 B1G1R1 B1G1
314 *
315 * e.g. with swizzles = { 2, 1, 0 }, stride = 4, dst_len = 6 results in
316 * R1G1B1A1 R2G2B2A2 = B1G1R1 B2G2R2 undef undef
317 *
318 * @param swizzles the swizzle array
319 * @param num_swizzles the number of elements in swizzles
320 * @param dst_len the length of the result
321 */
322 LLVMValueRef
323 lp_build_swizzle_aos_n(struct gallivm_state* gallivm,
324 LLVMValueRef src,
325 const unsigned char* swizzles,
326 unsigned num_swizzles,
327 unsigned stride,
328 unsigned dst_len)
329 {
330 LLVMBuilderRef builder = gallivm->builder;
331 LLVMValueRef shuffles[LP_MAX_VECTOR_WIDTH];
332
333 assert(dst_len < LP_MAX_VECTOR_WIDTH);
334
335 if (dst_len == 1) {
336 return LLVMBuildExtractElement(builder, src,
337 lp_build_const_int32(gallivm, swizzles[0]), "");
338 }
339
340 const unsigned src_len = LLVMGetVectorSize(LLVMTypeOf(src));
341
342 unsigned base_index = 0;
343 for (unsigned i = 0; i < dst_len; ++i) {
344 const unsigned local_index = i % num_swizzles;
345 unsigned swizzle = swizzles[local_index];
346 swizzle += base_index;
347 if (local_index + 1 == num_swizzles) {
348 base_index += stride;
349 }
350
351 if (swizzle >= src_len) {
352 swizzle = LP_BLD_SWIZZLE_DONTCARE;
353 }
354
355 if (swizzle == LP_BLD_SWIZZLE_DONTCARE) {
356 shuffles[i] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
357 } else {
358 shuffles[i] = lp_build_const_int32(gallivm, swizzle);
359 }
360 }
361
362 return LLVMBuildShuffleVector(builder, src,
363 LLVMGetUndef(LLVMTypeOf(src)),
364 LLVMConstVector(shuffles, dst_len), "");
365 }
366
367
368 LLVMValueRef
369 lp_build_swizzle_aos(struct lp_build_context *bld,
370 LLVMValueRef a,
371 const unsigned char swizzles[4])
372 {
373 LLVMBuilderRef builder = bld->gallivm->builder;
374 const struct lp_type type = bld->type;
375 const unsigned n = type.length;
376
377 if (swizzles[0] == PIPE_SWIZZLE_X &&
378 swizzles[1] == PIPE_SWIZZLE_Y &&
379 swizzles[2] == PIPE_SWIZZLE_Z &&
380 swizzles[3] == PIPE_SWIZZLE_W) {
381 return a;
382 }
383
384 if (swizzles[0] == swizzles[1] &&
385 swizzles[1] == swizzles[2] &&
386 swizzles[2] == swizzles[3]) {
387 switch (swizzles[0]) {
388 case PIPE_SWIZZLE_X:
389 case PIPE_SWIZZLE_Y:
390 case PIPE_SWIZZLE_Z:
391 case PIPE_SWIZZLE_W:
392 return lp_build_swizzle_scalar_aos(bld, a, swizzles[0], 4);
393 case PIPE_SWIZZLE_0:
394 return bld->zero;
395 case PIPE_SWIZZLE_1:
396 return bld->one;
397 case LP_BLD_SWIZZLE_DONTCARE:
398 return bld->undef;
399 default:
400 assert(0);
401 return bld->undef;
402 }
403 }
404
405 if (LLVMIsConstant(a) ||
406 type.width >= 16) {
407 /*
408 * Shuffle.
409 */
410 LLVMValueRef undef = LLVMGetUndef(lp_build_elem_type(bld->gallivm, type));
411 LLVMTypeRef i32t = LLVMInt32TypeInContext(bld->gallivm->context);
412 LLVMValueRef shuffles[LP_MAX_VECTOR_LENGTH];
413 LLVMValueRef aux[LP_MAX_VECTOR_LENGTH];
414
415 memset(aux, 0, sizeof aux);
416
417 for (unsigned j = 0; j < n; j += 4) {
418 for (unsigned i = 0; i < 4; ++i) {
419 unsigned shuffle;
420 switch (swizzles[i]) {
421 default:
422 UNREACHABLE("Unsupported swizzle");
423 case PIPE_SWIZZLE_X:
424 case PIPE_SWIZZLE_Y:
425 case PIPE_SWIZZLE_Z:
426 case PIPE_SWIZZLE_W:
427 shuffle = j + swizzles[i];
428 shuffles[j + i] = LLVMConstInt(i32t, shuffle, 0);
429 break;
430 case PIPE_SWIZZLE_0:
431 shuffle = type.length + 0;
432 shuffles[j + i] = LLVMConstInt(i32t, shuffle, 0);
433 if (!aux[0]) {
434 aux[0] = lp_build_const_elem(bld->gallivm, type, 0.0);
435 }
436 break;
437 case PIPE_SWIZZLE_1:
438 shuffle = type.length + 1;
439 shuffles[j + i] = LLVMConstInt(i32t, shuffle, 0);
440 if (!aux[1]) {
441 aux[1] = lp_build_const_elem(bld->gallivm, type, 1.0);
442 }
443 break;
444 case LP_BLD_SWIZZLE_DONTCARE:
445 shuffles[j + i] = LLVMGetUndef(i32t);
446 break;
447 }
448 }
449 }
450
451 for (unsigned i = 0; i < n; ++i) {
452 if (!aux[i]) {
453 aux[i] = undef;
454 }
455 }
456
457 return LLVMBuildShuffleVector(builder, a,
458 LLVMConstVector(aux, n),
459 LLVMConstVector(shuffles, n), "");
460 } else {
461 /*
462 * Bit mask and shifts.
463 *
464 * For example, this will convert BGRA to RGBA by doing
465 *
466 * Little endian:
467 * rgba = (bgra & 0x00ff0000) >> 16
468 * | (bgra & 0xff00ff00)
469 * | (bgra & 0x000000ff) << 16
470 *
471 * Big endian:A
472 * rgba = (bgra & 0x0000ff00) << 16
473 * | (bgra & 0x00ff00ff)
474 * | (bgra & 0xff000000) >> 16
475 *
476 * This is necessary not only for faster cause, but because X86 backend
477 * will refuse shuffles of <4 x i8> vectors
478 */
479
480 /*
481 * Start with a mixture of 1 and 0.
482 */
483 unsigned cond = 0;
484 for (unsigned chan = 0; chan < 4; ++chan) {
485 if (swizzles[chan] == PIPE_SWIZZLE_1) {
486 cond |= 1 << chan;
487 }
488 }
489 LLVMValueRef res =
490 lp_build_select_aos(bld, cond, bld->one, bld->zero, 4);
491
492 /*
493 * Build a type where each element is an integer that cover the four
494 * channels.
495 */
496 struct lp_type type4 = type;
497 type4.floating = false;
498 type4.width *= 4;
499 type4.length /= 4;
500
501 a = LLVMBuildBitCast(builder, a, lp_build_vec_type(bld->gallivm, type4), "");
502 res = LLVMBuildBitCast(builder, res, lp_build_vec_type(bld->gallivm, type4), "");
503
504 /*
505 * Mask and shift the channels, trying to group as many channels in the
506 * same shift as possible. The shift amount is positive for shifts left
507 * and negative for shifts right.
508 */
509 for (int shift = -3; shift <= 3; ++shift) {
510 uint64_t mask = 0;
511
512 assert(type4.width <= sizeof(mask)*8);
513
514 /*
515 * Vector element numbers follow the XYZW order, so 0 is always X,
516 * etc. After widening 4 times we have:
517 *
518 * 3210
519 * Little-endian register layout: WZYX
520 *
521 * 0123
522 * Big-endian register layout: XYZW
523 *
524 * For little-endian, higher-numbered channels are obtained by a
525 * shift right (negative shift amount) and lower-numbered channels by
526 * a shift left (positive shift amount). The opposite is true for
527 * big-endian.
528 */
529 for (unsigned chan = 0; chan < 4; ++chan) {
530 if (swizzles[chan] < 4) {
531 /* We need to move channel swizzles[chan] into channel chan */
532 #if UTIL_ARCH_LITTLE_ENDIAN
533 if (swizzles[chan] - chan == -shift) {
534 mask |= ((1ULL << type.width) - 1) << (swizzles[chan] * type.width);
535 }
536 #else
537 if (swizzles[chan] - chan == shift) {
538 mask |= ((1ULL << type.width) - 1) << (type4.width - type.width) >> (swizzles[chan] * type.width);
539 }
540 #endif
541 }
542 }
543
544 if (mask) {
545 LLVMValueRef masked;
546 LLVMValueRef shifted;
547 if (0)
548 debug_printf("shift = %i, mask = %" PRIx64 "\n", shift, mask);
549
550 masked = LLVMBuildAnd(builder, a,
551 lp_build_const_int_vec(bld->gallivm, type4, mask), "");
552 if (shift > 0) {
553 shifted = LLVMBuildShl(builder, masked,
554 lp_build_const_int_vec(bld->gallivm, type4, shift*type.width), "");
555 } else if (shift < 0) {
556 shifted = LLVMBuildLShr(builder, masked,
557 lp_build_const_int_vec(bld->gallivm, type4, -shift*type.width), "");
558 } else {
559 shifted = masked;
560 }
561
562 res = LLVMBuildOr(builder, res, shifted, "");
563 }
564 }
565
566 return LLVMBuildBitCast(builder, res,
567 lp_build_vec_type(bld->gallivm, type), "");
568 }
569 }
570
571
572 /**
573 * Extended swizzle of a single channel of a SoA vector.
574 *
575 * @param bld building context
576 * @param unswizzled array with the 4 unswizzled values
577 * @param swizzle one of the PIPE_SWIZZLE_*
578 *
579 * @return the swizzled value.
580 */
581 LLVMValueRef
582 lp_build_swizzle_soa_channel(struct lp_build_context *bld,
583 const LLVMValueRef *unswizzled,
584 enum pipe_swizzle swizzle)
585 {
586 switch (swizzle) {
587 case PIPE_SWIZZLE_X:
588 case PIPE_SWIZZLE_Y:
589 case PIPE_SWIZZLE_Z:
590 case PIPE_SWIZZLE_W:
591 return unswizzled[swizzle];
592 case PIPE_SWIZZLE_0:
593 return bld->zero;
594 case PIPE_SWIZZLE_1:
595 return bld->one;
596 default:
597 assert(0);
598 return bld->undef;
599 }
600 }
601
602
603 /**
604 * Extended swizzle of a SoA vector.
605 *
606 * @param bld building context
607 * @param unswizzled array with the 4 unswizzled values
608 * @param swizzles array of PIPE_SWIZZLE_*
609 * @param swizzled output swizzled values
610 */
611 void
612 lp_build_swizzle_soa(struct lp_build_context *bld,
613 const LLVMValueRef *unswizzled,
614 const unsigned char swizzles[4],
615 LLVMValueRef *swizzled)
616 {
617 for (unsigned chan = 0; chan < 4; ++chan) {
618 swizzled[chan] = lp_build_swizzle_soa_channel(bld, unswizzled,
619 swizzles[chan]);
620 }
621 }
622
623
624 /**
625 * Do an extended swizzle of a SoA vector inplace.
626 *
627 * @param bld building context
628 * @param values intput/output array with the 4 values
629 * @param swizzles array of PIPE_SWIZZLE_*
630 */
631 void
632 lp_build_swizzle_soa_inplace(struct lp_build_context *bld,
633 LLVMValueRef *values,
634 const unsigned char swizzles[4])
635 {
636 LLVMValueRef unswizzled[4];
637
638 for (unsigned chan = 0; chan < 4; ++chan) {
639 unswizzled[chan] = values[chan];
640 }
641
642 lp_build_swizzle_soa(bld, unswizzled, swizzles, values);
643 }
644
645
646 /**
647 * Transpose from AOS <-> SOA
648 *
649 * @param single_type_lp type of pixels
650 * @param src the 4 * n pixel input
651 * @param dst the 4 * n pixel output
652 */
653 void
654 lp_build_transpose_aos(struct gallivm_state *gallivm,
655 struct lp_type single_type_lp,
656 const LLVMValueRef src[4],
657 LLVMValueRef dst[4])
658 {
659 struct lp_type double_type_lp = single_type_lp;
660 double_type_lp.length >>= 1;
661 double_type_lp.width <<= 1;
662
663 LLVMTypeRef double_type = lp_build_vec_type(gallivm, double_type_lp);
664 LLVMTypeRef single_type = lp_build_vec_type(gallivm, single_type_lp);
665
666 LLVMValueRef double_type_zero = LLVMConstNull(double_type);
667 LLVMValueRef t0 = NULL, t1 = NULL, t2 = NULL, t3 = NULL;
668
669 /* Interleave x, y, z, w -> xy and zw */
670 if (src[0] || src[1]) {
671 LLVMValueRef src0 = src[0];
672 LLVMValueRef src1 = src[1];
673 if (!src0)
674 src0 = LLVMConstNull(single_type);
675 if (!src1)
676 src1 = LLVMConstNull(single_type);
677 t0 = lp_build_interleave2_half(gallivm, single_type_lp, src0, src1, 0);
678 t2 = lp_build_interleave2_half(gallivm, single_type_lp, src0, src1, 1);
679
680 /* Cast to double width type for second interleave */
681 t0 = LLVMBuildBitCast(gallivm->builder, t0, double_type, "t0");
682 t2 = LLVMBuildBitCast(gallivm->builder, t2, double_type, "t2");
683 }
684 if (src[2] || src[3]) {
685 LLVMValueRef src2 = src[2];
686 LLVMValueRef src3 = src[3];
687 if (!src2)
688 src2 = LLVMConstNull(single_type);
689 if (!src3)
690 src3 = LLVMConstNull(single_type);
691 t1 = lp_build_interleave2_half(gallivm, single_type_lp, src2, src3, 0);
692 t3 = lp_build_interleave2_half(gallivm, single_type_lp, src2, src3, 1);
693
694 /* Cast to double width type for second interleave */
695 t1 = LLVMBuildBitCast(gallivm->builder, t1, double_type, "t1");
696 t3 = LLVMBuildBitCast(gallivm->builder, t3, double_type, "t3");
697 }
698
699 if (!t0)
700 t0 = double_type_zero;
701 if (!t1)
702 t1 = double_type_zero;
703 if (!t2)
704 t2 = double_type_zero;
705 if (!t3)
706 t3 = double_type_zero;
707
708 /* Interleave xy, zw -> xyzw */
709 dst[0] = lp_build_interleave2_half(gallivm, double_type_lp, t0, t1, 0);
710 dst[1] = lp_build_interleave2_half(gallivm, double_type_lp, t0, t1, 1);
711 dst[2] = lp_build_interleave2_half(gallivm, double_type_lp, t2, t3, 0);
712 dst[3] = lp_build_interleave2_half(gallivm, double_type_lp, t2, t3, 1);
713
714 /* Cast back to original single width type */
715 dst[0] = LLVMBuildBitCast(gallivm->builder, dst[0], single_type, "dst0");
716 dst[1] = LLVMBuildBitCast(gallivm->builder, dst[1], single_type, "dst1");
717 dst[2] = LLVMBuildBitCast(gallivm->builder, dst[2], single_type, "dst2");
718 dst[3] = LLVMBuildBitCast(gallivm->builder, dst[3], single_type, "dst3");
719 }
720
721
722 /**
723 * Transpose from AOS <-> SOA for num_srcs
724 */
725 void
726 lp_build_transpose_aos_n(struct gallivm_state *gallivm,
727 struct lp_type type,
728 const LLVMValueRef* src,
729 unsigned num_srcs,
730 LLVMValueRef* dst)
731 {
732 switch (num_srcs) {
733 case 1:
734 dst[0] = src[0];
735 break;
736 case 2:
737 {
738 /* Note: we must use a temporary incase src == dst */
739 LLVMValueRef lo, hi;
740
741 lo = lp_build_interleave2_half(gallivm, type, src[0], src[1], 0);
742 hi = lp_build_interleave2_half(gallivm, type, src[0], src[1], 1);
743
744 dst[0] = lo;
745 dst[1] = hi;
746 break;
747 }
748 case 4:
749 lp_build_transpose_aos(gallivm, type, src, dst);
750 break;
751 default:
752 assert(0);
753 }
754 }
755
756
757 /**
758 * Pack n-th element of aos values,
759 * pad out to destination size.
760 * i.e. x1 y1 _ _ x2 y2 _ _ will become x1 x2 _ _
761 */
762 LLVMValueRef
763 lp_build_pack_aos_scalars(struct gallivm_state *gallivm,
764 struct lp_type src_type,
765 struct lp_type dst_type,
766 const LLVMValueRef src,
767 unsigned channel)
768 {
769 LLVMTypeRef i32t = LLVMInt32TypeInContext(gallivm->context);
770 LLVMValueRef undef = LLVMGetUndef(i32t);
771 LLVMValueRef shuffles[LP_MAX_VECTOR_LENGTH];
772 unsigned num_src = src_type.length / 4;
773 unsigned num_dst = dst_type.length;
774
775 assert(num_src <= num_dst);
776
777 for (unsigned i = 0; i < num_src; i++) {
778 shuffles[i] = LLVMConstInt(i32t, i * 4 + channel, 0);
779 }
780 for (unsigned i = num_src; i < num_dst; i++) {
781 shuffles[i] = undef;
782 }
783
784 if (num_dst == 1) {
785 return LLVMBuildExtractElement(gallivm->builder, src, shuffles[0], "");
786 }
787 else {
788 return LLVMBuildShuffleVector(gallivm->builder, src, src,
789 LLVMConstVector(shuffles, num_dst), "");
790 }
791 }
792
793
794 /**
795 * Unpack and broadcast packed aos values consisting of only the
796 * first value, i.e. x1 x2 _ _ will become x1 x1 x1 x1 x2 x2 x2 x2
797 */
798 LLVMValueRef
799 lp_build_unpack_broadcast_aos_scalars(struct gallivm_state *gallivm,
800 struct lp_type src_type,
801 struct lp_type dst_type,
802 const LLVMValueRef src)
803 {
804 LLVMTypeRef i32t = LLVMInt32TypeInContext(gallivm->context);
805 LLVMValueRef shuffles[LP_MAX_VECTOR_LENGTH];
806 unsigned num_dst = dst_type.length;
807 unsigned num_src = dst_type.length / 4;
808
809 assert(num_dst / 4 <= src_type.length);
810
811 for (unsigned i = 0; i < num_src; i++) {
812 shuffles[i*4] = LLVMConstInt(i32t, i, 0);
813 shuffles[i*4+1] = LLVMConstInt(i32t, i, 0);
814 shuffles[i*4+2] = LLVMConstInt(i32t, i, 0);
815 shuffles[i*4+3] = LLVMConstInt(i32t, i, 0);
816 }
817
818 if (num_src == 1) {
819 return lp_build_extract_broadcast(gallivm, src_type, dst_type,
820 src, shuffles[0]);
821 } else {
822 return LLVMBuildShuffleVector(gallivm->builder, src, src,
823 LLVMConstVector(shuffles, num_dst), "");
824 }
825 }