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