00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 #ifndef IL_ENDIAN_H
00014 #define IL_ENDIAN_H
00015 
00016 #include "il_internal.h"
00017 
00018 #ifdef WORDS_BIGENDIAN  // This is defined by ./configure.
00019         #ifndef __BIG_ENDIAN__
00020         #define __BIG_ENDIAN__ 1
00021         #endif
00022 #endif
00023 
00024 #if (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __BIG_ENDIAN__) \
00025   || (defined(__BIG_ENDIAN__) && !defined(__LITTLE_ENDIAN__))
00026         #undef __LITTLE_ENDIAN__
00027         #define Short(s) iSwapShort(s)
00028         #define UShort(s) iSwapUShort(s)
00029         #define Int(i) iSwapInt(i)
00030         #define UInt(i) iSwapUInt(i)
00031         #define Float(f) iSwapFloat(f)
00032         #define Double(d) iSwapDouble(d)
00033  
00034         #define BigShort(s)  
00035         #define BigUShort(s)  
00036         #define BigInt(i)  
00037         #define BigUInt(i)  
00038         #define BigFloat(f)  
00039         #define BigDouble(d)  
00040 #else
00041         #undef __BIG_ENDIAN__
00042         #undef __LITTLE_ENDIAN__  // Not sure if it's defined by any compiler...
00043         #define __LITTLE_ENDIAN__
00044         #define Short(s)  
00045         #define UShort(s)  
00046         #define Int(i)  
00047         #define UInt(i)  
00048         #define Float(f)  
00049         #define Double(d)  
00050 
00051         #define BigShort(s) iSwapShort(s)
00052         #define BigUShort(s) iSwapUShort(s)
00053         #define BigInt(i) iSwapInt(i)
00054         #define BigUInt(i) iSwapUInt(i)
00055         #define BigFloat(f) iSwapFloat(f)
00056         #define BigDouble(d) iSwapDouble(d)
00057 #endif
00058 
00059 void   iSwapUShort(ILushort *s);
00060 void   iSwapShort(ILshort *s);
00061 void   iSwapUInt(ILuint *i);
00062 void   iSwapInt(ILint *i);
00063 void   iSwapFloat(ILfloat *f);
00064 void   iSwapDouble(ILdouble *d);
00065 ILushort GetLittleUShort();
00066 ILshort  GetLittleShort();
00067 ILuint   GetLittleUInt();
00068 ILint    GetLittleInt();
00069 ILfloat  GetLittleFloat();
00070 ILdouble GetLittleDouble();
00071 ILushort GetBigUShort();
00072 ILshort  GetBigShort();
00073 ILuint   GetBigUInt();
00074 ILint    GetBigInt();
00075 ILfloat  GetBigFloat();
00076 ILdouble GetBigDouble();
00077 ILubyte SaveLittleUShort(ILushort s);
00078 ILubyte SaveLittleShort(ILshort s);
00079 ILubyte SaveLittleUInt(ILuint i);
00080 ILubyte SaveLittleInt(ILint i);
00081 ILubyte SaveLittleFloat(ILfloat f);
00082 ILubyte SaveLittleDouble(ILdouble d);
00083 ILubyte SaveBigUShort(ILushort s);
00084 ILubyte SaveBigShort(ILshort s);
00085 ILubyte SaveBigUInt(ILuint i);
00086 ILubyte SaveBigInt(ILint i);
00087 ILubyte SaveBigFloat(ILfloat f);
00088 ILubyte SaveBigDouble(ILdouble d);
00089 
00090 #ifdef IL_ENDIAN_C
00091 #undef NOINLINE
00092 #undef INLINE
00093 #define INLINE
00094 #endif
00095 
00096 #ifndef NOINLINE
00097 INLINE void iSwapUShort(ILushort *s)  {
00098         #ifdef USE_WIN32_ASM
00099                 __asm {
00100                         mov ebx, s
00101                         mov al, [ebx+1]
00102                         mov ah, [ebx  ]
00103                         mov [ebx], ax
00104                 }
00105         #else
00106         #ifdef GCC_X86_ASM
00107                 asm("ror $8,%0"
00108                         : "=r" (*s)
00109                         : "0" (*s));
00110         #else
00111                 *s = ((*s)>>8) | ((*s)<<8);
00112         #endif //GCC_X86_ASM
00113         #endif //USE_WIN32_ASM
00114 }
00115 
00116 INLINE void iSwapShort(ILshort *s) {
00117         iSwapUShort((ILushort*)s);
00118 }
00119 
00120 INLINE void iSwapUInt(ILuint *i) {
00121         #ifdef USE_WIN32_ASM
00122                 __asm {
00123                         mov ebx, i
00124                         mov eax, [ebx]
00125                         bswap eax
00126                         mov [ebx], eax
00127                 }
00128         #else
00129         #ifdef GCC_X86_ASM
00130                         asm("bswap %0;"
00131                                 : "+r" (*i));
00132         #else
00133                 *i = ((*i)>>24) | (((*i)>>8) & 0xff00) | (((*i)<<8) & 0xff0000) | ((*i)<<24);
00134         #endif //GCC_X86_ASM
00135         #endif //USE_WIN32_ASM
00136 }
00137 
00138 INLINE void iSwapInt(ILint *i) {
00139         iSwapUInt((ILuint*)i);
00140 }
00141 
00142 INLINE void iSwapFloat(ILfloat *f) {
00143         iSwapUInt((ILuint*)f);
00144 }
00145 
00146 INLINE void iSwapDouble(ILdouble *d) {
00147         #ifdef GCC_X86_ASM
00148         int *t = (int*)d;
00149         asm("bswap %2    \n"
00150                 "bswap %3    \n"
00151                 "movl  %2,%1 \n"
00152                 "movl  %3,%0 \n"
00153                 : "=g" (t[0]), "=g" (t[1])
00154                 : "r"  (t[0]), "r"  (t[1]));
00155         #else
00156         ILubyte t,*b = (ILubyte*)d;
00157         #define dswap(x,y) t=b[x];b[x]=b[y];b[y]=b[x];
00158         dswap(0,7);
00159         dswap(1,6);
00160         dswap(2,5);
00161         dswap(3,4);
00162         #undef dswap
00163         #endif
00164 }
00165 
00166 
00167 INLINE ILushort GetLittleUShort() {
00168         ILushort s;
00169         iread(&s, sizeof(ILushort), 1);
00170 #ifdef __BIG_ENDIAN__
00171         iSwapUShort(&s);
00172 #endif
00173         return s;
00174 }
00175 
00176 INLINE ILshort GetLittleShort() {
00177         ILshort s;
00178         iread(&s, sizeof(ILshort), 1);
00179 #ifdef __BIG_ENDIAN__
00180         iSwapShort(&s);
00181 #endif
00182         return s;
00183 }
00184 
00185 INLINE ILuint GetLittleUInt() {
00186         ILuint i;
00187         iread(&i, sizeof(ILuint), 1);
00188 #ifdef __BIG_ENDIAN__
00189         iSwapUInt(&i);
00190 #endif
00191         return i;
00192 }
00193 
00194 INLINE ILint GetLittleInt() {
00195         ILint i;
00196         iread(&i, sizeof(ILint), 1);
00197 #ifdef __BIG_ENDIAN__
00198         iSwapInt(&i);
00199 #endif
00200         return i;
00201 }
00202 
00203 INLINE ILfloat GetLittleFloat() {
00204         ILfloat f;
00205         iread(&f, sizeof(ILfloat), 1);
00206 #ifdef __BIG_ENDIAN__
00207         iSwapFloat(&f);
00208 #endif
00209         return f;
00210 }
00211 
00212 INLINE ILdouble GetLittleDouble() {
00213         ILdouble d;
00214         iread(&d, sizeof(ILdouble), 1);
00215 #ifdef __BIG_ENDIAN__
00216         iSwapDouble(&d);
00217 #endif
00218         return d;
00219 }
00220 
00221 
00222 INLINE ILushort GetBigUShort() {
00223         ILushort s;
00224         iread(&s, sizeof(ILushort), 1);
00225 #ifdef __LITTLE_ENDIAN__
00226         iSwapUShort(&s);
00227 #endif
00228         return s;
00229 }
00230 
00231 
00232 INLINE ILshort GetBigShort() {
00233         ILshort s;
00234         iread(&s, sizeof(ILshort), 1);
00235 #ifdef __LITTLE_ENDIAN__
00236         iSwapShort(&s);
00237 #endif
00238         return s;
00239 }
00240 
00241 
00242 INLINE ILuint GetBigUInt() {
00243         ILuint i;
00244         iread(&i, sizeof(ILuint), 1);
00245 #ifdef __LITTLE_ENDIAN__
00246         iSwapUInt(&i);
00247 #endif
00248         return i;
00249 }
00250 
00251 
00252 INLINE ILint GetBigInt() {
00253         ILint i;
00254         iread(&i, sizeof(ILint), 1);
00255 #ifdef __LITTLE_ENDIAN__
00256         iSwapInt(&i);
00257 #endif
00258         return i;
00259 }
00260 
00261 
00262 INLINE ILfloat GetBigFloat() {
00263         ILfloat f;
00264         iread(&f, sizeof(ILfloat), 1);
00265 #ifdef __LITTLE_ENDIAN__
00266         iSwapFloat(&f);
00267 #endif
00268         return f;
00269 }
00270 
00271 
00272 INLINE ILdouble GetBigDouble() {
00273         ILdouble d;
00274         iread(&d, sizeof(ILdouble), 1);
00275 #ifdef __LITTLE_ENDIAN__
00276         iSwapDouble(&d);
00277 #endif
00278         return d;
00279 }
00280 
00281 INLINE ILubyte SaveLittleUShort(ILushort s) {
00282 #ifdef __BIG_ENDIAN__
00283         iSwapUShort(&s);
00284 #endif
00285         return iwrite(&s, sizeof(ILushort), 1);
00286 }
00287 
00288 INLINE ILubyte SaveLittleShort(ILshort s) {
00289 #ifdef __BIG_ENDIAN__
00290         iSwapShort(&s);
00291 #endif
00292         return iwrite(&s, sizeof(ILshort), 1);
00293 }
00294 
00295 
00296 INLINE ILubyte SaveLittleUInt(ILuint i) {
00297 #ifdef __BIG_ENDIAN__
00298         iSwapUInt(&i);
00299 #endif
00300         return iwrite(&i, sizeof(ILuint), 1);
00301 }
00302 
00303 
00304 INLINE ILubyte SaveLittleInt(ILint i) {
00305 #ifdef __BIG_ENDIAN__
00306         iSwapInt(&i);
00307 #endif
00308         return iwrite(&i, sizeof(ILint), 1);
00309 }
00310 
00311 INLINE ILubyte SaveLittleFloat(ILfloat f) {
00312 #ifdef __BIG_ENDIAN__
00313         iSwapFloat(&f);
00314 #endif
00315         return iwrite(&f, sizeof(ILfloat), 1);
00316 }
00317 
00318 
00319 INLINE ILubyte SaveLittleDouble(ILdouble d) {
00320 #ifdef __BIG_ENDIAN__
00321         iSwapDouble(&d);
00322 #endif
00323         return iwrite(&d, sizeof(ILdouble), 1);
00324 }
00325 
00326 
00327 INLINE ILubyte SaveBigUShort(ILushort s) {
00328 #ifdef __LITTLE_ENDIAN__
00329         iSwapUShort(&s);
00330 #endif
00331         return iwrite(&s, sizeof(ILushort), 1);
00332 }
00333 
00334 
00335 INLINE ILubyte SaveBigShort(ILshort s) {
00336 #ifdef __LITTLE_ENDIAN__
00337         iSwapShort(&s);
00338 #endif
00339         return iwrite(&s, sizeof(ILshort), 1);
00340 }
00341 
00342 
00343 INLINE ILubyte SaveBigUInt(ILuint i) {
00344 #ifdef __LITTLE_ENDIAN__
00345         iSwapUInt(&i);
00346 #endif
00347         return iwrite(&i, sizeof(ILuint), 1);
00348 }
00349 
00350 
00351 INLINE ILubyte SaveBigInt(ILint i) {
00352 #ifdef __LITTLE_ENDIAN__
00353         iSwapInt(&i);
00354 #endif
00355         return iwrite(&i, sizeof(ILint), 1);
00356 }
00357 
00358 
00359 INLINE ILubyte SaveBigFloat(ILfloat f) {
00360 #ifdef __LITTLE_ENDIAN__
00361         iSwapFloat(&f);
00362 #endif
00363         return iwrite(&f, sizeof(ILfloat), 1);
00364 }
00365 
00366 
00367 INLINE ILubyte SaveBigDouble(ILdouble d) {
00368 #ifdef __LITTLE_ENDIAN__
00369         iSwapDouble(&d);
00370 #endif
00371         return iwrite(&d, sizeof(ILdouble), 1);
00372 }
00373 #endif//NOINLINE
00374 
00375 void            EndianSwapData(void *_Image);
00376 
00377 #endif//ENDIAN_H