time-to-botec

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

ndarray.js (2273B)


      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 floor = require( '@stdlib/math/base/special/floor' );
     24 
     25 
     26 // VARIABLES //
     27 
     28 var M = 3;
     29 
     30 
     31 // MAIN //
     32 
     33 /**
     34 * Reverses a single-precision floating-point strided array in-place.
     35 *
     36 * @param {PositiveInteger} N - number of indexed elements
     37 * @param {Float32Array} x - input array
     38 * @param {integer} stride - index increment
     39 * @param {NonNegativeInteger} offset - starting index
     40 * @returns {Float32Array} input array
     41 *
     42 * @example
     43 * var Float32Array = require( '@stdlib/array/float32' );
     44 *
     45 * var x = new Float32Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] );
     46 *
     47 * srev( 3, x, 1, x.length-3 );
     48 * // x => <Float32Array>[ 1.0, -2.0, 3.0, -6.0, 5.0, -4.0 ]
     49 */
     50 function srev( N, x, stride, offset ) {
     51 	var tmp;
     52 	var ix;
     53 	var iy;
     54 	var m;
     55 	var n;
     56 	var i;
     57 
     58 	if ( N <= 0 ) {
     59 		return x;
     60 	}
     61 	n = floor( N/2 );
     62 	ix = offset;
     63 
     64 	// Use loop unrolling if the stride is equal to `1`...
     65 	if ( stride === 1 ) {
     66 		m = n % M;
     67 		iy = ix + N - 1;
     68 
     69 		// If we have a remainder, run a clean-up loop...
     70 		if ( m > 0 ) {
     71 			for ( i = 0; i < m; i++ ) {
     72 				tmp = x[ ix ];
     73 				x[ ix ] = x[ iy ];
     74 				x[ iy ] = tmp;
     75 				ix += stride;
     76 				iy -= stride;
     77 			}
     78 		}
     79 		if ( n < M ) {
     80 			return x;
     81 		}
     82 		for ( i = m; i < n; i += M ) {
     83 			tmp = x[ ix ];
     84 			x[ ix ] = x[ iy ];
     85 			x[ iy ] = tmp;
     86 
     87 			tmp = x[ ix+1 ];
     88 			x[ ix+1 ] = x[ iy-1 ];
     89 			x[ iy-1 ] = tmp;
     90 
     91 			tmp = x[ ix+2 ];
     92 			x[ ix+2 ] = x[ iy-2 ];
     93 			x[ iy-2 ] = tmp;
     94 
     95 			ix += M;
     96 			iy -= M;
     97 		}
     98 		return x;
     99 	}
    100 	iy = ix + ((N-1)*stride);
    101 	for ( i = 0; i < n; i++ ) {
    102 		tmp = x[ ix ];
    103 		x[ ix ] = x[ iy ];
    104 		x[ iy ] = tmp;
    105 		ix += stride;
    106 		iy -= stride;
    107 	}
    108 	return x;
    109 }
    110 
    111 
    112 // EXPORTS //
    113 
    114 module.exports = srev;