Skip to content

Commit cfa10db

Browse files
committed
Code review. Clean up generator
1 parent 41fbfdf commit cfa10db

3 files changed

Lines changed: 57 additions & 96 deletions

File tree

include/dxc/Test/HlslTestUtils.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -470,19 +470,26 @@ inline bool GetTestParamUseWARP(bool defaultVal) {
470470

471471
#ifdef FP_SUBNORMAL
472472

473-
template <typename T = float> inline bool isdenorm(T f) {
474-
return FP_SUBNORMAL == std::fpclassify(f);
475-
}
473+
inline bool isdenorm(float f) { return FP_SUBNORMAL == std::fpclassify(f); }
474+
475+
inline bool isdenorm(double d) { return FP_SUBNORMAL == std::fpclassify(d); }
476476

477477
#else
478478

479-
template <typename T = float> inline bool isdenorm(T f) {
479+
inline bool isdenorm(float f) {
480480
return (std::numeric_limits<T>::denorm_min() <= f &&
481481
f < std::numeric_limits<T>::min()) ||
482482
(-std::numeric_limits<T>::min() < f &&
483483
f <= -std::numeric_limits<T>::denorm_min());
484484
}
485485

486+
inline bool isdenorm(double d) {
487+
return (std::numeric_limits<T>::denorm_min() <= d &&
488+
d < std::numeric_limits<T>::min()) ||
489+
(-std::numeric_limits<T>::min() < d &&
490+
d <= -std::numeric_limits<T>::denorm_min());
491+
}
492+
486493
#endif // FP_SUBNORMAL
487494

488495
inline float ifdenorm_flushf(float a) {

tools/clang/unittests/HLSLExec/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ file(TO_NATIVE_PATH "${CMAKE_CURRENT_SOURCE_DIR}" DOS_STYLE_SOURCE_DIR)
4040
file(TO_NATIVE_PATH "${TAEF_BIN_DIR}" DOS_TAEF_BIN_DIR)
4141
configure_file(ExecHLSLTests.vcxproj.user.txt ExecHLSLTests.vcxproj.user)
4242

43-
# Copy the ShaoderOpArith.xml file to the output directory. It's used by the
44-
# exec tests and it's convient to have it copied here if you want to easily copy
43+
# Copy the ShaderOpArith.xml file to the output directory. It's used by the exec
44+
# tests and it's convenient to have it copied here if you want to easily copy
4545
# the tests to another machine after building.
4646
set(XML_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/ShaderOpArith.xml)
4747
set(XML_DESTINATION ${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE}/bin)

tools/clang/unittests/HLSLExec/ExecutionTest.cpp

Lines changed: 44 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,6 @@
7171

7272
// Float values for this were taken from Microsoft online documentation for the
7373
// DirectX HALF data type. HALF is equivalent to IEEE 754 binary 16 format.
74-
const DirectX::PackedVector::HALF HALF_MIN =
75-
DirectX::PackedVector::XMConvertFloatToHalf(float(6.10e-5f));
76-
const DirectX::PackedVector::HALF HALF_MAX =
77-
DirectX::PackedVector::XMConvertFloatToHalf(float(65504.0f));
7874

7975
// A more recent Windows SDK than currently required is needed for these.
8076
typedef HRESULT(WINAPI *D3D12EnableExperimentalFeaturesFn)(
@@ -11511,95 +11507,53 @@ template <typename T> struct LongVectorOpTestConfig {
1151111507
LongVectorOpType OpType = LongVectorOpType_UnInitialized;
1151211508
};
1151311509

11514-
// A helper class to generate deterministic random numbers. For any given seed
11515-
// the generated sequence will always be the same. Each call to generate() will
11516-
// return the next number in the sequence.
11510+
template <typename T> struct LongVectorTestTraits {
11511+
std::uniform_int_distribution<T> UD = std::uniform_int_distribution(
11512+
std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
11513+
};
11514+
11515+
template <> struct LongVectorTestTraits<HLSLHalf_t> {
11516+
// Float values for this were taken from Microsoft online documentation for
11517+
// the DirectX HALF data type. HALF is equivalent to IEEE 754 binary 16
11518+
// format.
11519+
std::uniform_int_distribution<DirectX::PackedVector::HALF> UD =
11520+
std::uniform_int_distribution(
11521+
DirectX::PackedVector::XMConvertFloatToHalf(float(6.10e-5f)),
11522+
DirectX::PackedVector::XMConvertFloatToHalf(float(65504.0f)));
11523+
};
11524+
11525+
template <> struct LongVectorTestTraits<HLSLBool_t> {
11526+
std::uniform_int_distribution<uint16_t> UD =
11527+
std::uniform_int_distribution<uint16_t>(0u, 1u);
11528+
};
11529+
11530+
template <> struct LongVectorTestTraits<float> {
11531+
// The ranges for generation. A std::uniform_real_distribution can only
11532+
// have a range that is equal to the types largest value. This is due to
11533+
// precision issues. So instead we define some large values.
11534+
std::uniform_real_distribution<float> UD =
11535+
std::uniform_real_distribution(-1e20f, 1e20f);
11536+
};
11537+
11538+
template <> struct LongVectorTestTraits<double> {
11539+
// The ranges for generation. A std::uniform_real_distribution can only
11540+
// have a range that is equal to the types largest value. This is due to
11541+
// precision issues. So instead we define some large values.
11542+
std::uniform_real_distribution<double> UD =
11543+
std::uniform_real_distribution(-1e100, 1e100);
11544+
};
11545+
1151711546
template <typename T> class DeterministicNumberGenerator {
11547+
// Mersenne Twister 'random' number generator. Generated numbers are based
11548+
// on the seed value and are deterministic for any given seed.
11549+
std::mt19937 Generator;
11550+
11551+
LongVectorTestTraits<T> UD;
11552+
1151811553
public:
11519-
DeterministicNumberGenerator(unsigned int seedValue) : generator(seedValue) {
11520-
if constexpr (std::is_same_v<T, HLSLHalf_t>)
11521-
DXHalfDist = std::uniform_int_distribution<DirectX::PackedVector::HALF>(
11522-
HALF_MIN, HALF_MAX);
11523-
else if constexpr (std::is_same_v<T, HLSLBool_t>)
11524-
// Anything non-zero, including negative values, is true as a bool. So 0
11525-
// and 1 are all we need.
11526-
Uint16Dist = std::uniform_int_distribution<uint16_t>(0, 1);
11527-
else if constexpr (std::is_same_v<T, int16_t>)
11528-
Int16Dist = std::uniform_int_distribution<int16_t>(
11529-
std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
11530-
else if constexpr (std::is_same_v<T, int32_t>)
11531-
Int32Dist = std::uniform_int_distribution<T>(
11532-
std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
11533-
else if constexpr (std::is_same_v<T, int64_t>)
11534-
Int64Dist = std::uniform_int_distribution<T>(
11535-
std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
11536-
else if constexpr (std::is_same_v<T, uint16_t>)
11537-
Uint16Dist = std::uniform_int_distribution<T>(
11538-
std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
11539-
else if constexpr (std::is_same_v<T, uint32_t>)
11540-
Uint32Dist = std::uniform_int_distribution<T>(
11541-
std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
11542-
else if constexpr (std::is_same_v<T, uint64_t>)
11543-
Uint64Dist = std::uniform_int_distribution<T>(
11544-
std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
11545-
else if constexpr (std::is_same_v<T, float>)
11546-
FloatDist =
11547-
std::uniform_real_distribution<T>(FLOAT_RANGE_MIN, FLOAT_RANGE_MAX);
11548-
else if constexpr (std::is_same_v<T, double>)
11549-
DoubleDist =
11550-
std::uniform_real_distribution<T>(DOUBLE_RANGE_MIN, DOUBLE_RANGE_MAX);
11551-
else
11552-
VERIFY_FAIL("Unsupported type for DeterministicNumberGenerator");
11553-
}
11554-
11555-
// Function to generate a random number within the range of the type. Ranges
11556-
// are specificed at construction time.
11557-
T generate() {
11558-
if constexpr (std::is_same_v<T, int16_t>)
11559-
return Int16Dist(generator);
11560-
if constexpr (std::is_same_v<T, HLSLBool_t>)
11561-
return Uint16Dist(generator);
11562-
if constexpr (std::is_same_v<T, HLSLHalf_t>)
11563-
return DXHalfDist(generator);
11564-
if constexpr (std::is_same_v<T, int32_t>)
11565-
return Int32Dist(generator);
11566-
if constexpr (std::is_same_v<T, int64_t>)
11567-
return Int64Dist(generator);
11568-
if constexpr (std::is_same_v<T, float>)
11569-
return FloatDist(generator);
11570-
if constexpr (std::is_same_v<T, double>)
11571-
return DoubleDist(generator);
11572-
if constexpr (std::is_same_v<T, uint16_t>)
11573-
return Uint16Dist(generator);
11574-
if constexpr (std::is_same_v<T, uint32_t>)
11575-
return Uint32Dist(generator);
11576-
if constexpr (std::is_same_v<T, uint64_t>)
11577-
return Uint64Dist(generator);
11578-
11579-
VERIFY_FAIL("Unsupported data type for generate()");
11580-
}
11554+
DeterministicNumberGenerator(unsigned SeedValue) : Generator(SeedValue) {}
1158111555

11582-
private:
11583-
// Mersenne Twister 'random' number generator. Generated numbers are based on
11584-
// the seed value and are deterministic for any given seed.
11585-
std::mt19937 generator;
11586-
std::uniform_int_distribution<int16_t> Int16Dist;
11587-
std::uniform_int_distribution<int32_t> Int32Dist;
11588-
std::uniform_int_distribution<int64_t> Int64Dist;
11589-
std::uniform_int_distribution<DirectX::PackedVector::HALF> DXHalfDist;
11590-
std::uniform_int_distribution<uint16_t> Uint16Dist;
11591-
std::uniform_int_distribution<uint32_t> Uint32Dist;
11592-
std::uniform_int_distribution<uint64_t> Uint64Dist;
11593-
std::uniform_real_distribution<float> FloatDist;
11594-
std::uniform_real_distribution<double> DoubleDist;
11595-
11596-
// The ranges for generation. A std::uniform_real_distribution can only have
11597-
// a range that is equal to the types largest value. This is due to precision
11598-
// issues. So instead we define some large values.
11599-
const float FLOAT_RANGE_MIN = -1e20f;
11600-
const float FLOAT_RANGE_MAX = 1e20f;
11601-
const double DOUBLE_RANGE_MIN = -1e100;
11602-
const double DOUBLE_RANGE_MAX = 1e100;
11556+
T generate() { return UD.UD(Generator); }
1160311557
};
1160411558

1160511559
template <typename T, std::size_t N>

0 commit comments

Comments
 (0)