swap_bytes.h 442 B

1234567891011121314151617181920
  1. #pragma once
  2. #include <util/system/compiler.h>
  3. #include <util/system/defaults.h>
  4. namespace NYql {
  5. // clang generates bswap for ui32 and ui64
  6. template <typename TUnsigned>
  7. Y_FORCE_INLINE
  8. TUnsigned SwapBytes(TUnsigned value) {
  9. TUnsigned result;
  10. auto* from = (ui8*)&value + sizeof(TUnsigned) - 1;
  11. auto* to = (ui8*)&result;
  12. for (size_t i = 0; i < sizeof(TUnsigned); ++i) {
  13. *to++ = *from--;
  14. }
  15. return result;
  16. }
  17. }