time-to-botec

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

dcopy.js (2172B)


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