DEADSOFTWARE

f4708b2b0cf5dc67671bf97db231067ca2a4391c
[flatwaifu.git] / src / common / endianness.h
1 #ifndef COMMON_ENDIANNESS_H_INCLUDED
2 #define COMMON_ENDIANNESS_H_INCLUDED
4 #include <stdint.h>
6 #define SWAP_VAR(a, b) do { unsigned char t = a; a = b; b = t; } while(0)
8 static inline int16_t short2swap (int16_t x) {
9 union {
10 uint8_t a[2];
11 int16_t x;
12 } y;
13 y.x = x;
14 SWAP_VAR(y.a[0], y.a[1]);
15 return y.x;
16 }
18 static inline int32_t int2swap (int32_t x) {
19 union {
20 uint8_t a[4];
21 int32_t x;
22 } y;
23 y.x = x;
24 SWAP_VAR(y.a[0], y.a[3]);
25 SWAP_VAR(y.a[1], y.a[2]);
26 return y.x;
27 }
29 #undef SWAP_VAR
31 static inline int16_t short2host (int16_t x) {
32 #if __BIG_ENDIAN__
33 return short2swap(x);
34 #else
35 return x;
36 #endif
37 }
39 static inline int32_t int2host (int32_t x) {
40 #if __BIG_ENDIAN__
41 return int2swap(x);
42 #else
43 return x;
44 #endif
45 }
47 #endif /* COMMON_ENDIANNESS_H_INCLUDED */