lp_bld_const.c revision 6ed726b8fc6210a41fe325591e1428d19f419108
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 constant building. 32 * 33 * @author Jose Fonseca <jfonseca@vmware.com> 34 */ 35 36#include <float.h> 37 38#include "util/u_debug.h" 39 40#include "lp_bld_type.h" 41#include "lp_bld_const.h" 42 43 44unsigned 45lp_mantissa(struct lp_type type) 46{ 47 assert(type.floating); 48 49 if(type.floating) { 50 switch(type.width) { 51 case 32: 52 return 23; 53 case 64: 54 return 53; 55 default: 56 assert(0); 57 return 0; 58 } 59 } 60 else { 61 if(type.sign) 62 return type.width - 1; 63 else 64 return type.width; 65 } 66} 67 68 69/** 70 * Shift of the unity. 71 * 72 * Same as lp_const_scale(), but in terms of shifts. 73 */ 74unsigned 75lp_const_shift(struct lp_type type) 76{ 77 if(type.floating) 78 return 0; 79 else if(type.fixed) 80 return type.width/2; 81 else if(type.norm) 82 return type.sign ? type.width - 1 : type.width; 83 else 84 return 0; 85} 86 87 88unsigned 89lp_const_offset(struct lp_type type) 90{ 91 if(type.floating || type.fixed) 92 return 0; 93 else if(type.norm) 94 return 1; 95 else 96 return 0; 97} 98 99 100/** 101 * Scaling factor between the LLVM native value and its interpretation. 102 * 103 * This is 1.0 for all floating types and unnormalized integers, and something 104 * else for the fixed points types and normalized integers. 105 */ 106double 107lp_const_scale(struct lp_type type) 108{ 109 unsigned long long llscale; 110 double dscale; 111 112 llscale = (unsigned long long)1 << lp_const_shift(type); 113 llscale -= lp_const_offset(type); 114 dscale = (double)llscale; 115 assert((unsigned long long)dscale == llscale); 116 117 return dscale; 118} 119 120 121/** 122 * Minimum value representable by the type. 123 */ 124double 125lp_const_min(struct lp_type type) 126{ 127 unsigned bits; 128 129 if(!type.sign) 130 return 0.0; 131 132 if(type.norm) 133 return -1.0; 134 135 if (type.floating) { 136 switch(type.width) { 137 case 32: 138 return -FLT_MAX; 139 case 64: 140 return -DBL_MAX; 141 default: 142 assert(0); 143 return 0.0; 144 } 145 } 146 147 if(type.fixed) 148 /* FIXME: consider the fractional bits? */ 149 bits = type.width / 2 - 1; 150 else 151 bits = type.width - 1; 152 153 return (double)-((long long)1 << bits); 154} 155 156 157/** 158 * Maximum value representable by the type. 159 */ 160double 161lp_const_max(struct lp_type type) 162{ 163 unsigned bits; 164 165 if(type.norm) 166 return 1.0; 167 168 if (type.floating) { 169 switch(type.width) { 170 case 32: 171 return FLT_MAX; 172 case 64: 173 return DBL_MAX; 174 default: 175 assert(0); 176 return 0.0; 177 } 178 } 179 180 if(type.fixed) 181 bits = type.width / 2; 182 else 183 bits = type.width; 184 185 if(type.sign) 186 bits -= 1; 187 188 return (double)(((unsigned long long)1 << bits) - 1); 189} 190 191 192double 193lp_const_eps(struct lp_type type) 194{ 195 if (type.floating) { 196 switch(type.width) { 197 case 32: 198 return FLT_EPSILON; 199 case 64: 200 return DBL_EPSILON; 201 default: 202 assert(0); 203 return 0.0; 204 } 205 } 206 else { 207 double scale = lp_const_scale(type); 208 return 1.0/scale; 209 } 210} 211 212 213LLVMValueRef 214lp_build_undef(struct lp_type type) 215{ 216 LLVMTypeRef vec_type = lp_build_vec_type(type); 217 return LLVMGetUndef(vec_type); 218} 219 220 221LLVMValueRef 222lp_build_zero(struct lp_type type) 223{ 224 if (type.length == 1) { 225 if (type.floating) 226 return LLVMConstReal(LLVMFloatType(), 0.0); 227 else 228 return LLVMConstInt(LLVMIntType(type.width), 0, 0); 229 } 230 else { 231 LLVMTypeRef vec_type = lp_build_vec_type(type); 232 return LLVMConstNull(vec_type); 233 } 234} 235 236 237LLVMValueRef 238lp_build_one(struct lp_type type) 239{ 240 LLVMTypeRef elem_type; 241 LLVMValueRef elems[LP_MAX_VECTOR_LENGTH]; 242 unsigned i; 243 244 assert(type.length <= LP_MAX_VECTOR_LENGTH); 245 246 elem_type = lp_build_elem_type(type); 247 248 if(type.floating) 249 elems[0] = LLVMConstReal(elem_type, 1.0); 250 else if(type.fixed) 251 elems[0] = LLVMConstInt(elem_type, 1LL << (type.width/2), 0); 252 else if(!type.norm) 253 elems[0] = LLVMConstInt(elem_type, 1, 0); 254 else if(type.sign) 255 elems[0] = LLVMConstInt(elem_type, (1LL << (type.width - 1)) - 1, 0); 256 else { 257 /* special case' -- 1.0 for normalized types is more easily attained if 258 * we start with a vector consisting of all bits set */ 259 LLVMTypeRef vec_type = LLVMVectorType(elem_type, type.length); 260 LLVMValueRef vec = LLVMConstAllOnes(vec_type); 261 262#if 0 263 if(type.sign) 264 /* TODO: Unfortunately this caused "Tried to create a shift operation 265 * on a non-integer type!" */ 266 vec = LLVMConstLShr(vec, lp_build_const_int_vec(type, 1)); 267#endif 268 269 return vec; 270 } 271 272 for(i = 1; i < type.length; ++i) 273 elems[i] = elems[0]; 274 275 if (type.length == 1) 276 return elems[0]; 277 else 278 return LLVMConstVector(elems, type.length); 279} 280 281 282/** 283 * Build constant-valued element from a scalar value. 284 */ 285LLVMValueRef 286lp_build_const_elem(struct lp_type type, 287 double val) 288{ 289 LLVMTypeRef elem_type = lp_build_elem_type(type); 290 LLVMValueRef elem; 291 292 if(type.floating) { 293 elem = LLVMConstReal(elem_type, val); 294 } 295 else { 296 double dscale = lp_const_scale(type); 297 298 elem = LLVMConstInt(elem_type, val*dscale + 0.5, 0); 299 } 300 301 return elem; 302} 303 304 305/** 306 * Build constant-valued vector from a scalar value. 307 */ 308LLVMValueRef 309lp_build_const_vec(struct lp_type type, 310 double val) 311{ 312 if (type.length == 1) { 313 return lp_build_const_elem(type, val); 314 } else { 315 LLVMValueRef elems[LP_MAX_VECTOR_LENGTH]; 316 unsigned i; 317 elems[0] = lp_build_const_elem(type, val); 318 for(i = 1; i < type.length; ++i) 319 elems[i] = elems[0]; 320 return LLVMConstVector(elems, type.length); 321 } 322} 323 324 325LLVMValueRef 326lp_build_const_int_vec(struct lp_type type, 327 long long val) 328{ 329 LLVMTypeRef elem_type = lp_build_int_elem_type(type); 330 LLVMValueRef elems[LP_MAX_VECTOR_LENGTH]; 331 unsigned i; 332 333 assert(type.length <= LP_MAX_VECTOR_LENGTH); 334 335 for(i = 0; i < type.length; ++i) 336 elems[i] = LLVMConstInt(elem_type, val, type.sign ? 1 : 0); 337 338 if (type.length == 1) 339 return elems[0]; 340 341 return LLVMConstVector(elems, type.length); 342} 343 344 345LLVMValueRef 346lp_build_const_aos(struct lp_type type, 347 double r, double g, double b, double a, 348 const unsigned char *swizzle) 349{ 350 const unsigned char default_swizzle[4] = {0, 1, 2, 3}; 351 LLVMTypeRef elem_type; 352 LLVMValueRef elems[LP_MAX_VECTOR_LENGTH]; 353 unsigned i; 354 355 assert(type.length % 4 == 0); 356 assert(type.length <= LP_MAX_VECTOR_LENGTH); 357 358 elem_type = lp_build_elem_type(type); 359 360 if(swizzle == NULL) 361 swizzle = default_swizzle; 362 363 if(type.floating) { 364 elems[swizzle[0]] = LLVMConstReal(elem_type, r); 365 elems[swizzle[1]] = LLVMConstReal(elem_type, g); 366 elems[swizzle[2]] = LLVMConstReal(elem_type, b); 367 elems[swizzle[3]] = LLVMConstReal(elem_type, a); 368 } 369 else { 370 double dscale = lp_const_scale(type); 371 372 elems[swizzle[0]] = LLVMConstInt(elem_type, r*dscale + 0.5, 0); 373 elems[swizzle[1]] = LLVMConstInt(elem_type, g*dscale + 0.5, 0); 374 elems[swizzle[2]] = LLVMConstInt(elem_type, b*dscale + 0.5, 0); 375 elems[swizzle[3]] = LLVMConstInt(elem_type, a*dscale + 0.5, 0); 376 } 377 378 for(i = 4; i < type.length; ++i) 379 elems[i] = elems[i % 4]; 380 381 return LLVMConstVector(elems, type.length); 382} 383 384 385/** 386 * @param mask TGSI_WRITEMASK_xxx 387 */ 388LLVMValueRef 389lp_build_const_mask_aos(struct lp_type type, 390 unsigned mask) 391{ 392 LLVMTypeRef elem_type = LLVMIntType(type.width); 393 LLVMValueRef masks[LP_MAX_VECTOR_LENGTH]; 394 unsigned i, j; 395 396 assert(type.length <= LP_MAX_VECTOR_LENGTH); 397 398 for (j = 0; j < type.length; j += 4) { 399 for( i = 0; i < 4; ++i) { 400 masks[j + i] = LLVMConstInt(elem_type, 401 mask & (1 << i) ? ~0ULL : 0, 402 1); 403 } 404 } 405 406 return LLVMConstVector(masks, type.length); 407} 408