IT++ Logo Newcom Logo

machdep.h

Go to the documentation of this file.
00001 
00033 #ifndef MACHDEP_H
00034 #define MACHDEP_H
00035 
00036 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00037 
00038 #ifndef _MSC_VER
00039 #  include <itpp/config.h>
00040 #else
00041 #  include <itpp/config_msvc.h>
00042 #endif
00043 
00044 #ifdef HAVE_ENDIAN_H
00045 #  include <endian.h>
00046 #else
00047 
00048 #ifndef __LITTLE_ENDIAN
00049 #  define __LITTLE_ENDIAN 1234
00050 #endif
00051 #ifndef __BIG_ENDIAN
00052 #  define __BIG_ENDIAN 4321
00053 #endif
00054 
00055 #ifndef __BYTE_ORDER
00056 
00057   /*
00058    * Big endian: _AIX, AIX, sparc, __sparc__, __mc68000__, __m68k__, MIPSEB,
00059    * __MIPSEB__, __ARMEB, HPPA, __hppa__, PPC, __ppc_, __PPC__, __powerpc__.
00060    * 
00061    * Little endian: i386, __i386__, __x86_64__, __amd64__, __sh__, __vax__
00062    * (yes, really!), MIPSEL, __MIPSEL__, and __ARMEL__, __alpha, __alpha__,
00063    * __ia64__, _M_IX86, _M_X64, _M_IA64
00064    *
00065    * It seems that ppc, hppa, ia64, alpha, sparc, mips, arm and superh have
00066    * both big and little endian modes.
00067    */
00068 #if defined(_AIX) || defined(AIX)                                       \
00069   || defined(sparc) || defined(__sparc__)                               \
00070   || defined(__mc68000__) || defined(__mk68k__)                         \
00071   || defined(MIPSEB) || defined(__MIPSEB__)                             \
00072   || defined(__ARMEB)                                                   \
00073   || defined(HPPA) || defined(__hppa__)                                 \
00074   || defined(PPC) || defined(__ppc__) || defined(__PPC__)               \
00075   || defined(__powerpc__)                                               \
00076   || defined(__s390__) || defined(__s390x__)
00077 #  define __BYTE_ORDER __BIG_ENDIAN
00078 #elif defined(i386) || defined(__i386__) || defined(_M_IX86)            \
00079   || defined(__x86_64__) || defined(__amd64__) || defined(_M_X64)       \
00080   || defined(__ia64) || defined(__ia64__) || defined(_M_IA64)           \
00081   || defined(MIPSEL) || defined(__MIPSEL__)                             \
00082   || defined(__alpha) || defined(__alpha__)
00083 #  define __BYTE_ORDER __LITTLE_ENDIAN
00084 #endif
00085 
00086 #endif // ifndef __BYTE_ORDER
00087 
00088 #endif // ifndef HAVE_ENDIAN_H
00089 
00090 
00091 namespace itpp {
00092 
00093 #define REV_2(from, to)                                   \
00094               ((char *)(to))[0] = ((char *)(from))[1];    \
00095               ((char *)(to))[1] = ((char *)(from))[0];
00096 
00097 #define REV_4(from, to)                                   \
00098               ((char *)(to))[0] = ((char *)(from))[3];    \
00099               ((char *)(to))[1] = ((char *)(from))[2];    \
00100               ((char *)(to))[2] = ((char *)(from))[1];    \
00101               ((char *)(to))[3] = ((char *)(from))[0];
00102 
00103 #define REV_8(from, to)                                   \
00104               ((char *)(to))[0] = ((char *)(from))[7];    \
00105               ((char *)(to))[1] = ((char *)(from))[6];    \
00106               ((char *)(to))[2] = ((char *)(from))[5];    \
00107               ((char *)(to))[3] = ((char *)(from))[4];    \
00108               ((char *)(to))[4] = ((char *)(from))[3];    \
00109               ((char *)(to))[5] = ((char *)(from))[2];    \
00110               ((char *)(to))[6] = ((char *)(from))[1];    \
00111               ((char *)(to))[7] = ((char *)(from))[0];
00112 
00113 
00114   //Typedefs for 32 bit architechures (default)
00115   typedef signed char        it_s8;
00116   typedef unsigned char      it_u8;
00117   typedef signed short       it_s16;
00118   typedef unsigned short     it_u16;
00119   typedef signed int         it_s32;
00120   typedef unsigned int       it_u32;
00121   typedef float              it_f32;
00122   typedef double             it_f64;
00123 
00124   //One byte data types are independent of endianity:
00125   inline it_s8 big_endian(it_s8 x)              { return x; }
00126   inline it_u8 big_endian(it_u8 x)              { return x; }
00127   inline it_s8 little_endian(it_s8 x)           { return x; }
00128   inline it_u8 little_endian(it_u8 x)           { return x; }
00129   inline void  big_endian(it_s8 x, it_s8 &y)    { y = x; }
00130   inline void  big_endian(it_u8 x, it_u8 &y)    { y = x; }
00131   inline void  little_endian(it_s8 x, it_s8 &y) { y = x; }
00132   inline void  little_endian(it_u8 x, it_u8 &y) { y = x; }
00133 
00134 
00135 
00136 #if __BYTE_ORDER == __BIG_ENDIAN
00137 
00138   //------------------------------------------------------
00139   // Big Endian
00140   //------------------------------------------------------
00141 
00142   inline it_s16 big_endian(it_s16 x) { return x; }
00143   inline it_u16 big_endian(it_u16 x) { return x; }
00144   inline it_s32 big_endian(it_s32 x) { return x; }
00145   inline it_u32 big_endian(it_u32 x) { return x; }
00146   inline it_f32 big_endian(it_f32 x) { return x; }
00147   inline it_f64 big_endian(it_f64 x) { return x; }
00148 
00149   inline it_s16 little_endian(it_s16 x) { it_s16 y; REV_2(&x,&y); return y; }
00150   inline it_u16 little_endian(it_u16 x) { it_u16 y; REV_2(&x,&y); return y; }
00151   inline it_s32 little_endian(it_s32 x) { it_s32 y; REV_4(&x,&y); return y; }
00152   inline it_u32 little_endian(it_u32 x) { it_u32 y; REV_4(&x,&y); return y; }
00153   inline it_f32 little_endian(it_f32 x) { it_f32 y; REV_4(&x,&y); return y; }
00154   inline it_f64 little_endian(it_f64 x) { it_f64 y; REV_8(&x,&y); return y; }
00155 
00156   inline void big_endian(it_s16 x, it_s16 &y) { y = x; }
00157   inline void big_endian(it_u16 x, it_u16 &y) { y = x; }
00158   inline void big_endian(it_s32 x, it_s32 &y) { y = x; }
00159   inline void big_endian(it_u32 x, it_u32 &y) { y = x; }
00160   inline void big_endian(it_f32 x, it_f32 &y) { y = x; }
00161   inline void big_endian(it_f64 x, it_f64 &y) { y = x; }
00162 
00163   inline void little_endian(it_s16 x, it_s16 &y) { REV_2(&x,&y); }
00164   inline void little_endian(it_u16 x, it_u16 &y) { REV_2(&x,&y); }
00165   inline void little_endian(it_s32 x, it_s32 &y) { REV_4(&x,&y); }
00166   inline void little_endian(it_u32 x, it_u32 &y) { REV_4(&x,&y); }
00167   inline void little_endian(it_f32 x, it_f32 &y) { REV_4(&x,&y); }
00168   inline void little_endian(it_f64 x, it_f64 &y) { REV_8(&x,&y); }
00169 
00170   //Additions for some 64 bit architechtures
00171 // #if defined(sparc)
00172 //   typedef signed long        it_s64;
00173 //   typedef unsigned long      it_u64;
00174 //   inline it_s64 big_endian(it_s64 x) { return x; }
00175 //   inline it_u64 big_endian(it_u64 x) { return x; }
00176 //   inline it_s64 little_endian(it_s64 x) { it_s64 y; REV_4(&x,&y); return y; }
00177 //   inline it_u64 little_endian(it_u64 x) { it_u64 y; REV_4(&x,&y); return y; }
00178 //   inline void big_endian(it_s64 x, it_s64 &y) { y = x; }
00179 //   inline void big_endian(it_u64 x, it_u64 &y) { y = x; }
00180 //   inline void little_endian(it_s64 x, it_s64 &y) { REV_4(&x,&y); }
00181 //   inline void little_endian(it_u64 x, it_u64 &y) { REV_4(&x,&y); }
00182 // #endif
00183 
00184 #elif __BYTE_ORDER == __LITTLE_ENDIAN
00185 
00186   //------------------------------------------------------
00187   // Little Endian
00188   //------------------------------------------------------
00189 
00190   //32 bit architechures (default)
00191   inline it_s16 big_endian(it_s16 x) { it_s16 y; REV_2(&x,&y); return y; }
00192   inline it_u16 big_endian(it_u16 x) { it_u16 y; REV_2(&x,&y); return y; }
00193   inline it_s32 big_endian(it_s32 x) { it_s32 y; REV_4(&x,&y); return y; }
00194   inline it_u32 big_endian(it_u32 x) { it_u32 y; REV_4(&x,&y); return y; }
00195   inline it_f32 big_endian(it_f32 x) { it_f32 y; REV_4(&x,&y); return y; }
00196   inline it_f64 big_endian(it_f64 x) { it_f64 y; REV_8(&x,&y); return y; }
00197 
00198   inline it_s16 little_endian(it_s16 x) { return x; }
00199   inline it_u16 little_endian(it_u16 x) { return x; }
00200   inline it_s32 little_endian(it_s32 x) { return x; }
00201   inline it_u32 little_endian(it_u32 x) { return x; }
00202   inline it_f32 little_endian(it_f32 x) { return x; }
00203   inline it_f64 little_endian(it_f64 x) { return x; }
00204 
00205   inline void big_endian(it_s16 x, it_s16 &y) { REV_2(&x,&y); }
00206   inline void big_endian(it_u16 x, it_u16 &y) { REV_2(&x,&y); }
00207   inline void big_endian(it_s32 x, it_s32 &y) { REV_4(&x,&y); }
00208   inline void big_endian(it_u32 x, it_u32 &y) { REV_4(&x,&y); }
00209   inline void big_endian(it_f32 x, it_f32 &y) { REV_4(&x,&y); }
00210   inline void big_endian(it_f64 x, it_f64 &y) { REV_8(&x,&y); }
00211 
00212   inline void little_endian(it_s16 x, it_s16 &y) { y = x; }
00213   inline void little_endian(it_u16 x, it_u16 &y) { y = x; }
00214   inline void little_endian(it_s32 x, it_s32 &y) { y = x; }
00215   inline void little_endian(it_u32 x, it_u32 &y) { y = x; }
00216   inline void little_endian(it_f32 x, it_f32 &y) { y = x; }
00217   inline void little_endian(it_f64 x, it_f64 &y) { y = x; }
00218 
00219   //Additions for some 64 bit architechtures
00220 // #if defined(alpha) || defined(__x86_64__) || defined(__s390x__)
00221 //   typedef signed long        it_s64;
00222 //   typedef unsigned long      it_u64;
00223 //   inline it_s64 big_endian(it_s64 x) { it_s64 y; REV_4(&x,&y); return y; }
00224 //   inline it_u64 big_endian(it_u64 x) { it_u64 y; REV_4(&x,&y); return y; }
00225 //   inline it_s64 little_endian(it_s64 x) { return x; }
00226 //   inline it_u64 little_endian(it_u64 x) { return x; }
00227 //   inline void big_endian(it_s64 x, it_s64 &y) { REV_4(&x,&y); }
00228 //   inline void big_endian(it_u64 x, it_u64 &y) { REV_4(&x,&y); }
00229 //   inline void little_endian(it_s64 x, it_s64 &y) { y = x; }
00230 //   inline void little_endian(it_u64 x, it_u64 &y) { y = x; }
00231 // #endif
00232 
00233 #else
00234 
00235 #error "Could not determine endianity!!!"
00236 
00237 #endif // if __BYTE_ORDER == ...
00238 
00239 #undef REV_2
00240 #undef REV_4
00241 #undef REV_8
00242 
00243 } // namespace itpp
00244 
00245 #endif //DOXYGEN_SHOULD_SKIP_THIS
00246 
00247 #endif // #ifndef MACHDEP_H
00248 
SourceForge Logo

Generated on Thu Apr 19 14:19:51 2007 for IT++ by Doxygen 1.4.6