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 **************************************************************************/
31 * Helper functions for constant building.
33 * @author Jose Fonseca <jfonseca@vmware.com>
38 #include "util/u_debug.h"
40 #include "lp_bld_type.h"
41 #include "lp_bld_const.h"
45 lp_mantissa(struct lp_type type
)
47 assert(type
.floating
);
62 return type
.width
- 1;
72 * Same as lp_const_scale(), but in terms of shifts.
75 lp_const_shift(struct lp_type type
)
82 return type
.sign
? type
.width
- 1 : type
.width
;
89 lp_const_offset(struct lp_type type
)
91 if(type
.floating
|| type
.fixed
)
101 * Scaling factor between the LLVM native value and its interpretation.
103 * This is 1.0 for all floating types and unnormalized integers, and something
104 * else for the fixed points types and normalized integers.
107 lp_const_scale(struct lp_type type
)
109 unsigned long long llscale
;
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
);
122 * Minimum value representable by the type.
125 lp_const_min(struct lp_type type
)
148 /* FIXME: consider the fractional bits? */
149 bits
= type
.width
/ 2 - 1;
151 bits
= type
.width
- 1;
153 return (double)-((long long)1 << bits
);
158 * Maximum value representable by the type.
161 lp_const_max(struct lp_type type
)
181 bits
= type
.width
/ 2;
188 return (double)(((unsigned long long)1 << bits
) - 1);
193 lp_const_eps(struct lp_type type
)
207 double scale
= lp_const_scale(type
);
214 lp_build_undef(struct lp_type type
)
216 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
217 return LLVMGetUndef(vec_type
);
222 lp_build_zero(struct lp_type type
)
224 if (type
.length
== 1) {
226 return LLVMConstReal(LLVMFloatType(), 0.0);
228 return LLVMConstInt(LLVMIntType(type
.width
), 0, 0);
231 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
232 return LLVMConstNull(vec_type
);
238 lp_build_one(struct lp_type type
)
240 LLVMTypeRef elem_type
;
241 LLVMValueRef elems
[LP_MAX_VECTOR_LENGTH
];
244 assert(type
.length
<= LP_MAX_VECTOR_LENGTH
);
246 elem_type
= lp_build_elem_type(type
);
249 elems
[0] = LLVMConstReal(elem_type
, 1.0);
251 elems
[0] = LLVMConstInt(elem_type
, 1LL << (type
.width
/2), 0);
253 elems
[0] = LLVMConstInt(elem_type
, 1, 0);
255 elems
[0] = LLVMConstInt(elem_type
, (1LL << (type
.width
- 1)) - 1, 0);
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
);
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));
272 for(i
= 1; i
< type
.length
; ++i
)
275 if (type
.length
== 1)
278 return LLVMConstVector(elems
, type
.length
);
283 * Build constant-valued element from a scalar value.
286 lp_build_const_elem(struct lp_type type
,
289 LLVMTypeRef elem_type
= lp_build_elem_type(type
);
293 elem
= LLVMConstReal(elem_type
, val
);
296 double dscale
= lp_const_scale(type
);
298 elem
= LLVMConstInt(elem_type
, val
*dscale
+ 0.5, 0);
306 * Build constant-valued vector from a scalar value.
309 lp_build_const_vec(struct lp_type type
,
312 if (type
.length
== 1) {
313 return lp_build_const_elem(type
, val
);
315 LLVMValueRef elems
[LP_MAX_VECTOR_LENGTH
];
317 elems
[0] = lp_build_const_elem(type
, val
);
318 for(i
= 1; i
< type
.length
; ++i
)
320 return LLVMConstVector(elems
, type
.length
);
326 lp_build_const_int_vec(struct lp_type type
,
329 LLVMTypeRef elem_type
= lp_build_int_elem_type(type
);
330 LLVMValueRef elems
[LP_MAX_VECTOR_LENGTH
];
333 assert(type
.length
<= LP_MAX_VECTOR_LENGTH
);
335 for(i
= 0; i
< type
.length
; ++i
)
336 elems
[i
] = LLVMConstInt(elem_type
, val
, type
.sign
? 1 : 0);
338 if (type
.length
== 1)
341 return LLVMConstVector(elems
, type
.length
);
346 lp_build_const_aos(struct lp_type type
,
347 double r
, double g
, double b
, double a
,
348 const unsigned char *swizzle
)
350 const unsigned char default_swizzle
[4] = {0, 1, 2, 3};
351 LLVMTypeRef elem_type
;
352 LLVMValueRef elems
[LP_MAX_VECTOR_LENGTH
];
355 assert(type
.length
% 4 == 0);
356 assert(type
.length
<= LP_MAX_VECTOR_LENGTH
);
358 elem_type
= lp_build_elem_type(type
);
361 swizzle
= default_swizzle
;
364 elems
[swizzle
[0]] = LLVMConstReal(elem_type
, r
);
365 elems
[swizzle
[1]] = LLVMConstReal(elem_type
, g
);
366 elems
[swizzle
[2]] = LLVMConstReal(elem_type
, b
);
367 elems
[swizzle
[3]] = LLVMConstReal(elem_type
, a
);
370 double dscale
= lp_const_scale(type
);
372 elems
[swizzle
[0]] = LLVMConstInt(elem_type
, r
*dscale
+ 0.5, 0);
373 elems
[swizzle
[1]] = LLVMConstInt(elem_type
, g
*dscale
+ 0.5, 0);
374 elems
[swizzle
[2]] = LLVMConstInt(elem_type
, b
*dscale
+ 0.5, 0);
375 elems
[swizzle
[3]] = LLVMConstInt(elem_type
, a
*dscale
+ 0.5, 0);
378 for(i
= 4; i
< type
.length
; ++i
)
379 elems
[i
] = elems
[i
% 4];
381 return LLVMConstVector(elems
, type
.length
);
386 * @param mask TGSI_WRITEMASK_xxx
389 lp_build_const_mask_aos(struct lp_type type
,
392 LLVMTypeRef elem_type
= LLVMIntType(type
.width
);
393 LLVMValueRef masks
[LP_MAX_VECTOR_LENGTH
];
396 assert(type
.length
<= LP_MAX_VECTOR_LENGTH
);
398 for (j
= 0; j
< type
.length
; j
+= 4) {
399 for( i
= 0; i
< 4; ++i
) {
400 masks
[j
+ i
] = LLVMConstInt(elem_type
,
401 mask
& (1 << i
) ? ~0ULL : 0,
406 return LLVMConstVector(masks
, type
.length
);