Skip to content

Commit fb74648

Browse files
committed
feat: add 4d 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 92b4704 commit fb74648

File tree

1 file changed

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

1 file changed

+211
-0
lines changed
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
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_H
20+
#define STDLIB_NDARRAY_BASE_EVERY_MACROS_4D_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 four-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_4D_LOOP_PREMABLE {
39+
* // Innermost loop body...
40+
* }
41+
* STDLIB_NDARRAY_EVERY_4D_LOOP_EPILOGUE
42+
*/
43+
#define STDLIB_NDARRAY_EVERY_4D_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 d3x1; \
54+
int64_t S0; \
55+
int64_t S1; \
56+
int64_t S2; \
57+
int64_t S3; \
58+
int64_t i0; \
59+
int64_t i1; \
60+
int64_t i2; \
61+
int64_t i3; \
62+
/* Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments... */ \
63+
if ( stdlib_ndarray_order( x1 ) == STDLIB_NDARRAY_ROW_MAJOR ) { \
64+
/* For row-major ndarrays, the last dimensions have the fastest changing indices... */ \
65+
S0 = shape[ 3 ]; \
66+
S1 = shape[ 2 ]; \
67+
S2 = shape[ 1 ]; \
68+
S3 = shape[ 0 ]; \
69+
d0x1 = sx1[ 3 ]; \
70+
d1x1 = sx1[ 2 ] - ( S0*sx1[3] ); \
71+
d2x1 = sx1[ 1 ] - ( S1*sx1[2] ); \
72+
d3x1 = sx1[ 0 ] - ( S2*sx1[1] ); \
73+
} else { \
74+
/* For column-major ndarrays, the first dimensions have the fastest changing indices... */ \
75+
S0 = shape[ 0 ]; \
76+
S1 = shape[ 1 ]; \
77+
S2 = shape[ 2 ]; \
78+
S3 = shape[ 3 ]; \
79+
d0x1 = sx1[ 0 ]; \
80+
d1x1 = sx1[ 1 ] - ( S0*sx1[0] ); \
81+
d2x1 = sx1[ 2 ] - ( S1*sx1[1] ); \
82+
d3x1 = sx1[ 3 ] - ( S2*sx1[2] ); \
83+
} \
84+
/* Set a pointer to the first indexed elements... */ \
85+
px1 += stdlib_ndarray_offset( x1 ); \
86+
px2 += stdlib_ndarray_offset( x2 ); \
87+
/* Iterate over the ndarray dimensions... */ \
88+
for ( i3 = 0; i3 < S3; i3++, px1 += d3x1 ) { \
89+
for ( i2 = 0; i2 < S2; i2++, px1 += d2x1 ) { \
90+
for ( i1 = 0; i1 < S1; i1++, px1 += d1x1 ) { \
91+
for ( i0 = 0; i0 < S0; i0++, px1 += d0x1 )
92+
93+
/**
94+
* Macro containing the epilogue for nested loops which operate on elements of a four-dimensional ndarray.
95+
*
96+
* @example
97+
* STDLIB_NDARRAY_EVERY_4D_LOOP_PREMABLE {
98+
* // Innermost loop body...
99+
* }
100+
* STDLIB_NDARRAY_EVERY_4D_LOOP_EPILOGUE
101+
*/
102+
#define STDLIB_NDARRAY_EVERY_4D_LOOP_EPILOGUE \
103+
} \
104+
} \
105+
} \
106+
*px2 = true;
107+
108+
/**
109+
* Macro for a four-dimensional ndarray loop which inlines an expression.
110+
*
111+
* ## Notes
112+
*
113+
* - Retrieves each input ndarray element according to type `tin` via the pointer `px1` as `in1`.
114+
* - Expects a provided expression to operate on `tin in1`.
115+
* - Stores the final result in an output ndarray via the pointer `px2`.
116+
*
117+
* @param tin input type
118+
* @param expr expression to inline
119+
*
120+
* @example
121+
* STDLIB_NDARRAY_EVERY_4D_LOOP_INLINE( double, in1 )
122+
*/
123+
#define STDLIB_NDARRAY_EVERY_4D_LOOP_INLINE( tin, expr ) \
124+
STDLIB_NDARRAY_EVERY_4D_LOOP_PREAMBLE { \
125+
const tin in1 = *(tin *)px1; \
126+
if ( !( expr ) ) { \
127+
*px2 = false; \
128+
return; \
129+
} \
130+
} \
131+
STDLIB_NDARRAY_EVERY_4D_LOOP_EPILOGUE
132+
133+
/**
134+
* Macro for a four-dimensional ndarray loop which invokes a callback.
135+
*
136+
* ## Notes
137+
*
138+
* - Retrieves each ndarray element according to type `tin` via the pointer `px1`.
139+
* - Stores the final result in an output ndarray via the pointer `px2`.
140+
*
141+
* @param tin input type
142+
*
143+
* @example
144+
* // e.g., d_x
145+
* STDLIB_NDARRAY_EVERY_4D_LOOP_CLBK( double )
146+
*/
147+
#define STDLIB_NDARRAY_EVERY_4D_LOOP_CLBK( tin ) \
148+
STDLIB_NDARRAY_EVERY_4D_LOOP_PREAMBLE { \
149+
const tin x = *(tin *)px1; \
150+
if ( !( f( x ) ) ) { \
151+
*px2 = false; \
152+
return; \
153+
} \
154+
} \
155+
STDLIB_NDARRAY_EVERY_4D_LOOP_EPILOGUE
156+
157+
/**
158+
* Macro for a four-dimensional ndarray loop which invokes a callback requiring arguments be explicitly cast to a different type.
159+
*
160+
* ## Notes
161+
*
162+
* - Retrieves each ndarray element according to type `tin` via the pointer `px1`.
163+
* - Explicitly casts each function argument to `fin`.
164+
* - Stores the final result in an output ndarray via the pointer `px2`.
165+
*
166+
* @param tin input type
167+
* @param fin callback argument type
168+
*
169+
* @example
170+
* // e.g., f_x_as_d_x
171+
* STDLIB_NDARRAY_EVERY_4D_LOOP_CLBK_ARG_CAST( float, double )
172+
*/
173+
#define STDLIB_NDARRAY_EVERY_4D_LOOP_CLBK_ARG_CAST( tin, fin ) \
174+
STDLIB_NDARRAY_EVERY_4D_LOOP_PREAMBLE { \
175+
const tin x = *(tin *)px1; \
176+
if ( !( f( (fin)x ) ) ) { \
177+
*px2 = false; \
178+
return; \
179+
} \
180+
} \
181+
STDLIB_NDARRAY_EVERY_4D_LOOP_EPILOGUE
182+
183+
/**
184+
* Macro for a four-dimensional ndarray loop which invokes a callback requiring arguments be cast to a different type via casting functions.
185+
*
186+
* ## Notes
187+
*
188+
* - Retrieves each ndarray element according to type `tin` via a pointer `px1`.
189+
* - Explicitly casts each function argument via `cin`.
190+
* - Stores the final result in an output ndarray via the pointer `px2`.
191+
*
192+
* @param tin input type
193+
* @param cin input casting function
194+
*
195+
* @example
196+
* #include "stdlib/complex/float64/ctor.h"
197+
*
198+
* // e.g., f_x_as_z_x
199+
* STDLIB_NDARRAY_EVERY_4D_LOOP_CLBK_ARG_CAST_FCN( float, stdlib_complex128_from_float32 )
200+
*/
201+
#define STDLIB_NDARRAY_EVERY_4D_LOOP_CLBK_ARG_CAST_FCN( tin, cin ) \
202+
STDLIB_NDARRAY_EVERY_4D_LOOP_PREAMBLE { \
203+
const tin x = *(tin *)px1; \
204+
if ( !( f( cin( x ) ) ) ) { \
205+
*px2 = false; \
206+
return; \
207+
} \
208+
} \
209+
STDLIB_NDARRAY_EVERY_4D_LOOP_EPILOGUE
210+
211+
#endif // !STDLIB_NDARRAY_BASE_EVERY_MACROS_4D_H

0 commit comments

Comments
 (0)