Fix UB in bool packetmath test.

This commit is contained in:
Antonio Sánchez
2024-02-09 19:46:45 +00:00
committed by Rasmus Munk Larsen
parent 431e4a913b
commit 7b87b21910
3 changed files with 44 additions and 28 deletions

View File

@@ -382,12 +382,6 @@ struct packetmath_boolean_mask_ops_notcomplex_test<
}
};
// Packet16b representing bool does not support ptrue, pandnot or pcmp_eq, since
// the scalar path (for some compilers) compute the bitwise and with 0x1 of the
// results to keep the value in [0,1].
template <>
void packetmath_boolean_mask_ops<bool, internal::packet_traits<bool>::type>() {}
template <typename Scalar, typename Packet, typename EnableIf = void>
struct packetmath_minus_zero_add_test {
static void run() {}
@@ -662,11 +656,11 @@ void packetmath() {
{
for (int i = 0; i < PacketSize; ++i) {
// "if" mask
unsigned char v = internal::random<bool>() ? 0xff : 0;
char* bytes = (char*)(data1 + i);
for (int k = 0; k < int(sizeof(Scalar)); ++k) {
bytes[k] = v;
}
// Note: it's UB to load 0xFF directly into a `bool`.
uint8_t v =
internal::random<bool>() ? (std::is_same<Scalar, bool>::value ? static_cast<uint8_t>(true) : 0xff) : 0;
// Avoid strict aliasing violation by using memset.
memset(data1 + i, v, sizeof(Scalar));
// "then" packet
data1[i + PacketSize] = internal::random<Scalar>();
// "else" packet