time-to-botec

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

sdsdot.js (2524B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2020 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 'use strict';
     20 
     21 // MODULES //
     22 
     23 var float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' );
     24 
     25 
     26 // VARIABLES //
     27 
     28 var M = 5;
     29 
     30 
     31 // MAIN //
     32 
     33 /**
     34 * Computes the dot product of two single-precision floating-point vectors with extended accumulation.
     35 *
     36 * @param {PositiveInteger} N - number of values over which to compute the dot product
     37 * @param {number} scalar - scalar constant to add to dot product
     38 * @param {Float32Array} x - first input array
     39 * @param {integer} strideX - `x` stride length
     40 * @param {Float32Array} y - second input array
     41 * @param {integer} strideY - `y` stride length
     42 * @returns {number} dot product of `x` and `y`
     43 *
     44 * @example
     45 * var Float32Array = require( '@stdlib/array/float32' );
     46 *
     47 * var x = new Float32Array( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] );
     48 * var y = new Float32Array( [ 2.0, 6.0, -1.0, -4.0, 8.0 ] );
     49 *
     50 * var z = sdsdot( x.length, 0.0, x, 1, y, 1 );
     51 * // returns -5.0
     52 */
     53 function sdsdot( N, scalar, x, strideX, y, strideY ) {
     54 	var dot;
     55 	var ix;
     56 	var iy;
     57 	var m;
     58 	var i;
     59 
     60 	dot = scalar;
     61 	if ( N <= 0 ) {
     62 		return float64ToFloat32( dot );
     63 	}
     64 	// Use unrolled loops if both strides are equal to `1`...
     65 	if ( strideX === 1 && strideY === 1 ) {
     66 		m = N % M;
     67 
     68 		// If we have a remainder, run a clean-up loop...
     69 		if ( m > 0 ) {
     70 			for ( i = 0; i < m; i++ ) {
     71 				dot += x[ i ] * y[ i ];
     72 			}
     73 		}
     74 		if ( N < M ) {
     75 			return float64ToFloat32( dot );
     76 		}
     77 		for ( i = m; i < N; i += M ) {
     78 			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] ); // eslint-disable-line max-len
     79 		}
     80 		return float64ToFloat32( dot );
     81 	}
     82 	if ( strideX < 0 ) {
     83 		ix = ( 1-N ) * strideX;
     84 	} else {
     85 		ix = 0;
     86 	}
     87 	if ( strideY < 0 ) {
     88 		iy = ( 1-N ) * strideY;
     89 	} else {
     90 		iy = 0;
     91 	}
     92 	for ( i = 0; i < N; i++ ) {
     93 		dot += x[ ix ] * y[ iy ];
     94 		ix += strideX;
     95 		iy += strideY;
     96 	}
     97 	return float64ToFloat32( dot );
     98 }
     99 
    100 
    101 // EXPORTS //
    102 
    103 module.exports = sdsdot;