Skip to content

Commit 28aa078

Browse files
committed
feat: add 3d 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 b611927 commit 28aa078

File tree

1 file changed

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

1 file changed

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

0 commit comments

Comments
 (0)