time-to-botec

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

drev.js (2247B)


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