1
0
mirror of synced 2024-11-25 00:00:27 +01:00

Fixed 128 bit byte swapping

This commit is contained in:
WerWolv 2021-01-21 22:55:04 +01:00
parent b6e2bbc434
commit 0aacf1d07f

View File

@ -151,6 +151,8 @@ namespace hex {
return __builtin_bswap32(value); return __builtin_bswap32(value);
else if constexpr (sizeof(T) == 8) else if constexpr (sizeof(T) == 8)
return __builtin_bswap64(value); return __builtin_bswap64(value);
else if constexpr (sizeof(T) == 16)
return T(__builtin_bswap64(value & 0xFFFF'FFFF'FFFF'FFFF)) << 64 | __builtin_bswap64(value >> 64);
else else
static_assert(always_false<T>::value, "Invalid type provided!"); static_assert(always_false<T>::value, "Invalid type provided!");
} }
@ -168,11 +170,8 @@ namespace hex {
return __builtin_bswap32(value); return __builtin_bswap32(value);
else if (size == 8) else if (size == 8)
return __builtin_bswap64(value); return __builtin_bswap64(value);
else if (size == 16) { else if (size == 16)
u64 parts[2]; return u128(__builtin_bswap64(u128(value) & 0xFFFF'FFFF'FFFF'FFFF)) << 64 | __builtin_bswap64(u128(value) >> 64);
std::memcpy(parts, &value, size);
return u128(parts[1]) << 64 | u128(parts[0]);
}
else else
throw std::invalid_argument("Invalid value size!"); throw std::invalid_argument("Invalid value size!");
} }