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 lp_build_broadcast_aos(struct lp_build_context
*bld
,
103 const struct lp_type type
= bld
->type
;
104 const unsigned n
= type
.length
;
107 if(a
== bld
->undef
|| a
== bld
->zero
|| a
== bld
->one
)
110 /* XXX: SSE3 has PSHUFB which should be better than bitmasks, but forcing
111 * using shuffles here actually causes worst results. More investigation is
113 if (type
.width
>= 16) {
117 LLVMTypeRef elem_type
= LLVMInt32Type();
118 LLVMValueRef shuffles
[LP_MAX_VECTOR_LENGTH
];
120 for(j
= 0; j
< n
; j
+= 4)
121 for(i
= 0; i
< 4; ++i
)
122 shuffles
[j
+ i
] = LLVMConstInt(elem_type
, j
+ channel
, 0);
124 return LLVMBuildShuffleVector(bld
->builder
, a
, bld
->undef
, LLVMConstVector(shuffles
, n
), "");
128 * Bit mask and recursive shifts
130 * XYZW XYZW .... XYZW <= input
131 * 0Y00 0Y00 .... 0Y00
132 * YY00 YY00 .... YY00
133 * YYYY YYYY .... YYYY <= output
135 struct lp_type type4
;
136 const char shifts
[4][2] = {
144 a
= LLVMBuildAnd(bld
->builder
, a
,
145 lp_build_const_mask_aos(type
, 1 << channel
), "");
148 * Build a type where each element is an integer that cover the four
153 type4
.floating
= FALSE
;
157 a
= LLVMBuildBitCast(bld
->builder
, a
, lp_build_vec_type(type4
), "");
159 for(i
= 0; i
< 2; ++i
) {
160 LLVMValueRef tmp
= NULL
;
161 int shift
= shifts
[channel
][i
];
163 #ifdef PIPE_ARCH_LITTLE_ENDIAN
168 tmp
= LLVMBuildLShr(bld
->builder
, a
, lp_build_const_int_vec(type4
, shift
*type
.width
), "");
170 tmp
= LLVMBuildShl(bld
->builder
, a
, lp_build_const_int_vec(type4
, -shift
*type
.width
), "");
174 a
= LLVMBuildOr(bld
->builder
, a
, tmp
, "");
177 return LLVMBuildBitCast(bld
->builder
, a
, lp_build_vec_type(type
), "");
183 lp_build_swizzle_aos(struct lp_build_context
*bld
,
185 const unsigned char swizzles
[4])
187 const struct lp_type type
= bld
->type
;
188 const unsigned n
= type
.length
;
191 if (swizzles
[0] == PIPE_SWIZZLE_RED
&&
192 swizzles
[1] == PIPE_SWIZZLE_GREEN
&&
193 swizzles
[2] == PIPE_SWIZZLE_BLUE
&&
194 swizzles
[3] == PIPE_SWIZZLE_ALPHA
) {
198 if (swizzles
[0] == swizzles
[1] &&
199 swizzles
[1] == swizzles
[2] &&
200 swizzles
[2] == swizzles
[3]) {
201 switch (swizzles
[0]) {
202 case PIPE_SWIZZLE_RED
:
203 case PIPE_SWIZZLE_GREEN
:
204 case PIPE_SWIZZLE_BLUE
:
205 case PIPE_SWIZZLE_ALPHA
:
206 return lp_build_broadcast_aos(bld
, a
, swizzles
[0]);
207 case PIPE_SWIZZLE_ZERO
:
209 case PIPE_SWIZZLE_ONE
:
217 if (type
.width
>= 16) {
221 LLVMValueRef undef
= LLVMGetUndef(lp_build_elem_type(type
));
222 LLVMTypeRef i32t
= LLVMInt32Type();
223 LLVMValueRef shuffles
[LP_MAX_VECTOR_LENGTH
];
224 LLVMValueRef aux
[LP_MAX_VECTOR_LENGTH
];
226 memset(aux
, 0, sizeof aux
);
228 for(j
= 0; j
< n
; j
+= 4) {
229 for(i
= 0; i
< 4; ++i
) {
231 switch (swizzles
[i
]) {
235 case PIPE_SWIZZLE_RED
:
236 case PIPE_SWIZZLE_GREEN
:
237 case PIPE_SWIZZLE_BLUE
:
238 case PIPE_SWIZZLE_ALPHA
:
239 shuffle
= j
+ swizzles
[i
];
241 case PIPE_SWIZZLE_ZERO
:
242 shuffle
= type
.length
+ 0;
244 aux
[0] = lp_build_const_elem(type
, 0.0);
247 case PIPE_SWIZZLE_ONE
:
248 shuffle
= type
.length
+ 1;
250 aux
[1] = lp_build_const_elem(type
, 1.0);
254 shuffles
[j
+ i
] = LLVMConstInt(i32t
, shuffle
, 0);
258 for (i
= 0; i
< n
; ++i
) {
264 return LLVMBuildShuffleVector(bld
->builder
, a
,
265 LLVMConstVector(aux
, n
),
266 LLVMConstVector(shuffles
, n
), "");
269 * Bit mask and shifts.
271 * For example, this will convert BGRA to RGBA by doing
273 * rgba = (bgra & 0x00ff0000) >> 16
274 * | (bgra & 0xff00ff00)
275 * | (bgra & 0x000000ff) << 16
277 * This is necessary not only for faster cause, but because X86 backend
278 * will refuse shuffles of <4 x i8> vectors
281 struct lp_type type4
;
287 * Start with a mixture of 1 and 0.
289 for (chan
= 0; chan
< 4; ++chan
) {
290 if (swizzles
[chan
] == PIPE_SWIZZLE_ONE
) {
294 res
= lp_build_select_aos(bld
, cond
, bld
->one
, bld
->zero
);
297 * Build a type where each element is an integer that cover the four
301 type4
.floating
= FALSE
;
305 a
= LLVMBuildBitCast(bld
->builder
, a
, lp_build_vec_type(type4
), "");
306 res
= LLVMBuildBitCast(bld
->builder
, res
, lp_build_vec_type(type4
), "");
309 * Mask and shift the channels, trying to group as many channels in the
310 * same shift as possible
312 for (shift
= -3; shift
<= 3; ++shift
) {
313 unsigned long long mask
= 0;
315 assert(type4
.width
<= sizeof(mask
)*8);
317 for (chan
= 0; chan
< 4; ++chan
) {
318 /* FIXME: big endian */
319 if (swizzles
[chan
] < 4 &&
320 chan
- swizzles
[chan
] == shift
) {
321 mask
|= ((1ULL << type
.width
) - 1) << (swizzles
[chan
] * type
.width
);
327 LLVMValueRef shifted
;
330 debug_printf("shift = %i, mask = 0x%08llx\n", shift
, mask
);
332 masked
= LLVMBuildAnd(bld
->builder
, a
,
333 lp_build_const_int_vec(type4
, mask
), "");
335 shifted
= LLVMBuildShl(bld
->builder
, masked
,
336 lp_build_const_int_vec(type4
, shift
*type
.width
), "");
337 } else if (shift
< 0) {
338 shifted
= LLVMBuildLShr(bld
->builder
, masked
,
339 lp_build_const_int_vec(type4
, -shift
*type
.width
), "");
344 res
= LLVMBuildOr(bld
->builder
, res
, shifted
, "");
348 return LLVMBuildBitCast(bld
->builder
, res
, lp_build_vec_type(type
), "");
354 * Extended swizzle of a single channel of a SoA vector.
356 * @param bld building context
357 * @param unswizzled array with the 4 unswizzled values
358 * @param swizzle one of the PIPE_SWIZZLE_*
360 * @return the swizzled value.
363 lp_build_swizzle_soa_channel(struct lp_build_context
*bld
,
364 const LLVMValueRef
*unswizzled
,
368 case PIPE_SWIZZLE_RED
:
369 case PIPE_SWIZZLE_GREEN
:
370 case PIPE_SWIZZLE_BLUE
:
371 case PIPE_SWIZZLE_ALPHA
:
372 return unswizzled
[swizzle
];
373 case PIPE_SWIZZLE_ZERO
:
375 case PIPE_SWIZZLE_ONE
:
385 * Extended swizzle of a SoA vector.
387 * @param bld building context
388 * @param unswizzled array with the 4 unswizzled values
389 * @param swizzles array of PIPE_SWIZZLE_*
390 * @param swizzled output swizzled values
393 lp_build_swizzle_soa(struct lp_build_context
*bld
,
394 const LLVMValueRef
*unswizzled
,
395 const unsigned char swizzles
[4],
396 LLVMValueRef
*swizzled
)
400 for (chan
= 0; chan
< 4; ++chan
) {
401 swizzled
[chan
] = lp_build_swizzle_soa_channel(bld
, unswizzled
,
408 * Do an extended swizzle of a SoA vector inplace.
410 * @param bld building context
411 * @param values intput/output array with the 4 values
412 * @param swizzles array of PIPE_SWIZZLE_*
415 lp_build_swizzle_soa_inplace(struct lp_build_context
*bld
,
416 LLVMValueRef
*values
,
417 const unsigned char swizzles
[4])
419 LLVMValueRef unswizzled
[4];
422 for (chan
= 0; chan
< 4; ++chan
) {
423 unswizzled
[chan
] = values
[chan
];
426 lp_build_swizzle_soa(bld
, unswizzled
, swizzles
, values
);