Eurobot2012_Primary

Dependencies:   mbed Eurobot_2012_Primary

Committer:
narshu
Date:
Wed Oct 17 22:22:47 2012 +0000
Revision:
26:0995f61cb7b8
Parent:
25:143b19c1fb05
Eurobot 2012 Primary;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
narshu 25:143b19c1fb05 1 /*
narshu 25:143b19c1fb05 2 * Tiny Vector Matrix Library
narshu 25:143b19c1fb05 3 * Dense Vector Matrix Libary of Tiny size using Expression Templates
narshu 25:143b19c1fb05 4 *
narshu 25:143b19c1fb05 5 * Copyright (C) 2001 - 2007 Olaf Petzold <opetzold@users.sourceforge.net>
narshu 25:143b19c1fb05 6 *
narshu 25:143b19c1fb05 7 * This library is free software; you can redistribute it and/or
narshu 25:143b19c1fb05 8 * modify it under the terms of the GNU Lesser General Public
narshu 25:143b19c1fb05 9 * License as published by the Free Software Foundation; either
narshu 25:143b19c1fb05 10 * version 2.1 of the License, or (at your option) any later version.
narshu 25:143b19c1fb05 11 *
narshu 25:143b19c1fb05 12 * This library is distributed in the hope that it will be useful,
narshu 25:143b19c1fb05 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
narshu 25:143b19c1fb05 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
narshu 25:143b19c1fb05 15 * Lesser General Public License for more details.
narshu 25:143b19c1fb05 16 *
narshu 25:143b19c1fb05 17 * You should have received a copy of the GNU Lesser General Public
narshu 25:143b19c1fb05 18 * License along with this library; if not, write to the Free Software
narshu 25:143b19c1fb05 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
narshu 25:143b19c1fb05 20 *
narshu 25:143b19c1fb05 21 * $Id: Gemtm.h,v 1.9 2007-06-23 15:58:59 opetzold Exp $
narshu 25:143b19c1fb05 22 */
narshu 25:143b19c1fb05 23
narshu 25:143b19c1fb05 24 #ifndef TVMET_LOOP_GEMTM_H
narshu 25:143b19c1fb05 25 #define TVMET_LOOP_GEMTM_H
narshu 25:143b19c1fb05 26
narshu 25:143b19c1fb05 27 namespace tvmet {
narshu 25:143b19c1fb05 28
narshu 25:143b19c1fb05 29 namespace loop {
narshu 25:143b19c1fb05 30
narshu 25:143b19c1fb05 31
narshu 25:143b19c1fb05 32 /**
narshu 25:143b19c1fb05 33 * \class gemtm Gemtm.h "tvmet/loop/Gemtm.h"
narshu 25:143b19c1fb05 34 * \brief class for matrix-matrix product using loop unrolling.
narshu 25:143b19c1fb05 35 * using formula
narshu 25:143b19c1fb05 36 * \f[
narshu 25:143b19c1fb05 37 * M_1^{T}\,M_2
narshu 25:143b19c1fb05 38 * \f]
narshu 25:143b19c1fb05 39 * \par Example:
narshu 25:143b19c1fb05 40 * \code
narshu 25:143b19c1fb05 41 * template<class T, std::size_t Rows1, std::size_t Cols1, std::size_t Cols2>
narshu 25:143b19c1fb05 42 * inline
narshu 25:143b19c1fb05 43 * void
narshu 25:143b19c1fb05 44 * prod(const Matrix<T, Rows1, Cols1>& lhs, const Matrix<T, Rows1, Cols2>& rhs,
narshu 25:143b19c1fb05 45 * Matrix<T, Cols2, Cols1>& dest)
narshu 25:143b19c1fb05 46 * {
narshu 25:143b19c1fb05 47 * for (std::size_t i = 0; i != Cols1; ++i) {
narshu 25:143b19c1fb05 48 * for (std::size_t j = 0; j != Cols2; ++j) {
narshu 25:143b19c1fb05 49 * dest(i, j) = tvmet::loop::gemtm<Rows1, Cols1, Cols2>::prod(lhs, rhs, i, j);
narshu 25:143b19c1fb05 50 * }
narshu 25:143b19c1fb05 51 * }
narshu 25:143b19c1fb05 52 * }
narshu 25:143b19c1fb05 53 * \endcode
narshu 25:143b19c1fb05 54 * \note The number of rows of rhs matrix have to be equal rows of rhs matrix,
narshu 25:143b19c1fb05 55 * since lhs matrix 1 is transposed.
narshu 25:143b19c1fb05 56 * The result is a (Cols1 x Cols2) matrix.
narshu 25:143b19c1fb05 57 */
narshu 25:143b19c1fb05 58 template<std::size_t Rows1, std::size_t Cols1,
narshu 25:143b19c1fb05 59 std::size_t Cols2>
narshu 25:143b19c1fb05 60 class gemtm
narshu 25:143b19c1fb05 61 {
narshu 25:143b19c1fb05 62 gemtm(const gemtm&);
narshu 25:143b19c1fb05 63 gemtm& operator=(const gemtm&);
narshu 25:143b19c1fb05 64
narshu 25:143b19c1fb05 65 private:
narshu 25:143b19c1fb05 66 enum {
narshu 25:143b19c1fb05 67 count = Cols1,
narshu 25:143b19c1fb05 68 N = (count+7)/8
narshu 25:143b19c1fb05 69 };
narshu 25:143b19c1fb05 70
narshu 25:143b19c1fb05 71 public:
narshu 25:143b19c1fb05 72 gemtm() { }
narshu 25:143b19c1fb05 73
narshu 25:143b19c1fb05 74 public:
narshu 25:143b19c1fb05 75 template<class E1, class E2>
narshu 25:143b19c1fb05 76 static inline
narshu 25:143b19c1fb05 77 typename PromoteTraits<
narshu 25:143b19c1fb05 78 typename E1::value_type,
narshu 25:143b19c1fb05 79 typename E2::value_type
narshu 25:143b19c1fb05 80 >::value_type
narshu 25:143b19c1fb05 81 prod(const E1& lhs, const E2& rhs, std::size_t i, std::size_t j) {
narshu 25:143b19c1fb05 82 typename PromoteTraits<
narshu 25:143b19c1fb05 83 typename E1::value_type,
narshu 25:143b19c1fb05 84 typename E2::value_type
narshu 25:143b19c1fb05 85 >::value_type sum(0);
narshu 25:143b19c1fb05 86 std::size_t k(0);
narshu 25:143b19c1fb05 87 std::size_t n(N);
narshu 25:143b19c1fb05 88
narshu 25:143b19c1fb05 89 // Duff's device
narshu 25:143b19c1fb05 90 switch(count % 8) {
narshu 25:143b19c1fb05 91 case 0: do { sum += lhs(k, i) * rhs(k, j); ++k;
narshu 25:143b19c1fb05 92 case 7: sum += lhs(k, i) * rhs(k, j); ++k;
narshu 25:143b19c1fb05 93 case 6: sum += lhs(k, i) * rhs(k, j); ++k;
narshu 25:143b19c1fb05 94 case 5: sum += lhs(k, i) * rhs(k, j); ++k;
narshu 25:143b19c1fb05 95 case 4: sum += lhs(k, i) * rhs(k, j); ++k;
narshu 25:143b19c1fb05 96 case 3: sum += lhs(k, i) * rhs(k, j); ++k;
narshu 25:143b19c1fb05 97 case 2: sum += lhs(k, i) * rhs(k, j); ++k;
narshu 25:143b19c1fb05 98 case 1: sum += lhs(k, i) * rhs(k, j); ++k;
narshu 25:143b19c1fb05 99 } while(--n != 0);
narshu 25:143b19c1fb05 100 }
narshu 25:143b19c1fb05 101
narshu 25:143b19c1fb05 102 return sum;
narshu 25:143b19c1fb05 103 }
narshu 25:143b19c1fb05 104 };
narshu 25:143b19c1fb05 105
narshu 25:143b19c1fb05 106
narshu 25:143b19c1fb05 107 } // namespace loop
narshu 25:143b19c1fb05 108
narshu 25:143b19c1fb05 109 } // namespace tvmet
narshu 25:143b19c1fb05 110
narshu 25:143b19c1fb05 111 #endif /* TVMET_LOOP_GEMTM_H */
narshu 25:143b19c1fb05 112
narshu 25:143b19c1fb05 113 // Local Variables:
narshu 25:143b19c1fb05 114 // mode:C++
narshu 25:143b19c1fb05 115 // tab-width:8
narshu 25:143b19c1fb05 116 // End: