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_ASUM_TCC
 34 #define CXXBLAS_LEVEL1_ASUM_TCC 1
 35 
 36 #include <cmath>
 37 
 38 
 39 namespace cxxblas {
 40 
 41 template <typename IndexType, typename X, typename T>
 42 void
 43 asum_generic(IndexType n, const X *x, IndexType incX, T &absSum)
 44 {
 45     CXXBLAS_DEBUG_OUT("[ asum_generic");
 46 
 47     using std::abs;
 48 
 49     absSum = 0;
 50     for (IndexType i=0; i<n; ++i, x+=incX) {
 51         absSum += abs(cxxblas::real(*x)) + abs(cxxblas::imag(*x));
 52     }
 53 }
 54 
 55 template <typename IndexType, typename X, typename T>
 56 void
 57 asum(IndexType n, const X *x, IndexType incX, T &absSum)
 58 {
 59     if (incX<0) {
 60         x -= incX*(n-1);
 61     }
 62     asum_generic(n, x, incX, absSum);
 63 }
 64 
 65 #ifdef HAVE_CBLAS
 66 // sasum
 67 template <typename IndexType>
 68 typename If<IndexType>::isBlasCompatibleInteger
 69 asum(IndexType n, const float *x, IndexType incX, float &absSum)
 70 {
 71     CXXBLAS_DEBUG_OUT("[" BLAS_IMPL "] cblas_sasum");
 72 
 73     absSum = cblas_sasum(n, x, incX);
 74 }
 75 
 76 // dasum
 77 template <typename IndexType>
 78 typename If<IndexType>::isBlasCompatibleInteger
 79 asum(IndexType n, const double *x, IndexType incX, double &absSum)
 80 {
 81     CXXBLAS_DEBUG_OUT("[" BLAS_IMPL "] cblas_dasum");
 82 
 83     absSum = cblas_dasum(n, x, incX);
 84 }
 85 
 86 // scasum
 87 template <typename IndexType>
 88 typename If<IndexType>::isBlasCompatibleInteger
 89 asum(IndexType n, const ComplexFloat *x, IndexType incX, float &absSum)
 90 {
 91     CXXBLAS_DEBUG_OUT("[" BLAS_IMPL "] cblas_scasum");
 92 
 93     absSum = cblas_scasum(n, x, incX);
 94 }
 95 
 96 // dzasum
 97 template <typename IndexType>
 98 typename If<IndexType>::isBlasCompatibleInteger
 99 asum(IndexType n, const ComplexDouble *x, IndexType incX, double &absSum)
100 {
101     CXXBLAS_DEBUG_OUT("[" BLAS_IMPL "] cblas_dzasum");
102 
103     absSum = cblas_dzasum(n, x, incX);
104 }
105 
106 #endif // HAVE_CBLAS
107 
108 // namespace cxxblas
109 
110 #endif // CXXBLAS_LEVEL1_ASUM_TCC