summaryrefslogtreecommitdiff
path: root/g_src/endian.h
blob: b56dee4a1c3226d13974608c6dfb0c68ae1930dc (plain)
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
42
#ifndef ENDIAN_H
#define ENDIAN_H

inline unsigned short byteswap(unsigned short x)
{
#if defined(__ppc__) || defined(__ppc64__)
       return (x << 8 | x >> 8);
#else
       return x;
#endif
}

inline unsigned long byteswap(unsigned long x)
{
#if defined(__ppc__) || defined(__ppc64__)
       return
               ( (x << 24) & 0xFF000000) |
               ( (x <<  8) & 0x00FF0000) |
               ( (x >>  8) & 0x0000FF00) |
               ( (x >> 24) & 0x000000FF) ;
#else
       return x;
#endif
}

inline unsigned int byteswap(unsigned int x)
{
#if defined(__ppc__) || defined(__ppc64__)
       return
               ( (x << 24) & 0xFF000000) |
               ( (x <<  8) & 0x00FF0000) |
               ( (x >>  8) & 0x0000FF00) |
               ( (x >> 24) & 0x000000FF) ;
#else
       return x;
#endif
}

inline short byteswap(short x) { return byteswap( (unsigned short) x ); }
inline long byteswap(long x) { return byteswap( (unsigned long) x ); }
inline int byteswap(int x) { return byteswap( (unsigned int) x ); }
#endif