• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

Utils.h

Go to the documentation of this file.
00001 // ZenLib::Utils - Very small utilities
00002 // Copyright (C) 2002-2011 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 LittleEndian2float16 (const char* List);
00066 float32 LittleEndian2float32 (const char* List);
00067 float64 LittleEndian2float64 (const char* List);
00068 float80 LittleEndian2float80 (const char* List);
00069 
00070 void   int8s2LittleEndian     (char* List, int8s  Value);
00071 void   int8u2LittleEndian     (char* List, int8u  Value);
00072 void   int16s2LittleEndian    (char* List, int16s Value);
00073 void   int16u2LittleEndian    (char* List, int16u Value);
00074 void   int24s2LittleEndian    (char* List, int32s Value);
00075 void   int24u2LittleEndian    (char* List, int32u Value);
00076 void   int32s2LittleEndian    (char* List, int32s Value);
00077 void   int32u2LittleEndian    (char* List, int32u Value);
00078 #if (MAXTYPE_INT >= 64)
00079 void   int40s2LittleEndian    (char* List, int64s Value);
00080 void   int40u2LittleEndian    (char* List, int64u Value);
00081 void   int48s2LittleEndian    (char* List, int64s Value);
00082 void   int48u2LittleEndian    (char* List, int64u Value);
00083 void   int56s2LittleEndian    (char* List, int64s Value);
00084 void   int56u2LittleEndian    (char* List, int64u Value);
00085 void   int64s2LittleEndian    (char* List, int64s Value);
00086 void   int64u2LittleEndian    (char* List, int64u Value);
00087 void   int128u2LittleEndian   (char* List, int128u Value);
00088 #endif
00089 void   float162LittleEndian   (char* List, float32 Value);
00090 void   float322LittleEndian   (char* List, float32 Value);
00091 void   float642LittleEndian   (char* List, float64 Value);
00092 void   float802LittleEndian   (char* List, float80 Value);
00093 
00094 #ifndef __BORLANDC__
00095 inline int8s  LittleEndian2int8s  (const int8u* List) {return LittleEndian2int8s  ((const char*)List);}
00096 inline int8u  LittleEndian2int8u  (const int8u* List) {return LittleEndian2int8u  ((const char*)List);}
00097 inline int16s LittleEndian2int16s (const int8u* List) {return LittleEndian2int16s ((const char*)List);}
00098 inline int16u LittleEndian2int16u (const int8u* List) {return LittleEndian2int16u ((const char*)List);}
00099 inline int32s LittleEndian2int24s (const int8u* List) {return LittleEndian2int24s ((const char*)List);}
00100 inline int32u LittleEndian2int24u (const int8u* List) {return LittleEndian2int24u ((const char*)List);}
00101 inline int32s LittleEndian2int32s (const int8u* List) {return LittleEndian2int32s ((const char*)List);}
00102 inline int32u LittleEndian2int32u (const int8u* List) {return LittleEndian2int32u ((const char*)List);}
00103 #if (MAXTYPE_INT >= 64)
00104 inline int64s LittleEndian2int40s (const int8u* List) {return LittleEndian2int40s ((const char*)List);}
00105 inline int64u LittleEndian2int40u (const int8u* List) {return LittleEndian2int40u ((const char*)List);}
00106 inline int64s LittleEndian2int48s (const int8u* List) {return LittleEndian2int48s ((const char*)List);}
00107 inline int64u LittleEndian2int48u (const int8u* List) {return LittleEndian2int48u ((const char*)List);}
00108 inline int64s LittleEndian2int56s (const int8u* List) {return LittleEndian2int56s ((const char*)List);}
00109 inline int64u LittleEndian2int56u (const int8u* List) {return LittleEndian2int56u ((const char*)List);}
00110 inline int64s LittleEndian2int64s (const int8u* List) {return LittleEndian2int64s ((const char*)List);}
00111 inline int64u LittleEndian2int64u (const int8u* List) {return LittleEndian2int64u ((const char*)List);}
00112 inline int128u LittleEndian2int128u (const int8u* List) {return LittleEndian2int64u ((const char*)List);}
00113 #endif
00114 inline float32 LittleEndian2float16 (const int8u* List) {return LittleEndian2float16 ((const char*)List);}
00115 inline float32 LittleEndian2float32 (const int8u* List) {return LittleEndian2float32 ((const char*)List);}
00116 inline float64 LittleEndian2float64 (const int8u* List) {return LittleEndian2float64 ((const char*)List);}
00117 inline float80 LittleEndian2float80 (const int8u* List) {return LittleEndian2float80 ((const char*)List);}
00118 
00119 inline void   int8s2LittleEndian     (int8u* List, int8s  Value) {return int8s2LittleEndian    ((char*)List, Value);}
00120 inline void   int8u2LittleEndian     (int8u* List, int8u  Value) {return int8u2LittleEndian    ((char*)List, Value);}
00121 inline void   int16s2LittleEndian    (int8u* List, int16s Value) {return int16s2LittleEndian   ((char*)List, Value);}
00122 inline void   int16u2LittleEndian    (int8u* List, int16u Value) {return int16u2LittleEndian   ((char*)List, Value);}
00123 inline void   int24s2LittleEndian    (int8u* List, int32s Value) {return int24s2LittleEndian   ((char*)List, Value);}
00124 inline void   int24u2LittleEndian    (int8u* List, int32u Value) {return int24u2LittleEndian   ((char*)List, Value);}
00125 inline void   int32s2LittleEndian    (int8u* List, int32s Value) {return int32s2LittleEndian   ((char*)List, Value);}
00126 inline void   int32u2LittleEndian    (int8u* List, int32u Value) {return int32u2LittleEndian   ((char*)List, Value);}
00127 #if (MAXTYPE_INT >= 64)
00128 inline void   int40s2LittleEndian    (int8u* List, int64s Value) {return int40s2LittleEndian   ((char*)List, Value);}
00129 inline void   int40u2LittleEndian    (int8u* List, int64u Value) {return int40u2LittleEndian   ((char*)List, Value);}
00130 inline void   int48s2LittleEndian    (int8u* List, int64s Value) {return int48s2LittleEndian   ((char*)List, Value);}
00131 inline void   int48u2LittleEndian    (int8u* List, int64u Value) {return int48u2LittleEndian   ((char*)List, Value);}
00132 inline void   int56s2LittleEndian    (int8u* List, int64s Value) {return int56s2LittleEndian   ((char*)List, Value);}
00133 inline void   int56u2LittleEndian    (int8u* List, int64u Value) {return int56u2LittleEndian   ((char*)List, Value);}
00134 inline void   int64s2LittleEndian    (int8u* List, int64s Value) {return int64s2LittleEndian   ((char*)List, Value);}
00135 inline void   int64u2LittleEndian    (int8u* List, int64u Value) {return int64u2LittleEndian   ((char*)List, Value);}
00136 inline void   int128u2LittleEndian   (int8u* List, int128u Value) {return int128u2LittleEndian ((char*)List, Value);}
00137 #endif
00138 inline void   float162LittleEndian   (int8u* List, float32 Value) {return float162LittleEndian ((char*)List, Value);}
00139 inline void   float322LittleEndian   (int8u* List, float32 Value) {return float322LittleEndian ((char*)List, Value);}
00140 inline void   float642LittleEndian   (int8u* List, float64 Value) {return float642LittleEndian ((char*)List, Value);}
00141 inline void   float802LittleEndian   (int8u* List, float80 Value) {return float802LittleEndian ((char*)List, Value);}
00142 #endif //__BORLANDC__
00143 
00144 //---------------------------------------------------------------------------
00145 //Big Endians
00146 int8s  BigEndian2int8s     (const char* List);
00147 int8u  BigEndian2int8u     (const char* List);
00148 int16s BigEndian2int16s    (const char* List);
00149 int16u BigEndian2int16u    (const char* List);
00150 int32s BigEndian2int24s    (const char* List);
00151 int32u BigEndian2int24u    (const char* List);
00152 int32s BigEndian2int32s    (const char* List);
00153 int32u BigEndian2int32u    (const char* List);
00154 #if (MAXTYPE_INT >= 64)
00155 int64s BigEndian2int40s    (const char* List);
00156 int64u BigEndian2int40u    (const char* List);
00157 int64s BigEndian2int48s    (const char* List);
00158 int64u BigEndian2int48u    (const char* List);
00159 int64s BigEndian2int56s    (const char* List);
00160 int64u BigEndian2int56u    (const char* List);
00161 int64s BigEndian2int64s    (const char* List);
00162 int64u BigEndian2int64u    (const char* List);
00163 int128u BigEndian2int128u  (const char* List);
00164 #endif
00165 float32 BigEndian2float16  (const char* List);
00166 float32 BigEndian2float32  (const char* List);
00167 float64 BigEndian2float64  (const char* List);
00168 float80 BigEndian2float80  (const char* List);
00169 
00170 void   int8s2BigEndian     (char* List, int8s  Value);
00171 void   int8u2BigEndian     (char* List, int8u  Value);
00172 void   int16s2BigEndian    (char* List, int16s Value);
00173 void   int16u2BigEndian    (char* List, int16u Value);
00174 void   int24s2BigEndian    (char* List, int32s Value);
00175 void   int24u2BigEndian    (char* List, int32u Value);
00176 void   int32s2BigEndian    (char* List, int32s Value);
00177 void   int32u2BigEndian    (char* List, int32u Value);
00178 #if (MAXTYPE_INT >= 64)
00179 void   int40s2BigEndian    (char* List, int64s Value);
00180 void   int40u2BigEndian    (char* List, int64u Value);
00181 void   int48s2BigEndian    (char* List, int64s Value);
00182 void   int48u2BigEndian    (char* List, int64u Value);
00183 void   int56s2BigEndian    (char* List, int64s Value);
00184 void   int56u2BigEndian    (char* List, int64u Value);
00185 void   int64s2BigEndian    (char* List, int64s Value);
00186 void   int64u2BigEndian    (char* List, int64u Value);
00187 void   int128u2BigEndian   (char* List, int128u Value);
00188 #endif
00189 void   float162BigEndian   (char* List, float32 Value);
00190 void   float322BigEndian   (char* List, float32 Value);
00191 void   float642BigEndian   (char* List, float64 Value);
00192 void   float802BigEndian   (char* List, float80 Value);
00193 
00194 #ifndef __BORLANDC__
00195 inline int8s  BigEndian2int8s     (const int8u* List) {return BigEndian2int8s     ((const char*)List);}
00196 inline int8u  BigEndian2int8u     (const int8u* List) {return BigEndian2int8u     ((const char*)List);}
00197 inline int16s BigEndian2int16s    (const int8u* List) {return BigEndian2int16s    ((const char*)List);}
00198 inline int16u BigEndian2int16u    (const int8u* List) {return BigEndian2int16u    ((const char*)List);}
00199 inline int32s BigEndian2int32s    (const int8u* List) {return BigEndian2int32s    ((const char*)List);}
00200 inline int32u BigEndian2int24u    (const int8u* List) {return BigEndian2int24u    ((const char*)List);}
00201 inline int32s BigEndian2int24s    (const int8u* List) {return BigEndian2int24s    ((const char*)List);}
00202 inline int32u BigEndian2int32u    (const int8u* List) {return BigEndian2int32u    ((const char*)List);}
00203 #if (MAXTYPE_INT >= 64)
00204 inline int64s BigEndian2int40s    (const int8u* List) {return BigEndian2int40s    ((const char*)List);}
00205 inline int64u BigEndian2int40u    (const int8u* List) {return BigEndian2int40u    ((const char*)List);}
00206 inline int64s BigEndian2int48s    (const int8u* List) {return BigEndian2int48s    ((const char*)List);}
00207 inline int64u BigEndian2int48u    (const int8u* List) {return BigEndian2int48u    ((const char*)List);}
00208 inline int64s BigEndian2int56s    (const int8u* List) {return BigEndian2int56s    ((const char*)List);}
00209 inline int64u BigEndian2int56u    (const int8u* List) {return BigEndian2int56u    ((const char*)List);}
00210 inline int64s BigEndian2int64s    (const int8u* List) {return BigEndian2int64s    ((const char*)List);}
00211 inline int64u BigEndian2int64u    (const int8u* List) {return BigEndian2int64u    ((const char*)List);}
00212 inline int128u BigEndian2int128u  (const int8u* List) {return BigEndian2int128u   ((const char*)List);}
00213 #endif
00214 inline float32 BigEndian2float16  (const int8u* List) {return BigEndian2float16   ((const char*)List);}
00215 inline float32 BigEndian2float32  (const int8u* List) {return BigEndian2float32   ((const char*)List);}
00216 inline float64 BigEndian2float64  (const int8u* List) {return BigEndian2float64   ((const char*)List);}
00217 inline float80 BigEndian2float80  (const int8u* List) {return BigEndian2float80   ((const char*)List);}
00218 
00219 inline void   int8s2BigEndian     (int8u* List, int8s  Value) {return int8s2BigEndian    ((char*)List, Value);}
00220 inline void   int8u2BigEndian     (int8u* List, int8u  Value) {return int8u2BigEndian    ((char*)List, Value);}
00221 inline void   int16s2BigEndian    (int8u* List, int16s Value) {return int16s2BigEndian   ((char*)List, Value);}
00222 inline void   int16u2BigEndian    (int8u* List, int16u Value) {return int16u2BigEndian   ((char*)List, Value);}
00223 inline void   int24s2BigEndian    (int8u* List, int32s Value) {return int24s2BigEndian   ((char*)List, Value);}
00224 inline void   int24u2BigEndian    (int8u* List, int32u Value) {return int24u2BigEndian   ((char*)List, Value);}
00225 inline void   int32s2BigEndian    (int8u* List, int32s Value) {return int32s2BigEndian   ((char*)List, Value);}
00226 inline void   int32u2BigEndian    (int8u* List, int32u Value) {return int32u2BigEndian   ((char*)List, Value);}
00227 #if (MAXTYPE_INT >= 64)
00228 inline void   int40s2BigEndian    (int8u* List, int64s Value) {return int40s2BigEndian   ((char*)List, Value);}
00229 inline void   int40u2BigEndian    (int8u* List, int64u Value) {return int40u2BigEndian   ((char*)List, Value);}
00230 inline void   int48s2BigEndian    (int8u* List, int64s Value) {return int48s2BigEndian   ((char*)List, Value);}
00231 inline void   int48u2BigEndian    (int8u* List, int64u Value) {return int48u2BigEndian   ((char*)List, Value);}
00232 inline void   int56s2BigEndian    (int8u* List, int64s Value) {return int56s2BigEndian   ((char*)List, Value);}
00233 inline void   int56u2BigEndian    (int8u* List, int64u Value) {return int56u2BigEndian   ((char*)List, Value);}
00234 inline void   int64s2BigEndian    (int8u* List, int64s Value) {return int64s2BigEndian   ((char*)List, Value);}
00235 inline void   int64u2BigEndian    (int8u* List, int64u Value) {return int64u2BigEndian   ((char*)List, Value);}
00236 inline void   int128u2BigEndian   (int8u* List, int128u Value) {return int128u2BigEndian ((char*)List, Value);}
00237 #endif
00238 inline void   float162BigEndian   (int8u* List, float32 Value) {return float162BigEndian ((char*)List, Value);}
00239 inline void   float322BigEndian   (int8u* List, float32 Value) {return float322BigEndian ((char*)List, Value);}
00240 inline void   float642BigEndian   (int8u* List, float64 Value) {return float642BigEndian ((char*)List, Value);}
00241 inline void   float802BigEndian   (int8u* List, float80 Value) {return float802BigEndian ((char*)List, Value);}
00242 #endif //__BORLANDC__
00243 
00244 //---------------------------------------------------------------------------
00245 // int32 - int64
00246 int64s int32s_int64s (                int32s  High, int32u  Low);
00247 int64u int32u_int64u (                int32u  High, int32u  Low);
00248 void   int32s_int64s (int64s &BigInt, int32s  High, int32u  Low);
00249 void   int32u_int64u (int64s &BigInt, int32u  High, int32u  Low);
00250 void   int64s_int32s (int64s  BigInt, int32s &High, int32u &Low);
00251 void   int64u_int32u (int64u  BigInt, int32u &High, int32u &Low);
00252 
00253 //---------------------------------------------------------------------------
00254 // Floats and ints
00255 int32s float32_int32s  (float32 F, bool Rounded=true);
00256 int64s float32_int64s  (float32 F, bool Rounded=true);
00257 int32s float64_int32s  (float64 F, bool Rounded=true);
00258 int64s float64_int64s  (float64 F, bool Rounded=true);
00259 
00260 // 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.
00261 #if defined(_MSC_VER) && _MSC_VER<=1200
00262     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));}
00263     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));}
00264 #else
00265     inline float32 int64u_float32 (int64u v) {return (float32)v;}
00266     inline float64 int64u_float64 (int64u v) {return (float64)v;}
00267 #endif // defined(_MSC_VER) && _MSC_VER<=1200
00268 
00269 //---------------------------------------------------------------------------
00270 // CC (often used in all containers to identify a stream
00271 inline int64u CC8(const char*  C) {return BigEndian2int64u(C);}
00272 inline int64u CC7(const char*  C) {return BigEndian2int56u(C);}
00273 inline int64u CC6(const char*  C) {return BigEndian2int48u(C);}
00274 inline int64u CC5(const char*  C) {return BigEndian2int40u(C);}
00275 inline int32u CC4(const char*  C) {return BigEndian2int32u(C);}
00276 inline int32u CC3(const char*  C) {return BigEndian2int24u(C);}
00277 inline int16u CC2(const char*  C) {return BigEndian2int16u(C);}
00278 inline int8u  CC1(const char*  C) {return BigEndian2int8u (C);}
00279 #ifndef __BORLANDC__
00280 inline int64u CC8(const int8u* C) {return BigEndian2int64u(C);}
00281 inline int64u CC7(const int8u* C) {return BigEndian2int56u(C);}
00282 inline int64u CC6(const int8u* C) {return BigEndian2int48u(C);}
00283 inline int64u CC5(const int8u* C) {return BigEndian2int40u(C);}
00284 inline int32u CC4(const int8u* C) {return BigEndian2int32u(C);}
00285 inline int32u CC3(const int8u* C) {return BigEndian2int24u(C);}
00286 inline int16u CC2(const int8u* C) {return BigEndian2int16u(C);}
00287 inline int8u  CC1(const int8u* C) {return BigEndian2int8u (C);}
00288 #endif // __BORLANDC__
00289 
00290 //---------------------------------------------------------------------------
00291 // turn a numeric literal into a hex constant
00292 // (avoids problems with leading zeroes)
00293 // 8-bit constants max value 0x11111111, always fits in unsigned long
00294 #define HEX__(n) 0x##n##LU
00295 
00296 // 8-bit conversion function
00297 #define B8__(x) ((x&0x0000000FLU)?0x01:0) \
00298                +((x&0x000000F0LU)?0x02:0) \
00299                +((x&0x00000F00LU)?0x04:0) \
00300                +((x&0x0000F000LU)?0x08:0) \
00301                +((x&0x000F0000LU)?0x10:0) \
00302                +((x&0x00F00000LU)?0x20:0) \
00303                +((x&0x0F000000LU)?0x40:0) \
00304                +((x&0xF0000000LU)?0x80:0)
00305 
00306 // for upto 8-bit binary constants
00307 #define B8(d) ((int8u)B8__(HEX__(d)))
00308 
00309 // for upto 16-bit binary constants, MSB first
00310 #define B16(dmsb, dlsb) (((int16u)B8(dmsb)<<8) \
00311                        + ((int16u)B8(dlsb)<<0))
00312 
00313 // for upto 32-bit binary constants, MSB first
00314 #define B32(dmsb, db2, db3, dlsb) (((int32u)B8(dmsb)<<24) \
00315                                  + ((int32u)B8( db2)<<16) \
00316                                  + ((int32u)B8( db3)<< 8) \
00317                                  + ((int32u)B8(dlsb)<< 0))
00318 
00319 } //namespace ZenLib
00320 #endif

Generated on Fri Sep 23 2011 11:36:55 for ZenLib by  doxygen 1.7.1