time-to-botec

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

5d_accessors.js (5850B)


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