llvmpipe: Make lp_const_offset visible.
[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 /**
44 * Shift of the unity.
45 *
46 * Same as lp_const_scale(), but in terms of shifts.
47 */
48 unsigned
49 lp_const_shift(union lp_type type)
50 {
51 if(type.floating)
52 return 0;
53 else if(type.fixed)
54 return type.width/2;
55 else if(type.norm)
56 return type.sign ? type.width - 1 : type.width;
57 else
58 return 0;
59 }
60
61
62 unsigned
63 lp_const_offset(union lp_type type)
64 {
65 if(type.floating || type.fixed)
66 return 0;
67 else if(type.norm)
68 return 1;
69 else
70 return 0;
71 }
72
73
74 /**
75 * Scaling factor between the LLVM native value and its interpretation.
76 *
77 * This is 1.0 for all floating types and unnormalized integers, and something
78 * else for the fixed points types and normalized integers.
79 */
80 double
81 lp_const_scale(union lp_type type)
82 {
83 unsigned long long llscale;
84 double dscale;
85
86 llscale = (unsigned long long)1 << lp_const_shift(type);
87 llscale -= lp_const_offset(type);
88 dscale = (double)llscale;
89 assert((unsigned long long)dscale == llscale);
90
91 return dscale;
92 }
93
94
95 LLVMValueRef
96 lp_build_undef(union lp_type type)
97 {
98 LLVMTypeRef vec_type = lp_build_vec_type(type);
99 return LLVMGetUndef(vec_type);
100 }
101
102
103 LLVMValueRef
104 lp_build_zero(union lp_type type)
105 {
106 LLVMTypeRef vec_type = lp_build_vec_type(type);
107 return LLVMConstNull(vec_type);
108 }
109
110
111 LLVMValueRef
112 lp_build_one(union lp_type type)
113 {
114 LLVMTypeRef elem_type;
115 LLVMValueRef elems[LP_MAX_VECTOR_LENGTH];
116 unsigned i;
117
118 assert(type.length <= LP_MAX_VECTOR_LENGTH);
119
120 elem_type = lp_build_elem_type(type);
121
122 if(type.floating)
123 elems[0] = LLVMConstReal(elem_type, 1.0);
124 else if(type.fixed)
125 elems[0] = LLVMConstInt(elem_type, 1LL << (type.width/2), 0);
126 else if(!type.norm)
127 elems[0] = LLVMConstInt(elem_type, 1, 0);
128 else {
129 /* special case' -- 1.0 for normalized types is more easily attained if
130 * we start with a vector consisting of all bits set */
131 LLVMTypeRef vec_type = LLVMVectorType(elem_type, type.length);
132 LLVMValueRef vec = LLVMConstAllOnes(vec_type);
133
134 if(type.sign)
135 vec = LLVMConstLShr(vec, LLVMConstInt(LLVMInt32Type(), 1, 0));
136
137 return vec;
138 }
139
140 for(i = 1; i < type.length; ++i)
141 elems[i] = elems[0];
142
143 return LLVMConstVector(elems, type.length);
144 }
145
146
147 LLVMValueRef
148 lp_build_const_uni(union lp_type type,
149 double val)
150 {
151 LLVMTypeRef elem_type = lp_build_elem_type(type);
152 LLVMValueRef elems[LP_MAX_VECTOR_LENGTH];
153 unsigned i;
154
155 assert(type.length <= LP_MAX_VECTOR_LENGTH);
156
157 if(type.floating) {
158 elems[0] = LLVMConstReal(elem_type, val);
159 }
160 else {
161 double dscale = lp_const_scale(type);
162
163 elems[0] = LLVMConstInt(elem_type, val*dscale + 0.5, 0);
164 }
165
166 for(i = 1; i < type.length; ++i)
167 elems[i] = elems[0];
168
169 return LLVMConstVector(elems, type.length);
170 }
171
172
173 LLVMValueRef
174 lp_build_int_const_uni(union lp_type type,
175 long long val)
176 {
177 LLVMTypeRef elem_type = lp_build_int_elem_type(type);
178 LLVMValueRef elems[LP_MAX_VECTOR_LENGTH];
179 unsigned i;
180
181 assert(type.length <= LP_MAX_VECTOR_LENGTH);
182
183 for(i = 0; i < type.length; ++i)
184 elems[i] = LLVMConstInt(elem_type, val, type.sign ? 1 : 0);
185
186 return LLVMConstVector(elems, type.length);
187 }
188
189
190 LLVMValueRef
191 lp_build_const_aos(union lp_type type,
192 double r, double g, double b, double a,
193 const unsigned char *swizzle)
194 {
195 const unsigned char default_swizzle[4] = {0, 1, 2, 3};
196 LLVMTypeRef elem_type;
197 LLVMValueRef elems[LP_MAX_VECTOR_LENGTH];
198 unsigned i;
199
200 assert(type.length % 4 == 0);
201 assert(type.length <= LP_MAX_VECTOR_LENGTH);
202
203 elem_type = lp_build_elem_type(type);
204
205 if(swizzle == NULL)
206 swizzle = default_swizzle;
207
208 if(type.floating) {
209 elems[swizzle[0]] = LLVMConstReal(elem_type, r);
210 elems[swizzle[1]] = LLVMConstReal(elem_type, g);
211 elems[swizzle[2]] = LLVMConstReal(elem_type, b);
212 elems[swizzle[3]] = LLVMConstReal(elem_type, a);
213 }
214 else {
215 double dscale = lp_const_scale(type);
216
217 elems[swizzle[0]] = LLVMConstInt(elem_type, r*dscale + 0.5, 0);
218 elems[swizzle[1]] = LLVMConstInt(elem_type, g*dscale + 0.5, 0);
219 elems[swizzle[2]] = LLVMConstInt(elem_type, b*dscale + 0.5, 0);
220 elems[swizzle[3]] = LLVMConstInt(elem_type, a*dscale + 0.5, 0);
221 }
222
223 for(i = 4; i < type.length; ++i)
224 elems[i] = elems[i % 4];
225
226 return LLVMConstVector(elems, type.length);
227 }
228
229
230 LLVMValueRef
231 lp_build_const_mask_aos(union lp_type type,
232 boolean cond[4])
233 {
234 LLVMTypeRef elem_type = LLVMIntType(type.width);
235 LLVMValueRef masks[LP_MAX_VECTOR_LENGTH];
236 unsigned i, j;
237
238 assert(type.length <= LP_MAX_VECTOR_LENGTH);
239
240 for(j = 0; j < type.length; j += 4)
241 for(i = 0; i < 4; ++i)
242 masks[j + i] = LLVMConstInt(elem_type, cond[i] ? ~0 : 0, 0);
243
244 return LLVMConstVector(masks, type.length);
245 }