llvmpipe: Optimize blend swizzles by using bitmasks instead of shuffles for ubytes.
[mesa.git] / src / gallium / drivers / llvmpipe / lp_bld_const.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
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.
17 *
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.
25 *
26 **************************************************************************/
27
28
29 /**
30 * @file
31 * Helper functions for constant building.
32 *
33 * @author Jose Fonseca <jfonseca@vmware.com>
34 */
35
36
37 #include "util/u_debug.h"
38
39 #include "lp_bld_type.h"
40 #include "lp_bld_const.h"
41
42
43 LLVMValueRef
44 lp_build_undef(union lp_type type)
45 {
46 LLVMTypeRef vec_type = lp_build_vec_type(type);
47 return LLVMGetUndef(vec_type);
48 }
49
50
51 LLVMValueRef
52 lp_build_zero(union lp_type type)
53 {
54 LLVMTypeRef vec_type = lp_build_vec_type(type);
55 return LLVMConstNull(vec_type);
56 }
57
58
59 LLVMValueRef
60 lp_build_one(union lp_type type)
61 {
62 LLVMTypeRef elem_type;
63 LLVMValueRef elems[LP_MAX_VECTOR_LENGTH];
64 unsigned i;
65
66 assert(type.length <= LP_MAX_VECTOR_LENGTH);
67
68 elem_type = lp_build_elem_type(type);
69
70 if(type.floating)
71 elems[0] = LLVMConstReal(elem_type, 1.0);
72 else if(type.fixed)
73 elems[0] = LLVMConstInt(elem_type, 1LL << (type.width/2), 0);
74 else if(!type.norm)
75 elems[0] = LLVMConstInt(elem_type, 1, 0);
76 else {
77 /* special case' -- 1.0 for normalized types is more easily attained if
78 * we start with a vector consisting of all bits set */
79 LLVMTypeRef vec_type = LLVMVectorType(elem_type, type.length);
80 LLVMValueRef vec = LLVMConstAllOnes(vec_type);
81
82 if(type.sign)
83 vec = LLVMConstLShr(vec, LLVMConstInt(LLVMInt32Type(), 1, 0));
84
85 return vec;
86 }
87
88 for(i = 1; i < type.length; ++i)
89 elems[i] = elems[0];
90
91 return LLVMConstVector(elems, type.length);
92 }
93
94
95 LLVMValueRef
96 lp_build_const_aos(union lp_type type,
97 double r, double g, double b, double a,
98 const unsigned char *swizzle)
99 {
100 const unsigned char default_swizzle[4] = {0, 1, 2, 3};
101 LLVMTypeRef elem_type;
102 LLVMValueRef elems[LP_MAX_VECTOR_LENGTH];
103 unsigned i;
104
105 assert(type.length % 4 == 0);
106 assert(type.length <= LP_MAX_VECTOR_LENGTH);
107
108 elem_type = lp_build_elem_type(type);
109
110 if(swizzle == NULL)
111 swizzle = default_swizzle;
112
113 if(type.floating) {
114 elems[swizzle[0]] = LLVMConstReal(elem_type, r);
115 elems[swizzle[1]] = LLVMConstReal(elem_type, g);
116 elems[swizzle[2]] = LLVMConstReal(elem_type, b);
117 elems[swizzle[3]] = LLVMConstReal(elem_type, a);
118 }
119 else {
120 unsigned shift;
121 long long llscale;
122 double dscale;
123
124 if(type.fixed)
125 shift = type.width/2;
126 else if(type.norm)
127 shift = type.sign ? type.width - 1 : type.width;
128 else
129 shift = 0;
130
131 llscale = (long long)1 << shift;
132 dscale = (double)llscale;
133 assert((long long)dscale == llscale);
134
135 elems[swizzle[0]] = LLVMConstInt(elem_type, r*dscale + 0.5, 0);
136 elems[swizzle[1]] = LLVMConstInt(elem_type, g*dscale + 0.5, 0);
137 elems[swizzle[2]] = LLVMConstInt(elem_type, b*dscale + 0.5, 0);
138 elems[swizzle[3]] = LLVMConstInt(elem_type, a*dscale + 0.5, 0);
139 }
140
141 for(i = 4; i < type.length; ++i)
142 elems[i] = elems[i % 4];
143
144 return LLVMConstVector(elems, type.length);
145 }
146
147
148 LLVMValueRef
149 lp_build_const_shift(union lp_type type,
150 int c)
151 {
152 LLVMTypeRef elem_type = LLVMIntType(type.width);
153 LLVMValueRef elems[LP_MAX_VECTOR_LENGTH];
154 unsigned i;
155
156 assert(type.length <= LP_MAX_VECTOR_LENGTH);
157
158 for(i = 0; i < type.length; ++i)
159 elems[i] = LLVMConstInt(elem_type, c, 0);
160
161 return LLVMConstVector(elems, type.length);
162 }
163
164
165 LLVMValueRef
166 lp_build_const_mask_aos(union lp_type type,
167 boolean cond[4])
168 {
169 LLVMTypeRef elem_type = LLVMIntType(type.width);
170 LLVMValueRef masks[LP_MAX_VECTOR_LENGTH];
171 unsigned i, j;
172
173 assert(type.length <= LP_MAX_VECTOR_LENGTH);
174
175 for(j = 0; j < type.length; j += 4)
176 for(i = 0; i < 4; ++i)
177 masks[j + i] = LLVMConstInt(elem_type, cond[i] ? ~0 : 0, 0);
178
179 return LLVMConstVector(masks, type.length);
180 }