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
, "");
64 lp_build_broadcast_scalar(struct lp_build_context
*bld
,
67 const struct lp_type type
= bld
->type
;
72 for(i
= 0; i
< type
.length
; ++i
) {
73 LLVMValueRef index
= LLVMConstInt(LLVMInt32Type(), i
, 0);
74 res
= LLVMBuildInsertElement(bld
->builder
, res
, scalar
, index
, "");
82 lp_build_broadcast_aos(struct lp_build_context
*bld
,
86 const struct lp_type type
= bld
->type
;
87 const unsigned n
= type
.length
;
90 if(a
== bld
->undef
|| a
== bld
->zero
|| a
== bld
->one
)
93 /* XXX: SSE3 has PSHUFB which should be better than bitmasks, but forcing
94 * using shuffles here actually causes worst results. More investigation is
100 LLVMTypeRef elem_type
= LLVMInt32Type();
101 LLVMValueRef shuffles
[LP_MAX_VECTOR_LENGTH
];
103 for(j
= 0; j
< n
; j
+= 4)
104 for(i
= 0; i
< 4; ++i
)
105 shuffles
[j
+ i
] = LLVMConstInt(elem_type
, j
+ channel
, 0);
107 return LLVMBuildShuffleVector(bld
->builder
, a
, bld
->undef
, LLVMConstVector(shuffles
, n
), "");
111 * Bit mask and recursive shifts
113 * XYZW XYZW .... XYZW <= input
114 * 0Y00 0Y00 .... 0Y00
115 * YY00 YY00 .... YY00
116 * YYYY YYYY .... YYYY <= output
118 struct lp_type type4
= type
;
119 const char shifts
[4][2] = {
128 memset(cond
, 0, sizeof cond
);
131 a
= LLVMBuildAnd(bld
->builder
, a
, lp_build_const_mask_aos(type
, cond
), "");
136 a
= LLVMBuildBitCast(bld
->builder
, a
, lp_build_vec_type(type4
), "");
138 for(i
= 0; i
< 2; ++i
) {
139 LLVMValueRef tmp
= NULL
;
140 int shift
= shifts
[channel
][i
];
142 #ifdef PIPE_ARCH_LITTLE_ENDIAN
147 tmp
= LLVMBuildLShr(bld
->builder
, a
, lp_build_const_int_vec(type4
, shift
*type
.width
), "");
149 tmp
= LLVMBuildShl(bld
->builder
, a
, lp_build_const_int_vec(type4
, -shift
*type
.width
), "");
153 a
= LLVMBuildOr(bld
->builder
, a
, tmp
, "");
156 return LLVMBuildBitCast(bld
->builder
, a
, lp_build_vec_type(type
), "");
162 lp_build_swizzle1_aos(struct lp_build_context
*bld
,
164 const unsigned char swizzle
[4])
166 const unsigned n
= bld
->type
.length
;
169 if(a
== bld
->undef
|| a
== bld
->zero
|| a
== bld
->one
)
172 if(swizzle
[0] == swizzle
[1] && swizzle
[1] == swizzle
[2] && swizzle
[2] == swizzle
[3])
173 return lp_build_broadcast_aos(bld
, a
, swizzle
[0]);
179 LLVMTypeRef elem_type
= LLVMInt32Type();
180 LLVMValueRef shuffles
[LP_MAX_VECTOR_LENGTH
];
182 for(j
= 0; j
< n
; j
+= 4)
183 for(i
= 0; i
< 4; ++i
)
184 shuffles
[j
+ i
] = LLVMConstInt(elem_type
, j
+ swizzle
[i
], 0);
186 return LLVMBuildShuffleVector(bld
->builder
, a
, bld
->undef
, LLVMConstVector(shuffles
, n
), "");
192 lp_build_swizzle2_aos(struct lp_build_context
*bld
,
195 const unsigned char swizzle
[4])
197 const unsigned n
= bld
->type
.length
;
200 if(swizzle
[0] < 4 && swizzle
[1] < 4 && swizzle
[2] < 4 && swizzle
[3] < 4)
201 return lp_build_swizzle1_aos(bld
, a
, swizzle
);
204 unsigned char swizzle1
[4];
205 swizzle1
[0] = swizzle
[0] % 4;
206 swizzle1
[1] = swizzle
[1] % 4;
207 swizzle1
[2] = swizzle
[2] % 4;
208 swizzle1
[3] = swizzle
[3] % 4;
209 return lp_build_swizzle1_aos(bld
, a
, swizzle1
);
212 if(swizzle
[0] % 4 == 0 &&
213 swizzle
[1] % 4 == 1 &&
214 swizzle
[2] % 4 == 2 &&
215 swizzle
[3] % 4 == 3) {
217 cond
[0] = swizzle
[0] / 4;
218 cond
[1] = swizzle
[1] / 4;
219 cond
[2] = swizzle
[2] / 4;
220 cond
[3] = swizzle
[3] / 4;
221 return lp_build_select_aos(bld
, a
, b
, cond
);
228 LLVMTypeRef elem_type
= LLVMInt32Type();
229 LLVMValueRef shuffles
[LP_MAX_VECTOR_LENGTH
];
231 for(j
= 0; j
< n
; j
+= 4)
232 for(i
= 0; i
< 4; ++i
)
233 shuffles
[j
+ i
] = LLVMConstInt(elem_type
, j
+ (swizzle
[i
] % 4) + (swizzle
[i
] / 4 * n
), 0);
235 return LLVMBuildShuffleVector(bld
->builder
, a
, b
, LLVMConstVector(shuffles
, n
), "");