Skip to content

Commit 92b4704

Browse files
committed
feat: add 3d 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 382eff4 commit 92b4704

File tree

1 file changed

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

1 file changed

+202
-0
lines changed
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
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_H
20+
#define STDLIB_NDARRAY_BASE_EVERY_MACROS_3D_H
21+
22+
#include "stdlib/ndarray/ctor.h"
23+
#include "stdlib/ndarray/orders.h"
24+
#include <stdint.h>
25+
#include <stdbool.h>
26+
27+
/**
28+
* Macro containing the preamble for nested loops which operate on elements of a three-dimensional ndarray.
29+
*
30+
* ## Notes
31+
*
32+
* - Variable naming conventions:
33+
*
34+
* - `sx#`, `px#`, and `d@x#` where `#` corresponds to the ndarray argument number, starting at `1`.
35+
* - `S@`, `i@`, and `d@x#` where `@` corresponds to the loop number, with `0` being the innermost loop.
36+
*
37+
* @example
38+
* STDLIB_NDARRAY_EVERY_3D_LOOP_PREMABLE {
39+
* // Innermost loop body...
40+
* }
41+
* STDLIB_NDARRAY_EVERY_3D_LOOP_EPILOGUE
42+
*/
43+
#define STDLIB_NDARRAY_EVERY_3D_LOOP_PREAMBLE \
44+
const struct ndarray *x1 = arrays[ 0 ]; \
45+
const struct ndarray *x2 = arrays[ 1 ]; \
46+
const int64_t *shape = stdlib_ndarray_shape( x1 ); \
47+
const int64_t *sx1 = stdlib_ndarray_strides( x1 ); \
48+
uint8_t *px1 = stdlib_ndarray_data( x1 ); \
49+
bool *px2 = stdlib_ndarray_data( x2 ); \
50+
int64_t d0x1; \
51+
int64_t d1x1; \
52+
int64_t d2x1; \
53+
int64_t S0; \
54+
int64_t S1; \
55+
int64_t S2; \
56+
int64_t i0; \
57+
int64_t i1; \
58+
int64_t i2; \
59+
/* Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments... */ \
60+
if ( stdlib_ndarray_order( x1 ) == STDLIB_NDARRAY_ROW_MAJOR ) { \
61+
/* For row-major ndarrays, the last dimensions have the fastest changing indices... */ \
62+
S0 = shape[ 2 ]; \
63+
S1 = shape[ 1 ]; \
64+
S2 = shape[ 0 ]; \
65+
d0x1 = sx1[ 2 ]; \
66+
d1x1 = sx1[ 1 ] - ( S0*sx1[2] ); \
67+
d2x1 = sx1[ 0 ] - ( S1*sx1[1] ); \
68+
} else { \
69+
/* For column-major ndarrays, the first dimensions have the fastest changing indices... */ \
70+
S0 = shape[ 0 ]; \
71+
S1 = shape[ 1 ]; \
72+
S2 = shape[ 2 ]; \
73+
d0x1 = sx1[ 0 ]; \
74+
d1x1 = sx1[ 1 ] - ( S0*sx1[0] ); \
75+
d2x1 = sx1[ 2 ] - ( S1*sx1[1] ); \
76+
} \
77+
/* Set a pointer to the first indexed elements... */ \
78+
px1 += stdlib_ndarray_offset( x1 ); \
79+
px2 += stdlib_ndarray_offset( x2 ); \
80+
/* Iterate over the ndarray dimensions... */ \
81+
for ( i2 = 0; i2 < S2; i2++, px1 += d2x1 ) { \
82+
for ( i1 = 0; i1 < S1; i1++, px1 += d1x1 ) { \
83+
for ( i0 = 0; i0 < S0; i0++, px1 += d0x1 )
84+
85+
/**
86+
* Macro containing the epilogue for nested loops which operate on elements of a three-dimensional ndarray.
87+
*
88+
* @example
89+
* STDLIB_NDARRAY_EVERY_3D_LOOP_PREMABLE {
90+
* // Innermost loop body...
91+
* }
92+
* STDLIB_NDARRAY_EVERY_3D_LOOP_EPILOGUE
93+
*/
94+
#define STDLIB_NDARRAY_EVERY_3D_LOOP_EPILOGUE \
95+
} \
96+
} \
97+
*px2 = true;
98+
99+
/**
100+
* Macro for a three-dimensional ndarray loop which inlines an expression.
101+
*
102+
* ## Notes
103+
*
104+
* - Retrieves each input ndarray element according to type `tin` via the pointer `px1` as `in1`.
105+
* - Expects a provided expression to operate on `tin in1`.
106+
* - Stores the final result in an output ndarray via the pointer `px2`.
107+
*
108+
* @param tin input type
109+
* @param expr expression to inline
110+
*
111+
* @example
112+
* STDLIB_NDARRAY_EVERY_3D_LOOP_INLINE( double, in1 )
113+
*/
114+
#define STDLIB_NDARRAY_EVERY_3D_LOOP_INLINE( tin, expr ) \
115+
STDLIB_NDARRAY_EVERY_3D_LOOP_PREAMBLE { \
116+
const tin in1 = *(tin *)px1; \
117+
if ( !( expr ) ) { \
118+
*px2 = false; \
119+
return; \
120+
} \
121+
} \
122+
STDLIB_NDARRAY_EVERY_3D_LOOP_EPILOGUE
123+
124+
/**
125+
* Macro for a three-dimensional ndarray loop which invokes a callback.
126+
*
127+
* ## Notes
128+
*
129+
* - Retrieves each ndarray element according to type `tin` via the pointer `px1`.
130+
* - Stores the final result in an output ndarray via the pointer `px2`.
131+
*
132+
* @param tin input type
133+
*
134+
* @example
135+
* // e.g., d_x
136+
* STDLIB_NDARRAY_EVERY_3D_LOOP_CLBK( double )
137+
*/
138+
#define STDLIB_NDARRAY_EVERY_3D_LOOP_CLBK( tin ) \
139+
STDLIB_NDARRAY_EVERY_3D_LOOP_PREAMBLE { \
140+
const tin x = *(tin *)px1; \
141+
if ( !( f( x ) ) ) { \
142+
*px2 = false; \
143+
return; \
144+
} \
145+
} \
146+
STDLIB_NDARRAY_EVERY_3D_LOOP_EPILOGUE
147+
148+
/**
149+
* Macro for a three-dimensional ndarray loop which invokes a callback requiring arguments be explicitly cast to a different type.
150+
*
151+
* ## Notes
152+
*
153+
* - Retrieves each ndarray element according to type `tin` via the pointer `px1`.
154+
* - Explicitly casts each function argument to `fin`.
155+
* - Stores the final result in an output ndarray via the pointer `px2`.
156+
*
157+
* @param tin input type
158+
* @param fin callback argument type
159+
*
160+
* @example
161+
* // e.g., f_x_as_d_x
162+
* STDLIB_NDARRAY_EVERY_3D_LOOP_CLBK_ARG_CAST( float, double )
163+
*/
164+
#define STDLIB_NDARRAY_EVERY_3D_LOOP_CLBK_ARG_CAST( tin, fin ) \
165+
STDLIB_NDARRAY_EVERY_3D_LOOP_PREAMBLE { \
166+
const tin x = *(tin *)px1; \
167+
if ( !( f( (fin)x ) ) ) { \
168+
*px2 = false; \
169+
return; \
170+
} \
171+
} \
172+
STDLIB_NDARRAY_EVERY_3D_LOOP_EPILOGUE
173+
174+
/**
175+
* Macro for a three-dimensional ndarray loop which invokes a callback requiring arguments be cast to a different type via casting functions.
176+
*
177+
* ## Notes
178+
*
179+
* - Retrieves each ndarray element according to type `tin` via a pointer `px1`.
180+
* - Explicitly casts each function argument via `cin`.
181+
* - Stores the final result in an output ndarray via the pointer `px2`.
182+
*
183+
* @param tin input type
184+
* @param cin input casting function
185+
*
186+
* @example
187+
* #include "stdlib/complex/float64/ctor.h"
188+
*
189+
* // e.g., f_x_as_z_x
190+
* STDLIB_NDARRAY_EVERY_3D_LOOP_CLBK_ARG_CAST_FCN( float, stdlib_complex128_from_float32 )
191+
*/
192+
#define STDLIB_NDARRAY_EVERY_3D_LOOP_CLBK_ARG_CAST_FCN( tin, cin ) \
193+
STDLIB_NDARRAY_EVERY_3D_LOOP_PREAMBLE { \
194+
const tin x = *(tin *)px1; \
195+
if ( !( f( cin( x ) ) ) ) { \
196+
*px2 = false; \
197+
return; \
198+
} \
199+
} \
200+
STDLIB_NDARRAY_EVERY_3D_LOOP_EPILOGUE
201+
202+
#endif // !STDLIB_NDARRAY_BASE_EVERY_MACROS_3D_H

0 commit comments

Comments
 (0)