forked from microsoft/DirectXShaderCompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHlslTestUtils.h
More file actions
831 lines (754 loc) · 26 KB
/
HlslTestUtils.h
File metadata and controls
831 lines (754 loc) · 26 KB
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
///////////////////////////////////////////////////////////////////////////////
// //
// HlslTestUtils.h //
// Copyright (C) Microsoft Corporation. All rights reserved. //
// This file is distributed under the University of Illinois Open Source //
// License. See LICENSE.TXT for details. //
// //
// Provides utility functions for HLSL tests. //
// //
///////////////////////////////////////////////////////////////////////////////
// *** THIS FILE CANNOT TAKE ANY LLVM DEPENDENCIES *** //
#ifndef HLSLTESTUTILS_H
#define HLSLTESTUTILS_H
#include <algorithm>
#include <atomic>
#include <cmath>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#ifdef _WIN32
// Disable -Wignored-qualifiers for WexTestClass.h.
// For const size_t GetSize() const; in TestData.h.
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wignored-qualifiers"
#endif
#include "WexTestClass.h"
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#include <dxgiformat.h>
#else
#include "WEXAdapter.h"
#include "dxc/Support/Global.h" // DXASSERT_LOCALVAR
#endif
#include "dxc/DXIL/DxilConstants.h" // DenormMode
#ifdef _HLK_CONF
#define DEFAULT_TEST_DIR L""
#define DEFAULT_EXEC_TEST_DIR DEFAULT_TEST_DIR
#else
#include "dxc/Test/TestConfig.h"
#endif
using std::max;
using std::min;
#ifndef HLSLDATAFILEPARAM
#define HLSLDATAFILEPARAM L"HlslDataDir"
#endif
#ifndef FILECHECKDUMPDIRPARAM
#define FILECHECKDUMPDIRPARAM L"FileCheckDumpDir"
#endif
// If TAEF verify macros are available, use them to alias other legacy
// comparison macros that don't have a direct translation.
//
// Other common replacements are as follows.
//
// EXPECT_EQ -> VERIFY_ARE_EQUAL
// ASSERT_EQ -> VERIFY_ARE_EQUAL
//
// Note that whether verification throws or continues depends on
// preprocessor settings.
#ifdef VERIFY_ARE_EQUAL
#ifndef EXPECT_STREQ
#define EXPECT_STREQ(a, b) VERIFY_ARE_EQUAL(0, strcmp(a, b))
#endif
#define EXPECT_STREQW(a, b) VERIFY_ARE_EQUAL(0, wcscmp(a, b))
#define VERIFY_ARE_EQUAL_CMP(a, b, ...) VERIFY_IS_TRUE(a == b, __VA_ARGS__)
#define VERIFY_ARE_EQUAL_STR(a, b) \
{ \
const char *pTmpA = (a); \
const char *pTmpB = (b); \
if (0 != strcmp(pTmpA, pTmpB)) { \
CA2W conv(pTmpB); \
WEX::Logging::Log::Comment(conv); \
const char *pA = pTmpA; \
const char *pB = pTmpB; \
while (*pA == *pB) { \
pA++; \
pB++; \
} \
wchar_t diffMsg[32]; \
swprintf_s(diffMsg, _countof(diffMsg), L"diff at %u", \
(unsigned)(pA - pTmpA)); \
WEX::Logging::Log::Comment(diffMsg); \
} \
VERIFY_ARE_EQUAL(0, strcmp(pTmpA, pTmpB)); \
}
#define VERIFY_ARE_EQUAL_WSTR(a, b) \
{ \
if (0 != wcscmp(a, b)) { \
WEX::Logging::Log::Comment(b); \
} \
VERIFY_ARE_EQUAL(0, wcscmp(a, b)); \
}
#ifndef ASSERT_EQ
#define ASSERT_EQ(expected, actual) VERIFY_ARE_EQUAL(expected, actual)
#endif
#ifndef ASSERT_NE
#define ASSERT_NE(expected, actual) VERIFY_ARE_NOT_EQUAL(expected, actual)
#endif
#ifndef TEST_F
#define TEST_F(typeName, functionName) void typeName::functionName()
#endif
#define ASSERT_HRESULT_SUCCEEDED VERIFY_SUCCEEDED
#ifndef EXPECT_EQ
#define EXPECT_EQ(expected, actual) VERIFY_ARE_EQUAL(expected, actual)
#endif
#endif // VERIFY_ARE_EQUAL
static constexpr char whitespaceChars[] = " \t\r\n";
static constexpr wchar_t wideWhitespaceChars[] = L" \t\r\n";
inline std::string strltrim(const std::string &value) {
size_t first = value.find_first_not_of(whitespaceChars);
return first == std::string::npos ? value : value.substr(first);
}
inline std::string strrtrim(const std::string &value) {
size_t last = value.find_last_not_of(whitespaceChars);
return last == std::string::npos ? value : value.substr(0, last + 1);
}
inline std::string strtrim(const std::string &value) {
return strltrim(strrtrim(value));
}
inline bool strstartswith(const std::string &value, const char *pattern) {
for (size_t i = 0;; ++i) {
if (pattern[i] == '\0')
return true;
if (i == value.size() || value[i] != pattern[i])
return false;
}
}
inline std::vector<std::string>
strtok(const std::string &value, const char *delimiters = whitespaceChars) {
size_t searchOffset = 0;
std::vector<std::string> tokens;
while (searchOffset != value.size()) {
size_t tokenStartIndex = value.find_first_not_of(delimiters, searchOffset);
if (tokenStartIndex == std::string::npos)
break;
size_t tokenEndIndex = value.find_first_of(delimiters, tokenStartIndex);
if (tokenEndIndex == std::string::npos)
tokenEndIndex = value.size();
tokens.emplace_back(
value.substr(tokenStartIndex, tokenEndIndex - tokenStartIndex));
searchOffset = tokenEndIndex;
}
return tokens;
}
inline std::vector<std::wstring>
strtok(const std::wstring &value,
const wchar_t *delimiters = wideWhitespaceChars) {
size_t searchOffset = 0;
std::vector<std::wstring> tokens;
while (searchOffset != value.size()) {
size_t tokenStartIndex = value.find_first_not_of(delimiters, searchOffset);
if (tokenStartIndex == std::string::npos)
break;
size_t tokenEndIndex = value.find_first_of(delimiters, tokenStartIndex);
if (tokenEndIndex == std::string::npos)
tokenEndIndex = value.size();
tokens.emplace_back(
value.substr(tokenStartIndex, tokenEndIndex - tokenStartIndex));
searchOffset = tokenEndIndex;
}
return tokens;
}
// strreplace will replace all instances of lookFors with replacements at the
// same index. Will log an error if the string is not found, unless the first
// character is ? marking it optional.
inline void strreplace(const std::vector<std::string> &lookFors,
const std::vector<std::string> &replacements,
std::string &str) {
for (unsigned i = 0; i < lookFors.size(); ++i) {
bool bOptional = false;
bool found = false;
size_t pos = 0;
LPCSTR pLookFor = lookFors[i].data();
size_t lookForLen = lookFors[i].size();
if (pLookFor[0] == '?') {
bOptional = true;
pLookFor++;
lookForLen--;
}
if (!pLookFor || !*pLookFor) {
continue;
}
for (;;) {
pos = str.find(pLookFor, pos);
if (pos == std::string::npos)
break;
found = true; // at least once
str.replace(pos, lookForLen, replacements[i]);
pos += replacements[i].size();
}
if (!bOptional) {
if (!found) {
WEX::Logging::Log::Comment(WEX::Common::String().Format(
L"String not found: '%S' in text:\r\n%.*S", pLookFor,
(unsigned)str.size(), str.data()));
}
VERIFY_IS_TRUE(found);
}
}
}
namespace hlsl_test {
inline std::wstring vFormatToWString(const wchar_t *fmt, va_list argptr) {
std::wstring result;
#ifdef _WIN32
int len = _vscwprintf(fmt, argptr);
result.resize(len + 1);
vswprintf_s((wchar_t *)result.data(), len + 1, fmt, argptr);
#else
wchar_t fmtOut[1000];
int len = vswprintf(fmtOut, 1000, fmt, argptr);
DXASSERT_LOCALVAR(len, len >= 0,
"Too long formatted string in vFormatToWstring");
result = fmtOut;
#endif
return result;
}
inline std::wstring FormatToWString(const wchar_t *fmt, ...) {
va_list args;
va_start(args, fmt);
std::wstring result(vFormatToWString(fmt, args));
va_end(args);
return result;
}
inline void LogCommentFmt(const wchar_t *fmt, ...) {
va_list args;
va_start(args, fmt);
std::wstring buf(vFormatToWString(fmt, args));
va_end(args);
WEX::Logging::Log::Comment(buf.data());
}
inline void LogErrorFmt(const wchar_t *fmt, ...) {
va_list args;
va_start(args, fmt);
std::wstring buf(vFormatToWString(fmt, args));
va_end(args);
WEX::Logging::Log::Error(buf.data());
}
inline void LogWarningFmt(const wchar_t *fmt, ...) {
va_list args;
va_start(args, fmt);
std::wstring buf(vFormatToWString(fmt, args));
va_end(args);
#ifdef _WIN32
WEX::Logging::Log::Warning(buf.data());
#else
WEX::Logging::Log::Comment((L"WARNING: " + buf).data());
#endif
}
inline void LogErrorFmtThrow(const char *fileName, int line, const wchar_t *fmt,
...) {
va_list args;
va_start(args, fmt);
std::wstring buf(vFormatToWString(fmt, args));
va_end(args);
std::wstringstream wss;
wss << L"Error in file: " << fileName << L" at line: " << line << L"\n"
<< buf.data() << L"\n"
<< buf;
WEX::Logging::Log::Error(wss.str().c_str());
// Throws an exception to abort the test.
VERIFY_FAIL(L"Test error");
}
// Macro to pass the file name and line number. Otherwise TAEF prints this file
// and line number.
#define LOG_ERROR_FMT_THROW(fmt, ...) \
hlsl_test::LogErrorFmtThrow(__FILE__, __LINE__, fmt, __VA_ARGS__)
inline std::wstring
GetPathToHlslDataFile(const wchar_t *relative,
LPCWSTR paramName = HLSLDATAFILEPARAM,
LPCWSTR defaultDataDir = DEFAULT_TEST_DIR) {
WEX::TestExecution::SetVerifyOutput verifySettings(
WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
WEX::Common::String HlslDataDirValue;
if (std::wstring(paramName).compare(HLSLDATAFILEPARAM) != 0) {
// Not fatal, for instance, FILECHECKDUMPDIRPARAM will dump files before
// running FileCheck, so they can be compared run to run
if (FAILED(WEX::TestExecution::RuntimeParameters::TryGetValue(
paramName, HlslDataDirValue)))
return std::wstring();
} else {
if (FAILED(WEX::TestExecution::RuntimeParameters::TryGetValue(
HLSLDATAFILEPARAM, HlslDataDirValue)))
HlslDataDirValue = defaultDataDir;
}
wchar_t envPath[MAX_PATH];
wchar_t expanded[MAX_PATH];
swprintf_s(envPath, _countof(envPath), L"%ls\\%ls",
reinterpret_cast<const wchar_t *>(HlslDataDirValue.GetBuffer()),
relative);
VERIFY_WIN32_BOOL_SUCCEEDED(
ExpandEnvironmentStringsW(envPath, expanded, _countof(expanded)));
return std::wstring(expanded);
}
inline bool PathLooksAbsolute(LPCWSTR name) {
// Very simplified, only for the cases we care about in the test suite.
#ifdef _WIN32
return name && *name && ((*name == L'\\') || (name[1] == L':'));
#else
return name && *name && (*name == L'/');
#endif
}
static bool HasRunLine(std::string &line) {
const char *delimiters = " ;/";
auto lineelems = strtok(line, delimiters);
return !lineelems.empty() && lineelems.front().compare("RUN:") == 0;
}
inline std::vector<std::string> GetRunLines(const LPCWSTR name) {
const std::wstring path = PathLooksAbsolute(name)
? std::wstring(name)
: hlsl_test::GetPathToHlslDataFile(name);
#ifdef _WIN32
std::ifstream infile(path);
#else
std::ifstream infile((CW2A(path.c_str())));
#endif
if (infile.fail() || infile.bad()) {
std::wstring errMsg(L"Unable to read file ");
errMsg += path;
WEX::Logging::Log::Error(errMsg.c_str());
VERIFY_FAIL();
}
std::vector<std::string> runlines;
std::string line;
constexpr size_t runlinesize = 300;
while (std::getline(infile, line)) {
if (!HasRunLine(line))
continue;
char runline[runlinesize];
memset(runline, 0, runlinesize);
memcpy(runline, line.c_str(), min(runlinesize, line.size()));
runlines.emplace_back(runline);
}
return runlines;
}
inline std::string GetFirstLine(LPCWSTR name) {
char firstLine[300];
memset(firstLine, 0, sizeof(firstLine));
const std::wstring path = PathLooksAbsolute(name)
? std::wstring(name)
: hlsl_test::GetPathToHlslDataFile(name);
#ifdef _WIN32
std::ifstream infile(path);
#else
std::ifstream infile((CW2A(path.c_str())));
#endif
if (infile.bad()) {
std::wstring errMsg(L"Unable to read file ");
errMsg += path;
WEX::Logging::Log::Error(errMsg.c_str());
VERIFY_FAIL();
}
infile.getline(firstLine, _countof(firstLine));
return firstLine;
}
inline HANDLE CreateFileForReading(LPCWSTR path) {
HANDLE sourceHandle =
CreateFileW(path, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
if (sourceHandle == INVALID_HANDLE_VALUE) {
DWORD err = GetLastError();
std::wstring errorMessage(
FormatToWString(L"Unable to open file '%s', err=%u", path, err)
.c_str());
VERIFY_SUCCEEDED(HRESULT_FROM_WIN32(err), errorMessage.c_str());
}
return sourceHandle;
}
inline HANDLE CreateNewFileForReadWrite(LPCWSTR path) {
HANDLE sourceHandle = CreateFileW(path, GENERIC_READ | GENERIC_WRITE, 0, 0,
CREATE_ALWAYS, 0, 0);
if (sourceHandle == INVALID_HANDLE_VALUE) {
DWORD err = GetLastError();
std::wstring errorMessage(
FormatToWString(L"Unable to create file '%s', err=%u", path, err)
.c_str());
VERIFY_SUCCEEDED(HRESULT_FROM_WIN32(err), errorMessage.c_str());
}
return sourceHandle;
}
// Copy of Unicode::IsStarMatchT/IsStarMatchWide is included here to avoid the
// dependency on DXC support libraries.
template <typename TChar>
inline static bool IsStarMatchT(const TChar *pMask, size_t maskLen,
const TChar *pName, size_t nameLen,
TChar star) {
if (maskLen == 0 && nameLen == 0) {
return true;
}
if (maskLen == 0 || nameLen == 0) {
return false;
}
if (pMask[maskLen - 1] == star) {
// Prefix match.
if (maskLen == 1) { // For just '*', everything is a match.
return true;
}
--maskLen;
if (maskLen > nameLen) { // Mask is longer than name, can't be a match.
return false;
}
return 0 == memcmp(pMask, pName, sizeof(TChar) * maskLen);
} else {
// Exact match.
if (nameLen != maskLen) {
return false;
}
return 0 == memcmp(pMask, pName, sizeof(TChar) * nameLen);
}
}
inline bool IsStarMatchWide(const wchar_t *pMask, size_t maskLen,
const wchar_t *pName, size_t nameLen) {
return IsStarMatchT<wchar_t>(pMask, maskLen, pName, nameLen, L'*');
}
inline bool GetTestParamBool(LPCWSTR name) {
WEX::Common::String ParamValue;
WEX::Common::String NameValue;
if (FAILED(WEX::TestExecution::RuntimeParameters::TryGetValue(name,
ParamValue))) {
return false;
}
if (ParamValue.IsEmpty()) {
return false;
}
if (0 == wcscmp(ParamValue, L"*")) {
return true;
}
VERIFY_SUCCEEDED(WEX::TestExecution::RuntimeParameters::TryGetValue(
L"TestName", NameValue));
if (NameValue.IsEmpty()) {
return false;
}
return hlsl_test::IsStarMatchWide(ParamValue, ParamValue.GetLength(),
NameValue, NameValue.GetLength());
}
inline bool GetTestParamUseWARP(bool defaultVal) {
WEX::Common::String AdapterValue;
if (FAILED(WEX::TestExecution::RuntimeParameters::TryGetValue(
L"Adapter", AdapterValue))) {
return defaultVal;
}
if ((defaultVal && AdapterValue.IsEmpty()) ||
AdapterValue.CompareNoCase(L"WARP") == 0 ||
AdapterValue.CompareNoCase(L"Microsoft Basic Render Driver") == 0) {
return true;
}
return false;
}
} // namespace hlsl_test
#ifdef FP_SUBNORMAL
template <typename T> inline bool isdenorm(T f) {
return FP_SUBNORMAL == std::fpclassify(f);
}
#else
template <typename T> inline bool isdenorm(T f) {
return (std::numeric_limits<T>::denorm_min() <= f &&
f < std::numeric_limits<T>::min()) ||
(-std::numeric_limits<T>::min() < f &&
f <= -std::numeric_limits<T>::denorm_min());
}
#endif // FP_SUBNORMAL
inline float ifdenorm_flushf(float a) {
return isdenorm(a) ? copysign(0.0f, a) : a;
}
inline bool ifdenorm_flushf_eq(float a, float b) {
return ifdenorm_flushf(a) == ifdenorm_flushf(b);
}
static const uint16_t Float16NaN = 0xff80;
static const uint16_t Float16PosInf = 0x7c00;
static const uint16_t Float16NegInf = 0xfc00;
static const uint16_t Float16PosDenorm = 0x0008;
static const uint16_t Float16NegDenorm = 0x8008;
static const uint16_t Float16PosZero = 0x0000;
static const uint16_t Float16NegZero = 0x8000;
inline bool GetSign(float x) { return std::signbit(x); }
inline int GetMantissa(float x) {
int bits = reinterpret_cast<int &>(x);
return bits & 0x7fffff;
}
inline int GetExponent(float x) {
int bits = reinterpret_cast<int &>(x);
return (bits >> 23) & 0xff;
}
#define FLOAT16_BIT_SIGN 0x8000
#define FLOAT16_BIT_EXP 0x7c00
#define FLOAT16_BIT_MANTISSA 0x03ff
#define FLOAT16_BIGGEST_DENORM FLOAT16_BIT_MANTISSA
#define FLOAT16_BIGGEST_NORMAL 0x7bff
inline bool isnanFloat16(uint16_t val) {
return (val & FLOAT16_BIT_EXP) == FLOAT16_BIT_EXP &&
(val & FLOAT16_BIT_MANTISSA) != 0;
}
// These are defined in ShaderOpTest.cpp using DirectXPackedVector functions.
uint16_t ConvertFloat32ToFloat16(float val) throw();
float ConvertFloat16ToFloat32(uint16_t val) throw();
inline bool CompareDoubleULP(
const double &Src, const double &Ref, int64_t ULPTolerance,
hlsl::DXIL::Float32DenormMode Mode = hlsl::DXIL::Float32DenormMode::Any) {
if (Src == Ref) {
return true;
}
if (std::isnan(Src)) {
return std::isnan(Ref);
}
if (Mode == hlsl::DXIL::Float32DenormMode::Any) {
// If denorm expected, output can be sign preserved zero. Otherwise output
// should pass the regular ulp testing.
if (isdenorm(Ref) && Src == 0 && std::signbit(Src) == std::signbit(Ref))
return true;
}
// For FTZ or Preserve mode, we should get the expected number within
// ULPTolerance for any operations.
int64_t Diff = *((const uint64_t *)&Src) - *((const uint64_t *)&Ref);
uint64_t AbsoluteDiff = Diff < 0 ? -Diff : Diff;
return AbsoluteDiff <= (uint64_t)ULPTolerance;
}
inline bool CompareDoubleEpsilon(const double &Src, const double &Ref,
double Epsilon) {
if (Src == Ref) {
return true;
}
if (std::isnan(Src)) {
return std::isnan(Ref);
}
// For FTZ or Preserve mode, we should get the expected number within
// epsilon for any operations.
return fabs(Src - Ref) < Epsilon;
}
inline bool CompareFloatULP(
const float &fsrc, const float &fref, int ULPTolerance,
hlsl::DXIL::Float32DenormMode mode = hlsl::DXIL::Float32DenormMode::Any) {
if (fsrc == fref) {
return true;
}
if (std::isnan(fsrc)) {
return std::isnan(fref);
}
if (mode == hlsl::DXIL::Float32DenormMode::Any) {
// If denorm expected, output can be sign preserved zero. Otherwise output
// should pass the regular ulp testing.
if (isdenorm(fref) && fsrc == 0 && std::signbit(fsrc) == std::signbit(fref))
return true;
}
// For FTZ or Preserve mode, we should get the expected number within
// ULPTolerance for any operations.
int diff = *((const DWORD *)&fsrc) - *((const DWORD *)&fref);
unsigned int uDiff = diff < 0 ? -diff : diff;
return uDiff <= (unsigned int)ULPTolerance;
}
inline bool CompareFloatEpsilon(
const float &fsrc, const float &fref, float epsilon,
hlsl::DXIL::Float32DenormMode mode = hlsl::DXIL::Float32DenormMode::Any) {
if (fsrc == fref) {
return true;
}
if (std::isnan(fsrc)) {
return std::isnan(fref);
}
if (mode == hlsl::DXIL::Float32DenormMode::Any) {
// If denorm expected, output can be sign preserved zero. Otherwise output
// should pass the regular epsilon testing.
if (isdenorm(fref) && fsrc == 0 && std::signbit(fsrc) == std::signbit(fref))
return true;
}
// For FTZ or Preserve mode, we should get the expected number within
// epsilon for any operations.
return fabsf(fsrc - fref) < epsilon;
}
// Compare using relative error (relative error < 2^{nRelativeExp})
inline bool CompareFloatRelativeEpsilon(
const float &fsrc, const float &fref, int nRelativeExp,
hlsl::DXIL::Float32DenormMode mode = hlsl::DXIL::Float32DenormMode::Any) {
return CompareFloatULP(fsrc, fref, 23 - nRelativeExp, mode);
}
inline bool CompareHalfULP(const uint16_t &fsrc, const uint16_t &fref,
float ULPTolerance) {
// Treat +0 and -0 as equal
if ((fsrc & ~FLOAT16_BIT_SIGN) == 0 && (fref & ~FLOAT16_BIT_SIGN) == 0)
return true;
if (fsrc == fref)
return true;
const bool nanRef = isnanFloat16(fref);
const bool nanSrc = isnanFloat16(fsrc);
if (nanRef || nanSrc)
return nanRef && nanSrc;
// Map to monotonic ordering for correct ULP diff
auto toOrdered = [](uint16_t h) -> int {
return (h & FLOAT16_BIT_SIGN) ? (~h & 0xFFFF) : (h | 0x8000);
};
// 16-bit floating point numbers must preserve denorms
int i_fsrc = toOrdered(fsrc);
int i_fref = toOrdered(fref);
int diff = i_fsrc - i_fref;
unsigned int uDiff = diff < 0 ? -diff : diff;
return uDiff <= (unsigned int)ULPTolerance;
}
inline bool CompareHalfEpsilon(const uint16_t &fsrc, const uint16_t &fref,
float epsilon) {
if (fsrc == fref)
return true;
if (isnanFloat16(fsrc))
return isnanFloat16(fref);
float src_f32 = ConvertFloat16ToFloat32(fsrc);
float ref_f32 = ConvertFloat16ToFloat32(fref);
return std::abs(src_f32 - ref_f32) < epsilon;
}
inline bool CompareHalfRelativeEpsilon(const uint16_t &fsrc,
const uint16_t &fref, int nRelativeExp) {
return CompareHalfULP(fsrc, fref, (float)(10 - nRelativeExp));
}
#ifdef _WIN32
// returns the number of bytes per pixel for a given dxgi format
// add more cases if different format needed to copy back resources
inline UINT GetByteSizeForFormat(DXGI_FORMAT value) {
switch (value) {
case DXGI_FORMAT_R32G32B32A32_TYPELESS:
return 16;
case DXGI_FORMAT_R32G32B32A32_FLOAT:
return 16;
case DXGI_FORMAT_R32G32B32A32_UINT:
return 16;
case DXGI_FORMAT_R32G32B32A32_SINT:
return 16;
case DXGI_FORMAT_R32G32B32_TYPELESS:
return 12;
case DXGI_FORMAT_R32G32B32_FLOAT:
return 12;
case DXGI_FORMAT_R32G32B32_UINT:
return 12;
case DXGI_FORMAT_R32G32B32_SINT:
return 12;
case DXGI_FORMAT_R16G16B16A16_TYPELESS:
return 8;
case DXGI_FORMAT_R16G16B16A16_FLOAT:
return 8;
case DXGI_FORMAT_R16G16B16A16_UNORM:
return 8;
case DXGI_FORMAT_R16G16B16A16_UINT:
return 8;
case DXGI_FORMAT_R16G16B16A16_SNORM:
return 8;
case DXGI_FORMAT_R16G16B16A16_SINT:
return 8;
case DXGI_FORMAT_R32G32_TYPELESS:
return 8;
case DXGI_FORMAT_R32G32_FLOAT:
return 8;
case DXGI_FORMAT_R32G32_UINT:
return 8;
case DXGI_FORMAT_R32G32_SINT:
return 8;
case DXGI_FORMAT_R32G8X24_TYPELESS:
return 8;
case DXGI_FORMAT_D32_FLOAT_S8X24_UINT:
return 4;
case DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS:
return 4;
case DXGI_FORMAT_X32_TYPELESS_G8X24_UINT:
return 4;
case DXGI_FORMAT_R10G10B10A2_TYPELESS:
return 4;
case DXGI_FORMAT_R10G10B10A2_UNORM:
return 4;
case DXGI_FORMAT_R10G10B10A2_UINT:
return 4;
case DXGI_FORMAT_R11G11B10_FLOAT:
return 4;
case DXGI_FORMAT_R8G8B8A8_TYPELESS:
return 4;
case DXGI_FORMAT_R8G8B8A8_UNORM:
return 4;
case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
return 4;
case DXGI_FORMAT_R8G8B8A8_UINT:
return 4;
case DXGI_FORMAT_R8G8B8A8_SNORM:
return 4;
case DXGI_FORMAT_R8G8B8A8_SINT:
return 4;
case DXGI_FORMAT_R16G16_TYPELESS:
return 4;
case DXGI_FORMAT_R16G16_FLOAT:
return 4;
case DXGI_FORMAT_R16G16_UNORM:
return 4;
case DXGI_FORMAT_R16G16_UINT:
return 4;
case DXGI_FORMAT_R16G16_SNORM:
return 4;
case DXGI_FORMAT_R16G16_SINT:
return 4;
case DXGI_FORMAT_R32_TYPELESS:
return 4;
case DXGI_FORMAT_D32_FLOAT:
return 4;
case DXGI_FORMAT_R32_FLOAT:
return 4;
case DXGI_FORMAT_R32_UINT:
return 4;
case DXGI_FORMAT_R32_SINT:
return 4;
case DXGI_FORMAT_R24G8_TYPELESS:
return 4;
case DXGI_FORMAT_D24_UNORM_S8_UINT:
return 4;
case DXGI_FORMAT_R24_UNORM_X8_TYPELESS:
return 4;
case DXGI_FORMAT_X24_TYPELESS_G8_UINT:
return 4;
case DXGI_FORMAT_R8G8_TYPELESS:
return 2;
case DXGI_FORMAT_R8G8_UNORM:
return 2;
case DXGI_FORMAT_R8G8_UINT:
return 2;
case DXGI_FORMAT_R8G8_SNORM:
return 2;
case DXGI_FORMAT_R8G8_SINT:
return 2;
case DXGI_FORMAT_R16_TYPELESS:
return 2;
case DXGI_FORMAT_R16_FLOAT:
return 2;
case DXGI_FORMAT_D16_UNORM:
return 2;
case DXGI_FORMAT_R16_UNORM:
return 2;
case DXGI_FORMAT_R16_UINT:
return 2;
case DXGI_FORMAT_R16_SNORM:
return 2;
case DXGI_FORMAT_R16_SINT:
return 2;
case DXGI_FORMAT_R8_TYPELESS:
return 1;
case DXGI_FORMAT_R8_UNORM:
return 1;
case DXGI_FORMAT_R8_UINT:
return 1;
case DXGI_FORMAT_R8_SNORM:
return 1;
case DXGI_FORMAT_R8_SINT:
return 1;
case DXGI_FORMAT_A8_UNORM:
return 1;
case DXGI_FORMAT_R1_UNORM:
return 1;
default:
VERIFY_FAILED(E_INVALIDARG);
return 0;
}
}
#endif
#endif // HLSLTESTUTILS_H