Skip to content

Commit a671596

Browse files
authored
LongVectors: rearrange and tidy up code (microsoft#7748)
The previous changes to LongVectors (7f80fbc) tried to avoid moving code around too much. This change tries to avoid any substantial changes in functionality and focuses on moving code around, as well as some miscellaneous cleanups. Changes in here: * Move code around to be closer to where it is needed * Remove code from LongVectors.h that doesn't absolutely need to be there * Prefer auto* over auto * More explicit about conversions HLSLHalf_t construction from integers… * Remove unnecessary explicit copy constructor * Define NOMINMAX so we don't need to do strange things to use min/max * Consolidate things for dealing with Data Types * Prefer enum class
1 parent 1390b76 commit a671596

3 files changed

Lines changed: 595 additions & 678 deletions

File tree

tools/clang/unittests/HLSLExec/LongVectorTestData.h

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
#include <string>
1010
#include <vector>
1111

12+
#include <DirectXMath.h>
13+
#include <DirectXPackedVector.h>
14+
1215
namespace LongVector {
1316

1417
// A helper struct because C++ bools are 1 byte and HLSL bools are 4 bytes.
@@ -19,7 +22,6 @@ struct HLSLBool_t {
1922
HLSLBool_t() : Val(0) {}
2023
HLSLBool_t(int32_t Val) : Val(Val) {}
2124
HLSLBool_t(bool Val) : Val(Val) {}
22-
HLSLBool_t(const HLSLBool_t &Other) : Val(Other.Val) {}
2325

2426
bool operator==(const HLSLBool_t &Other) const {
2527
return static_cast<bool>(Val) == static_cast<bool>(Other.Val);
@@ -76,27 +78,30 @@ struct HLSLBool_t {
7678
// it. Simple little wrapping struct to help handle the right behavior.
7779
struct HLSLHalf_t {
7880
HLSLHalf_t() : Val(0) {}
79-
HLSLHalf_t(DirectX::PackedVector::HALF Val) : Val(Val) {}
80-
HLSLHalf_t(const HLSLHalf_t &Other) : Val(Other.Val) {}
8181
HLSLHalf_t(const float F) {
8282
Val = DirectX::PackedVector::XMConvertFloatToHalf(F);
8383
}
8484
HLSLHalf_t(const double D) {
85-
float F = 0.0f;
86-
// We wrap '::max' in () to prevent it from being expanded as a
87-
// macro by the Windows SDK.
88-
if (D >= (std::numeric_limits<double>::max)())
89-
F = (std::numeric_limits<float>::max)();
85+
float F;
86+
if (D >= std::numeric_limits<double>::max())
87+
F = std::numeric_limits<float>::max();
9088
else if (D <= std::numeric_limits<double>::lowest())
9189
F = std::numeric_limits<float>::lowest();
9290
else
9391
F = static_cast<float>(D);
9492

9593
Val = DirectX::PackedVector::XMConvertFloatToHalf(F);
9694
}
97-
HLSLHalf_t(const int I) {
98-
const float F = static_cast<float>(I);
99-
Val = DirectX::PackedVector::XMConvertFloatToHalf(F);
95+
96+
// PackedVector::HALF is a uint16. Make sure we don't ever accidentally
97+
// convert one of these to a HLSLHalf_t by arithmetically converting it to a
98+
// float.
99+
HLSLHalf_t(DirectX::PackedVector::HALF) = delete;
100+
101+
static HLSLHalf_t FromHALF(DirectX::PackedVector::HALF Half) {
102+
HLSLHalf_t H;
103+
H.Val = Half;
104+
return H;
100105
}
101106

102107
// Implicit conversion to float for use with things like std::acos, std::tan,
@@ -150,32 +155,32 @@ struct HLSLHalf_t {
150155
HLSLHalf_t operator*(const HLSLHalf_t &Other) const {
151156
const float A = DirectX::PackedVector::XMConvertHalfToFloat(Val);
152157
const float B = DirectX::PackedVector::XMConvertHalfToFloat(Other.Val);
153-
return HLSLHalf_t(DirectX::PackedVector::XMConvertFloatToHalf(A * B));
158+
return FromHALF(DirectX::PackedVector::XMConvertFloatToHalf(A * B));
154159
}
155160

156161
HLSLHalf_t operator+(const HLSLHalf_t &Other) const {
157162
const float A = DirectX::PackedVector::XMConvertHalfToFloat(Val);
158163
const float B = DirectX::PackedVector::XMConvertHalfToFloat(Other.Val);
159-
return HLSLHalf_t(DirectX::PackedVector::XMConvertFloatToHalf(A + B));
164+
return FromHALF((DirectX::PackedVector::XMConvertFloatToHalf(A + B)));
160165
}
161166

162167
HLSLHalf_t operator-(const HLSLHalf_t &Other) const {
163168
const float A = DirectX::PackedVector::XMConvertHalfToFloat(Val);
164169
const float B = DirectX::PackedVector::XMConvertHalfToFloat(Other.Val);
165-
return HLSLHalf_t(DirectX::PackedVector::XMConvertFloatToHalf(A - B));
170+
return FromHALF(DirectX::PackedVector::XMConvertFloatToHalf(A - B));
166171
}
167172

168173
HLSLHalf_t operator/(const HLSLHalf_t &Other) const {
169174
const float A = DirectX::PackedVector::XMConvertHalfToFloat(Val);
170175
const float B = DirectX::PackedVector::XMConvertHalfToFloat(Other.Val);
171-
return HLSLHalf_t(DirectX::PackedVector::XMConvertFloatToHalf(A / B));
176+
return FromHALF(DirectX::PackedVector::XMConvertFloatToHalf(A / B));
172177
}
173178

174179
HLSLHalf_t operator%(const HLSLHalf_t &Other) const {
175180
const float A = DirectX::PackedVector::XMConvertHalfToFloat(Val);
176181
const float B = DirectX::PackedVector::XMConvertHalfToFloat(Other.Val);
177182
const float C = std::fmod(A, B);
178-
return HLSLHalf_t(DirectX::PackedVector::XMConvertFloatToHalf(C));
183+
return FromHALF(DirectX::PackedVector::XMConvertFloatToHalf(C));
179184
}
180185

181186
// So we can construct std::wstrings using std::wostream

0 commit comments

Comments
 (0)