MemoryUtils.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #pragma once
  2. #include <stdint.h>
  3. #include "xxhash.h"
  4. namespace il2cpp
  5. {
  6. namespace utils
  7. {
  8. class MemoryUtils
  9. {
  10. public:
  11. template<typename T>
  12. static int32_t MemCmpRef(T* left, T* right)
  13. {
  14. return memcmp(left, right, sizeof(T));
  15. }
  16. #if IL2CPP_TINY
  17. template<typename T>
  18. static int32_t MemHashRef(T* val)
  19. {
  20. return XXH32(val, sizeof(T), 0x8f37154b);
  21. }
  22. #endif
  23. };
  24. #define DECL_MEMCMP_NUM(typ) template<> inline int32_t MemoryUtils::MemCmpRef<typ>(typ* left, typ* right) { return (*right > *left) ? -1 : (*right < *left) ? 1 : 0; }
  25. DECL_MEMCMP_NUM(int8_t)
  26. DECL_MEMCMP_NUM(int16_t)
  27. DECL_MEMCMP_NUM(int32_t)
  28. DECL_MEMCMP_NUM(int64_t)
  29. DECL_MEMCMP_NUM(uint8_t)
  30. DECL_MEMCMP_NUM(uint16_t)
  31. DECL_MEMCMP_NUM(uint32_t)
  32. DECL_MEMCMP_NUM(uint64_t)
  33. // don't think this will give the right result for NaNs and such
  34. DECL_MEMCMP_NUM(float)
  35. DECL_MEMCMP_NUM(double)
  36. #undef DECL_MEMCMP_NUM
  37. #define DECL_MEMHASH_NUM(typ) template<> inline int32_t MemoryUtils::MemHashRef(typ* val) { return (int32_t)(*val); }
  38. DECL_MEMHASH_NUM(int8_t)
  39. DECL_MEMHASH_NUM(int16_t)
  40. DECL_MEMHASH_NUM(int32_t)
  41. DECL_MEMHASH_NUM(uint8_t)
  42. DECL_MEMHASH_NUM(uint16_t)
  43. DECL_MEMHASH_NUM(uint32_t)
  44. DECL_MEMHASH_NUM(float)
  45. #undef DECL_MEMHASH_NUM
  46. template<> inline int32_t MemoryUtils::MemHashRef(int64_t* val) { int64_t k = *val; return (int32_t)(k & 0xffffffff) ^ (int32_t)((k >> 32) & 0xffffffff); }
  47. template<> inline int32_t MemoryUtils::MemHashRef(uint64_t* val) { return MemHashRef(reinterpret_cast<int64_t*>(val)); }
  48. template<> inline int32_t MemoryUtils::MemHashRef(double* val) { return MemHashRef(reinterpret_cast<int64_t*>(val)); }
  49. } // namespace utils
  50. } // namespace il2cpp