evio  6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ByteOrder.h
Go to the documentation of this file.
1 //
2 // Copyright 2020, Jefferson Science Associates, LLC.
3 // Subject to the terms in the LICENSE file found in the top-level directory.
4 //
5 // EPSCI Group
6 // Thomas Jefferson National Accelerator Facility
7 // 12000, Jefferson Ave, Newport News, VA 23606
8 // (757)-269-7100
9 
10 
11 #ifndef EVIO_6_0_BYTEORDER_H
12 #define EVIO_6_0_BYTEORDER_H
13 
14 
15 #include <string>
16 #include <iostream>
17 
18 
19 namespace evio {
20 
21 
23 #define SWAP_16(x) \
24  ((uint16_t)((((uint16_t)(x)) >> 8) | \
25  (((uint16_t)(x)) << 8)))
26 
28 #define SWAP_32(x) \
29  ((uint32_t)((((uint32_t)(x)) >> 24) | \
30  (((uint32_t)(x) & 0x00ff0000) >> 8) | \
31  (((uint32_t)(x) & 0x0000ff00) << 8) | \
32  (((uint32_t)(x)) << 24)))
33 
35 #define SWAP_64(x) \
36  ((uint64_t)((((uint64_t)(x)) >> 56) | \
37  (((uint64_t)(x) & 0x00ff000000000000ULL) >> 40) | \
38  (((uint64_t)(x) & 0x0000ff0000000000ULL) >> 24) | \
39  (((uint64_t)(x) & 0x000000ff00000000ULL) >> 8) | \
40  (((uint64_t)(x) & 0x00000000ff000000ULL) << 8) | \
41  (((uint64_t)(x) & 0x0000000000ff0000ULL) << 24) | \
42  (((uint64_t)(x) & 0x000000000000ff00ULL) << 40) | \
43  (((uint64_t)(x)) << 56)))
44 
45 
53  class ByteOrder {
54 
55  public:
56 
57  static const ByteOrder ENDIAN_LITTLE;
58  static const ByteOrder ENDIAN_BIG;
59 
60  static const ByteOrder ENDIAN_UNKNOWN;
61  static const ByteOrder ENDIAN_LOCAL;
62 
63  private:
64 
66  int value;
67 
69  std::string name;
70 
76  ByteOrder(int val, std::string name) : value(val), name(std::move(name)) {}
77 
78  public:
79 
84  std::string getName() const {return name;}
85 
90  bool isBigEndian() const {return (value == ENDIAN_BIG.value);}
91 
96  bool isLittleEndian() const {return (value == ENDIAN_LITTLE.value);}
97 
102  bool isLocalEndian() const {return (value == ENDIAN_LOCAL.value);}
103 
109  return isBigEndian() ? ENDIAN_LITTLE : ENDIAN_BIG;
110  }
111 
112 
113  bool operator==(const ByteOrder &rhs) const;
114 
115  bool operator!=(const ByteOrder &rhs) const;
116 
117 
122  static ByteOrder const & getLocalByteOrder() {
123  if (isLocalHostBigEndian()) {
124  return ENDIAN_BIG;
125  }
126  return ENDIAN_LITTLE;
127  }
128 
133  static ByteOrder const & nativeOrder() {
134  return getLocalByteOrder();
135  };
136 
141  static bool isLocalHostBigEndian() {
142  int32_t i = 1;
143  return !*((char *) &i);
144  }
145 
151  static bool needToSwap(ByteOrder & order) {
152  return !(order == getLocalByteOrder());
153  }
154 
155  // Templates to swap stuff in place
156 
162  template <typename T>
163  static void byteSwapInPlace(T& var);
164 
171  template <typename T>
172  static void byteSwapInPlace(T& var, size_t elements);
173 
181  template <typename T>
182  static void byteSwapInPlace(T* var, size_t elements);
183 
184  // Methods to swap and return floats and doubles
185  static float byteSwap(float var);
186  static double byteSwap(double var);
187 
188  // Swapping arrays
189  static uint16_t* byteSwap16(uint16_t* src, size_t elements, uint16_t* dst);
190  static uint32_t* byteSwap32(uint32_t* src, size_t elements, uint32_t* dst);
191  static uint64_t* byteSwap64(uint64_t* src, size_t elements, uint64_t* dst);
192  static void byteNoSwap32(const uint32_t* src, size_t elements, uint32_t* dst);
193 
194  };
195 
196 }
197 
198 
199 #endif //EVIO_6_0_BYTEORDER_H
static const ByteOrder ENDIAN_LITTLE
Little endian byte order.
Definition: ByteOrder.h:57
Numerical values associated with endian byte order.
Definition: ByteOrder.h:53
static uint64_t * byteSwap64(uint64_t *src, size_t elements, uint64_t *dst)
This method swaps an array of 8-byte data.
Definition: ByteOrder.cpp:132
bool isBigEndian() const
Is this big endian?
Definition: ByteOrder.h:90
static ByteOrder const & nativeOrder()
Get the byte order of the local host.
Definition: ByteOrder.h:133
bool operator==(const ByteOrder &rhs) const
Definition: ByteOrder.cpp:180
static void byteNoSwap32(const uint32_t *src, size_t elements, uint32_t *dst)
This routine swaps nothing, it just copies the given number of 32 bit ints.
Definition: ByteOrder.cpp:155
bool isLittleEndian() const
Is this little endian?
Definition: ByteOrder.h:96
bool isLocalEndian() const
Is this endian same as the local host?
Definition: ByteOrder.h:102
static float byteSwap(float var)
Convenience method to return swapped float.
Definition: ByteOrder.cpp:29
static const ByteOrder ENDIAN_UNKNOWN
Unknown endian byte order.
Definition: ByteOrder.h:60
ByteOrder getOppositeEndian() const
Get the oppposite endian (little if this is big and vice versa).
Definition: ByteOrder.h:108
static uint32_t * byteSwap32(uint32_t *src, size_t elements, uint32_t *dst)
This method swaps an array of 4-byte data.
Definition: ByteOrder.cpp:109
std::string getName() const
Get the object name.
Definition: ByteOrder.h:84
static bool isLocalHostBigEndian()
Is the local host big endian?
Definition: ByteOrder.h:141
static void byteSwapInPlace(T &var)
Templated method to swap data in place.
Definition: ByteOrder.cpp:18
static ByteOrder const & getLocalByteOrder()
Get the byte order of the local host.
Definition: ByteOrder.h:122
static uint16_t * byteSwap16(uint16_t *src, size_t elements, uint16_t *dst)
This method swaps an array of 2-byte data.
Definition: ByteOrder.cpp:86
bool operator!=(const ByteOrder &rhs) const
Definition: ByteOrder.cpp:184
static const ByteOrder ENDIAN_BIG
Big endian byte order.
Definition: ByteOrder.h:58
static bool needToSwap(ByteOrder &order)
Is the argument the opposite of the local host&#39;s endian?
Definition: ByteOrder.h:151
static const ByteOrder ENDIAN_LOCAL
Local host&#39;s byte order.
Definition: ByteOrder.h:61