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) {
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), "");
85 /* XXX: The above path provokes a bug in LLVM 2.6 */
88 for(i
= 0; i
< type
.length
; ++i
) {
89 LLVMValueRef index
= LLVMConstInt(LLVMInt32Type(), i
, 0);
90 res
= LLVMBuildInsertElement(bld
->builder
, res
, scalar
, index
, "");
99 * Swizzle one channel into all other three channels.
102 lp_build_swizzle_scalar_aos(struct lp_build_context
*bld
,
106 const struct lp_type type
= bld
->type
;
107 const unsigned n
= type
.length
;
110 if(a
== bld
->undef
|| a
== bld
->zero
|| a
== bld
->one
)
113 /* XXX: SSE3 has PSHUFB which should be better than bitmasks, but forcing
114 * using shuffles here actually causes worst results. More investigation is
116 if (type
.width
>= 16) {
120 LLVMTypeRef elem_type
= LLVMInt32Type();
121 LLVMValueRef shuffles
[LP_MAX_VECTOR_LENGTH
];
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);
127 return LLVMBuildShuffleVector(bld
->builder
, a
, bld
->undef
, LLVMConstVector(shuffles
, n
), "");
131 * Bit mask and recursive shifts
133 * XYZW XYZW .... XYZW <= input
134 * 0Y00 0Y00 .... 0Y00
135 * YY00 YY00 .... YY00
136 * YYYY YYYY .... YYYY <= output
138 struct lp_type type4
;
139 const char shifts
[4][2] = {
147 a
= LLVMBuildAnd(bld
->builder
, a
,
148 lp_build_const_mask_aos(type
, 1 << channel
), "");
151 * Build a type where each element is an integer that cover the four
156 type4
.floating
= FALSE
;
160 a
= LLVMBuildBitCast(bld
->builder
, a
, lp_build_vec_type(type4
), "");
162 for(i
= 0; i
< 2; ++i
) {
163 LLVMValueRef tmp
= NULL
;
164 int shift
= shifts
[channel
][i
];
166 #ifdef PIPE_ARCH_LITTLE_ENDIAN
171 tmp
= LLVMBuildLShr(bld
->builder
, a
, lp_build_const_int_vec(type4
, shift
*type
.width
), "");
173 tmp
= LLVMBuildShl(bld
->builder
, a
, lp_build_const_int_vec(type4
, -shift
*type
.width
), "");
177 a
= LLVMBuildOr(bld
->builder
, a
, tmp
, "");
180 return LLVMBuildBitCast(bld
->builder
, a
, lp_build_vec_type(type
), "");
186 lp_build_swizzle_aos(struct lp_build_context
*bld
,
188 const unsigned char swizzles
[4])
190 const struct lp_type type
= bld
->type
;
191 const unsigned n
= type
.length
;
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
) {
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
:
212 case PIPE_SWIZZLE_ONE
:
220 if (type
.width
>= 16) {
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
];
229 memset(aux
, 0, sizeof aux
);
231 for(j
= 0; j
< n
; j
+= 4) {
232 for(i
= 0; i
< 4; ++i
) {
234 switch (swizzles
[i
]) {
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
];
244 case PIPE_SWIZZLE_ZERO
:
245 shuffle
= type
.length
+ 0;
247 aux
[0] = lp_build_const_elem(type
, 0.0);
250 case PIPE_SWIZZLE_ONE
:
251 shuffle
= type
.length
+ 1;
253 aux
[1] = lp_build_const_elem(type
, 1.0);
257 shuffles
[j
+ i
] = LLVMConstInt(i32t
, shuffle
, 0);
261 for (i
= 0; i
< n
; ++i
) {
267 return LLVMBuildShuffleVector(bld
->builder
, a
,
268 LLVMConstVector(aux
, n
),
269 LLVMConstVector(shuffles
, n
), "");
272 * Bit mask and shifts.
274 * For example, this will convert BGRA to RGBA by doing
276 * rgba = (bgra & 0x00ff0000) >> 16
277 * | (bgra & 0xff00ff00)
278 * | (bgra & 0x000000ff) << 16
280 * This is necessary not only for faster cause, but because X86 backend
281 * will refuse shuffles of <4 x i8> vectors
284 struct lp_type type4
;
290 * Start with a mixture of 1 and 0.
292 for (chan
= 0; chan
< 4; ++chan
) {
293 if (swizzles
[chan
] == PIPE_SWIZZLE_ONE
) {
297 res
= lp_build_select_aos(bld
, cond
, bld
->one
, bld
->zero
);
300 * Build a type where each element is an integer that cover the four
304 type4
.floating
= FALSE
;
308 a
= LLVMBuildBitCast(bld
->builder
, a
, lp_build_vec_type(type4
), "");
309 res
= LLVMBuildBitCast(bld
->builder
, res
, lp_build_vec_type(type4
), "");
312 * Mask and shift the channels, trying to group as many channels in the
313 * same shift as possible
315 for (shift
= -3; shift
<= 3; ++shift
) {
316 unsigned long long mask
= 0;
318 assert(type4
.width
<= sizeof(mask
)*8);
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
);
330 LLVMValueRef shifted
;
333 debug_printf("shift = %i, mask = 0x%08llx\n", shift
, mask
);
335 masked
= LLVMBuildAnd(bld
->builder
, a
,
336 lp_build_const_int_vec(type4
, mask
), "");
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
), "");
347 res
= LLVMBuildOr(bld
->builder
, res
, shifted
, "");
351 return LLVMBuildBitCast(bld
->builder
, res
, lp_build_vec_type(type
), "");
357 * Extended swizzle of a single channel of a SoA vector.
359 * @param bld building context
360 * @param unswizzled array with the 4 unswizzled values
361 * @param swizzle one of the PIPE_SWIZZLE_*
363 * @return the swizzled value.
366 lp_build_swizzle_soa_channel(struct lp_build_context
*bld
,
367 const LLVMValueRef
*unswizzled
,
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
:
378 case PIPE_SWIZZLE_ONE
:
388 * Extended swizzle of a SoA vector.
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
396 lp_build_swizzle_soa(struct lp_build_context
*bld
,
397 const LLVMValueRef
*unswizzled
,
398 const unsigned char swizzles
[4],
399 LLVMValueRef
*swizzled
)
403 for (chan
= 0; chan
< 4; ++chan
) {
404 swizzled
[chan
] = lp_build_swizzle_soa_channel(bld
, unswizzled
,
411 * Do an extended swizzle of a SoA vector inplace.
413 * @param bld building context
414 * @param values intput/output array with the 4 values
415 * @param swizzles array of PIPE_SWIZZLE_*
418 lp_build_swizzle_soa_inplace(struct lp_build_context
*bld
,
419 LLVMValueRef
*values
,
420 const unsigned char swizzles
[4])
422 LLVMValueRef unswizzled
[4];
425 for (chan
= 0; chan
< 4; ++chan
) {
426 unswizzled
[chan
] = values
[chan
];
429 lp_build_swizzle_soa(bld
, unswizzled
, swizzles
, values
);