1 /* -*- C++ -*- ------------------------------------------------------------
3 Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
5 The Configurable Math Library (CML) is distributed under the terms of the
6 Boost Software License, v1.0 (see cml/LICENSE for details).
8 *-----------------------------------------------------------------------*/
13 #ifndef scalar_promotions_h
14 #define scalar_promotions_h
17 #include <cml/core/cml_meta.h>
22 /* The type promotion code below is a slightly modified version of:
23 * http://ubiety.uwaterloo.ca/~tveldhui/papers/techniques/techniques01.html
28 struct precision_trait
{
29 enum { precisionRank
= 0,
30 knowPrecisionRank
= 0 };
33 #define DECLARE_PRECISION(T,rank) \
35 struct precision_trait< T > { \
36 enum { precisionRank = rank, \
37 knowPrecisionRank = 1 }; \
40 DECLARE_PRECISION(int,100)
41 DECLARE_PRECISION(unsigned int,200)
42 DECLARE_PRECISION(long,300)
43 DECLARE_PRECISION(unsigned long,400)
45 DECLARE_PRECISION(long long,425)
46 DECLARE_PRECISION(unsigned long long,475)
48 DECLARE_PRECISION(float,500)
49 DECLARE_PRECISION(double,600)
50 DECLARE_PRECISION(long double,700)
51 DECLARE_PRECISION(std::complex<float>,800)
52 DECLARE_PRECISION(std::complex<double>,900)
53 DECLARE_PRECISION(std::complex<long double>,1000)
56 struct autopromote_trait
{
60 #define DECLARE_AUTOPROMOTE(T1,T2) \
62 struct autopromote_trait<T1> { \
63 typedef T2 T_numtype; \
66 // These are the odd cases where small integer types
67 // are automatically promoted to int or unsigned int for
69 DECLARE_AUTOPROMOTE(bool, int)
70 DECLARE_AUTOPROMOTE(char, int)
71 DECLARE_AUTOPROMOTE(unsigned char, int)
72 DECLARE_AUTOPROMOTE(short int, int)
73 DECLARE_AUTOPROMOTE(short unsigned int, unsigned int)
75 template<class T1
, class T2
, int promoteToT1
>
80 template<class T1
, class T2
>
81 struct promote2
<T1
,T2
,0> {
85 template<class T1_orig
, class T2_orig
>
86 struct promote_trait
{
88 // Need to remove const-ness:
89 typedef typename
cml::remove_const
<T1_orig
>::type T1_non_const
;
90 typedef typename
cml::remove_const
<T2_orig
>::type T2_non_const
;
92 // Handle promotion of small integers to int/unsigned int
93 typedef typename autopromote_trait
<T1_non_const
>::T_numtype T1
;
94 typedef typename autopromote_trait
<T2_non_const
>::T_numtype T2
;
96 // True if T1 is higher ranked
99 (int) precision_trait
<T1
>::precisionRank
>
100 (int) precision_trait
<T2
>::precisionRank
,
102 // True if we know ranks for both T1 and T2
104 precision_trait
<T1
>::knowPrecisionRank
105 && precision_trait
<T2
>::knowPrecisionRank
,
107 // True if we know T1 but not T2
108 knowT1butNotT2
= precision_trait
<T1
>::knowPrecisionRank
109 && !(precision_trait
<T2
>::knowPrecisionRank
),
111 // True if we know T2 but not T1
112 knowT2butNotT1
= precision_trait
<T2
>::knowPrecisionRank
113 && !(precision_trait
<T1
>::knowPrecisionRank
),
115 // True if T1 is bigger than T2
116 T1IsLarger
= sizeof(T1
) >= sizeof(T2
),
118 // We know T1 but not T2: true
119 // We know T2 but not T1: false
120 // Otherwise, if T1 is bigger than T2: true
121 defaultPromotion
= knowT1butNotT2
? false :
122 (knowT2butNotT1
? true : T1IsLarger
)
125 // If we have both ranks, then use them.
126 // If we have only one rank, then use the unknown type.
127 // If we have neither rank, then promote to the larger type.
130 promoteToT1
= (knowBothRanks
? T1IsBetter
: defaultPromotion
)
134 typedef typename promote2
<T1
,T2
,promoteToT1
>::T_promote T_promote
;
137 } // namespace detail
139 /** Defers to detail::promote_trait<>. */
140 template<class E1
, class E2
> struct ScalarPromote
142 typedef typename
detail::promote_trait
<E1
,E2
>::T_promote type
;
150 // -------------------------------------------------------------------------