time-to-botec

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

ndarray.js (2617B)


      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 {integer} 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 {NonNegativeInteger} offsetX - starting index for `x`
     41 * @param {Float32Array} y - second input array
     42 * @param {integer} strideY - `y` stride length
     43 * @param {NonNegativeInteger} offsetY - starting index for `y`
     44 * @returns {number} dot product of `x` and `y`
     45 *
     46 * @example
     47 * var Float32Array = require( '@stdlib/array/float32' );
     48 *
     49 * var x = new Float32Array( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] );
     50 * var y = new Float32Array( [ 2.0, 6.0, -1.0, -4.0, 8.0 ] );
     51 *
     52 * var z = sdsdot( x.length, 0.0, x, 1, 0, y, 1, 0 );
     53 * // returns -5.0
     54 */
     55 function sdsdot( N, scalar, x, strideX, offsetX, y, strideY, offsetY ) {
     56 	var dot;
     57 	var ix;
     58 	var iy;
     59 	var m;
     60 	var i;
     61 
     62 	dot = scalar;
     63 	if ( N <= 0 ) {
     64 		return float64ToFloat32( dot );
     65 	}
     66 	ix = offsetX;
     67 	iy = offsetY;
     68 
     69 	// Use unrolled loops if both strides are equal to `1`...
     70 	if ( strideX === 1 && strideY === 1 ) {
     71 		m = N % M;
     72 
     73 		// If we have a remainder, run a clean-up loop...
     74 		if ( m > 0 ) {
     75 			for ( i = 0; i < m; i++ ) {
     76 				dot += x[ ix ] * y[ iy ];
     77 				ix += 1;
     78 				iy += 1;
     79 			}
     80 		}
     81 		if ( N < M ) {
     82 			return float64ToFloat32( dot );
     83 		}
     84 		for ( i = m; i < N; i += M ) {
     85 			dot += ( x[ix]*y[iy] ) + ( x[ix+1]*y[iy+1] ) + ( x[ix+2]*y[iy+2] ) + ( x[ix+3]*y[iy+3] ) + ( x[ix+4]*y[iy+4] ); // eslint-disable-line max-len
     86 			ix += M;
     87 			iy += M;
     88 		}
     89 		return float64ToFloat32( dot );
     90 	}
     91 	for ( i = 0; i < N; i++ ) {
     92 		dot += x[ ix ] * y[ iy ];
     93 		ix += strideX;
     94 		iy += strideY;
     95 	}
     96 	return float64ToFloat32( dot );
     97 }
     98 
     99 
    100 // EXPORTS //
    101 
    102 module.exports = sdsdot;