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_NRM2_TCC
 34 #define CXXBLAS_LEVEL1_NRM2_TCC 1
 35 
 36 #include <cxxblas/aux/complex.h>
 37 
 38 namespace cxxblas {
 39 
 40 template <typename IndexType, typename X, typename T>
 41 void
 42 nrm2_generic(IndexType n, const X *x, IndexType incX, T &norm)
 43 {
 44     CXXBLAS_DEBUG_OUT("nrm2_generic");
 45 
 46     using std::abs;
 47     using std::pow;
 48     using std::sqrt;
 49 
 50     if (n<1) {
 51         norm = T(0);
 52     } else if (n==1) {
 53         norm = abs(*x);
 54     } else {
 55         T scale = 0;
 56         T ssq = 1;
 57 //      The following loop is equivalent to this call to the LAPACK
 58 //      auxiliary routine:
 59 //      CALL DLASSQ( N, X, INCX, SCALE, SSQ )
 60 //
 61         for (IndexType i=0, iX=0; i<n; ++i, iX+=incX) {
 62             if (x[iX]!=T(0)) {
 63                 T absXi = abs(x[iX]);
 64                 if (scale<absXi) {
 65                     ssq = T(1) + ssq * pow(scale/absXi, 2);
 66                     scale = absXi;
 67                 } else {
 68                     ssq += pow(absXi/scale, 2);
 69                 }
 70             }
 71         }
 72         norm = scale*sqrt(ssq);
 73     }
 74 }
 75 
 76 template <typename IndexType, typename X, typename T>
 77 void
 78 nrm2(IndexType n, const X *x, IndexType incX, T &norm)
 79 {
 80     if (incX<0) {
 81         x -= incX*(n-1);
 82     }
 83     nrm2_generic(n, x, incX, norm);
 84 }
 85 
 86 #ifdef HAVE_CBLAS
 87 
 88 // snrm2
 89 template <typename IndexType>
 90 typename If<IndexType>::isBlasCompatibleInteger
 91 nrm2(IndexType n, const float *x, IndexType incX, float &norm)
 92 {
 93     CXXBLAS_DEBUG_OUT("[" BLAS_IMPL "] cblas_snrm2");
 94 
 95     norm = cblas_snrm2(n, x, incX);
 96 }
 97 
 98 // dnrm2
 99 template <typename IndexType>
100 typename If<IndexType>::isBlasCompatibleInteger
101 nrm2(IndexType n, const double *x, IndexType incX, double &norm)
102 {
103     CXXBLAS_DEBUG_OUT("[" BLAS_IMPL "] cblas_dnrm2");
104 
105     norm = cblas_dnrm2(n, x, incX);
106 }
107 
108 // scnrm2
109 template <typename IndexType>
110 typename If<IndexType>::isBlasCompatibleInteger
111 nrm2(IndexType n, const ComplexFloat *x, IndexType incX, float &norm)
112 {
113     CXXBLAS_DEBUG_OUT("[" BLAS_IMPL "] cblas_scnrm2");
114 
115     norm = cblas_scnrm2(n, x, incX);
116 }
117 
118 // dznrm2
119 template <typename IndexType>
120 typename If<IndexType>::isBlasCompatibleInteger
121 nrm2(IndexType n, const ComplexDouble *x, IndexType incX, double &norm)
122 {
123     CXXBLAS_DEBUG_OUT("[" BLAS_IMPL "] cblas_dznrm2");
124 
125     norm = cblas_dznrm2(n, x, incX);
126 }
127 
128 #endif // HAVE_CBLAS
129 
130 // namespace cxxblas
131 
132 #endif // CXXBLAS_LEVEL1_NRM2_TCC