summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/llvmpipe/lp_bld_const.c
blob: 21487365eaebb5483dd0718503bb7f942af2713d (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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/**************************************************************************
 *
 * 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 functions for constant building.
 *
 * @author Jose Fonseca <jfonseca@vmware.com>
 */

#include <float.h>

#include "util/u_debug.h"

#include "lp_bld_type.h"
#include "lp_bld_const.h"


unsigned
lp_mantissa(union lp_type type)
{
   assert(type.floating);

   if(type.floating) {
      switch(type.width) {
      case 32:
         return 23;
      case 64:
         return 53;
      default:
         assert(0);
         return 0;
      }
   }
   else {
      if(type.sign)
         return type.width - 1;
      else
         return type.width;
   }
}


/**
 * Shift of the unity.
 *
 * Same as lp_const_scale(), but in terms of shifts.
 */
unsigned
lp_const_shift(union lp_type type)
{
   if(type.floating)
      return 0;
   else if(type.fixed)
      return type.width/2;
   else if(type.norm)
      return type.sign ? type.width - 1 : type.width;
   else
      return 0;
}


unsigned
lp_const_offset(union lp_type type)
{
   if(type.floating || type.fixed)
      return 0;
   else if(type.norm)
      return 1;
   else
      return 0;
}


/**
 * Scaling factor between the LLVM native value and its interpretation.
 *
 * This is 1.0 for all floating types and unnormalized integers, and something
 * else for the fixed points types and normalized integers.
 */
double
lp_const_scale(union lp_type type)
{
   unsigned long long llscale;
   double dscale;

   llscale = (unsigned long long)1 << lp_const_shift(type);
   llscale -= lp_const_offset(type);
   dscale = (double)llscale;
   assert((unsigned long long)dscale == llscale);

   return dscale;
}


/**
 * Minimum value representable by the type.
 */
double
lp_const_min(union lp_type type)
{
   unsigned bits;

   if(!type.sign)
      return 0.0;

   if(type.norm)
      return -1.0;

   if (type.floating) {
      switch(type.width) {
      case 32:
         return -FLT_MAX;
      case 64:
         return -DBL_MAX;
      default:
         assert(0);
         return 0.0;
      }
   }

   if(type.fixed)
      /* FIXME: consider the fractional bits? */
      bits = type.width / 2 - 1;
   else
      bits = type.width - 1;

   return (double)-((long long)1 << bits);
}


/**
 * Maximum value representable by the type.
 */
double
lp_const_max(union lp_type type)
{
   unsigned bits;

   if(type.norm)
      return 1.0;

   if (type.floating) {
      switch(type.width) {
      case 32:
         return FLT_MAX;
      case 64:
         return DBL_MAX;
      default:
         assert(0);
         return 0.0;
      }
   }

   if(type.fixed)
      bits = type.width / 2;
   else
      bits = type.width;

   if(type.sign)
      bits -= 1;

   return (double)(((unsigned long long)1 << bits) - 1);
}


double
lp_const_eps(union lp_type type)
{
   if (type.floating) {
      switch(type.width) {
      case 32:
         return FLT_EPSILON;
      case 64:
         return DBL_EPSILON;
      default:
         assert(0);
         return 0.0;
      }
   }
   else {
      double scale = lp_const_scale(type);
      return 1.0/scale;
   }
}


LLVMValueRef
lp_build_undef(union lp_type type)
{
   LLVMTypeRef vec_type = lp_build_vec_type(type);
   return LLVMGetUndef(vec_type);
}
               

LLVMValueRef
lp_build_zero(union lp_type type)
{
   LLVMTypeRef vec_type = lp_build_vec_type(type);
   return LLVMConstNull(vec_type);
}
               

LLVMValueRef
lp_build_one(union lp_type type)
{
   LLVMTypeRef elem_type;
   LLVMValueRef elems[LP_MAX_VECTOR_LENGTH];
   unsigned i;

   assert(type.length <= LP_MAX_VECTOR_LENGTH);

   elem_type = lp_build_elem_type(type);

   if(type.floating)
      elems[0] = LLVMConstReal(elem_type, 1.0);
   else if(type.fixed)
      elems[0] = LLVMConstInt(elem_type, 1LL << (type.width/2), 0);
   else if(!type.norm)
      elems[0] = LLVMConstInt(elem_type, 1, 0);
   else if(type.sign)
      elems[0] = LLVMConstInt(elem_type, (1LL << (type.width - 1)) - 1, 0);
   else {
      /* special case' -- 1.0 for normalized types is more easily attained if
       * we start with a vector consisting of all bits set */
      LLVMTypeRef vec_type = LLVMVectorType(elem_type, type.length);
      LLVMValueRef vec = LLVMConstAllOnes(vec_type);

#if 0
      if(type.sign)
         /* TODO: Unfortunately this caused "Tried to create a shift operation
          * on a non-integer type!" */
         vec = LLVMConstLShr(vec, lp_build_int_const_scalar(type, 1));
#endif

      return vec;
   }

   for(i = 1; i < type.length; ++i)
      elems[i] = elems[0];

   return LLVMConstVector(elems, type.length);
}
               

LLVMValueRef
lp_build_const_scalar(union lp_type type,
                      double val)
{
   LLVMTypeRef elem_type = lp_build_elem_type(type);
   LLVMValueRef elems[LP_MAX_VECTOR_LENGTH];
   unsigned i;

   assert(type.length <= LP_MAX_VECTOR_LENGTH);

   if(type.floating) {
      elems[0] = LLVMConstReal(elem_type, val);
   }
   else {
      double dscale = lp_const_scale(type);

      elems[0] = LLVMConstInt(elem_type, val*dscale + 0.5, 0);
   }

   for(i = 1; i < type.length; ++i)
      elems[i] = elems[0];

   return LLVMConstVector(elems, type.length);
}


LLVMValueRef
lp_build_int_const_scalar(union lp_type type,
                          long long val)
{
   LLVMTypeRef elem_type = lp_build_int_elem_type(type);
   LLVMValueRef elems[LP_MAX_VECTOR_LENGTH];
   unsigned i;

   assert(type.length <= LP_MAX_VECTOR_LENGTH);

   for(i = 0; i < type.length; ++i)
      elems[i] = LLVMConstInt(elem_type, val, type.sign ? 1 : 0);

   return LLVMConstVector(elems, type.length);
}


LLVMValueRef
lp_build_const_aos(union lp_type 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;
   LLVMValueRef elems[LP_MAX_VECTOR_LENGTH];
   unsigned i;

   assert(type.length % 4 == 0);
   assert(type.length <= LP_MAX_VECTOR_LENGTH);

   elem_type = lp_build_elem_type(type);

   if(swizzle == NULL)
      swizzle = default_swizzle;

   if(type.floating) {
      elems[swizzle[0]] = LLVMConstReal(elem_type, r);
      elems[swizzle[1]] = LLVMConstReal(elem_type, g);
      elems[swizzle[2]] = LLVMConstReal(elem_type, b);
      elems[swizzle[3]] = LLVMConstReal(elem_type, a);
   }
   else {
      double dscale = lp_const_scale(type);

      elems[swizzle[0]] = LLVMConstInt(elem_type, r*dscale + 0.5, 0);
      elems[swizzle[1]] = LLVMConstInt(elem_type, g*dscale + 0.5, 0);
      elems[swizzle[2]] = LLVMConstInt(elem_type, b*dscale + 0.5, 0);
      elems[swizzle[3]] = LLVMConstInt(elem_type, a*dscale + 0.5, 0);
   }

   for(i = 4; i < type.length; ++i)
      elems[i] = elems[i % 4];

   return LLVMConstVector(elems, type.length);
}


LLVMValueRef
lp_build_const_mask_aos(union lp_type type,
                        boolean cond[4])
{
   LLVMTypeRef elem_type = LLVMIntType(type.width);
   LLVMValueRef masks[LP_MAX_VECTOR_LENGTH];
   unsigned i, j;

   assert(type.length <= LP_MAX_VECTOR_LENGTH);

   for(j = 0; j < type.length; j += 4)
      for(i = 0; i < 4; ++i)
         masks[j + i] = LLVMConstInt(elem_type, cond[i] ? ~0 : 0, 0);

   return LLVMConstVector(masks, type.length);
}