@@ -75,54 +75,45 @@ class MemScanner
7575 T secondByte;
7676 std::memcpy (&firstByte, first, sizeof (T));
7777 std::memcpy (&secondByte, second, sizeof (T));
78- size_t size = sizeof (T);
79- switch (size)
80- {
81- case 2 :
78+
79+ constexpr size_t size{sizeof (T)};
80+ static_assert (size == 1 || size == 2 || size == 4 || size == 8 , " Unexpected type size" );
81+
82+ if constexpr (size == 2 )
8283 {
83- u16 firstHalfword = 0 ;
84- std::memcpy (&firstHalfword, &firstByte, sizeof (u16 ));
84+ u16 firstHalfword = Common::bit_cast<u16 , T>(firstByte);
8585 firstHalfword = Common::bSwap16 (firstHalfword);
86- std::memcpy (&firstByte, &firstHalfword, sizeof ( u16 ) );
86+ firstByte = Common::bit_cast<T, u16 >(firstHalfword );
8787 if (bswapSecond)
8888 {
89- std::memcpy (&firstHalfword, &secondByte, sizeof ( u16 ) );
89+ firstHalfword = Common::bit_cast< u16 , T>(secondByte );
9090 firstHalfword = Common::bSwap16 (firstHalfword);
91- std::memcpy (&secondByte, &firstHalfword, sizeof ( u16 ) );
91+ secondByte = Common::bit_cast<T, u16 >(firstHalfword );
9292 }
93- break ;
9493 }
95- case 4 :
94+ else if constexpr (size == 4 )
9695 {
97- u32 firstWord = 0 ;
98- std::memcpy (&firstWord, &firstByte, sizeof (u32 ));
96+ u32 firstWord = Common::bit_cast<u32 , T>(firstByte);
9997 firstWord = Common::bSwap32 (firstWord);
100- std::memcpy (&firstByte, &firstWord, sizeof ( u32 ) );
98+ firstByte = Common::bit_cast<T, u32 >(firstWord );
10199 if (bswapSecond)
102100 {
103- std::memcpy (&firstWord, &secondByte, sizeof ( u32 ) );
101+ firstWord = Common::bit_cast< u32 , T>(secondByte );
104102 firstWord = Common::bSwap32 (firstWord);
105- std::memcpy (&secondByte, &firstWord, sizeof ( u32 ) );
103+ secondByte = Common::bit_cast<T, u32 >(firstWord );
106104 }
107- break ;
108105 }
109- case 8 :
106+ else if constexpr (size == 8 )
110107 {
111- u64 firstDoubleword = 0 ;
112- std::memcpy (&firstDoubleword, &firstByte, sizeof (u64 ));
108+ u64 firstDoubleword = Common::bit_cast<u64 , T>(firstByte);
113109 firstDoubleword = Common::bSwap64 (firstDoubleword);
114- std::memcpy (&firstByte, &firstDoubleword, sizeof ( u64 ) );
110+ firstByte = Common::bit_cast<T, u64 >(firstDoubleword );
115111 if (bswapSecond)
116112 {
117- std::memcpy (&firstDoubleword, &secondByte, sizeof ( u64 ) );
113+ firstDoubleword = Common::bit_cast< u64 , T>(secondByte );
118114 firstDoubleword = Common::bSwap64 (firstDoubleword);
119- std::memcpy (&secondByte, &firstDoubleword, sizeof ( u64 ) );
115+ secondByte = Common::bit_cast<T, u64 >(firstDoubleword );
120116 }
121- break ;
122- }
123- default :
124- assert (0 && " Unexpected type size" );
125- break ;
126117 }
127118
128119 if constexpr (std::is_floating_point<T>::value)
0 commit comments