Merge branch '7.8'
[mesa.git] / src / gallium / auxiliary / gallivm / 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 #include <float.h>
37
38 #include "util/u_debug.h"
39
40 #include "lp_bld_type.h"
41 #include "lp_bld_const.h"
42
43
44 unsigned
45 lp_mantissa(struct lp_type type)
46 {
47 assert(type.floating);
48
49 if(type.floating) {
50 switch(type.width) {
51 case 32:
52 return 23;
53 case 64:
54 return 53;
55 default:
56 assert(0);
57 return 0;
58 }
59 }
60 else {
61 if(type.sign)
62 return type.width - 1;
63 else
64 return type.width;
65 }
66 }
67
68
69 /**
70 * Shift of the unity.
71 *
72 * Same as lp_const_scale(), but in terms of shifts.
73 */
74 unsigned
75 lp_const_shift(struct lp_type type)
76 {
77 if(type.floating)
78 return 0;
79 else if(type.fixed)
80 return type.width/2;
81 else if(type.norm)
82 return type.sign ? type.width - 1 : type.width;
83 else
84 return 0;
85 }
86
87
88 unsigned
89 lp_const_offset(struct lp_type type)
90 {
91 if(type.floating || type.fixed)
92 return 0;
93 else if(type.norm)
94 return 1;
95 else
96 return 0;
97 }
98
99
100 /**
101 * Scaling factor between the LLVM native value and its interpretation.
102 *
103 * This is 1.0 for all floating types and unnormalized integers, and something
104 * else for the fixed points types and normalized integers.
105 */
106 double
107 lp_const_scale(struct lp_type type)
108 {
109 unsigned long long llscale;
110 double dscale;
111
112 llscale = (unsigned long long)1 << lp_const_shift(type);
113 llscale -= lp_const_offset(type);
114 dscale = (double)llscale;
115 assert((unsigned long long)dscale == llscale);
116
117 return dscale;
118 }
119
120
121 /**
122 * Minimum value representable by the type.
123 */
124 double
125 lp_const_min(struct lp_type type)
126 {
127 unsigned bits;
128
129 if(!type.sign)
130 return 0.0;
131
132 if(type.norm)
133 return -1.0;
134
135 if (type.floating) {
136 switch(type.width) {
137 case 32:
138 return -FLT_MAX;
139 case 64:
140 return -DBL_MAX;
141 default:
142 assert(0);
143 return 0.0;
144 }
145 }
146
147 if(type.fixed)
148 /* FIXME: consider the fractional bits? */
149 bits = type.width / 2 - 1;
150 else
151 bits = type.width - 1;
152
153 return (double)-((long long)1 << bits);
154 }
155
156
157 /**
158 * Maximum value representable by the type.
159 */
160 double
161 lp_const_max(struct lp_type type)
162 {
163 unsigned bits;
164
165 if(type.norm)
166 return 1.0;
167
168 if (type.floating) {
169 switch(type.width) {
170 case 32:
171 return FLT_MAX;
172 case 64:
173 return DBL_MAX;
174 default:
175 assert(0);
176 return 0.0;
177 }
178 }
179
180 if(type.fixed)
181 bits = type.width / 2;
182 else
183 bits = type.width;
184
185 if(type.sign)
186 bits -= 1;
187
188 return (double)(((unsigned long long)1 << bits) - 1);
189 }
190
191
192 double
193 lp_const_eps(struct lp_type type)
194 {
195 if (type.floating) {
196 switch(type.width) {
197 case 32:
198 return FLT_EPSILON;
199 case 64:
200 return DBL_EPSILON;
201 default:
202 assert(0);
203 return 0.0;
204 }
205 }
206 else {
207 double scale = lp_const_scale(type);
208 return 1.0/scale;
209 }
210 }
211
212
213 LLVMValueRef
214 lp_build_undef(struct lp_type type)
215 {
216 LLVMTypeRef vec_type = lp_build_vec_type(type);
217 return LLVMGetUndef(vec_type);
218 }
219
220
221 LLVMValueRef
222 lp_build_zero(struct lp_type type)
223 {
224 if (type.length == 1) {
225 if (type.floating)
226 return LLVMConstReal(LLVMFloatType(), 0.0);
227 else
228 return LLVMConstInt(LLVMIntType(type.width), 0, 0);
229 }
230 else {
231 LLVMTypeRef vec_type = lp_build_vec_type(type);
232 return LLVMConstNull(vec_type);
233 }
234 }
235
236
237 LLVMValueRef
238 lp_build_one(struct lp_type type)
239 {
240 LLVMTypeRef elem_type;
241 LLVMValueRef elems[LP_MAX_VECTOR_LENGTH];
242 unsigned i;
243
244 assert(type.length <= LP_MAX_VECTOR_LENGTH);
245
246 elem_type = lp_build_elem_type(type);
247
248 if(type.floating)
249 elems[0] = LLVMConstReal(elem_type, 1.0);
250 else if(type.fixed)
251 elems[0] = LLVMConstInt(elem_type, 1LL << (type.width/2), 0);
252 else if(!type.norm)
253 elems[0] = LLVMConstInt(elem_type, 1, 0);
254 else if(type.sign)
255 elems[0] = LLVMConstInt(elem_type, (1LL << (type.width - 1)) - 1, 0);
256 else {
257 /* special case' -- 1.0 for normalized types is more easily attained if
258 * we start with a vector consisting of all bits set */
259 LLVMTypeRef vec_type = LLVMVectorType(elem_type, type.length);
260 LLVMValueRef vec = LLVMConstAllOnes(vec_type);
261
262 #if 0
263 if(type.sign)
264 /* TODO: Unfortunately this caused "Tried to create a shift operation
265 * on a non-integer type!" */
266 vec = LLVMConstLShr(vec, lp_build_const_int_vec(type, 1));
267 #endif
268
269 return vec;
270 }
271
272 for(i = 1; i < type.length; ++i)
273 elems[i] = elems[0];
274
275 if (type.length == 1)
276 return elems[0];
277 else
278 return LLVMConstVector(elems, type.length);
279 }
280
281
282 /**
283 * Build constant-valued vector from a scalar value.
284 */
285 LLVMValueRef
286 lp_build_const_vec(struct lp_type type,
287 double val)
288 {
289 LLVMTypeRef elem_type = lp_build_elem_type(type);
290 LLVMValueRef elems[LP_MAX_VECTOR_LENGTH];
291 unsigned i;
292
293 assert(type.length <= LP_MAX_VECTOR_LENGTH);
294
295 if(type.floating) {
296 elems[0] = LLVMConstReal(elem_type, val);
297 }
298 else {
299 double dscale = lp_const_scale(type);
300
301 elems[0] = LLVMConstInt(elem_type, val*dscale + 0.5, 0);
302 }
303
304 for(i = 1; i < type.length; ++i)
305 elems[i] = elems[0];
306
307 return LLVMConstVector(elems, type.length);
308 }
309
310
311 LLVMValueRef
312 lp_build_const_int_vec(struct lp_type type,
313 long long val)
314 {
315 LLVMTypeRef elem_type = lp_build_int_elem_type(type);
316 LLVMValueRef elems[LP_MAX_VECTOR_LENGTH];
317 unsigned i;
318
319 assert(type.length <= LP_MAX_VECTOR_LENGTH);
320
321 for(i = 0; i < type.length; ++i)
322 elems[i] = LLVMConstInt(elem_type, val, type.sign ? 1 : 0);
323
324 return LLVMConstVector(elems, type.length);
325 }
326
327
328 LLVMValueRef
329 lp_build_const_aos(struct lp_type type,
330 double r, double g, double b, double a,
331 const unsigned char *swizzle)
332 {
333 const unsigned char default_swizzle[4] = {0, 1, 2, 3};
334 LLVMTypeRef elem_type;
335 LLVMValueRef elems[LP_MAX_VECTOR_LENGTH];
336 unsigned i;
337
338 assert(type.length % 4 == 0);
339 assert(type.length <= LP_MAX_VECTOR_LENGTH);
340
341 elem_type = lp_build_elem_type(type);
342
343 if(swizzle == NULL)
344 swizzle = default_swizzle;
345
346 if(type.floating) {
347 elems[swizzle[0]] = LLVMConstReal(elem_type, r);
348 elems[swizzle[1]] = LLVMConstReal(elem_type, g);
349 elems[swizzle[2]] = LLVMConstReal(elem_type, b);
350 elems[swizzle[3]] = LLVMConstReal(elem_type, a);
351 }
352 else {
353 double dscale = lp_const_scale(type);
354
355 elems[swizzle[0]] = LLVMConstInt(elem_type, r*dscale + 0.5, 0);
356 elems[swizzle[1]] = LLVMConstInt(elem_type, g*dscale + 0.5, 0);
357 elems[swizzle[2]] = LLVMConstInt(elem_type, b*dscale + 0.5, 0);
358 elems[swizzle[3]] = LLVMConstInt(elem_type, a*dscale + 0.5, 0);
359 }
360
361 for(i = 4; i < type.length; ++i)
362 elems[i] = elems[i % 4];
363
364 return LLVMConstVector(elems, type.length);
365 }
366
367
368 LLVMValueRef
369 lp_build_const_mask_aos(struct lp_type type,
370 const boolean cond[4])
371 {
372 LLVMTypeRef elem_type = LLVMIntType(type.width);
373 LLVMValueRef masks[LP_MAX_VECTOR_LENGTH];
374 unsigned i, j;
375
376 assert(type.length <= LP_MAX_VECTOR_LENGTH);
377
378 for(j = 0; j < type.length; j += 4)
379 for(i = 0; i < 4; ++i)
380 masks[j + i] = LLVMConstInt(elem_type, cond[i] ? ~0 : 0, 0);
381
382 return LLVMConstVector(masks, type.length);
383 }