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 **************************************************************************/
30 * Convenient representation of SIMD types.
32 * @author Jose Fonseca <jfonseca@vmware.com>
40 #include "pipe/p_compiler.h"
41 #include "gallivm/lp_bld.h"
46 * Native SIMD register width.
48 * 128 for all architectures we care about.
50 #define LP_NATIVE_VECTOR_WIDTH 128
53 * Several functions can only cope with vectors of length up to this value.
54 * You may need to increase that value if you want to represent bigger vectors.
56 #define LP_MAX_VECTOR_LENGTH 16
60 * The LLVM type system can't conveniently express all the things we care about
61 * on the types used for intermediate computations, such as signed vs unsigned,
62 * normalized values, or fixed point.
66 * Floating-point. Cannot be used with fixed. Integer numbers are
67 * represented by this zero.
72 * Fixed-point. Cannot be used with floating. Integer numbers are
73 * represented by this zero.
78 * Whether it can represent negative values or not.
80 * If this is not set for floating point, it means that all values are
81 * assumed to be positive.
86 * Whether values are normalized to fit [0, 1] interval, or [-1, 1]
87 * interval for signed types.
89 * For integer types it means the representable integer range should be
90 * interpreted as the interval above.
92 * For floating and fixed point formats it means the values should be
93 * clamped to the interval above.
100 * For fixed point values, the fixed point is assumed to be at half the
106 * Vector length. If length==1, this is a scalar (float/int) type.
108 * width*length should be a power of two greater or equal to eight.
110 * @sa LP_MAX_VECTOR_LENGTH
117 * We need most of the information here in order to correctly and efficiently
118 * translate an arithmetic operation into LLVM IR. Putting it here avoids the
119 * trouble of passing it as parameters.
121 struct lp_build_context
123 LLVMBuilderRef builder
;
126 * This not only describes the input/output LLVM types, but also whether
127 * to normalize/clamp the results.
131 /** Same as lp_build_undef(type) */
132 LLVMTypeRef elem_type
;
134 /** Same as lp_build_undef(type) */
135 LLVMTypeRef vec_type
;
137 /** Same as lp_build_undef(type) */
138 LLVMTypeRef int_elem_type
;
140 /** Same as lp_build_undef(type) */
141 LLVMTypeRef int_vec_type
;
143 /** Same as lp_build_undef(type) */
146 /** Same as lp_build_zero(type) */
149 /** Same as lp_build_one(type) */
154 /** Create scalar float type */
155 static INLINE
struct lp_type
156 lp_type_float(unsigned width
)
158 struct lp_type res_type
;
160 memset(&res_type
, 0, sizeof res_type
);
161 res_type
.floating
= TRUE
;
162 res_type
.sign
= TRUE
;
163 res_type
.width
= width
;
170 /** Create vector of float type */
171 static INLINE
struct lp_type
172 lp_type_float_vec(unsigned width
)
174 struct lp_type res_type
;
176 memset(&res_type
, 0, sizeof res_type
);
177 res_type
.floating
= TRUE
;
178 res_type
.sign
= TRUE
;
179 res_type
.width
= width
;
180 res_type
.length
= LP_NATIVE_VECTOR_WIDTH
/ width
;
186 /** Create scalar int type */
187 static INLINE
struct lp_type
188 lp_type_int(unsigned width
)
190 struct lp_type res_type
;
192 memset(&res_type
, 0, sizeof res_type
);
193 res_type
.sign
= TRUE
;
194 res_type
.width
= width
;
201 /** Create vector int type */
202 static INLINE
struct lp_type
203 lp_type_int_vec(unsigned width
)
205 struct lp_type res_type
;
207 memset(&res_type
, 0, sizeof res_type
);
208 res_type
.sign
= TRUE
;
209 res_type
.width
= width
;
210 res_type
.length
= LP_NATIVE_VECTOR_WIDTH
/ width
;
216 /** Create scalar uint type */
217 static INLINE
struct lp_type
218 lp_type_uint(unsigned width
)
220 struct lp_type res_type
;
222 memset(&res_type
, 0, sizeof res_type
);
223 res_type
.width
= width
;
230 /** Create vector uint type */
231 static INLINE
struct lp_type
232 lp_type_uint_vec(unsigned width
)
234 struct lp_type res_type
;
236 memset(&res_type
, 0, sizeof res_type
);
237 res_type
.width
= width
;
238 res_type
.length
= LP_NATIVE_VECTOR_WIDTH
/ width
;
244 static INLINE
struct lp_type
245 lp_type_unorm(unsigned width
)
247 struct lp_type res_type
;
249 memset(&res_type
, 0, sizeof res_type
);
250 res_type
.norm
= TRUE
;
251 res_type
.width
= width
;
252 res_type
.length
= LP_NATIVE_VECTOR_WIDTH
/ width
;
258 static INLINE
struct lp_type
259 lp_type_fixed(unsigned width
)
261 struct lp_type res_type
;
263 memset(&res_type
, 0, sizeof res_type
);
264 res_type
.sign
= TRUE
;
265 res_type
.fixed
= TRUE
;
266 res_type
.width
= width
;
267 res_type
.length
= LP_NATIVE_VECTOR_WIDTH
/ width
;
273 static INLINE
struct lp_type
274 lp_type_ufixed(unsigned width
)
276 struct lp_type res_type
;
278 memset(&res_type
, 0, sizeof res_type
);
279 res_type
.fixed
= TRUE
;
280 res_type
.width
= width
;
281 res_type
.length
= LP_NATIVE_VECTOR_WIDTH
/ width
;
288 lp_build_elem_type(struct lp_type type
);
292 lp_build_vec_type(struct lp_type type
);
296 lp_check_elem_type(struct lp_type type
, LLVMTypeRef elem_type
);
300 lp_check_vec_type(struct lp_type type
, LLVMTypeRef vec_type
);
304 lp_check_value(struct lp_type type
, LLVMValueRef val
);
308 lp_build_int_elem_type(struct lp_type type
);
312 lp_build_int_vec_type(struct lp_type type
);
316 lp_build_int32_vec4_type(void);
320 lp_uint_type(struct lp_type type
);
324 lp_int_type(struct lp_type type
);
328 lp_wider_type(struct lp_type type
);
332 lp_sizeof_llvm_type(LLVMTypeRef t
);
336 lp_typekind_name(LLVMTypeKind t
);
340 lp_dump_llvmtype(LLVMTypeRef t
);
344 lp_build_context_init(struct lp_build_context
*bld
,
345 LLVMBuilderRef builder
,
346 struct lp_type type
);
349 #endif /* !LP_BLD_TYPE_H */