time-to-botec

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

7d.js (5847B)


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