time-to-botec

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

cswap.js (2656B)


      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 // MODULES //
     22 
     23 var Float32Array = require( '@stdlib/array/float32' );
     24 
     25 
     26 // MAIN //
     27 
     28 /**
     29 * Interchanges two complex single-precision floating-point vectors.
     30 *
     31 * @param {PositiveInteger} N - number of values to swap
     32 * @param {Complex64Array} x - first input array
     33 * @param {integer} strideX - `x` stride length
     34 * @param {Complex64Array} y - second input array
     35 * @param {integer} strideY - `y` stride length
     36 * @returns {Complex64Array} `y`
     37 *
     38 * @example
     39 * var Complex64Array = require( '@stdlib/array/complex64' );
     40 * var real = require( '@stdlib/complex/real' );
     41 * var imag = require( '@stdlib/complex/imag' );
     42 *
     43 * var x = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
     44 * var y = new Complex64Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
     45 *
     46 * cswap( x.length, x, 1, y, 1 );
     47 *
     48 * var z = y.get( 0 );
     49 * // returns <Complex64>
     50 *
     51 * var re = real( z );
     52 * // returns 1.0
     53 *
     54 * var im = imag( z );
     55 * // returns 2.0
     56 *
     57 * z = x.get( 0 );
     58 * // returns <Complex64>
     59 *
     60 * re = real( z );
     61 * // returns 7.0
     62 *
     63 * im = imag( z );
     64 * // returns 8.0
     65 */
     66 function cswap( N, x, strideX, y, strideY ) {
     67 	var viewX;
     68 	var viewY;
     69 	var tmp;
     70 	var sx;
     71 	var sy;
     72 	var ix;
     73 	var iy;
     74 	var i;
     75 	var j;
     76 
     77 	if ( N <= 0 ) {
     78 		return y;
     79 	}
     80 	viewX = new Float32Array( x.buffer, x.byteOffset, x.length*2 );
     81 	viewY = new Float32Array( y.buffer, y.byteOffset, y.length*2 );
     82 	if ( strideX === 1 && strideY === 1 ) {
     83 		for ( i = 0; i < N*2; i += 2 ) {
     84 			tmp = viewX[ i ];
     85 			viewX[ i ] = viewY[ i ];
     86 			viewY[ i ] = tmp;
     87 
     88 			j = i + 1;
     89 			tmp = viewX[ j ];
     90 			viewX[ j ] = viewY[ j ];
     91 			viewY[ j ] = tmp;
     92 		}
     93 		return y;
     94 	}
     95 	if ( strideX < 0 ) {
     96 		ix = 2 * (1-N) * strideX;
     97 	} else {
     98 		ix = 0;
     99 	}
    100 	if ( strideY < 0 ) {
    101 		iy = 2 * (1-N) * strideY;
    102 	} else {
    103 		iy = 0;
    104 	}
    105 	sx = strideX * 2;
    106 	sy = strideY * 2;
    107 	for ( i = 0; i < N; i++ ) {
    108 		tmp = viewX[ ix ];
    109 		viewX[ ix ] = viewY[ iy ];
    110 		viewY[ iy ] = tmp;
    111 
    112 		tmp = viewX[ ix+1 ];
    113 		viewX[ ix+1 ] = viewY[ iy+1 ];
    114 		viewY[ iy+1 ] = tmp;
    115 
    116 		ix += sx;
    117 		iy += sy;
    118 	}
    119 	return y;
    120 }
    121 
    122 
    123 // EXPORTS //
    124 
    125 module.exports = cswap;