00001 #ifndef MISC_ENDIAN_H
00002 #define MISC_ENDIAN_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00035 #include <glt/config.h>
00036
00037 #if !defined(GLT_LITTLE_ENDIAN) && !defined(GLT_BIG_ENDIAN)
00038 #error GLT_LITTLE_ENDIAN or GLT_BIG_ENDIAN is not defined!
00039 #endif
00040
00041 #if defined(GLT_LITTLE_ENDIAN) && defined(GLT_BIG_ENDIAN)
00042 #error GLT_LITTLE_ENDIAN and GLT_BIG_ENDIAN are both defined!
00043 #endif
00044
00049 inline uint32 flip(const uint32 *v)
00050 {
00051 const byte *b = (const byte *) v;
00052 return b[0]<<24 | b[1]<<16 | b[2]<<8 | b[3];
00053 }
00054
00055 inline uint32 flip(const uint32 &v)
00056 {
00057 return flip(&v);
00058 }
00059
00060 inline uint32 flip(const int32 &v)
00061 {
00062 return flip(reinterpret_cast<const uint32 *>(&v));
00063 }
00064
00069 inline uint16 flip(const uint16 *v)
00070 {
00071 const byte *b = (const byte *) v;
00072 return b[0]<<8 | b[1];
00073 }
00074
00075 inline uint16 flip(const uint16 &v)
00076 {
00077 return flip(&v);
00078 }
00079
00080 inline int16 flip(const int16 &v)
00081 {
00082 return flip(reinterpret_cast<const uint32 *>(&v));
00083 }
00084
00090 inline uint32 littleEndian(const uint32 *v)
00091 {
00092 #ifdef GLT_LITTLE_ENDIAN
00093 return *v;
00094 #endif
00095
00096 #ifdef GLT_BIG_ENDIAN
00097 return flip(v);
00098 #endif
00099 }
00100
00106 inline uint16 littleEndian(const uint16 *v)
00107 {
00108 #ifdef GLT_LITTLE_ENDIAN
00109 return *v;
00110 #endif
00111
00112 #ifdef GLT_BIG_ENDIAN
00113 return flip(v);
00114 #endif
00115 }
00116
00122 inline uint32 littleEndian(const uint32 &v)
00123 {
00124 #ifdef GLT_LITTLE_ENDIAN
00125 return v;
00126 #endif
00127
00128 #ifdef GLT_BIG_ENDIAN
00129 return flip(&v);
00130 #endif
00131 }
00132
00138 inline uint16 littleEndian(const uint16 &v)
00139 {
00140 #ifdef GLT_LITTLE_ENDIAN
00141 return v;
00142 #endif
00143
00144 #ifdef GLT_BIG_ENDIAN
00145 return flip(&v);
00146 #endif
00147 }
00148
00154 inline uint32 bigEndian(const uint32 *v)
00155 {
00156 #ifdef GLT_BIG_ENDIAN
00157 return *v;
00158 #endif
00159
00160 #ifdef GLT_LITTLE_ENDIAN
00161 return flip(v);
00162 #endif
00163 }
00164
00170 inline uint16 bigEndian(const uint16 *v)
00171 {
00172 #ifdef GLT_BIG_ENDIAN
00173 return *v;
00174 #endif
00175
00176 #ifdef GLT_LITTLE_ENDIAN
00177 return flip(v);
00178 #endif
00179 }
00180
00186 inline uint32 bigEndian(const uint32 &v)
00187 {
00188 #ifdef GLT_BIG_ENDIAN
00189 return v;
00190 #endif
00191
00192 #ifdef GLT_LITTLE_ENDIAN
00193 return flip(&v);
00194 #endif
00195 }
00196
00202 inline uint32 bigEndian(const uint16 &v)
00203 {
00204 #ifdef GLT_BIG_ENDIAN
00205 return v;
00206 #endif
00207
00208 #ifdef GLT_LITTLE_ENDIAN
00209 return flip(&v);
00210 #endif
00211 }
00212
00213 #endif