summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/libgme/blargg_endian.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/libgme/blargg_endian.h')
-rw-r--r--lib/rbcodec/codecs/libgme/blargg_endian.h168
1 files changed, 168 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libgme/blargg_endian.h b/lib/rbcodec/codecs/libgme/blargg_endian.h
new file mode 100644
index 0000000000..dce5cb2048
--- /dev/null
+++ b/lib/rbcodec/codecs/libgme/blargg_endian.h
@@ -0,0 +1,168 @@
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
54static 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
67static inline unsigned get_le16( void const* p )
68{
69 return (unsigned) ((unsigned char const*) p) [1] << 8 |
70 (unsigned) ((unsigned char const*) p) [0];
71}
72
73static inline unsigned get_be16( void const* p )
74{
75 return (unsigned) ((unsigned char const*) p) [0] << 8 |
76 (unsigned) ((unsigned char const*) p) [1];
77}
78
79static inline unsigned get_le32( void const* p )
80{
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];
85}
86
87static inline unsigned get_be32( void const* p )
88{
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];
93}
94
95static inline void set_le16( void* p, unsigned n )
96{
97 ((unsigned char*) p) [1] = (unsigned char) (n >> 8);
98 ((unsigned char*) p) [0] = (unsigned char) n;
99}
100
101static inline void set_be16( void* p, unsigned n )
102{
103 ((unsigned char*) p) [0] = (unsigned char) (n >> 8);
104 ((unsigned char*) p) [1] = (unsigned char) n;
105}
106
107static inline void set_le32( void* p, unsigned n )
108{
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);
113}
114
115static inline void set_be32( void* p, unsigned n )
116{
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);
121}
122
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
136
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
147
148#ifndef GET_LE16
149 #define GET_LE16( addr ) get_le16( addr )
150 #define SET_LE16( addr, data ) set_le16( addr, data )
151#endif
152
153#ifndef GET_LE32
154 #define GET_LE32( addr ) get_le32( addr )
155 #define SET_LE32( addr, data ) set_le32( addr, data )
156#endif
157
158#ifndef GET_BE16
159 #define GET_BE16( addr ) get_be16( addr )
160 #define SET_BE16( addr, data ) set_be16( addr, data )
161#endif
162
163#ifndef GET_BE32
164 #define GET_BE32( addr ) get_be32( addr )
165 #define SET_BE32( addr, data ) set_be32( addr, data )
166#endif
167
168#endif