summaryrefslogtreecommitdiffstats
path: root/yql/essentials/utils/swap_bytes.h
blob: 90f12cab341a8e0c45bbd87bd899a9b80ae60b6b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#pragma once
#include <util/system/compiler.h>
#include <util/system/defaults.h>

namespace NYql {

// clang generates bswap for ui32 and ui64
template <typename TUnsigned>
Y_FORCE_INLINE TUnsigned SwapBytes(TUnsigned value) {
    TUnsigned result;
    auto* from = (ui8*)&value + sizeof(TUnsigned) - 1;
    auto* to = (ui8*)&result;
    for (size_t i = 0; i < sizeof(TUnsigned); ++i) {
        *to++ = *from--;
    }
    return result;
}

} // namespace NYql