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