Viskores  1.0
Endian.h
Go to the documentation of this file.
1 //============================================================================
2 // The contents of this file are covered by the Viskores license. See
3 // LICENSE.txt for details.
4 //
5 // By contributing to this file, all contributors agree to the Developer
6 // Certificate of Origin Version 1.1 (DCO 1.1) as stated in DCO.txt.
7 //============================================================================
8 
9 //============================================================================
10 // Copyright (c) Kitware, Inc.
11 // All rights reserved.
12 // See LICENSE.txt for details.
13 //
14 // This software is distributed WITHOUT ANY WARRANTY; without even
15 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 // PURPOSE. See the above copyright notice for more information.
17 //============================================================================
18 #ifndef viskores_io_internal_Endian_h
19 #define viskores_io_internal_Endian_h
20 
21 #include <viskores/Types.h>
22 
23 #include <algorithm>
24 #include <vector>
25 
26 namespace viskores
27 {
28 namespace io
29 {
30 namespace internal
31 {
32 
33 inline bool IsLittleEndian()
34 {
35  static constexpr viskores::Int16 i16 = 0x1;
36  const viskores::Int8* i8p = reinterpret_cast<const viskores::Int8*>(&i16);
37  return (*i8p == 1);
38 }
39 
40 template <typename T>
41 inline void FlipEndianness(std::vector<T>& buffer)
42 {
43  viskores::UInt8* bytes = reinterpret_cast<viskores::UInt8*>(&buffer[0]);
44  const std::size_t tsize = sizeof(T);
45  const std::size_t bsize = buffer.size();
46  for (std::size_t i = 0; i < bsize; i++, bytes += tsize)
47  {
48  std::reverse(bytes, bytes + tsize);
49  }
50 }
51 
52 template <typename T, viskores::IdComponent N>
53 inline void FlipEndianness(std::vector<viskores::Vec<T, N>>& buffer)
54 {
55  viskores::UInt8* bytes = reinterpret_cast<viskores::UInt8*>(&buffer[0]);
56  const std::size_t tsize = sizeof(T);
57  const std::size_t bsize = buffer.size();
58  for (std::size_t i = 0; i < bsize; i++)
59  {
60  for (viskores::IdComponent j = 0; j < N; j++, bytes += tsize)
61  {
62  std::reverse(bytes, bytes + tsize);
63  }
64  }
65 }
66 }
67 }
68 } // viskores::io::internal
69 
70 #endif //viskores_io_internal_Endian_h
viskores::Int16
int16_t Int16
Base type to use for 16-bit signed integer numbers.
Definition: Types.h:181
Types.h
viskores::Int8
int8_t Int8
Base type to use for 8-bit signed integer numbers.
Definition: Types.h:173
viskores::IdComponent
viskores::Int32 IdComponent
Base type to use to index small lists.
Definition: Types.h:202
viskores
Groups connected points that have the same field value.
Definition: Atomic.h:27
viskores::UInt8
uint8_t UInt8
Base type to use for 8-bit unsigned integer numbers.
Definition: Types.h:177
viskores::Vec
A short fixed-length array.
Definition: Types.h:365