summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/llvmpipe/lp_bld_arit.c
blob: 7e5af284aabc84c6ab4f038c41b107704c3e033d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/**************************************************************************
 *
 * Copyright 2009 VMware, Inc.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/


/**
 * @file
 * Helper
 *
 * LLVM IR doesn't support all basic arithmetic operations we care about (most
 * notably min/max and saturated operations), and it is often necessary to
 * resort machine-specific intrinsics directly. The functions here hide all
 * these implementation details from the other modules.
 *
 * We also do simple expressions simplification here. Reasons are:
 * - it is very easy given we have all necessary information readily available
 * - LLVM optimization passes fail to simplify several vector expressions
 * - We often know value constraints which the optimization passes have no way
 *   of knowing, such as when source arguments are known to be in [0, 1] range.
 *
 * @author Jose Fonseca <jfonseca@vmware.com>
 */


#include "pipe/p_state.h"

#include "lp_bld_arit.h"


LLVMValueRef
lp_build_const_aos(LLVMTypeRef type, 
                   double r, double g, double b, double a, 
                   const unsigned char *swizzle)
{
   const unsigned char default_swizzle[4] = {0, 1, 2, 3};
   LLVMTypeRef elem_type;
   unsigned num_elems;
   unsigned elem_width;
   LLVMValueRef elems[LP_MAX_VECTOR_SIZE];
   double scale;
   unsigned i;

   num_elems = LLVMGetVectorSize(type);
   assert(num_elems % 4 == 0);
   assert(num_elems < LP_MAX_VECTOR_SIZE);

   elem_type = LLVMGetElementType(type);

   if(swizzle == NULL)
      swizzle = default_swizzle;

   switch(LLVMGetTypeKind(elem_type)) {
   case LLVMFloatTypeKind:
      for(i = 0; i < num_elems; i += 4) {
         elems[i + swizzle[0]] = LLVMConstReal(elem_type, r);
         elems[i + swizzle[1]] = LLVMConstReal(elem_type, g);
         elems[i + swizzle[2]] = LLVMConstReal(elem_type, b);
         elems[i + swizzle[3]] = LLVMConstReal(elem_type, a);
      }
      break;

   case LLVMIntegerTypeKind:
      elem_width = LLVMGetIntTypeWidth(elem_type);
      assert(elem_width <= 32);
      scale = (double)((1 << elem_width) - 1);
      for(i = 0; i < num_elems; i += 4) {
         elems[i + swizzle[0]] = LLVMConstInt(elem_type, r*scale + 0.5, 0);
         elems[i + swizzle[1]] = LLVMConstInt(elem_type, g*scale + 0.5, 0);
         elems[i + swizzle[2]] = LLVMConstInt(elem_type, b*scale + 0.5, 0);
         elems[i + swizzle[3]] = LLVMConstInt(elem_type, a*scale + 0.5, 0);
      }
      break;

   default:
      assert(0);
      return LLVMGetUndef(type);
   }

   return LLVMConstVector(elems, num_elems);
}
               

static LLVMValueRef
lp_build_intrinsic_binary(LLVMBuilderRef builder,
                          const char *name,
                          LLVMValueRef a,
                          LLVMValueRef b)
{
   LLVMModuleRef module = LLVMGetGlobalParent(LLVMGetBasicBlockParent(LLVMGetInsertBlock(builder)));
   LLVMValueRef function;
   LLVMValueRef args[2];

   function = LLVMGetNamedFunction(module, name);
   if(!function) {
      LLVMTypeRef type = LLVMTypeOf(a);
      LLVMTypeRef arg_types[2];
      arg_types[0] = type;
      arg_types[1] = type;
      function = LLVMAddFunction(module, name, LLVMFunctionType(type, arg_types, 2, 0));
      LLVMSetFunctionCallConv(function, LLVMCCallConv);
      LLVMSetLinkage(function, LLVMExternalLinkage);
   }
   assert(LLVMIsDeclaration(function));

   args[0] = a;
   args[1] = b;

   return LLVMBuildCall(builder, function, args, 2, "");
}


