lp_bld_type.h revision 6299f241e9fdd86e705d144a42d9b1979c13f9ad
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 "pipe/p_compiler.h"
41#include "gallivm/lp_bld.h"
42
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   struct gallivm_state *gallivm;
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_elem_type(type) */
132   LLVMTypeRef elem_type;
133
134   /** Same as lp_build_vec_type(type) */
135   LLVMTypeRef vec_type;
136
137   /** Same as lp_build_int_elem_type(type) */
138   LLVMTypeRef int_elem_type;
139
140   /** Same as lp_build_int_vec_type(type) */
141   LLVMTypeRef int_vec_type;
142
143   /** Same as lp_build_undef(type) */
144   LLVMValueRef undef;
145
146   /** Same as lp_build_zero(type) */
147   LLVMValueRef zero;
148
149   /** Same as lp_build_one(type) */
150   LLVMValueRef one;
151};
152
153
154/** Create scalar float type */
155static INLINE struct lp_type
156lp_type_float(unsigned width)
157{
158   struct lp_type res_type;
159
160   memset(&res_type, 0, sizeof res_type);
161   res_type.floating = TRUE;
162   res_type.sign = TRUE;
163   res_type.width = width;
164   res_type.length = 1;
165
166   return res_type;
167}
168
169
170/** Create vector of float type */
171static INLINE struct lp_type
172lp_type_float_vec(unsigned width)
173{
174   struct lp_type res_type;
175
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;
181
182   return res_type;
183}
184
185
186/** Create scalar int type */
187static INLINE struct lp_type
188lp_type_int(unsigned width)
189{
190   struct lp_type res_type;
191
192   memset(&res_type, 0, sizeof res_type);
193   res_type.sign = TRUE;
194   res_type.width = width;
195   res_type.length = 1;
196
197   return res_type;
198}
199
200
201/** Create vector int type */
202static INLINE struct lp_type
203lp_type_int_vec(unsigned width)
204{
205   struct lp_type res_type;
206
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;
211
212   return res_type;
213}
214
215
216/** Create scalar uint type */
217static INLINE struct lp_type
218lp_type_uint(unsigned width)
219{
220   struct lp_type res_type;
221
222   memset(&res_type, 0, sizeof res_type);
223   res_type.width = width;
224   res_type.length = 1;
225
226   return res_type;
227}
228
229
230/** Create vector uint type */
231static INLINE struct lp_type
232lp_type_uint_vec(unsigned width)
233{
234   struct lp_type res_type;
235
236   memset(&res_type, 0, sizeof res_type);
237   res_type.width = width;
238   res_type.length = LP_NATIVE_VECTOR_WIDTH / width;
239
240   return res_type;
241}
242
243
244static INLINE struct lp_type
245lp_type_unorm(unsigned width)
246{
247   struct lp_type res_type;
248
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;
253
254   return res_type;
255}
256
257
258static INLINE struct lp_type
259lp_type_fixed(unsigned width)
260{
261   struct lp_type res_type;
262
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;
268
269   return res_type;
270}
271
272
273static INLINE struct lp_type
274lp_type_ufixed(unsigned width)
275{
276   struct lp_type res_type;
277
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;
282
283   return res_type;
284}
285
286
287LLVMTypeRef
288lp_build_elem_type(struct gallivm_state *gallivm, struct lp_type type);
289
290
291LLVMTypeRef
292lp_build_vec_type(struct gallivm_state *gallivm, struct lp_type type);
293
294
295boolean
296lp_check_elem_type(struct lp_type type, LLVMTypeRef elem_type);
297
298
299boolean
300lp_check_vec_type(struct lp_type type, LLVMTypeRef vec_type);
301
302
303boolean
304lp_check_value(struct lp_type type, LLVMValueRef val);
305
306
307LLVMTypeRef
308lp_build_int_elem_type(struct gallivm_state *gallivm, struct lp_type type);
309
310
311LLVMTypeRef
312lp_build_int_vec_type(struct gallivm_state *gallivm, struct lp_type type);
313
314
315LLVMTypeRef
316lp_build_int32_vec4_type(struct gallivm_state *gallivm);
317
318
319static INLINE struct lp_type
320lp_float32_vec4_type(void)
321{
322   struct lp_type type;
323
324   memset(&type, 0, sizeof(type));
325   type.floating = TRUE;
326   type.sign = TRUE;
327   type.norm = FALSE;
328   type.width = 32;
329   type.length = 4;
330
331   return type;
332}
333
334
335static INLINE struct lp_type
336lp_int32_vec4_type(void)
337{
338   struct lp_type type;
339
340   memset(&type, 0, sizeof(type));
341   type.floating = FALSE;
342   type.sign = TRUE;
343   type.norm = FALSE;
344   type.width = 32;
345   type.length = 4;
346
347   return type;
348}
349
350
351static INLINE struct lp_type
352lp_unorm8_vec4_type(void)
353{
354   struct lp_type type;
355
356   memset(&type, 0, sizeof(type));
357   type.floating = FALSE;
358   type.sign = FALSE;
359   type.norm = TRUE;
360   type.width = 8;
361   type.length = 4;
362
363   return type;
364}
365
366
367struct lp_type
368lp_elem_type(struct lp_type type);
369
370
371struct lp_type
372lp_uint_type(struct lp_type type);
373
374
375struct lp_type
376lp_int_type(struct lp_type type);
377
378
379struct lp_type
380lp_wider_type(struct lp_type type);
381
382
383unsigned
384lp_sizeof_llvm_type(LLVMTypeRef t);
385
386
387const char *
388lp_typekind_name(LLVMTypeKind t);
389
390
391void
392lp_dump_llvmtype(LLVMTypeRef t);
393
394
395void
396lp_build_context_init(struct lp_build_context *bld,
397                      struct gallivm_state *gallivm,
398                      struct lp_type type);
399
400
401#endif /* !LP_BLD_TYPE_H */
402