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