NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
Endian.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <array>
31#include <climits>
32
34#include "NumCpp/Core/Types.hpp"
35
36namespace nc::endian
37{
38 //============================================================================
39 // Function Description:
44 inline bool isLittleEndian() noexcept
45 {
46 union
47 {
48 uint32 i{};
49 std::array<char, 4> c;
50 } fourBytes = { 0x01020304 }; // NOLINT(cppcoreguidelines-avoid-magic-numbers)
51
52 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access)
53 return fourBytes.c[0] == 4;
54 }
55
56 //============================================================================
57 // Function Description:
63 template<typename dtype>
64 dtype byteSwap(dtype value) noexcept
65 {
67 static_assert(CHAR_BIT == 8, "CHAR_BIT != 8"); // NOLINT(cppcoreguidelines-avoid-magic-numbers)
68
69 union
70 {
71 dtype value;
72 std::array<uint8, sizeof(dtype)> value8;
73 } source, dest;
74
75 source.value = value;
76
77 for (std::size_t k = 0; k < sizeof(dtype); ++k)
78 {
79 dest.value8[k] = source.value8[sizeof(dtype) - k - 1];
80 }
81
82 return dest.value;
83 }
84} // namespace nc::endian
#define STATIC_ASSERT_INTEGER(dtype)
Definition StaticAsserts.hpp:43
Definition Endian.hpp:37
bool isLittleEndian() noexcept
Definition Endian.hpp:44
dtype byteSwap(dtype value) noexcept
Definition Endian.hpp:64
std::uint8_t uint8
Definition Types.hpp:42
std::uint32_t uint32
Definition Types.hpp:40