1 | // -*- C++ -*- |
---|
2 | /*************************************************************************** |
---|
3 | * blitz/meta/dot.h Tiny vector dot product metaprogram |
---|
4 | * |
---|
5 | * $Id$ |
---|
6 | * |
---|
7 | * Copyright (C) 1997-2011 Todd Veldhuizen <tveldhui@acm.org> |
---|
8 | * |
---|
9 | * This file is a part of Blitz. |
---|
10 | * |
---|
11 | * Blitz is free software: you can redistribute it and/or modify |
---|
12 | * it under the terms of the GNU Lesser General Public License |
---|
13 | * as published by the Free Software Foundation, either version 3 |
---|
14 | * of the License, or (at your option) any later version. |
---|
15 | * |
---|
16 | * Blitz is distributed in the hope that it will be useful, |
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
19 | * GNU Lesser General Public License for more details. |
---|
20 | * |
---|
21 | * You should have received a copy of the GNU Lesser General Public |
---|
22 | * License along with Blitz. If not, see <http://www.gnu.org/licenses/>. |
---|
23 | * |
---|
24 | * Suggestions: blitz-devel@lists.sourceforge.net |
---|
25 | * Bugs: blitz-support@lists.sourceforge.net |
---|
26 | * |
---|
27 | * For more information, please see the Blitz++ Home Page: |
---|
28 | * https://sourceforge.net/projects/blitz/ |
---|
29 | * |
---|
30 | ***************************************************************************/ |
---|
31 | |
---|
32 | #ifndef BZ_META_DOT_H |
---|
33 | #define BZ_META_DOT_H |
---|
34 | |
---|
35 | #ifndef BZ_PROMOTE_H |
---|
36 | #include <blitz/promote.h> |
---|
37 | #endif |
---|
38 | |
---|
39 | #ifndef BZ_METAPROG_H |
---|
40 | #include <blitz/meta/metaprog.h> |
---|
41 | #endif |
---|
42 | |
---|
43 | BZ_NAMESPACE(blitz) |
---|
44 | |
---|
45 | template<int N, int I> |
---|
46 | class _bz_meta_vectorDot { |
---|
47 | public: |
---|
48 | static const int loopFlag = (I < N-1) ? 1 : 0; |
---|
49 | |
---|
50 | template<typename T_expr1, typename T_expr2> |
---|
51 | static inline BZ_PROMOTE(_bz_typename T_expr1::T_numtype, |
---|
52 | _bz_typename T_expr2::T_numtype) |
---|
53 | f(const T_expr1& a, const T_expr2& b) |
---|
54 | { |
---|
55 | return a[I] * b[I] |
---|
56 | + _bz_meta_vectorDot<loopFlag * N, loopFlag * (I+1)>::f(a,b); |
---|
57 | } |
---|
58 | |
---|
59 | template<typename T_expr1, typename T_expr2> |
---|
60 | static inline BZ_PROMOTE(_bz_typename T_expr1::T_numtype, |
---|
61 | _bz_typename T_expr2::T_numtype) |
---|
62 | f_value_ref(T_expr1 a, const T_expr2& b) |
---|
63 | { |
---|
64 | return a[I] * b[I] |
---|
65 | + _bz_meta_vectorDot<loopFlag * N, loopFlag * (I+1)>::f(a,b); |
---|
66 | } |
---|
67 | |
---|
68 | template<typename T_expr1, typename T_expr2> |
---|
69 | static inline BZ_PROMOTE(_bz_typename T_expr1::T_numtype, |
---|
70 | _bz_typename T_expr2::T_numtype) |
---|
71 | f_ref_value(const T_expr1& a, T_expr2 b) |
---|
72 | { |
---|
73 | return a[I] * b[I] |
---|
74 | + _bz_meta_vectorDot<loopFlag * N, loopFlag * (I+1)>::f(a,b); |
---|
75 | } |
---|
76 | |
---|
77 | template<typename T_expr1, typename P_numtype2> |
---|
78 | static inline BZ_PROMOTE(_bz_typename T_expr1::T_numtype, |
---|
79 | P_numtype2) |
---|
80 | dotWithArgs(const T_expr1& a, P_numtype2 i1, P_numtype2 i2=0, |
---|
81 | P_numtype2 i3=0, P_numtype2 i4=0, P_numtype2 i5=0, P_numtype2 i6=0, |
---|
82 | P_numtype2 i7=0, P_numtype2 i8=0, P_numtype2 i9=0, P_numtype2 i10=0) |
---|
83 | { |
---|
84 | return a[I] * i1 |
---|
85 | + _bz_meta_vectorDot<loopFlag * N, loopFlag * (I+1)>::dotWithArgs |
---|
86 | (a, i2, i3, i4, i5, i6, i7, i8, i9); |
---|
87 | } |
---|
88 | }; |
---|
89 | |
---|
90 | template<> |
---|
91 | class _bz_meta_vectorDot<0,0> { |
---|
92 | public: |
---|
93 | template<typename T_expr1, typename T_expr2> |
---|
94 | static inline _bz_meta_nullOperand f(const T_expr1&, const T_expr2&) |
---|
95 | { return _bz_meta_nullOperand(); } |
---|
96 | |
---|
97 | template<typename T_expr1, typename P_numtype2> |
---|
98 | static inline _bz_meta_nullOperand |
---|
99 | dotWithArgs(const T_expr1& a, P_numtype2 i1, P_numtype2 i2=0, |
---|
100 | P_numtype2 i3=0, P_numtype2 i4=0, P_numtype2 i5=0, P_numtype2 i6=0, |
---|
101 | P_numtype2 i7=0, P_numtype2 i8=0, P_numtype2 i9=0, P_numtype2 i10=0) |
---|
102 | { |
---|
103 | return _bz_meta_nullOperand(); |
---|
104 | } |
---|
105 | |
---|
106 | }; |
---|
107 | |
---|
108 | |
---|
109 | template<int N, int I, typename T_ret> |
---|
110 | class _bz_meta_vectorDotRet { |
---|
111 | public: |
---|
112 | static const int loopFlag = (I < N-1) ? 1 : 0; |
---|
113 | |
---|
114 | template<typename T_expr1, typename T_expr2> |
---|
115 | static inline T_ret |
---|
116 | f(const T_expr1& a, const T_expr2& b) |
---|
117 | { |
---|
118 | return static_cast<T_ret>(a[I]) * static_cast<T_ret>(b[I]) |
---|
119 | + _bz_meta_vectorDotRet<loopFlag * N, loopFlag * (I+1), T_ret>::f(a,b); |
---|
120 | } |
---|
121 | |
---|
122 | template<typename T_expr1, typename T_expr2> |
---|
123 | static inline T_ret |
---|
124 | f_value_ref(T_expr1 a, const T_expr2& b) |
---|
125 | { |
---|
126 | return static_cast<T_ret>(a[I]) * static_cast<T_ret>(b[I]) |
---|
127 | + _bz_meta_vectorDotRet<loopFlag * N, loopFlag * (I+1), T_ret>::f(a,b); |
---|
128 | } |
---|
129 | |
---|
130 | template<typename T_expr1, typename T_expr2> |
---|
131 | static inline T_ret |
---|
132 | f_ref_value(const T_expr1& a, T_expr2 b) |
---|
133 | { |
---|
134 | return static_cast<T_ret>(a[I]) * static_cast<T_ret>(b[I]) |
---|
135 | + _bz_meta_vectorDotRet<loopFlag * N, loopFlag * (I+1), T_ret>::f(a,b); |
---|
136 | } |
---|
137 | |
---|
138 | template<typename T_expr1, typename P_numtype2> |
---|
139 | static inline T_ret |
---|
140 | dotWithArgs(const T_expr1& a, P_numtype2 i1, P_numtype2 i2=0, |
---|
141 | P_numtype2 i3=0, P_numtype2 i4=0, P_numtype2 i5=0, P_numtype2 i6=0, |
---|
142 | P_numtype2 i7=0, P_numtype2 i8=0, P_numtype2 i9=0, P_numtype2 i10=0) |
---|
143 | { |
---|
144 | return static_cast<T_ret>(a[I]) * static_cast<T_ret>(i1) |
---|
145 | + _bz_meta_vectorDotRet<loopFlag * N, loopFlag * (I+1), T_ret>::dotWithArgs |
---|
146 | (a, i2, i3, i4, i5, i6, i7, i8, i9); |
---|
147 | } |
---|
148 | }; |
---|
149 | |
---|
150 | template<typename T_ret> |
---|
151 | class _bz_meta_vectorDotRet<0,0, T_ret> { |
---|
152 | public: |
---|
153 | template<typename T_expr1, typename T_expr2> |
---|
154 | static inline _bz_meta_nullOperand f(const T_expr1&, const T_expr2&) |
---|
155 | { return _bz_meta_nullOperand(); } |
---|
156 | |
---|
157 | template<typename T_expr1, typename P_numtype2> |
---|
158 | static inline _bz_meta_nullOperand |
---|
159 | dotWithArgs(const T_expr1& a, P_numtype2 i1, P_numtype2 i2=0, |
---|
160 | P_numtype2 i3=0, P_numtype2 i4=0, P_numtype2 i5=0, P_numtype2 i6=0, |
---|
161 | P_numtype2 i7=0, P_numtype2 i8=0, P_numtype2 i9=0, P_numtype2 i10=0) |
---|
162 | { |
---|
163 | return _bz_meta_nullOperand(); |
---|
164 | } |
---|
165 | |
---|
166 | }; |
---|
167 | |
---|
168 | BZ_NAMESPACE_END |
---|
169 | |
---|
170 | #endif // BZ_META_DOT_H |
---|