Skip to content

Commit 30a9f4d

Browse files
committed
feat: add 1d 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 b4d1f2a commit 30a9f4d

File tree

1 file changed

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

1 file changed

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

0 commit comments

Comments
 (0)