Skip to content

Commit

Permalink
Patch ccm::nan in a manner that should make msvc happy
Browse files Browse the repository at this point in the history
  • Loading branch information
Rinzii committed Apr 1, 2024
1 parent 7306590 commit ed90954
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
4 changes: 3 additions & 1 deletion include/ccmath/math/basic/impl/nan_double_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ namespace ccm::internal
// NOLINTEND

// Set the tag bits for NaN
dbl_bits |= UINT64_C(0x7FF8000000000000);
//dbl_bits |= UINT64_C(0x7FF8000000000000);
dbl_bits |= ccm::helpers::bit_cast<std::uint64_t>(std::numeric_limits<double>::quiet_NaN());


// Convert the uint64_t tag into a double NaN
return ccm::helpers::bit_cast<double>(dbl_bits);
Expand Down
5 changes: 3 additions & 2 deletions include/ccmath/math/basic/impl/nan_float_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ namespace ccm::internal
// NOLINTEND

// Set the tag bits for NaN
flt_bits |= UINT32_C(0x7F800000);
//flt_bits |= UINT32_C(0x7F800000);
flt_bits |= ccm::helpers::bit_cast<std::uint32_t>(std::numeric_limits<float>::quiet_NaN());

// Convert the uint32_t bits to a float
// Convert the bits to a float
return ccm::helpers::bit_cast<float>(flt_bits);
}
} // namespace impl
Expand Down
18 changes: 6 additions & 12 deletions test/basic/nan_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,10 @@ TEST(CcmathBasicTests, NanStaticAssert)
TEST(CcmathBasicTests, NanDouble)
{

// TODO: Investigate why MSVC is adding 1 to the value of the nan bits of tests using this variable.
#if defined(_MSC_VER)
const std::uint64_t addPlusOneForMsvc = 1;
#else
const std::uint64_t addPlusOneForMsvc = 0;
#endif

// Check the outcome if we are handling an empty string
std::uint64_t ccmNanBits = ccm::helpers::bit_cast<std::uint64_t>(ccm::nan("")) + addPlusOneForMsvc;
std::uint64_t stdNanBits = ccm::helpers::bit_cast<std::uint64_t>(std::nan("")) + addPlusOneForMsvc;
std::uint64_t ccmNanBits = ccm::helpers::bit_cast<std::uint64_t>(ccm::nan(""));
std::uint64_t stdNanBits = ccm::helpers::bit_cast<std::uint64_t>(std::nan(""));
EXPECT_EQ(ccmNanBits, stdNanBits);

// Check the outcome if we are handling a string with a single character that represents a number
Expand All @@ -57,13 +51,13 @@ TEST(CcmathBasicTests, NanDouble)
EXPECT_EQ(ccmNanBits, stdNanBits);

// Check the outcome if we are handling a string with multiple characters with both numbers and characters with no hex prefix
ccmNanBits = ccm::helpers::bit_cast<std::uint64_t>(ccm::nan("foo123bar")) + addPlusOneForMsvc;
stdNanBits = ccm::helpers::bit_cast<std::uint64_t>(std::nan("foo123bar")) + addPlusOneForMsvc;
ccmNanBits = ccm::helpers::bit_cast<std::uint64_t>(ccm::nan("foo123bar"));
stdNanBits = ccm::helpers::bit_cast<std::uint64_t>(std::nan("foo123bar"));
EXPECT_EQ(ccmNanBits, stdNanBits);

// Check the outcome if we are handling a string with a hex prefix and a single character
ccmNanBits = ccm::helpers::bit_cast<std::uint64_t>(ccm::nan("0x1")) + addPlusOneForMsvc;
stdNanBits = ccm::helpers::bit_cast<std::uint64_t>(std::nan("0x1") + addPlusOneForMsvc);
ccmNanBits = ccm::helpers::bit_cast<std::uint64_t>(ccm::nan("0x1"));
stdNanBits = ccm::helpers::bit_cast<std::uint64_t>(std::nan("0x1"));
EXPECT_EQ(ccmNanBits, stdNanBits);

// Check for multi-digit wrapping
Expand Down

0 comments on commit ed90954

Please sign in to comment.