Fix makefile conditions
[maemo-rb.git] / apps / codecs / libgme / blargg_endian.h
blobdce5cb20481a3e7d0e37cb5e6534e6403d09e1d2
1 // CPU Byte Order Utilities
3 // Game_Music_Emu 0.5.2
4 #ifndef BLARGG_ENDIAN
5 #define BLARGG_ENDIAN
7 #include "blargg_common.h"
9 // BLARGG_CPU_CISC: Defined if CPU has very few general-purpose registers (< 16)
10 #if defined (_M_IX86) || defined (_M_IA64) || defined (__i486__) || \
11 defined (__x86_64__) || defined (__ia64__) || defined (__i386__)
12 #define BLARGG_CPU_X86 1
13 #define BLARGG_CPU_CISC 1
14 #endif
16 #if defined (__powerpc__) || defined (__ppc__) || defined (__POWERPC__) || defined (__powerc)
17 #define BLARGG_CPU_POWERPC 1
18 #endif
20 // BLARGG_BIG_ENDIAN, BLARGG_LITTLE_ENDIAN: Determined automatically, otherwise only
21 // one may be #defined to 1. Only needed if something actually depends on byte order.
22 #if !defined (BLARGG_BIG_ENDIAN) && !defined (BLARGG_LITTLE_ENDIAN)
23 #ifdef __GLIBC__
24 // GCC handles this for us
25 #include <endian.h>
26 #if __BYTE_ORDER == __LITTLE_ENDIAN
27 #define BLARGG_LITTLE_ENDIAN 1
28 #elif __BYTE_ORDER == __BIG_ENDIAN
29 #define BLARGG_BIG_ENDIAN 1
30 #endif
31 #else
33 #if defined (LSB_FIRST) || defined (__LITTLE_ENDIAN__) || defined (BLARGG_CPU_X86) || \
34 (defined (LITTLE_ENDIAN) && LITTLE_ENDIAN+0 != 1234)
35 #define BLARGG_LITTLE_ENDIAN 1
36 #endif
38 #if defined (MSB_FIRST) || defined (__BIG_ENDIAN__) || defined (WORDS_BIGENDIAN) || \
39 defined (__mips__) || defined (__sparc__) || defined (BLARGG_CPU_POWERPC) || \
40 (defined (BIG_ENDIAN) && BIG_ENDIAN+0 != 4321)
41 #define BLARGG_BIG_ENDIAN 1
42 #else
43 // No endian specified; assume little-endian, since it's most common
44 #define BLARGG_LITTLE_ENDIAN 1
45 #endif
46 #endif
47 #endif
49 #if defined (BLARGG_LITTLE_ENDIAN) && defined(BLARGG_BIG_ENDIAN)
50 #undef BLARGG_LITTLE_ENDIAN
51 #undef BLARGG_BIG_ENDIAN
52 #endif
54 static inline void blargg_verify_byte_order( void )
56 #ifndef NDEBUG
57 #if BLARGG_BIG_ENDIAN
58 volatile int i = 1;
59 assert( *(volatile char*) &i == 0 );
60 #elif BLARGG_LITTLE_ENDIAN
61 volatile int i = 1;
62 assert( *(volatile char*) &i != 0 );
63 #endif
64 #endif
67 static inline unsigned get_le16( void const* p )
69 return (unsigned) ((unsigned char const*) p) [1] << 8 |
70 (unsigned) ((unsigned char const*) p) [0];
73 static inline unsigned get_be16( void const* p )
75 return (unsigned) ((unsigned char const*) p) [0] << 8 |
76 (unsigned) ((unsigned char const*) p) [1];
79 static inline unsigned get_le32( void const* p )
81 return (unsigned) ((unsigned char const*) p) [3] << 24 |
82 (unsigned) ((unsigned char const*) p) [2] << 16 |
83 (unsigned) ((unsigned char const*) p) [1] << 8 |
84 (unsigned) ((unsigned char const*) p) [0];
87 static inline unsigned get_be32( void const* p )
89 return (unsigned) ((unsigned char const*) p) [0] << 24 |
90 (unsigned) ((unsigned char const*) p) [1] << 16 |
91 (unsigned) ((unsigned char const*) p) [2] << 8 |
92 (unsigned) ((unsigned char const*) p) [3];
95 static inline void set_le16( void* p, unsigned n )
97 ((unsigned char*) p) [1] = (unsigned char) (n >> 8);
98 ((unsigned char*) p) [0] = (unsigned char) n;
101 static inline void set_be16( void* p, unsigned n )
103 ((unsigned char*) p) [0] = (unsigned char) (n >> 8);
104 ((unsigned char*) p) [1] = (unsigned char) n;
107 static inline void set_le32( void* p, unsigned n )
109 ((unsigned char*) p) [0] = (unsigned char) n;
110 ((unsigned char*) p) [1] = (unsigned char) (n >> 8);
111 ((unsigned char*) p) [2] = (unsigned char) (n >> 16);
112 ((unsigned char*) p) [3] = (unsigned char) (n >> 24);
115 static inline void set_be32( void* p, unsigned n )
117 ((unsigned char*) p) [3] = (unsigned char) n;
118 ((unsigned char*) p) [2] = (unsigned char) (n >> 8);
119 ((unsigned char*) p) [1] = (unsigned char) (n >> 16);
120 ((unsigned char*) p) [0] = (unsigned char) (n >> 24);
123 #if defined(BLARGG_NONPORTABLE)
124 // Optimized implementation if byte order is known
125 #if defined(BLARGG_LITTLE_ENDIAN)
126 #define GET_LE16( addr ) (*(BOOST::uint16_t*) (addr))
127 #define GET_LE32( addr ) (*(BOOST::uint32_t*) (addr))
128 #define SET_LE16( addr, data ) (void) (*(BOOST::uint16_t*) (addr) = (data))
129 #define SET_LE32( addr, data ) (void) (*(BOOST::uint32_t*) (addr) = (data))
130 #elif defined(BLARGG_BIG_ENDIAN)
131 #define GET_BE16( addr ) (*(BOOST::uint16_t*) (addr))
132 #define GET_BE32( addr ) (*(BOOST::uint32_t*) (addr))
133 #define SET_BE16( addr, data ) (void) (*(BOOST::uint16_t*) (addr) = (data))
134 #define SET_BE32( addr, data ) (void) (*(BOOST::uint32_t*) (addr) = (data))
135 #endif
137 #if defined(BLARGG_CPU_POWERPC) && defined (__MWERKS__)
138 // PowerPC has special byte-reversed instructions
139 // to do: assumes that PowerPC is running in big-endian mode
140 // to do: implement for other compilers which don't support these macros
141 #define GET_LE16( addr ) (__lhbrx( (addr), 0 ))
142 #define GET_LE32( addr ) (__lwbrx( (addr), 0 ))
143 #define SET_LE16( addr, data ) (__sthbrx( (data), (addr), 0 ))
144 #define SET_LE32( addr, data ) (__stwbrx( (data), (addr), 0 ))
145 #endif
146 #endif
148 #ifndef GET_LE16
149 #define GET_LE16( addr ) get_le16( addr )
150 #define SET_LE16( addr, data ) set_le16( addr, data )
151 #endif
153 #ifndef GET_LE32
154 #define GET_LE32( addr ) get_le32( addr )
155 #define SET_LE32( addr, data ) set_le32( addr, data )
156 #endif
158 #ifndef GET_BE16
159 #define GET_BE16( addr ) get_be16( addr )
160 #define SET_BE16( addr, data ) set_be16( addr, data )
161 #endif
163 #ifndef GET_BE32
164 #define GET_BE32( addr ) get_be32( addr )
165 #define SET_BE32( addr, data ) set_be32( addr, data )
166 #endif
168 #endif