lp_bld_conv.c revision 37f4c2f906c8e2a6df609a190e4ca9ff028b265b
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 type conversions. 32 * 33 * We want to use the fastest type for a given computation whenever feasible. 34 * The other side of this is that we need to be able convert between several 35 * types accurately and efficiently. 36 * 37 * Conversion between types of different bit width is quite complex since a 38 * 39 * To remember there are a few invariants in type conversions: 40 * 41 * - register width must remain constant: 42 * 43 * src_type.width * src_type.length == dst_type.width * dst_type.length 44 * 45 * - total number of elements must remain constant: 46 * 47 * src_type.length * num_srcs == dst_type.length * num_dsts 48 * 49 * It is not always possible to do the conversion both accurately and 50 * efficiently, usually due to lack of adequate machine instructions. In these 51 * cases it is important not to cut shortcuts here and sacrifice accuracy, as 52 * there this functions can be used anywhere. In the future we might have a 53 * precision parameter which can gauge the accuracy vs efficiency compromise, 54 * but for now if the data conversion between two stages happens to be the 55 * bottleneck, then most likely should just avoid converting at all and run 56 * both stages with the same type. 57 * 58 * Make sure to run lp_test_conv unit test after any change to this file. 59 * 60 * @author Jose Fonseca <jfonseca@vmware.com> 61 */ 62 63 64#include "util/u_debug.h" 65#include "util/u_math.h" 66 67#include "lp_bld_type.h" 68#include "lp_bld_const.h" 69#include "lp_bld_arit.h" 70#include "lp_bld_pack.h" 71#include "lp_bld_conv.h" 72 73 74/** 75 * Special case for converting clamped IEEE-754 floats to unsigned norms. 76 * 77 * The mathematical voodoo below may seem excessive but it is actually 78 * paramount we do it this way for several reasons. First, there is no single 79 * precision FP to unsigned integer conversion Intel SSE instruction. Second, 80 * secondly, even if there was, since the FP's mantissa takes only a fraction 81 * of register bits the typically scale and cast approach would require double 82 * precision for accurate results, and therefore half the throughput 83 * 84 * Although the result values can be scaled to an arbitrary bit width specified 85 * by dst_width, the actual result type will have the same width. 86 * 87 * Ex: src = { float, float, float, float } 88 * return { i32, i32, i32, i32 } where each value is in [0, 2^dst_width-1]. 89 */ 90LLVMValueRef 91lp_build_clamped_float_to_unsigned_norm(LLVMBuilderRef builder, 92 struct lp_type src_type, 93 unsigned dst_width, 94 LLVMValueRef src) 95{ 96 LLVMTypeRef int_vec_type = lp_build_int_vec_type(src_type); 97 LLVMValueRef res; 98 unsigned mantissa; 99 unsigned n; 100 unsigned long long ubound; 101 unsigned long long mask; 102 double scale; 103 double bias; 104 105 assert(src_type.floating); 106 107 mantissa = lp_mantissa(src_type); 108 109 /* We cannot carry more bits than the mantissa */ 110 n = MIN2(mantissa, dst_width); 111 112 /* This magic coefficients will make the desired result to appear in the 113 * lowest significant bits of the mantissa. 114 */ 115 ubound = ((unsigned long long)1 << n); 116 mask = ubound - 1; 117 scale = (double)mask/ubound; 118 bias = (double)((unsigned long long)1 << (mantissa - n)); 119 120 res = LLVMBuildMul(builder, src, lp_build_const_vec(src_type, scale), ""); 121 res = LLVMBuildAdd(builder, res, lp_build_const_vec(src_type, bias), ""); 122 res = LLVMBuildBitCast(builder, res, int_vec_type, ""); 123 124 if(dst_width > n) { 125 int shift = dst_width - n; 126 res = LLVMBuildShl(builder, res, lp_build_const_int_vec(src_type, shift), ""); 127 128 /* TODO: Fill in the empty lower bits for additional precision? */ 129 /* YES: this fixes progs/trivial/tri-z-eq.c. 130 * Otherwise vertex Z=1.0 values get converted to something like 131 * 0xfffffb00 and the test for equality with 0xffffffff fails. 132 */ 133#if 0 134 { 135 LLVMValueRef msb; 136 msb = LLVMBuildLShr(builder, res, lp_build_const_int_vec(src_type, dst_width - 1), ""); 137 msb = LLVMBuildShl(builder, msb, lp_build_const_int_vec(src_type, shift), ""); 138 msb = LLVMBuildSub(builder, msb, lp_build_const_int_vec(src_type, 1), ""); 139 res = LLVMBuildOr(builder, res, msb, ""); 140 } 141#elif 0 142 while(shift > 0) { 143 res = LLVMBuildOr(builder, res, LLVMBuildLShr(builder, res, lp_build_const_int_vec(src_type, n), ""), ""); 144 shift -= n; 145 n *= 2; 146 } 147#endif 148 } 149 else 150 res = LLVMBuildAnd(builder, res, lp_build_const_int_vec(src_type, mask), ""); 151 152 return res; 153} 154 155 156/** 157 * Inverse of lp_build_clamped_float_to_unsigned_norm above. 158 * Ex: src = { i32, i32, i32, i32 } with values in range [0, 2^src_width-1] 159 * return {float, float, float, float} with values in range [0, 1]. 160 */ 161LLVMValueRef 162lp_build_unsigned_norm_to_float(LLVMBuilderRef builder, 163 unsigned src_width, 164 struct lp_type dst_type, 165 LLVMValueRef src) 166{ 167 LLVMTypeRef vec_type = lp_build_vec_type(dst_type); 168 LLVMTypeRef int_vec_type = lp_build_int_vec_type(dst_type); 169 LLVMValueRef bias_; 170 LLVMValueRef res; 171 unsigned mantissa; 172 unsigned n; 173 unsigned long long ubound; 174 unsigned long long mask; 175 double scale; 176 double bias; 177 178 mantissa = lp_mantissa(dst_type); 179 180 n = MIN2(mantissa, src_width); 181 182 ubound = ((unsigned long long)1 << n); 183 mask = ubound - 1; 184 scale = (double)ubound/mask; 185 bias = (double)((unsigned long long)1 << (mantissa - n)); 186 187 res = src; 188 189 if(src_width > mantissa) { 190 int shift = src_width - mantissa; 191 res = LLVMBuildLShr(builder, res, lp_build_const_int_vec(dst_type, shift), ""); 192 } 193 194 bias_ = lp_build_const_vec(dst_type, bias); 195 196 res = LLVMBuildOr(builder, 197 res, 198 LLVMBuildBitCast(builder, bias_, int_vec_type, ""), ""); 199 200 res = LLVMBuildBitCast(builder, res, vec_type, ""); 201 202 res = LLVMBuildSub(builder, res, bias_, ""); 203 res = LLVMBuildMul(builder, res, lp_build_const_vec(dst_type, scale), ""); 204 205 return res; 206} 207 208 209/** 210 * Generic type conversion. 211 * 212 * TODO: Take a precision argument, or even better, add a new precision member 213 * to the lp_type union. 214 */ 215void 216lp_build_conv(LLVMBuilderRef builder, 217 struct lp_type src_type, 218 struct lp_type dst_type, 219 const LLVMValueRef *src, unsigned num_srcs, 220 LLVMValueRef *dst, unsigned num_dsts) 221{ 222 struct lp_type tmp_type; 223 LLVMValueRef tmp[LP_MAX_VECTOR_LENGTH]; 224 unsigned num_tmps; 225 unsigned i; 226 227 /* We must not loose or gain channels. Only precision */ 228 assert(src_type.length * num_srcs == dst_type.length * num_dsts); 229 230 assert(src_type.length <= LP_MAX_VECTOR_LENGTH); 231 assert(dst_type.length <= LP_MAX_VECTOR_LENGTH); 232 assert(num_srcs <= LP_MAX_VECTOR_LENGTH); 233 assert(num_dsts <= LP_MAX_VECTOR_LENGTH); 234 235 tmp_type = src_type; 236 for(i = 0; i < num_srcs; ++i) 237 tmp[i] = src[i]; 238 num_tmps = num_srcs; 239 240 /* 241 * Clamp if necessary 242 */ 243 244 if(memcmp(&src_type, &dst_type, sizeof src_type) != 0) { 245 struct lp_build_context bld; 246 double src_min = lp_const_min(src_type); 247 double dst_min = lp_const_min(dst_type); 248 double src_max = lp_const_max(src_type); 249 double dst_max = lp_const_max(dst_type); 250 LLVMValueRef thres; 251 252 lp_build_context_init(&bld, builder, tmp_type); 253 254 if(src_min < dst_min) { 255 if(dst_min == 0.0) 256 thres = bld.zero; 257 else 258 thres = lp_build_const_vec(src_type, dst_min); 259 for(i = 0; i < num_tmps; ++i) 260 tmp[i] = lp_build_max(&bld, tmp[i], thres); 261 } 262 263 if(src_max > dst_max) { 264 if(dst_max == 1.0) 265 thres = bld.one; 266 else 267 thres = lp_build_const_vec(src_type, dst_max); 268 for(i = 0; i < num_tmps; ++i) 269 tmp[i] = lp_build_min(&bld, tmp[i], thres); 270 } 271 } 272 273 /* 274 * Scale to the narrowest range 275 */ 276 277 if(dst_type.floating) { 278 /* Nothing to do */ 279 } 280 else if(tmp_type.floating) { 281 if(!dst_type.fixed && !dst_type.sign && dst_type.norm) { 282 for(i = 0; i < num_tmps; ++i) { 283 tmp[i] = lp_build_clamped_float_to_unsigned_norm(builder, 284 tmp_type, 285 dst_type.width, 286 tmp[i]); 287 } 288 tmp_type.floating = FALSE; 289 } 290 else { 291 double dst_scale = lp_const_scale(dst_type); 292 LLVMTypeRef tmp_vec_type; 293 294 if (dst_scale != 1.0) { 295 LLVMValueRef scale = lp_build_const_vec(tmp_type, dst_scale); 296 for(i = 0; i < num_tmps; ++i) 297 tmp[i] = LLVMBuildMul(builder, tmp[i], scale, ""); 298 } 299 300 /* Use an equally sized integer for intermediate computations */ 301 tmp_type.floating = FALSE; 302 tmp_vec_type = lp_build_vec_type(tmp_type); 303 for(i = 0; i < num_tmps; ++i) { 304#if 0 305 if(dst_type.sign) 306 tmp[i] = LLVMBuildFPToSI(builder, tmp[i], tmp_vec_type, ""); 307 else 308 tmp[i] = LLVMBuildFPToUI(builder, tmp[i], tmp_vec_type, ""); 309#else 310 /* FIXME: there is no SSE counterpart for LLVMBuildFPToUI */ 311 tmp[i] = LLVMBuildFPToSI(builder, tmp[i], tmp_vec_type, ""); 312#endif 313 } 314 } 315 } 316 else { 317 unsigned src_shift = lp_const_shift(src_type); 318 unsigned dst_shift = lp_const_shift(dst_type); 319 320 /* FIXME: compensate different offsets too */ 321 if(src_shift > dst_shift) { 322 LLVMValueRef shift = lp_build_const_int_vec(tmp_type, src_shift - dst_shift); 323 for(i = 0; i < num_tmps; ++i) 324 if(src_type.sign) 325 tmp[i] = LLVMBuildAShr(builder, tmp[i], shift, ""); 326 else 327 tmp[i] = LLVMBuildLShr(builder, tmp[i], shift, ""); 328 } 329 } 330 331 /* 332 * Truncate or expand bit width 333 * 334 * No data conversion should happen here, although the sign bits are 335 * crucial to avoid bad clamping. 336 */ 337 338 { 339 struct lp_type new_type; 340 341 new_type = tmp_type; 342 new_type.sign = dst_type.sign; 343 new_type.width = dst_type.width; 344 new_type.length = dst_type.length; 345 346 lp_build_resize(builder, tmp_type, new_type, tmp, num_srcs, tmp, num_dsts); 347 348 tmp_type = new_type; 349 num_tmps = num_dsts; 350 } 351 352 /* 353 * Scale to the widest range 354 */ 355 356 if(src_type.floating) { 357 /* Nothing to do */ 358 } 359 else if(!src_type.floating && dst_type.floating) { 360 if(!src_type.fixed && !src_type.sign && src_type.norm) { 361 for(i = 0; i < num_tmps; ++i) { 362 tmp[i] = lp_build_unsigned_norm_to_float(builder, 363 src_type.width, 364 dst_type, 365 tmp[i]); 366 } 367 tmp_type.floating = TRUE; 368 } 369 else { 370 double src_scale = lp_const_scale(src_type); 371 LLVMTypeRef tmp_vec_type; 372 373 /* Use an equally sized integer for intermediate computations */ 374 tmp_type.floating = TRUE; 375 tmp_type.sign = TRUE; 376 tmp_vec_type = lp_build_vec_type(tmp_type); 377 for(i = 0; i < num_tmps; ++i) { 378#if 0 379 if(dst_type.sign) 380 tmp[i] = LLVMBuildSIToFP(builder, tmp[i], tmp_vec_type, ""); 381 else 382 tmp[i] = LLVMBuildUIToFP(builder, tmp[i], tmp_vec_type, ""); 383#else 384 /* FIXME: there is no SSE counterpart for LLVMBuildUIToFP */ 385 tmp[i] = LLVMBuildSIToFP(builder, tmp[i], tmp_vec_type, ""); 386#endif 387 } 388 389 if (src_scale != 1.0) { 390 LLVMValueRef scale = lp_build_const_vec(tmp_type, 1.0/src_scale); 391 for(i = 0; i < num_tmps; ++i) 392 tmp[i] = LLVMBuildMul(builder, tmp[i], scale, ""); 393 } 394 } 395 } 396 else { 397 unsigned src_shift = lp_const_shift(src_type); 398 unsigned dst_shift = lp_const_shift(dst_type); 399 400 /* FIXME: compensate different offsets too */ 401 if(src_shift < dst_shift) { 402 LLVMValueRef shift = lp_build_const_int_vec(tmp_type, dst_shift - src_shift); 403 for(i = 0; i < num_tmps; ++i) 404 tmp[i] = LLVMBuildShl(builder, tmp[i], shift, ""); 405 } 406 } 407 408 for(i = 0; i < num_dsts; ++i) 409 dst[i] = tmp[i]; 410} 411 412 413/** 414 * Bit mask conversion. 415 * 416 * This will convert the integer masks that match the given types. 417 * 418 * The mask values should 0 or -1, i.e., all bits either set to zero or one. 419 * Any other value will likely cause in unpredictable results. 420 * 421 * This is basically a very trimmed down version of lp_build_conv. 422 */ 423void 424lp_build_conv_mask(LLVMBuilderRef builder, 425 struct lp_type src_type, 426 struct lp_type dst_type, 427 const LLVMValueRef *src, unsigned num_srcs, 428 LLVMValueRef *dst, unsigned num_dsts) 429{ 430 /* Register width must remain constant */ 431 assert(src_type.width * src_type.length == dst_type.width * dst_type.length); 432 433 /* We must not loose or gain channels. Only precision */ 434 assert(src_type.length * num_srcs == dst_type.length * num_dsts); 435 436 /* 437 * Drop 438 * 439 * We assume all values are 0 or -1 440 */ 441 442 src_type.floating = FALSE; 443 src_type.fixed = FALSE; 444 src_type.sign = TRUE; 445 src_type.norm = FALSE; 446 447 dst_type.floating = FALSE; 448 dst_type.fixed = FALSE; 449 dst_type.sign = TRUE; 450 dst_type.norm = FALSE; 451 452 /* 453 * Truncate or expand bit width 454 */ 455 456 if(src_type.width > dst_type.width) { 457 assert(num_dsts == 1); 458 dst[0] = lp_build_pack(builder, src_type, dst_type, TRUE, src, num_srcs); 459 } 460 else if(src_type.width < dst_type.width) { 461 assert(num_srcs == 1); 462 lp_build_unpack(builder, src_type, dst_type, src[0], dst, num_dsts); 463 } 464 else { 465 assert(num_srcs == num_dsts); 466 memcpy(dst, src, num_dsts * sizeof *dst); 467 } 468} 469