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
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
= type
;
136 const char shifts
[4][2] = {
145 memset(cond
, 0, sizeof cond
);
148 a
= LLVMBuildAnd(bld
->builder
, a
, lp_build_const_mask_aos(type
, cond
), "");
153 a
= LLVMBuildBitCast(bld
->builder
, a
, lp_build_vec_type(type4
), "");
155 for(i
= 0; i
< 2; ++i
) {
156 LLVMValueRef tmp
= NULL
;
157 int shift
= shifts
[channel
][i
];
159 #ifdef PIPE_ARCH_LITTLE_ENDIAN
164 tmp
= LLVMBuildLShr(bld
->builder
, a
, lp_build_const_int_vec(type4
, shift
*type
.width
), "");
166 tmp
= LLVMBuildShl(bld
->builder
, a
, lp_build_const_int_vec(type4
, -shift
*type
.width
), "");
170 a
= LLVMBuildOr(bld
->builder
, a
, tmp
, "");
173 return LLVMBuildBitCast(bld
->builder
, a
, lp_build_vec_type(type
), "");
179 lp_build_swizzle1_aos(struct lp_build_context
*bld
,
181 const unsigned char swizzle
[4])
183 const unsigned n
= bld
->type
.length
;
186 if(a
== bld
->undef
|| a
== bld
->zero
|| a
== bld
->one
)
189 if(swizzle
[0] == swizzle
[1] && swizzle
[1] == swizzle
[2] && swizzle
[2] == swizzle
[3])
190 return lp_build_broadcast_aos(bld
, a
, swizzle
[0]);
196 LLVMTypeRef elem_type
= LLVMInt32Type();
197 LLVMValueRef shuffles
[LP_MAX_VECTOR_LENGTH
];
199 for(j
= 0; j
< n
; j
+= 4)
200 for(i
= 0; i
< 4; ++i
)
201 shuffles
[j
+ i
] = LLVMConstInt(elem_type
, j
+ swizzle
[i
], 0);
203 return LLVMBuildShuffleVector(bld
->builder
, a
, bld
->undef
, LLVMConstVector(shuffles
, n
), "");
209 lp_build_swizzle2_aos(struct lp_build_context
*bld
,
212 const unsigned char swizzle
[4])
214 const unsigned n
= bld
->type
.length
;
217 if(swizzle
[0] < 4 && swizzle
[1] < 4 && swizzle
[2] < 4 && swizzle
[3] < 4)
218 return lp_build_swizzle1_aos(bld
, a
, swizzle
);
221 unsigned char swizzle1
[4];
222 swizzle1
[0] = swizzle
[0] % 4;
223 swizzle1
[1] = swizzle
[1] % 4;
224 swizzle1
[2] = swizzle
[2] % 4;
225 swizzle1
[3] = swizzle
[3] % 4;
226 return lp_build_swizzle1_aos(bld
, a
, swizzle1
);
229 if(swizzle
[0] % 4 == 0 &&
230 swizzle
[1] % 4 == 1 &&
231 swizzle
[2] % 4 == 2 &&
232 swizzle
[3] % 4 == 3) {
234 cond
[0] = swizzle
[0] / 4;
235 cond
[1] = swizzle
[1] / 4;
236 cond
[2] = swizzle
[2] / 4;
237 cond
[3] = swizzle
[3] / 4;
238 return lp_build_select_aos(bld
, a
, b
, cond
);
245 LLVMTypeRef elem_type
= LLVMInt32Type();
246 LLVMValueRef shuffles
[LP_MAX_VECTOR_LENGTH
];
248 for(j
= 0; j
< n
; j
+= 4)
249 for(i
= 0; i
< 4; ++i
)
250 shuffles
[j
+ i
] = LLVMConstInt(elem_type
, j
+ (swizzle
[i
] % 4) + (swizzle
[i
] / 4 * n
), 0);
252 return LLVMBuildShuffleVector(bld
->builder
, a
, b
, LLVMConstVector(shuffles
, n
), "");
258 * Extended swizzle of a single channel of a SoA vector.
260 * @param bld building context
261 * @param unswizzled array with the 4 unswizzled values
262 * @param swizzle one of the PIPE_SWIZZLE_*
264 * @return the swizzled value.
267 lp_build_swizzle_soa_channel(struct lp_build_context
*bld
,
268 const LLVMValueRef
*unswizzled
,
272 case PIPE_SWIZZLE_RED
:
273 case PIPE_SWIZZLE_GREEN
:
274 case PIPE_SWIZZLE_BLUE
:
275 case PIPE_SWIZZLE_ALPHA
:
276 return unswizzled
[swizzle
];
277 case PIPE_SWIZZLE_ZERO
:
279 case PIPE_SWIZZLE_ONE
:
289 * Extended swizzle of a SoA vector.
291 * @param bld building context
292 * @param unswizzled array with the 4 unswizzled values
293 * @param swizzles array of PIPE_SWIZZLE_*
294 * @param swizzled output swizzled values
297 lp_build_swizzle_soa(struct lp_build_context
*bld
,
298 const LLVMValueRef
*unswizzled
,
299 const unsigned char swizzles
[4],
300 LLVMValueRef
*swizzled
)
304 for (chan
= 0; chan
< 4; ++chan
) {
305 swizzled
[chan
] = lp_build_swizzle_soa_channel(bld
, unswizzled
,
312 * Do an extended swizzle of a SoA vector inplace.
314 * @param bld building context
315 * @param values intput/output array with the 4 values
316 * @param swizzles array of PIPE_SWIZZLE_*
319 lp_build_swizzle_soa_inplace(struct lp_build_context
*bld
,
320 LLVMValueRef
*values
,
321 const unsigned char swizzles
[4])
323 LLVMValueRef unswizzled
[4];
326 for (chan
= 0; chan
< 4; ++chan
) {
327 unswizzled
[chan
] = values
[chan
];
330 lp_build_swizzle_soa(bld
, unswizzled
, swizzles
, values
);