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