byteswap_portable.hpp 907 Bytes
Newer Older
Pietro Incardona's avatar
Pietro Incardona committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
 * byteswap_portable.hpp
 *
 *  Created on: Feb 20, 2017
 *      Author: i-bird
 */

#ifndef OPENFPM_IO_SRC_VTKWRITER_BYTESWAP_PORTABLE_HPP_
#define OPENFPM_IO_SRC_VTKWRITER_BYTESWAP_PORTABLE_HPP_

#include <climits>

/*! \brief This function swap byte from little endian to big endian format
 *
 * \warning in the case of big-endian machine this function should do nothing.
 *          Unfortunately this is not the case because I never had the bad luck
 *          of getting one
 *
 * \param T value to convert
 *
 */
template <typename T>
T swap_endian_lt(T u)
{
    static_assert (CHAR_BIT == 8, "CHAR_BIT != 8");

    union
    {
        T u;
        unsigned char u8[sizeof(T)];
    } source, dest;

    source.u = u;

    for (size_t k = 0; k < sizeof(T); k++)
        dest.u8[k] = source.u8[sizeof(T) - k - 1];

    return dest.u;
}

#endif /* OPENFPM_IO_SRC_VTKWRITER_BYTESWAP_PORTABLE_HPP_ */