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