ndarray.js (2174B)
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 {NumericArray} x - input array 33 * @param {integer} strideX - `x` stride length 34 * @param {NonNegativeInteger} offsetX - starting `x` index 35 * @param {NumericArray} y - destination 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 * gcopy( x.length, x, 1, 0, y, 1, 0 ); 45 * // y => [ 1.0, 2.0, 3.0, 4.0, 5.0 ] 46 */ 47 function gcopy( N, x, strideX, offsetX, y, strideY, offsetY ) { 48 var ix; 49 var iy; 50 var m; 51 var i; 52 if ( N <= 0 ) { 53 return y; 54 } 55 ix = offsetX; 56 iy = offsetY; 57 58 // Use unrolled loops if both strides are equal to `1`... 59 if ( strideX === 1 && strideY === 1 ) { 60 m = N % M; 61 62 // If we have a remainder, run a clean-up loop... 63 if ( m > 0 ) { 64 for ( i = 0; i < m; i++ ) { 65 y[ iy ] = x[ ix ]; 66 ix += strideX; 67 iy += strideY; 68 } 69 } 70 if ( N < M ) { 71 return y; 72 } 73 for ( i = m; i < N; i += M ) { 74 y[ iy ] = x[ ix ]; 75 y[ iy+1 ] = x[ ix+1 ]; 76 y[ iy+2 ] = x[ ix+2 ]; 77 y[ iy+3 ] = x[ ix+3 ]; 78 y[ iy+4 ] = x[ ix+4 ]; 79 y[ iy+5 ] = x[ ix+5 ]; 80 y[ iy+6 ] = x[ ix+6 ]; 81 y[ iy+7 ] = x[ ix+7 ]; 82 ix += M; 83 iy += M; 84 } 85 return y; 86 } 87 for ( i = 0; i < N; i++ ) { 88 y[ iy ] = x[ ix ]; 89 ix += strideX; 90 iy += strideY; 91 } 92 return y; 93 } 94 95 96 // EXPORTS // 97 98 module.exports = gcopy;