Utils.h

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

Generated on Fri Mar 20 09:24:08 2009 for ZenLib by  doxygen 1.4.7