MathUtilTest: Fix tests on MSVC - Document compiler bug

MSVC's implementation of numeric_limits currently generates incorrect
signaling NaNs. The resulting values are actually quiet NaNs instead.

This commit is based off of a solution by shuffle2. The only
difference is a template specialization for floats is also added
to cover all bases
This commit is contained in:
Lioncash 2016-04-03 19:08:47 -04:00
parent b3c77fd96a
commit 8d9221a71e
2 changed files with 28 additions and 2 deletions

View File

@ -12,6 +12,32 @@
namespace MathUtil
{
template <typename T>
constexpr T SNANConstant()
{
return std::numeric_limits<T>::signaling_NaN();
}
#ifdef _MSC_VER
// MSVC needs a workaround, because its std::numeric_limits<double>::signaling_NaN()
// will use __builtin_nans, which is improperly handled by the compiler and generates
// a bad constant. Here we go back to the version MSVC used before the builtin.
// TODO: Remove this and use numeric_limits directly whenever this bug is fixed.
template <>
constexpr double SNANConstant()
{
return (_CSTD _Snan._Double);
}
template <>
constexpr float SNANConstant()
{
return (_CSTD _Snan._Float);
}
#endif
template<class T>
constexpr T Clamp(const T val, const T& min, const T& max)
{

View File

@ -23,13 +23,13 @@ TEST(MathUtil, Clamp)
TEST(MathUtil, IsQNAN)
{
EXPECT_TRUE(MathUtil::IsQNAN(std::numeric_limits<double>::quiet_NaN()));
EXPECT_FALSE(MathUtil::IsQNAN(std::numeric_limits<double>::signaling_NaN()));
EXPECT_FALSE(MathUtil::IsQNAN(MathUtil::SNANConstant<double>()));
}
TEST(MathUtil, IsSNAN)
{
EXPECT_FALSE(MathUtil::IsSNAN(std::numeric_limits<double>::quiet_NaN()));
EXPECT_TRUE(MathUtil::IsSNAN(std::numeric_limits<double>::signaling_NaN()));
EXPECT_TRUE(MathUtil::IsSNAN(MathUtil::SNANConstant<double>()));
}
TEST(MathUtil, IntLog2)