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 **************************************************************************/
29 #include "util/u_debug.h"
31 #include "lp_bld_type.h"
32 #include "lp_bld_const.h"
36 lp_build_elem_type(struct lp_type type
)
41 return LLVMFloatType();
44 return LLVMDoubleType();
48 return LLVMFloatType();
52 return LLVMIntType(type
.width
);
58 lp_build_vec_type(struct lp_type type
)
60 LLVMTypeRef elem_type
= lp_build_elem_type(type
);
61 return LLVMVectorType(elem_type
, type
.length
);
66 * This function is a mirror of lp_build_elem_type() above.
68 * XXX: I'm not sure if it wouldn't be easier/efficient to just recreate the
69 * type and check for identity.
72 lp_check_elem_type(struct lp_type type
, LLVMTypeRef elem_type
)
74 LLVMTypeKind elem_kind
;
80 elem_kind
= LLVMGetTypeKind(elem_type
);
85 if(elem_kind
!= LLVMFloatTypeKind
)
89 if(elem_kind
!= LLVMDoubleTypeKind
)
98 if(elem_kind
!= LLVMIntegerTypeKind
)
101 if(LLVMGetIntTypeWidth(elem_type
) != type
.width
)
110 lp_check_vec_type(struct lp_type type
, LLVMTypeRef vec_type
)
112 LLVMTypeRef elem_type
;
118 if(LLVMGetTypeKind(vec_type
) != LLVMVectorTypeKind
)
121 if(LLVMGetVectorSize(vec_type
) != type
.length
)
124 elem_type
= LLVMGetElementType(vec_type
);
126 return lp_check_elem_type(type
, elem_type
);
131 lp_check_value(struct lp_type type
, LLVMValueRef val
)
133 LLVMTypeRef vec_type
;
139 vec_type
= LLVMTypeOf(val
);
141 return lp_check_vec_type(type
, vec_type
);
146 lp_build_int_elem_type(struct lp_type type
)
148 return LLVMIntType(type
.width
);
153 lp_build_int_vec_type(struct lp_type type
)
155 LLVMTypeRef elem_type
= lp_build_int_elem_type(type
);
156 return LLVMVectorType(elem_type
, type
.length
);
161 * Build int32[4] vector type
164 lp_build_int32_vec4_type(void)
169 memset(&t
, 0, sizeof(t
));
170 t
.floating
= FALSE
; /* floating point values */
171 t
.sign
= TRUE
; /* values are signed */
172 t
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
173 t
.width
= 32; /* 32-bit int */
174 t
.length
= 4; /* 4 elements per vector */
176 type
= lp_build_int_elem_type(t
);
177 return LLVMVectorType(type
, t
.length
);
182 lp_int_type(struct lp_type type
)
184 struct lp_type res_type
;
186 memset(&res_type
, 0, sizeof res_type
);
187 res_type
.width
= type
.width
;
188 res_type
.length
= type
.length
;
195 * Return the type with twice the bit width (hence half the number of elements).
198 lp_wider_type(struct lp_type type
)
200 struct lp_type res_type
;
202 memcpy(&res_type
, &type
, sizeof res_type
);
204 res_type
.length
/= 2;
206 assert(res_type
.length
);
213 lp_build_context_init(struct lp_build_context
*bld
,
214 LLVMBuilderRef builder
,
217 bld
->builder
= builder
;
219 bld
->undef
= lp_build_undef(type
);
220 bld
->zero
= lp_build_zero(type
);
221 bld
->one
= lp_build_one(type
);