time-to-botec

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

main.js (2195B)


      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 = 3;
     24 
     25 
     26 // MAIN //
     27 
     28 /**
     29 * Interchanges vectors `x` and `y`.
     30 *
     31 * @param {PositiveInteger} N - number of values to swap
     32 * @param {NumericArray} x - first input array
     33 * @param {integer} strideX - `x` stride length
     34 * @param {NumericArray} y - second input array
     35 * @param {integer} strideY - `y` stride length
     36 * @returns {NumericArray} `y`
     37 *
     38 * @example
     39 * var x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ];
     40 * var y = [ 6.0, 7.0, 8.0, 9.0, 10.0 ];
     41 *
     42 * gswap( x.length, x, 1, y, 1 );
     43 * // x => [ 6.0, 7.0, 8.0, 9.0, 10.0 ]
     44 * // y => [ 1.0, 2.0, 3.0, 4.0, 5.0 ]
     45 */
     46 function gswap( N, x, strideX, y, strideY ) {
     47 	var tmp;
     48 	var ix;
     49 	var iy;
     50 	var m;
     51 	var i;
     52 	var j;
     53 	if ( N <= 0 ) {
     54 		return y;
     55 	}
     56 	// Use unrolled loops if both strides are equal to `1`...
     57 	if ( strideX === 1 && strideY === 1 ) {
     58 		m = N % M;
     59 
     60 		// If we have a remainder, run a clean-up loop...
     61 		if ( m > 0 ) {
     62 			for ( i = 0; i < m; i++ ) {
     63 				tmp = x[ i ];
     64 				x[ i ] = y[ i ];
     65 				y[ i ] = tmp;
     66 			}
     67 		}
     68 		if ( N < M ) {
     69 			return y;
     70 		}
     71 		for ( i = m; i < N; i += M ) {
     72 			tmp = x[ i ];
     73 			x[ i ] = y[ i ];
     74 			y[ i ] = tmp;
     75 
     76 			j = i + 1;
     77 			tmp = x[ j ];
     78 			x[ j ] = y[ j ];
     79 			y[ j ] = tmp;
     80 
     81 			j += 1;
     82 			tmp = x[ j ];
     83 			x[ j ] = y[ j ];
     84 			y[ j ] = tmp;
     85 		}
     86 		return y;
     87 	}
     88 	if ( strideX < 0 ) {
     89 		ix = (1-N) * strideX;
     90 	} else {
     91 		ix = 0;
     92 	}
     93 	if ( strideY < 0 ) {
     94 		iy = (1-N) * strideY;
     95 	} else {
     96 		iy = 0;
     97 	}
     98 	for ( i = 0; i < N; i++ ) {
     99 		tmp = x[ ix ];
    100 		x[ ix ] = y[ iy ];
    101 		y[ iy ] = tmp;
    102 		ix += strideX;
    103 		iy += strideY;
    104 	}
    105 	return y;
    106 }
    107 
    108 
    109 // EXPORTS //
    110 
    111 module.exports = gswap;