#ifndef __type_h__ #define __type_h__ #define ENDIANITY_BIG 0 #define ENDIANITY_LITTLE 1 #ifdef ARCH_PENTIUM #define ENDIANITY ENDIANITY_LITTLE #endif #ifdef ARCH_I80486 #define ENDIANITY ENDIANITY_LITTLE #endif #ifndef ENDIANITY #error ENDIANITY #endif #define ALIGN(value,align) ( ( (value) + ((align)-1) ) & ~((align)-1) ) #define N_ELEMENTS(tab) (sizeof(tab)/sizeof(*tab)) #define OFFSET_OF(obj,field) ((U32)&(((obj*)0)->field)) #define ROOT_OF(obj,field,pt) ((obj*)(((U8*)pt)+OFFSET_OF(obj,field))) const int kilo=1024; const int mega=kilo*kilo; const int giga=kilo*mega; typedef char S8; typedef unsigned char U8; typedef short S16; typedef unsigned short U16; typedef long S32; typedef unsigned long U32; typedef long long S64; typedef unsigned long long U64; struct Void { U8 reallyVoid[0]; } __attribute__((packed)); template struct MaskMaker { static const U32 mask=~(((U32)~0)<<(U32)len)<<((U32)first); }; template<> struct MaskMaker<0,32> { static const int mask=~0; }; template struct Field { volatile U32 ici[0]; inline static const U32 mask() { return MaskMaker::mask; } inline Field & operator=(type v) { *ici= (*ici&~mask())|(((U32)v<>first); } inline Field & operator|=(type v) { return *this=*this|v; }; inline Field & operator&=(type v) { return *this=*this&v; }; } __attribute__((packed)); template struct RelativeField { U8 ici[0]; inline volatile U32 * const address() const { return (volatile U32*)(ici+offset); } inline static const U32 mask() { return MaskMaker::mask; } inline RelativeField & operator=(type v) { *address()= (*address()&~mask())|(((U32)v<>first); } inline RelativeField & operator|=(type v) { return *this=*this|v; }; inline RelativeField & operator&=(type v) { return *this=*this&v; }; inline RelativeField & operator+=(type v) { return *this=*this+v; }; inline RelativeField & operator-=(type v) { return *this=*this-v; }; } __attribute__((packed)); template struct Field32 { volatile U32 ici[0]; inline static const U32 mask() { return ~(((U32)~0)<<(U32)len)<<((U32)first); } inline U32 & operator=(U32 v) { return *ici= (*ici&~mask())|((v<>first); } } __attribute__((packed)); template struct Relative { U8 base[0]; inline accesseur* address() { return (accesseur*)(base+_offset); } inline const U32 offset() const { return _offset; } inline accesseur * operator->() { return address(); } inline Relative & operator=(type v) { //logMsg("*(0x%08x+0x%08x)=0x%08x\n",(int)base,offset,v,0,0,0); *address()=v; return *this; }; inline Relative & operator|=(type v) { return (*this)=v|((type)(*this)); } inline Relative & operator&=(type v) { return (*this)=v&((type)(*this)); } inline operator type() { return *address(); } inline accesseur * operator&() { //logMsg("*(0x%08x+0x%08x)=0x%08x\n",(int)base,offset,v,0,0,0); return address(); }; } __attribute__((packed)); template struct RelativeRef { U8 base[0]; typedef type *typeRef; inline operator typeRef() { return (typeRef)(base+offset); } inline type& operator*() { return *(typeRef)(base+offset); } inline type * operator->() { return (typeRef)(base+offset); } inline type& operator=(const type &value) { (typeRef&)(base+offset)=value; return *this; } inline bool operator!=(const type &value) { return (typeRef&)(base+offset)!=value; } } __attribute__((packed)); template struct Swap { type value; inline static type swap(type); inline Swap & operator=(type v) { value=swap(v); return *this; }; inline operator type() { return swap(value); } inline Swap & operator|=(type v) { return value= swap(v)| value; } inline Swap & operator&=(type v) { return value= swap(v)& value; } }; #if defined(ARCH_PENTIUM) || defined(ARCH_I80486) template<> inline U32 Swap::swap(U32 v) { register U32 reg=v; asm("bswap %0": "=r" (reg): "0" (reg) ); return reg; } template<> inline U16 Swap::swap(U16 v) { register U16 reg=v; v<<=4; asm("rolw $8,%0": "=r" (reg): "0" (reg) ); return reg; } template<> inline S32 Swap::swap(S32 v) { return Swap::swap(v); } template<> inline S16 Swap::swap(S16 v) { return Swap::swap(v); } #else #error Swap #endif #if ENDIANITY==ENDIANITY_LITTLE typedef S16 Sle16; typedef U16 Ule16; typedef S32 Sle32; typedef U32 Ule32; typedef Swap Sbe16; typedef Swap Ube16; typedef Swap Sbe32; typedef Swap Ube32; #endif #if ENDIANITY==ENDIANITY_BIG typedef S16 Sbe16; typedef U16 Ube16; typedef S32 Sbe32; typedef U32 Ube32; typedef Swap Sle16; typedef Swap Ule16; typedef Swap Sle32; typedef Swap Ule32; #endif template struct Aligned { private: type buf[len+align]; public: inline operator type *() { return (type*) ALIGN( (U32)buf, align); } }; #endif