time-to-botec

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

ndarray.js (2292B)


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