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 vector from a scalar value.
286 lp_build_const_vec(struct lp_type type
,
289 LLVMTypeRef elem_type
= lp_build_elem_type(type
);
290 LLVMValueRef elems
[LP_MAX_VECTOR_LENGTH
];
293 assert(type
.length
<= LP_MAX_VECTOR_LENGTH
);
296 elems
[0] = LLVMConstReal(elem_type
, val
);
299 double dscale
= lp_const_scale(type
);
301 elems
[0] = LLVMConstInt(elem_type
, val
*dscale
+ 0.5, 0);
304 for(i
= 1; i
< type
.length
; ++i
)
307 return LLVMConstVector(elems
, type
.length
);
312 lp_build_const_int_vec(struct lp_type type
,
315 LLVMTypeRef elem_type
= lp_build_int_elem_type(type
);
316 LLVMValueRef elems
[LP_MAX_VECTOR_LENGTH
];
319 assert(type
.length
<= LP_MAX_VECTOR_LENGTH
);
321 for(i
= 0; i
< type
.length
; ++i
)
322 elems
[i
] = LLVMConstInt(elem_type
, val
, type
.sign
? 1 : 0);
324 return LLVMConstVector(elems
, type
.length
);
329 lp_build_const_aos(struct lp_type type
,
330 double r
, double g
, double b
, double a
,
331 const unsigned char *swizzle
)
333 const unsigned char default_swizzle
[4] = {0, 1, 2, 3};
334 LLVMTypeRef elem_type
;
335 LLVMValueRef elems
[LP_MAX_VECTOR_LENGTH
];
338 assert(type
.length
% 4 == 0);
339 assert(type
.length
<= LP_MAX_VECTOR_LENGTH
);
341 elem_type
= lp_build_elem_type(type
);
344 swizzle
= default_swizzle
;
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
);
353 double dscale
= lp_const_scale(type
);
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);
361 for(i
= 4; i
< type
.length
; ++i
)
362 elems
[i
] = elems
[i
% 4];
364 return LLVMConstVector(elems
, type
.length
);
369 lp_build_const_mask_aos(struct lp_type type
,
370 const boolean cond
[4])
372 LLVMTypeRef elem_type
= LLVMIntType(type
.width
);
373 LLVMValueRef masks
[LP_MAX_VECTOR_LENGTH
];
376 assert(type
.length
<= LP_MAX_VECTOR_LENGTH
);
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);
382 return LLVMConstVector(masks
, type
.length
);