time-to-botec

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

ndarray.js (2432B)


      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 {NonNegativeInteger} offsetX - starting `x` index
     35 * @param {Complex64Array} y - second input array
     36 * @param {integer} strideY - `y` stride length
     37 * @param {NonNegativeInteger} offsetY - starting `y` index
     38 * @returns {Complex64Array} `y`
     39 *
     40 * @example
     41 * var Complex64Array = require( '@stdlib/array/complex64' );
     42 * var real = require( '@stdlib/complex/real' );
     43 * var imag = require( '@stdlib/complex/imag' );
     44 *
     45 * var x = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
     46 * var y = new Complex64Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
     47 *
     48 * cswap( x.length, x, 1, 0, y, 1, 0 );
     49 *
     50 * var z = y.get( 0 );
     51 * // returns <Complex64>
     52 *
     53 * var re = real( z );
     54 * // returns 1.0
     55 *
     56 * var im = imag( z );
     57 * // returns 2.0
     58 *
     59 * z = x.get( 0 );
     60 * // returns <Complex64>
     61 *
     62 * re = real( z );
     63 * // returns 7.0
     64 *
     65 * im = imag( z );
     66 * // returns 8.0
     67 */
     68 function cswap( N, x, strideX, offsetX, y, strideY, offsetY ) {
     69 	var viewX;
     70 	var viewY;
     71 	var tmp;
     72 	var sx;
     73 	var sy;
     74 	var ix;
     75 	var iy;
     76 	var i;
     77 
     78 	if ( N <= 0 ) {
     79 		return y;
     80 	}
     81 	viewX = new Float32Array( x.buffer, x.byteOffset, x.length*2 );
     82 	viewY = new Float32Array( y.buffer, y.byteOffset, y.length*2 );
     83 	sx = strideX * 2;
     84 	sy = strideY * 2;
     85 	ix = offsetX * 2;
     86 	iy = offsetY * 2;
     87 	for ( i = 0; i < N; i++ ) {
     88 		tmp = viewX[ ix ];
     89 		viewX[ ix ] = viewY[ iy ];
     90 		viewY[ iy ] = tmp;
     91 
     92 		tmp = viewX[ ix+1 ];
     93 		viewX[ ix+1 ] = viewY[ iy+1 ];
     94 		viewY[ iy+1 ] = tmp;
     95 
     96 		ix += sx;
     97 		iy += sy;
     98 	}
     99 	return y;
    100 }
    101 
    102 
    103 // EXPORTS //
    104 
    105 module.exports = cswap;