aboutsummaryrefslogtreecommitdiff
path: root/src/std/misc.c
blob: f53797de972a718cbfd5374d345a2ad946a84199 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "../../include/std/misc.h"

#if defined(_MSC_VER)
u16 byteswap16(u16 value) {
    return _byteswap_ushort(value);
}

u32 byteswap32(u32 value) {
    return _byteswap_ulong(value);
}

u64 byteswap64(u64 value) {
    return _byteswap_uint64(value);
}
#elif defined(__GNUC__)
u16 byteswap16(u16 value) {
    return __builtin_bswap16(value);
}
u32 byteswap32(u32 value) {
    return __builtin_bswap32(value);
}
u64 byteswap64(u64 value) {
    return __builtin_bswap64(value);
}
#else
u16 byteswap16(u16 value) {
    u16 result = 0;
    result |= (value & 0x00FF) << 8;
    result |= (value & 0xFF00) >> 8;
    return result;
}

u32 byteswap32(u32 value) {
    u32 result = 0;
    result |= (value & 0x000000FF) << 24;
    result |= (value & 0x0000FF00) << 8;
    result |= (value & 0x00FF0000) >> 8;
    result |= (value & 0xFF000000) >> 24;
    return result;
}

u64 byteswap64(u64 value) {
    u64 result = 0;
    result |= (value & 0x00000000000000FF) << 56;
    result |= (value & 0x000000000000FF00) << 40;
    result |= (value & 0x0000000000FF0000) << 24;
    result |= (value & 0x00000000FF000000) << 8;
    result |= (value & 0x000000FF00000000) >> 8;
    result |= (value & 0x0000FF0000000000) >> 24;
    result |= (value & 0x00FF000000000000) >> 40;
    result |= (value & 0xFF00000000000000) >> 56;
    return result;
}
#endif