ML Reference
MeVis/Foundation/Sources/MLUtilities/mlTypeDefTraits.h
Go to the documentation of this file.
00001 // **InsertLicense** code
00002 //----------------------------------------------------------------------------------
00004 
00009 //----------------------------------------------------------------------------------
00010 #ifndef __mlTypeDefTraits_H
00011 #define __mlTypeDefTraits_H
00012 
00014 #ifndef __mlTypeDefs_H
00015 #include "mlTypeDefs.h"
00016 #endif
00017 
00018 #include "mlSystemWarningsDisable.h"
00019 
00020 #if defined(WIN32)
00021 # include <float.h>
00022 # define __ML_1ST_WINDOWS_2ND_UNIX_FUNCTION(__ml_win_func,__ml_unix_func)  __ml_win_func
00023 #else
00024 # include <cmath>
00025 # define __ML_1ST_WINDOWS_2ND_UNIX_FUNCTION(__ml_win_func,__ml_unix_func)  __ml_unix_func
00026 #endif
00027 
00028 #include <limits>
00029 #include <boost/static_assert.hpp>
00030 
00031 #include "mlSystemWarningsRestore.h"
00032 
00035 
00036   inline MLint8    MLTypeRangeMax(MLint8   )                  { return  ML_INT8_MAX   ; }
00037   inline MLuint8   MLTypeRangeMax(MLuint8  )                  { return  ML_UINT8_MAX  ; }
00038   inline MLint16   MLTypeRangeMax(MLint16  )                  { return  ML_INT16_MAX  ; }
00039   inline MLuint16  MLTypeRangeMax(MLuint16 )                  { return  ML_UINT16_MAX ; }
00040   inline MLint32   MLTypeRangeMax(MLint32  )                  { return  ML_INT32_MAX  ; }
00041   inline MLuint32  MLTypeRangeMax(MLuint32 )                  { return  ML_UINT32_MAX ; }
00042   inline MLfloat   MLTypeRangeMax(MLfloat  )                  { return  ML_FLOAT_MAX  ; }
00043   inline MLdouble  MLTypeRangeMax(MLdouble )                  { return  ML_DOUBLE_MAX ; }
00044   inline MLldouble MLTypeRangeMax(MLldouble)                  { return  ML_LDOUBLE_MAX; }
00045   inline MLuint64  MLTypeRangeMax(MLuint64 )                  { return  ML_UINT64_MAX ; }
00046   inline MLint64   MLTypeRangeMax(MLint64  )                  { return  ML_INT64_MAX  ; }
00048 
00051 
00052   inline MLint8    MLTypeRangeMin(MLint8   )                  { return  ML_INT8_MIN   ; }
00053   inline MLuint8   MLTypeRangeMin(MLuint8  )                  { return  ML_UINT8_MIN  ; }
00054   inline MLint16   MLTypeRangeMin(MLint16  )                  { return  ML_INT16_MIN  ; }
00055   inline MLuint16  MLTypeRangeMin(MLuint16 )                  { return  ML_UINT16_MIN ; }
00056   inline MLint32   MLTypeRangeMin(MLint32  )                  { return  ML_INT32_MIN  ; }
00057   inline MLuint32  MLTypeRangeMin(MLuint32 )                  { return  ML_UINT32_MIN ; }
00058   inline MLfloat   MLTypeRangeMin(MLfloat  )                  { return -ML_FLOAT_MAX  ; }
00059   inline MLdouble  MLTypeRangeMin(MLdouble )                  { return -ML_DOUBLE_MAX ; }
00060   inline MLldouble MLTypeRangeMin(MLldouble)                  { return -ML_LDOUBLE_MAX; }
00061   inline MLuint64  MLTypeRangeMin(MLuint64 )                  { return  ML_UINT64_MIN ; }
00062   inline MLint64   MLTypeRangeMin(MLint64  )                  { return  ML_INT64_MIN  ; }
00064 
00067 
00068   inline MLint8    MLTypeMinDifference(MLint8   )             { return  1                 ; }
00069   inline MLuint8   MLTypeMinDifference(MLuint8  )             { return  1                 ; }
00070   inline MLint16   MLTypeMinDifference(MLint16  )             { return  1                 ; }
00071   inline MLuint16  MLTypeMinDifference(MLuint16 )             { return  1                 ; }
00072   inline MLint32   MLTypeMinDifference(MLint32  )             { return  1                 ; }
00073   inline MLuint32  MLTypeMinDifference(MLuint32 )             { return  1                 ; }
00074   inline MLfloat   MLTypeMinDifference(MLfloat  )             { return  ML_FLOAT_EPSILON  ; }
00075   inline MLdouble  MLTypeMinDifference(MLdouble )             { return  ML_DOUBLE_EPSILON ; }
00076   inline MLldouble MLTypeMinDifference(MLldouble)             { return  ML_LDOUBLE_EPSILON; }
00077   inline MLuint64  MLTypeMinDifference(MLuint64 )             { return  1                 ; }
00078   inline MLint64   MLTypeMinDifference(MLint64  )             { return  1                 ; }
00080 
00083 
00084   inline MLfloat   MLfabs(MLfloat   v){ return fabsf(v); }
00085   inline MLdouble  MLfabs(MLdouble  v){ return fabs (v); }
00086   inline MLldouble MLfabs(MLldouble v){ return fabsl(v); }
00088 
00091   inline int MLisfinite(MLdouble v){ return __ML_1ST_WINDOWS_2ND_UNIX_FUNCTION( _finite(v) , std::isfinite(v) ); }
00092 
00095   inline int MLisnan(MLdouble v){ return __ML_1ST_WINDOWS_2ND_UNIX_FUNCTION( _isnan(v) , std::isnan(v) ); }
00096 
00100   template <typename DT>
00101     inline DT MLAbs(const DT val){ return (val < 0) ? -val : val; }
00102 
00104   template<>
00105     inline MLuint8  MLAbs(const MLuint8  val){ return val; }
00106   template<>
00107     inline MLuint16 MLAbs(const MLuint16 val){ return val; }
00108   template<>
00109     inline MLuint32 MLAbs(const MLuint32 val){ return val; }
00110   template<>
00111     inline MLuint64 MLAbs(const MLuint64 val){ return val; }
00112 
00113   // Prevent usage of abs() for floating point types, which will result in miscalculations
00114   // on UNIX systems due to stdlib.h 'int abs(int)'
00115   namespace ML_NAMESPACE {
00116     template <typename DT>
00117     inline DT abs (DT val) {
00118       // If you come here because of a compiler error, please replace abs() by MLAbs(),
00119       // MLfabs, or std::abs(). The usage of abs() will result in calculation errors!
00120       BOOST_STATIC_ASSERT(std::numeric_limits<DT>::is_integer);
00121       return (val < 0) ? -val : val;
00122     }
00123   }
00124 
00140   template <typename T>
00141   inline bool MLFloatValuesEqual(const T a, const T b, const T m)
00142   {
00143     return MLfabs(a - b) < MLTypeMinDifference(a)*m;
00144   }
00145 
00148 
00149   inline bool      MLValuesAreEqual(MLint8    a, MLint8    b, MLint8    /*m*/) { return a == b; }
00150   inline bool      MLValuesAreEqual(MLuint8   a, MLuint8   b, MLuint8   /*m*/) { return a == b; }
00151   inline bool      MLValuesAreEqual(MLint16   a, MLint16   b, MLint16   /*m*/) { return a == b; }
00152   inline bool      MLValuesAreEqual(MLuint16  a, MLuint16  b, MLuint16  /*m*/) { return a == b; }
00153   inline bool      MLValuesAreEqual(MLint32   a, MLint32   b, MLint32   /*m*/) { return a == b; }
00154   inline bool      MLValuesAreEqual(MLuint32  a, MLuint32  b, MLuint32  /*m*/) { return a == b; }
00155   inline bool      MLValuesAreEqual(MLfloat   a, MLfloat   b, MLfloat       m) { return MLFloatValuesEqual(a, b, m); }
00156   inline bool      MLValuesAreEqual(MLdouble  a, MLdouble  b, MLdouble      m) { return MLFloatValuesEqual(a, b, m); }
00157   inline bool      MLValuesAreEqual(MLldouble a, MLldouble b, MLldouble     m) { return MLFloatValuesEqual(a, b, m); }
00158   inline bool      MLValuesAreEqual(MLuint64  a, MLuint64  b, MLuint64  /*m*/) { return a == b; }
00159   inline bool      MLValuesAreEqual(MLint64   a, MLint64   b, MLint64   /*m*/) { return a == b; }
00160 
00161   // For all other cases.
00162   template <typename T1, typename T2>
00163   inline bool      MLValuesAreEqual(T1        a, T2        b, MLint64   /*m*/) { return a == b; }
00165 
00168 
00169   inline bool      MLValuesDiffer(MLint8    a, MLint8    b, MLint8     /*m*/)  { return a != b; }
00170   inline bool      MLValuesDiffer(MLuint8   a, MLuint8   b, MLuint8    /*m*/)  { return a != b; }
00171   inline bool      MLValuesDiffer(MLint16   a, MLint16   b, MLint16    /*m*/)  { return a != b; }
00172   inline bool      MLValuesDiffer(MLuint16  a, MLuint16  b, MLuint16   /*m*/)  { return a != b; }
00173   inline bool      MLValuesDiffer(MLint32   a, MLint32   b, MLint32    /*m*/)  { return a != b; }
00174   inline bool      MLValuesDiffer(MLuint32  a, MLuint32  b, MLuint32   /*m*/)  { return a != b; }
00175   inline bool      MLValuesDiffer(MLfloat   a, MLfloat   b, MLfloat        m)  { return !MLFloatValuesEqual(a, b, m); }
00176   inline bool      MLValuesDiffer(MLdouble  a, MLdouble  b, MLdouble       m)  { return !MLFloatValuesEqual(a, b, m); }
00177   inline bool      MLValuesDiffer(MLldouble a, MLldouble b, MLldouble      m)  { return !MLFloatValuesEqual(a, b, m); }
00178   inline bool      MLValuesDiffer(MLuint64  a, MLuint64  b, MLuint64   /*m*/)  { return a != b; }
00179   inline bool      MLValuesDiffer(MLint64   a, MLint64   b, MLint64    /*m*/)  { return a != b; }
00180 
00181   // For all other cases.
00182   template <typename T1, typename T2>
00183   inline bool      MLValuesDiffer(T1        a, T2        b, MLint64    /*m*/)  { return a != b; }
00185 
00188 
00189   inline bool      MLValuesAreEqualWOM(MLint8    a, MLint8    b) { return a == b; }
00190   inline bool      MLValuesAreEqualWOM(MLuint8   a, MLuint8   b) { return a == b; }
00191   inline bool      MLValuesAreEqualWOM(MLint16   a, MLint16   b) { return a == b; }
00192   inline bool      MLValuesAreEqualWOM(MLuint16  a, MLuint16  b) { return a == b; }
00193   inline bool      MLValuesAreEqualWOM(MLint32   a, MLint32   b) { return a == b; }
00194   inline bool      MLValuesAreEqualWOM(MLuint32  a, MLuint32  b) { return a == b; }
00195   inline bool      MLValuesAreEqualWOM(MLfloat   a, MLfloat   b) { return MLfabs(a -b) < ML_FLOAT_MIN  ; }
00196   inline bool      MLValuesAreEqualWOM(MLdouble  a, MLdouble  b) { return MLfabs(a -b) < ML_DOUBLE_MIN ; }
00197   inline bool      MLValuesAreEqualWOM(MLldouble a, MLldouble b) { return MLfabs(a -b) < ML_LDOUBLE_MIN; }
00198   inline bool      MLValuesAreEqualWOM(MLuint64  a, MLuint64  b) { return a == b; }
00199   inline bool      MLValuesAreEqualWOM(MLint64   a, MLint64   b) { return a == b; }
00200 
00201   // For all other cases.
00202   template <typename T1, typename T2>
00203   inline bool      MLValuesAreEqualWOM(T1        a, T2        b) { return a == b; }
00205 
00208 
00209   inline bool      MLValuesDifferWOM(MLint8    a, MLint8    b)  { return a != b; }
00210   inline bool      MLValuesDifferWOM(MLuint8   a, MLuint8   b)  { return a != b; }
00211   inline bool      MLValuesDifferWOM(MLint16   a, MLint16   b)  { return a != b; }
00212   inline bool      MLValuesDifferWOM(MLuint16  a, MLuint16  b)  { return a != b; }
00213   inline bool      MLValuesDifferWOM(MLint32   a, MLint32   b)  { return a != b; }
00214   inline bool      MLValuesDifferWOM(MLuint32  a, MLuint32  b)  { return a != b; }
00215   inline bool      MLValuesDifferWOM(MLfloat   a, MLfloat   b)  { return MLfabs(a -b) >= ML_FLOAT_MIN  ; }
00216   inline bool      MLValuesDifferWOM(MLdouble  a, MLdouble  b)  { return MLfabs(a -b) >= ML_DOUBLE_MIN ; }
00217   inline bool      MLValuesDifferWOM(MLldouble a, MLldouble b)  { return MLfabs(a -b) >= ML_LDOUBLE_MIN; }
00218   inline bool      MLValuesDifferWOM(MLuint64  a, MLuint64  b)  { return a != b; }
00219   inline bool      MLValuesDifferWOM(MLint64   a, MLint64   b)  { return a != b; }
00220 
00221   // For all other cases.
00222   template <typename T1, typename T2>
00223   inline bool      MLValuesDifferWOM(T1        a, T2        b)  { return a != b; }
00225 
00228 
00229   inline bool      MLValueIs0WOM(MLint8    a) { return a == 0; }
00230   inline bool      MLValueIs0WOM(MLuint8   a) { return a == 0; }
00231   inline bool      MLValueIs0WOM(MLint16   a) { return a == 0; }
00232   inline bool      MLValueIs0WOM(MLuint16  a) { return a == 0; }
00233   inline bool      MLValueIs0WOM(MLint32   a) { return a == 0; }
00234   inline bool      MLValueIs0WOM(MLuint32  a) { return a == 0; }
00235   inline bool      MLValueIs0WOM(MLfloat   a) { return MLfabs(a) < ML_FLOAT_MIN  ; }
00236   inline bool      MLValueIs0WOM(MLdouble  a) { return MLfabs(a) < ML_DOUBLE_MIN ; }
00237   inline bool      MLValueIs0WOM(MLldouble a) { return MLfabs(a) < ML_LDOUBLE_MIN; }
00238   inline bool      MLValueIs0WOM(MLuint64  a) { return a == 0; }
00239   inline bool      MLValueIs0WOM(MLint64   a) { return a == 0; }
00240 
00241   // For all other cases.
00242   template <typename T1>
00243   inline bool      MLValueIs0WOM(T1        a) { return a == 0; }
00245 
00246 #endif // __mlTypeDefTraits_H
00247 
00248