Reformatted Utils.h

This commit is contained in:
calcium 2024-08-19 20:05:06 -04:00
parent 5166947bf0
commit 74f95aa25f

View File

@ -1,24 +1,32 @@
#ifndef __JAVA_PARSER_UTILS_UTILS_H #ifndef __CAVM_UTILS_UTILS_H
#define __JAVA_PARSER_UTILS_UTILS_H #define __CAVM_UTILS_UTILS_H
#include <vector> #include <Config.h>
#include <SpecTypes.h>
#include <cassert>
#include <cstdint> #include <cstdint>
#include <stdexcept> #include <stdexcept>
#include <SpecTypes.h> #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
namespace Utils namespace Utils
{ {
template <typename T, typename X> template <typename T, typename X> ALWAYS_INLINE T M_POW(T x, X power)
ALWAYS_INLINE T M_POW(T x, X power)
{ {
T ret = 1; T ret = 1;
if (power == 0) if (power == 0)
return 1; return 1;
for (int i = 0; i < power; i++) for (int i = 0; i < power; i++)
{ {
ret *= x; ret *= x;
@ -35,10 +43,9 @@ ALWAYS_INLINE T M_POW(T x, X power)
return ret; return ret;
} }
template<typename T = unsigned int> template <typename T = unsigned int> ALWAYS_INLINE T Hex2Int(T x)
ALWAYS_INLINE T Hex2Int(T x)
{ {
T ret = 0; T ret = 0;
int digits = 0; int digits = 0;
while (x != 0) while (x != 0)
@ -52,8 +59,7 @@ ALWAYS_INLINE T Hex2Int(T x)
return ret; return ret;
} }
template <typename T> template <typename T> ALWAYS_INLINE const char *Int2Hex(T x)
ALWAYS_INLINE const char* Int2Hex(T x)
{ {
constexpr const char *digits[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}; constexpr const char *digits[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};
@ -62,15 +68,15 @@ ALWAYS_INLINE const char* Int2Hex(T x)
while (x != 0) while (x != 0)
{ {
T rem = x % 16; T rem = x % 16;
x = (int)(x / 16); x = (int)(x / 16);
ret.push_back(digits[rem]); ret.push_back(digits[rem]);
} }
for (int i = 0; i < ret.size() / 2; i++) for (int i = 0; i < ret.size() / 2; i++)
{ {
char tmp = ret[i]; char tmp = ret[i];
ret[i] = ret[ret.size() - i]; ret[i] = ret[ret.size() - i];
ret[ret.size() - i] = tmp; ret[ret.size() - i] = tmp;
} }
@ -79,8 +85,7 @@ ALWAYS_INLINE const char* Int2Hex(T x)
return data; return data;
} }
template <typename T> template <typename T> ALWAYS_INLINE std::vector<T> Read(std::vector<T> &vec, int start, int end)
ALWAYS_INLINE std::vector<T> Read(std::vector<T> &vec, int start, int end)
{ {
std::vector<T> ret; std::vector<T> ret;
@ -92,8 +97,7 @@ ALWAYS_INLINE std::vector<T> Read(std::vector<T> &vec, int start, int end)
return ret; return ret;
} }
template <typename T> template <typename T> ALWAYS_INLINE std::vector<T> ReadFromStart(std::vector<T> &vec, int end)
ALWAYS_INLINE std::vector<T> ReadFromStart(std::vector<T> &vec, int end)
{ {
std::vector<T> ret = Read(vec, 0, end); std::vector<T> ret = Read(vec, 0, end);
for (int i = 0; i < end; i++) for (int i = 0; i < end; i++)
@ -101,9 +105,7 @@ ALWAYS_INLINE std::vector<T> ReadFromStart(std::vector<T> &vec, int end)
return ret; return ret;
} }
template <typename T, typename vec_type = uint8_t> ALWAYS_INLINE T vecToVal(std::vector<vec_type> &vec)
template <typename T, typename vec_type=uint8_t>
ALWAYS_INLINE T vecToVal(std::vector<vec_type> &vec)
{ {
if (vec.size() > sizeof(T)) if (vec.size() > sizeof(T))
{ {
@ -120,19 +122,17 @@ ALWAYS_INLINE T vecToVal(std::vector<vec_type> &vec)
return ret; return ret;
} }
template <typename T, typename vec_type> template <typename T, typename vec_type> ALWAYS_INLINE T ReadFromStartIntoVal(std::vector<vec_type> &vec, int end)
ALWAYS_INLINE T ReadFromStartIntoVal(std::vector<vec_type> &vec, int end)
{ {
std::vector<uint8_t> val = ReadFromStart<vec_type>(vec, end); std::vector<uint8_t> val = ReadFromStart<vec_type>(vec, end);
return vecToVal<T>(val); return vecToVal<T>(val);
} }
template <typename T, typename vec_type> template <typename T, typename vec_type> ALWAYS_INLINE T ReadFromStartIntoVal(std::vector<vec_type> &vec)
ALWAYS_INLINE T ReadFromStartIntoVal(std::vector<vec_type> &vec)
{ {
std::vector<uint8_t> val = ReadFromStart<vec_type>(vec, sizeof(T)); std::vector<uint8_t> val = ReadFromStart<vec_type>(vec, sizeof(T));
return vecToVal<T>(val); return vecToVal<T>(val);
} }
} } // namespace Utils
#endif #endif