CaVM/include/Utils/Utils.h

138 lines
2.5 KiB
C
Raw Normal View History

2024-08-20 00:05:06 +00:00
#ifndef __CAVM_UTILS_UTILS_H
#define __CAVM_UTILS_UTILS_H
2024-08-13 12:12:22 +00:00
2024-08-20 00:05:06 +00:00
#include <Config.h>
#include <SpecTypes.h>
#include <cassert>
2024-08-13 12:12:22 +00:00
#include <cstdint>
#include <stdexcept>
2024-08-20 00:05:06 +00:00
#include <typeinfo>
#include <vector>
#define TYPE(x) typeid(x).name()
#define VEC_ELEM_TYPE(x) x.type().name()
#ifdef CAVM_RUNTIME_CHECK_SUPPORT
#define ASSERT(x) assert(x)
#else
#define ASSERT(x)
#endif
2024-08-13 12:12:22 +00:00
namespace Utils
{
2024-08-20 00:05:06 +00:00
template <typename T, typename X> ALWAYS_INLINE T M_POW(T x, X power)
2024-08-13 12:12:22 +00:00
{
T ret = 1;
if (power == 0)
return 1;
for (int i = 0; i < power; i++)
{
ret *= x;
}
if (power < 0)
{
for (int i = (int)power; i > 0; i++)
{
ret *= 1 / x;
}
}
return ret;
}
2024-08-20 00:05:06 +00:00
template <typename T = unsigned int> ALWAYS_INLINE T Hex2Int(T x)
2024-08-13 12:12:22 +00:00
{
2024-08-20 00:05:06 +00:00
T ret = 0;
2024-08-13 12:12:22 +00:00
int digits = 0;
while (x != 0)
{
T a = x % 10;
ret += a * M_POW(16, digits);
x = (int)(x / 10);
digits += 1;
}
return ret;
}
2024-08-20 00:05:06 +00:00
template <typename T> ALWAYS_INLINE const char *Int2Hex(T x)
2024-08-13 12:12:22 +00:00
{
constexpr const char *digits[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};
std::vector<char> ret;
while (x != 0)
{
T rem = x % 16;
2024-08-20 00:05:06 +00:00
x = (int)(x / 16);
2024-08-13 12:12:22 +00:00
ret.push_back(digits[rem]);
}
for (int i = 0; i < ret.size() / 2; i++)
{
2024-08-20 00:05:06 +00:00
char tmp = ret[i];
ret[i] = ret[ret.size() - i];
2024-08-13 12:12:22 +00:00
ret[ret.size() - i] = tmp;
}
const char *data = ret.data();
return data;
}
2024-08-20 00:05:06 +00:00
template <typename T> ALWAYS_INLINE std::vector<T> Read(std::vector<T> &vec, int start, int end)
2024-08-13 12:12:22 +00:00
{
std::vector<T> ret;
for (int i = start; i < end; i++)
{
ret.push_back(vec.at(i));
}
return ret;
}
2024-08-20 00:05:06 +00:00
template <typename T> ALWAYS_INLINE std::vector<T> ReadFromStart(std::vector<T> &vec, int end)
2024-08-13 12:12:22 +00:00
{
std::vector<T> ret = Read(vec, 0, end);
for (int i = 0; i < end; i++)
vec.erase(vec.begin());
return ret;
}
2024-08-20 00:05:06 +00:00
template <typename T, typename vec_type = uint8_t> ALWAYS_INLINE T vecToVal(std::vector<vec_type> &vec)
2024-08-13 12:12:22 +00:00
{
if (vec.size() > sizeof(T))
{
throw std::out_of_range("Provided vector larger than type to convert to.");
}
T ret = 0;
for (int i = vec.size() - 1; i >= 0; i--)
{
ret |= vec.at(i) << (i * 8);
}
return ret;
}
2024-08-20 00:05:06 +00:00
template <typename T, typename vec_type> ALWAYS_INLINE T ReadFromStartIntoVal(std::vector<vec_type> &vec, int end)
2024-08-13 12:12:22 +00:00
{
std::vector<uint8_t> val = ReadFromStart<vec_type>(vec, end);
return vecToVal<T>(val);
}
2024-08-20 00:05:06 +00:00
template <typename T, typename vec_type> ALWAYS_INLINE T ReadFromStartIntoVal(std::vector<vec_type> &vec)
2024-08-13 12:12:22 +00:00
{
std::vector<uint8_t> val = ReadFromStart<vec_type>(vec, sizeof(T));
return vecToVal<T>(val);
}
2024-08-20 00:05:06 +00:00
} // namespace Utils
2024-08-13 12:12:22 +00:00
#endif