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 "assert.hpp"
9#include "compat.hpp"
10#include "system.hpp"
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 constexpr uint8_t byteswap(_In_ const uint8_t value)
48 {
49 return value;
50 }
51
52 inline uint16_t byteswap(_In_ const uint16_t value)
53 {
54#if _MSC_VER >= 1300
55 return _byteswap_ushort(value);
56#elif defined(_MSC_VER)
57 uint16_t t = (value & 0x00ff) << 8;
58 t |= (value) >> 8;
59 return t;
60#else
61 return __builtin_bswap16(value);
62#endif
63 }
64
65 inline uint32_t byteswap(_In_ const uint32_t value)
66 {
67#if _MSC_VER >= 1300
68 return _byteswap_ulong(value);
69#elif defined(_MSC_VER)
70 uint32_t t = (value & 0x000000ff) << 24;
71 t |= (value & 0x0000ff00) << 8;
72 t |= (value & 0x00ff0000) >> 8;
73 t |= (value) >> 24;
74 return t;
75#else
76 return __builtin_bswap32(value);
77#endif
78 }
79
80 inline uint64_t byteswap(_In_ const uint64_t value)
81 {
82#if _MSC_VER >= 1300
83 return _byteswap_uint64(value);
84#elif defined(_MSC_VER)
85 uint64_t t = (value & 0x00000000000000ff) << 56;
86 t |= (value & 0x000000000000ff00) << 40;
87 t |= (value & 0x0000000000ff0000) << 24;
88 t |= (value & 0x00000000ff000000) << 8;
89 t |= (value & 0x000000ff00000000) >> 8;
90 t |= (value & 0x0000ff0000000000) >> 24;
91 t |= (value & 0x00ff000000000000) >> 40;
92 t |= (value) >> 56;
93 return t;
94#else
95 return __builtin_bswap64(value);
96#endif
97 }
98
99 inline constexpr int8_t byteswap(_In_ const char value) { return static_cast<int8_t>(byteswap(static_cast<uint8_t>(value))); }
100 inline constexpr int8_t byteswap(_In_ const int8_t value) { return static_cast<int8_t>(byteswap(static_cast<uint8_t>(value))); }
101 inline int16_t byteswap(_In_ const int16_t value) { return static_cast<int16_t>(byteswap(static_cast<uint16_t>(value))); }
102 inline int32_t byteswap(_In_ const int32_t value) { return static_cast<int32_t>(byteswap(static_cast<uint32_t>(value))); }
103 inline int64_t byteswap(_In_ const int64_t value) { return static_cast<int64_t>(byteswap(static_cast<uint64_t>(value))); }
104
105 inline float byteswap(_In_ const float value)
106 {
107 uint32_t r = byteswap(*reinterpret_cast<const uint32_t*>(&value));
108 return *reinterpret_cast<float*>(&r);
109 }
110
111 inline double byteswap(_In_ const double value)
112 {
113 uint64_t r = byteswap(*reinterpret_cast<const uint64_t*>(&value));
114 return *reinterpret_cast<double*>(&r);
115 }
116
117 inline void byteswap(_Inout_ uint8_t* value) { stdex_assert(value); *value = byteswap(*value); }
118 inline void byteswap(_Inout_ uint16_t* value) { stdex_assert(value); *value = byteswap(*value); }
119 inline void byteswap(_Inout_ uint32_t* value) { stdex_assert(value); *value = byteswap(*value); }
120 inline void byteswap(_Inout_ uint64_t* value) { stdex_assert(value); *value = byteswap(*value); }
121
122 inline void byteswap(_Inout_ char* value) { byteswap(reinterpret_cast<uint8_t*>(value)); }
123 inline void byteswap(_Inout_ int8_t* value) { byteswap(reinterpret_cast<uint8_t*>(value)); }
124 inline void byteswap(_Inout_ int16_t* value) { byteswap(reinterpret_cast<uint16_t*>(value)); }
125 inline void byteswap(_Inout_ int32_t* value) { byteswap(reinterpret_cast<uint32_t*>(value)); }
126 inline void byteswap(_Inout_ int64_t* value) { byteswap(reinterpret_cast<uint64_t*>(value)); }
127 inline void byteswap(_Inout_ float* value) { byteswap(reinterpret_cast<uint32_t*>(value)); }
128 inline void byteswap(_Inout_ double* value) { byteswap(reinterpret_cast<uint64_t*>(value)); }
129}
130
131#if BYTE_ORDER == BIG_ENDIAN
132#define LE2HE(x) stdex::byteswap(x)
133#define BE2HE(x) (x)
134#define HE2LE(x) stdex::byteswap(x)
135#define HE2BE(x) (x)
136#else
137#define LE2HE(x) (x)
138#define BE2HE(x) stdex::byteswap(x)
139#define HE2LE(x) (x)
140#define HE2BE(x) stdex::byteswap(x)
141#endif