home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 113 / EnigmaAmiga113CD.iso / software / varie / xad / developer / sources / clients / converte.c next >
Encoding:
C/C++ Source or Header  |  2000-06-25  |  2.5 KB  |  73 lines

  1. #ifndef XADMASTER_CONVERTE_C
  2. #define XADMASTER_CONVERTE_C
  3.  
  4. /* Programmheader
  5.  
  6.     Name:        ConvertE.c
  7.     Main:        xadmaster
  8.     Versionstring:    $VER: ConvertE.c 1.1 (22.06.2000)
  9.     Author:        SDI
  10.     Distribution:    Freeware
  11.     Description:    endian conversion
  12.  
  13.  1.0   22.08.1999 : first version
  14.  1.1   22.06.2000 : improved version by Kyzer, removed EndSet funcs
  15. */
  16.  
  17. /* macros to use:
  18.  * EndGetXXX(a)  - reads value of endianness XXX from memory.
  19.  *                 'a' should be of type 'char *' or 'unsigned char *'
  20.  * EndConvXXX(a) - converts expression/value/var of endianness XXX to
  21.  *                 the correct endianness for this architecture.
  22.  *
  23.  * XXX can be:
  24.  * M32 - big-endian Motorola format, 32 bit value
  25.  * M16 - big-endian Motorola format, 16 bit value
  26.  * I32 - little-endian Intel format, 32 bit value
  27.  * I16 - little-endian Intel format, 16 bit value
  28.  *
  29.  * Note that the EndConvXXX macros only work for architectures that are
  30.  * themselves little- or big-endian, and only work if the defines tested
  31.  * below reveal the correct endianness of the architecture being compiled for.
  32.  * Define either XAD_BIGENDIAN or XAD_LITTLEENDIAN.
  33.  *
  34.  * It is recommended that you only use the EndGetXXX() macros, as these are
  35.  * endian-neutral, and even work on middle-endian machine like PDPs :)
  36.  *
  37.  * Keep in mind, that the macros require calculation time, so avoid to use
  38.  * them double time. Call them once and reuse results.
  39.  */
  40.  
  41. #define EndGetM32(a)  ((((a)[0])<<24)|(((a)[1])<<16)|(((a)[2])<<8)|((a)[3]))
  42. #define EndGetM16(a)  ((((a)[0])<<8)|((a)[1]))
  43. #define EndGetI32(a)  ((((a)[3])<<24)|(((a)[2])<<16)|(((a)[1])<<8)|((a)[0]))
  44. #define EndGetI16(a)  ((((a)[1])<<8)|((a)[0]))
  45.  
  46. /* private */
  47. #define _xecswap16(a) ((((a) & 0x00FF) <<  8) | (((a) >>  8) & 0x00FF))
  48. #define _xecswap32(a) ((((a) & 0x00FF) << 24) | (((a) & 0xFF00) <<  8) |  \
  49.                        (((a) >>  8) & 0xFF00) | (((a) >> 24) & 0x00FF))
  50. /* some future music */
  51. #define _xecswap64(a) (_ecswap32((a) >> 32) | (_ecswap32(a) << 32))
  52.  
  53. /* make life easier on our main machine, the famous Amiga */
  54. #if defined(AMIGA) && !defined(XAD_BIGENDIAN) && !defined(XAD_LITTLEENDIAN)
  55.   #define XAD_BIGENDIAN
  56. #endif
  57.  
  58. #if defined(XAD_BIGENDIAN)
  59. #define EndConvM32(a)    (a)
  60. #define EndConvM16(a)    (a)
  61. #define EndConvI32(a)    _xecswap32(a)
  62. #define EndConvI16(a)    _xecswap16(a)
  63. #elif defined(XAD_LITTLEENDIAN)
  64. #define EndConvM32(a)    _xecswap32(a)
  65. #define EndConvM16(a)    _xecswap16(a)
  66. #define EndConvI32(a)    (a)
  67. #define EndConvI16(a)    (a)
  68. #else
  69.   #error Either XAD_BIGENDIAN or XAD_LITTLEENDIAN must be defined!
  70. #endif
  71.  
  72. #endif /* XADMASTER_CONVERTE_C */
  73.