int128_ut_helpers.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include "int128_ut_helpers.h"
  2. namespace NInt128Private {
  3. #if defined(_little_endian_)
  4. std::array<ui8, 16> GetAsArray(const ui128 value) {
  5. std::array<ui8, 16> result;
  6. const ui64 low = GetLow(value);
  7. const ui64 high = GetHigh(value);
  8. MemCopy(result.data(), reinterpret_cast<const ui8*>(&low), sizeof(low));
  9. MemCopy(result.data() + sizeof(low), reinterpret_cast<const ui8*>(&high), sizeof(high));
  10. return result;
  11. }
  12. std::array<ui8, 16> GetAsArray(const i128 value) {
  13. std::array<ui8, 16> result;
  14. const ui64 low = GetLow(value);
  15. const ui64 high = GetHigh(value);
  16. MemCopy(result.data(), reinterpret_cast<const ui8*>(&low), sizeof(low));
  17. MemCopy(result.data() + sizeof(low), reinterpret_cast<const ui8*>(&high), sizeof(high));
  18. return result;
  19. }
  20. #elif defined(_big_endian_)
  21. std::array<ui8, 16> GetAsArray(const i128 value) {
  22. std::array<ui8, 16> result;
  23. const ui64 low = GetLow(value);
  24. const ui64 high = GetHigh(value);
  25. MemCopy(result.data(), reinterpret_cast<const ui8*>(&high), sizeof(high));
  26. MemCopy(result.data() + sizeof(high), reinterpret_cast<const ui8*>(&low), sizeof(low));
  27. return result;
  28. }
  29. std::array<ui8, 16> GetAsArray(const ui128 value) {
  30. std::array<ui8, 16> result;
  31. const ui64 low = GetLow(value);
  32. const ui64 high = GetHigh(value);
  33. MemCopy(result.data(), reinterpret_cast<const ui8*>(&high), sizeof(high));
  34. MemCopy(result.data() + sizeof(high), reinterpret_cast<const ui8*>(&low), sizeof(low));
  35. return result;
  36. }
  37. #endif
  38. #if defined(Y_HAVE_INT128)
  39. std::array<ui8, 16> GetAsArray(const unsigned __int128 value) {
  40. std::array<ui8, 16> result;
  41. MemCopy(result.data(), reinterpret_cast<const ui8*>(&value), sizeof(value));
  42. return result;
  43. }
  44. std::array<ui8, 16> GetAsArray(const signed __int128 value) {
  45. std::array<ui8, 16> result;
  46. MemCopy(result.data(), reinterpret_cast<const ui8*>(&value), sizeof(value));
  47. return result;
  48. }
  49. #endif
  50. }