ZenLib
|
00001 // ZenLib::Utils - Very small utilities 00002 // Copyright (C) 2002-2010 MediaArea.net SARL, Info@MediaArea.net 00003 // 00004 // This software is provided 'as-is', without any express or implied 00005 // warranty. In no event will the authors be held liable for any damages 00006 // arising from the use of this software. 00007 // 00008 // Permission is granted to anyone to use this software for any purpose, 00009 // including commercial applications, and to alter it and redistribute it 00010 // freely, subject to the following restrictions: 00011 // 00012 // 1. The origin of this software must not be misrepresented; you must not 00013 // claim that you wrote the original software. If you use this software 00014 // in a product, an acknowledgment in the product documentation would be 00015 // appreciated but is not required. 00016 // 2. Altered source versions must be plainly marked as such, and must not be 00017 // misrepresented as being the original software. 00018 // 3. This notice may not be removed or altered from any source distribution. 00019 // 00020 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 00021 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 00022 // 00023 // Integer and float manipulation 00024 // 00025 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 00026 00027 //--------------------------------------------------------------------------- 00028 #ifndef ZenUtilsH 00029 #define ZenUtilsH 00030 //--------------------------------------------------------------------------- 00031 00032 //--------------------------------------------------------------------------- 00033 #include "ZenLib/Conf.h" 00034 #include "ZenLib/int128u.h" 00035 //--------------------------------------------------------------------------- 00036 00037 namespace ZenLib 00038 { 00039 00040 //*************************************************************************** 00041 // Integer transformations 00042 //*************************************************************************** 00043 00044 //--------------------------------------------------------------------------- 00045 //Little Endians 00046 int8s LittleEndian2int8s (const char* List); 00047 int8u LittleEndian2int8u (const char* List); 00048 int16s LittleEndian2int16s (const char* List); 00049 int16u LittleEndian2int16u (const char* List); 00050 int32s LittleEndian2int24s (const char* List); 00051 int32u LittleEndian2int24u (const char* List); 00052 int32s LittleEndian2int32s (const char* List); 00053 int32u LittleEndian2int32u (const char* List); 00054 #if (MAXTYPE_INT >= 64) 00055 int64s LittleEndian2int40s (const char* List); 00056 int64u LittleEndian2int40u (const char* List); 00057 int64s LittleEndian2int48s (const char* List); 00058 int64u LittleEndian2int48u (const char* List); 00059 int64s LittleEndian2int56s (const char* List); 00060 int64u LittleEndian2int56u (const char* List); 00061 int64s LittleEndian2int64s (const char* List); 00062 int64u LittleEndian2int64u (const char* List); 00063 int128u LittleEndian2int128u (const char* List); 00064 #endif 00065 float32 LittleEndian2float32 (const char* List); 00066 float64 LittleEndian2float64 (const char* List); 00067 float80 LittleEndian2float80 (const char* List); 00068 00069 void int8s2LittleEndian (char* List, int8s Value); 00070 void int8u2LittleEndian (char* List, int8u Value); 00071 void int16s2LittleEndian (char* List, int16s Value); 00072 void int16u2LittleEndian (char* List, int16u Value); 00073 void int24s2LittleEndian (char* List, int32s Value); 00074 void int24u2LittleEndian (char* List, int32u Value); 00075 void int32s2LittleEndian (char* List, int32s Value); 00076 void int32u2LittleEndian (char* List, int32u Value); 00077 #if (MAXTYPE_INT >= 64) 00078 void int40s2LittleEndian (char* List, int64s Value); 00079 void int40u2LittleEndian (char* List, int64u Value); 00080 void int48s2LittleEndian (char* List, int64s Value); 00081 void int48u2LittleEndian (char* List, int64u Value); 00082 void int56s2LittleEndian (char* List, int64s Value); 00083 void int56u2LittleEndian (char* List, int64u Value); 00084 void int64s2LittleEndian (char* List, int64s Value); 00085 void int64u2LittleEndian (char* List, int64u Value); 00086 void int128u2LittleEndian (char* List, int128u Value); 00087 #endif 00088 void float322LittleEndian (char* List, float32 Value); 00089 void float642LittleEndian (char* List, float64 Value); 00090 void float802LittleEndian (char* List, float80 Value); 00091 00092 #ifndef __BORLANDC__ 00093 inline int8s LittleEndian2int8s (const int8u* List) {return LittleEndian2int8s ((const char*)List);} 00094 inline int8u LittleEndian2int8u (const int8u* List) {return LittleEndian2int8u ((const char*)List);} 00095 inline int16s LittleEndian2int16s (const int8u* List) {return LittleEndian2int16s ((const char*)List);} 00096 inline int16u LittleEndian2int16u (const int8u* List) {return LittleEndian2int16u ((const char*)List);} 00097 inline int32s LittleEndian2int24s (const int8u* List) {return LittleEndian2int24s ((const char*)List);} 00098 inline int32u LittleEndian2int24u (const int8u* List) {return LittleEndian2int24u ((const char*)List);} 00099 inline int32s LittleEndian2int32s (const int8u* List) {return LittleEndian2int32s ((const char*)List);} 00100 inline int32u LittleEndian2int32u (const int8u* List) {return LittleEndian2int32u ((const char*)List);} 00101 #if (MAXTYPE_INT >= 64) 00102 inline int64s LittleEndian2int40s (const int8u* List) {return LittleEndian2int40s ((const char*)List);} 00103 inline int64u LittleEndian2int40u (const int8u* List) {return LittleEndian2int40u ((const char*)List);} 00104 inline int64s LittleEndian2int48s (const int8u* List) {return LittleEndian2int48s ((const char*)List);} 00105 inline int64u LittleEndian2int48u (const int8u* List) {return LittleEndian2int48u ((const char*)List);} 00106 inline int64s LittleEndian2int56s (const int8u* List) {return LittleEndian2int56s ((const char*)List);} 00107 inline int64u LittleEndian2int56u (const int8u* List) {return LittleEndian2int56u ((const char*)List);} 00108 inline int64s LittleEndian2int64s (const int8u* List) {return LittleEndian2int64s ((const char*)List);} 00109 inline int64u LittleEndian2int64u (const int8u* List) {return LittleEndian2int64u ((const char*)List);} 00110 inline int128u LittleEndian2int128u (const int8u* List) {return LittleEndian2int64u ((const char*)List);} 00111 #endif 00112 inline float32 LittleEndian2float32 (const int8u* List) {return LittleEndian2float32 ((const char*)List);} 00113 inline float64 LittleEndian2float64 (const int8u* List) {return LittleEndian2float64 ((const char*)List);} 00114 inline float80 LittleEndian2float80 (const int8u* List) {return LittleEndian2float80 ((const char*)List);} 00115 00116 inline void int8s2LittleEndian (int8u* List, int8s Value) {return int8s2LittleEndian ((char*)List, Value);} 00117 inline void int8u2LittleEndian (int8u* List, int8u Value) {return int8u2LittleEndian ((char*)List, Value);} 00118 inline void int16s2LittleEndian (int8u* List, int16s Value) {return int16s2LittleEndian ((char*)List, Value);} 00119 inline void int16u2LittleEndian (int8u* List, int16u Value) {return int16u2LittleEndian ((char*)List, Value);} 00120 inline void int24s2LittleEndian (int8u* List, int32s Value) {return int24s2LittleEndian ((char*)List, Value);} 00121 inline void int24u2LittleEndian (int8u* List, int32u Value) {return int24u2LittleEndian ((char*)List, Value);} 00122 inline void int32s2LittleEndian (int8u* List, int32s Value) {return int32s2LittleEndian ((char*)List, Value);} 00123 inline void int32u2LittleEndian (int8u* List, int32u Value) {return int32u2LittleEndian ((char*)List, Value);} 00124 #if (MAXTYPE_INT >= 64) 00125 inline void int40s2LittleEndian (int8u* List, int64s Value) {return int40s2LittleEndian ((char*)List, Value);} 00126 inline void int40u2LittleEndian (int8u* List, int64u Value) {return int40u2LittleEndian ((char*)List, Value);} 00127 inline void int48s2LittleEndian (int8u* List, int64s Value) {return int48s2LittleEndian ((char*)List, Value);} 00128 inline void int48u2LittleEndian (int8u* List, int64u Value) {return int48u2LittleEndian ((char*)List, Value);} 00129 inline void int56s2LittleEndian (int8u* List, int64s Value) {return int56s2LittleEndian ((char*)List, Value);} 00130 inline void int56u2LittleEndian (int8u* List, int64u Value) {return int56u2LittleEndian ((char*)List, Value);} 00131 inline void int64s2LittleEndian (int8u* List, int64s Value) {return int64s2LittleEndian ((char*)List, Value);} 00132 inline void int64u2LittleEndian (int8u* List, int64u Value) {return int64u2LittleEndian ((char*)List, Value);} 00133 inline void int128u2LittleEndian (int8u* List, int128u Value) {return int128u2LittleEndian ((char*)List, Value);} 00134 #endif 00135 inline void float322LittleEndian (int8u* List, float32 Value) {return float322LittleEndian ((char*)List, Value);} 00136 inline void float642LittleEndian (int8u* List, float64 Value) {return float642LittleEndian ((char*)List, Value);} 00137 inline void float802LittleEndian (int8u* List, float80 Value) {return float802LittleEndian ((char*)List, Value);} 00138 #endif //__BORLANDC__ 00139 00140 //--------------------------------------------------------------------------- 00141 //Big Endians 00142 int8s BigEndian2int8s (const char* List); 00143 int8u BigEndian2int8u (const char* List); 00144 int16s BigEndian2int16s (const char* List); 00145 int16u BigEndian2int16u (const char* List); 00146 int32s BigEndian2int24s (const char* List); 00147 int32u BigEndian2int24u (const char* List); 00148 int32s BigEndian2int32s (const char* List); 00149 int32u BigEndian2int32u (const char* List); 00150 #if (MAXTYPE_INT >= 64) 00151 int64s BigEndian2int40s (const char* List); 00152 int64u BigEndian2int40u (const char* List); 00153 int64s BigEndian2int48s (const char* List); 00154 int64u BigEndian2int48u (const char* List); 00155 int64s BigEndian2int56s (const char* List); 00156 int64u BigEndian2int56u (const char* List); 00157 int64s BigEndian2int64s (const char* List); 00158 int64u BigEndian2int64u (const char* List); 00159 int128u BigEndian2int128u (const char* List); 00160 #endif 00161 float32 BigEndian2float32 (const char* List); 00162 float64 BigEndian2float64 (const char* List); 00163 float80 BigEndian2float80 (const char* List); 00164 00165 void int8s2BigEndian (char* List, int8s Value); 00166 void int8u2BigEndian (char* List, int8u Value); 00167 void int16s2BigEndian (char* List, int16s Value); 00168 void int16u2BigEndian (char* List, int16u Value); 00169 void int24s2BigEndian (char* List, int32s Value); 00170 void int24u2BigEndian (char* List, int32u Value); 00171 void int32s2BigEndian (char* List, int32s Value); 00172 void int32u2BigEndian (char* List, int32u Value); 00173 #if (MAXTYPE_INT >= 64) 00174 void int40s2BigEndian (char* List, int64s Value); 00175 void int40u2BigEndian (char* List, int64u Value); 00176 void int48s2BigEndian (char* List, int64s Value); 00177 void int48u2BigEndian (char* List, int64u Value); 00178 void int56s2BigEndian (char* List, int64s Value); 00179 void int56u2BigEndian (char* List, int64u Value); 00180 void int64s2BigEndian (char* List, int64s Value); 00181 void int64u2BigEndian (char* List, int64u Value); 00182 void int128u2BigEndian (char* List, int128u Value); 00183 #endif 00184 void float322BigEndian (char* List, float32 Value); 00185 void float642BigEndian (char* List, float64 Value); 00186 void float802BigEndian (char* List, float80 Value); 00187 00188 #ifndef __BORLANDC__ 00189 inline int8s BigEndian2int8s (const int8u* List) {return BigEndian2int8s ((const char*)List);} 00190 inline int8u BigEndian2int8u (const int8u* List) {return BigEndian2int8u ((const char*)List);} 00191 inline int16s BigEndian2int16s (const int8u* List) {return BigEndian2int16s ((const char*)List);} 00192 inline int16u BigEndian2int16u (const int8u* List) {return BigEndian2int16u ((const char*)List);} 00193 inline int32s BigEndian2int32s (const int8u* List) {return BigEndian2int32s ((const char*)List);} 00194 inline int32u BigEndian2int24u (const int8u* List) {return BigEndian2int24u ((const char*)List);} 00195 inline int32s BigEndian2int24s (const int8u* List) {return BigEndian2int24s ((const char*)List);} 00196 inline int32u BigEndian2int32u (const int8u* List) {return BigEndian2int32u ((const char*)List);} 00197 #if (MAXTYPE_INT >= 64) 00198 inline int64s BigEndian2int40s (const int8u* List) {return BigEndian2int40s ((const char*)List);} 00199 inline int64u BigEndian2int40u (const int8u* List) {return BigEndian2int40u ((const char*)List);} 00200 inline int64s BigEndian2int48s (const int8u* List) {return BigEndian2int48s ((const char*)List);} 00201 inline int64u BigEndian2int48u (const int8u* List) {return BigEndian2int48u ((const char*)List);} 00202 inline int64s BigEndian2int56s (const int8u* List) {return BigEndian2int56s ((const char*)List);} 00203 inline int64u BigEndian2int56u (const int8u* List) {return BigEndian2int56u ((const char*)List);} 00204 inline int64s BigEndian2int64s (const int8u* List) {return BigEndian2int64s ((const char*)List);} 00205 inline int64u BigEndian2int64u (const int8u* List) {return BigEndian2int64u ((const char*)List);} 00206 inline int128u BigEndian2int128u (const int8u* List) {return BigEndian2int128u ((const char*)List);} 00207 #endif 00208 inline float32 BigEndian2float32 (const int8u* List) {return BigEndian2float32 ((const char*)List);} 00209 inline float64 BigEndian2float64 (const int8u* List) {return BigEndian2float64 ((const char*)List);} 00210 inline float80 BigEndian2float80 (const int8u* List) {return BigEndian2float80 ((const char*)List);} 00211 00212 inline void int8s2BigEndian (int8u* List, int8s Value) {return int8s2BigEndian ((char*)List, Value);} 00213 inline void int8u2BigEndian (int8u* List, int8u Value) {return int8u2BigEndian ((char*)List, Value);} 00214 inline void int16s2BigEndian (int8u* List, int16s Value) {return int16s2BigEndian ((char*)List, Value);} 00215 inline void int16u2BigEndian (int8u* List, int16u Value) {return int16u2BigEndian ((char*)List, Value);} 00216 inline void int24s2BigEndian (int8u* List, int32s Value) {return int24s2BigEndian ((char*)List, Value);} 00217 inline void int24u2BigEndian (int8u* List, int32u Value) {return int24u2BigEndian ((char*)List, Value);} 00218 inline void int32s2BigEndian (int8u* List, int32s Value) {return int32s2BigEndian ((char*)List, Value);} 00219 inline void int32u2BigEndian (int8u* List, int32u Value) {return int32u2BigEndian ((char*)List, Value);} 00220 #if (MAXTYPE_INT >= 64) 00221 inline void int40s2BigEndian (int8u* List, int64s Value) {return int40s2BigEndian ((char*)List, Value);} 00222 inline void int40u2BigEndian (int8u* List, int64u Value) {return int40u2BigEndian ((char*)List, Value);} 00223 inline void int48s2BigEndian (int8u* List, int64s Value) {return int48s2BigEndian ((char*)List, Value);} 00224 inline void int48u2BigEndian (int8u* List, int64u Value) {return int48u2BigEndian ((char*)List, Value);} 00225 inline void int56s2BigEndian (int8u* List, int64s Value) {return int56s2BigEndian ((char*)List, Value);} 00226 inline void int56u2BigEndian (int8u* List, int64u Value) {return int56u2BigEndian ((char*)List, Value);} 00227 inline void int64s2BigEndian (int8u* List, int64s Value) {return int64s2BigEndian ((char*)List, Value);} 00228 inline void int64u2BigEndian (int8u* List, int64u Value) {return int64u2BigEndian ((char*)List, Value);} 00229 inline void int128u2BigEndian (int8u* List, int128u Value) {return int128u2BigEndian ((char*)List, Value);} 00230 #endif 00231 inline void float322BigEndian (int8u* List, float32 Value) {return float322BigEndian ((char*)List, Value);} 00232 inline void float642BigEndian (int8u* List, float64 Value) {return float642BigEndian ((char*)List, Value);} 00233 inline void float802BigEndian (int8u* List, float80 Value) {return float802BigEndian ((char*)List, Value);} 00234 #endif //__BORLANDC__ 00235 00236 //--------------------------------------------------------------------------- 00237 // int32 - int64 00238 int64s int32s_int64s ( int32s High, int32u Low); 00239 int64u int32u_int64u ( int32u High, int32u Low); 00240 void int32s_int64s (int64s &BigInt, int32s High, int32u Low); 00241 void int32u_int64u (int64s &BigInt, int32u High, int32u Low); 00242 void int64s_int32s (int64s BigInt, int32s &High, int32u &Low); 00243 void int64u_int32u (int64u BigInt, int32u &High, int32u &Low); 00244 00245 //--------------------------------------------------------------------------- 00246 // Floats and ints 00247 int32s float32_int32s (float32 F, bool Rounded=true); 00248 int64s float32_int64s (float32 F, bool Rounded=true); 00249 int32s float64_int32s (float64 F, bool Rounded=true); 00250 int64s float64_int64s (float64 F, bool Rounded=true); 00251 00252 // These functions are used because MSVC6 isn't able to convert an unsigned int64 to a floating-point value, and I couldn't think of a cleaner way to handle it. 00253 #if defined(_MSC_VER) && _MSC_VER<=1200 00254 inline float32 int64u_float32 (int64u v) {return static_cast<float32>(static_cast<int64s>(v>>1))*2.0f + static_cast<float32>(static_cast<int64s>(v & 1));} 00255 inline float64 int64u_float64 (int64u v) {return static_cast<float64>(static_cast<int64s>(v>>1))*2.0f + static_cast<float32>(static_cast<int64s>(v & 1));} 00256 #else 00257 inline float32 int64u_float32 (int64u v) {return (float32)v;} 00258 inline float64 int64u_float64 (int64u v) {return (float64)v;} 00259 #endif // defined(_MSC_VER) && _MSC_VER<=1200 00260 00261 //--------------------------------------------------------------------------- 00262 // CC (often used in all containers to identify a stream 00263 inline int64u CC8(const char* C) {return BigEndian2int64u(C);} 00264 inline int64u CC7(const char* C) {return BigEndian2int56u(C);} 00265 inline int64u CC6(const char* C) {return BigEndian2int48u(C);} 00266 inline int64u CC5(const char* C) {return BigEndian2int40u(C);} 00267 inline int32u CC4(const char* C) {return BigEndian2int32u(C);} 00268 inline int32u CC3(const char* C) {return BigEndian2int24u(C);} 00269 inline int16u CC2(const char* C) {return BigEndian2int16u(C);} 00270 inline int8u CC1(const char* C) {return BigEndian2int8u (C);} 00271 #ifndef __BORLANDC__ 00272 inline int64u CC8(const int8u* C) {return BigEndian2int64u(C);} 00273 inline int64u CC7(const int8u* C) {return BigEndian2int56u(C);} 00274 inline int64u CC6(const int8u* C) {return BigEndian2int48u(C);} 00275 inline int64u CC5(const int8u* C) {return BigEndian2int40u(C);} 00276 inline int32u CC4(const int8u* C) {return BigEndian2int32u(C);} 00277 inline int32u CC3(const int8u* C) {return BigEndian2int24u(C);} 00278 inline int16u CC2(const int8u* C) {return BigEndian2int16u(C);} 00279 inline int8u CC1(const int8u* C) {return BigEndian2int8u (C);} 00280 #endif // __BORLANDC__ 00281 00282 //--------------------------------------------------------------------------- 00283 // turn a numeric literal into a hex constant 00284 // (avoids problems with leading zeroes) 00285 // 8-bit constants max value 0x11111111, always fits in unsigned long 00286 #define HEX__(n) 0x##n##LU 00287 00288 // 8-bit conversion function 00289 #define B8__(x) ((x&0x0000000FLU)?0x01:0) \ 00290 +((x&0x000000F0LU)?0x02:0) \ 00291 +((x&0x00000F00LU)?0x04:0) \ 00292 +((x&0x0000F000LU)?0x08:0) \ 00293 +((x&0x000F0000LU)?0x10:0) \ 00294 +((x&0x00F00000LU)?0x20:0) \ 00295 +((x&0x0F000000LU)?0x40:0) \ 00296 +((x&0xF0000000LU)?0x80:0) 00297 00298 // for upto 8-bit binary constants 00299 #define B8(d) ((int8u)B8__(HEX__(d))) 00300 00301 // for upto 16-bit binary constants, MSB first 00302 #define B16(dmsb, dlsb) (((int16u)B8(dmsb)<<8) \ 00303 + ((int16u)B8(dlsb)<<0)) 00304 00305 // for upto 32-bit binary constants, MSB first 00306 #define B32(dmsb, db2, db3, dlsb) (((int32u)B8(dmsb)<<24) \ 00307 + ((int32u)B8( db2)<<16) \ 00308 + ((int32u)B8( db3)<< 8) \ 00309 + ((int32u)B8(dlsb)<< 0)) 00310 00311 } //namespace ZenLib 00312 #endif