1bf215546Sopenharmony_ci/************************************************************************** 2bf215546Sopenharmony_ci * 3bf215546Sopenharmony_ci * Copyright 2011 VMware, Inc. 4bf215546Sopenharmony_ci * All Rights Reserved. 5bf215546Sopenharmony_ci * 6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the 8bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including 9bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish, 10bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to 11bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to 12bf215546Sopenharmony_ci * the following conditions: 13bf215546Sopenharmony_ci * 14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the 15bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions 16bf215546Sopenharmony_ci * of the Software. 17bf215546Sopenharmony_ci * 18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21bf215546Sopenharmony_ci * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 22bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25bf215546Sopenharmony_ci * 26bf215546Sopenharmony_ci **************************************************************************/ 27bf215546Sopenharmony_ci 28bf215546Sopenharmony_ci 29bf215546Sopenharmony_ci#include <limits.h> 30bf215546Sopenharmony_ci#include <stdio.h> 31bf215546Sopenharmony_ci#include <stdlib.h> 32bf215546Sopenharmony_ci 33bf215546Sopenharmony_ci#include "util/u_pointer.h" 34bf215546Sopenharmony_ci#include "util/u_memory.h" 35bf215546Sopenharmony_ci#include "util/u_math.h" 36bf215546Sopenharmony_ci#include "util/u_cpu_detect.h" 37bf215546Sopenharmony_ci 38bf215546Sopenharmony_ci#include "gallivm/lp_bld.h" 39bf215546Sopenharmony_ci#include "gallivm/lp_bld_debug.h" 40bf215546Sopenharmony_ci#include "gallivm/lp_bld_init.h" 41bf215546Sopenharmony_ci#include "gallivm/lp_bld_arit.h" 42bf215546Sopenharmony_ci 43bf215546Sopenharmony_ci#include "lp_test.h" 44bf215546Sopenharmony_ci 45bf215546Sopenharmony_ci 46bf215546Sopenharmony_civoid 47bf215546Sopenharmony_ciwrite_tsv_header(FILE *fp) 48bf215546Sopenharmony_ci{ 49bf215546Sopenharmony_ci fprintf(fp, 50bf215546Sopenharmony_ci "result\t" 51bf215546Sopenharmony_ci "format\n"); 52bf215546Sopenharmony_ci 53bf215546Sopenharmony_ci fflush(fp); 54bf215546Sopenharmony_ci} 55bf215546Sopenharmony_ci 56bf215546Sopenharmony_ci 57bf215546Sopenharmony_citypedef void (*unary_func_t)(float *out, const float *in); 58bf215546Sopenharmony_ci 59bf215546Sopenharmony_ci 60bf215546Sopenharmony_ci/** 61bf215546Sopenharmony_ci * Describe a test case of one unary function. 62bf215546Sopenharmony_ci */ 63bf215546Sopenharmony_cistruct unary_test_t 64bf215546Sopenharmony_ci{ 65bf215546Sopenharmony_ci /* 66bf215546Sopenharmony_ci * Test name -- name of the mathematical function under test. 67bf215546Sopenharmony_ci */ 68bf215546Sopenharmony_ci 69bf215546Sopenharmony_ci const char *name; 70bf215546Sopenharmony_ci 71bf215546Sopenharmony_ci LLVMValueRef 72bf215546Sopenharmony_ci (*builder)(struct lp_build_context *bld, LLVMValueRef a); 73bf215546Sopenharmony_ci 74bf215546Sopenharmony_ci /* 75bf215546Sopenharmony_ci * Reference (pure-C) function. 76bf215546Sopenharmony_ci */ 77bf215546Sopenharmony_ci float 78bf215546Sopenharmony_ci (*ref)(float a); 79bf215546Sopenharmony_ci 80bf215546Sopenharmony_ci /* 81bf215546Sopenharmony_ci * Test values. 82bf215546Sopenharmony_ci */ 83bf215546Sopenharmony_ci const float *values; 84bf215546Sopenharmony_ci unsigned num_values; 85bf215546Sopenharmony_ci 86bf215546Sopenharmony_ci /* 87bf215546Sopenharmony_ci * Required precision in bits. 88bf215546Sopenharmony_ci */ 89bf215546Sopenharmony_ci double precision; 90bf215546Sopenharmony_ci}; 91bf215546Sopenharmony_ci 92bf215546Sopenharmony_ci 93bf215546Sopenharmony_cistatic float negf(float x) 94bf215546Sopenharmony_ci{ 95bf215546Sopenharmony_ci return -x; 96bf215546Sopenharmony_ci} 97bf215546Sopenharmony_ci 98bf215546Sopenharmony_ci 99bf215546Sopenharmony_cistatic float sgnf(float x) 100bf215546Sopenharmony_ci{ 101bf215546Sopenharmony_ci if (x > 0.0f) { 102bf215546Sopenharmony_ci return 1.0f; 103bf215546Sopenharmony_ci } 104bf215546Sopenharmony_ci if (x < 0.0f) { 105bf215546Sopenharmony_ci return -1.0f; 106bf215546Sopenharmony_ci } 107bf215546Sopenharmony_ci return 0.0f; 108bf215546Sopenharmony_ci} 109bf215546Sopenharmony_ci 110bf215546Sopenharmony_ci 111bf215546Sopenharmony_ciconst float sgn_values[] = { 112bf215546Sopenharmony_ci -INFINITY, 113bf215546Sopenharmony_ci -60, 114bf215546Sopenharmony_ci -4, 115bf215546Sopenharmony_ci -2, 116bf215546Sopenharmony_ci -1, 117bf215546Sopenharmony_ci -1e-007, 118bf215546Sopenharmony_ci 0, 119bf215546Sopenharmony_ci 1e-007, 120bf215546Sopenharmony_ci 0.01, 121bf215546Sopenharmony_ci 0.1, 122bf215546Sopenharmony_ci 0.9, 123bf215546Sopenharmony_ci 0.99, 124bf215546Sopenharmony_ci 1, 125bf215546Sopenharmony_ci 2, 126bf215546Sopenharmony_ci 4, 127bf215546Sopenharmony_ci 60, 128bf215546Sopenharmony_ci INFINITY, 129bf215546Sopenharmony_ci NAN 130bf215546Sopenharmony_ci}; 131bf215546Sopenharmony_ci 132bf215546Sopenharmony_ci 133bf215546Sopenharmony_ciconst float exp2_values[] = { 134bf215546Sopenharmony_ci -INFINITY, 135bf215546Sopenharmony_ci -60, 136bf215546Sopenharmony_ci -4, 137bf215546Sopenharmony_ci -2, 138bf215546Sopenharmony_ci -1, 139bf215546Sopenharmony_ci -1e-007, 140bf215546Sopenharmony_ci 0, 141bf215546Sopenharmony_ci 1e-007, 142bf215546Sopenharmony_ci 0.01, 143bf215546Sopenharmony_ci 0.1, 144bf215546Sopenharmony_ci 0.9, 145bf215546Sopenharmony_ci 0.99, 146bf215546Sopenharmony_ci 1, 147bf215546Sopenharmony_ci 2, 148bf215546Sopenharmony_ci 4, 149bf215546Sopenharmony_ci 60, 150bf215546Sopenharmony_ci INFINITY, 151bf215546Sopenharmony_ci NAN 152bf215546Sopenharmony_ci}; 153bf215546Sopenharmony_ci 154bf215546Sopenharmony_ci 155bf215546Sopenharmony_ciconst float log2_values[] = { 156bf215546Sopenharmony_ci#if 0 157bf215546Sopenharmony_ci /* 158bf215546Sopenharmony_ci * Smallest denormalized number; meant just for experimentation, but not 159bf215546Sopenharmony_ci * validation. 160bf215546Sopenharmony_ci */ 161bf215546Sopenharmony_ci 1.4012984643248171e-45, 162bf215546Sopenharmony_ci#endif 163bf215546Sopenharmony_ci -INFINITY, 164bf215546Sopenharmony_ci 0, 165bf215546Sopenharmony_ci 1e-007, 166bf215546Sopenharmony_ci 0.1, 167bf215546Sopenharmony_ci 0.5, 168bf215546Sopenharmony_ci 0.99, 169bf215546Sopenharmony_ci 1, 170bf215546Sopenharmony_ci 1.01, 171bf215546Sopenharmony_ci 1.1, 172bf215546Sopenharmony_ci 1.9, 173bf215546Sopenharmony_ci 1.99, 174bf215546Sopenharmony_ci 2, 175bf215546Sopenharmony_ci 4, 176bf215546Sopenharmony_ci 100000, 177bf215546Sopenharmony_ci 1e+018, 178bf215546Sopenharmony_ci INFINITY, 179bf215546Sopenharmony_ci NAN 180bf215546Sopenharmony_ci}; 181bf215546Sopenharmony_ci 182bf215546Sopenharmony_ci 183bf215546Sopenharmony_cistatic float rcpf(float x) 184bf215546Sopenharmony_ci{ 185bf215546Sopenharmony_ci return 1.0/x; 186bf215546Sopenharmony_ci} 187bf215546Sopenharmony_ci 188bf215546Sopenharmony_ci 189bf215546Sopenharmony_ciconst float rcp_values[] = { 190bf215546Sopenharmony_ci -0.0, 0.0, 191bf215546Sopenharmony_ci -1.0, 1.0, 192bf215546Sopenharmony_ci -1e-007, 1e-007, 193bf215546Sopenharmony_ci -4.0, 4.0, 194bf215546Sopenharmony_ci -1e+035, -100000, 195bf215546Sopenharmony_ci 100000, 1e+035, 196bf215546Sopenharmony_ci 5.88e-39f, // denormal 197bf215546Sopenharmony_ci INFINITY, -INFINITY, 198bf215546Sopenharmony_ci}; 199bf215546Sopenharmony_ci 200bf215546Sopenharmony_ci 201bf215546Sopenharmony_cistatic float rsqrtf(float x) 202bf215546Sopenharmony_ci{ 203bf215546Sopenharmony_ci return 1.0/(float)sqrt(x); 204bf215546Sopenharmony_ci} 205bf215546Sopenharmony_ci 206bf215546Sopenharmony_ci 207bf215546Sopenharmony_ciconst float rsqrt_values[] = { 208bf215546Sopenharmony_ci // http://msdn.microsoft.com/en-us/library/windows/desktop/bb147346.aspx 209bf215546Sopenharmony_ci 0.0, // must yield infinity 210bf215546Sopenharmony_ci 1.0, // must yield 1.0 211bf215546Sopenharmony_ci 1e-007, 4.0, 212bf215546Sopenharmony_ci 100000, 1e+035, 213bf215546Sopenharmony_ci 5.88e-39f, // denormal 214bf215546Sopenharmony_ci INFINITY, 215bf215546Sopenharmony_ci}; 216bf215546Sopenharmony_ci 217bf215546Sopenharmony_ci 218bf215546Sopenharmony_ciconst float sincos_values[] = { 219bf215546Sopenharmony_ci -INFINITY, 220bf215546Sopenharmony_ci -5*M_PI/4, 221bf215546Sopenharmony_ci -4*M_PI/4, 222bf215546Sopenharmony_ci -4*M_PI/4, 223bf215546Sopenharmony_ci -3*M_PI/4, 224bf215546Sopenharmony_ci -2*M_PI/4, 225bf215546Sopenharmony_ci -1*M_PI/4, 226bf215546Sopenharmony_ci 1*M_PI/4, 227bf215546Sopenharmony_ci 2*M_PI/4, 228bf215546Sopenharmony_ci 3*M_PI/4, 229bf215546Sopenharmony_ci 4*M_PI/4, 230bf215546Sopenharmony_ci 5*M_PI/4, 231bf215546Sopenharmony_ci INFINITY, 232bf215546Sopenharmony_ci NAN 233bf215546Sopenharmony_ci}; 234bf215546Sopenharmony_ci 235bf215546Sopenharmony_ciconst float round_values[] = { 236bf215546Sopenharmony_ci -10.0, -1, 0.0, 12.0, 237bf215546Sopenharmony_ci -1.49, -0.25, 1.25, 2.51, 238bf215546Sopenharmony_ci -0.99, -0.01, 0.01, 0.99, 239bf215546Sopenharmony_ci -1.5, -0.5, 0.5, 1.5, 240bf215546Sopenharmony_ci 1.401298464324817e-45f, // smallest denormal 241bf215546Sopenharmony_ci -1.401298464324817e-45f, 242bf215546Sopenharmony_ci 1.62981451e-08f, 243bf215546Sopenharmony_ci -1.62981451e-08f, 244bf215546Sopenharmony_ci 1.62981451e15f, // large number not representable as 32bit int 245bf215546Sopenharmony_ci -1.62981451e15f, 246bf215546Sopenharmony_ci FLT_EPSILON, 247bf215546Sopenharmony_ci -FLT_EPSILON, 248bf215546Sopenharmony_ci 1.0f - 0.5f*FLT_EPSILON, 249bf215546Sopenharmony_ci -1.0f + FLT_EPSILON, 250bf215546Sopenharmony_ci FLT_MAX, 251bf215546Sopenharmony_ci -FLT_MAX 252bf215546Sopenharmony_ci}; 253bf215546Sopenharmony_ci 254bf215546Sopenharmony_cistatic float fractf(float x) 255bf215546Sopenharmony_ci{ 256bf215546Sopenharmony_ci x -= floorf(x); 257bf215546Sopenharmony_ci if (x >= 1.0f) { 258bf215546Sopenharmony_ci // clamp to the largest number smaller than one 259bf215546Sopenharmony_ci x = 1.0f - 0.5f*FLT_EPSILON; 260bf215546Sopenharmony_ci } 261bf215546Sopenharmony_ci return x; 262bf215546Sopenharmony_ci} 263bf215546Sopenharmony_ci 264bf215546Sopenharmony_ci 265bf215546Sopenharmony_ciconst float fract_values[] = { 266bf215546Sopenharmony_ci // http://en.wikipedia.org/wiki/IEEE_754-1985#Examples 267bf215546Sopenharmony_ci 0.0f, 268bf215546Sopenharmony_ci -0.0f, 269bf215546Sopenharmony_ci 1.0f, 270bf215546Sopenharmony_ci -1.0f, 271bf215546Sopenharmony_ci 0.5f, 272bf215546Sopenharmony_ci -0.5f, 273bf215546Sopenharmony_ci 1.401298464324817e-45f, // smallest denormal 274bf215546Sopenharmony_ci -1.401298464324817e-45f, 275bf215546Sopenharmony_ci 5.88e-39f, // middle denormal 276bf215546Sopenharmony_ci 1.18e-38f, // largest denormal 277bf215546Sopenharmony_ci -1.18e-38f, 278bf215546Sopenharmony_ci -1.62981451e-08f, 279bf215546Sopenharmony_ci FLT_EPSILON, 280bf215546Sopenharmony_ci -FLT_EPSILON, 281bf215546Sopenharmony_ci 1.0f - 0.5f*FLT_EPSILON, 282bf215546Sopenharmony_ci -1.0f + FLT_EPSILON, 283bf215546Sopenharmony_ci FLT_MAX, 284bf215546Sopenharmony_ci -FLT_MAX 285bf215546Sopenharmony_ci}; 286bf215546Sopenharmony_ci 287bf215546Sopenharmony_ci 288bf215546Sopenharmony_ci/* 289bf215546Sopenharmony_ci * Unary test cases. 290bf215546Sopenharmony_ci */ 291bf215546Sopenharmony_ci 292bf215546Sopenharmony_ci#ifdef _MSC_VER 293bf215546Sopenharmony_ci#define WRAP(func) \ 294bf215546Sopenharmony_cistatic float \ 295bf215546Sopenharmony_ciwrap_ ## func(float x) \ 296bf215546Sopenharmony_ci{ \ 297bf215546Sopenharmony_ci return func(x); \ 298bf215546Sopenharmony_ci} 299bf215546Sopenharmony_ciWRAP(expf) 300bf215546Sopenharmony_ciWRAP(logf) 301bf215546Sopenharmony_ciWRAP(sinf) 302bf215546Sopenharmony_ciWRAP(cosf) 303bf215546Sopenharmony_ciWRAP(floorf) 304bf215546Sopenharmony_ciWRAP(ceilf) 305bf215546Sopenharmony_ci#define expf wrap_expf 306bf215546Sopenharmony_ci#define logf wrap_logf 307bf215546Sopenharmony_ci#define sinf wrap_sinf 308bf215546Sopenharmony_ci#define cosf wrap_cosf 309bf215546Sopenharmony_ci#define floorf wrap_floorf 310bf215546Sopenharmony_ci#define ceilf wrap_ceilf 311bf215546Sopenharmony_ci#endif 312bf215546Sopenharmony_ci 313bf215546Sopenharmony_cistatic const struct unary_test_t 314bf215546Sopenharmony_ciunary_tests[] = { 315bf215546Sopenharmony_ci {"abs", &lp_build_abs, &fabsf, sgn_values, ARRAY_SIZE(sgn_values), 20.0 }, 316bf215546Sopenharmony_ci {"neg", &lp_build_negate, &negf, sgn_values, ARRAY_SIZE(sgn_values), 20.0 }, 317bf215546Sopenharmony_ci {"sgn", &lp_build_sgn, &sgnf, sgn_values, ARRAY_SIZE(sgn_values), 20.0 }, 318bf215546Sopenharmony_ci {"exp2", &lp_build_exp2, &exp2f, exp2_values, ARRAY_SIZE(exp2_values), 18.0 }, 319bf215546Sopenharmony_ci {"log2", &lp_build_log2_safe, &log2f, log2_values, ARRAY_SIZE(log2_values), 20.0 }, 320bf215546Sopenharmony_ci {"exp", &lp_build_exp, &expf, exp2_values, ARRAY_SIZE(exp2_values), 18.0 }, 321bf215546Sopenharmony_ci {"log", &lp_build_log_safe, &logf, log2_values, ARRAY_SIZE(log2_values), 20.0 }, 322bf215546Sopenharmony_ci {"rcp", &lp_build_rcp, &rcpf, rcp_values, ARRAY_SIZE(rcp_values), 20.0 }, 323bf215546Sopenharmony_ci {"rsqrt", &lp_build_rsqrt, &rsqrtf, rsqrt_values, ARRAY_SIZE(rsqrt_values), 20.0 }, 324bf215546Sopenharmony_ci {"sin", &lp_build_sin, &sinf, sincos_values, ARRAY_SIZE(sincos_values), 20.0 }, 325bf215546Sopenharmony_ci {"cos", &lp_build_cos, &cosf, sincos_values, ARRAY_SIZE(sincos_values), 20.0 }, 326bf215546Sopenharmony_ci {"sgn", &lp_build_sgn, &sgnf, sgn_values, ARRAY_SIZE(sgn_values), 20.0 }, 327bf215546Sopenharmony_ci {"round", &lp_build_round, &nearbyintf, round_values, ARRAY_SIZE(round_values), 24.0 }, 328bf215546Sopenharmony_ci {"trunc", &lp_build_trunc, &truncf, round_values, ARRAY_SIZE(round_values), 24.0 }, 329bf215546Sopenharmony_ci {"floor", &lp_build_floor, &floorf, round_values, ARRAY_SIZE(round_values), 24.0 }, 330bf215546Sopenharmony_ci {"ceil", &lp_build_ceil, &ceilf, round_values, ARRAY_SIZE(round_values), 24.0 }, 331bf215546Sopenharmony_ci {"fract", &lp_build_fract_safe, &fractf, fract_values, ARRAY_SIZE(fract_values), 24.0 }, 332bf215546Sopenharmony_ci}; 333bf215546Sopenharmony_ci 334bf215546Sopenharmony_ci 335bf215546Sopenharmony_ci/* 336bf215546Sopenharmony_ci * Build LLVM function that exercises the unary operator builder. 337bf215546Sopenharmony_ci */ 338bf215546Sopenharmony_cistatic LLVMValueRef 339bf215546Sopenharmony_cibuild_unary_test_func(struct gallivm_state *gallivm, 340bf215546Sopenharmony_ci const struct unary_test_t *test, 341bf215546Sopenharmony_ci unsigned length, 342bf215546Sopenharmony_ci const char *test_name) 343bf215546Sopenharmony_ci{ 344bf215546Sopenharmony_ci struct lp_type type = lp_type_float_vec(32, length * 32); 345bf215546Sopenharmony_ci LLVMContextRef context = gallivm->context; 346bf215546Sopenharmony_ci LLVMModuleRef module = gallivm->module; 347bf215546Sopenharmony_ci LLVMTypeRef vf32t = lp_build_vec_type(gallivm, type); 348bf215546Sopenharmony_ci LLVMTypeRef args[2] = { LLVMPointerType(vf32t, 0), LLVMPointerType(vf32t, 0) }; 349bf215546Sopenharmony_ci LLVMValueRef func = LLVMAddFunction(module, test_name, 350bf215546Sopenharmony_ci LLVMFunctionType(LLVMVoidTypeInContext(context), 351bf215546Sopenharmony_ci args, ARRAY_SIZE(args), 0)); 352bf215546Sopenharmony_ci LLVMValueRef arg0 = LLVMGetParam(func, 0); 353bf215546Sopenharmony_ci LLVMValueRef arg1 = LLVMGetParam(func, 1); 354bf215546Sopenharmony_ci LLVMBuilderRef builder = gallivm->builder; 355bf215546Sopenharmony_ci LLVMBasicBlockRef block = LLVMAppendBasicBlockInContext(context, func, "entry"); 356bf215546Sopenharmony_ci LLVMValueRef ret; 357bf215546Sopenharmony_ci 358bf215546Sopenharmony_ci struct lp_build_context bld; 359bf215546Sopenharmony_ci 360bf215546Sopenharmony_ci lp_build_context_init(&bld, gallivm, type); 361bf215546Sopenharmony_ci 362bf215546Sopenharmony_ci LLVMSetFunctionCallConv(func, LLVMCCallConv); 363bf215546Sopenharmony_ci 364bf215546Sopenharmony_ci LLVMPositionBuilderAtEnd(builder, block); 365bf215546Sopenharmony_ci 366bf215546Sopenharmony_ci arg1 = LLVMBuildLoad(builder, arg1, ""); 367bf215546Sopenharmony_ci 368bf215546Sopenharmony_ci ret = test->builder(&bld, arg1); 369bf215546Sopenharmony_ci 370bf215546Sopenharmony_ci LLVMBuildStore(builder, ret, arg0); 371bf215546Sopenharmony_ci 372bf215546Sopenharmony_ci LLVMBuildRetVoid(builder); 373bf215546Sopenharmony_ci 374bf215546Sopenharmony_ci gallivm_verify_function(gallivm, func); 375bf215546Sopenharmony_ci 376bf215546Sopenharmony_ci return func; 377bf215546Sopenharmony_ci} 378bf215546Sopenharmony_ci 379bf215546Sopenharmony_ci 380bf215546Sopenharmony_ci/* 381bf215546Sopenharmony_ci * Flush denorms to zero. 382bf215546Sopenharmony_ci */ 383bf215546Sopenharmony_cistatic float 384bf215546Sopenharmony_ciflush_denorm_to_zero(float val) 385bf215546Sopenharmony_ci{ 386bf215546Sopenharmony_ci /* 387bf215546Sopenharmony_ci * If we have a denorm manually set it to (+-)0. 388bf215546Sopenharmony_ci * This is because the reference may or may not do the right thing 389bf215546Sopenharmony_ci * otherwise because we want the result according to treating all 390bf215546Sopenharmony_ci * denormals as zero (FTZ/DAZ). Not using fpclassify because 391bf215546Sopenharmony_ci * a) some compilers are stuck at c89 (msvc) 392bf215546Sopenharmony_ci * b) not sure it reliably works with non-standard ftz/daz mode 393bf215546Sopenharmony_ci * And, right now we only disable denorms with jited code on x86/sse 394bf215546Sopenharmony_ci * (albeit this should be classified as a bug) so to get results which 395bf215546Sopenharmony_ci * match we must only flush them to zero here in that case too. 396bf215546Sopenharmony_ci */ 397bf215546Sopenharmony_ci union fi fi_val; 398bf215546Sopenharmony_ci 399bf215546Sopenharmony_ci fi_val.f = val; 400bf215546Sopenharmony_ci 401bf215546Sopenharmony_ci#if defined(PIPE_ARCH_SSE) 402bf215546Sopenharmony_ci if (util_get_cpu_caps()->has_sse) { 403bf215546Sopenharmony_ci if ((fi_val.ui & 0x7f800000) == 0) { 404bf215546Sopenharmony_ci fi_val.ui &= 0xff800000; 405bf215546Sopenharmony_ci } 406bf215546Sopenharmony_ci } 407bf215546Sopenharmony_ci#endif 408bf215546Sopenharmony_ci 409bf215546Sopenharmony_ci return fi_val.f; 410bf215546Sopenharmony_ci} 411bf215546Sopenharmony_ci 412bf215546Sopenharmony_ci/* 413bf215546Sopenharmony_ci * Test one LLVM unary arithmetic builder function. 414bf215546Sopenharmony_ci */ 415bf215546Sopenharmony_cistatic boolean 416bf215546Sopenharmony_citest_unary(unsigned verbose, FILE *fp, const struct unary_test_t *test, unsigned length) 417bf215546Sopenharmony_ci{ 418bf215546Sopenharmony_ci char test_name[128]; 419bf215546Sopenharmony_ci snprintf(test_name, sizeof test_name, "%s.v%u", test->name, length); 420bf215546Sopenharmony_ci LLVMContextRef context; 421bf215546Sopenharmony_ci struct gallivm_state *gallivm; 422bf215546Sopenharmony_ci LLVMValueRef test_func; 423bf215546Sopenharmony_ci unary_func_t test_func_jit; 424bf215546Sopenharmony_ci boolean success = TRUE; 425bf215546Sopenharmony_ci int i, j; 426bf215546Sopenharmony_ci float *in, *out; 427bf215546Sopenharmony_ci 428bf215546Sopenharmony_ci in = align_malloc(length * 4, length * 4); 429bf215546Sopenharmony_ci out = align_malloc(length * 4, length * 4); 430bf215546Sopenharmony_ci 431bf215546Sopenharmony_ci /* random NaNs or 0s could wreak havoc */ 432bf215546Sopenharmony_ci for (i = 0; i < length; i++) { 433bf215546Sopenharmony_ci in[i] = 1.0; 434bf215546Sopenharmony_ci } 435bf215546Sopenharmony_ci 436bf215546Sopenharmony_ci context = LLVMContextCreate(); 437bf215546Sopenharmony_ci#if LLVM_VERSION_MAJOR >= 15 438bf215546Sopenharmony_ci LLVMContextSetOpaquePointers(context, false); 439bf215546Sopenharmony_ci#endif 440bf215546Sopenharmony_ci gallivm = gallivm_create("test_module", context, NULL); 441bf215546Sopenharmony_ci 442bf215546Sopenharmony_ci test_func = build_unary_test_func(gallivm, test, length, test_name); 443bf215546Sopenharmony_ci 444bf215546Sopenharmony_ci gallivm_compile_module(gallivm); 445bf215546Sopenharmony_ci 446bf215546Sopenharmony_ci test_func_jit = (unary_func_t) gallivm_jit_function(gallivm, test_func); 447bf215546Sopenharmony_ci 448bf215546Sopenharmony_ci gallivm_free_ir(gallivm); 449bf215546Sopenharmony_ci 450bf215546Sopenharmony_ci for (j = 0; j < (test->num_values + length - 1) / length; j++) { 451bf215546Sopenharmony_ci int num_vals = ((j + 1) * length <= test->num_values) ? length : 452bf215546Sopenharmony_ci test->num_values % length; 453bf215546Sopenharmony_ci 454bf215546Sopenharmony_ci for (i = 0; i < num_vals; ++i) { 455bf215546Sopenharmony_ci in[i] = test->values[i+j*length]; 456bf215546Sopenharmony_ci } 457bf215546Sopenharmony_ci 458bf215546Sopenharmony_ci test_func_jit(out, in); 459bf215546Sopenharmony_ci for (i = 0; i < num_vals; ++i) { 460bf215546Sopenharmony_ci float testval, ref; 461bf215546Sopenharmony_ci double error, precision; 462bf215546Sopenharmony_ci boolean expected_pass = TRUE; 463bf215546Sopenharmony_ci bool pass; 464bf215546Sopenharmony_ci 465bf215546Sopenharmony_ci testval = flush_denorm_to_zero(in[i]); 466bf215546Sopenharmony_ci ref = flush_denorm_to_zero(test->ref(testval)); 467bf215546Sopenharmony_ci 468bf215546Sopenharmony_ci if (util_inf_sign(ref) && util_inf_sign(out[i]) == util_inf_sign(ref)) { 469bf215546Sopenharmony_ci error = 0; 470bf215546Sopenharmony_ci } else { 471bf215546Sopenharmony_ci error = fabs(out[i] - ref); 472bf215546Sopenharmony_ci } 473bf215546Sopenharmony_ci precision = error ? -log2(error/fabs(ref)) : FLT_MANT_DIG; 474bf215546Sopenharmony_ci 475bf215546Sopenharmony_ci pass = precision >= test->precision; 476bf215546Sopenharmony_ci 477bf215546Sopenharmony_ci if (isnan(ref)) { 478bf215546Sopenharmony_ci continue; 479bf215546Sopenharmony_ci } 480bf215546Sopenharmony_ci 481bf215546Sopenharmony_ci if (!util_get_cpu_caps()->has_neon && 482bf215546Sopenharmony_ci util_get_cpu_caps()->family != CPU_S390X && 483bf215546Sopenharmony_ci test->ref == &nearbyintf && length == 2 && 484bf215546Sopenharmony_ci ref != roundf(testval)) { 485bf215546Sopenharmony_ci /* FIXME: The generic (non SSE) path in lp_build_iround, which is 486bf215546Sopenharmony_ci * always taken for length==2 regardless of native round support, 487bf215546Sopenharmony_ci * does not round to even. */ 488bf215546Sopenharmony_ci expected_pass = FALSE; 489bf215546Sopenharmony_ci } 490bf215546Sopenharmony_ci 491bf215546Sopenharmony_ci if (test->ref == &expf && util_inf_sign(testval) == -1) { 492bf215546Sopenharmony_ci /* Some older 64-bit MSVCRT versions return -inf instead of 0 493bf215546Sopenharmony_ci * for expf(-inf). As detecting the VC runtime version is 494bf215546Sopenharmony_ci * non-trivial, just ignore the test result. */ 495bf215546Sopenharmony_ci#if defined(_MSC_VER) && defined(_WIN64) 496bf215546Sopenharmony_ci expected_pass = pass; 497bf215546Sopenharmony_ci#endif 498bf215546Sopenharmony_ci } 499bf215546Sopenharmony_ci 500bf215546Sopenharmony_ci if (pass != expected_pass || verbose) { 501bf215546Sopenharmony_ci printf("%s(%.9g): ref = %.9g, out = %.9g, precision = %f bits, %s%s\n", 502bf215546Sopenharmony_ci test_name, in[i], ref, out[i], precision, 503bf215546Sopenharmony_ci pass ? "PASS" : "FAIL", 504bf215546Sopenharmony_ci !expected_pass ? (pass ? " (unexpected)" : " (expected)" ): ""); 505bf215546Sopenharmony_ci fflush(stdout); 506bf215546Sopenharmony_ci } 507bf215546Sopenharmony_ci 508bf215546Sopenharmony_ci if (pass != expected_pass) { 509bf215546Sopenharmony_ci success = FALSE; 510bf215546Sopenharmony_ci } 511bf215546Sopenharmony_ci } 512bf215546Sopenharmony_ci } 513bf215546Sopenharmony_ci 514bf215546Sopenharmony_ci gallivm_destroy(gallivm); 515bf215546Sopenharmony_ci LLVMContextDispose(context); 516bf215546Sopenharmony_ci 517bf215546Sopenharmony_ci align_free(in); 518bf215546Sopenharmony_ci align_free(out); 519bf215546Sopenharmony_ci 520bf215546Sopenharmony_ci return success; 521bf215546Sopenharmony_ci} 522bf215546Sopenharmony_ci 523bf215546Sopenharmony_ci 524bf215546Sopenharmony_ciboolean 525bf215546Sopenharmony_citest_all(unsigned verbose, FILE *fp) 526bf215546Sopenharmony_ci{ 527bf215546Sopenharmony_ci boolean success = TRUE; 528bf215546Sopenharmony_ci int i; 529bf215546Sopenharmony_ci 530bf215546Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(unary_tests); ++i) { 531bf215546Sopenharmony_ci unsigned max_length = lp_native_vector_width / 32; 532bf215546Sopenharmony_ci unsigned length; 533bf215546Sopenharmony_ci for (length = 1; length <= max_length; length *= 2) { 534bf215546Sopenharmony_ci if (!test_unary(verbose, fp, &unary_tests[i], length)) { 535bf215546Sopenharmony_ci success = FALSE; 536bf215546Sopenharmony_ci } 537bf215546Sopenharmony_ci } 538bf215546Sopenharmony_ci } 539bf215546Sopenharmony_ci 540bf215546Sopenharmony_ci return success; 541bf215546Sopenharmony_ci} 542bf215546Sopenharmony_ci 543bf215546Sopenharmony_ci 544bf215546Sopenharmony_ciboolean 545bf215546Sopenharmony_citest_some(unsigned verbose, FILE *fp, 546bf215546Sopenharmony_ci unsigned long n) 547bf215546Sopenharmony_ci{ 548bf215546Sopenharmony_ci /* 549bf215546Sopenharmony_ci * Not randomly generated test cases, so test all. 550bf215546Sopenharmony_ci */ 551bf215546Sopenharmony_ci 552bf215546Sopenharmony_ci return test_all(verbose, fp); 553bf215546Sopenharmony_ci} 554bf215546Sopenharmony_ci 555bf215546Sopenharmony_ci 556bf215546Sopenharmony_ciboolean 557bf215546Sopenharmony_citest_single(unsigned verbose, FILE *fp) 558bf215546Sopenharmony_ci{ 559bf215546Sopenharmony_ci return TRUE; 560bf215546Sopenharmony_ci} 561