aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/utils/swap_bytes.h
blob: bf09bb321abf0fdb1844a5f237dc75b0664a0c9a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#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;
}

}