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<T>::max().

Once we migrate to C++20, bitCount() could also be replaced with
std::popcount() from <bits>.
This commit is contained in:
Link Mauve 2025-06-23 23:50:50 +02:00
parent 38f63130e2
commit 56a8801575
3 changed files with 16 additions and 95 deletions

View File

@ -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<typename T>
static inline T _min(const T& a, const T& b) { return a < b ? a : b; }
template<typename T>
static inline T _max(const T& a, const T& b) { return a > b ? a : b; }
// --------------------------------------------------------------------------
// [Limits]
// --------------------------------------------------------------------------
template<typename T>
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;
}
};
//! @}

View File

@ -11,6 +11,9 @@
#include "../core/intutil.h"
#include "../core/stringbuilder.h"
// [Dependencies - C++]
#include <limits>
// [Dependencies - C]
#include <stdarg.h>
@ -65,7 +68,7 @@ char* StringBuilder::prepare(uint32_t op, size_t len)
if (_capacity < len)
{
if (len >= IntUtil::maxValue<size_t>() - sizeof(uintptr_t) * 2)
if (len >= std::numeric_limits<size_t>::max() - sizeof(uintptr_t) * 2)
return NULL;
size_t to = IntUtil::align<size_t>(len, sizeof(uintptr_t));
@ -106,7 +109,7 @@ char* StringBuilder::prepare(uint32_t op, size_t len)
return _data + _length;
// Overflow.
if (IntUtil::maxValue<size_t>() - sizeof(uintptr_t) * 2 - _length < len)
if (std::numeric_limits<size_t>::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<size_t>() - 1024 * 32))
if (to < (std::numeric_limits<size_t>::max() - 1024 * 32))
to = IntUtil::align<size_t>(to, 1024 * 32);
}
@ -159,7 +162,7 @@ bool StringBuilder::reserve(size_t to)
if (_capacity >= to)
return true;
if (to >= IntUtil::maxValue<size_t>() - sizeof(uintptr_t) * 2)
if (to >= std::numeric_limits<size_t>::max() - sizeof(uintptr_t) * 2)
return false;
to = IntUtil::align<size_t>(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<size_t>() / 2)
if (len >= std::numeric_limits<size_t>::max() / 2)
return false;
char* dst = prepare(op, len);

View File

@ -23,6 +23,9 @@
#include "../x86/x86operand.h"
#include "../x86/x86util.h"
// [Dependencies - C++]
#include <algorithm>
// [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<uint32_t>(o0->getSize(), 4));
_FINISHED_IMMEDIATE(o1, std::min<uint32_t>(o0->getSize(), 4));
}
}
if (o0->isRegMem() && o1->isImm())
{
const Imm& imm = reinterpret_cast<const Imm&>(*o1);
immSize = IntUtil::isInt8(imm.getValue()) ? 1 : IntUtil::_min<uint32_t>(o0->getSize(), 4);
immSize = IntUtil::isInt8(imm.getValue()) ? 1 : std::min<uint32_t>(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<uint32_t>(dst.getSize(), 4);
immSize = std::min<uint32_t>(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<uint32_t>(o0->getSize(), 4);
immSize = std::min<uint32_t>(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<uint32_t>(o0->getSize(), 4);
immSize = std::min<uint32_t>(o0->getSize(), 4);
if (o0->getSize() == 2) _emitByte(0x66); // 16-bit.
_emitSegmentPrefix(reinterpret_cast<const Operand&>(*o0)); // Segment prefix.