Skip to content

Commit

Permalink
Merge pull request #11 from jason7708/fix_to
Browse files Browse the repository at this point in the history
[mph] fix mph::to undefined behavior
  • Loading branch information
kris-jusiak authored Sep 24, 2024
2 parents 6f51c3d + f6842f2 commit 695778c
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions mph
Original file line number Diff line number Diff line change
Expand Up @@ -774,20 +774,21 @@ template<class T>
}();
}
#endif
T t;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Warray-bounds"
__builtin_memcpy(&t, data.data(), sizeof(t));
#pragma GCC diagnostic pop
T t{};
__builtin_memcpy(&t, data.data(), data.size() < sizeof(t) ? data.size() : sizeof(t));
const auto index = T(data.size() * __CHAR_BIT__);
#ifdef __BMI2__
if constexpr (sizeof(t) <= sizeof(u32)) {
return __builtin_ia32_bzhi_si(t, index);
} else if constexpr (sizeof(t) <= sizeof(u64)) {
return __builtin_ia32_bzhi_di(t, index);
} else
} else {
#endif
constexpr T size = sizeof(T) * __CHAR_BIT__;
return index >= size ? t : t & ((T(1) << index) - T(1));
#ifdef __BMI2__
}
#endif
return t & ((T(1) << index) - T(1));
}
} else { // unsafe
#pragma GCC diagnostic push
Expand Down

0 comments on commit 695778c

Please sign in to comment.