Merge pull request #10957 from Pokechu22/std-bitcast

Replace Common::BitCast with std::bit_cast
This commit is contained in:
Tilka
2024-05-04 08:24:59 +01:00
committed by GitHub
36 changed files with 177 additions and 215 deletions

View File

@ -5,6 +5,7 @@
#include <algorithm>
#include <array>
#include <bit>
#include <cstring>
#include <optional>
#include <tuple>
@ -15,7 +16,6 @@
#include "Common/Align.h"
#include "Common/Assert.h"
#include "Common/BitUtils.h"
#include "Common/CommonTypes.h"
#include "Common/MathUtil.h"
#include "Common/SmallVector.h"
@ -51,12 +51,12 @@ float FPImm8ToFloat(u8 bits)
const u32 mantissa = (bits & 0xF) << 19;
const u32 f = (sign << 31) | (exp << 23) | mantissa;
return Common::BitCast<float>(f);
return std::bit_cast<float>(f);
}
std::optional<u8> FPImm8FromFloat(float value)
{
const u32 f = Common::BitCast<u32>(value);
const u32 f = std::bit_cast<u32>(value);
const u32 mantissa4 = (f & 0x7FFFFF) >> 19;
const u32 exponent = (f >> 23) & 0xFF;
const u32 sign = f >> 31;
@ -4410,7 +4410,7 @@ void ARM64FloatEmitter::MOVI2F(ARM64Reg Rd, float value, ARM64Reg scratch, bool
if (negate)
value = -value;
const u32 ival = Common::BitCast<u32>(value);
const u32 ival = std::bit_cast<u32>(value);
m_emit->MOVI2R(scratch, ival);
FMOV(Rd, scratch);
}

View File

@ -3,6 +3,7 @@
#include "Common/Assembler/GekkoIRGen.h"
#include <bit>
#include <functional>
#include <map>
#include <numeric>
@ -436,13 +437,13 @@ void GekkoIRPlugin::AddBytes(T val)
else if constexpr (std::is_same_v<T, float>)
{
static_assert(sizeof(double) == sizeof(u64));
AddBytes(BitCast<u32>(val));
AddBytes(std::bit_cast<u32>(val));
}
else
{
// std::is_same_v<T, double>
static_assert(sizeof(double) == sizeof(u64));
AddBytes(BitCast<u64>(val));
AddBytes(std::bit_cast<u64>(val));
}
}

View File

