time-to-botec

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

gapx.js (1827B)


      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 // VARIABLES //
     22 
     23 var M = 5;
     24 
     25 
     26 // MAIN //
     27 
     28 /**
     29 * Adds a constant to each element in a strided array.
     30 *
     31 * @param {PositiveInteger} N - number of indexed elements
     32 * @param {number} alpha - scalar
     33 * @param {NumericArray} x - input array
     34 * @param {integer} stride - index increment
     35 * @returns {NumericArray} input array
     36 *
     37 * @example
     38 * var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ];
     39 *
     40 * gapx( x.length, 5.0, x, 1 );
     41 * // x => [ 3.0, 6.0, 8.0, 0.0, 9.0, 5.0, 4.0, 2.0 ]
     42 */
     43 function gapx( N, alpha, x, stride ) {
     44 	var ix;
     45 	var i;
     46 	var m;
     47 
     48 	if ( N <= 0 || alpha === 0.0 ) {
     49 		return x;
     50 	}
     51 	// Use loop unrolling if the stride is equal to `1`...
     52 	if ( stride === 1 ) {
     53 		m = N % M;
     54 
     55 		// If we have a remainder, run a clean-up loop...
     56 		if ( m > 0 ) {
     57 			for ( i = 0; i < m; i++ ) {
     58 				x[ i ] += alpha;
     59 			}
     60 		}
     61 		if ( N < M ) {
     62 			return x;
     63 		}
     64 		for ( i = m; i < N; i += M ) {
     65 			x[ i ] += alpha;
     66 			x[ i+1 ] += alpha;
     67 			x[ i+2 ] += alpha;
     68 			x[ i+3 ] += alpha;
     69 			x[ i+4 ] += alpha;
     70 		}
     71 		return x;
     72 	}
     73 	if ( stride < 0 ) {
     74 		ix = (1-N) * stride;
     75 	} else {
     76 		ix = 0;
     77 	}
     78 	for ( i = 0; i < N; i++ ) {
     79 		x[ ix ] += alpha;
     80 		ix += stride;
     81 	}
     82 	return x;
     83 }
     84 
     85 
     86 // EXPORTS //
     87 
     88 module.exports = gapx;