physfs_byteorder.c
changeset 177 9aaf01f4b8ec
child 194 09c353d30cd4
equal deleted inserted replaced
176:4a996749e39d 177:9aaf01f4b8ec
       
     1 /**
       
     2  * PhysicsFS; a portable, flexible file i/o abstraction.
       
     3  *
       
     4  * Documentation is in physfs.h. It's verbose, honest.  :)
       
     5  *
       
     6  * Please see the file LICENSE in the source's root directory.
       
     7  *
       
     8  *  This file written by Ryan C. Gordon.
       
     9  */
       
    10 
       
    11 #include <stdio.h>
       
    12 #include <stdlib.h>
       
    13 #include <assert.h>
       
    14 #include "physfs.h"
       
    15 
       
    16 /* This byteorder stuff was lifted from SDL. http://www.libsdl.org/ */
       
    17 #define PHYSFS_LIL_ENDIAN  1234
       
    18 #define PHYSFS_BIG_ENDIAN  4321
       
    19 
       
    20 #if  defined(__i386__) || defined(__ia64__) || defined(WIN32) || \
       
    21     (defined(__alpha__) || defined(__alpha)) || \
       
    22      defined(__arm__) || \
       
    23     (defined(__mips__) && defined(__MIPSEL__)) || \
       
    24      defined(__SYMBIAN32__) || \
       
    25      defined(__LITTLE_ENDIAN__)
       
    26 #define PHYSFS_BYTEORDER    PHYSFS_LIL_ENDIAN
       
    27 #else
       
    28 #define PHYSFS_BYTEORDER    PHYSFS_BIG_ENDIAN
       
    29 #endif
       
    30 
       
    31 
       
    32 /* The macros used to swap values */
       
    33 /* Try to use superfast macros on systems that support them */
       
    34 #ifdef linux
       
    35 #include <asm/byteorder.h>
       
    36 #ifdef __arch__swab16
       
    37 #define PHYSFS_Swap16  __arch__swab16
       
    38 #endif
       
    39 #ifdef __arch__swab32
       
    40 #define PHYSFS_Swap32  __arch__swab32
       
    41 #endif
       
    42 #endif /* linux */
       
    43 
       
    44 #ifndef PHYSFS_Swap16
       
    45 static __inline__ PHYSFS_uint16 PHYSFS_Swap16(PHYSFS_uint16 D)
       
    46 {
       
    47 	return((D<<8)|(D>>8));
       
    48 }
       
    49 #endif
       
    50 #ifndef PHYSFS_Swap32
       
    51 static __inline__ PHYSFS_uint32 PHYSFS_Swap32(PHYSFS_uint32 D)
       
    52 {
       
    53 	return((D<<24)|((D<<8)&0x00FF0000)|((D>>8)&0x0000FF00)|(D>>24));
       
    54 }
       
    55 #endif
       
    56 #ifndef PHYSFS_NO_64BIT_SUPPORT
       
    57 #ifndef PHYSFS_Swap64
       
    58 static __inline__ PHYSFS_uint64 PHYSFS_Swap64(PHYSFS_uint64 val) {
       
    59 	PHYSFS_uint32 hi, lo;
       
    60 
       
    61 	/* Separate into high and low 32-bit values and swap them */
       
    62 	lo = (PHYSFS_uint32)(val&0xFFFFFFFF);
       
    63 	val >>= 32;
       
    64 	hi = (PHYSFS_uint32)(val&0xFFFFFFFF);
       
    65 	val = PHYSFS_Swap32(lo);
       
    66 	val <<= 32;
       
    67 	val |= PHYSFS_Swap32(hi);
       
    68 	return(val);
       
    69 }
       
    70 #endif
       
    71 #else
       
    72 #ifndef PHYSFS_Swap64
       
    73 /* This is mainly to keep compilers from complaining in PHYSFS code.
       
    74    If there is no real 64-bit datatype, then compilers will complain about
       
    75    the fake 64-bit datatype that PHYSFS provides when it compiles user code.
       
    76 */
       
    77 #define PHYSFS_Swap64(X)	(X)
       
    78 #endif
       
    79 #endif /* PHYSFS_NO_64BIT_SUPPORT */
       
    80 
       
    81 
       
    82 /* Byteswap item from the specified endianness to the native endianness */
       
    83 #if PHYSFS_BYTEORDER == PHYSFS_LIL_ENDIAN
       
    84 PHYSFS_uint16 PHYSFS_swapULE16(PHYSFS_uint16 x) { return(x); }
       
    85 PHYSFS_sint16 PHYSFS_swapSLE16(PHYSFS_sint16 x) { return(x); }
       
    86 PHYSFS_uint32 PHYSFS_swapULE32(PHYSFS_uint32 x) { return(x); }
       
    87 PHYSFS_sint32 PHYSFS_swapSLE32(PHYSFS_sint32 x) { return(x); }
       
    88 PHYSFS_uint64 PHYSFS_swapULE64(PHYSFS_uint64 x) { return(x); }
       
    89 PHYSFS_sint64 PHYSFS_swapSLE64(PHYSFS_sint64 x) { return(x); }
       
    90 
       
    91 PHYSFS_uint16 PHYSFS_swapUBE16(PHYSFS_uint16 x) { return(PHYSFS_Swap16(x)); }
       
    92 PHYSFS_sint16 PHYSFS_swapSBE16(PHYSFS_sint16 x) { return(PHYSFS_Swap16(x)); }
       
    93 PHYSFS_uint32 PHYSFS_swapUBE32(PHYSFS_uint32 x) { return(PHYSFS_Swap32(x)); }
       
    94 PHYSFS_sint32 PHYSFS_swapSBE32(PHYSFS_sint32 x) { return(PHYSFS_Swap32(x)); }
       
    95 PHYSFS_uint64 PHYSFS_swapUBE64(PHYSFS_uint64 x) { return(PHYSFS_Swap64(x)); }
       
    96 PHYSFS_sint64 PHYSFS_swapSBE64(PHYSFS_sint64 x) { return(PHYSFS_Swap64(x)); }
       
    97 #else
       
    98 PHYSFS_uint16 PHYSFS_swapULE16(PHYSFS_uint16 x) { return(PHYSFS_Swap16(x)); }
       
    99 PHYSFS_sint16 PHYSFS_swapSLE16(PHYSFS_sint16 x) { return(PHYSFS_Swap16(x)); }
       
   100 PHYSFS_uint32 PHYSFS_swapULE32(PHYSFS_uint32 x) { return(PHYSFS_Swap32(x)); }
       
   101 PHYSFS_sint32 PHYSFS_swapSLE32(PHYSFS_sint32 x) { return(PHYSFS_Swap32(x)); }
       
   102 PHYSFS_uint64 PHYSFS_swapULE64(PHYSFS_uint64 x) { return(PHYSFS_Swap64(x)); }
       
   103 PHYSFS_sint64 PHYSFS_swapSLE64(PHYSFS_sint64 x) { return(PHYSFS_Swap64(x)); }
       
   104 
       
   105 PHYSFS_uint16 PHYSFS_swapUBE16(PHYSFS_uint16 x) { return(x); }
       
   106 PHYSFS_sint16 PHYSFS_swapSBE16(PHYSFS_sint16 x) { return(x); }
       
   107 PHYSFS_uint32 PHYSFS_swapUBE32(PHYSFS_uint32 x) { return(x); }
       
   108 PHYSFS_sint32 PHYSFS_swapSBE32(PHYSFS_sint32 x) { return(x); }
       
   109 PHYSFS_uint64 PHYSFS_swapUBE64(PHYSFS_uint64 x) { return(x); }
       
   110 PHYSFS_sint64 PHYSFS_swapSBE64(PHYSFS_sint64 x) { return(x); }
       
   111 #endif
       
   112 
       
   113 /* end of physfs_byteorder.c ... */
       
   114