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>
37 #include "util/u_debug.h"
39 #include "lp_bld_type.h"
40 #include "lp_bld_const.h"
46 * Same as lp_const_scale(), but in terms of shifts.
49 lp_const_shift(union lp_type type
)
56 return type
.sign
? type
.width
- 1 : type
.width
;
63 lp_const_offset(union lp_type type
)
65 if(type
.floating
|| type
.fixed
)
75 * Scaling factor between the LLVM native value and its interpretation.
77 * This is 1.0 for all floating types and unnormalized integers, and something
78 * else for the fixed points types and normalized integers.
81 lp_const_scale(union lp_type type
)
83 unsigned long long llscale
;
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
);
96 lp_build_undef(union lp_type type
)
98 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
99 return LLVMGetUndef(vec_type
);
104 lp_build_zero(union lp_type type
)
106 LLVMTypeRef vec_type
= lp_build_vec_type(type
);
107 return LLVMConstNull(vec_type
);
112 lp_build_one(union lp_type type
)
114 LLVMTypeRef elem_type
;
115 LLVMValueRef elems
[LP_MAX_VECTOR_LENGTH
];
118 assert(type
.length
<= LP_MAX_VECTOR_LENGTH
);
120 elem_type
= lp_build_elem_type(type
);
123 elems
[0] = LLVMConstReal(elem_type
, 1.0);
125 elems
[0] = LLVMConstInt(elem_type
, 1LL << (type
.width
/2), 0);
127 elems
[0] = LLVMConstInt(elem_type
, 1, 0);
129 elems
[0] = LLVMConstInt(elem_type
, (1LL << (type
.width
- 1)) - 1, 0);
131 /* special case' -- 1.0 for normalized types is more easily attained if
132 * we start with a vector consisting of all bits set */
133 LLVMTypeRef vec_type
= LLVMVectorType(elem_type
, type
.length
);
134 LLVMValueRef vec
= LLVMConstAllOnes(vec_type
);
138 /* TODO: Unfortunately this caused "Tried to create a shift operation
139 * on a non-integer type!" */
140 vec
= LLVMConstLShr(vec
, lp_build_int_const_uni(type
, 1));
146 for(i
= 1; i
< type
.length
; ++i
)
149 return LLVMConstVector(elems
, type
.length
);
154 lp_build_const_uni(union lp_type type
,
157 LLVMTypeRef elem_type
= lp_build_elem_type(type
);
158 LLVMValueRef elems
[LP_MAX_VECTOR_LENGTH
];
161 assert(type
.length
<= LP_MAX_VECTOR_LENGTH
);
164 elems
[0] = LLVMConstReal(elem_type
, val
);
167 double dscale
= lp_const_scale(type
);
169 elems
[0] = LLVMConstInt(elem_type
, val
*dscale
+ 0.5, 0);
172 for(i
= 1; i
< type
.length
; ++i
)
175 return LLVMConstVector(elems
, type
.length
);
180 lp_build_int_const_uni(union lp_type type
,
183 LLVMTypeRef elem_type
= lp_build_int_elem_type(type
);
184 LLVMValueRef elems
[LP_MAX_VECTOR_LENGTH
];
187 assert(type
.length
<= LP_MAX_VECTOR_LENGTH
);
189 for(i
= 0; i
< type
.length
; ++i
)
190 elems
[i
] = LLVMConstInt(elem_type
, val
, type
.sign
? 1 : 0);
192 return LLVMConstVector(elems
, type
.length
);
197 lp_build_const_aos(union lp_type type
,
198 double r
, double g
, double b
, double a
,
199 const unsigned char *swizzle
)
201 const unsigned char default_swizzle
[4] = {0, 1, 2, 3};
202 LLVMTypeRef elem_type
;
203 LLVMValueRef elems
[LP_MAX_VECTOR_LENGTH
];
206 assert(type
.length
% 4 == 0);
207 assert(type
.length
<= LP_MAX_VECTOR_LENGTH
);
209 elem_type
= lp_build_elem_type(type
);
212 swizzle
= default_swizzle
;
215 elems
[swizzle
[0]] = LLVMConstReal(elem_type
, r
);
216 elems
[swizzle
[1]] = LLVMConstReal(elem_type
, g
);
217 elems
[swizzle
[2]] = LLVMConstReal(elem_type
, b
);
218 elems
[swizzle
[3]] = LLVMConstReal(elem_type
, a
);
221 double dscale
= lp_const_scale(type
);
223 elems
[swizzle
[0]] = LLVMConstInt(elem_type
, r
*dscale
+ 0.5, 0);
224 elems
[swizzle
[1]] = LLVMConstInt(elem_type
, g
*dscale
+ 0.5, 0);
225 elems
[swizzle
[2]] = LLVMConstInt(elem_type
, b
*dscale
+ 0.5, 0);
226 elems
[swizzle
[3]] = LLVMConstInt(elem_type
, a
*dscale
+ 0.5, 0);
229 for(i
= 4; i
< type
.length
; ++i
)
230 elems
[i
] = elems
[i
% 4];
232 return LLVMConstVector(elems
, type
.length
);
237 lp_build_const_mask_aos(union lp_type type
,
240 LLVMTypeRef elem_type
= LLVMIntType(type
.width
);
241 LLVMValueRef masks
[LP_MAX_VECTOR_LENGTH
];
244 assert(type
.length
<= LP_MAX_VECTOR_LENGTH
);
246 for(j
= 0; j
< type
.length
; j
+= 4)
247 for(i
= 0; i
< 4; ++i
)
248 masks
[j
+ i
] = LLVMConstInt(elem_type
, cond
[i
] ? ~0 : 0, 0);
250 return LLVMConstVector(masks
, type
.length
);