We are going to win! wohoo

Dependencies:   mbed mbed-rtos

Committer:
madcowswe
Date:
Wed Nov 14 17:15:53 2012 +0000
Revision:
9:08552997b544
Parent:
1:6799c07fe510
Added an important comment

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sv 1:6799c07fe510 1 /*
sv 1:6799c07fe510 2 * Tiny Vector Matrix Library
sv 1:6799c07fe510 3 * Dense Vector Matrix Libary of Tiny size using Expression Templates
sv 1:6799c07fe510 4 *
sv 1:6799c07fe510 5 * Copyright (C) 2001 - 2007 Olaf Petzold <opetzold@users.sourceforge.net>
sv 1:6799c07fe510 6 *
sv 1:6799c07fe510 7 * This library is free software; you can redistribute it and/or
sv 1:6799c07fe510 8 * modify it under the terms of the GNU Lesser General Public
sv 1:6799c07fe510 9 * License as published by the Free Software Foundation; either
sv 1:6799c07fe510 10 * version 2.1 of the License, or (at your option) any later version.
sv 1:6799c07fe510 11 *
sv 1:6799c07fe510 12 * This library is distributed in the hope that it will be useful,
sv 1:6799c07fe510 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
sv 1:6799c07fe510 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
sv 1:6799c07fe510 15 * Lesser General Public License for more details.
sv 1:6799c07fe510 16 *
sv 1:6799c07fe510 17 * You should have received a copy of the GNU Lesser General Public
sv 1:6799c07fe510 18 * License along with this library; if not, write to the Free Software
sv 1:6799c07fe510 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
sv 1:6799c07fe510 20 *
sv 1:6799c07fe510 21 * $Id: TypePromotion.h,v 1.10 2007-06-23 15:58:58 opetzold Exp $
sv 1:6799c07fe510 22 */
sv 1:6799c07fe510 23
sv 1:6799c07fe510 24 #ifndef TVMET_TYPE_PROMOTION_H
sv 1:6799c07fe510 25 #define TVMET_TYPE_PROMOTION_H
sv 1:6799c07fe510 26
sv 1:6799c07fe510 27 namespace tvmet {
sv 1:6799c07fe510 28
sv 1:6799c07fe510 29
sv 1:6799c07fe510 30 /**
sv 1:6799c07fe510 31 * \class PrecisionTraits TypePromotion.h "tvmet/TypePromotion.h"
sv 1:6799c07fe510 32 * \brief Declaring ranks of types to avoid specializing
sv 1:6799c07fe510 33 *
sv 1:6799c07fe510 34 * All possible promoted types. For example, bool=1, int=2, float=3, double=4,
sv 1:6799c07fe510 35 * etc. We can use a traits class to map from a type such as float onto its
sv 1:6799c07fe510 36 * "precision rank". We will promote to whichever type has a higher
sv 1:6799c07fe510 37 * "precision rank". f there is no "precision rank" for a type, we'll
sv 1:6799c07fe510 38 * promote to whichever type requires more storage space
sv 1:6799c07fe510 39 * (and hopefully more precision).
sv 1:6799c07fe510 40 */
sv 1:6799c07fe510 41 template<class T>
sv 1:6799c07fe510 42 struct PrecisionTraits {
sv 1:6799c07fe510 43 enum {
sv 1:6799c07fe510 44 rank = 0, /**< the rank of type. */
sv 1:6799c07fe510 45 known = 0 /**< true, if the rank is specialized = known. */
sv 1:6799c07fe510 46 };
sv 1:6799c07fe510 47 };
sv 1:6799c07fe510 48
sv 1:6799c07fe510 49
sv 1:6799c07fe510 50 #define TVMET_PRECISION(T,R) \
sv 1:6799c07fe510 51 template<> \
sv 1:6799c07fe510 52 struct PrecisionTraits< T > { \
sv 1:6799c07fe510 53 enum { \
sv 1:6799c07fe510 54 rank = R, \
sv 1:6799c07fe510 55 known = 1 \
sv 1:6799c07fe510 56 }; \
sv 1:6799c07fe510 57 };
sv 1:6799c07fe510 58
sv 1:6799c07fe510 59
sv 1:6799c07fe510 60 /*
sv 1:6799c07fe510 61 * pod types
sv 1:6799c07fe510 62 */
sv 1:6799c07fe510 63 TVMET_PRECISION(int, 100)
sv 1:6799c07fe510 64 TVMET_PRECISION(unsigned int, 200)
sv 1:6799c07fe510 65 TVMET_PRECISION(long, 300)
sv 1:6799c07fe510 66 TVMET_PRECISION(unsigned long, 400)
sv 1:6799c07fe510 67
sv 1:6799c07fe510 68 #if defined(TVMET_HAVE_LONG_LONG)
sv 1:6799c07fe510 69 TVMET_PRECISION(long long, 500)
sv 1:6799c07fe510 70 TVMET_PRECISION(unsigned long long, 600)
sv 1:6799c07fe510 71 #endif // defined(TVMET_HAVE_LONG_LONG)
sv 1:6799c07fe510 72
sv 1:6799c07fe510 73 TVMET_PRECISION(float, 700)
sv 1:6799c07fe510 74 TVMET_PRECISION(double, 800)
sv 1:6799c07fe510 75
sv 1:6799c07fe510 76 #if defined(TVMET_HAVE_LONG_DOUBLE)
sv 1:6799c07fe510 77 TVMET_PRECISION(long double, 900)
sv 1:6799c07fe510 78 #endif // defined(TVMET_HAVE_LONG_DOUBLE)
sv 1:6799c07fe510 79
sv 1:6799c07fe510 80
sv 1:6799c07fe510 81 /*
sv 1:6799c07fe510 82 * complex types
sv 1:6799c07fe510 83 */
sv 1:6799c07fe510 84 #if defined(TVMET_HAVE_COMPLEX)
sv 1:6799c07fe510 85 TVMET_PRECISION(std::complex<int>, 1000)
sv 1:6799c07fe510 86 TVMET_PRECISION(std::complex<unsigned int>, 1100)
sv 1:6799c07fe510 87 TVMET_PRECISION(std::complex<long>, 1200)
sv 1:6799c07fe510 88 TVMET_PRECISION(std::complex<unsigned long>, 1300)
sv 1:6799c07fe510 89
sv 1:6799c07fe510 90 #if defined(TVMET_HAVE_LONG_LONG)
sv 1:6799c07fe510 91 TVMET_PRECISION(std::complex<long long>, 1400)
sv 1:6799c07fe510 92 TVMET_PRECISION(std::complex<unsigned long long>, 1500)
sv 1:6799c07fe510 93 #endif // defined(TVMET_HAVE_LONG_LONG)
sv 1:6799c07fe510 94
sv 1:6799c07fe510 95 TVMET_PRECISION(std::complex<float>, 1600)
sv 1:6799c07fe510 96 TVMET_PRECISION(std::complex<double>, 1700)
sv 1:6799c07fe510 97
sv 1:6799c07fe510 98 #if defined(TVMET_HAVE_LONG_DOUBLE)
sv 1:6799c07fe510 99 TVMET_PRECISION(std::complex<long double>, 1800)
sv 1:6799c07fe510 100 #endif // defined(TVMET_HAVE_LONG_DOUBLE)
sv 1:6799c07fe510 101
sv 1:6799c07fe510 102 #endif // defined(TVMET_HAVE_COMPLEX)
sv 1:6799c07fe510 103
sv 1:6799c07fe510 104
sv 1:6799c07fe510 105 /** \class PrecisionTraits<int> TypePromotion.h "tvmet/TypePromotion.h" */
sv 1:6799c07fe510 106 /** \class PrecisionTraits<unsigned int> TypePromotion.h "tvmet/TypePromotion.h" */
sv 1:6799c07fe510 107 /** \class PrecisionTraits<long> TypePromotion.h "tvmet/TypePromotion.h" */
sv 1:6799c07fe510 108 /** \class PrecisionTraits<unsigned long> TypePromotion.h "tvmet/TypePromotion.h" */
sv 1:6799c07fe510 109 /** \class PrecisionTraits<long long> TypePromotion.h "tvmet/TypePromotion.h" */
sv 1:6799c07fe510 110 /** \class PrecisionTraits<unsigned long long> TypePromotion.h "tvmet/TypePromotion.h" */
sv 1:6799c07fe510 111 /** \class PrecisionTraits<float> TypePromotion.h "tvmet/TypePromotion.h" */
sv 1:6799c07fe510 112 /** \class PrecisionTraits<double> TypePromotion.h "tvmet/TypePromotion.h" */
sv 1:6799c07fe510 113 /** \class PrecisionTraits<long double> TypePromotion.h "tvmet/TypePromotion.h" */
sv 1:6799c07fe510 114 /** \class PrecisionTraits< std::complex<int> > TypePromotion.h "tvmet/TypePromotion.h" */
sv 1:6799c07fe510 115 /** \class PrecisionTraits< std::complex<unsigned int> > TypePromotion.h "tvmet/TypePromotion.h" */
sv 1:6799c07fe510 116 /** \class PrecisionTraits< std::complex<long> > TypePromotion.h "tvmet/TypePromotion.h" */
sv 1:6799c07fe510 117 /** \class PrecisionTraits< std::complex<unsigned long> > TypePromotion.h "tvmet/TypePromotion.h" */
sv 1:6799c07fe510 118 /** \class PrecisionTraits< std::complex<long long> > TypePromotion.h "tvmet/TypePromotion.h" */
sv 1:6799c07fe510 119 /** \class PrecisionTraits< std::complex<unsigned long long> > TypePromotion.h "tvmet/TypePromotion.h" */
sv 1:6799c07fe510 120 /** \class PrecisionTraits< std::complex<float> > TypePromotion.h "tvmet/TypePromotion.h" */
sv 1:6799c07fe510 121 /** \class PrecisionTraits< std::complex<double> > TypePromotion.h "tvmet/TypePromotion.h" */
sv 1:6799c07fe510 122 /** \class PrecisionTraits< std::complex<long double> > TypePromotion.h "tvmet/TypePromotion.h" */
sv 1:6799c07fe510 123
sv 1:6799c07fe510 124 #undef TVMET_PRECISION
sv 1:6799c07fe510 125
sv 1:6799c07fe510 126
sv 1:6799c07fe510 127 /**
sv 1:6799c07fe510 128 * \class AutopromoteTraits TypePromotion.h "tvmet/TypePromotion.h"
sv 1:6799c07fe510 129 * \brief The promoted types traits.
sv 1:6799c07fe510 130 */
sv 1:6799c07fe510 131 template<class T>
sv 1:6799c07fe510 132 struct AutopromoteTraits {
sv 1:6799c07fe510 133 typedef T value_type;
sv 1:6799c07fe510 134 };
sv 1:6799c07fe510 135
sv 1:6799c07fe510 136
sv 1:6799c07fe510 137 /*
sv 1:6799c07fe510 138 * Defines a macro for specializing/defining
sv 1:6799c07fe510 139 * the promotion traits. bool, char, unsigned char, short int, etc. will
sv 1:6799c07fe510 140 * be autopromote to int, as in C and C++.
sv 1:6799c07fe510 141 */
sv 1:6799c07fe510 142 #define TVMET_AUTOPROMOTE(T1,T2) \
sv 1:6799c07fe510 143 template<> \
sv 1:6799c07fe510 144 struct AutopromoteTraits<T1> { \
sv 1:6799c07fe510 145 typedef T2 value_type; \
sv 1:6799c07fe510 146 };
sv 1:6799c07fe510 147
sv 1:6799c07fe510 148 TVMET_AUTOPROMOTE(bool, int)
sv 1:6799c07fe510 149 TVMET_AUTOPROMOTE(char, int)
sv 1:6799c07fe510 150 TVMET_AUTOPROMOTE(unsigned char, int)
sv 1:6799c07fe510 151 TVMET_AUTOPROMOTE(short int, int)
sv 1:6799c07fe510 152 TVMET_AUTOPROMOTE(short unsigned int, unsigned int)
sv 1:6799c07fe510 153
sv 1:6799c07fe510 154 /** \class AutopromoteTraits<bool> TypePromotion.h "tvmet/TypePromotion.h" */
sv 1:6799c07fe510 155 /** \class AutopromoteTraits<char> TypePromotion.h "tvmet/TypePromotion.h" */
sv 1:6799c07fe510 156 /** \class AutopromoteTraits<unsigned char> TypePromotion.h "tvmet/TypePromotion.h" */
sv 1:6799c07fe510 157 /** \class AutopromoteTraits<short int> TypePromotion.h "tvmet/TypePromotion.h" */
sv 1:6799c07fe510 158 /** \class AutopromoteTraits<short unsigned int> TypePromotion.h "tvmet/TypePromotion.h" */
sv 1:6799c07fe510 159
sv 1:6799c07fe510 160 #undef TVMET_AUTOPROMOTE
sv 1:6799c07fe510 161
sv 1:6799c07fe510 162
sv 1:6799c07fe510 163 /**
sv 1:6799c07fe510 164 * \class promoteTo TypePromotion.h "tvmet/TypePromotion.h"
sv 1:6799c07fe510 165 * \brief Promote to T1.
sv 1:6799c07fe510 166 */
sv 1:6799c07fe510 167 template<class T1, class T2, int promoteToT1>
sv 1:6799c07fe510 168 struct promoteTo {
sv 1:6799c07fe510 169 typedef T1 value_type;
sv 1:6799c07fe510 170 };
sv 1:6799c07fe510 171
sv 1:6799c07fe510 172
sv 1:6799c07fe510 173 /**
sv 1:6799c07fe510 174 * \class promoteTo<T1,T2,0> TypePromotion.h "tvmet/TypePromotion.h"
sv 1:6799c07fe510 175 * \brief Promote to T2
sv 1:6799c07fe510 176 */
sv 1:6799c07fe510 177 template<class T1, class T2>
sv 1:6799c07fe510 178 struct promoteTo<T1,T2,0> {
sv 1:6799c07fe510 179 typedef T2 value_type;
sv 1:6799c07fe510 180 };
sv 1:6799c07fe510 181
sv 1:6799c07fe510 182
sv 1:6799c07fe510 183 /**
sv 1:6799c07fe510 184 * \class PromoteTraits TypePromotion.h "tvmet/TypePromotion.h"
sv 1:6799c07fe510 185 * \brief Promote type traits
sv 1:6799c07fe510 186 */
sv 1:6799c07fe510 187 template<class T1org, class T2org>
sv 1:6799c07fe510 188 class PromoteTraits {
sv 1:6799c07fe510 189 // Handle promotion of small integers to int/unsigned int
sv 1:6799c07fe510 190 typedef typename AutopromoteTraits<T1org>::value_type T1;
sv 1:6799c07fe510 191 typedef typename AutopromoteTraits<T2org>::value_type T2;
sv 1:6799c07fe510 192
sv 1:6799c07fe510 193 enum {
sv 1:6799c07fe510 194 // True if T1 is higher ranked
sv 1:6799c07fe510 195 T1IsBetter = int(PrecisionTraits<T1>::rank) > int(PrecisionTraits<T2>::rank),
sv 1:6799c07fe510 196
sv 1:6799c07fe510 197 // True if we know ranks for both T1 and T2
sv 1:6799c07fe510 198 knowBothRanks = PrecisionTraits<T1>::known && PrecisionTraits<T2>::known,
sv 1:6799c07fe510 199
sv 1:6799c07fe510 200 // True if we know T1 but not T2
sv 1:6799c07fe510 201 knowT1butNotT2 = PrecisionTraits<T1>::known && !(PrecisionTraits<T2>::known),
sv 1:6799c07fe510 202
sv 1:6799c07fe510 203 // True if we know T2 but not T1
sv 1:6799c07fe510 204 knowT2butNotT1 = PrecisionTraits<T2>::known && !(PrecisionTraits<T1>::known),
sv 1:6799c07fe510 205
sv 1:6799c07fe510 206 // True if T1 is bigger than T2
sv 1:6799c07fe510 207 T1IsLarger = sizeof(T1) >= sizeof(T2),
sv 1:6799c07fe510 208
sv 1:6799c07fe510 209 // We know T1 but not T2: true
sv 1:6799c07fe510 210 // We know T2 but not T1: false
sv 1:6799c07fe510 211 // Otherwise, if T1 is bigger than T2: true
sv 1:6799c07fe510 212 defaultPromotion = knowT1butNotT2 ? false : (knowT2butNotT1 ? true : T1IsLarger),
sv 1:6799c07fe510 213
sv 1:6799c07fe510 214 // If we have both ranks, then use them.
sv 1:6799c07fe510 215 // If we have only one rank, then use the unknown type.
sv 1:6799c07fe510 216 // If we have neither rank, then promote to the larger type.
sv 1:6799c07fe510 217 promoteToT1 = (knowBothRanks ? T1IsBetter : defaultPromotion) ? 1 : 0
sv 1:6799c07fe510 218 };
sv 1:6799c07fe510 219
sv 1:6799c07fe510 220 public:
sv 1:6799c07fe510 221 typedef typename promoteTo<T1,T2,promoteToT1>::value_type value_type;
sv 1:6799c07fe510 222 };
sv 1:6799c07fe510 223
sv 1:6799c07fe510 224
sv 1:6799c07fe510 225 } // namespace tvmet
sv 1:6799c07fe510 226
sv 1:6799c07fe510 227 #endif // TVMET_TYPE_PROMOTION_H
sv 1:6799c07fe510 228
sv 1:6799c07fe510 229 // Local Variables:
sv 1:6799c07fe510 230 // mode:C++
sv 1:6799c07fe510 231 // tab-width:8
sv 1:6799c07fe510 232 // End: