From 56a8801575fc5fd7955ef9b4569880fa8e837726 Mon Sep 17 00:00:00 2001 From: Link Mauve Date: Mon, 23 Jun 2025 23:50:50 +0200 Subject: [PATCH] AsmJit: Remove unused/standard features The casts through union were undefined behaviour (memcpy() should be used instead), but they were unused so nothing bad happened. _min() and _max() could be replaced with std::min() and std::max(), whereas maxValue() could be replaced with std::numeric_limits::max(). Once we migrate to C++20, bitCount() could also be replaced with std::popcount() from . --- desmume/src/utils/AsmJit/core/intutil.h | 85 ------------------- .../src/utils/AsmJit/core/stringbuilder.cpp | 13 +-- desmume/src/utils/AsmJit/x86/x86assembler.cpp | 13 +-- 3 files changed, 16 insertions(+), 95 deletions(-) diff --git a/desmume/src/utils/AsmJit/core/intutil.h b/desmume/src/utils/AsmJit/core/intutil.h index 0125db745..72667e8d8 100644 --- a/desmume/src/utils/AsmJit/core/intutil.h +++ b/desmume/src/utils/AsmJit/core/intutil.h @@ -19,61 +19,12 @@ namespace AsmJit { //! @addtogroup AsmJit_Core //! @{ -// ============================================================================ -// [AsmJit::I32FPUnion] -// ============================================================================ - -//! @internal -//! -//! @brief used to cast from float to 32-bit integer and vica versa. -union I32FPUnion -{ - //! @brief 32-bit signed integer value. - int32_t i; - //! @brief 32-bit SP-FP value. - float f; -}; - -// ============================================================================ -// [AsmJit::I64FPUnion] -// ============================================================================ - -//! @internal -//! -//! @brief used to cast from double to 64-bit integer and vica versa. -union I64FPUnion -{ - //! @brief 64-bit signed integer value. - int64_t i; - //! @brief 64-bit DP-FP value. - double f; -}; - // ============================================================================ // [AsmJit::IntUtil] // ============================================================================ namespace IntUtil { - // -------------------------------------------------------------------------- - // [Min/Max] - // -------------------------------------------------------------------------- - - // NOTE: Because some environments declare min() and max() as macros, we - // decided to use different name so we never collide. - - template - static inline T _min(const T& a, const T& b) { return a < b ? a : b; } - template - static inline T _max(const T& a, const T& b) { return a > b ? a : b; } - - // -------------------------------------------------------------------------- - // [Limits] - // -------------------------------------------------------------------------- - - template - static inline T maxValue() { return ~T(0); } - // -------------------------------------------------------------------------- // [IsInt / IsUInt] // -------------------------------------------------------------------------- @@ -213,42 +164,6 @@ namespace IntUtil return base + 1; } - - // -------------------------------------------------------------------------- - // [Cast] - // -------------------------------------------------------------------------- - - //! @brief Binary cast from 32-bit integer to SP-FP value (@c float). - static inline float int32AsFloat(int32_t i) - { - I32FPUnion u; - u.i = i; - return u.f; - } - - //! @brief Binary cast SP-FP value (@c float) to 32-bit integer. - static inline int32_t floatAsInt32(float f) - { - I32FPUnion u; - u.f = f; - return u.i; - } - - //! @brief Binary cast from 64-bit integer to DP-FP value (@c double). - static inline double int64AsDouble(int64_t i) - { - I64FPUnion u; - u.i = i; - return u.f; - } - - //! @brief Binary cast from DP-FP value (@c double) to 64-bit integer. - static inline int64_t doubleAsInt64(double f) - { - I64FPUnion u; - u.f = f; - return u.i; - } }; //! @} diff --git a/desmume/src/utils/AsmJit/core/stringbuilder.cpp b/desmume/src/utils/AsmJit/core/stringbuilder.cpp index fc6b6dd56..588fd2ea2 100644 --- a/desmume/src/utils/AsmJit/core/stringbuilder.cpp +++ b/desmume/src/utils/AsmJit/core/stringbuilder.cpp @@ -11,6 +11,9 @@ #include "../core/intutil.h" #include "../core/stringbuilder.h" +// [Dependencies - C++] +#include + // [Dependencies - C] #include @@ -65,7 +68,7 @@ char* StringBuilder::prepare(uint32_t op, size_t len) if (_capacity < len) { - if (len >= IntUtil::maxValue() - sizeof(uintptr_t) * 2) + if (len >= std::numeric_limits::max() - sizeof(uintptr_t) * 2) return NULL; size_t to = IntUtil::align(len, sizeof(uintptr_t)); @@ -106,7 +109,7 @@ char* StringBuilder::prepare(uint32_t op, size_t len) return _data + _length; // Overflow. - if (IntUtil::maxValue() - sizeof(uintptr_t) * 2 - _length < len) + if (std::numeric_limits::max() - sizeof(uintptr_t) * 2 - _length < len) return NULL; size_t after = _length + len; @@ -125,7 +128,7 @@ char* StringBuilder::prepare(uint32_t op, size_t len) if (to < after) { to = after; - if (to < (IntUtil::maxValue() - 1024 * 32)) + if (to < (std::numeric_limits::max() - 1024 * 32)) to = IntUtil::align(to, 1024 * 32); } @@ -159,7 +162,7 @@ bool StringBuilder::reserve(size_t to) if (_capacity >= to) return true; - if (to >= IntUtil::maxValue() - sizeof(uintptr_t) * 2) + if (to >= std::numeric_limits::max() - sizeof(uintptr_t) * 2) return false; to = IntUtil::align(to, sizeof(uintptr_t)); @@ -316,7 +319,7 @@ bool StringBuilder::_opNumber(uint32_t op, uint64_t i, uint32_t base, size_t wid bool StringBuilder::_opHex(uint32_t op, const void* data, size_t len) { - if (len >= IntUtil::maxValue() / 2) + if (len >= std::numeric_limits::max() / 2) return false; char* dst = prepare(op, len); diff --git a/desmume/src/utils/AsmJit/x86/x86assembler.cpp b/desmume/src/utils/AsmJit/x86/x86assembler.cpp index 842f6ef0f..9f863db53 100644 --- a/desmume/src/utils/AsmJit/x86/x86assembler.cpp +++ b/desmume/src/utils/AsmJit/x86/x86assembler.cpp @@ -23,6 +23,9 @@ #include "../x86/x86operand.h" #include "../x86/x86util.h" +// [Dependencies - C++] +#include + // [Api-Begin] #include "../core/apibegin.h" @@ -1009,14 +1012,14 @@ void X86Assembler::_emitInstruction(uint32_t code, const Operand* o0, const Oper _emitByte(0x48); // REX.W. _emitByte((opReg << 3) | (0x04 + (o0->getSize() != 1))); - _FINISHED_IMMEDIATE(o1, IntUtil::_min(o0->getSize(), 4)); + _FINISHED_IMMEDIATE(o1, std::min(o0->getSize(), 4)); } } if (o0->isRegMem() && o1->isImm()) { const Imm& imm = reinterpret_cast(*o1); - immSize = IntUtil::isInt8(imm.getValue()) ? 1 : IntUtil::_min(o0->getSize(), 4); + immSize = IntUtil::isInt8(imm.getValue()) ? 1 : std::min(o0->getSize(), 4); _emitX86RM(id->_opCode[1] + (o0->getSize() != 1 ? (immSize != 1 ? 1 : 3) : 0), o0->getSize() == 2, @@ -1593,7 +1596,7 @@ _Emit_Mov_Sreg_RM: // Mem <- Imm case (kOperandMem << 4) | kOperandImm: { - immSize = IntUtil::_min(dst.getSize(), 4); + immSize = std::min(dst.getSize(), 4); _emitX86RM(0xC6 + (dst.getSize() != 1), dst.getSize() == 2, @@ -1944,7 +1947,7 @@ _Emit_Mov_Sreg_RM: // Alternate Form - AL, AX, EAX, RAX. if (o0->isRegIndex(0) && o1->isImm()) { - immSize = IntUtil::_min(o0->getSize(), 4); + immSize = std::min(o0->getSize(), 4); if (o0->getSize() == 2) _emitByte(0x66); // 16-bit. #if defined(ASMJIT_X64) @@ -1956,7 +1959,7 @@ _Emit_Mov_Sreg_RM: if (o0->isRegMem() && o1->isImm()) { - immSize = IntUtil::_min(o0->getSize(), 4); + immSize = std::min(o0->getSize(), 4); if (o0->getSize() == 2) _emitByte(0x66); // 16-bit. _emitSegmentPrefix(reinterpret_cast(*o0)); // Segment prefix.