VTK-m  2.0
Endian.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 #ifndef vtk_m_io_internal_Endian_h
11 #define vtk_m_io_internal_Endian_h
12 
13 #include <vtkm/Types.h>
14 
15 #include <algorithm>
16 #include <vector>
17 
18 namespace vtkm
19 {
20 namespace io
21 {
22 namespace internal
23 {
24 
25 inline bool IsLittleEndian()
26 {
27  static constexpr vtkm::Int16 i16 = 0x1;
28  const vtkm::Int8* i8p = reinterpret_cast<const vtkm::Int8*>(&i16);
29  return (*i8p == 1);
30 }
31 
32 template <typename T>
33 inline void FlipEndianness(std::vector<T>& buffer)
34 {
35  vtkm::UInt8* bytes = reinterpret_cast<vtkm::UInt8*>(&buffer[0]);
36  const std::size_t tsize = sizeof(T);
37  const std::size_t bsize = buffer.size();
38  for (std::size_t i = 0; i < bsize; i++, bytes += tsize)
39  {
40  std::reverse(bytes, bytes + tsize);
41  }
42 }
43 
44 template <typename T, vtkm::IdComponent N>
45 inline void FlipEndianness(std::vector<vtkm::Vec<T, N>>& buffer)
46 {
47  vtkm::UInt8* bytes = reinterpret_cast<vtkm::UInt8*>(&buffer[0]);
48  const std::size_t tsize = sizeof(T);
49  const std::size_t bsize = buffer.size();
50  for (std::size_t i = 0; i < bsize; i++)
51  {
52  for (vtkm::IdComponent j = 0; j < N; j++, bytes += tsize)
53  {
54  std::reverse(bytes, bytes + tsize);
55  }
56  }
57 }
58 }
59 }
60 } // vtkm::io::internal
61 
62 #endif //vtk_m_io_internal_Endian_h
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
Types.h
vtkm::IdComponent
vtkm::Int32 IdComponent
Represents a component ID (index of component in a vector).
Definition: Types.h:168
vtkm::Int16
int16_t Int16
Definition: Types.h:158
vtkm::Int8
int8_t Int8
Definition: Types.h:156
vtkm::UInt8
uint8_t UInt8
Definition: Types.h:157
vtkm::Vec< T, N >