1 /*
  2  *   Copyright (c) 2009, Michael Lehn
  3  *
  4  *   All rights reserved.
  5  *
  6  *   Redistribution and use in source and binary forms, with or without
  7  *   modification, are permitted provided that the following conditions
  8  *   are met:
  9  *
 10  *   1) Redistributions of source code must retain the above copyright
 11  *      notice, this list of conditions and the following disclaimer.
 12  *   2) Redistributions in binary form must reproduce the above copyright
 13  *      notice, this list of conditions and the following disclaimer in
 14  *      the documentation and/or other materials provided with the
 15  *      distribution.
 16  *   3) Neither the name of the FLENS development group nor the names of
 17  *      its contributors may be used to endorse or promote products derived
 18  *      from this software without specific prior written permission.
 19  *
 20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 31  */
 32 
 33 #ifndef CXXBLAS_LEVEL1_DOT_TCC
 34 #define CXXBLAS_LEVEL1_DOT_TCC 1
 35 
 36 #include <cxxblas/aux/complex.h>
 37 
 38 namespace cxxblas {
 39 
 40 template <typename IndexType, typename X, typename Y, typename Result>
 41 void
 42 dotu_generic(IndexType n,
 43              const X *x, IndexType incX, const Y *y, IndexType incY,
 44              Result &result)
 45 {
 46     CXXBLAS_DEBUG_OUT("dotu_generic");
 47 
 48     result = Result(0);
 49     for (IndexType i=0, iX=0, iY=0; i<n; ++i, iX+=incX, iY+=incY) {
 50         result += x[iX]*y[iY];
 51     }
 52 }
 53 
 54 template <typename IndexType, typename X, typename Y, typename Result>
 55 void
 56 dot_generic(IndexType n,
 57             const X *x, IndexType incX, const Y *y, IndexType incY,
 58             Result &result)
 59 {
 60     CXXBLAS_DEBUG_OUT("dot_generic");
 61 
 62     result = Result(0);
 63     for (IndexType i=0, iX=0, iY=0; i<n; ++i, iX+=incX, iY+=incY) {
 64         result += conjugate(x[iX])*y[iY];
 65     }
 66 }
 67 
 68 //------------------------------------------------------------------------------
 69 
 70 template <typename IndexType, typename X, typename Y, typename Result>
 71 void
 72 dotu(IndexType n,
 73      const X *x, IndexType incX, const Y *y, IndexType incY,
 74      Result &result)
 75 {
 76     if (incX<0) {
 77         x -= incX*(n-1);
 78     }
 79     if (incY<0) {
 80         y -= incY*(n-1);
 81     }
 82     dotu_generic(n, x, incX, y, incY, result);
 83 }
 84 
 85 template <typename IndexType, typename X, typename Y, typename Result>
 86 void
 87 dot(IndexType n,
 88     const X *x, IndexType incX, const Y *y, IndexType incY,
 89     Result &result)
 90 {
 91     if (incX<0) {
 92         x -= incX*(n-1);
 93     }
 94     if (incY<0) {
 95         y -= incY*(n-1);
 96     }
 97     dot_generic(n, x, incX, y, incY, result);
 98 }
 99 
100 #ifdef HAVE_CBLAS
101 
102 // sdsdot
103 template <typename IndexType>
104 typename If<IndexType>::isBlasCompatibleInteger
105 sdot(IndexType n, float alpha,
106      const float *x, IndexType incX,
107      const float *y, IndexType incY,
108      float &result)
109 {
110     CXXBLAS_DEBUG_OUT("[" BLAS_IMPL "] cblas_sdsdot");
111 
112     result = cblas_sdsdot(n, alpha, x, incX, y, incY);
113 }
114 
115 // dsdot
116 template <typename IndexType>
117 typename If<IndexType>::isBlasCompatibleInteger
118 dot(IndexType n,
119     const float *x, IndexType incX,
120     const float *y, IndexType incY,
121     double &result)
122 {
123     CXXBLAS_DEBUG_OUT("[" BLAS_IMPL "] cblas_dsdot");
124 
125     result = cblas_dsdot(n, x, incX, y, incY);
126 }
127 
128 // sdot
129 template <typename IndexType>
130 typename If<IndexType>::isBlasCompatibleInteger
131 dot(IndexType n,
132     const float *x, IndexType incX,
133     const float  *y, IndexType incY,
134     float &result)
135 {
136     CXXBLAS_DEBUG_OUT("[" BLAS_IMPL "] cblas_sdot");
137 
138     result = cblas_sdot(n, x, incX, y, incY);
139 }
140 
141 // ddot
142 template <typename IndexType>
143 typename If<IndexType>::isBlasCompatibleInteger
144 dot(IndexType n,
145     const double *x, IndexType incX,
146     const double *y, IndexType incY,
147     double &result)
148 {
149     CXXBLAS_DEBUG_OUT("[" BLAS_IMPL "] cblas_ddot");
150 
151     result = cblas_ddot(n, x, incX, y, incY);
152 }
153 
154 // cdotu_sub
155 template <typename IndexType>
156 typename If<IndexType>::isBlasCompatibleInteger
157 dotu(IndexType n,
158      const ComplexFloat  *x, IndexType incX,
159      const ComplexFloat  *y, IndexType incY,
160      ComplexFloat &result)
161 {
162     CXXBLAS_DEBUG_OUT("[" BLAS_IMPL "] cblas_cdotu_sub");
163 
164     cblas_cdotu_sub(n, reinterpret_cast<const float *>(x), incX,
165                        reinterpret_cast<const float *>(y), incY,
166                        reinterpret_cast<const float *>(&result));
167 }
168 
169 
170 // cdotc_sub
171 template <typename IndexType>
172 typename If<IndexType>::isBlasCompatibleInteger
173 dot(IndexType n,
174     const ComplexFloat  *x, IndexType incX,
175     const ComplexFloat  *y, IndexType incY,
176     ComplexFloat &result)
177 {
178     CXXBLAS_DEBUG_OUT("[" BLAS_IMPL "] cblas_cdotc_sub");
179 
180     cblas_cdotc_sub(n, reinterpret_cast<const float *>(x), incX,
181                        reinterpret_cast<const float *>(y), incY,
182                        reinterpret_cast<const float *>(&result));
183 }
184 
185 // zdotu_sub
186 template <typename IndexType>
187 typename If<IndexType>::isBlasCompatibleInteger
188 dotu(IndexType n,
189      const ComplexDouble *x, IndexType incX,
190      const ComplexDouble *y, IndexType incY,
191      ComplexDouble &result)
192 {
193     CXXBLAS_DEBUG_OUT("[" BLAS_IMPL "] cblas_zdotu_sub");
194 
195     cblas_zdotu_sub(n, reinterpret_cast<const double *>(x), incX,
196                        reinterpret_cast<const double *>(y), incY,
197                        reinterpret_cast<const double *>(&result));
198 }
199 
200 // zdotc_sub
201 template <typename IndexType>
202 typename If<IndexType>::isBlasCompatibleInteger
203 dot(IndexType n,
204     const ComplexDouble *x, IndexType incX,
205     const ComplexDouble *y, IndexType incY,
206     ComplexDouble &result)
207 {
208     CXXBLAS_DEBUG_OUT("[" BLAS_IMPL "] cblas_zdotc_sub");
209 
210     cblas_zdotc_sub(n, reinterpret_cast<const double *>(x), incX,
211                        reinterpret_cast<const double *>(y), incY,
212                        reinterpret_cast<const double *>(&result));
213 }
214 
215 #endif // HAVE_CBLAS
216 
217 // namespace cxxblas
218 
219 #endif // CXXBLAS_LEVEL1_DOT_TCC