M5Utility 0.0.2 git rev:5c1a751
Loading...
Searching...
No Matches
endianness.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
3 *
4 * SPDX-License-Identifier: MIT
5 */
10#ifndef M5_UTILITY_STL_ENDIANESS_HPP
11#define M5_UTILITY_STL_ENDIANESS_HPP
12
13#if __cplusplus >= 202002L
14// #pragma message "Using std::endian"
15#include <bit>
16#elif __has_include(<endian.h>)
17// #pragma message "Using endian.h"
18#include <endian.h>
19#elif __has_include(<machine/endian.h>)
20// #pragma message "Using machine/endian.h"
21#include <machine/endian.h>
22#else
23// #pragma message "Using hacked"
24#include <cstdint>
25#endif
26
27namespace m5 {
28namespace stl {
29
30// C++20 or later
31#if __cplusplus >= 202002L || DOXYGEN_PROCESS
32
33using endian = std::endian;
34
35// endian header
36#elif __has_include(<endian.h>) || __has_include(<machine/endian.h>)
37
38enum class endian {
39#if defined(__BYTE_ORDER)
40 little = __LITTLE_ENDIAN,
41 big = __BIG_ENDIAN,
42 native = __BYTE_ORDER
43#elif defined(_BYTE_ORDER)
44 little = _LITTLE_ENDIAN,
45 big = _BIG_ENDIAN,
46 native = _BYTE_ORDER
47#elif defined(BYTE_ORDER)
48 little = LITTLE_ENDIAN,
49 big = BIG_ENDIAN,
50 native = BYTE_ORDER
51#else
52 little = 0,
53 big = 0,
54 native = 0,
55#endif
56};
57
58#else
59
61constexpr uint32_t val32 = 0x11223344;
62constexpr uint8_t ref8 = static_cast<const uint8_t&>(val32);
64enum class endian { little = 0x44, big = 0x11, native = ref8 };
65#endif
66} // namespace stl
67
72namespace endian {
75constexpr bool little = m5::stl::endian::native == m5::stl::endian::little;
76constexpr bool big = m5::stl::endian::native == m5::stl::endian::big;
77constexpr bool other = !little && !big;
79
80static_assert(little || big || other, "Unable to determine endianness");
81static_assert(((int)little + (int)big + (int)other) == 1, "Endian matches more than one");
82} // namespace endian
83} // namespace m5
84
85#endif
constexpr bool little
true if little endian.
Definition endianness.hpp:75
constexpr bool other
true if other endian.
Definition endianness.hpp:77
constexpr bool big
true if big endian.
Definition endianness.hpp:76
Top level namespace of M5.
Definition bit_segment.hpp:17
STL compatibility functions and classes.