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 swizzling/shuffling.
32 * @author Jose Fonseca <jfonseca@vmware.com>
36 #include "util/u_debug.h"
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"
46 lp_build_broadcast(struct gallivm_state
*gallivm
,
52 if (LLVMGetTypeKind(vec_type
) != LLVMVectorTypeKind
) {
54 assert(vec_type
== LLVMTypeOf(scalar
));
57 LLVMBuilderRef builder
= gallivm
->builder
;
58 const unsigned length
= LLVMGetVectorSize(vec_type
);
59 LLVMValueRef undef
= LLVMGetUndef(vec_type
);
60 LLVMTypeRef i32_type
= LLVMInt32TypeInContext(gallivm
->context
);
62 assert(LLVMGetElementType(vec_type
) == LLVMTypeOf(scalar
));
64 if (HAVE_LLVM
>= 0x207) {
65 /* The shuffle vector is always made of int32 elements */
66 LLVMTypeRef i32_vec_type
= LLVMVectorType(i32_type
, length
);
67 res
= LLVMBuildInsertElement(builder
, undef
, scalar
, LLVMConstNull(i32_type
), "");
68 res
= LLVMBuildShuffleVector(builder
, res
, undef
, LLVMConstNull(i32_vec_type
), "");
70 /* XXX: The above path provokes a bug in LLVM 2.6 */
73 for(i
= 0; i
< length
; ++i
) {
74 LLVMValueRef index
= lp_build_const_int32(gallivm
, i
);
75 res
= LLVMBuildInsertElement(builder
, res
, scalar
, index
, "");
88 lp_build_broadcast_scalar(struct lp_build_context
*bld
,
91 assert(lp_check_elem_type(bld
->type
, LLVMTypeOf(scalar
)));
93 return lp_build_broadcast(bld
->gallivm
, bld
->vec_type
, scalar
);
98 * Combined extract and broadcast (or a mere shuffle when the two types match)
101 lp_build_extract_broadcast(struct gallivm_state
*gallivm
,
102 struct lp_type src_type
,
103 struct lp_type dst_type
,
107 LLVMTypeRef i32t
= LLVMInt32TypeInContext(gallivm
->context
);
110 assert(src_type
.floating
== dst_type
.floating
);
111 assert(src_type
.width
== dst_type
.width
);
113 assert(lp_check_value(src_type
, vector
));
114 assert(LLVMTypeOf(index
) == i32t
);
116 if (src_type
.length
== 1) {
117 if (dst_type
.length
== 1) {
119 * Trivial scalar -> scalar.
126 * Broadcast scalar -> vector.
129 res
= lp_build_broadcast(gallivm
,
130 lp_build_vec_type(gallivm
, dst_type
),
135 if (dst_type
.length
== src_type
.length
) {
137 * Special shuffle of the same size.
140 LLVMValueRef shuffle
;
141 shuffle
= lp_build_broadcast(gallivm
,
142 LLVMVectorType(i32t
, dst_type
.length
),
144 res
= LLVMBuildShuffleVector(gallivm
->builder
, vector
,
145 LLVMGetUndef(lp_build_vec_type(gallivm
, dst_type
)),
150 scalar
= LLVMBuildExtractElement(gallivm
->builder
, vector
, index
, "");
151 if (dst_type
.length
== 1) {
153 * Trivial extract scalar from vector.
160 * General case of different sized vectors.
163 res
= lp_build_broadcast(gallivm
,
164 lp_build_vec_type(gallivm
, dst_type
),
175 * Swizzle one channel into all other three channels.
178 lp_build_swizzle_scalar_aos(struct lp_build_context
*bld
,
182 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
183 const struct lp_type type
= bld
->type
;
184 const unsigned n
= type
.length
;
187 if(a
== bld
->undef
|| a
== bld
->zero
|| a
== bld
->one
)
190 /* XXX: SSE3 has PSHUFB which should be better than bitmasks, but forcing
191 * using shuffles here actually causes worst results. More investigation is
193 if (type
.width
>= 16) {
197 LLVMTypeRef elem_type
= LLVMInt32TypeInContext(bld
->gallivm
->context
);
198 LLVMValueRef shuffles
[LP_MAX_VECTOR_LENGTH
];
200 for(j
= 0; j
< n
; j
+= 4)
201 for(i
= 0; i
< 4; ++i
)
202 shuffles
[j
+ i
] = LLVMConstInt(elem_type
, j
+ channel
, 0);
204 return LLVMBuildShuffleVector(builder
, a
, bld
->undef
, LLVMConstVector(shuffles
, n
), "");
208 * Bit mask and recursive shifts
210 * XYZW XYZW .... XYZW <= input
211 * 0Y00 0Y00 .... 0Y00
212 * YY00 YY00 .... YY00
213 * YYYY YYYY .... YYYY <= output
215 struct lp_type type4
;
216 const char shifts
[4][2] = {
224 a
= LLVMBuildAnd(builder
, a
,
225 lp_build_const_mask_aos(bld
->gallivm
,
226 type
, 1 << channel
), "");
229 * Build a type where each element is an integer that cover the four
234 type4
.floating
= FALSE
;
238 a
= LLVMBuildBitCast(builder
, a
, lp_build_vec_type(bld
->gallivm
, type4
), "");
240 for(i
= 0; i
< 2; ++i
) {
241 LLVMValueRef tmp
= NULL
;
242 int shift
= shifts
[channel
][i
];
244 #ifdef PIPE_ARCH_LITTLE_ENDIAN
249 tmp
= LLVMBuildLShr(builder
, a
, lp_build_const_int_vec(bld
->gallivm
, type4
, shift
*type
.width
), "");
251 tmp
= LLVMBuildShl(builder
, a
, lp_build_const_int_vec(bld
->gallivm
, type4
, -shift
*type
.width
), "");
255 a
= LLVMBuildOr(builder
, a
, tmp
, "");
258 return LLVMBuildBitCast(builder
, a
, lp_build_vec_type(bld
->gallivm
, type
), "");
264 lp_build_swizzle_aos(struct lp_build_context
*bld
,
266 const unsigned char swizzles
[4])
268 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
269 const struct lp_type type
= bld
->type
;
270 const unsigned n
= type
.length
;
273 if (swizzles
[0] == PIPE_SWIZZLE_RED
&&
274 swizzles
[1] == PIPE_SWIZZLE_GREEN
&&
275 swizzles
[2] == PIPE_SWIZZLE_BLUE
&&
276 swizzles
[3] == PIPE_SWIZZLE_ALPHA
) {
280 if (swizzles
[0] == swizzles
[1] &&
281 swizzles
[1] == swizzles
[2] &&
282 swizzles
[2] == swizzles
[3]) {
283 switch (swizzles
[0]) {
284 case PIPE_SWIZZLE_RED
:
285 case PIPE_SWIZZLE_GREEN
:
286 case PIPE_SWIZZLE_BLUE
:
287 case PIPE_SWIZZLE_ALPHA
:
288 return lp_build_swizzle_scalar_aos(bld
, a
, swizzles
[0]);
289 case PIPE_SWIZZLE_ZERO
:
291 case PIPE_SWIZZLE_ONE
:
299 if (type
.width
>= 16) {
303 LLVMValueRef undef
= LLVMGetUndef(lp_build_elem_type(bld
->gallivm
, type
));
304 LLVMTypeRef i32t
= LLVMInt32TypeInContext(bld
->gallivm
->context
);
305 LLVMValueRef shuffles
[LP_MAX_VECTOR_LENGTH
];
306 LLVMValueRef aux
[LP_MAX_VECTOR_LENGTH
];
308 memset(aux
, 0, sizeof aux
);
310 for(j
= 0; j
< n
; j
+= 4) {
311 for(i
= 0; i
< 4; ++i
) {
313 switch (swizzles
[i
]) {
317 case PIPE_SWIZZLE_RED
:
318 case PIPE_SWIZZLE_GREEN
:
319 case PIPE_SWIZZLE_BLUE
:
320 case PIPE_SWIZZLE_ALPHA
:
321 shuffle
= j
+ swizzles
[i
];
323 case PIPE_SWIZZLE_ZERO
:
324 shuffle
= type
.length
+ 0;
326 aux
[0] = lp_build_const_elem(bld
->gallivm
, type
, 0.0);
329 case PIPE_SWIZZLE_ONE
:
330 shuffle
= type
.length
+ 1;
332 aux
[1] = lp_build_const_elem(bld
->gallivm
, type
, 1.0);
336 shuffles
[j
+ i
] = LLVMConstInt(i32t
, shuffle
, 0);
340 for (i
= 0; i
< n
; ++i
) {
346 return LLVMBuildShuffleVector(builder
, a
,
347 LLVMConstVector(aux
, n
),
348 LLVMConstVector(shuffles
, n
), "");
351 * Bit mask and shifts.
353 * For example, this will convert BGRA to RGBA by doing
355 * rgba = (bgra & 0x00ff0000) >> 16
356 * | (bgra & 0xff00ff00)
357 * | (bgra & 0x000000ff) << 16
359 * This is necessary not only for faster cause, but because X86 backend
360 * will refuse shuffles of <4 x i8> vectors
363 struct lp_type type4
;
369 * Start with a mixture of 1 and 0.
371 for (chan
= 0; chan
< 4; ++chan
) {
372 if (swizzles
[chan
] == PIPE_SWIZZLE_ONE
) {
376 res
= lp_build_select_aos(bld
, cond
, bld
->one
, bld
->zero
);
379 * Build a type where each element is an integer that cover the four
383 type4
.floating
= FALSE
;
387 a
= LLVMBuildBitCast(builder
, a
, lp_build_vec_type(bld
->gallivm
, type4
), "");
388 res
= LLVMBuildBitCast(builder
, res
, lp_build_vec_type(bld
->gallivm
, type4
), "");
391 * Mask and shift the channels, trying to group as many channels in the
392 * same shift as possible
394 for (shift
= -3; shift
<= 3; ++shift
) {
395 unsigned long long mask
= 0;
397 assert(type4
.width
<= sizeof(mask
)*8);
399 for (chan
= 0; chan
< 4; ++chan
) {
400 /* FIXME: big endian */
401 if (swizzles
[chan
] < 4 &&
402 chan
- swizzles
[chan
] == shift
) {
403 mask
|= ((1ULL << type
.width
) - 1) << (swizzles
[chan
] * type
.width
);
409 LLVMValueRef shifted
;
412 debug_printf("shift = %i, mask = 0x%08llx\n", shift
, mask
);
414 masked
= LLVMBuildAnd(builder
, a
,
415 lp_build_const_int_vec(bld
->gallivm
, type4
, mask
), "");
417 shifted
= LLVMBuildShl(builder
, masked
,
418 lp_build_const_int_vec(bld
->gallivm
, type4
, shift
*type
.width
), "");
419 } else if (shift
< 0) {
420 shifted
= LLVMBuildLShr(builder
, masked
,
421 lp_build_const_int_vec(bld
->gallivm
, type4
, -shift
*type
.width
), "");
426 res
= LLVMBuildOr(builder
, res
, shifted
, "");
430 return LLVMBuildBitCast(builder
, res
,
431 lp_build_vec_type(bld
->gallivm
, type
), "");
437 * Extended swizzle of a single channel of a SoA vector.
439 * @param bld building context
440 * @param unswizzled array with the 4 unswizzled values
441 * @param swizzle one of the PIPE_SWIZZLE_*
443 * @return the swizzled value.
446 lp_build_swizzle_soa_channel(struct lp_build_context
*bld
,
447 const LLVMValueRef
*unswizzled
,
451 case PIPE_SWIZZLE_RED
:
452 case PIPE_SWIZZLE_GREEN
:
453 case PIPE_SWIZZLE_BLUE
:
454 case PIPE_SWIZZLE_ALPHA
:
455 return unswizzled
[swizzle
];
456 case PIPE_SWIZZLE_ZERO
:
458 case PIPE_SWIZZLE_ONE
:
468 * Extended swizzle of a SoA vector.
470 * @param bld building context
471 * @param unswizzled array with the 4 unswizzled values
472 * @param swizzles array of PIPE_SWIZZLE_*
473 * @param swizzled output swizzled values
476 lp_build_swizzle_soa(struct lp_build_context
*bld
,
477 const LLVMValueRef
*unswizzled
,
478 const unsigned char swizzles
[4],
479 LLVMValueRef
*swizzled
)
483 for (chan
= 0; chan
< 4; ++chan
) {
484 swizzled
[chan
] = lp_build_swizzle_soa_channel(bld
, unswizzled
,
491 * Do an extended swizzle of a SoA vector inplace.
493 * @param bld building context
494 * @param values intput/output array with the 4 values
495 * @param swizzles array of PIPE_SWIZZLE_*
498 lp_build_swizzle_soa_inplace(struct lp_build_context
*bld
,
499 LLVMValueRef
*values
,
500 const unsigned char swizzles
[4])
502 LLVMValueRef unswizzled
[4];
505 for (chan
= 0; chan
< 4; ++chan
) {
506 unswizzled
[chan
] = values
[chan
];
509 lp_build_swizzle_soa(bld
, unswizzled
, swizzles
, values
);