git.s-ol.nu ~forks/DiligentCore / 5d708d2
MathLib: added stream insreters for vector types assiduous 7 months ago
3 changed file(s) with 182 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
4949
5050 #include <cmath>
5151 #include <algorithm>
52 #include <iostream>
5253
5354 #include "HashUtils.hpp"
5455
21882189 return x | (y << 1u);
21892190 }
21902191
2192 /// Returns the least-signficant bit and clears it in the input argument
2193 template <typename T>
2194 typename std::enable_if<std::is_integral<T>::value, T>::type ExtractLSB(T& bits)
2195 {
2196 if (bits == T{0})
2197 return 0;
2198
2199 const T bit = bits & ~(bits - T{1});
2200 bits &= ~bit;
2201
2202 return bit;
2203 }
2204
2205 /// Returns the enum value representing the least-signficant bit and clears it in the input argument
2206 template <typename T>
2207 typename std::enable_if<std::is_enum<T>::value, T>::type ExtractLSB(T& bits)
2208 {
2209 return static_cast<T>(ExtractLSB(reinterpret_cast<typename std::underlying_type<T>::type&>(bits)));
2210 }
2211
2212
2213 inline std::ostream& operator<<(std::ostream& os, const float4& vec)
2214 {
2215 return os << "float4(" << vec.x << ", " << vec.y << ", " << vec.z << ", " << vec.w << ')';
2216 }
2217 inline std::ostream& operator<<(std::ostream& os, const float3& vec)
2218 {
2219 return os << "float3(" << vec.x << ", " << vec.y << ", " << vec.z << ')';
2220 }
2221 inline std::ostream& operator<<(std::ostream& os, const float2& vec)
2222 {
2223 return os << "float2(" << vec.x << ", " << vec.y << ')';
2224 }
2225
2226 inline std::ostream& operator<<(std::ostream& os, const int4& vec)
2227 {
2228 return os << "int4(" << vec.x << ", " << vec.y << ", " << vec.z << ", " << vec.w << ')';
2229 }
2230 inline std::ostream& operator<<(std::ostream& os, const int3& vec)
2231 {
2232 return os << "int3(" << vec.x << ", " << vec.y << ", " << vec.z << ')';
2233 }
2234 inline std::ostream& operator<<(std::ostream& os, const int2& vec)
2235 {
2236 return os << "int2(" << vec.x << ", " << vec.y << ')';
2237 }
2238
2239
2240 inline std::ostream& operator<<(std::ostream& os, const uint4& vec)
2241 {
2242 return os << "uint4(" << vec.x << ", " << vec.y << ", " << vec.z << ", " << vec.w << ')';
2243 }
2244 inline std::ostream& operator<<(std::ostream& os, const uint3& vec)
2245 {
2246 return os << "uint3(" << vec.x << ", " << vec.y << ", " << vec.z << ')';
2247 }
2248 inline std::ostream& operator<<(std::ostream& os, const uint2& vec)
2249 {
2250 return os << "uint2(" << vec.x << ", " << vec.y << ')';
2251 }
2252
21912253 } // namespace Diligent
21922254
21932255
7575 LOG_INFO_MESSAGE(ss.str());
7676 }
7777
78 } // namespace Test
78 } // namespace Testing
7979
8080 } // namespace Diligent
8181 namespace
2525 */
2626
2727 #include <climits>
28 #include <sstream>
2829
2930 #include "BasicMath.hpp"
3031 #include "AdvancedMath.hpp"
16141615 res |= y != 0 ? (Uint32{2} << (j * 2u)) : 0;
16151616 EXPECT_EQ(BitInterleave16(x, y), res);
16161617 }
1618 }
1619 }
1620
1621 TEST(Common_BasicMath, ExtractLSB)
1622 {
1623 {
1624 Uint32 Bits = 0;
1625 EXPECT_EQ(ExtractLSB(Bits), 0U);
1626 EXPECT_EQ(Bits, 0U);
1627 }
1628
1629 for (Uint8 i = 0; i < 8; ++i)
1630 {
1631 const Uint8 Bit = 1 << i;
1632
1633 Uint8 Bits = Bit;
1634 EXPECT_EQ(ExtractLSB(Bits), Bit);
1635 EXPECT_EQ(Bits, 0U);
1636 }
1637
1638 for (Uint32 i = 0; i < 32; ++i)
1639 {
1640 const Uint32 Bit = 1 << i;
1641
1642 Uint32 Bits = Bit;
1643 EXPECT_EQ(ExtractLSB(Bits), Bit);
1644 EXPECT_EQ(Bits, 0U);
1645 }
1646
1647 for (Uint64 i = 0; i < 64; ++i)
1648 {
1649 const Uint64 Bit = Uint64{1} << i;
1650
1651 Uint64 Bits = Bit;
1652 EXPECT_EQ(ExtractLSB(Bits), Bit);
1653 EXPECT_EQ(Bits, 0U);
1654 }
1655
1656 for (Uint32 i = 0; i < 32; ++i)
1657 {
1658 for (Uint32 j = i + 1; j < 32; ++j)
1659 {
1660 const Uint32 LSB = 1 << i;
1661 const Uint32 MSB = 1 << j;
1662
1663 Uint32 Bits = LSB | MSB;
1664 EXPECT_EQ(ExtractLSB(Bits), LSB);
1665 EXPECT_EQ(ExtractLSB(Bits), MSB);
1666 EXPECT_EQ(Bits, 0U);
1667 }
1668 }
1669
1670 {
1671 enum FLAG_ENUM : Uint32
1672 {
1673 FLAG_ENUM_00 = 0x00,
1674 FLAG_ENUM_01 = 0x01,
1675 FLAG_ENUM_02 = 0x02,
1676 FLAG_ENUM_10 = 0x10
1677 };
1678 FLAG_ENUM Bits = static_cast<FLAG_ENUM>(FLAG_ENUM_01 | FLAG_ENUM_02 | FLAG_ENUM_10);
1679 EXPECT_EQ(ExtractLSB(Bits), FLAG_ENUM_01);
1680 EXPECT_EQ(Bits, static_cast<FLAG_ENUM>(FLAG_ENUM_02 | FLAG_ENUM_10));
1681 EXPECT_EQ(ExtractLSB(Bits), FLAG_ENUM_02);
1682 EXPECT_EQ(Bits, FLAG_ENUM_10);
1683 EXPECT_EQ(ExtractLSB(Bits), FLAG_ENUM_10);
1684 EXPECT_EQ(Bits, FLAG_ENUM_00);
1685 }
1686 }
1687
1688 TEST(Common_BasicMath, VectorInserters)
1689 {
1690 {
1691 std::stringstream ss;
1692 ss << float4{1, 20, 300, 4000};
1693 EXPECT_STREQ(ss.str().c_str(), "float4(1, 20, 300, 4000)");
1694 }
1695 {
1696 std::stringstream ss;
1697 ss << float3{1, 201, 302};
1698 EXPECT_STREQ(ss.str().c_str(), "float3(1, 201, 302)");
1699 }
1700 {
1701 std::stringstream ss;
1702 ss << float2{1, 23};
1703 EXPECT_STREQ(ss.str().c_str(), "float2(1, 23)");
1704 }
1705
1706 {
1707 std::stringstream ss;
1708 ss << int4{1, 20, 300, 4000};
1709 EXPECT_STREQ(ss.str().c_str(), "int4(1, 20, 300, 4000)");
1710 }
1711 {
1712 std::stringstream ss;
1713 ss << int3{1, 201, 302};
1714 EXPECT_STREQ(ss.str().c_str(), "int3(1, 201, 302)");
1715 }
1716 {
1717 std::stringstream ss;
1718 ss << int2{1, 23};
1719 EXPECT_STREQ(ss.str().c_str(), "int2(1, 23)");
1720 }
1721
1722 {
1723 std::stringstream ss;
1724 ss << uint4{1, 20, 300, 4000};
1725 EXPECT_STREQ(ss.str().c_str(), "uint4(1, 20, 300, 4000)");
1726 }
1727 {
1728 std::stringstream ss;
1729 ss << uint3{1, 201, 302};
1730 EXPECT_STREQ(ss.str().c_str(), "uint3(1, 201, 302)");
1731 }
1732 {
1733 std::stringstream ss;
1734 ss << uint2{1, 23};
1735 EXPECT_STREQ(ss.str().c_str(), "uint2(1, 23)");
16171736 }
16181737 }
16191738