time-to-botec

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

8d.js (6147B)


      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 an eight-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, 1, 3, 1, 2 ];
     59 *
     60 * // Define the array strides:
     61 * var sx = [ 12, 12, 12, 12, 12, 4, 4, 1 ];
     62 * var sy = [ 6, 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 * unary8d( 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 unary8d( 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 dx7;
    103 	var dy0;
    104 	var dy1;
    105 	var dy2;
    106 	var dy3;
    107 	var dy4;
    108 	var dy5;
    109 	var dy6;
    110 	var dy7;
    111 	var sh;
    112 	var S0;
    113 	var S1;
    114 	var S2;
    115 	var S3;
    116 	var S4;
    117 	var S5;
    118 	var S6;
    119 	var S7;
    120 	var sx;
    121 	var sy;
    122 	var ix;
    123 	var iy;
    124 	var i0;
    125 	var i1;
    126 	var i2;
    127 	var i3;
    128 	var i4;
    129 	var i5;
    130 	var i6;
    131 	var i7;
    132 
    133 	// Note on variable naming convention: S#, dx#, dy#, i# where # corresponds to the loop number, with `0` being the innermost loop...
    134 
    135 	// Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments...
    136 	sh = x.shape;
    137 	sx = x.strides;
    138 	sy = y.strides;
    139 	if ( x.order === 'row-major' ) {
    140 		// For row-major ndarrays, the last dimensions have the fastest changing indices...
    141 		S0 = sh[ 7 ];
    142 		S1 = sh[ 6 ];
    143 		S2 = sh[ 5 ];
    144 		S3 = sh[ 4 ];
    145 		S4 = sh[ 3 ];
    146 		S5 = sh[ 2 ];
    147 		S6 = sh[ 1 ];
    148 		S7 = sh[ 0 ];
    149 		dx0 = sx[ 7 ];                // offset increment for innermost loop
    150 		dx1 = sx[ 6 ] - ( S0*sx[7] );
    151 		dx2 = sx[ 5 ] - ( S1*sx[6] );
    152 		dx3 = sx[ 4 ] - ( S2*sx[5] );
    153 		dx4 = sx[ 3 ] - ( S3*sx[4] );
    154 		dx5 = sx[ 2 ] - ( S4*sx[3] );
    155 		dx6 = sx[ 1 ] - ( S5*sx[2] );
    156 		dx7 = sx[ 0 ] - ( S6*sx[1] ); // offset increment for outermost loop
    157 		dy0 = sy[ 7 ];
    158 		dy1 = sy[ 6 ] - ( S0*sy[7] );
    159 		dy2 = sy[ 5 ] - ( S1*sy[6] );
    160 		dy3 = sy[ 4 ] - ( S2*sy[5] );
    161 		dy4 = sy[ 3 ] - ( S3*sy[4] );
    162 		dy5 = sy[ 2 ] - ( S4*sy[3] );
    163 		dy6 = sy[ 1 ] - ( S5*sy[2] );
    164 		dy7 = sy[ 0 ] - ( S6*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 		S5 = sh[ 5 ];
    173 		S6 = sh[ 6 ];
    174 		S7 = sh[ 7 ];
    175 		dx0 = sx[ 0 ];                // offset increment for innermost loop
    176 		dx1 = sx[ 1 ] - ( S0*sx[0] );
    177 		dx2 = sx[ 2 ] - ( S1*sx[1] );
    178 		dx3 = sx[ 3 ] - ( S2*sx[2] );
    179 		dx4 = sx[ 4 ] - ( S3*sx[3] );
    180 		dx5 = sx[ 5 ] - ( S4*sx[4] );
    181 		dx6 = sx[ 6 ] - ( S5*sx[5] );
    182 		dx7 = sx[ 7 ] - ( S6*sx[6] ); // offset increment for outermost loop
    183 		dy0 = sy[ 0 ];
    184 		dy1 = sy[ 1 ] - ( S0*sy[0] );
    185 		dy2 = sy[ 2 ] - ( S1*sy[1] );
    186 		dy3 = sy[ 3 ] - ( S2*sy[2] );
    187 		dy4 = sy[ 4 ] - ( S3*sy[3] );
    188 		dy5 = sy[ 5 ] - ( S4*sy[4] );
    189 		dy6 = sy[ 6 ] - ( S5*sy[5] );
    190 		dy7 = sy[ 7 ] - ( S6*sy[6] );
    191 	}
    192 	// Set the pointers to the first indexed elements in the respective ndarrays...
    193 	ix = x.offset;
    194 	iy = y.offset;
    195 
    196 	// Cache references to the input and output ndarray buffers...
    197 	xbuf = x.data;
    198 	ybuf = y.data;
    199 
    200 	// Iterate over the ndarray dimensions...
    201 	for ( i7 = 0; i7 < S7; i7++ ) {
    202 		for ( i6 = 0; i6 < S6; i6++ ) {
    203 			for ( i5 = 0; i5 < S5; i5++ ) {
    204 				for ( i4 = 0; i4 < S4; i4++ ) {
    205 					for ( i3 = 0; i3 < S3; i3++ ) {
    206 						for ( i2 = 0; i2 < S2; i2++ ) {
    207 							for ( i1 = 0; i1 < S1; i1++ ) {
    208 								for ( i0 = 0; i0 < S0; i0++ ) {
    209 									ybuf[ iy ] = fcn( xbuf[ ix ] );
    210 									ix += dx0;
    211 									iy += dy0;
    212 								}
    213 								ix += dx1;
    214 								iy += dy1;
    215 							}
    216 							ix += dx2;
    217 							iy += dy2;
    218 						}
    219 						ix += dx3;
    220 						iy += dy3;
    221 					}
    222 					ix += dx4;
    223 					iy += dy4;
    224 				}
    225 				ix += dx5;
    226 				iy += dy5;
    227 			}
    228 			ix += dx6;
    229 			iy += dy6;
    230 		}
    231 		ix += dx7;
    232 		iy += dy7;
    233 	}
    234 }
    235 
    236 
    237 // EXPORTS //
    238 
    239 module.exports = unary8d;