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_logic.h"
41 #include "lp_bld_swizzle.h"
45 lp_build_broadcast(LLVMBuilderRef builder
,
49 const unsigned n
= LLVMGetVectorSize(vec_type
);
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
, "");
67 lp_build_broadcast_scalar(struct lp_build_context
*bld
,
70 const struct lp_type type
= bld
->type
;
72 assert(lp_check_elem_type(type
, LLVMTypeOf(scalar
)));
74 if (type
.length
== 1) {
80 #if HAVE_LLVM >= 0x207
81 /* The shuffle vector is always made of int32 elements */
82 struct lp_type i32_vec_type
= lp_type_int_vec(32);
83 i32_vec_type
.length
= type
.length
;
85 res
= LLVMBuildInsertElement(bld
->builder
, bld
->undef
, scalar
,
86 LLVMConstInt(LLVMInt32Type(), 0, 0), "");
87 res
= LLVMBuildShuffleVector(bld
->builder
, res
, bld
->undef
,
88 lp_build_const_int_vec(i32_vec_type
, 0), "");
90 /* XXX: The above path provokes a bug in LLVM 2.6 */
93 for(i
= 0; i
< type
.length
; ++i
) {
94 LLVMValueRef index
= LLVMConstInt(LLVMInt32Type(), i
, 0);
95 res
= LLVMBuildInsertElement(bld
->builder
, res
, scalar
, index
, "");
104 * Combined extract and broadcast (or a mere shuffle when the two types match)
107 lp_build_extract_broadcast(LLVMBuilderRef builder
,
108 struct lp_type src_type
,
109 struct lp_type dst_type
,
113 LLVMTypeRef i32t
= LLVMInt32Type();
116 assert(src_type
.floating
== dst_type
.floating
);
117 assert(src_type
.width
== dst_type
.width
);
119 assert(lp_check_value(src_type
, vector
));
120 assert(LLVMTypeOf(index
) == i32t
);
122 if (src_type
.length
== 1) {
123 if (dst_type
.length
== 1) {
125 * Trivial scalar -> scalar.
132 * Broadcast scalar -> vector.
135 res
= lp_build_broadcast(builder
,
136 lp_build_vec_type(dst_type
),
141 if (dst_type
.length
== src_type
.length
) {
143 * Special shuffle of the same size.
146 LLVMValueRef shuffle
;
147 shuffle
= lp_build_broadcast(builder
,
148 LLVMVectorType(i32t
, dst_type
.length
),
150 res
= LLVMBuildShuffleVector(builder
, vector
,
151 LLVMGetUndef(lp_build_vec_type(dst_type
)),
156 scalar
= LLVMBuildExtractElement(builder
, vector
, index
, "");
157 if (dst_type
.length
== 1) {
159 * Trivial extract scalar from vector.
166 * General case of different sized vectors.
169 res
= lp_build_broadcast(builder
,
170 lp_build_vec_type(dst_type
),
181 * Swizzle one channel into all other three channels.
184 lp_build_swizzle_scalar_aos(struct lp_build_context
*bld
,
188 const struct lp_type type
= bld
->type
;
189 const unsigned n
= type
.length
;
192 if(a
== bld
->undef
|| a
== bld
->zero
|| a
== bld
->one
)
195 /* XXX: SSE3 has PSHUFB which should be better than bitmasks, but forcing
196 * using shuffles here actually causes worst results. More investigation is
198 if (type
.width
>= 16) {
202 LLVMTypeRef elem_type
= LLVMInt32Type();
203 LLVMValueRef shuffles
[LP_MAX_VECTOR_LENGTH
];
205 for(j
= 0; j
< n
; j
+= 4)
206 for(i
= 0; i
< 4; ++i
)
207 shuffles
[j
+ i
] = LLVMConstInt(elem_type
, j
+ channel
, 0);
209 return LLVMBuildShuffleVector(bld
->builder
, a
, bld
->undef
, LLVMConstVector(shuffles
, n
), "");
213 * Bit mask and recursive shifts
215 * XYZW XYZW .... XYZW <= input
216 * 0Y00 0Y00 .... 0Y00
217 * YY00 YY00 .... YY00
218 * YYYY YYYY .... YYYY <= output
220 struct lp_type type4
;
221 const char shifts
[4][2] = {
229 a
= LLVMBuildAnd(bld
->builder
, a
,
230 lp_build_const_mask_aos(type
, 1 << channel
), "");
233 * Build a type where each element is an integer that cover the four
238 type4
.floating
= FALSE
;
242 a
= LLVMBuildBitCast(bld
->builder
, a
, lp_build_vec_type(type4
), "");
244 for(i
= 0; i
< 2; ++i
) {
245 LLVMValueRef tmp
= NULL
;
246 int shift
= shifts
[channel
][i
];
248 #ifdef PIPE_ARCH_LITTLE_ENDIAN
253 tmp
= LLVMBuildLShr(bld
->builder
, a
, lp_build_const_int_vec(type4
, shift
*type
.width
), "");
255 tmp
= LLVMBuildShl(bld
->builder
, a
, lp_build_const_int_vec(type4
, -shift
*type
.width
), "");
259 a
= LLVMBuildOr(bld
->builder
, a
, tmp
, "");
262 return LLVMBuildBitCast(bld
->builder
, a
, lp_build_vec_type(type
), "");
268 lp_build_swizzle_aos(struct lp_build_context
*bld
,
270 const unsigned char swizzles
[4])
272 const struct lp_type type
= bld
->type
;
273 const unsigned n
= type
.length
;
276 if (swizzles
[0] == PIPE_SWIZZLE_RED
&&
277 swizzles
[1] == PIPE_SWIZZLE_GREEN
&&
278 swizzles
[2] == PIPE_SWIZZLE_BLUE
&&
279 swizzles
[3] == PIPE_SWIZZLE_ALPHA
) {
283 if (swizzles
[0] == swizzles
[1] &&
284 swizzles
[1] == swizzles
[2] &&
285 swizzles
[2] == swizzles
[3]) {
286 switch (swizzles
[0]) {
287 case PIPE_SWIZZLE_RED
:
288 case PIPE_SWIZZLE_GREEN
:
289 case PIPE_SWIZZLE_BLUE
:
290 case PIPE_SWIZZLE_ALPHA
:
291 return lp_build_swizzle_scalar_aos(bld
, a
, swizzles
[0]);
292 case PIPE_SWIZZLE_ZERO
:
294 case PIPE_SWIZZLE_ONE
:
302 if (type
.width
>= 16) {
306 LLVMValueRef undef
= LLVMGetUndef(lp_build_elem_type(type
));
307 LLVMTypeRef i32t
= LLVMInt32Type();
308 LLVMValueRef shuffles
[LP_MAX_VECTOR_LENGTH
];
309 LLVMValueRef aux
[LP_MAX_VECTOR_LENGTH
];
311 memset(aux
, 0, sizeof aux
);
313 for(j
= 0; j
< n
; j
+= 4) {
314 for(i
= 0; i
< 4; ++i
) {
316 switch (swizzles
[i
]) {
320 case PIPE_SWIZZLE_RED
:
321 case PIPE_SWIZZLE_GREEN
:
322 case PIPE_SWIZZLE_BLUE
:
323 case PIPE_SWIZZLE_ALPHA
:
324 shuffle
= j
+ swizzles
[i
];
326 case PIPE_SWIZZLE_ZERO
:
327 shuffle
= type
.length
+ 0;
329 aux
[0] = lp_build_const_elem(type
, 0.0);
332 case PIPE_SWIZZLE_ONE
:
333 shuffle
= type
.length
+ 1;
335 aux
[1] = lp_build_const_elem(type
, 1.0);
339 shuffles
[j
+ i
] = LLVMConstInt(i32t
, shuffle
, 0);
343 for (i
= 0; i
< n
; ++i
) {
349 return LLVMBuildShuffleVector(bld
->builder
, a
,
350 LLVMConstVector(aux
, n
),
351 LLVMConstVector(shuffles
, n
), "");
354 * Bit mask and shifts.
356 * For example, this will convert BGRA to RGBA by doing
358 * rgba = (bgra & 0x00ff0000) >> 16
359 * | (bgra & 0xff00ff00)
360 * | (bgra & 0x000000ff) << 16
362 * This is necessary not only for faster cause, but because X86 backend
363 * will refuse shuffles of <4 x i8> vectors
366 struct lp_type type4
;
372 * Start with a mixture of 1 and 0.
374 for (chan
= 0; chan
< 4; ++chan
) {
375 if (swizzles
[chan
] == PIPE_SWIZZLE_ONE
) {
379 res
= lp_build_select_aos(bld
, cond
, bld
->one
, bld
->zero
);
382 * Build a type where each element is an integer that cover the four
386 type4
.floating
= FALSE
;
390 a
= LLVMBuildBitCast(bld
->builder
, a
, lp_build_vec_type(type4
), "");
391 res
= LLVMBuildBitCast(bld
->builder
, res
, lp_build_vec_type(type4
), "");
394 * Mask and shift the channels, trying to group as many channels in the
395 * same shift as possible
397 for (shift
= -3; shift
<= 3; ++shift
) {
398 unsigned long long mask
= 0;
400 assert(type4
.width
<= sizeof(mask
)*8);
402 for (chan
= 0; chan
< 4; ++chan
) {
403 /* FIXME: big endian */
404 if (swizzles
[chan
] < 4 &&
405 chan
- swizzles
[chan
] == shift
) {
406 mask
|= ((1ULL << type
.width
) - 1) << (swizzles
[chan
] * type
.width
);
412 LLVMValueRef shifted
;
415 debug_printf("shift = %i, mask = 0x%08llx\n", shift
, mask
);
417 masked
= LLVMBuildAnd(bld
->builder
, a
,
418 lp_build_const_int_vec(type4
, mask
), "");
420 shifted
= LLVMBuildShl(bld
->builder
, masked
,
421 lp_build_const_int_vec(type4
, shift
*type
.width
), "");
422 } else if (shift
< 0) {
423 shifted
= LLVMBuildLShr(bld
->builder
, masked
,
424 lp_build_const_int_vec(type4
, -shift
*type
.width
), "");
429 res
= LLVMBuildOr(bld
->builder
, res
, shifted
, "");
433 return LLVMBuildBitCast(bld
->builder
, res
, lp_build_vec_type(type
), "");
439 * Extended swizzle of a single channel of a SoA vector.
441 * @param bld building context
442 * @param unswizzled array with the 4 unswizzled values
443 * @param swizzle one of the PIPE_SWIZZLE_*
445 * @return the swizzled value.
448 lp_build_swizzle_soa_channel(struct lp_build_context
*bld
,
449 const LLVMValueRef
*unswizzled
,
453 case PIPE_SWIZZLE_RED
:
454 case PIPE_SWIZZLE_GREEN
:
455 case PIPE_SWIZZLE_BLUE
:
456 case PIPE_SWIZZLE_ALPHA
:
457 return unswizzled
[swizzle
];
458 case PIPE_SWIZZLE_ZERO
:
460 case PIPE_SWIZZLE_ONE
:
470 * Extended swizzle of a SoA vector.
472 * @param bld building context
473 * @param unswizzled array with the 4 unswizzled values
474 * @param swizzles array of PIPE_SWIZZLE_*
475 * @param swizzled output swizzled values
478 lp_build_swizzle_soa(struct lp_build_context
*bld
,
479 const LLVMValueRef
*unswizzled
,
480 const unsigned char swizzles
[4],
481 LLVMValueRef
*swizzled
)
485 for (chan
= 0; chan
< 4; ++chan
) {
486 swizzled
[chan
] = lp_build_swizzle_soa_channel(bld
, unswizzled
,
493 * Do an extended swizzle of a SoA vector inplace.
495 * @param bld building context
496 * @param values intput/output array with the 4 values
497 * @param swizzles array of PIPE_SWIZZLE_*
500 lp_build_swizzle_soa_inplace(struct lp_build_context
*bld
,
501 LLVMValueRef
*values
,
502 const unsigned char swizzles
[4])
504 LLVMValueRef unswizzled
[4];
507 for (chan
= 0; chan
< 4; ++chan
) {
508 unswizzled
[chan
] = values
[chan
];
511 lp_build_swizzle_soa(bld
, unswizzled
, swizzles
, values
);