Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /**************************************************************************
  2.  *
  3.  * Copyright 2010 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 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.
  21.  *
  22.  * The above copyright notice and this permission notice (including the
  23.  * next paragraph) shall be included in all copies or substantial portions
  24.  * of the Software.
  25.  *
  26.  **************************************************************************/
  27.  
  28.  
  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"
  36.  
  37.  
  38. /**
  39.  * Get the pointer to one element from scatter positions in memory.
  40.  *
  41.  * @sa lp_build_gather()
  42.  */
  43. LLVMValueRef
  44. lp_build_gather_elem_ptr(struct gallivm_state *gallivm,
  45.                          unsigned length,
  46.                          LLVMValueRef base_ptr,
  47.                          LLVMValueRef offsets,
  48.                          unsigned i)
  49. {
  50.    LLVMValueRef offset;
  51.    LLVMValueRef ptr;
  52.  
  53.    assert(LLVMTypeOf(base_ptr) == LLVMPointerType(LLVMInt8TypeInContext(gallivm->context), 0));
  54.  
  55.    if (length == 1) {
  56.       assert(i == 0);
  57.       offset = offsets;
  58.    } else {
  59.       LLVMValueRef index = lp_build_const_int32(gallivm, i);
  60.       offset = LLVMBuildExtractElement(gallivm->builder, offsets, index, "");
  61.    }
  62.  
  63.    ptr = LLVMBuildGEP(gallivm->builder, base_ptr, &offset, 1, "");
  64.  
  65.    return ptr;
  66. }
  67.  
  68.  
  69. /**
  70.  * Gather one element from scatter positions in memory.
  71.  *
  72.  * @sa lp_build_gather()
  73.  */
  74. LLVMValueRef
  75. lp_build_gather_elem(struct gallivm_state *gallivm,
  76.                      unsigned length,
  77.                      unsigned src_width,
  78.                      unsigned dst_width,
  79.                      LLVMValueRef base_ptr,
  80.                      LLVMValueRef offsets,
  81.                      unsigned i,
  82.                      boolean vector_justify)
  83. {
  84.    LLVMTypeRef src_type = LLVMIntTypeInContext(gallivm->context, src_width);
  85.    LLVMTypeRef src_ptr_type = LLVMPointerType(src_type, 0);
  86.    LLVMTypeRef dst_elem_type = LLVMIntTypeInContext(gallivm->context, dst_width);
  87.    LLVMValueRef ptr;
  88.    LLVMValueRef res;
  89.  
  90.    assert(LLVMTypeOf(base_ptr) == LLVMPointerType(LLVMInt8TypeInContext(gallivm->context), 0));
  91.  
  92.    ptr = lp_build_gather_elem_ptr(gallivm, length, base_ptr, offsets, i);
  93.    ptr = LLVMBuildBitCast(gallivm->builder, ptr, src_ptr_type, "");
  94.    res = LLVMBuildLoad(gallivm->builder, ptr, "");
  95.  
  96.    assert(src_width <= dst_width);
  97.    if (src_width > dst_width) {
  98.       res = LLVMBuildTrunc(gallivm->builder, res, dst_elem_type, "");
  99.    } else if (src_width < dst_width) {
  100.       res = LLVMBuildZExt(gallivm->builder, res, dst_elem_type, "");
  101.       if (vector_justify) {
  102. #ifdef PIPE_ARCH_BIG_ENDIAN
  103.          res = LLVMBuildShl(gallivm->builder, res,
  104.                             LLVMConstInt(dst_elem_type, dst_width - src_width, 0), "");
  105. #endif
  106.       }
  107.    }
  108.  
  109.    return res;
  110. }
  111.  
  112.  
  113. /**
  114.  * Gather elements from scatter positions in memory into a single vector.
  115.  * Use for fetching texels from a texture.
  116.  * For SSE, typical values are length=4, src_width=32, dst_width=32.
  117.  *
  118.  * When src_width < dst_width, the return value can be justified in
  119.  * one of two ways:
  120.  * "integer justification" is used when the caller treats the destination
  121.  * as a packed integer bitmask, as described by the channels' "shift" and
  122.  * "width" fields;
  123.  * "vector justification" is used when the caller casts the destination
  124.  * to a vector and needs channel X to be in vector element 0.
  125.  *
  126.  * @param length length of the offsets
  127.  * @param src_width src element width in bits
  128.  * @param dst_width result element width in bits (src will be expanded to fit)
  129.  * @param base_ptr base pointer, should be a i8 pointer type.
  130.  * @param offsets vector with offsets
  131.  * @param vector_justify select vector rather than integer justification
  132.  */
  133. LLVMValueRef
  134. lp_build_gather(struct gallivm_state *gallivm,
  135.                 unsigned length,
  136.                 unsigned src_width,
  137.                 unsigned dst_width,
  138.                 LLVMValueRef base_ptr,
  139.                 LLVMValueRef offsets,
  140.                 boolean vector_justify)
  141. {
  142.    LLVMValueRef res;
  143.  
  144.    if (length == 1) {
  145.       /* Scalar */
  146.       return lp_build_gather_elem(gallivm, length,
  147.                                   src_width, dst_width,
  148.                                   base_ptr, offsets, 0, vector_justify);
  149.    } else {
  150.       /* Vector */
  151.  
  152.       LLVMTypeRef dst_elem_type = LLVMIntTypeInContext(gallivm->context, dst_width);
  153.       LLVMTypeRef dst_vec_type = LLVMVectorType(dst_elem_type, length);
  154.       unsigned i;
  155.  
  156.       res = LLVMGetUndef(dst_vec_type);
  157.       for (i = 0; i < length; ++i) {
  158.          LLVMValueRef index = lp_build_const_int32(gallivm, i);
  159.          LLVMValueRef elem;
  160.          elem = lp_build_gather_elem(gallivm, length,
  161.                                      src_width, dst_width,
  162.                                      base_ptr, offsets, i, vector_justify);
  163.          res = LLVMBuildInsertElement(gallivm->builder, res, elem, index, "");
  164.       }
  165.    }
  166.  
  167.    return res;
  168. }
  169.  
  170. LLVMValueRef
  171. lp_build_gather_values(struct gallivm_state * gallivm,
  172.                        LLVMValueRef * values,
  173.                        unsigned value_count)
  174. {
  175.    LLVMTypeRef vec_type = LLVMVectorType(LLVMTypeOf(values[0]), value_count);
  176.    LLVMBuilderRef builder = gallivm->builder;
  177.    LLVMValueRef vec = LLVMGetUndef(vec_type);
  178.    unsigned i;
  179.  
  180.    for (i = 0; i < value_count; i++) {
  181.       LLVMValueRef index = lp_build_const_int32(gallivm, i);
  182.       vec = LLVMBuildInsertElement(builder, vec, values[i], index, "");
  183.    }
  184.    return vec;
  185. }
  186.