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 * Unit tests for type conversion. 32 * 33 * @author Jose Fonseca <jfonseca@vmware.com> 34 */ 35 36 37#include "util/u_pointer.h" 38#include "gallivm/lp_bld_init.h" 39#include "gallivm/lp_bld_type.h" 40#include "gallivm/lp_bld_const.h" 41#include "gallivm/lp_bld_conv.h" 42#include "gallivm/lp_bld_debug.h" 43#include "lp_test.h" 44 45 46typedef void (*conv_test_ptr_t)(const void *src, const void *dst); 47 48 49void 50write_tsv_header(FILE *fp) 51{ 52 fprintf(fp, 53 "result\t" 54 "cycles_per_channel\t" 55 "src_type\t" 56 "dst_type\n"); 57 58 fflush(fp); 59} 60 61 62static void 63write_tsv_row(FILE *fp, 64 struct lp_type src_type, 65 struct lp_type dst_type, 66 double cycles, 67 boolean success) 68{ 69 fprintf(fp, "%s\t", success ? "pass" : "fail"); 70 71 fprintf(fp, "%.1f\t", cycles / MAX2(src_type.length, dst_type.length)); 72 73 dump_type(fp, src_type); 74 fprintf(fp, "\t"); 75 76 dump_type(fp, dst_type); 77 fprintf(fp, "\n"); 78 79 fflush(fp); 80} 81 82 83static void 84dump_conv_types(FILE *fp, 85 struct lp_type src_type, 86 struct lp_type dst_type) 87{ 88 fprintf(fp, "src_type="); 89 dump_type(fp, src_type); 90 91 fprintf(fp, " dst_type="); 92 dump_type(fp, dst_type); 93 94 fprintf(fp, " ...\n"); 95 fflush(fp); 96} 97 98 99static LLVMValueRef 100add_conv_test(struct gallivm_state *gallivm, 101 struct lp_type src_type, unsigned num_srcs, 102 struct lp_type dst_type, unsigned num_dsts) 103{ 104 LLVMModuleRef module = gallivm->module; 105 LLVMContextRef context = gallivm->context; 106 LLVMBuilderRef builder = gallivm->builder; 107 LLVMTypeRef args[2]; 108 LLVMValueRef func; 109 LLVMValueRef src_ptr; 110 LLVMValueRef dst_ptr; 111 LLVMBasicBlockRef block; 112 LLVMValueRef src[LP_MAX_VECTOR_LENGTH]; 113 LLVMValueRef dst[LP_MAX_VECTOR_LENGTH]; 114 unsigned i; 115 116 args[0] = LLVMPointerType(lp_build_vec_type(gallivm, src_type), 0); 117 args[1] = LLVMPointerType(lp_build_vec_type(gallivm, dst_type), 0); 118 119 func = LLVMAddFunction(module, "test", 120 LLVMFunctionType(LLVMVoidTypeInContext(context), 121 args, 2, 0)); 122 LLVMSetFunctionCallConv(func, LLVMCCallConv); 123 src_ptr = LLVMGetParam(func, 0); 124 dst_ptr = LLVMGetParam(func, 1); 125 126 block = LLVMAppendBasicBlockInContext(context, func, "entry"); 127 LLVMPositionBuilderAtEnd(builder, block); 128 129 for(i = 0; i < num_srcs; ++i) { 130 LLVMValueRef index = LLVMConstInt(LLVMInt32TypeInContext(context), i, 0); 131 LLVMValueRef ptr = LLVMBuildGEP(builder, src_ptr, &index, 1, ""); 132 src[i] = LLVMBuildLoad(builder, ptr, ""); 133 } 134 135 lp_build_conv(gallivm, src_type, dst_type, src, num_srcs, dst, num_dsts); 136 137 for(i = 0; i < num_dsts; ++i) { 138 LLVMValueRef index = LLVMConstInt(LLVMInt32TypeInContext(context), i, 0); 139 LLVMValueRef ptr = LLVMBuildGEP(builder, dst_ptr, &index, 1, ""); 140 LLVMBuildStore(builder, dst[i], ptr); 141 } 142 143 LLVMBuildRetVoid(builder); 144 145 gallivm_verify_function(gallivm, func); 146 147 return func; 148} 149 150 151PIPE_ALIGN_STACK 152static boolean 153test_one(unsigned verbose, 154 FILE *fp, 155 struct lp_type src_type, 156 struct lp_type dst_type) 157{ 158 LLVMContextRef context; 159 struct gallivm_state *gallivm; 160 LLVMValueRef func = NULL; 161 conv_test_ptr_t conv_test_ptr; 162 boolean success; 163 const unsigned n = LP_TEST_NUM_SAMPLES; 164 int64_t cycles[LP_TEST_NUM_SAMPLES]; 165 double cycles_avg = 0.0; 166 unsigned num_srcs; 167 unsigned num_dsts; 168 double eps; 169 unsigned i, j; 170 171 if ((src_type.width >= dst_type.width && src_type.length > dst_type.length) || 172 (src_type.width <= dst_type.width && src_type.length < dst_type.length)) { 173 return TRUE; 174 } 175 176 /* Known failures 177 * - fixed point 32 -> float 32 178 * - float 32 -> signed normalized integer 32 179 */ 180 if ((src_type.floating && !dst_type.floating && dst_type.sign && dst_type.norm && src_type.width == dst_type.width) || 181 (!src_type.floating && dst_type.floating && src_type.fixed && src_type.width == dst_type.width)) { 182 return TRUE; 183 } 184 185 /* Known failures 186 * - fixed point 32 -> float 32 187 * - float 32 -> signed normalized integer 32 188 */ 189 if ((src_type.floating && !dst_type.floating && dst_type.sign && dst_type.norm && src_type.width == dst_type.width) || 190 (!src_type.floating && dst_type.floating && src_type.fixed && src_type.width == dst_type.width)) { 191 return TRUE; 192 } 193 194 if(verbose >= 1) 195 dump_conv_types(stderr, src_type, dst_type); 196 197 if (src_type.length > dst_type.length) { 198 num_srcs = 1; 199 num_dsts = src_type.length/dst_type.length; 200 } 201 else if (src_type.length < dst_type.length) { 202 num_dsts = 1; 203 num_srcs = dst_type.length/src_type.length; 204 } 205 else { 206 num_dsts = 1; 207 num_srcs = 1; 208 } 209 210 /* We must not loose or gain channels. Only precision */ 211 assert(src_type.length * num_srcs == dst_type.length * num_dsts); 212 213 eps = MAX2(lp_const_eps(src_type), lp_const_eps(dst_type)); 214 if (dst_type.norm && dst_type.sign && src_type.sign && !src_type.floating) { 215 /* 216 * This is quite inaccurate due to shift being used. 217 * I don't think it's possible to hit such conversions with 218 * llvmpipe though. 219 */ 220 eps *= 2; 221 } 222 223 context = LLVMContextCreate(); 224#if LLVM_VERSION_MAJOR >= 15 225 LLVMContextSetOpaquePointers(context, false); 226#endif 227 gallivm = gallivm_create("test_module", context, NULL); 228 229 func = add_conv_test(gallivm, src_type, num_srcs, dst_type, num_dsts); 230 231 gallivm_compile_module(gallivm); 232 233 conv_test_ptr = (conv_test_ptr_t)gallivm_jit_function(gallivm, func); 234 235 gallivm_free_ir(gallivm); 236 237 success = TRUE; 238 for(i = 0; i < n && success; ++i) { 239 unsigned src_stride = src_type.length*src_type.width/8; 240 unsigned dst_stride = dst_type.length*dst_type.width/8; 241 alignas(LP_MIN_VECTOR_ALIGN) uint8_t src[LP_MAX_VECTOR_LENGTH*LP_MAX_VECTOR_LENGTH]; 242 alignas(LP_MIN_VECTOR_ALIGN) uint8_t dst[LP_MAX_VECTOR_LENGTH*LP_MAX_VECTOR_LENGTH]; 243 double fref[LP_MAX_VECTOR_LENGTH*LP_MAX_VECTOR_LENGTH]; 244 uint8_t ref[LP_MAX_VECTOR_LENGTH*LP_MAX_VECTOR_LENGTH]; 245 int64_t start_counter = 0; 246 int64_t end_counter = 0; 247 248 for(j = 0; j < num_srcs; ++j) { 249 random_vec(src_type, src + j*src_stride); 250 read_vec(src_type, src + j*src_stride, fref + j*src_type.length); 251 } 252 253 for(j = 0; j < num_dsts; ++j) { 254 write_vec(dst_type, ref + j*dst_stride, fref + j*dst_type.length); 255 } 256 257 start_counter = rdtsc(); 258 conv_test_ptr(src, dst); 259 end_counter = rdtsc(); 260 261 cycles[i] = end_counter - start_counter; 262 263 for(j = 0; j < num_dsts; ++j) { 264 if(!compare_vec_with_eps(dst_type, dst + j*dst_stride, ref + j*dst_stride, eps)) 265 success = FALSE; 266 } 267 268 if (!success || verbose >= 3) { 269 if(verbose < 1) 270 dump_conv_types(stderr, src_type, dst_type); 271 if (success) { 272 fprintf(stderr, "PASS\n"); 273 } 274 else { 275 fprintf(stderr, "MISMATCH\n"); 276 } 277 278 for(j = 0; j < num_srcs; ++j) { 279 fprintf(stderr, " Src%u: ", j); 280 dump_vec(stderr, src_type, src + j*src_stride); 281 fprintf(stderr, "\n"); 282 } 283 284#if 1 285 fprintf(stderr, " Ref: "); 286 for(j = 0; j < src_type.length*num_srcs; ++j) 287 fprintf(stderr, " %f", fref[j]); 288 fprintf(stderr, "\n"); 289#endif 290 291 for(j = 0; j < num_dsts; ++j) { 292 fprintf(stderr, " Dst%u: ", j); 293 dump_vec(stderr, dst_type, dst + j*dst_stride); 294 fprintf(stderr, "\n"); 295 296 fprintf(stderr, " Ref%u: ", j); 297 dump_vec(stderr, dst_type, ref + j*dst_stride); 298 fprintf(stderr, "\n"); 299 } 300 } 301 } 302 303 /* 304 * Unfortunately the output of cycle counter is not very reliable as it comes 305 * -- sometimes we get outliers (due IRQs perhaps?) which are 306 * better removed to avoid random or biased data. 307 */ 308 { 309 double sum = 0.0, sum2 = 0.0; 310 double avg, std; 311 unsigned m; 312 313 for(i = 0; i < n; ++i) { 314 sum += cycles[i]; 315 sum2 += cycles[i]*cycles[i]; 316 } 317 318 avg = sum/n; 319 std = sqrtf((sum2 - n*avg*avg)/n); 320 321 m = 0; 322 sum = 0.0; 323 for(i = 0; i < n; ++i) { 324 if(fabs(cycles[i] - avg) <= 4.0*std) { 325 sum += cycles[i]; 326 ++m; 327 } 328 } 329 330 cycles_avg = sum/m; 331 332 } 333 334 if(fp) 335 write_tsv_row(fp, src_type, dst_type, cycles_avg, success); 336 337 gallivm_destroy(gallivm); 338 LLVMContextDispose(context); 339 340 return success; 341} 342 343 344const struct lp_type conv_types[] = { 345 /* float, fixed, sign, norm, width, len */ 346 347 /* Float */ 348 { TRUE, FALSE, TRUE, TRUE, 32, 4 }, 349 { TRUE, FALSE, TRUE, FALSE, 32, 4 }, 350 { TRUE, FALSE, FALSE, TRUE, 32, 4 }, 351 { TRUE, FALSE, FALSE, FALSE, 32, 4 }, 352 353 { TRUE, FALSE, TRUE, TRUE, 32, 8 }, 354 { TRUE, FALSE, TRUE, FALSE, 32, 8 }, 355 { TRUE, FALSE, FALSE, TRUE, 32, 8 }, 356 { TRUE, FALSE, FALSE, FALSE, 32, 8 }, 357 358 /* Fixed */ 359 { FALSE, TRUE, TRUE, TRUE, 32, 4 }, 360 { FALSE, TRUE, TRUE, FALSE, 32, 4 }, 361 { FALSE, TRUE, FALSE, TRUE, 32, 4 }, 362 { FALSE, TRUE, FALSE, FALSE, 32, 4 }, 363 364 { FALSE, TRUE, TRUE, TRUE, 32, 8 }, 365 { FALSE, TRUE, TRUE, FALSE, 32, 8 }, 366 { FALSE, TRUE, FALSE, TRUE, 32, 8 }, 367 { FALSE, TRUE, FALSE, FALSE, 32, 8 }, 368 369 /* Integer */ 370 { FALSE, FALSE, TRUE, TRUE, 32, 4 }, 371 { FALSE, FALSE, TRUE, FALSE, 32, 4 }, 372 { FALSE, FALSE, FALSE, TRUE, 32, 4 }, 373 { FALSE, FALSE, FALSE, FALSE, 32, 4 }, 374 375 { FALSE, FALSE, TRUE, TRUE, 32, 8 }, 376 { FALSE, FALSE, TRUE, FALSE, 32, 8 }, 377 { FALSE, FALSE, FALSE, TRUE, 32, 8 }, 378 { FALSE, FALSE, FALSE, FALSE, 32, 8 }, 379 380 { FALSE, FALSE, TRUE, TRUE, 16, 8 }, 381 { FALSE, FALSE, TRUE, FALSE, 16, 8 }, 382 { FALSE, FALSE, FALSE, TRUE, 16, 8 }, 383 { FALSE, FALSE, FALSE, FALSE, 16, 8 }, 384 385 { FALSE, FALSE, TRUE, TRUE, 8, 16 }, 386 { FALSE, FALSE, TRUE, FALSE, 8, 16 }, 387 { FALSE, FALSE, FALSE, TRUE, 8, 16 }, 388 { FALSE, FALSE, FALSE, FALSE, 8, 16 }, 389 390 { FALSE, FALSE, TRUE, TRUE, 8, 4 }, 391 { FALSE, FALSE, TRUE, FALSE, 8, 4 }, 392 { FALSE, FALSE, FALSE, TRUE, 8, 4 }, 393 { FALSE, FALSE, FALSE, FALSE, 8, 4 }, 394 395 { FALSE, FALSE, FALSE, TRUE, 8, 8 }, 396}; 397 398 399const unsigned num_types = ARRAY_SIZE(conv_types); 400 401 402boolean 403test_all(unsigned verbose, FILE *fp) 404{ 405 const struct lp_type *src_type; 406 const struct lp_type *dst_type; 407 boolean success = TRUE; 408 int error_count = 0; 409 410 for(src_type = conv_types; src_type < &conv_types[num_types]; ++src_type) { 411 for(dst_type = conv_types; dst_type < &conv_types[num_types]; ++dst_type) { 412 413 if(src_type == dst_type) 414 continue; 415 416 if(!test_one(verbose, fp, *src_type, *dst_type)){ 417 success = FALSE; 418 ++error_count; 419 } 420 } 421 } 422 423 fprintf(stderr, "%d failures\n", error_count); 424 425 return success; 426} 427 428 429boolean 430test_some(unsigned verbose, FILE *fp, 431 unsigned long n) 432{ 433 const struct lp_type *src_type; 434 const struct lp_type *dst_type; 435 unsigned long i; 436 boolean success = TRUE; 437 438 for(i = 0; i < n; ++i) { 439 src_type = &conv_types[rand() % num_types]; 440 441 do { 442 dst_type = &conv_types[rand() % num_types]; 443 } while (src_type == dst_type || src_type->norm != dst_type->norm); 444 445 if(!test_one(verbose, fp, *src_type, *dst_type)) 446 success = FALSE; 447 } 448 449 return success; 450} 451 452 453boolean 454test_single(unsigned verbose, FILE *fp) 455{ 456 /* float, fixed, sign, norm, width, len */ 457 struct lp_type f32x4_type = 458 { TRUE, FALSE, TRUE, TRUE, 32, 4 }; 459 struct lp_type ub8x4_type = 460 { FALSE, FALSE, FALSE, TRUE, 8, 16 }; 461 462 boolean success; 463 464 success = test_one(verbose, fp, f32x4_type, ub8x4_type); 465 466 return success; 467} 468