time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

ddot.c (2070B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2019 The Stdlib Authors.
      5 *
      6 * Licensed under the Apache License, Version 2.0 (the "License");
      7 * you may not use this file except in compliance with the License.
      8 * You may obtain a copy of the License at
      9 *
     10 *    http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing, software
     13 * distributed under the License is distributed on an "AS IS" BASIS,
     14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15 * See the License for the specific language governing permissions and
     16 * limitations under the License.
     17 */
     18 
     19 /**
     20  * Compute the dot product of two double-precision floating-point vectors.
     21  *
     22  * @see <a href="http://www.netlib.org/lapack/expolore-html/df/d28/group__single__blas__level1.html">ddot</a>
     23  */
     24 #include "stdlib/blas/base/ddot.h"
     25 
     26 /**
     27 * Computes the dot product of two double-precision floating-point vectors.
     28 *
     29 * @param N        number of values over which to compute the dot product
     30 * @param X        first array
     31 * @param strideX  X stride length
     32 * @param Y        second array
     33 * @param strideY  Y stride length
     34 * @returns        the dot product of X and Y
     35 */
     36 double c_ddot( const int N, const double *X, const int strideX, const double *Y, const int strideY ) {
     37 	double dot;
     38 	int ix;
     39 	int iy;
     40 	int m;
     41 	int i;
     42 
     43 	dot = 0.0;
     44 	if ( N <= 0 ) {
     45 		return dot;
     46 	}
     47 	// If both strides are equal to `1`, use unrolled loops...
     48 	if ( strideX == 1 && strideY == 1 ) {
     49 		m = N % 5;
     50 
     51 		// If we have a remainder, do a clean-up loop...
     52 		if ( m > 0 ) {
     53 			for ( i = 0; i < m; i++ ) {
     54 				dot += X[ i ] * Y[ i ];
     55 			}
     56 		}
     57 		if ( N < 5 ) {
     58 			return dot;
     59 		}
     60 		for ( i = m; i < N; i += 5 ) {
     61 			dot += ( X[i]*Y[i] ) + ( X[i+1]*Y[i+1] ) + ( X[i+2]*Y[i+2] ) + ( X[i+3]*Y[i+3] ) + ( X[i+4]*Y[i+4] );
     62 		}
     63 		return dot;
     64 	}
     65 	if ( strideX < 0 ) {
     66 		ix = (1-N) * strideX;
     67 	} else {
     68 		ix = 0;
     69 	}
     70 	if ( strideY < 0 ) {
     71 		iy = (1-N) * strideY;
     72 	} else {
     73 		iy = 0;
     74 	}
     75 	for ( i = 0; i < N; i++ ) {
     76 		dot += X[ ix ] * Y[ iy ];
     77 		ix += strideX;
     78 		iy += strideY;
     79 	}
     80 	return dot;
     81 }
     82