gallium: add target-helpers/wrap_screen.c to C_SOURCES
[mesa/mesa-lb.git] / src / gallium / auxiliary / gallivm / lp_bld_swizzle.c
blob278c838eaca5fb63a4c531ebe1a379b5cfae68bb
1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
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
16 * of the Software.
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 **************************************************************************/
28 /**
29 * @file
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"
44 LLVMValueRef
45 lp_build_broadcast(LLVMBuilderRef builder,
46 LLVMTypeRef vec_type,
47 LLVMValueRef scalar)
49 const unsigned n = LLVMGetVectorSize(vec_type);
50 LLVMValueRef res;
51 unsigned i;
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, "");
59 return res;
63 LLVMValueRef
64 lp_build_broadcast_scalar(struct lp_build_context *bld,
65 LLVMValueRef scalar)
67 const struct lp_type type = bld->type;
68 LLVMValueRef res;
69 unsigned i;
71 res = bld->undef;
72 for(i = 0; i < type.length; ++i) {
73 LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), i, 0);
74 res = LLVMBuildInsertElement(bld->builder, res, scalar, index, "");
77 return res;
81 LLVMValueRef
82 lp_build_broadcast_aos(struct lp_build_context *bld,
83 LLVMValueRef a,
84 unsigned channel)
86 const struct lp_type type = bld->type;
87 const unsigned n = type.length;
88 unsigned i, j;
90 if(a == bld->undef || a == bld->zero || a == bld->one)
91 return a;
93 /* XXX: SSE3 has PSHUFB which should be better than bitmasks, but forcing
94 * using shuffles here actually causes worst results. More investigation is
95 * needed. */
96 if (n <= 4) {
98 * Shuffle.
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), "");
109 else {
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] = {
120 { 1, 2},
121 {-1, 2},
122 { 1, -2},
123 {-1, -2}
125 boolean cond[4];
126 unsigned i;
128 memset(cond, 0, sizeof cond);
129 cond[channel] = 1;
131 a = LLVMBuildAnd(bld->builder, a, lp_build_const_mask_aos(type, cond), "");
133 type4.width *= 4;
134 type4.length /= 4;
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
143 shift = -shift;
144 #endif
146 if(shift > 0)
147 tmp = LLVMBuildLShr(bld->builder, a, lp_build_const_int_vec(type4, shift*type.width), "");
148 if(shift < 0)
149 tmp = LLVMBuildShl(bld->builder, a, lp_build_const_int_vec(type4, -shift*type.width), "");
151 assert(tmp);
152 if(tmp)
153 a = LLVMBuildOr(bld->builder, a, tmp, "");
156 return LLVMBuildBitCast(bld->builder, a, lp_build_vec_type(type), "");
161 LLVMValueRef
162 lp_build_swizzle1_aos(struct lp_build_context *bld,
163 LLVMValueRef a,
164 const unsigned char swizzle[4])
166 const unsigned n = bld->type.length;
167 unsigned i, j;
169 if(a == bld->undef || a == bld->zero || a == bld->one)
170 return a;
172 if(swizzle[0] == swizzle[1] && swizzle[1] == swizzle[2] && swizzle[2] == swizzle[3])
173 return lp_build_broadcast_aos(bld, a, swizzle[0]);
177 * Shuffle.
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), "");
191 LLVMValueRef
192 lp_build_swizzle2_aos(struct lp_build_context *bld,
193 LLVMValueRef a,
194 LLVMValueRef b,
195 const unsigned char swizzle[4])
197 const unsigned n = bld->type.length;
198 unsigned i, j;
200 if(swizzle[0] < 4 && swizzle[1] < 4 && swizzle[2] < 4 && swizzle[3] < 4)
201 return lp_build_swizzle1_aos(bld, a, swizzle);
203 if(a == b) {
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) {
216 boolean cond[4];
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);
226 * Shuffle.
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), "");