lp_bld_type.h revision 2ccae040a458ad0f95ee46916e2ea467d5cf9d02
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 * @file
30 * Convenient representation of SIMD types.
31 *
32 * @author Jose Fonseca <jfonseca@vmware.com>
33 */
34
35
36#ifndef LP_BLD_TYPE_H
37#define LP_BLD_TYPE_H
38
39
40#include <llvm-c/Core.h>
41
42#include <pipe/p_compiler.h>
43
44
45/**
46 * Native SIMD register width.
47 *
48 * 128 for all architectures we care about.
49 */
50#define LP_NATIVE_VECTOR_WIDTH 128
51
52/**
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.
55 */
56#define LP_MAX_VECTOR_LENGTH 16
57
58
59/**
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.
63 */
64struct lp_type {
65   /**
66    * Floating-point. Cannot be used with fixed. Integer numbers are
67    * represented by this zero.
68    */
69   unsigned floating:1;
70
71   /**
72    * Fixed-point. Cannot be used with floating. Integer numbers are
73    * represented by this zero.
74    */
75   unsigned fixed:1;
76
77   /**
78    * Whether it can represent negative values or not.
79    *
80    * If this is not set for floating point, it means that all values are
81    * assumed to be positive.
82    */
83   unsigned sign:1;
84
85   /**
86    * Whether values are normalized to fit [0, 1] interval, or [-1, 1]
87    * interval for signed types.
88    *
89    * For integer types it means the representable integer range should be
90    * interpreted as the interval above.
91    *
92    * For floating and fixed point formats it means the values should be
93    * clamped to the interval above.
94    */
95   unsigned norm:1;
96
97   /**
98    * Element width.
99    *
100    * For fixed point values, the fixed point is assumed to be at half the
101    * width.
102    */
103   unsigned width:14;
104
105   /**
106    * Vector length.  If length==1, this is a scalar (float/int) type.
107    *
108    * width*length should be a power of two greater or equal to eight.
109    *
110    * @sa LP_MAX_VECTOR_LENGTH
111    */
112   unsigned length:14;
113};
114
115
116/**
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.
120 */
121struct lp_build_context
122{
123   LLVMBuilderRef builder;
124
125   /**
126    * This not only describes the input/output LLVM types, but also whether
127    * to normalize/clamp the results.
128    */
129   struct lp_type type;
130
131   /** Same as lp_build_undef(type) */
132   LLVMValueRef undef;
133
134   /** Same as lp_build_zero(type) */
135   LLVMValueRef zero;
136
137   /** Same as lp_build_one(type) */
138   LLVMValueRef one;
139};
140
141
142/** Create scalar float type */
143static INLINE struct lp_type
144lp_type_float(unsigned width)
145{
146   struct lp_type res_type;
147
148   memset(&res_type, 0, sizeof res_type);
149   res_type.floating = TRUE;
150   res_type.sign = TRUE;
151   res_type.width = width;
152   res_type.length = 1;
153
154   return res_type;
155}
156
157
158/** Create vector of float type */
159static INLINE struct lp_type
160lp_type_float_vec(unsigned width)
161{
162   struct lp_type res_type;
163
164   memset(&res_type, 0, sizeof res_type);
165   res_type.floating = TRUE;
166   res_type.sign = TRUE;
167   res_type.width = width;
168   res_type.length = LP_NATIVE_VECTOR_WIDTH / width;
169
170   return res_type;
171}
172
173
174/** Create scalar int type */
175static INLINE struct lp_type
176lp_type_int(unsigned width)
177{
178   struct lp_type res_type;
179
180   memset(&res_type, 0, sizeof res_type);
181   res_type.sign = TRUE;
182   res_type.width = width;
183   res_type.length = 1;
184
185   return res_type;
186}
187
188
189/** Create vector int type */
190static INLINE struct lp_type
191lp_type_int_vec(unsigned width)
192{
193   struct lp_type res_type;
194
195   memset(&res_type, 0, sizeof res_type);
196   res_type.sign = TRUE;
197   res_type.width = width;
198   res_type.length = LP_NATIVE_VECTOR_WIDTH / width;
199
200   return res_type;
201}
202
203
204/** Create scalar uint type */
205static INLINE struct lp_type
206lp_type_uint(unsigned width)
207{
208   struct lp_type res_type;
209
210   memset(&res_type, 0, sizeof res_type);
211   res_type.width = width;
212   res_type.length = 1;
213
214   return res_type;
215}
216
217
218/** Create vector uint type */
219static INLINE struct lp_type
220lp_type_uint_vec(unsigned width)
221{
222   struct lp_type res_type;
223
224   memset(&res_type, 0, sizeof res_type);
225   res_type.width = width;
226   res_type.length = LP_NATIVE_VECTOR_WIDTH / width;
227
228   return res_type;
229}
230
231
232static INLINE struct lp_type
233lp_type_unorm(unsigned width)
234{
235   struct lp_type res_type;
236
237   memset(&res_type, 0, sizeof res_type);
238   res_type.norm = TRUE;
239   res_type.width = width;
240   res_type.length = LP_NATIVE_VECTOR_WIDTH / width;
241
242   return res_type;
243}
244
245
246static INLINE struct lp_type
247lp_type_fixed(unsigned width)
248{
249   struct lp_type res_type;
250
251   memset(&res_type, 0, sizeof res_type);
252   res_type.sign = TRUE;
253   res_type.fixed = TRUE;
254   res_type.width = width;
255   res_type.length = LP_NATIVE_VECTOR_WIDTH / width;
256
257   return res_type;
258}
259
260
261static INLINE struct lp_type
262lp_type_ufixed(unsigned width)
263{
264   struct lp_type res_type;
265
266   memset(&res_type, 0, sizeof res_type);
267   res_type.fixed = TRUE;
268   res_type.width = width;
269   res_type.length = LP_NATIVE_VECTOR_WIDTH / width;
270
271   return res_type;
272}
273
274
275LLVMTypeRef
276lp_build_elem_type(struct lp_type type);
277
278
279LLVMTypeRef
280lp_build_vec_type(struct lp_type type);
281
282
283boolean
284lp_check_elem_type(struct lp_type type, LLVMTypeRef elem_type);
285
286
287boolean
288lp_check_vec_type(struct lp_type type, LLVMTypeRef vec_type);
289
290
291boolean
292lp_check_value(struct lp_type type, LLVMValueRef val);
293
294
295LLVMTypeRef
296lp_build_int_elem_type(struct lp_type type);
297
298
299LLVMTypeRef
300lp_build_int_vec_type(struct lp_type type);
301
302
303LLVMTypeRef
304lp_build_int32_vec4_type(void);
305
306
307struct lp_type
308lp_uint_type(struct lp_type type);
309
310
311struct lp_type
312lp_int_type(struct lp_type type);
313
314
315struct lp_type
316lp_wider_type(struct lp_type type);
317
318
319void
320lp_build_context_init(struct lp_build_context *bld,
321                      LLVMBuilderRef builder,
322                      struct lp_type type);
323
324
325#endif /* !LP_BLD_TYPE_H */
326