time-to-botec

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

6d_accessors.js (6171B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2021 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 /* eslint-disable max-depth */
     20 
     21 'use strict';
     22 
     23 // MAIN //
     24 
     25 /**
     26 * Applies a unary callback to elements in a six-dimensional input ndarray and assigns results to elements in an equivalently shaped output ndarray.
     27 *
     28 * @private
     29 * @param {Object} x - object containing input ndarray meta data
     30 * @param {string} x.dtype - data type
     31 * @param {Collection} x.data - data buffer
     32 * @param {NonNegativeIntegerArray} x.shape - dimensions
     33 * @param {IntegerArray} x.strides - stride lengths
     34 * @param {NonNegativeInteger} x.offset - index offset
     35 * @param {string} x.order - specifies whether `x` is row-major (C-style) or column-major (Fortran-style)
     36 * @param {Function} x.getter - callback for accessing `x` data buffer elements
     37 * @param {Object} y - object containing output ndarray meta data
     38 * @param {string} y.dtype - data type
     39 * @param {Collection} y.data - data buffer
     40 * @param {NonNegativeIntegerArray} y.shape - dimensions
     41 * @param {IntegerArray} y.strides - stride lengths
     42 * @param {NonNegativeInteger} y.offset - index offset
     43 * @param {string} y.order - specifies whether `y` is row-major (C-style) or column-major (Fortran-style)
     44 * @param {Function} y.setter - callback for setting `y` data buffer elements
     45 * @param {Callback} fcn - unary callback
     46 * @returns {void}
     47 *
     48 * @example
     49 * var Complex64Array = require( '@stdlib/array/complex64' );
     50 * var Complex64 = require( '@stdlib/complex/float32' );
     51 * var real = require( '@stdlib/complex/real' );
     52 * var imag = require( '@stdlib/complex/imag' );
     53 *
     54 * function scale( z ) {
     55 *     return new Complex64( real(z)*10.0, imag(z)*10.0 );
     56 * }
     57 *
     58 * // Create data buffers:
     59 * var xbuf = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
     60 * var ybuf = new Complex64Array( 4 );
     61 *
     62 * // Define the shape of the input and output arrays:
     63 * var shape = [ 1, 1, 1, 1, 2, 2 ];
     64 *
     65 * // Define the array strides:
     66 * var sx = [ 2, 2, 2, 2, 2, 1 ];
     67 * var sy = [ 2, 2, 2, 2, 2, 1 ];
     68 *
     69 * // Define the index offsets:
     70 * var ox = 0;
     71 * var oy = 0;
     72 *
     73 * // Define getters and setters:
     74 * function getter( buf, idx ) {
     75 *     return buf.get( idx );
     76 * }
     77 *
     78 * function setter( buf, idx, value ) {
     79 *     buf.set( value, idx );
     80 * }
     81 *
     82 * // Create the input and output ndarray-like objects:
     83 * var x = {
     84 *     'dtype': 'complex64',
     85 *     'data': xbuf,
     86 *     'shape': shape,
     87 *     'strides': sx,
     88 *     'offset': ox,
     89 *     'order': 'row-major',
     90 *     'getter': getter
     91 * };
     92 * var y = {
     93 *     'dtype': 'complex64',
     94 *     'data': ybuf,
     95 *     'shape': shape,
     96 *     'strides': sy,
     97 *     'offset': oy,
     98 *     'order': 'row-major',
     99 *     'setter': setter
    100 * };
    101 *
    102 * // Apply the unary function:
    103 * unary6d( x, y, scale );
    104 *
    105 * var v = y.data.get( 0 );
    106 *
    107 * var re = real( v );
    108 * // returns 10.0
    109 *
    110 * var im = imag( v );
    111 * // returns 20.0
    112 */
    113 function unary6d( x, y, fcn ) {
    114 	var xbuf;
    115 	var ybuf;
    116 	var get;
    117 	var set;
    118 	var dx0;
    119 	var dx1;
    120 	var dx2;
    121 	var dx3;
    122 	var dx4;
    123 	var dx5;
    124 	var dy0;
    125 	var dy1;
    126 	var dy2;
    127 	var dy3;
    128 	var dy4;
    129 	var dy5;
    130 	var sh;
    131 	var S0;
    132 	var S1;
    133 	var S2;
    134 	var S3;
    135 	var S4;
    136 	var S5;
    137 	var sx;
    138 	var sy;
    139 	var ix;
    140 	var iy;
    141 	var i0;
    142 	var i1;
    143 	var i2;
    144 	var i3;
    145 	var i4;
    146 	var i5;
    147 
    148 	// Note on variable naming convention: S#, dx#, dy#, i# where # corresponds to the loop number, with `0` being the innermost loop...
    149 
    150 	// Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments...
    151 	sh = x.shape;
    152 	sx = x.strides;
    153 	sy = y.strides;
    154 	if ( x.order === 'row-major' ) {
    155 		// For row-major ndarrays, the last dimensions have the fastest changing indices...
    156 		S0 = sh[ 5 ];
    157 		S1 = sh[ 4 ];
    158 		S2 = sh[ 3 ];
    159 		S3 = sh[ 2 ];
    160 		S4 = sh[ 1 ];
    161 		S5 = sh[ 0 ];
    162 		dx0 = sx[ 5 ];                // offset increment for innermost loop
    163 		dx1 = sx[ 4 ] - ( S0*sx[5] );
    164 		dx2 = sx[ 3 ] - ( S1*sx[4] );
    165 		dx3 = sx[ 2 ] - ( S2*sx[3] );
    166 		dx4 = sx[ 1 ] - ( S3*sx[2] );
    167 		dx5 = sx[ 0 ] - ( S4*sx[1] ); // offset increment for outermost loop
    168 		dy0 = sy[ 5 ];
    169 		dy1 = sy[ 4 ] - ( S0*sy[5] );
    170 		dy2 = sy[ 3 ] - ( S1*sy[4] );
    171 		dy3 = sy[ 2 ] - ( S2*sy[3] );
    172 		dy4 = sy[ 1 ] - ( S3*sy[2] );
    173 		dy5 = sy[ 0 ] - ( S4*sy[1] );
    174 	} else { // order === 'column-major'
    175 		// For column-major ndarrays, the first dimensions have the fastest changing indices...
    176 		S0 = sh[ 0 ];
    177 		S1 = sh[ 1 ];
    178 		S2 = sh[ 2 ];
    179 		S3 = sh[ 3 ];
    180 		S4 = sh[ 4 ];
    181 		S5 = sh[ 5 ];
    182 		dx0 = sx[ 0 ];                // offset increment for innermost loop
    183 		dx1 = sx[ 1 ] - ( S0*sx[0] );
    184 		dx2 = sx[ 2 ] - ( S1*sx[1] );
    185 		dx3 = sx[ 3 ] - ( S2*sx[2] );
    186 		dx4 = sx[ 4 ] - ( S3*sx[3] );
    187 		dx5 = sx[ 5 ] - ( S4*sx[4] ); // offset increment for outermost loop
    188 		dy0 = sy[ 0 ];
    189 		dy1 = sy[ 1 ] - ( S0*sy[0] );
    190 		dy2 = sy[ 2 ] - ( S1*sy[1] );
    191 		dy3 = sy[ 3 ] - ( S2*sy[2] );
    192 		dy4 = sy[ 4 ] - ( S3*sy[3] );
    193 		dy5 = sy[ 5 ] - ( S4*sy[4] );
    194 	}
    195 	// Set the pointers to the first indexed elements in the respective ndarrays...
    196 	ix = x.offset;
    197 	iy = y.offset;
    198 
    199 	// Cache references to the input and output ndarray buffers...
    200 	xbuf = x.data;
    201 	ybuf = y.data;
    202 
    203 	// Cache accessors:
    204 	get = x.getter;
    205 	set = y.setter;
    206 
    207 	// Iterate over the ndarray dimensions...
    208 	for ( i5 = 0; i5 < S5; i5++ ) {
    209 		for ( i4 = 0; i4 < S4; i4++ ) {
    210 			for ( i3 = 0; i3 < S3; i3++ ) {
    211 				for ( i2 = 0; i2 < S2; i2++ ) {
    212 					for ( i1 = 0; i1 < S1; i1++ ) {
    213 						for ( i0 = 0; i0 < S0; i0++ ) {
    214 							set( ybuf, iy, fcn( get( xbuf, ix ) ) );
    215 							ix += dx0;
    216 							iy += dy0;
    217 						}
    218 						ix += dx1;
    219 						iy += dy1;
    220 					}
    221 					ix += dx2;
    222 					iy += dy2;
    223 				}
    224 				ix += dx3;
    225 				iy += dy3;
    226 			}
    227 			ix += dx4;
    228 			iy += dy4;
    229 		}
    230 		ix += dx5;
    231 		iy += dy5;
    232 	}
    233 }
    234 
    235 
    236 // EXPORTS //
    237 
    238 module.exports = unary6d;