Misc: Replace bit_cast wrapper with std::bit_cast

This commit is contained in:
Stenzek 2023-07-22 13:19:54 +10:00 committed by Connor McLaughlin
parent d2a5cdcca7
commit 088630a999
6 changed files with 10 additions and 47 deletions

View File

@ -1,29 +0,0 @@
/* PCSX2 - PS2 Emulator for PCs
* Copyright (C) 2002-2021 PCSX2 Dev Team
*
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with PCSX2.
* If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
// An implementation of bit_cast until we get c++20
template<class T2, class T1>
T2 bit_cast(const T1& src)
{
static_assert(sizeof(T2) == sizeof(T1), "bit_cast: types must be equal size");
static_assert(std::is_pod<T1>::value, "bit_cast: source must be POD");
static_assert(std::is_pod<T2>::value, "bit_cast: destination must be POD");
T2 dst;
memcpy(&dst, &src, sizeof(T2));
return dst;
}

View File

@ -105,7 +105,6 @@
<ItemGroup> <ItemGroup>
<ClInclude Include="Align.h" /> <ClInclude Include="Align.h" />
<ClInclude Include="AlignedMalloc.h" /> <ClInclude Include="AlignedMalloc.h" />
<ClInclude Include="BitCast.h" />
<ClInclude Include="ByteSwap.h" /> <ClInclude Include="ByteSwap.h" />
<ClInclude Include="CrashHandler.h" /> <ClInclude Include="CrashHandler.h" />
<ClInclude Include="DynamicLibrary.h" /> <ClInclude Include="DynamicLibrary.h" />

View File

@ -276,9 +276,6 @@
<ClInclude Include="WindowInfo.h"> <ClInclude Include="WindowInfo.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="BitCast.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="StringUtil.h"> <ClInclude Include="StringUtil.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>

View File

@ -26,7 +26,6 @@
#include "DebugTools/Breakpoints.h" #include "DebugTools/Breakpoints.h"
#include "DebugTools/BiosDebugData.h" #include "DebugTools/BiosDebugData.h"
#include "DebugTools/MipsStackWalk.h" #include "DebugTools/MipsStackWalk.h"
#include "common/BitCast.h"
#include "QtUtils.h" #include "QtUtils.h"
#include <QtGui/QClipboard> #include <QtGui/QClipboard>
@ -539,7 +538,7 @@ static std::vector<u32> searchWorker(DebugInterface* cpu, u32 start, u32 end, T
{ {
const float fTop = value + 0.00001f; const float fTop = value + 0.00001f;
const float fBottom = value - 0.00001f; const float fBottom = value - 0.00001f;
const float memValue = bit_cast<float, u32>(cpu->read32(addr)); const float memValue = std::bit_cast<float, u32>(cpu->read32(addr));
if (fBottom < memValue && memValue < fTop) if (fBottom < memValue && memValue < fTop)
{ {
hitAddresses.emplace_back(addr); hitAddresses.emplace_back(addr);
@ -556,7 +555,7 @@ static std::vector<u32> searchWorker(DebugInterface* cpu, u32 start, u32 end, T
{ {
const double dTop = value + 0.00001f; const double dTop = value + 0.00001f;
const double dBottom = value - 0.00001f; const double dBottom = value - 0.00001f;
const double memValue = bit_cast<double, u64>(cpu->read64(addr)); const double memValue = std::bit_cast<double, u64>(cpu->read64(addr));
if (dBottom < memValue && memValue < dTop) if (dBottom < memValue && memValue < dTop)
{ {
hitAddresses.emplace_back(addr); hitAddresses.emplace_back(addr);
@ -588,7 +587,7 @@ static std::vector<u32> searchWorkerByteArray(DebugInterface* cpu, u32 start, u3
for (u32 addr = start; addr < end; addr += 1) for (u32 addr = start; addr < end; addr += 1)
{ {
bool hit = true; bool hit = true;
for (size_t i = 0; i < value.length(); i++) for (qsizetype i = 0; i < value.length(); i++)
{ {
if (cpu->read8(addr + i) != value[i]) if (cpu->read8(addr + i) != value[i])
{ {

View File

@ -17,8 +17,6 @@
#include "MemoryViewWidget.h" #include "MemoryViewWidget.h"
#include "common/BitCast.h"
#include "QtHost.h" #include "QtHost.h"
#include "QtUtils.h" #include "QtUtils.h"
#include <QtGui/QMouseEvent> #include <QtGui/QMouseEvent>

View File

@ -17,8 +17,6 @@
#include "RegisterWidget.h" #include "RegisterWidget.h"
#include "common/BitCast.h"
#include "QtUtils.h" #include "QtUtils.h"
#include <QtGui/QMouseEvent> #include <QtGui/QMouseEvent>
#include <QtWidgets/QTabBar> #include <QtWidgets/QTabBar>
@ -30,6 +28,7 @@
#include <QtWidgets/QMessageBox> #include <QtWidgets/QMessageBox>
#include <algorithm> #include <algorithm>
#include <bit>
#define CAT_SHOW_FLOAT (categoryIndex == EECAT_FPR && m_showFPRFloat) || (categoryIndex == EECAT_VU0F && m_showVU0FFloat) #define CAT_SHOW_FLOAT (categoryIndex == EECAT_FPR && m_showFPRFloat) || (categoryIndex == EECAT_VU0F && m_showVU0FFloat)
@ -151,7 +150,7 @@ void RegisterWidget::paintEvent(QPaintEvent* event)
if (categoryIndex == EECAT_VU0F && m_showVU0FFloat) if (categoryIndex == EECAT_VU0F && m_showVU0FFloat)
painter.drawText(m_fieldStartX[j], yStart, m_fieldWidth, m_rowHeight, Qt::AlignLeft, painter.drawText(m_fieldStartX[j], yStart, m_fieldWidth, m_rowHeight, Qt::AlignLeft,
painter.fontMetrics().elidedText(QString::number(bit_cast<float>(m_cpu->getRegister(categoryIndex, registerIndex)._u32[regIndex])), Qt::ElideRight, m_fieldWidth - painter.fontMetrics().averageCharWidth())); painter.fontMetrics().elidedText(QString::number(std::bit_cast<float>(m_cpu->getRegister(categoryIndex, registerIndex)._u32[regIndex])), Qt::ElideRight, m_fieldWidth - painter.fontMetrics().averageCharWidth()));
else else
painter.drawText(m_fieldStartX[j], yStart, m_fieldWidth, m_rowHeight, painter.drawText(m_fieldStartX[j], yStart, m_fieldWidth, m_rowHeight,
Qt::AlignLeft, FilledQStringFromValue(curRegister._u32[regIndex], 16)); Qt::AlignLeft, FilledQStringFromValue(curRegister._u32[regIndex], 16));
@ -168,7 +167,7 @@ void RegisterWidget::paintEvent(QPaintEvent* event)
if (categoryIndex == EECAT_FPR && m_showFPRFloat) if (categoryIndex == EECAT_FPR && m_showFPRFloat)
painter.drawText(safeValueStartX, yStart, renderSize.width(), m_rowHeight, Qt::AlignLeft, painter.drawText(safeValueStartX, yStart, renderSize.width(), m_rowHeight, Qt::AlignLeft,
QString("%1").arg(QString::number(bit_cast<float>(m_cpu->getRegister(categoryIndex, registerIndex)._u32[0]))).toUpper()); QString("%1").arg(QString::number(std::bit_cast<float>(m_cpu->getRegister(categoryIndex, registerIndex)._u32[0]))).toUpper());
else if (m_cpu->getRegisterSize(categoryIndex) == 64) else if (m_cpu->getRegisterSize(categoryIndex) == 64)
painter.drawText(safeValueStartX, yStart, renderSize.width(), m_rowHeight, Qt::AlignLeft, painter.drawText(safeValueStartX, yStart, renderSize.width(), m_rowHeight, Qt::AlignLeft,
FilledQStringFromValue(m_cpu->getRegister(categoryIndex, registerIndex).lo, 16)); FilledQStringFromValue(m_cpu->getRegister(categoryIndex, registerIndex).lo, 16));
@ -299,7 +298,7 @@ void RegisterWidget::contextCopyValue()
const int categoryIndex = ui.registerTabs->currentIndex(); const int categoryIndex = ui.registerTabs->currentIndex();
const u128 val = m_cpu->getRegister(categoryIndex, m_selectedRow); const u128 val = m_cpu->getRegister(categoryIndex, m_selectedRow);
if (CAT_SHOW_FLOAT) if (CAT_SHOW_FLOAT)
QApplication::clipboard()->setText(QString("%1").arg(QString::number(bit_cast<float>(val._u32[0])).toUpper(), 16)); QApplication::clipboard()->setText(QString("%1").arg(QString::number(std::bit_cast<float>(val._u32[0])).toUpper(), 16));
else else
QApplication::clipboard()->setText(QString("%1").arg(QString::number(val._u64[0], 16).toUpper(), 16)); QApplication::clipboard()->setText(QString("%1").arg(QString::number(val._u64[0], 16).toUpper(), 16));
} }
@ -323,7 +322,7 @@ void RegisterWidget::contextCopySegment()
const int categoryIndex = ui.registerTabs->currentIndex(); const int categoryIndex = ui.registerTabs->currentIndex();
const u128 val = m_cpu->getRegister(categoryIndex, m_selectedRow); const u128 val = m_cpu->getRegister(categoryIndex, m_selectedRow);
if (CAT_SHOW_FLOAT) if (CAT_SHOW_FLOAT)
QApplication::clipboard()->setText(FilledQStringFromValue(bit_cast<float>(val._u32[3 - m_selected128Field]), 10)); QApplication::clipboard()->setText(FilledQStringFromValue(std::bit_cast<float>(val._u32[3 - m_selected128Field]), 10));
else else
QApplication::clipboard()->setText(FilledQStringFromValue(val._u32[3 - m_selected128Field], 16)); QApplication::clipboard()->setText(FilledQStringFromValue(val._u32[3 - m_selected128Field], 16));
} }
@ -340,7 +339,7 @@ bool RegisterWidget::contextFetchNewValue(u64& out, u64 currentValue, bool segme
if (!floatingPoint) if (!floatingPoint)
existingValue = existingValue.arg(currentValue, regSize == 64 ? 16 : 8, 16, QChar('0')); existingValue = existingValue.arg(currentValue, regSize == 64 ? 16 : 8, 16, QChar('0'));
else else
existingValue = existingValue.arg(bit_cast<float>((u32)currentValue)); existingValue = existingValue.arg(std::bit_cast<float>((u32)currentValue));
//: Changing the value in a CPU register (e.g. "Change t0") //: Changing the value in a CPU register (e.g. "Change t0")
QString input = QInputDialog::getText(this, tr("Change %1").arg(m_cpu->getRegisterName(categoryIndex, m_selectedRow)), "", QString input = QInputDialog::getText(this, tr("Change %1").arg(m_cpu->getRegisterName(categoryIndex, m_selectedRow)), "",
@ -360,7 +359,7 @@ bool RegisterWidget::contextFetchNewValue(u64& out, u64 currentValue, bool segme
} }
else else
{ {
out = bit_cast<u32>(input.toFloat(&ok)); out = std::bit_cast<u32>(input.toFloat(&ok));
if (!ok) if (!ok)
{ {
QMessageBox::warning(this, tr("Invalid register value"), tr("Invalid floating-point register value.")); QMessageBox::warning(this, tr("Invalid register value"), tr("Invalid floating-point register value."));