intnslib 0.1
A library to hold common functionality used across multiple projects.
Loading...
Searching...
No Matches
IoTypes.hpp
1#ifndef INTNS_IO_IOTYPES_HPP
2#define INTNS_IO_IOTYPES_HPP
3
4#include <cstdint>
5
6#if defined(_MSC_VER)
7#include <intrin.h>
8#endif
9
10namespace intns::io {
11
12enum class Endianness : uint8_t {
13 kLittle = 0, // Little-endian byte order
14 kBig // Big-endian byte order
15};
16
17inline uint16_t bswap_16(uint16_t x) noexcept {
18#if defined(__GNUC__) || defined(__clang__)
19 return __builtin_bswap16(x);
20#elif defined(_MSC_VER)
21 return _byteswap_ushort(x);
22#else
23 return (x << 8) | (x >> 8);
24#endif
25}
26
27inline uint32_t bswap_32(uint32_t x) noexcept {
28#if defined(__GNUC__) || defined(__clang__)
29 return __builtin_bswap32(x);
30#elif defined(_MSC_VER)
31 return _byteswap_ulong(x);
32#else
33 return ((x << 24) & 0xFF000000U) | ((x << 8) & 0x00FF0000U) |
34 ((x >> 8) & 0x0000FF00U) | ((x >> 24) & 0x000000FFU);
35#endif
36}
37
38inline uint64_t bswap_64(uint64_t x) noexcept {
39#if defined(__GNUC__) || defined(__clang__)
40 return __builtin_bswap64(x);
41#elif defined(_MSC_VER)
42 return _byteswap_uint64(x);
43#else
44 return ((x << 56) & 0xFF00000000000000ULL) |
45 ((x << 40) & 0x00FF000000000000ULL) |
46 ((x << 24) & 0x0000FF0000000000ULL) |
47 ((x << 8) & 0x000000FF00000000ULL) |
48 ((x >> 8) & 0x00000000FF000000ULL) |
49 ((x >> 24) & 0x0000000000FF0000ULL) |
50 ((x >> 40) & 0x000000000000FF00ULL) |
51 ((x >> 56) & 0x00000000000000FFULL);
52#endif
53}
54
55} // namespace intns::io
56
57#endif