@ -125,39 +125,6 @@ constexpr bool IsValidLowMask(const T mask) noexcept
return (mask & (mask + 1)) == 0;
}
///
/// Reinterpret objects of one type as another by bit-casting between object representations.
///
/// @remark This is the example implementation of std::bit_cast which is to be included
/// in C++2a. See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0476r2.html
/// for more details. The only difference is this variant is not constexpr,
/// as the mechanism for bit_cast requires a compiler built-in to have that quality.
///
/// @param source The source object to convert to another representation.
///
/// @tparam To The type to reinterpret source as.
/// @tparam From The initial type representation of source.
///
/// @return The representation of type From as type To.
///
/// @pre Both To and From types must be the same size
/// @pre Both To and From types must satisfy the TriviallyCopyable concept.
///
template <typename To, typename From>
inline To BitCast(const From& source) noexcept
{
static_assert(sizeof(From) == sizeof(To),
"BitCast source and destination types must be equal in size.");
static_assert(std::is_trivially_copyable<From>(),
"BitCast source type must be trivially copyable.");
static_assert(std::is_trivially_copyable<To>(),
"BitCast destination type must be trivially copyable.");
alignas(To) std::byte storage[sizeof(To)];
std::memcpy(&storage, &source, sizeof(storage));
return reinterpret_cast<To&>(storage);
}
template <typename T, typename PtrType>
class BitCastPtrType
{

View File

@ -3,15 +3,14 @@
#include "Common/FloatUtils.h"
#include <bit>
#include <cmath>
#include "Common/BitUtils.h"
namespace Common
{
u32 ClassifyDouble(double dvalue)
{
const u64 ivalue = BitCast<u64>(dvalue);
const u64 ivalue = std::bit_cast<u64>(dvalue);
const u64 sign = ivalue & DOUBLE_SIGN;
const u64 exp = ivalue & DOUBLE_EXP;
@ -43,7 +42,7 @@ u32 ClassifyDouble(double dvalue)
u32 ClassifyFloat(float fvalue)
{
const u32 ivalue = BitCast<u32>(fvalue);
const u32 ivalue = std::bit_cast<u32>(fvalue);
const u32 sign = ivalue & FLOAT_SIGN;
const u32 exp = ivalue & FLOAT_EXP;
@ -86,7 +85,7 @@ const std::array<BaseAndDec, 32> frsqrte_expected = {{
double ApproximateReciprocalSquareRoot(double val)
{
s64 integral = BitCast<s64>(val);
s64 integral = std::bit_cast<s64>(val);
s64 mantissa = integral & ((1LL << 52) - 1);
const s64 sign = integral & (1ULL << 63);
s64 exponent = integral & (0x7FFLL << 52);
@ -136,7 +135,7 @@ double ApproximateReciprocalSquareRoot(double val)
const auto& entry = frsqrte_expected[i / 2048];
integral |= static_cast<s64>(entry.m_base + entry.m_dec * (i % 2048)) << 26;
return BitCast<double>(integral);
return std::bit_cast<double>(integral);
}
const std::array<BaseAndDec, 32> fres_expected = {{
@ -152,7 +151,7 @@ const std::array<BaseAndDec, 32> fres_expected = {{
// Used by fres and ps_res.
double ApproximateReciprocal(double val)
{
s64 integral = BitCast<s64>(val);
s64 integral = std::bit_cast<s64>(val);
const s64 mantissa = integral & ((1LL << 52) - 1);
const s64 sign = integral & (1ULL << 63);
s64 exponent = integral & (0x7FFLL << 52);
@ -184,7 +183,7 @@ double ApproximateReciprocal(double val)
integral = sign | exponent;
integral |= static_cast<s64>(entry.m_base - (entry.m_dec * (i % 1024) + 1) / 2) << 29;
return BitCast<double>(integral);
return std::bit_cast<double>(integral);
}
} // namespace Common

View File

@ -4,9 +4,9 @@
#pragma once
#include <array>
#include <bit>
#include <limits>
#include "Common/BitUtils.h"
#include "Common/CommonTypes.h"
namespace Common
@ -35,37 +35,37 @@ static constexpr int FLOAT_FRAC_WIDTH = 23;
inline bool IsQNAN(double d)
{
const u64 i = BitCast<u64>(d);
const u64 i = std::bit_cast<u64>(d);
return ((i & DOUBLE_EXP) == DOUBLE_EXP) && ((i & DOUBLE_QBIT) == DOUBLE_QBIT);
}
inline bool IsSNAN(double d)
{
const u64 i = BitCast<u64>(d);
const u64 i = std::bit_cast<u64>(d);
return ((i & DOUBLE_EXP) == DOUBLE_EXP) && ((i & DOUBLE_FRAC) != DOUBLE_ZERO) &&
((i & DOUBLE_QBIT) == DOUBLE_ZERO);
}
inline float FlushToZero(float f)
{
u32 i = BitCast<u32>(f);
u32 i = std::bit_cast<u32>(f);
if ((i & FLOAT_EXP) == 0)
{
// Turn into signed zero
i &= FLOAT_SIGN;
}
return BitCast<float>(i);
return std::bit_cast<float>(i);
}
inline double FlushToZero(double d)
{
u64 i = BitCast<u64>(d);
u64 i = std::bit_cast<u64>(d);
if ((i & DOUBLE_EXP) == 0)
{
// Turn into signed zero
i &= DOUBLE_SIGN;
}
return BitCast<double>(i);
return std::bit_cast<double>(i);
}
enum PPCFpClass

View File

@ -4,6 +4,7 @@
#include "Common/Network.h"
#include <algorithm>
#include <bit>
#include <string_view>
#include <vector>
@ -307,8 +308,8 @@ u16 ComputeNetworkChecksum(const void* data, u16 length, u32 initial_value)
u16 ComputeTCPNetworkChecksum(const IPAddress& from, const IPAddress& to, const void* data,
u16 length, u8 protocol)
{
const u32 source_addr = ntohl(Common::BitCast<u32>(from));
const u32 destination_addr = ntohl(Common::BitCast<u32>(to));
const u32 source_addr = ntohl(std::bit_cast<u32>(from));
const u32 destination_addr = ntohl(std::bit_cast<u32>(to));
const u32 initial_value = (source_addr >> 16) + (source_addr & 0xFFFF) +
(destination_addr >> 16) + (destination_addr & 0xFFFF) + protocol +
length;