X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2Fcml%2Fet%2Fscalar_promotions.h;h=bb6f8a271e7eed965376895a5065d21fda7b7b54;hp=55d033ca5ef52a93053baa679618e5f00f2e8c39;hb=50c1239917f5e443b8ec91773c85ceb3db7da67b;hpb=1dd005530930657fd6216edc1dfcfa4c270a81c9 diff --git a/src/Moof/cml/et/scalar_promotions.h b/src/Moof/cml/et/scalar_promotions.h index 55d033c..bb6f8a2 100644 --- a/src/Moof/cml/et/scalar_promotions.h +++ b/src/Moof/cml/et/scalar_promotions.h @@ -13,11 +13,135 @@ Boost Software License, v1.0 (see cml/LICENSE for details). #ifndef scalar_promotions_h #define scalar_promotions_h +#include #include namespace cml { namespace et { +// #define CML_USE_OLD_SCALAR_PROMOTIONS +#if !defined(CML_USE_OLD_SCALAR_PROMOTIONS) + +/* The type promotion code below is a slightly modified version of: + * http://ubiety.uwaterloo.ca/~tveldhui/papers/techniques/techniques01.html + */ +namespace detail { + +template +struct precision_trait { + enum { precisionRank = 0, + knowPrecisionRank = 0 }; +}; + +#define DECLARE_PRECISION(T,rank) \ + template<> \ + struct precision_trait< T > { \ + enum { precisionRank = rank, \ + knowPrecisionRank = 1 }; \ + }; + +DECLARE_PRECISION(int,100) +DECLARE_PRECISION(unsigned int,200) +DECLARE_PRECISION(long,300) +DECLARE_PRECISION(unsigned long,400) + +DECLARE_PRECISION(long long,425) +DECLARE_PRECISION(unsigned long long,475) + +DECLARE_PRECISION(float,500) +DECLARE_PRECISION(double,600) +DECLARE_PRECISION(long double,700) +DECLARE_PRECISION(std::complex,800) +DECLARE_PRECISION(std::complex,900) +DECLARE_PRECISION(std::complex,1000) + +template +struct autopromote_trait { + typedef T T_numtype; +}; + +#define DECLARE_AUTOPROMOTE(T1,T2) \ + template<> \ + struct autopromote_trait { \ + typedef T2 T_numtype; \ + }; + +// These are the odd cases where small integer types +// are automatically promoted to int or unsigned int for +// arithmetic. +DECLARE_AUTOPROMOTE(bool, int) +DECLARE_AUTOPROMOTE(char, int) +DECLARE_AUTOPROMOTE(unsigned char, int) +DECLARE_AUTOPROMOTE(short int, int) +DECLARE_AUTOPROMOTE(short unsigned int, unsigned int) + +template +struct promote2 { + typedef T1 T_promote; +}; + +template +struct promote2 { + typedef T2 T_promote; +}; + +template +struct promote_trait { + // Handle promotion of small integers to int/unsigned int + typedef typename autopromote_trait::T_numtype T1; + typedef typename autopromote_trait::T_numtype T2; + + // True if T1 is higher ranked + enum { + T1IsBetter = + (int) precision_trait::precisionRank > + (int) precision_trait::precisionRank, + + // True if we know ranks for both T1 and T2 + knowBothRanks = + precision_trait::knowPrecisionRank + && precision_trait::knowPrecisionRank, + + // True if we know T1 but not T2 + knowT1butNotT2 = precision_trait::knowPrecisionRank + && !(precision_trait::knowPrecisionRank), + + // True if we know T2 but not T1 + knowT2butNotT1 = precision_trait::knowPrecisionRank + && !(precision_trait::knowPrecisionRank), + + // True if T1 is bigger than T2 + T1IsLarger = sizeof(T1) >= sizeof(T2), + + // We know T1 but not T2: true + // We know T2 but not T1: false + // Otherwise, if T1 is bigger than T2: true + defaultPromotion = knowT1butNotT2 ? false : + (knowT2butNotT1 ? true : T1IsLarger) + }; + + // If we have both ranks, then use them. + // If we have only one rank, then use the unknown type. + // If we have neither rank, then promote to the larger type. + + enum { + promoteToT1 = (knowBothRanks ? T1IsBetter : defaultPromotion) + ? 1 : 0 + }; + + typedef typename promote2::T_promote T_promote; +}; + +} // namespace detail + +/** Defers to detail::promote_trait<>. */ +template struct ScalarPromote +{ + typedef typename detail::promote_trait::T_promote type; +}; + +#else + namespace detail { /** @class IntPromote @@ -43,7 +167,6 @@ template struct IntPromote */ template struct ScalarPromote { - /* Integral-promote the types (if possible). */ typedef typename detail::IntPromote::result E1; typedef typename detail::IntPromote::result E2; @@ -110,6 +233,7 @@ template struct ScalarPromote same_type::is_true, int_filter, float_filter>::result type; }; +#endif } // namespace et } // namespace cml