time-to-botec

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

saxpy.js (2456B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2018 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 = 4;
     29 
     30 
     31 // MAIN //
     32 
     33 /**
     34 * Multiplies a vector `x` by a constant and adds the result to `y`.
     35 *
     36 * @param {PositiveInteger} N - number of elements
     37 * @param {number} alpha - scalar
     38 * @param {Float32Array} x - input array
     39 * @param {integer} strideX - `x` stride length
     40 * @param {Float32Array} y - destination array
     41 * @param {integer} strideY - `y` stride length
     42 * @returns {Float32Array} `y`
     43 *
     44 * @example
     45 * var Float32Array = require( '@stdlib/array/float32' );
     46 *
     47 * var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
     48 * var y = new Float32Array( [ 1.0, 1.0, 1.0, 1.0, 1.0 ] );
     49 * var alpha = 5.0;
     50 *
     51 * saxpy( x.length, alpha, x, 1, y, 1 );
     52 * // y => <Float32Array>[ 6.0, 11.0, 16.0, 21.0, 26.0 ]
     53 */
     54 function saxpy( N, alpha, x, strideX, y, strideY ) {
     55 	var ix;
     56 	var iy;
     57 	var m;
     58 	var i;
     59 	if ( N <= 0 || alpha === 0.0 ) {
     60 		return y;
     61 	}
     62 	// Use unrolled loops if both strides are equal to `1`...
     63 	if ( strideX === 1 && strideY === 1 ) {
     64 		m = N % M;
     65 
     66 		// If we have a remainder, run a clean-up loop...
     67 		if ( m > 0 ) {
     68 			for ( i = 0; i < m; i++ ) {
     69 				y[ i ] += float64ToFloat32( alpha * x[ i ] );
     70 			}
     71 		}
     72 		if ( N < M ) {
     73 			return y;
     74 		}
     75 		for ( i = m; i < N; i += M ) {
     76 			y[ i ] += float64ToFloat32( alpha * x[ i ] );
     77 			y[ i+1 ] += float64ToFloat32( alpha * x[ i+1 ] );
     78 			y[ i+2 ] += float64ToFloat32( alpha * x[ i+2 ] );
     79 			y[ i+3 ] += float64ToFloat32( alpha * x[ i+3 ] );
     80 		}
     81 		return y;
     82 	}
     83 	if ( strideX < 0 ) {
     84 		ix = (1-N) * strideX;
     85 	} else {
     86 		ix = 0;
     87 	}
     88 	if ( strideY < 0 ) {
     89 		iy = (1-N) * strideY;
     90 	} else {
     91 		iy = 0;
     92 	}
     93 	for ( i = 0; i < N; i++ ) {
     94 		y[ iy ] += float64ToFloat32( alpha * x[ ix ] );
     95 		ix += strideX;
     96 		iy += strideY;
     97 	}
     98 	return y;
     99 }
    100 
    101 
    102 // EXPORTS //
    103 
    104 module.exports = saxpy;