Skip to content

Commit 6f0da38

Browse files
committed
feat: add 6d blocked macros
--- 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: na - 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 74fe0a2 commit 6f0da38

File tree

1 file changed

+310
-0
lines changed
  • lib/node_modules/@stdlib/ndarray/base/every/include/stdlib/ndarray/base/every/macros

1 file changed

+310
-0
lines changed
Lines changed: 310 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,310 @@
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+
#ifndef STDLIB_NDARRAY_BASE_EVERY_MACROS_6D_BLOCKED_H
20+
#define STDLIB_NDARRAY_BASE_EVERY_MACROS_6D_BLOCKED_H
21+
22+
#include "stdlib/ndarray/base/nullary/macros/constants.h"
23+
#include "stdlib/ndarray/base/nullary/internal/permute.h"
24+
#include "stdlib/ndarray/base/nullary/internal/range.h"
25+
#include "stdlib/ndarray/base/nullary/internal/sort2ins.h"
26+
#include "stdlib/ndarray/base/bytes_per_element.h"
27+
#include "stdlib/ndarray/ctor.h"
28+
#include <stdbool.h>
29+
#include <stdint.h>
30+
#include <string.h>
31+
32+
/**
33+
* Macro containing the preamble for blocked nested loops which operate on elements of a six-dimensional ndarray.
34+
*
35+
* ## Notes
36+
*
37+
* - Variable naming conventions:
38+
*
39+
* - `sx#`, `pbx#`, `px#`, `ox#`, `nbx#`, and `d@x#` where `#` corresponds to the ndarray argument number, starting at `1`.
40+
* - `S@`, `i@`, `j@`, `o@x#`, and `d@x#` where `@` corresponds to the loop number, with `0` being the innermost loop.
41+
*
42+
* @example
43+
* STDLIB_NDARRAY_EVERY_6D_BLOCKED_LOOP_PREMABLE {
44+
* // Innermost loop body...
45+
* }
46+
* STDLIB_NDARRAY_EVERY_6D_BLOCKED_LOOP_EPILOGUE
47+
*/
48+
#define STDLIB_NDARRAY_EVERY_6D_BLOCKED_LOOP_PREAMBLE \
49+
const struct ndarray *x1 = arrays[ 0 ]; \
50+
const struct ndarray *x2 = arrays[ 1 ]; \
51+
bool *px2 = stdlib_ndarray_data( x2 ); \
52+
int64_t shape[6]; \
53+
int64_t sx1[6]; \
54+
int64_t idx[6]; \
55+
int64_t tmp[6]; \
56+
int64_t bsize; \
57+
uint8_t *pbx1; \
58+
uint8_t *px1; \
59+
int64_t d0x1; \
60+
int64_t d1x1; \
61+
int64_t d2x1; \
62+
int64_t d3x1; \
63+
int64_t d4x1; \
64+
int64_t d5x1; \
65+
int64_t o1x1; \
66+
int64_t o2x1; \
67+
int64_t o3x1; \
68+
int64_t o4x1; \
69+
int64_t o5x1; \
70+
int64_t nbx1; \
71+
int64_t ox1; \
72+
int64_t s0; \
73+
int64_t s1; \
74+
int64_t s2; \
75+
int64_t s3; \
76+
int64_t s4; \
77+
int64_t s5; \
78+
int64_t i0; \
79+
int64_t i1; \
80+
int64_t i2; \
81+
int64_t i3; \
82+
int64_t i4; \
83+
int64_t i5; \
84+
int64_t j0; \
85+
int64_t j1; \
86+
int64_t j2; \
87+
int64_t j3; \
88+
int64_t j4; \
89+
int64_t j5; \
90+
/* Copy strides to prevent mutation to the original ndarray: */ \
91+
memcpy( sx1, stdlib_ndarray_strides( x1 ), sizeof sx1 ); \
92+
/* Create a loop interchange index array for loop order permutation: */ \
93+
stdlib_ndarray_base_nullary_internal_range( 6, idx ); \
94+
/* Sort the input array strides in increasing order (of magnitude): */ \
95+
stdlib_ndarray_base_nullary_internal_sort2ins( 6, sx1, idx ); \
96+
/* Permute the shape (avoiding mutation) according to loop order: */ \
97+
stdlib_ndarray_base_nullary_internal_permute( 6, stdlib_ndarray_shape( x1 ), idx, tmp ); \
98+
memcpy( shape, tmp, sizeof shape ); \
99+
/* Determine the block size... */ \
100+
nbx1 = stdlib_ndarray_bytes_per_element( stdlib_ndarray_dtype( x1 ) ); \
101+
if ( nbx1 == 0 ) { \
102+
bsize = STDLIB_NDARRAY_EVERY_BLOCK_SIZE_IN_ELEMENTS; \
103+
} else { \
104+
bsize = STDLIB_NDARRAY_EVERY_BLOCK_SIZE_IN_BYTES / nbx1; \
105+
} \
106+
/* Cache a pointer to the ndarray buffer... */ \
107+
pbx1 = stdlib_ndarray_data( x1 ); \
108+
/* Cache the byte offset to the first indexed element... */ \
109+
ox1 = stdlib_ndarray_offset( x1 ); \
110+
/* Set a pointer to the first indexed element of the output ndarray... */ \
111+
px2 += stdlib_ndarray_offset( x2 ); \
112+
/* Cache the offset increment for the innermost loop... */ \
113+
d0x1 = sx1[0]; \
114+
/* Iterate over blocks... */ \
115+
for ( j5 = shape[5]; j5 > 0; ) { \
116+
if ( j5 < bsize ) { \
117+
s5 = j5; \
118+
j5 = 0; \
119+
} else { \
120+
s5 = bsize; \
121+
j5 -= bsize; \
122+
} \
123+
o5x1 = ox1 + ( j5*sx1[5] ); \
124+
for ( j4 = shape[4]; j4 > 0; ) { \
125+
if ( j4 < bsize ) { \
126+
s4 = j4; \
127+
j4 = 0; \
128+
} else { \
129+
s4 = bsize; \
130+
j4 -= bsize; \
131+
} \
132+
d5x1 = sx1[5] - ( s4*sx1[4] ); \
133+
o4x1 = o5x1 + ( j4*sx1[4] ); \
134+
for ( j3 = shape[3]; j3 > 0; ) { \
135+
if ( j3 < bsize ) { \
136+
s3 = j3; \
137+
j3 = 0; \
138+
} else { \
139+
s3 = bsize; \
140+
j3 -= bsize; \
141+
} \
142+
d4x1 = sx1[4] - ( s3*sx1[3] ); \
143+
o3x1 = o4x1 + ( j3*sx1[3] ); \
144+
for ( j2 = shape[2]; j2 > 0; ) { \
145+
if ( j2 < bsize ) { \
146+
s2 = j2; \
147+
j2 = 0; \
148+
} else { \
149+
s2 = bsize; \
150+
j2 -= bsize; \
151+
} \
152+
d3x1 = sx1[3] - ( s2*sx1[2] ); \
153+
o2x1 = o3x1 + ( j2*sx1[2] ); \
154+
for ( j1 = shape[1]; j1 > 0; ) { \
155+
if ( j1 < bsize ) { \
156+
s1 = j1; \
157+
j1 = 0; \
158+
} else { \
159+
s1 = bsize; \
160+
j1 -= bsize; \
161+
} \
162+
d2x1 = sx1[2] - ( s1*sx1[1] ); \
163+
o1x1 = o2x1 + ( j1*sx1[1] ); \
164+
for ( j0 = shape[0]; j0 > 0; ) { \
165+
if ( j0 < bsize ) { \
166+
s0 = j0; \
167+
j0 = 0; \
168+
} else { \
169+
s0 = bsize; \
170+
j0 -= bsize; \
171+
} \
172+
/* Compute the pointer to the first ndarray element in the current block... */ \
173+
px1 = pbx1 + o1x1 + ( j0*sx1[0] ); \
174+
/* Compute the loop offset increment... */ \
175+
d1x1 = sx1[1] - ( s0*sx1[0] ); \
176+
/* Iterate over the ndarray dimensions... */ \
177+
for ( i5 = 0; i5 < s5; i5++, px1 += d5x1 ) { \
178+
for ( i4 = 0; i4 < s4; i4++, px1 += d4x1 ) { \
179+
for ( i3 = 0; i3 < s3; i3++, px1 += d3x1 ) { \
180+
for ( i2 = 0; i2 < s2; i2++, px1 += d2x1 ) { \
181+
for ( i1 = 0; i1 < s1; i1++, px1 += d1x1 ) { \
182+
for ( i0 = 0; i0 < s0; i0++, px1 += d0x1 )
183+
184+
/**
185+
* Macro containing the epilogue for blocked nested loops which operate on elements of a six-dimensional ndarray.
186+
*
187+
* @example
188+
* STDLIB_NDARRAY_EVERY_6D_BLOCKED_LOOP_PREMABLE {
189+
* // Innermost loop body...
190+
* }
191+
* STDLIB_NDARRAY_EVERY_6D_BLOCKED_LOOP_EPILOGUE
192+
*/
193+
#define STDLIB_NDARRAY_EVERY_6D_BLOCKED_LOOP_EPILOGUE \
194+
} \
195+
} \
196+
} \
197+
} \
198+
} \
199+
} \
200+
} \
201+
} \
202+
} \
203+
} \
204+
} \
205+
*px2 = true;
206+
207+
/**
208+
* Macro for a blocked six-dimensional ndarray loop which inlines an expression.
209+
*
210+
* ## Notes
211+
*
212+
* - Retrieves each input ndarray element according to type `tin` via the pointer `px1` as `in1`.
213+
* - Expects a provided expression to operate on `tin in1`.
214+
* - Stores the final result in an output ndarray via the pointer `px2`.
215+
*
216+
* @param tin input type
217+
* @param expr expression to inline
218+
*
219+
* @example
220+
* STDLIB_NDARRAY_EVERY_6D_BLOCKED_LOOP_INLINE( double, in1 )
221+
*/
222+
#define STDLIB_NDARRAY_EVERY_6D_BLOCKED_LOOP_INLINE( tin, expr ) \
223+
STDLIB_NDARRAY_EVERY_6D_BLOCKED_LOOP_PREAMBLE { \
224+
const tin in1 = *(tin *)px1; \
225+
if ( !( expr ) ) { \
226+
*px2 = false; \
227+
return; \
228+
} \
229+
} \
230+
STDLIB_NDARRAY_EVERY_6D_BLOCKED_LOOP_EPILOGUE
231+
232+
/**
233+
* Macro for a blocked six-dimensional ndarray loop which invokes a callback.
234+
*
235+
* ## Notes
236+
*
237+
* - Retrieves each ndarray element according to type `tin` via the pointer `px1`.
238+
* - Stores the final result in an output ndarray via the pointer `px2`.
239+
*
240+
* @param tin input type
241+
*
242+
* @example
243+
* // e.g., d_x
244+
* STDLIB_NDARRAY_EVERY_6D_BLOCKED_LOOP_CLBK( double )
245+
*/
246+
#define STDLIB_NDARRAY_EVERY_6D_BLOCKED_LOOP_CLBK( tin ) \
247+
STDLIB_NDARRAY_EVERY_6D_BLOCKED_LOOP_PREAMBLE { \
248+
const tin x = *(tin *)px1; \
249+
if ( !( f( x ) ) ) { \
250+
*px2 = false; \
251+
return; \
252+
} \
253+
} \
254+
STDLIB_NDARRAY_EVERY_6D_BLOCKED_LOOP_EPILOGUE
255+
256+
/**
257+
* Macro for a blocked six-dimensional ndarray loop which invokes a callback requiring arguments be explicitly cast to a different type.
258+
*
259+
* ## Notes
260+
*
261+
* - Retrieves each ndarray element according to type `tin` via the pointer `px1`.
262+
* - Explicitly casts each function argument to `fin`.
263+
* - Stores the final result in an output ndarray via the pointer `px2`.
264+
*
265+
* @param tin input type
266+
* @param fin callback argument type
267+
*
268+
* @example
269+
* // e.g., f_x_as_d_x
270+
* STDLIB_NDARRAY_EVERY_6D_BLOCKED_LOOP_CLBK_ARG_CAST( float, double )
271+
*/
272+
#define STDLIB_NDARRAY_EVERY_6D_BLOCKED_LOOP_CLBK_ARG_CAST( tin, fin ) \
273+
STDLIB_NDARRAY_EVERY_6D_BLOCKED_LOOP_PREAMBLE { \
274+
const tin x = *(tin *)px1; \
275+
if ( !( f( (fin)x ) ) ) { \
276+
*px2 = false; \
277+
return; \
278+
} \
279+
} \
280+
STDLIB_NDARRAY_EVERY_6D_BLOCKED_LOOP_EPILOGUE
281+
282+
/**
283+
* Macro for a blocked six-dimensional ndarray loop which invokes a callback requiring arguments be cast to a different type via casting functions.
284+
*
285+
* ## Notes
286+
*
287+
* - Retrieves each ndarray element according to type `tin` via a pointer `px1`.
288+
* - Explicitly casts each function argument via `cin`.
289+
* - Stores the final result in an output ndarray via the pointer `px2`.
290+
*
291+
* @param tin input type
292+
* @param cin input casting function
293+
*
294+
* @example
295+
* #include "stdlib/complex/float64/ctor.h"
296+
*
297+
* // e.g., f_x_as_z_x
298+
* STDLIB_NDARRAY_EVERY_6D_BLOCKED_LOOP_CLBK_ARG_CAST_FCN( float, stdlib_complex128_from_float32 )
299+
*/
300+
#define STDLIB_NDARRAY_EVERY_6D_BLOCKED_LOOP_CLBK_ARG_CAST_FCN( tin, cin ) \
301+
STDLIB_NDARRAY_EVERY_6D_BLOCKED_LOOP_PREAMBLE { \
302+
const tin x = *(tin *)px1; \
303+
if ( !( f( cin( x ) ) ) ) { \
304+
*px2 = false; \
305+
return; \
306+
} \
307+
} \
308+
STDLIB_NDARRAY_EVERY_6D_BLOCKED_LOOP_EPILOGUE
309+
310+
#endif // !STDLIB_NDARRAY_BASE_EVERY_MACROS_6D_BLOCKED_H

0 commit comments

Comments
 (0)