Skip to content

Commit 26395f5

Browse files
committed
feat: add 4d 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 28aa078 commit 26395f5

File tree

1 file changed

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

1 file changed

+274
-0
lines changed
Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
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_4D_BLOCKED_H
20+
#define STDLIB_NDARRAY_BASE_EVERY_MACROS_4D_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 four-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_4D_BLOCKED_LOOP_PREMABLE {
44+
* // Innermost loop body...
45+
* }
46+
* STDLIB_NDARRAY_EVERY_4D_BLOCKED_LOOP_EPILOGUE
47+
*/
48+
#define STDLIB_NDARRAY_EVERY_4D_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[4]; \
53+
int64_t sx1[4]; \
54+
int64_t idx[4]; \
55+
int64_t tmp[4]; \
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 o1x1; \
64+
int64_t o2x1; \
65+
int64_t o3x1; \
66+
int64_t nbx1; \
67+
int64_t ox1; \
68+
int64_t s0; \
69+
int64_t s1; \
70+
int64_t s2; \
71+
int64_t s3; \
72+
int64_t i0; \
73+
int64_t i1; \
74+
int64_t i2; \
75+
int64_t i3; \
76+
int64_t j0; \
77+
int64_t j1; \
78+
int64_t j2; \
79+
int64_t j3; \
80+
/* Copy strides to prevent mutation to the original ndarray: */ \
81+
memcpy( sx1, stdlib_ndarray_strides( x1 ), sizeof sx1 ); \
82+
/* Create a loop interchange index array for loop order permutation: */ \
83+
stdlib_ndarray_base_nullary_internal_range( 4, idx ); \
84+
/* Sort the input array strides in increasing order (of magnitude): */ \
85+
stdlib_ndarray_base_nullary_internal_sort2ins( 4, sx1, idx ); \
86+
/* Permute the shape (avoiding mutation) according to loop order: */ \
87+
stdlib_ndarray_base_nullary_internal_permute( 4, stdlib_ndarray_shape( x1 ), idx, tmp ); \
88+
memcpy( shape, tmp, sizeof shape ); \
89+
/* Determine the block size... */ \
90+
nbx1 = stdlib_ndarray_bytes_per_element( stdlib_ndarray_dtype( x1 ) ); \
91+
if ( nbx1 == 0 ) { \
92+
bsize = STDLIB_NDARRAY_EVERY_BLOCK_SIZE_IN_ELEMENTS; \
93+
} else { \
94+
bsize = STDLIB_NDARRAY_EVERY_BLOCK_SIZE_IN_BYTES / nbx1; \
95+
} \
96+
/* Cache a pointer to the ndarray buffer... */ \
97+
pbx1 = stdlib_ndarray_data( x1 ); \
98+
/* Cache a byte offset to the first indexed element... */ \
99+
ox1 = stdlib_ndarray_offset( x1 ); \
100+
/* Set a pointer to the first indexed element of the output ndarray... */ \
101+
px2 += stdlib_ndarray_offset( x2 ); \
102+
/* Cache the offset increment for the innermost loop... */ \
103+
d0x1 = sx1[0]; \
104+
/* Iterate over blocks... */ \
105+
for ( j3 = shape[3]; j3 > 0; ) { \
106+
if ( j3 < bsize ) { \
107+
s3 = j3; \
108+
j3 = 0; \
109+
} else { \
110+
s3 = bsize; \
111+
j3 -= bsize; \
112+
} \
113+
o3x1 = ox1 + ( j3*sx1[3] ); \
114+
for ( j2 = shape[2]; j2 > 0; ) { \
115+
if ( j2 < bsize ) { \
116+
s2 = j2; \
117+
j2 = 0; \
118+
} else { \
119+
s2 = bsize; \
120+
j2 -= bsize; \
121+
} \
122+
d3x1 = sx1[3] - ( s2*sx1[2] ); \
123+
o2x1 = o3x1 + ( j2*sx1[2] ); \
124+
for ( j1 = shape[1]; j1 > 0; ) { \
125+
if ( j1 < bsize ) { \
126+
s1 = j1; \
127+
j1 = 0; \
128+
} else { \
129+
s1 = bsize; \
130+
j1 -= bsize; \
131+
} \
132+
d2x1 = sx1[2] - ( s1*sx1[1] ); \
133+
o1x1 = o2x1 + ( j1*sx1[1] ); \
134+
for ( j0 = shape[0]; j0 > 0; ) { \
135+
if ( j0 < bsize ) { \
136+
s0 = j0; \
137+
j0 = 0; \
138+
} else { \
139+
s0 = bsize; \
140+
j0 -= bsize; \
141+
} \
142+
/* Compute a pointer to the first ndarray element in the current block... */ \
143+
px1 = pbx1 + o1x1 + ( j0*sx1[0] ); \
144+
/* Compute the loop offset increment... */ \
145+
d1x1 = sx1[1] - ( s0*sx1[0] ); \
146+
/* Iterate over the ndarray dimensions... */ \
147+
for ( i3 = 0; i3 < s3; i3++, px1 += d3x1 ) { \
148+
for ( i2 = 0; i2 < s2; i2++, px1 += d2x1 ) { \
149+
for ( i1 = 0; i1 < s1; i1++, px1 += d1x1 ) { \
150+
for ( i0 = 0; i0 < s0; i0++, px1 += d0x1 )
151+
152+
/**
153+
* Macro containing the epilogue for blocked nested loops which operate on elements of a four-dimensional ndarray.
154+
*
155+
* @example
156+
* STDLIB_NDARRAY_EVERY_4D_BLOCKED_LOOP_PREMABLE {
157+
* // Innermost loop body...
158+
* }
159+
* STDLIB_NDARRAY_EVERY_4D_BLOCKED_LOOP_EPILOGUE
160+
*/
161+
#define STDLIB_NDARRAY_EVERY_4D_BLOCKED_LOOP_EPILOGUE \
162+
} \
163+
} \
164+
} \
165+
} \
166+
} \
167+
} \
168+
} \
169+
*px2 = true;
170+
171+
/**
172+
* Macro for a blocked four-dimensional ndarray loop which inlines an expression.
173+
*
174+
* ## Notes
175+
*
176+
* - Retrieves each input ndarray element according to type `tin` via the pointer `px1` as `in1`.
177+
* - Expects a provided expression to operate on `tin in1`.
178+
* - Stores the final result in an output ndarray via the pointer `px2`.
179+
*
180+
* @param tin input type
181+
* @param expr expression to inline
182+
*
183+
* @example
184+
* STDLIB_NDARRAY_EVERY_4D_BLOCKED_LOOP_INLINE( double, in1 )
185+
*/
186+
#define STDLIB_NDARRAY_EVERY_4D_BLOCKED_LOOP_INLINE( tin, expr ) \
187+
STDLIB_NDARRAY_EVERY_4D_BLOCKED_LOOP_PREAMBLE { \
188+
const tin in1 = *(tin *)px1; \
189+
if ( !( expr ) ) { \
190+
*px2 = false; \
191+
return; \
192+
} \
193+
} \
194+
STDLIB_NDARRAY_EVERY_4D_BLOCKED_LOOP_EPILOGUE
195+
196+
/**
197+
* Macro for a blocked four-dimensional ndarray loop which invokes a callback.
198+
*
199+
* ## Notes
200+
*
201+
* - Retrieves each ndarray element according to type `tin` via the pointer `px1`.
202+
* - Stores the final result in an output ndarray via the pointer `px2`.
203+
*
204+
* @param tin input type
205+
*
206+
* @example
207+
* // e.g., d_x
208+
* STDLIB_NDARRAY_EVERY_4D_BLOCKED_LOOP_CLBK( double )
209+
*/
210+
#define STDLIB_NDARRAY_EVERY_4D_BLOCKED_LOOP_CLBK( tin ) \
211+
STDLIB_NDARRAY_EVERY_4D_BLOCKED_LOOP_PREAMBLE { \
212+
const tin x = *(tin *)px1; \
213+
if ( !( f( x ) ) ) { \
214+
*px2 = false; \
215+
return; \
216+
} \
217+
} \
218+
STDLIB_NDARRAY_EVERY_4D_BLOCKED_LOOP_EPILOGUE
219+
220+
/**
221+
* Macro for a blocked four-dimensional ndarray loop which invokes a callback requiring arguments be explicitly cast to a different type.
222+
*
223+
* ## Notes
224+
*
225+
* - Retrieves each ndarray element according to type `tin` via the pointer `px1`.
226+
* - Explicitly casts each function argument to `fin`.
227+
* - Stores the final result in an output ndarray via the pointer `px2`.
228+
*
229+
* @param tin input type
230+
* @param fin callback argument type
231+
*
232+
* @example
233+
* // e.g., f_x_as_d_x
234+
* STDLIB_NDARRAY_EVERY_4D_BLOCKED_LOOP_CLBK_ARG_CAST( float, double )
235+
*/
236+
#define STDLIB_NDARRAY_EVERY_4D_BLOCKED_LOOP_CLBK_ARG_CAST( tin, fin ) \
237+
STDLIB_NDARRAY_EVERY_4D_BLOCKED_LOOP_PREAMBLE { \
238+
const tin x = *(tin *)px1; \
239+
if ( !( f( (fin)x ) ) ) { \
240+
*px2 = false; \
241+
return; \
242+
} \
243+
} \
244+
STDLIB_NDARRAY_EVERY_4D_BLOCKED_LOOP_EPILOGUE
245+
246+
/**
247+
* Macro for a blocked four-dimensional ndarray loop which invokes a callback requiring arguments be cast to a different type via casting functions.
248+
*
249+
* ## Notes
250+
*
251+
* - Retrieves each ndarray element according to type `tin` via a pointer `px1`.
252+
* - Explicitly casts each function argument via `cin`.
253+
* - Stores the final result in an output ndarray via the pointer `px2`.
254+
*
255+
* @param tin input type
256+
* @param cin input casting function
257+
*
258+
* @example
259+
* #include "stdlib/complex/float64/ctor.h"
260+
*
261+
* // e.g., f_x_as_z_x
262+
* STDLIB_NDARRAY_EVERY_4D_BLOCKED_LOOP_CLBK_ARG_CAST_FCN( float, stdlib_complex128_from_float32 )
263+
*/
264+
#define STDLIB_NDARRAY_EVERY_4D_BLOCKED_LOOP_CLBK_ARG_CAST_FCN( tin, cin ) \
265+
STDLIB_NDARRAY_EVERY_4D_BLOCKED_LOOP_PREAMBLE { \
266+
const tin x = *(tin *)px1; \
267+
if ( !( f( cin( x ) ) ) ) { \
268+
*px2 = false; \
269+
return; \
270+
} \
271+
} \
272+
STDLIB_NDARRAY_EVERY_4D_BLOCKED_LOOP_EPILOGUE
273+
274+
#endif // !STDLIB_NDARRAY_BASE_EVERY_MACROS_4D_BLOCKED_H

0 commit comments

Comments
 (0)