Move align helpers to util/asm.hpp

Also add some files:
GLTextureCache.cpp
VKTextureCache.cpp
This commit is contained in:
Nekotekina 2020-12-18 17:43:34 +03:00
parent d254a5736b
commit eec11bfba9
52 changed files with 794 additions and 713 deletions

View file

@ -292,6 +292,32 @@ namespace utils
do _mm_pause();
while (__rdtsc() - start < cycles);
}
// Align to power of 2
template <typename T, typename = std::enable_if_t<std::is_integral<T>::value && std::is_unsigned<T>::value>>
constexpr T align(T value, ullong align)
{
return static_cast<T>((value + (align - 1)) & (0 - align));
}
// General purpose aligned division, the result is rounded up not truncated
template <typename T, typename = std::enable_if_t<std::is_integral<T>::value && std::is_unsigned<T>::value>>
constexpr T aligned_div(T value, ullong align)
{
return static_cast<T>((value + align - 1) / align);
}
// General purpose aligned division, the result is rounded to nearest
template <typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
constexpr T rounded_div(T value, std::conditional_t<std::is_signed<T>::value, llong, ullong> align)
{
if constexpr (std::is_unsigned<T>::value)
{
return static_cast<T>((value + (align / 2)) / align);
}
return static_cast<T>((value + (value < 0 ? 0 - align : align) / 2) / align);
}
} // namespace utils
using utils::busy_wait;

View file

@ -15,6 +15,8 @@
#include <errno.h>
#endif
#include "util/asm.hpp"
inline std::array<u32, 4> utils::get_cpuid(u32 func, u32 subfunc)
{
int regs[4];
@ -298,7 +300,7 @@ std::string utils::get_OS_version()
static constexpr ullong round_tsc(ullong val)
{
return ::rounded_div(val, 1'000'000) * 1'000'000;
return utils::rounded_div(val, 1'000'000) * 1'000'000;
}
ullong utils::get_tsc_freq()

View file

@ -595,31 +595,6 @@ struct f16
}
};
template <typename T, typename = std::enable_if_t<std::is_integral<T>::value && std::is_unsigned<T>::value>>
constexpr T align(T value, ullong align)
{
return static_cast<T>((value + (align - 1)) & (0 - align));
}
// General purpose aligned division, the result is rounded up not truncated
template <typename T, typename = std::enable_if_t<std::is_integral<T>::value && std::is_unsigned<T>::value>>
constexpr T aligned_div(T value, ullong align)
{
return static_cast<T>((value + align - 1) / align);
}
// General purpose aligned division, the result is rounded to nearest
template <typename T, typename = std::enable_if_t<std::is_integral<T>::value>>
constexpr T rounded_div(T value, std::conditional_t<std::is_signed<T>::value, llong, ullong> align)
{
if constexpr (std::is_unsigned<T>::value)
{
return static_cast<T>((value + (align / 2)) / align);
}
return static_cast<T>((value + (value < 0 ? 0 - align : align) / 2) / align);
}
template <typename T, typename T2>
inline u32 offset32(T T2::*const mptr)
{

View file

@ -1,6 +1,7 @@
#include "stdafx.h"
#include "util/logs.hpp"
#include "util/vm.hpp"
#include "util/asm.hpp"
#ifdef _WIN32
#include "util/dyn_lib.hpp"
#include <Windows.h>
@ -209,7 +210,7 @@ namespace utils
}
shm::shm(u32 size, u32 flags)
: m_size(::align(size, 0x10000))
: m_size(utils::align(size, 0x10000))
, m_flags(flags)
, m_ptr(0)
{
@ -306,7 +307,7 @@ namespace utils
{
const u64 res64 = reinterpret_cast<u64>(::mmap(reinterpret_cast<void*>(ptr64), m_size + 0xf000, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0));
const u64 aligned = ::align(res64, 0x10000);
const u64 aligned = utils::align(res64, 0x10000);
const auto result = ::mmap(reinterpret_cast<void*>(aligned), m_size, +prot, MAP_SHARED | MAP_FIXED, m_file, 0);
// Now cleanup remnants