lp_bld_pack.c revision f263fdee8146719b14d9f9b14cf0c224461f35dc
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/**
30 * @file
31 * Helper functions for packing/unpacking.
32 *
33 * Pack/unpacking is necessary for conversion between types of different
34 * bit width.
35 *
36 * They are also commonly used when an computation needs higher
37 * precision for the intermediate values. For example, if one needs the
38 * function:
39 *
40 *   c = compute(a, b);
41 *
42 * to use more precision for intermediate results then one should implement it
43 * as:
44 *
45 *   LLVMValueRef
46 *   compute(LLVMBuilderRef builder struct lp_type type, LLVMValueRef a, LLVMValueRef b)
47 *   {
48 *      struct lp_type wide_type = lp_wider_type(type);
49 *      LLVMValueRef al, ah, bl, bh, cl, ch, c;
50 *
51 *      lp_build_unpack2(builder, type, wide_type, a, &al, &ah);
52 *      lp_build_unpack2(builder, type, wide_type, b, &bl, &bh);
53 *
54 *      cl = compute_half(al, bl);
55 *      ch = compute_half(ah, bh);
56 *
57 *      c = lp_build_pack2(bld->builder, wide_type, type, cl, ch);
58 *
59 *      return c;
60 *   }
61 *
62 * where compute_half() would do the computation for half the elements with
63 * twice the precision.
64 *
65 * @author Jose Fonseca <jfonseca@vmware.com>
66 */
67
68
69#include "util/u_debug.h"
70#include "util/u_math.h"
71#include "util/u_cpu_detect.h"
72
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"
78
79
80/**
81 * Build shuffle vectors that match PUNPCKLxx and PUNPCKHxx instructions.
82 */
83static LLVMValueRef
84lp_build_const_unpack_shuffle(unsigned n, unsigned lo_hi)
85{
86   LLVMValueRef elems[LP_MAX_VECTOR_LENGTH];
87   unsigned i, j;
88
89   assert(n <= LP_MAX_VECTOR_LENGTH);
90   assert(lo_hi < 2);
91
92   /* TODO: cache results in a static table */
93
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);
97   }
98
99   return LLVMConstVector(elems, n);
100}
101
102
103/**
104 * Build shuffle vectors that match PACKxx instructions.
105 */
106static LLVMValueRef
107lp_build_const_pack_shuffle(unsigned n)
108{
109   LLVMValueRef elems[LP_MAX_VECTOR_LENGTH];
110   unsigned i;
111
112   assert(n <= LP_MAX_VECTOR_LENGTH);
113
114   /* TODO: cache results in a static table */
115
116   for(i = 0; i < n; ++i)
117      elems[i] = LLVMConstInt(LLVMInt32Type(), 2*i, 0);
118
119   return LLVMConstVector(elems, n);
120}
121
122
123/**
124 * Interleave vector elements.
125 *
126 * Matches the PUNPCKLxx and PUNPCKHxx SSE instructions.
127 */
128LLVMValueRef
129lp_build_interleave2(LLVMBuilderRef builder,
130                     struct lp_type type,
131                     LLVMValueRef a,
132                     LLVMValueRef b,
133                     unsigned lo_hi)
134{
135   LLVMValueRef shuffle;
136
137   shuffle = lp_build_const_unpack_shuffle(type.length, lo_hi);
138
139   return LLVMBuildShuffleVector(builder, a, b, shuffle, "");
140}
141
142
143/**
144 * Double the bit width.
145 *
146 * This will only change the number of bits the values are represented, not the
147 * values themselves.
148 */
149void
150lp_build_unpack2(LLVMBuilderRef builder,
151                 struct lp_type src_type,
152                 struct lp_type dst_type,
153                 LLVMValueRef src,
154                 LLVMValueRef *dst_lo,
155                 LLVMValueRef *dst_hi)
156{
157   LLVMValueRef msb;
158   LLVMTypeRef dst_vec_type;
159
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);
164
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), "");
168   }
169   else
170      /* Most significant bits always zero */
171      msb = lp_build_zero(src_type);
172
173   /* Interleave bits */
174   if(util_cpu_caps.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);
177   }
178   else {
179      *dst_lo = lp_build_interleave2(builder, src_type, msb, src, 0);
180      *dst_hi = lp_build_interleave2(builder, src_type, msb, src, 1);
181   }
182
183   /* Cast the result into the new type (twice as wide) */
184
185   dst_vec_type = lp_build_vec_type(dst_type);
186
187   *dst_lo = LLVMBuildBitCast(builder, *dst_lo, dst_vec_type, "");
188   *dst_hi = LLVMBuildBitCast(builder, *dst_hi, dst_vec_type, "");
189}
190
191
192/**
193 * Expand the bit width.
194 *
195 * This will only change the number of bits the values are represented, not the
196 * values themselves.
197 */
198void
199lp_build_unpack(LLVMBuilderRef builder,
200                struct lp_type src_type,
201                struct lp_type dst_type,
202                LLVMValueRef src,
203                LLVMValueRef *dst, unsigned num_dsts)
204{
205   unsigned num_tmps;
206   unsigned i;
207
208   /* Register width must remain constant */
209   assert(src_type.width * src_type.length == dst_type.width * dst_type.length);
210
211   /* We must not loose or gain channels. Only precision */
212   assert(src_type.length == dst_type.length * num_dsts);
213
214   num_tmps = 1;
215   dst[0] = src;
216
217   while(src_type.width < dst_type.width) {
218      struct lp_type tmp_type = src_type;
219
220      tmp_type.width *= 2;
221      tmp_type.length /= 2;
222
223      for(i = num_tmps; i--; ) {
224         lp_build_unpack2(builder, src_type, tmp_type, dst[i], &dst[2*i + 0], &dst[2*i + 1]);
225      }
226
227      src_type = tmp_type;
228
229      num_tmps *= 2;
230   }
231
232   assert(num_tmps == num_dsts);
233}
234
235
236/**
237 * Non-interleaved pack.
238 *
239 * This will move values as
240 *
241 *   lo =   __ l0 __ l1 __ l2 __..  __ ln
242 *   hi =   __ h0 __ h1 __ h2 __..  __ hn
243 *   res =  l0 l1 l2 .. ln h0 h1 h2 .. hn
244 *
245 * This will only change the number of bits the values are represented, not the
246 * values themselves.
247 *
248 * It is assumed the values are already clamped into the destination type range.
249 * Values outside that range will produce undefined results. Use
250 * lp_build_packs2 instead.
251 */
252LLVMValueRef
253lp_build_pack2(LLVMBuilderRef builder,
254               struct lp_type src_type,
255               struct lp_type dst_type,
256               LLVMValueRef lo,
257               LLVMValueRef hi)
258{
259#if HAVE_LLVM < 0x0207
260   LLVMTypeRef src_vec_type = lp_build_vec_type(src_type);
261#endif
262   LLVMTypeRef dst_vec_type = lp_build_vec_type(dst_type);
263   LLVMValueRef shuffle;
264   LLVMValueRef res = NULL;
265
266   assert(!src_type.floating);
267   assert(!dst_type.floating);
268   assert(src_type.width == dst_type.width * 2);
269   assert(src_type.length * 2 == dst_type.length);
270
271   /* Check for special cases first */
272   if(util_cpu_caps.has_sse2 && src_type.width * src_type.length == 128) {
273      switch(src_type.width) {
274      case 32:
275         if(dst_type.sign) {
276#if HAVE_LLVM >= 0x0207
277            res = lp_build_intrinsic_binary(builder, "llvm.x86.sse2.packssdw.128", dst_vec_type, lo, hi);
278#else
279            res = lp_build_intrinsic_binary(builder, "llvm.x86.sse2.packssdw.128", src_vec_type, lo, hi);
280#endif
281         }
282         else {
283            if (util_cpu_caps.has_sse4_1) {
284               return lp_build_intrinsic_binary(builder, "llvm.x86.sse41.packusdw", dst_vec_type, lo, hi);
285            }
286            else {
287               /* use generic shuffle below */
288               res = NULL;
289            }
290         }
291         break;
292
293      case 16:
294         if(dst_type.sign)
295#if HAVE_LLVM >= 0x0207
296            res = lp_build_intrinsic_binary(builder, "llvm.x86.sse2.packsswb.128", dst_vec_type, lo, hi);
297#else
298            res = lp_build_intrinsic_binary(builder, "llvm.x86.sse2.packsswb.128", src_vec_type, lo, hi);
299#endif
300         else
301#if HAVE_LLVM >= 0x0207
302            res = lp_build_intrinsic_binary(builder, "llvm.x86.sse2.packuswb.128", dst_vec_type, lo, hi);
303#else
304            res = lp_build_intrinsic_binary(builder, "llvm.x86.sse2.packuswb.128", src_vec_type, lo, hi);
305#endif
306         break;
307
308      default:
309         assert(0);
310         return LLVMGetUndef(dst_vec_type);
311         break;
312      }
313
314      if (res) {
315         res = LLVMBuildBitCast(builder, res, dst_vec_type, "");
316         return res;
317      }
318   }
319
320   /* generic shuffle */
321   lo = LLVMBuildBitCast(builder, lo, dst_vec_type, "");
322   hi = LLVMBuildBitCast(builder, hi, dst_vec_type, "");
323
324   shuffle = lp_build_const_pack_shuffle(dst_type.length);
325
326   res = LLVMBuildShuffleVector(builder, lo, hi, shuffle, "");
327
328   return res;
329}
330
331
332
333/**
334 * Non-interleaved pack and saturate.
335 *
336 * Same as lp_build_pack2 but will saturate values so that they fit into the
337 * destination type.
338 */
339LLVMValueRef
340lp_build_packs2(LLVMBuilderRef builder,
341                struct lp_type src_type,
342                struct lp_type dst_type,
343                LLVMValueRef lo,
344                LLVMValueRef hi)
345{
346   boolean clamp;
347
348   assert(!src_type.floating);
349   assert(!dst_type.floating);
350   assert(src_type.sign == dst_type.sign);
351   assert(src_type.width == dst_type.width * 2);
352   assert(src_type.length * 2 == dst_type.length);
353
354   clamp = TRUE;
355
356   /* All X86 SSE non-interleaved pack instructions take signed inputs and
357    * saturate them, so no need to clamp for those cases. */
358   if(util_cpu_caps.has_sse2 &&
359      src_type.width * src_type.length == 128 &&
360      src_type.sign)
361      clamp = FALSE;
362
363   if(clamp) {
364      struct lp_build_context bld;
365      unsigned dst_bits = dst_type.sign ? dst_type.width - 1 : dst_type.width;
366      LLVMValueRef dst_max = lp_build_const_int_vec(src_type, ((unsigned long long)1 << dst_bits) - 1);
367      lp_build_context_init(&bld, builder, src_type);
368      lo = lp_build_min(&bld, lo, dst_max);
369      hi = lp_build_min(&bld, hi, dst_max);
370      /* FIXME: What about lower bound? */
371   }
372
373   return lp_build_pack2(builder, src_type, dst_type, lo, hi);
374}
375
376
377/**
378 * Truncate the bit width.
379 *
380 * TODO: Handle saturation consistently.
381 */
382LLVMValueRef
383lp_build_pack(LLVMBuilderRef builder,
384              struct lp_type src_type,
385              struct lp_type dst_type,
386              boolean clamped,
387              const LLVMValueRef *src, unsigned num_srcs)
388{
389   LLVMValueRef (*pack2)(LLVMBuilderRef builder,
390                         struct lp_type src_type,
391                         struct lp_type dst_type,
392                         LLVMValueRef lo,
393                         LLVMValueRef hi);
394   LLVMValueRef tmp[LP_MAX_VECTOR_LENGTH];
395   unsigned i;
396
397
398   /* Register width must remain constant */
399   assert(src_type.width * src_type.length == dst_type.width * dst_type.length);
400
401   /* We must not loose or gain channels. Only precision */
402   assert(src_type.length * num_srcs == dst_type.length);
403
404   if(clamped)
405      pack2 = &lp_build_pack2;
406   else
407      pack2 = &lp_build_packs2;
408
409   for(i = 0; i < num_srcs; ++i)
410      tmp[i] = src[i];
411
412   while(src_type.width > dst_type.width) {
413      struct lp_type tmp_type = src_type;
414
415      tmp_type.width /= 2;
416      tmp_type.length *= 2;
417
418      /* Take in consideration the sign changes only in the last step */
419      if(tmp_type.width == dst_type.width)
420         tmp_type.sign = dst_type.sign;
421
422      num_srcs /= 2;
423
424      for(i = 0; i < num_srcs; ++i)
425         tmp[i] = pack2(builder, src_type, tmp_type, tmp[2*i + 0], tmp[2*i + 1]);
426
427      src_type = tmp_type;
428   }
429
430   assert(num_srcs == 1);
431
432   return tmp[0];
433}
434
435
436/**
437 * Truncate or expand the bitwidth.
438 *
439 * NOTE: Getting the right sign flags is crucial here, as we employ some
440 * intrinsics that do saturation.
441 */
442void
443lp_build_resize(LLVMBuilderRef builder,
444                struct lp_type src_type,
445                struct lp_type dst_type,
446                const LLVMValueRef *src, unsigned num_srcs,
447                LLVMValueRef *dst, unsigned num_dsts)
448{
449   LLVMValueRef tmp[LP_MAX_VECTOR_LENGTH];
450   unsigned i;
451
452   /*
453    * We don't support float <-> int conversion here. That must be done
454    * before/after calling this function.
455    */
456   assert(src_type.floating == dst_type.floating);
457
458   /*
459    * We don't support double <-> float conversion yet, although it could be
460    * added with little effort.
461    */
462   assert((!src_type.floating && !dst_type.floating) ||
463          src_type.width == dst_type.width);
464
465   /* We must not loose or gain channels. Only precision */
466   assert(src_type.length * num_srcs == dst_type.length * num_dsts);
467
468   /* We don't support M:N conversion, only 1:N, M:1, or 1:1 */
469   assert(num_srcs == 1 || num_dsts == 1);
470
471   assert(src_type.length <= LP_MAX_VECTOR_LENGTH);
472   assert(dst_type.length <= LP_MAX_VECTOR_LENGTH);
473   assert(num_srcs <= LP_MAX_VECTOR_LENGTH);
474   assert(num_dsts <= LP_MAX_VECTOR_LENGTH);
475
476   if (src_type.width > dst_type.width) {
477      /*
478       * Truncate bit width.
479       */
480
481      assert(num_dsts == 1);
482
483      if (src_type.width * src_type.length == dst_type.width * dst_type.length) {
484        /*
485         * Register width remains constant -- use vector packing intrinsics
486         */
487
488         tmp[0] = lp_build_pack(builder, src_type, dst_type, TRUE, src, num_srcs);
489      }
490      else {
491         /*
492          * Do it element-wise.
493          */
494
495         assert(src_type.length == dst_type.length);
496         tmp[0] = lp_build_undef(dst_type);
497         for (i = 0; i < dst_type.length; ++i) {
498            LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), i, 0);
499            LLVMValueRef val = LLVMBuildExtractElement(builder, src[0], index, "");
500            val = LLVMBuildTrunc(builder, val, lp_build_elem_type(dst_type), "");
501            tmp[0] = LLVMBuildInsertElement(builder, tmp[0], val, index, "");
502         }
503      }
504   }
505   else if (src_type.width < dst_type.width) {
506      /*
507       * Expand bit width.
508       */
509
510      assert(num_srcs == 1);
511
512      if (src_type.width * src_type.length == dst_type.width * dst_type.length) {
513         /*
514          * Register width remains constant -- use vector unpack intrinsics
515          */
516         lp_build_unpack(builder, src_type, dst_type, src[0], tmp, num_dsts);
517      }
518      else {
519         /*
520          * Do it element-wise.
521          */
522
523         assert(src_type.length == dst_type.length);
524         tmp[0] = lp_build_undef(dst_type);
525         for (i = 0; i < dst_type.length; ++i) {
526            LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), i, 0);
527            LLVMValueRef val = LLVMBuildExtractElement(builder, src[0], index, "");
528
529            if (src_type.sign && dst_type.sign) {
530               val = LLVMBuildSExt(builder, val, lp_build_elem_type(dst_type), "");
531            } else {
532               val = LLVMBuildZExt(builder, val, lp_build_elem_type(dst_type), "");
533            }
534            tmp[0] = LLVMBuildInsertElement(builder, tmp[0], val, index, "");
535         }
536      }
537   }
538   else {
539      /*
540       * No-op
541       */
542
543      assert(num_srcs == 1);
544      assert(num_dsts == 1);
545
546      tmp[0] = src[0];
547   }
548
549   for(i = 0; i < num_dsts; ++i)
550      dst[i] = tmp[i];
551}
552
553
554