stdex
Additional custom or not Standard C++ covered algorithms
Loading...
Searching...
No Matches
endian.hpp
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 2023 Amebis
4*/
5
6#pragma once
7
8#include "compat.hpp"
9#include "system.hpp"
10#include <assert.h>
11#include <stdint.h>
12
13#ifndef LITTLE_ENDIAN
14#define LITTLE_ENDIAN 1234
15#endif
16#ifndef BIG_ENDIAN
17#define BIG_ENDIAN 4321
18#endif
19#ifndef BYTE_ORDER
20#if defined(_WIN32)
21#if REG_DWORD == REG_DWORD_LITTLE_ENDIAN
22#define BYTE_ORDER LITTLE_ENDIAN
23#elif REG_DWORD == REG_DWORD_BIG_ENDIAN
24#define BYTE_ORDER BIG_ENDIAN
25#endif
26#elif defined(__APPLE__)
27#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
28#define BYTE_ORDER LITTLE_ENDIAN
29#elif __BYTE_ORDER == __ORDER_BIG_ENDIAN__
30#define BYTE_ORDER BIG_ENDIAN
31#endif
32#else
33#include <endian.h>
34#if __BYTE_ORDER == __LITTLE_ENDIAN
35#define BYTE_ORDER LITTLE_ENDIAN
36#elif __BYTE_ORDER == __BIG_ENDIAN
37#define BYTE_ORDER BIG_ENDIAN
38#endif
39#endif
40#ifndef BYTE_ORDER
41#error Unknown endian
42#endif
43#endif
44
45namespace stdex
46{
47 inline uint16_t byteswap(_In_ const uint16_t value)
48 {
49#if _MSC_VER >= 1300
50 return _byteswap_ushort(value);
51#elif defined(_MSC_VER)
52 uint16_t t = (value & 0x00ff) << 8;
53 t |= (value) >> 8;
54 return t;
55#else
56 return __builtin_bswap16(value);
57#endif
58 }
59
60 inline uint32_t byteswap(_In_ const uint32_t value)
61 {
62#if _MSC_VER >= 1300
63 return _byteswap_ulong(value);
64#elif defined(_MSC_VER)
65 uint32_t t = (value & 0x000000ff) << 24;
66 t |= (value & 0x0000ff00) << 8;
67 t |= (value & 0x00ff0000) >> 8;
68 t |= (value) >> 24;
69 return t;
70#else
71 return __builtin_bswap32(value);
72#endif
73 }
74
75 inline uint64_t byteswap(_In_ const uint64_t value)
76 {
77#if _MSC_VER >= 1300
78 return _byteswap_uint64(value);
79#elif defined(_MSC_VER)
80 uint64_t t = (value & 0x00000000000000ff) << 56;
81 t |= (value & 0x000000000000ff00) << 40;
82 t |= (value & 0x0000000000ff0000) << 24;
83 t |= (value & 0x00000000ff000000) << 8;
84 t |= (value & 0x000000ff00000000) >> 8;
85 t |= (value & 0x0000ff0000000000) >> 24;
86 t |= (value & 0x00ff000000000000) >> 40;
87 t |= (value) >> 56;
88 return t;
89#else
90 return __builtin_bswap64(value);
91#endif
92 }
93
94 inline int16_t byteswap(_In_ const int16_t value) { return byteswap(static_cast<uint16_t>(value)); }
95 inline int32_t byteswap(_In_ const int32_t value) { return byteswap(static_cast<uint32_t>(value)); }
96 inline int64_t byteswap(_In_ const int64_t value) { return byteswap(static_cast<uint64_t>(value)); }
97 inline float byteswap(_In_ const float value) { return byteswap(*reinterpret_cast<const uint32_t*>(&value)); }
98 inline double byteswap(_In_ const double value) { return byteswap(*reinterpret_cast<const uint64_t*>(&value)); }
99
100 inline void byteswap(_Inout_ uint16_t* value) { assert(value); *value = byteswap(*value); }
101 inline void byteswap(_Inout_ uint32_t* value) { assert(value); *value = byteswap(*value); }
102 inline void byteswap(_Inout_ uint64_t* value) { assert(value); *value = byteswap(*value); }
103
104 inline void byteswap(_Inout_ int16_t* value) { byteswap(reinterpret_cast<uint16_t*>(value)); }
105 inline void byteswap(_Inout_ int32_t* value) { byteswap(reinterpret_cast<uint32_t*>(value)); }
106 inline void byteswap(_Inout_ int64_t* value) { byteswap(reinterpret_cast<uint64_t*>(value)); }
107 inline void byteswap(_Inout_ float* value) { byteswap(reinterpret_cast<uint32_t*>(value)); }
108 inline void byteswap(_Inout_ double* value) { byteswap(reinterpret_cast<uint64_t*>(value)); }
109}
110
111#if BYTE_ORDER == BIG_ENDIAN
112#define LE2HE(x) stdex::byteswap(x)
113#define BE2HE(x) (x)
114#define HE2LE(x) stdex::byteswap(x)
115#define HE2BE(x) (x)
116#else
117#define LE2HE(x) (x)
118#define BE2HE(x) stdex::byteswap(x)
119#define HE2LE(x) (x)
120#define HE2BE(x) stdex::byteswap(x)
121#endif