types.hpp: implement smin, smax, amin, amax

Rewritten the following global utility constants:
`umax` returns max number, restricted to unsigned.
`smax` returns max signed number, restricted to integrals.
`smin` returns min signed number, restricted to signed.
`amin` returns smin or zero, less restricted.
`amax` returns smax or umax, less restricted.

Fix operators == and <=> for synthesized rel-ops.
This commit is contained in:
Nekotekina
2021-05-22 10:35:15 +03:00
parent 613777afde
commit 160b131de3
38 changed files with 219 additions and 111 deletions
+6 -6
View File
@@ -252,7 +252,7 @@ namespace cfg
static_assert(Min < Max, "Invalid cfg::_int range");
// Prefer 32 bit type if possible
using int_type = std::conditional_t<Min >= INT32_MIN && Max <= INT32_MAX, s32, s64>;
using int_type = std::conditional_t<Min >= s32{smin} && Max <= s32{smax}, s32, s64>;
atomic_t<int_type> m_value;
@@ -314,10 +314,10 @@ namespace cfg
};
// Alias for 32 bit int
using int32 = _int<INT32_MIN, INT32_MAX>;
using int32 = _int<s32{smin}, s32{smax}>;
// Alias for 64 bit int
using int64 = _int<INT64_MIN, INT64_MAX>;
using int64 = _int<s64{smin}, s64{smax}>;
// Unsigned 32/64-bit integer entry with custom Min/Max range.
template <u64 Min, u64 Max>
@@ -326,7 +326,7 @@ namespace cfg
static_assert(Min < Max, "Invalid cfg::uint range");
// Prefer 32 bit type if possible
using int_type = std::conditional_t<Max <= UINT32_MAX, u32, u64>;
using int_type = std::conditional_t<Max <= u32{umax}, u32, u64>;
atomic_t<int_type> m_value;
@@ -388,10 +388,10 @@ namespace cfg
};
// Alias for 32 bit uint
using uint32 = uint<0, UINT32_MAX>;
using uint32 = uint<0, u32{umax}>;
// Alias for 64 bit int
using uint64 = uint<0, UINT64_MAX>;
using uint64 = uint<0, u64{umax}>;
// Simple string entry with mutex
class string : public _base
+1 -1
View File
@@ -97,7 +97,7 @@ static FILETIME from_time(s64 _time)
result.dwLowDateTime = 0;
result.dwHighDateTime = 0;
}
else if (_time > INT64_MAX / 10000000ll - 11644473600ll)
else if (_time > s64{smax} / 10000000ll - 11644473600ll)
{
result.dwLowDateTime = 0xffffffff;
result.dwHighDateTime = 0x7fffffff;
+2 -2
View File
@@ -1644,7 +1644,7 @@ static LONG exception_handler(PEXCEPTION_POINTERS pExp) noexcept
{
addr = addr0;
}
else if (const usz exec64 = (ptr - vm::g_exec_addr) / 2; exec64 <= UINT32_MAX)
else if (const usz exec64 = (ptr - vm::g_exec_addr) / 2; exec64 <= u32{umax})
{
addr = static_cast<u32>(exec64);
}
@@ -2808,7 +2808,7 @@ u64 thread_ctrl::get_affinity_mask(thread_class group)
}
}
return UINT64_MAX;
return -1;
}
void thread_ctrl::set_native_priority(int priority)
+3 -3
View File
@@ -41,7 +41,7 @@ namespace utils
class address_range
{
public:
u32 start = UINT32_MAX; // First address in range
u32 start = umax; // First address in range
u32 end = 0; // Last address
private:
@@ -174,7 +174,7 @@ namespace utils
address_range get_min_max(const address_range &other) const
{
return {
std::min(valid() ? start : UINT32_MAX, other.valid() ? other.start : UINT32_MAX),
std::min(valid() ? start : umax, other.valid() ? other.start : umax),
std::max(valid() ? end : 0, other.valid() ? other.end : 0)
};
}
@@ -234,7 +234,7 @@ namespace utils
void invalidate()
{
start = UINT32_MAX;
start = umax;
end = 0;
}
+1 -1
View File
@@ -136,7 +136,7 @@ public:
void append_title_patches(const std::string& title_id);
// Apply patch (returns the number of entries applied)
std::basic_string<u32> apply(const std::string& name, u8* dst, u32 filesz = UINT32_MAX, u32 min_addr = 0);
std::basic_string<u32> apply(const std::string& name, u8* dst, u32 filesz = -1, u32 min_addr = 0);
private:
// Database
+1 -2
View File
@@ -1,7 +1,6 @@
#pragma once
#include "util/types.hpp"
#include <climits>
#include <string>
#include <vector>
#include <algorithm>
@@ -46,7 +45,7 @@ usz cfmt_append(Dst& out, const Char* fmt, Src&& src)
const auto read_decimal = [&](uint result) -> uint
{
while (fmt[0] >= '0' && fmt[0] <= '9' && result <= (UINT_MAX / 10))
while (fmt[0] >= '0' && fmt[0] <= '9' && result <= (uint{umax} / 10))
{
result = result * 10 + (fmt[0] - '0');
fmt++, ctx.size++;
+1 -1
View File
@@ -9,7 +9,7 @@ void cond_variable::imp_wait(u32 _old, u64 _timeout) noexcept
ensure(_old);
// Wait with timeout
m_value.wait(_old, c_signal_mask, atomic_wait_timeout{_timeout > max_timeout ? UINT64_MAX : _timeout * 1000});
m_value.wait(_old, c_signal_mask, atomic_wait_timeout{_timeout > max_timeout ? umax : _timeout * 1000});
// Cleanup
m_value.atomic_op([](u32& value)
+1 -1
View File
@@ -91,5 +91,5 @@ public:
}
}
static constexpr u64 max_timeout = UINT64_MAX / 1000;
static constexpr u64 max_timeout = u64{umax} / 1000;
};
+3 -3
View File
@@ -22,20 +22,20 @@ void semaphore_base::imp_wait()
const s32 value = m_value.atomic_op([](s32& value)
{
// Use sign bit to acknowledge waiter presence
if (value && value > INT32_MIN)
if (value && value > smin)
{
value--;
if (value < 0)
{
// Remove sign bit
value -= INT32_MIN;
value -= s32{smin};
}
}
else
{
// Set sign bit
value = INT32_MIN;
value = smin;
}
return value;