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 **************************************************************************/
31 * Helper functions for packing/unpacking.
33 * Pack/unpacking is necessary for conversion between types of different
36 * They are also commonly used when an computation needs higher
37 * precision for the intermediate values. For example, if one needs the
42 * to use more precision for intermediate results then one should implement it
46 * compute(LLVMBuilderRef builder struct lp_type type, LLVMValueRef a, LLVMValueRef b)
48 * struct lp_type wide_type = lp_wider_type(type);
49 * LLVMValueRef al, ah, bl, bh, cl, ch, c;
51 * lp_build_unpack2(builder, type, wide_type, a, &al, &ah);
52 * lp_build_unpack2(builder, type, wide_type, b, &bl, &bh);
54 * cl = compute_half(al, bl);
55 * ch = compute_half(ah, bh);
57 * c = lp_build_pack2(bld->builder, wide_type, type, cl, ch);
62 * where compute_half() would do the computation for half the elements with
63 * twice the precision.
65 * @author Jose Fonseca <jfonseca@vmware.com>
69 #include "util/u_debug.h"
70 #include "util/u_math.h"
71 #include "util/u_cpu_detect.h"
73 #include "lp_bld_type.h"
74 #include "lp_bld_const.h"
75 #include "lp_bld_intr.h"
76 #include "lp_bld_arit.h"
77 #include "lp_bld_pack.h"
81 * Build shuffle vectors that match PUNPCKLxx and PUNPCKHxx instructions.
84 lp_build_const_unpack_shuffle(unsigned n
, unsigned lo_hi
)
86 LLVMValueRef elems
[LP_MAX_VECTOR_LENGTH
];
89 assert(n
<= LP_MAX_VECTOR_LENGTH
);
92 /* TODO: cache results in a static table */
94 for(i
= 0, j
= lo_hi
*n
/2; i
< n
; i
+= 2, ++j
) {
95 elems
[i
+ 0] = LLVMConstInt(LLVMInt32Type(), 0 + j
, 0);
96 elems
[i
+ 1] = LLVMConstInt(LLVMInt32Type(), n
+ j
, 0);
99 return LLVMConstVector(elems
, n
);
104 * Build shuffle vectors that match PACKxx instructions.
107 lp_build_const_pack_shuffle(unsigned n
)
109 LLVMValueRef elems
[LP_MAX_VECTOR_LENGTH
];
112 assert(n
<= LP_MAX_VECTOR_LENGTH
);
114 /* TODO: cache results in a static table */
116 for(i
= 0; i
< n
; ++i
)
117 elems
[i
] = LLVMConstInt(LLVMInt32Type(), 2*i
, 0);
119 return LLVMConstVector(elems
, n
);
124 * Interleave vector elements.
126 * Matches the PUNPCKLxx and PUNPCKHxx SSE instructions.
129 lp_build_interleave2(LLVMBuilderRef builder
,
135 LLVMValueRef shuffle
;
137 shuffle
= lp_build_const_unpack_shuffle(type
.length
, lo_hi
);
139 return LLVMBuildShuffleVector(builder
, a
, b
, shuffle
, "");
144 * Double the bit width.
146 * This will only change the number of bits the values are represented, not the
150 lp_build_unpack2(LLVMBuilderRef builder
,
151 struct lp_type src_type
,
152 struct lp_type dst_type
,
154 LLVMValueRef
*dst_lo
,
155 LLVMValueRef
*dst_hi
)
158 LLVMTypeRef dst_vec_type
;
160 assert(!src_type
.floating
);
161 assert(!dst_type
.floating
);
162 assert(dst_type
.width
== src_type
.width
* 2);
163 assert(dst_type
.length
* 2 == src_type
.length
);
165 if(dst_type
.sign
&& src_type
.sign
) {
166 /* Replicate the sign bit in the most significant bits */
167 msb
= LLVMBuildAShr(builder
, src
, lp_build_const_int_vec(src_type
, src_type
.width
- 1), "");
170 /* Most significant bits always zero */
171 msb
= lp_build_zero(src_type
);
173 /* Interleave bits */
174 #ifdef PIPE_ARCH_LITTLE_ENDIAN
175 *dst_lo
= lp_build_interleave2(builder
, src_type
, src
, msb
, 0);
176 *dst_hi
= lp_build_interleave2(builder
, src_type
, src
, msb
, 1);
178 *dst_lo
= lp_build_interleave2(builder
, src_type
, msb
, src
, 0);
179 *dst_hi
= lp_build_interleave2(builder
, src_type
, msb
, src
, 1);
182 /* Cast the result into the new type (twice as wide) */
184 dst_vec_type
= lp_build_vec_type(dst_type
);
186 *dst_lo
= LLVMBuildBitCast(builder
, *dst_lo
, dst_vec_type
, "");
187 *dst_hi
= LLVMBuildBitCast(builder
, *dst_hi
, dst_vec_type
, "");
192 * Expand the bit width.
194 * This will only change the number of bits the values are represented, not the
198 lp_build_unpack(LLVMBuilderRef builder
,
199 struct lp_type src_type
,
200 struct lp_type dst_type
,
202 LLVMValueRef
*dst
, unsigned num_dsts
)
207 /* Register width must remain constant */
208 assert(src_type
.width
* src_type
.length
== dst_type
.width
* dst_type
.length
);
210 /* We must not loose or gain channels. Only precision */
211 assert(src_type
.length
== dst_type
.length
* num_dsts
);
216 while(src_type
.width
< dst_type
.width
) {
217 struct lp_type tmp_type
= src_type
;
220 tmp_type
.length
/= 2;
222 for(i
= num_tmps
; i
--; ) {
223 lp_build_unpack2(builder
, src_type
, tmp_type
, dst
[i
], &dst
[2*i
+ 0], &dst
[2*i
+ 1]);
231 assert(num_tmps
== num_dsts
);
236 * Non-interleaved pack.
238 * This will move values as
240 * lo = __ l0 __ l1 __ l2 __.. __ ln
241 * hi = __ h0 __ h1 __ h2 __.. __ hn
242 * res = l0 l1 l2 .. ln h0 h1 h2 .. hn
244 * This will only change the number of bits the values are represented, not the
247 * It is assumed the values are already clamped into the destination type range.
248 * Values outside that range will produce undefined results. Use
249 * lp_build_packs2 instead.
252 lp_build_pack2(LLVMBuilderRef builder
,
253 struct lp_type src_type
,
254 struct lp_type dst_type
,
258 #if HAVE_LLVM < 0x0207
259 LLVMTypeRef src_vec_type
= lp_build_vec_type(src_type
);
261 LLVMTypeRef dst_vec_type
= lp_build_vec_type(dst_type
);
262 LLVMValueRef shuffle
;
263 LLVMValueRef res
= NULL
;
265 assert(!src_type
.floating
);
266 assert(!dst_type
.floating
);
267 assert(src_type
.width
== dst_type
.width
* 2);
268 assert(src_type
.length
* 2 == dst_type
.length
);
270 /* Check for special cases first */
271 if(util_cpu_caps
.has_sse2
&& src_type
.width
* src_type
.length
== 128) {
272 switch(src_type
.width
) {
275 #if HAVE_LLVM >= 0x0207
276 res
= lp_build_intrinsic_binary(builder
, "llvm.x86.sse2.packssdw.128", dst_vec_type
, lo
, hi
);
278 res
= lp_build_intrinsic_binary(builder
, "llvm.x86.sse2.packssdw.128", src_vec_type
, lo
, hi
);
282 if (util_cpu_caps
.has_sse4_1
) {
283 return lp_build_intrinsic_binary(builder
, "llvm.x86.sse41.packusdw", dst_vec_type
, lo
, hi
);
286 /* use generic shuffle below */
294 #if HAVE_LLVM >= 0x0207
295 res
= lp_build_intrinsic_binary(builder
, "llvm.x86.sse2.packsswb.128", dst_vec_type
, lo
, hi
);
297 res
= lp_build_intrinsic_binary(builder
, "llvm.x86.sse2.packsswb.128", src_vec_type
, lo
, hi
);
300 #if HAVE_LLVM >= 0x0207
301 res
= lp_build_intrinsic_binary(builder
, "llvm.x86.sse2.packuswb.128", dst_vec_type
, lo
, hi
);
303 res
= lp_build_intrinsic_binary(builder
, "llvm.x86.sse2.packuswb.128", src_vec_type
, lo
, hi
);
309 return LLVMGetUndef(dst_vec_type
);
314 res
= LLVMBuildBitCast(builder
, res
, dst_vec_type
, "");
319 /* generic shuffle */
320 lo
= LLVMBuildBitCast(builder
, lo
, dst_vec_type
, "");
321 hi
= LLVMBuildBitCast(builder
, hi
, dst_vec_type
, "");
323 shuffle
= lp_build_const_pack_shuffle(dst_type
.length
);
325 res
= LLVMBuildShuffleVector(builder
, lo
, hi
, shuffle
, "");
333 * Non-interleaved pack and saturate.
335 * Same as lp_build_pack2 but will saturate values so that they fit into the
339 lp_build_packs2(LLVMBuilderRef builder
,
340 struct lp_type src_type
,
341 struct lp_type dst_type
,
347 assert(!src_type
.floating
);
348 assert(!dst_type
.floating
);
349 assert(src_type
.sign
== dst_type
.sign
);
350 assert(src_type
.width
== dst_type
.width
* 2);
351 assert(src_type
.length
* 2 == dst_type
.length
);
355 /* All X86 SSE non-interleaved pack instructions take signed inputs and
356 * saturate them, so no need to clamp for those cases. */
357 if(util_cpu_caps
.has_sse2
&&
358 src_type
.width
* src_type
.length
== 128 &&
363 struct lp_build_context bld
;
364 unsigned dst_bits
= dst_type
.sign
? dst_type
.width
- 1 : dst_type
.width
;
365 LLVMValueRef dst_max
= lp_build_const_int_vec(src_type
, ((unsigned long long)1 << dst_bits
) - 1);
366 lp_build_context_init(&bld
, builder
, src_type
);
367 lo
= lp_build_min(&bld
, lo
, dst_max
);
368 hi
= lp_build_min(&bld
, hi
, dst_max
);
369 /* FIXME: What about lower bound? */
372 return lp_build_pack2(builder
, src_type
, dst_type
, lo
, hi
);
377 * Truncate the bit width.
379 * TODO: Handle saturation consistently.
382 lp_build_pack(LLVMBuilderRef builder
,
383 struct lp_type src_type
,
384 struct lp_type dst_type
,
386 const LLVMValueRef
*src
, unsigned num_srcs
)
388 LLVMValueRef (*pack2
)(LLVMBuilderRef builder
,
389 struct lp_type src_type
,
390 struct lp_type dst_type
,
393 LLVMValueRef tmp
[LP_MAX_VECTOR_LENGTH
];
397 /* Register width must remain constant */
398 assert(src_type
.width
* src_type
.length
== dst_type
.width
* dst_type
.length
);
400 /* We must not loose or gain channels. Only precision */
401 assert(src_type
.length
* num_srcs
== dst_type
.length
);
404 pack2
= &lp_build_pack2
;
406 pack2
= &lp_build_packs2
;
408 for(i
= 0; i
< num_srcs
; ++i
)
411 while(src_type
.width
> dst_type
.width
) {
412 struct lp_type tmp_type
= src_type
;
415 tmp_type
.length
*= 2;
417 /* Take in consideration the sign changes only in the last step */
418 if(tmp_type
.width
== dst_type
.width
)
419 tmp_type
.sign
= dst_type
.sign
;
423 for(i
= 0; i
< num_srcs
; ++i
)
424 tmp
[i
] = pack2(builder
, src_type
, tmp_type
, tmp
[2*i
+ 0], tmp
[2*i
+ 1]);
429 assert(num_srcs
== 1);
436 * Truncate or expand the bitwidth.
438 * NOTE: Getting the right sign flags is crucial here, as we employ some
439 * intrinsics that do saturation.
442 lp_build_resize(LLVMBuilderRef builder
,
443 struct lp_type src_type
,
444 struct lp_type dst_type
,
445 const LLVMValueRef
*src
, unsigned num_srcs
,
446 LLVMValueRef
*dst
, unsigned num_dsts
)
448 LLVMValueRef tmp
[LP_MAX_VECTOR_LENGTH
];
452 * We don't support float <-> int conversion here. That must be done
453 * before/after calling this function.
455 assert(src_type
.floating
== dst_type
.floating
);
458 * We don't support double <-> float conversion yet, although it could be
459 * added with little effort.
461 assert((!src_type
.floating
&& !dst_type
.floating
) ||
462 src_type
.width
== dst_type
.width
);
464 /* We must not loose or gain channels. Only precision */
465 assert(src_type
.length
* num_srcs
== dst_type
.length
* num_dsts
);
467 /* We don't support M:N conversion, only 1:N, M:1, or 1:1 */
468 assert(num_srcs
== 1 || num_dsts
== 1);
470 assert(src_type
.length
<= LP_MAX_VECTOR_LENGTH
);
471 assert(dst_type
.length
<= LP_MAX_VECTOR_LENGTH
);
472 assert(num_srcs
<= LP_MAX_VECTOR_LENGTH
);
473 assert(num_dsts
<= LP_MAX_VECTOR_LENGTH
);
475 if (src_type
.width
> dst_type
.width
) {
477 * Truncate bit width.
480 assert(num_dsts
== 1);
482 if (src_type
.width
* src_type
.length
== dst_type
.width
* dst_type
.length
) {
484 * Register width remains constant -- use vector packing intrinsics
487 tmp
[0] = lp_build_pack(builder
, src_type
, dst_type
, TRUE
, src
, num_srcs
);
491 * Do it element-wise.
494 assert(src_type
.length
== dst_type
.length
);
495 tmp
[0] = lp_build_undef(dst_type
);
496 for (i
= 0; i
< dst_type
.length
; ++i
) {
497 LLVMValueRef index
= LLVMConstInt(LLVMInt32Type(), i
, 0);
498 LLVMValueRef val
= LLVMBuildExtractElement(builder
, src
[0], index
, "");
499 val
= LLVMBuildTrunc(builder
, val
, lp_build_elem_type(dst_type
), "");
500 tmp
[0] = LLVMBuildInsertElement(builder
, tmp
[0], val
, index
, "");
504 else if (src_type
.width
< dst_type
.width
) {
509 assert(num_srcs
== 1);
511 if (src_type
.width
* src_type
.length
== dst_type
.width
* dst_type
.length
) {
513 * Register width remains constant -- use vector unpack intrinsics
515 lp_build_unpack(builder
, src_type
, dst_type
, src
[0], tmp
, num_dsts
);
519 * Do it element-wise.
522 assert(src_type
.length
== dst_type
.length
);
523 tmp
[0] = lp_build_undef(dst_type
);
524 for (i
= 0; i
< dst_type
.length
; ++i
) {
525 LLVMValueRef index
= LLVMConstInt(LLVMInt32Type(), i
, 0);
526 LLVMValueRef val
= LLVMBuildExtractElement(builder
, src
[0], index
, "");
528 if (src_type
.sign
&& dst_type
.sign
) {
529 val
= LLVMBuildSExt(builder
, val
, lp_build_elem_type(dst_type
), "");
531 val
= LLVMBuildZExt(builder
, val
, lp_build_elem_type(dst_type
), "");
533 tmp
[0] = LLVMBuildInsertElement(builder
, tmp
[0], val
, index
, "");
542 assert(num_srcs
== 1);
543 assert(num_dsts
== 1);
548 for(i
= 0; i
< num_dsts
; ++i
)