diff options
Diffstat (limited to 'apps/codecs/libgme/blargg_endian.h')
-rw-r--r-- | apps/codecs/libgme/blargg_endian.h | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/apps/codecs/libgme/blargg_endian.h b/apps/codecs/libgme/blargg_endian.h new file mode 100644 index 0000000000..ae55d7fd3b --- /dev/null +++ b/apps/codecs/libgme/blargg_endian.h | |||
@@ -0,0 +1,147 @@ | |||
1 | // CPU Byte Order Utilities | ||
2 | |||
3 | // Game_Music_Emu 0.5.2 | ||
4 | #ifndef BLARGG_ENDIAN | ||
5 | #define BLARGG_ENDIAN | ||
6 | |||
7 | #include "blargg_common.h" | ||
8 | |||
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 | ||
15 | |||
16 | #if defined (__powerpc__) || defined (__ppc__) || defined (__POWERPC__) || defined (__powerc) | ||
17 | #define BLARGG_CPU_POWERPC 1 | ||
18 | #endif | ||
19 | |||
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 | ||
32 | |||
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 | ||
37 | |||
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 | ||
48 | |||
49 | #if defined (BLARGG_LITTLE_ENDIAN) && defined(BLARGG_BIG_ENDIAN) | ||
50 | #undef BLARGG_LITTLE_ENDIAN | ||
51 | #undef BLARGG_BIG_ENDIAN | ||
52 | #endif | ||
53 | |||
54 | static inline void blargg_verify_byte_order( void ) | ||
55 | { | ||
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 | ||
65 | } | ||
66 | |||
67 | static inline unsigned get_le16( void const* p ) { | ||
68 | return ((unsigned char const*) p) [1] * 0x100u + | ||
69 | ((unsigned char const*) p) [0]; | ||
70 | } | ||
71 | static inline unsigned get_be16( void const* p ) { | ||
72 | return ((unsigned char const*) p) [0] * 0x100u + | ||
73 | ((unsigned char const*) p) [1]; | ||
74 | } | ||
75 | static inline blargg_ulong get_le32( void const* p ) { | ||
76 | return ((unsigned char const*) p) [3] * 0x01000000u + | ||
77 | ((unsigned char const*) p) [2] * 0x00010000u + | ||
78 | ((unsigned char const*) p) [1] * 0x00000100u + | ||
79 | ((unsigned char const*) p) [0]; | ||
80 | } | ||
81 | static inline blargg_ulong get_be32( void const* p ) { | ||
82 | return ((unsigned char const*) p) [0] * 0x01000000u + | ||
83 | ((unsigned char const*) p) [1] * 0x00010000u + | ||
84 | ((unsigned char const*) p) [2] * 0x00000100u + | ||
85 | ((unsigned char const*) p) [3]; | ||
86 | } | ||
87 | static inline void set_le16( void* p, unsigned n ) { | ||
88 | ((unsigned char*) p) [1] = (unsigned char) (n >> 8); | ||
89 | ((unsigned char*) p) [0] = (unsigned char) n; | ||
90 | } | ||
91 | static inline void set_be16( void* p, unsigned n ) { | ||
92 | ((unsigned char*) p) [0] = (unsigned char) (n >> 8); | ||
93 | ((unsigned char*) p) [1] = (unsigned char) n; | ||
94 | } | ||
95 | static inline void set_le32( void* p, blargg_ulong n ) { | ||
96 | ((unsigned char*) p) [3] = (unsigned char) (n >> 24); | ||
97 | ((unsigned char*) p) [2] = (unsigned char) (n >> 16); | ||
98 | ((unsigned char*) p) [1] = (unsigned char) (n >> 8); | ||
99 | ((unsigned char*) p) [0] = (unsigned char) n; | ||
100 | } | ||
101 | static inline void set_be32( void* p, blargg_ulong n ) { | ||
102 | ((unsigned char*) p) [0] = (unsigned char) (n >> 24); | ||
103 | ((unsigned char*) p) [1] = (unsigned char) (n >> 16); | ||
104 | ((unsigned char*) p) [2] = (unsigned char) (n >> 8); | ||
105 | ((unsigned char*) p) [3] = (unsigned char) n; | ||
106 | } | ||
107 | |||
108 | #if defined(BLARGG_NONPORTABLE) | ||
109 | // Optimized implementation if byte order is known | ||
110 | #if defined(BLARGG_LITTLE_ENDIAN) | ||
111 | #define GET_LE16( addr ) (*(BOOST::uint16_t*) (addr)) | ||
112 | #define GET_LE32( addr ) (*(BOOST::uint32_t*) (addr)) | ||
113 | #define SET_LE16( addr, data ) (void) (*(BOOST::uint16_t*) (addr) = (data)) | ||
114 | #define SET_LE32( addr, data ) (void) (*(BOOST::uint32_t*) (addr) = (data)) | ||
115 | #elif defined(BLARGG_BIG_ENDIAN) | ||
116 | #define GET_BE16( addr ) (*(BOOST::uint16_t*) (addr)) | ||
117 | #define GET_BE32( addr ) (*(BOOST::uint32_t*) (addr)) | ||
118 | #define SET_BE16( addr, data ) (void) (*(BOOST::uint16_t*) (addr) = (data)) | ||
119 | #define SET_BE32( addr, data ) (void) (*(BOOST::uint32_t*) (addr) = (data)) | ||
120 | #endif | ||
121 | |||
122 | #if defined(BLARGG_CPU_POWERPC) && defined (__MWERKS__) | ||
123 | // PowerPC has special byte-reversed instructions | ||
124 | // to do: assumes that PowerPC is running in big-endian mode | ||
125 | // to do: implement for other compilers which don't support these macros | ||
126 | #define GET_LE16( addr ) (__lhbrx( (addr), 0 )) | ||
127 | #define GET_LE32( addr ) (__lwbrx( (addr), 0 )) | ||
128 | #define SET_LE16( addr, data ) (__sthbrx( (data), (addr), 0 )) | ||
129 | #define SET_LE32( addr, data ) (__stwbrx( (data), (addr), 0 )) | ||
130 | #endif | ||
131 | #endif | ||
132 | |||
133 | #ifndef GET_LE16 | ||
134 | #define GET_LE16( addr ) get_le16( addr ) | ||
135 | #define GET_LE32( addr ) get_le32( addr ) | ||
136 | #define SET_LE16( addr, data ) set_le16( addr, data ) | ||
137 | #define SET_LE32( addr, data ) set_le32( addr, data ) | ||
138 | #endif | ||
139 | |||
140 | #ifndef GET_BE16 | ||
141 | #define GET_BE16( addr ) get_be16( addr ) | ||
142 | #define GET_BE32( addr ) get_be32( addr ) | ||
143 | #define SET_BE16( addr, data ) set_be16( addr, data ) | ||
144 | #define SET_BE32( addr, data ) set_be32( addr, data ) | ||
145 | #endif | ||
146 | |||
147 | #endif | ||