1/*
2 * byteswap_portable.hpp
3 *
4 * Created on: Feb 20, 2017
5 * Author: i-bird
6 */
7
8#ifndef OPENFPM_IO_SRC_VTKWRITER_BYTESWAP_PORTABLE_HPP_
9#define OPENFPM_IO_SRC_VTKWRITER_BYTESWAP_PORTABLE_HPP_
10
11#include <climits>
12
13/*! \brief This function swap byte from little endian to big endian format
14 *
15 * \warning in the case of big-endian machine this function should do nothing.
16 * Unfortunately this is not the case because I never had the bad luck
17 * of getting one
18 *
19 * \param T value to convert
20 *
21 */
22template <typename T>
23T swap_endian_lt(T u)
24{
25 static_assert (CHAR_BIT == 8, "CHAR_BIT != 8");
26
27 union
28 {
29 T u;
30 unsigned char u8[sizeof(T)];
31 } source, dest;
32
33 source.u = u;
34
35 for (size_t k = 0; k < sizeof(T); k++)
36 {dest.u8[k] = source.u8[sizeof(T) - k - 1];}
37
38 return dest.u;
39}
40
41#endif /* OPENFPM_IO_SRC_VTKWRITER_BYTESWAP_PORTABLE_HPP_ */
42