1 /**************************************************************************
3 * Copyright 2010 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 SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
26 **************************************************************************/
29 #include "util/u_debug.h"
30 #include "lp_bld_debug.h"
31 #include "lp_bld_const.h"
32 #include "lp_bld_format.h"
33 #include "lp_bld_gather.h"
34 #include "lp_bld_init.h"
35 #include "lp_bld_intr.h"
39 * Get the pointer to one element from scatter positions in memory.
41 * @sa lp_build_gather()
44 lp_build_gather_elem_ptr(struct gallivm_state
*gallivm
,
46 LLVMValueRef base_ptr
,
53 assert(LLVMTypeOf(base_ptr
) == LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0));
59 LLVMValueRef index
= lp_build_const_int32(gallivm
, i
);
60 offset
= LLVMBuildExtractElement(gallivm
->builder
, offsets
, index
, "");
63 ptr
= LLVMBuildGEP(gallivm
->builder
, base_ptr
, &offset
, 1, "");
70 * Gather one element from scatter positions in memory.
72 * @sa lp_build_gather()
75 lp_build_gather_elem(struct gallivm_state
*gallivm
,
79 LLVMValueRef base_ptr
,
83 LLVMTypeRef src_type
= LLVMIntTypeInContext(gallivm
->context
, src_width
);
84 LLVMTypeRef src_ptr_type
= LLVMPointerType(src_type
, 0);
85 LLVMTypeRef dst_elem_type
= LLVMIntTypeInContext(gallivm
->context
, dst_width
);
89 assert(LLVMTypeOf(base_ptr
) == LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0));
91 ptr
= lp_build_gather_elem_ptr(gallivm
, length
, base_ptr
, offsets
, i
);
92 ptr
= LLVMBuildBitCast(gallivm
->builder
, ptr
, src_ptr_type
, "");
93 res
= LLVMBuildLoad(gallivm
->builder
, ptr
, "");
95 assert(src_width
<= dst_width
);
96 if (src_width
> dst_width
) {
97 res
= LLVMBuildTrunc(gallivm
->builder
, res
, dst_elem_type
, "");
98 } else if (src_width
< dst_width
) {
99 res
= LLVMBuildZExt(gallivm
->builder
, res
, dst_elem_type
, "");
100 #ifdef PIPE_ARCH_BIG_ENDIAN
101 res
= LLVMBuildShl(gallivm
->builder
, res
,
102 LLVMConstInt(dst_elem_type
, dst_width
- src_width
, 0), "");
111 * Gather elements from scatter positions in memory into a single vector.
112 * Use for fetching texels from a texture.
113 * For SSE, typical values are length=4, src_width=32, dst_width=32.
115 * @param length length of the offsets
116 * @param src_width src element width in bits
117 * @param dst_width result element width in bits (src will be expanded to fit)
118 * @param base_ptr base pointer, should be a i8 pointer type.
119 * @param offsets vector with offsets
122 lp_build_gather(struct gallivm_state
*gallivm
,
126 LLVMValueRef base_ptr
,
127 LLVMValueRef offsets
)
133 return lp_build_gather_elem(gallivm
, length
,
134 src_width
, dst_width
,
135 base_ptr
, offsets
, 0);
139 LLVMTypeRef dst_elem_type
= LLVMIntTypeInContext(gallivm
->context
, dst_width
);
140 LLVMTypeRef dst_vec_type
= LLVMVectorType(dst_elem_type
, length
);
143 res
= LLVMGetUndef(dst_vec_type
);
144 for (i
= 0; i
< length
; ++i
) {
145 LLVMValueRef index
= lp_build_const_int32(gallivm
, i
);
147 elem
= lp_build_gather_elem(gallivm
, length
,
148 src_width
, dst_width
,
149 base_ptr
, offsets
, i
);
150 res
= LLVMBuildInsertElement(gallivm
->builder
, res
, elem
, index
, "");
158 lp_build_gather_values(struct gallivm_state
* gallivm
,
159 LLVMValueRef
* values
,
160 unsigned value_count
)
162 LLVMTypeRef vec_type
= LLVMVectorType(LLVMTypeOf(values
[0]), value_count
);
163 LLVMBuilderRef builder
= gallivm
->builder
;
164 LLVMValueRef vec
= LLVMGetUndef(vec_type
);
167 for (i
= 0; i
< value_count
; i
++) {
168 LLVMValueRef index
= lp_build_const_int32(gallivm
, i
);
169 vec
= LLVMBuildInsertElement(builder
, vec
, values
[i
], index
, "");