time-to-botec

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

4d.js (4903B)


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