Skip to content

Commit fdf57c8

Browse files
committed
feat: add 9d accessors kernel
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent c7aeb32 commit fdf57c8

File tree

1 file changed

+334
-0
lines changed

1 file changed

+334
-0
lines changed
Lines changed: 334 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,334 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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, max-len, max-statements */
20+
21+
'use strict';
22+
23+
// MODULES //
24+
25+
var isRowMajor = require( '@stdlib/ndarray/base/assert/is-row-major-string' );
26+
27+
28+
// MAIN //
29+
30+
/**
31+
* Applies a binary callback to elements in nine-dimensional input ndarrays and assigns results to elements in an equivalently shaped output ndarray.
32+
*
33+
* @private
34+
* @param {Object} x - object containing input ndarray meta data
35+
* @param {string} x.dtype - data type
36+
* @param {Collection} x.data - data buffer
37+
* @param {NonNegativeIntegerArray} x.shape - dimensions
38+
* @param {IntegerArray} x.strides - stride lengths
39+
* @param {NonNegativeInteger} x.offset - index offset
40+
* @param {string} x.order - specifies whether `x` is row-major (C-style) or column-major (Fortran-style)
41+
* @param {Array<Function>} x.accessors - data buffer accessors
42+
* @param {Object} y - object containing input ndarray meta data
43+
* @param {string} y.dtype - data type
44+
* @param {Collection} y.data - data buffer
45+
* @param {NonNegativeIntegerArray} y.shape - dimensions
46+
* @param {IntegerArray} y.strides - stride lengths
47+
* @param {NonNegativeInteger} y.offset - index offset
48+
* @param {string} y.order - specifies whether `y` is row-major (C-style) or column-major (Fortran-style)
49+
* @param {Array<Function>} y.accessors - data buffer accessors
50+
* @param {Object} z - object containing output ndarray meta data
51+
* @param {string} z.dtype - data type
52+
* @param {Collection} z.data - data buffer
53+
* @param {NonNegativeIntegerArray} z.shape - dimensions
54+
* @param {IntegerArray} z.strides - stride lengths
55+
* @param {NonNegativeInteger} z.offset - index offset
56+
* @param {string} z.order - specifies whether `z` is row-major (C-style) or column-major (Fortran-style)
57+
* @param {Array<Function>} z.accessors - data buffer accessors
58+
* @param {Callback} fcn - binary callback
59+
* @returns {void}
60+
*
61+
* @example
62+
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
63+
* var accessors = require( '@stdlib/array/base/accessors' );
64+
* var copy = require( '@stdlib/array/base/copy' );
65+
*
66+
* function fcn( x, y ) {
67+
* return x + y;
68+
* }
69+
*
70+
* // Create data buffers:
71+
* var xbuf = toAccessorArray( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
72+
* var ybuf = toAccessorArray( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
73+
* var zbuf = toAccessorArray( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );
74+
*
75+
* // Define the shape of the input and output arrays:
76+
* var shape = [ 1, 1, 1, 1, 1, 1, 3, 2, 2 ];
77+
*
78+
* // Define the array strides:
79+
* var sx = [ 12, 12, 12, 12, 12, 12, 4, 2, 1 ];
80+
* var sy = [ 12, 12, 12, 12, 12, 12, 4, 2, 1 ];
81+
* var sz = [ 12, 12, 12, 12, 12, 12, 4, 2, 1 ];
82+
*
83+
* // Define the index offsets:
84+
* var ox = 0;
85+
* var oy = 0;
86+
* var oz = 0;
87+
*
88+
* // Create the input and output ndarray-like objects:
89+
* var x = {
90+
* 'dtype': 'generic',
91+
* 'data': xbuf,
92+
* 'shape': shape,
93+
* 'strides': sx,
94+
* 'offset': ox,
95+
* 'order': 'row-major',
96+
* 'accessors': accessors( xbuf ).accessors
97+
* };
98+
* var y = {
99+
* 'dtype': 'generic',
100+
* 'data': ybuf,
101+
* 'shape': shape,
102+
* 'strides': sy,
103+
* 'offset': oy,
104+
* 'order': 'row-major',
105+
* 'accessors': accessors( ybuf ).accessors
106+
* };
107+
* var z = {
108+
* 'dtype': 'generic',
109+
* 'data': zbuf,
110+
* 'shape': shape,
111+
* 'strides': sz,
112+
* 'offset': oz,
113+
* 'order': 'row-major',
114+
* 'accessors': accessors( zbuf ).accessors
115+
* };
116+
*
117+
* // Apply the binary function:
118+
* binary9d( x, y, z, fcn );
119+
*
120+
* console.log( copy( z.data ) );
121+
* // => [ 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0 ]
122+
*/
123+
function binary9d( x, y, z, fcn ) {
124+
var xbuf;
125+
var ybuf;
126+
var zbuf;
127+
var xget;
128+
var yget;
129+
var zset;
130+
var dx0;
131+
var dx1;
132+
var dx2;
133+
var dx3;
134+
var dx4;
135+
var dx5;
136+
var dx6;
137+
var dx7;
138+
var dx8;
139+
var dy0;
140+
var dy1;
141+
var dy2;
142+
var dy3;
143+
var dy4;
144+
var dy5;
145+
var dy6;
146+
var dy7;
147+
var dy8;
148+
var dz0;
149+
var dz1;
150+
var dz2;
151+
var dz3;
152+
var dz4;
153+
var dz5;
154+
var dz6;
155+
var dz7;
156+
var dz8;
157+
var sh;
158+
var S0;
159+
var S1;
160+
var S2;
161+
var S3;
162+
var S4;
163+
var S5;
164+
var S6;
165+
var S7;
166+
var S8;
167+
var sx;
168+
var sy;
169+
var sz;
170+
var ix;
171+
var iy;
172+
var iz;
173+
var i0;
174+
var i1;
175+
var i2;
176+
var i3;
177+
var i4;
178+
var i5;
179+
var i6;
180+
var i7;
181+
var i8;
182+
183+
// Note on variable naming convention: S#, dx#, dy#, dz#, i# where # corresponds to the loop number, with `0` being the innermost loop...
184+
185+
// Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments...
186+
sh = x.shape;
187+
sx = x.strides;
188+
sy = y.strides;
189+
sz = z.strides;
190+
if ( isRowMajor( x.order ) ) {
191+
// For row-major ndarrays, the last dimensions have the fastest changing indices...
192+
S0 = sh[ 8 ];
193+
S1 = sh[ 7 ];
194+
S2 = sh[ 6 ];
195+
S3 = sh[ 5 ];
196+
S4 = sh[ 4 ];
197+
S5 = sh[ 3 ];
198+
S6 = sh[ 2 ];
199+
S7 = sh[ 1 ];
200+
S8 = sh[ 0 ];
201+
dx0 = sx[ 8 ]; // offset increment for innermost loop
202+
dx1 = sx[ 7 ] - ( S0*sx[8] );
203+
dx2 = sx[ 6 ] - ( S1*sx[7] );
204+
dx3 = sx[ 5 ] - ( S2*sx[6] );
205+
dx4 = sx[ 4 ] - ( S3*sx[5] );
206+
dx5 = sx[ 3 ] - ( S4*sx[4] );
207+
dx6 = sx[ 2 ] - ( S5*sx[3] );
208+
dx7 = sx[ 1 ] - ( S6*sx[2] );
209+
dx8 = sx[ 0 ] - ( S7*sx[1] ); // offset increment for outermost loop
210+
dy0 = sy[ 8 ];
211+
dy1 = sy[ 7 ] - ( S0*sy[8] );
212+
dy2 = sy[ 6 ] - ( S1*sy[7] );
213+
dy3 = sy[ 5 ] - ( S2*sy[6] );
214+
dy4 = sy[ 4 ] - ( S3*sy[5] );
215+
dy5 = sy[ 3 ] - ( S4*sy[4] );
216+
dy6 = sy[ 2 ] - ( S5*sy[3] );
217+
dy7 = sy[ 1 ] - ( S6*sy[2] );
218+
dy8 = sy[ 0 ] - ( S7*sy[1] );
219+
dz0 = sz[ 8 ];
220+
dz1 = sz[ 7 ] - ( S0*sz[8] );
221+
dz2 = sz[ 6 ] - ( S1*sz[7] );
222+
dz3 = sz[ 5 ] - ( S2*sz[6] );
223+
dz4 = sz[ 4 ] - ( S3*sz[5] );
224+
dz5 = sz[ 3 ] - ( S4*sz[4] );
225+
dz6 = sz[ 2 ] - ( S5*sz[3] );
226+
dz7 = sz[ 1 ] - ( S6*sz[2] );
227+
dz8 = sz[ 0 ] - ( S7*sz[1] );
228+
} else { // order === 'column-major'
229+
// For column-major ndarrays, the first dimensions have the fastest changing indices...
230+
S0 = sh[ 0 ];
231+
S1 = sh[ 1 ];
232+
S2 = sh[ 2 ];
233+
S3 = sh[ 3 ];
234+
S4 = sh[ 4 ];
235+
S5 = sh[ 5 ];
236+
S6 = sh[ 6 ];
237+
S7 = sh[ 7 ];
238+
S8 = sh[ 8 ];
239+
dx0 = sx[ 0 ]; // offset increment for innermost loop
240+
dx1 = sx[ 1 ] - ( S0*sx[0] );
241+
dx2 = sx[ 2 ] - ( S1*sx[1] );
242+
dx3 = sx[ 3 ] - ( S2*sx[2] );
243+
dx4 = sx[ 4 ] - ( S3*sx[3] );
244+
dx5 = sx[ 5 ] - ( S4*sx[4] );
245+
dx6 = sx[ 6 ] - ( S5*sx[5] );
246+
dx7 = sx[ 7 ] - ( S6*sx[6] );
247+
dx8 = sx[ 8 ] - ( S7*sx[7] ); // offset increment for outermost loop
248+
dy0 = sy[ 0 ];
249+
dy1 = sy[ 1 ] - ( S0*sy[0] );
250+
dy2 = sy[ 2 ] - ( S1*sy[1] );
251+
dy3 = sy[ 3 ] - ( S2*sy[2] );
252+
dy4 = sy[ 4 ] - ( S3*sy[3] );
253+
dy5 = sy[ 5 ] - ( S4*sy[4] );
254+
dy6 = sy[ 6 ] - ( S5*sy[5] );
255+
dy7 = sy[ 7 ] - ( S6*sy[6] );
256+
dy8 = sy[ 8 ] - ( S7*sy[7] );
257+
dz0 = sz[ 0 ];
258+
dz1 = sz[ 1 ] - ( S0*sz[0] );
259+
dz2 = sz[ 2 ] - ( S1*sz[1] );
260+
dz3 = sz[ 3 ] - ( S2*sz[2] );
261+
dz4 = sz[ 4 ] - ( S3*sz[3] );
262+
dz5 = sz[ 5 ] - ( S4*sz[4] );
263+
dz6 = sz[ 6 ] - ( S5*sz[5] );
264+
dz7 = sz[ 7 ] - ( S6*sz[6] );
265+
dz8 = sz[ 8 ] - ( S7*sz[7] );
266+
}
267+
// Set the pointers to the first indexed elements in the respective ndarrays...
268+
ix = x.offset;
269+
iy = y.offset;
270+
iz = z.offset;
271+
272+
// Cache references to the input and output ndarray buffers...
273+
xbuf = x.data;
274+
ybuf = y.data;
275+
zbuf = z.data;
276+
277+
// Cache accessors:
278+
xget = x.accessors[ 0 ];
279+
yget = y.accessors[ 0 ];
280+
zset = z.accessors[ 1 ];
281+
282+
// Iterate over the ndarray dimensions...
283+
for ( i8 = 0; i8 < S8; i8++ ) {
284+
for ( i7 = 0; i7 < S7; i7++ ) {
285+
for ( i6 = 0; i6 < S6; i6++ ) {
286+
for ( i5 = 0; i5 < S5; i5++ ) {
287+
for ( i4 = 0; i4 < S4; i4++ ) {
288+
for ( i3 = 0; i3 < S3; i3++ ) {
289+
for ( i2 = 0; i2 < S2; i2++ ) {
290+
for ( i1 = 0; i1 < S1; i1++ ) {
291+
for ( i0 = 0; i0 < S0; i0++ ) {
292+
zset( zbuf, iz, fcn( xget( xbuf, ix ), yget( ybuf, iy ) ) );
293+
ix += dx0;
294+
iy += dy0;
295+
iz += dz0;
296+
}
297+
ix += dx1;
298+
iy += dy1;
299+
iz += dz1;
300+
}
301+
ix += dx2;
302+
iy += dy2;
303+
iz += dz2;
304+
}
305+
ix += dx3;
306+
iy += dy3;
307+
iz += dz3;
308+
}
309+
ix += dx4;
310+
iy += dy4;
311+
iz += dz4;
312+
}
313+
ix += dx5;
314+
iy += dy5;
315+
iz += dz5;
316+
}
317+
ix += dx6;
318+
iy += dy6;
319+
iz += dz6;
320+
}
321+
ix += dx7;
322+
iy += dy7;
323+
iz += dz7;
324+
}
325+
ix += dx8;
326+
iy += dy8;
327+
iz += dz8;
328+
}
329+
}
330+
331+
332+
// EXPORTS //
333+
334+
module.exports = binary9d;

0 commit comments

Comments
 (0)