LLVMValueRef
lp_build_add(LLVMBuilderRef builder,
             LLVMValueRef a,
             LLVMValueRef b,
             LLVMValueRef zero)
{
   if(a == zero)
      return b;
   else if(b == zero)
      return a;
   else if(LLVMIsConstant(a) && LLVMIsConstant(b))
      return LLVMConstAdd(a, b);
   else
      return LLVMBuildAdd(builder, a, b, "");
}


LLVMValueRef
lp_build_sub(LLVMBuilderRef builder,
             LLVMValueRef a,
             LLVMValueRef b,
             LLVMValueRef zero)
{
   if(b == zero)
      return a;
   else if(a == b)
      return zero;
   else if(LLVMIsConstant(a) && LLVMIsConstant(b))
      return LLVMConstSub(a, b);
   else
      return LLVMBuildSub(builder, a, b, "");
}


LLVMValueRef
lp_build_mul(LLVMBuilderRef builder,
             LLVMValueRef a,
             LLVMValueRef b,
             LLVMValueRef zero,
             LLVMValueRef one)
{
   if(a == zero)
      return zero;
   else if(a == one)
      return b;
   else if(b == zero)
      return zero;
   else if(b == one)
      return a;
   else if(LLVMIsConstant(a) && LLVMIsConstant(b))
      return LLVMConstMul(a, b);
   else
      return LLVMBuildMul(builder, a, b, "");
}


LLVMValueRef
lp_build_min(LLVMBuilderRef builder,
             LLVMValueRef a,
             LLVMValueRef b)
{
   /* TODO: optimize the constant case */

#if defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)

   return lp_build_intrinsic_binary(builder, "llvm.x86.sse.min.ps", a, b);

#else

   LLVMValueRef cond = LLVMBuildFCmp(values->builder, LLVMRealULT, a, b, "");
   return LLVMBuildSelect(values->builder, cond, a, b, "");

#endif
}


LLVMValueRef
lp_build_max(LLVMBuilderRef builder,
             LLVMValueRef a,
             LLVMValueRef b)
{
   /* TODO: optimize the constant case */

#if defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)

   return lp_build_intrinsic_binary(builder, "llvm.x86.sse.max.ps", a, b);

#else

   LLVMValueRef cond = LLVMBuildFCmp(values->builder, LLVMRealULT, a, b, "");
   return LLVMBuildSelect(values->builder, cond, b, a, "");

#endif
}


LLVMValueRef
lp_build_add_sat(LLVMBuilderRef builder,
                 LLVMValueRef a,
                 LLVMValueRef b,
                 LLVMValueRef zero,
                 LLVMValueRef one)
{
   if(a == zero)
      return b;
   else if(b == zero)
      return a;
   else if(a == one || b == one)
      return one;
   else
      return lp_build_min(builder, lp_build_add(builder, a, b, zero), one);
}

LLVMValueRef
lp_build_sub_sat(LLVMBuilderRef builder,
                 LLVMValueRef a,
                 LLVMValueRef b,
                 LLVMValueRef zero,
                 LLVMValueRef one)
{
   if(b == zero)
      return a;
   else if(b == one)
      return zero;
   else
      return lp_build_max(builder, lp_build_sub(builder, a, b, zero), zero);
}

LLVMValueRef
lp_build_min_sat(LLVMBuilderRef builder,
                 LLVMValueRef a,
                 LLVMValueRef b,
                 LLVMValueRef zero,
                 LLVMValueRef one)
{
   if(a == zero || b == zero)
      return zero;
   else if(a == one)
      return b;
   else if(b == one)
      return a;
   else
      return lp_build_min(builder, a, b);
}


LLVMValueRef
lp_build_max_sat(LLVMBuilderRef builder,
                 LLVMValueRef a,
                 LLVMValueRef b,
                 LLVMValueRef zero,
                 LLVMValueRef one)
{
   if(a == zero)
      return b;
   else if(b == zero)
      return a;
   else if(a == one || b == one)
      return one;
   else
      return lp_build_max(builder, a, b);
}