|
| 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_ND_H |
| 20 | +#define STDLIB_NDARRAY_BASE_EVERY_MACROS_ND_H |
| 21 | + |
| 22 | +#include "stdlib/ndarray/ctor.h" |
| 23 | +#include "stdlib/ndarray/orders.h" |
| 24 | +#include "stdlib/ndarray/index_modes.h" |
| 25 | +#include "stdlib/ndarray/orders.h" |
| 26 | +#include "stdlib/ndarray/base/vind2bind.h" |
| 27 | +#include <stdint.h> |
| 28 | +#include <stdbool.h> |
| 29 | + |
| 30 | +/** |
| 31 | +* Macro containing the preamble for nested loops which operate on elements of an n-dimensional ndarray. |
| 32 | +* |
| 33 | +* ## Notes |
| 34 | +* |
| 35 | +* - Variable naming conventions: |
| 36 | +* |
| 37 | +* - `sx#`, `px#`, and `d@x#` where `#` corresponds to the ndarray argument number, starting at `1`. |
| 38 | +* - `S@`, `i@`, and `d@x#` where `@` corresponds to the loop number, with `0` being the innermost loop. |
| 39 | +* |
| 40 | +* @example |
| 41 | +* STDLIB_NDARRAY_EVERY_ND_LOOP_PREMABLE { |
| 42 | +* // Innermost loop body... |
| 43 | +* } |
| 44 | +* STDLIB_NDARRAY_EVERY_ND_LOOP_EPILOGUE |
| 45 | +*/ |
| 46 | +#define STDLIB_NDARRAY_EVERY_ND_LOOP_PREAMBLE \ |
| 47 | + const struct ndarray *x1 = arrays[ 0 ]; \ |
| 48 | + const struct ndarray *x2 = arrays[ 1 ]; \ |
| 49 | + enum STDLIB_NDARRAY_INDEX_MODE mx1 = stdlib_ndarray_index_mode( x1 ); \ |
| 50 | + enum STDLIB_NDARRAY_ORDER ordx1 = stdlib_ndarray_order( x1 ); \ |
| 51 | + const int64_t *shape = stdlib_ndarray_shape( x1 ); \ |
| 52 | + uint8_t *pbx1 = stdlib_ndarray_data( x1 ); \ |
| 53 | + int64_t ndims = stdlib_ndarray_ndims( x1 ); \ |
| 54 | + int64_t *sx1 = stdlib_ndarray_strides( x1 ); \ |
| 55 | + int64_t ox1 = stdlib_ndarray_offset( x1 ); \ |
| 56 | + int64_t len = stdlib_ndarray_length( x1 ); \ |
| 57 | + bool *px2 = stdlib_ndarray_data( x2 ); \ |
| 58 | + uint8_t *px1; \ |
| 59 | + int64_t i; \ |
| 60 | + /* Iterate over each ndarray element based on the linear **view** index, regardless as to how the data is stored in memory... */ \ |
| 61 | + for ( i = 0; i < len; i++ ) { \ |
| 62 | + px1 = pbx1 + stdlib_ndarray_vind2bind( ndims, shape, sx1, ox1, ordx1, i, mx1 ); \ |
| 63 | + do |
| 64 | + |
| 65 | +/** |
| 66 | +* Macro containing the epilogue for nested loops which operate on elements of an n-dimensional ndarray. |
| 67 | +* |
| 68 | +* @example |
| 69 | +* STDLIB_NDARRAY_EVERY_ND_LOOP_PREMABLE { |
| 70 | +* // Innermost loop body... |
| 71 | +* } |
| 72 | +* STDLIB_NDARRAY_EVERY_ND_LOOP_EPILOGUE |
| 73 | +*/ |
| 74 | +#define STDLIB_NDARRAY_EVERY_ND_LOOP_EPILOGUE \ |
| 75 | + while( 0 ); \ |
| 76 | + } \ |
| 77 | + *px2 = true; |
| 78 | + |
| 79 | +/** |
| 80 | +* Macro for an n-dimensional ndarray loop which inlines an expression. |
| 81 | +* |
| 82 | +* ## Notes |
| 83 | +* |
| 84 | +* - Retrieves each input ndarray element according to type `tin` via the pointer `px1` as `in1`. |
| 85 | +* - Expects a provided expression to operate on `tin in1`. |
| 86 | +* - Stores the final result in an output ndarray via the pointer `px2`. |
| 87 | +* |
| 88 | +* @param tin input type |
| 89 | +* @param expr expression to inline |
| 90 | +* |
| 91 | +* @example |
| 92 | +* STDLIB_NDARRAY_EVERY_ND_LOOP_INLINE( double, in1 ) |
| 93 | +*/ |
| 94 | +#define STDLIB_NDARRAY_EVERY_ND_LOOP_INLINE( tin, expr ) \ |
| 95 | + STDLIB_NDARRAY_EVERY_ND_LOOP_PREAMBLE { \ |
| 96 | + const tin in1 = *(tin *)px1; \ |
| 97 | + if ( !( expr ) ) { \ |
| 98 | + *px2 = false; \ |
| 99 | + return; \ |
| 100 | + } \ |
| 101 | + } \ |
| 102 | + STDLIB_NDARRAY_EVERY_ND_LOOP_EPILOGUE |
| 103 | + |
| 104 | +/** |
| 105 | +* Macro for an n-dimensional ndarray loop which invokes a callback. |
| 106 | +* |
| 107 | +* ## Notes |
| 108 | +* |
| 109 | +* - Retrieves each ndarray element according to type `tin` via the pointer `px1`. |
| 110 | +* - Stores the final result in an output ndarray via the pointer `px2`. |
| 111 | +* |
| 112 | +* @param tin input type |
| 113 | +* |
| 114 | +* @example |
| 115 | +* // e.g., d_x |
| 116 | +* STDLIB_NDARRAY_EVERY_ND_LOOP_CLBK( double ) |
| 117 | +*/ |
| 118 | +#define STDLIB_NDARRAY_EVERY_ND_LOOP_CLBK( tin ) \ |
| 119 | + STDLIB_NDARRAY_EVERY_ND_LOOP_PREAMBLE { \ |
| 120 | + const tin x = *(tin *)px1; \ |
| 121 | + if ( !( f( x ) ) ) { \ |
| 122 | + *px2 = false; \ |
| 123 | + return; \ |
| 124 | + } \ |
| 125 | + } \ |
| 126 | + STDLIB_NDARRAY_EVERY_ND_LOOP_EPILOGUE |
| 127 | + |
| 128 | +/** |
| 129 | +* Macro for an n-dimensional ndarray loop which invokes a callback requiring arguments be explicitly cast to a different type. |
| 130 | +* |
| 131 | +* ## Notes |
| 132 | +* |
| 133 | +* - Retrieves each ndarray element according to type `tin` via the pointer `px1`. |
| 134 | +* - Explicitly casts each function argument to `fin`. |
| 135 | +* - Stores the final result in an output ndarray via the pointer `px2`. |
| 136 | +* |
| 137 | +* @param tin input type |
| 138 | +* @param fin callback argument type |
| 139 | +* |
| 140 | +* @example |
| 141 | +* // e.g., f_x_as_d_x |
| 142 | +* STDLIB_NDARRAY_EVERY_ND_LOOP_CLBK_ARG_CAST( float, double ) |
| 143 | +*/ |
| 144 | +#define STDLIB_NDARRAY_EVERY_ND_LOOP_CLBK_ARG_CAST( tin, fin ) \ |
| 145 | + STDLIB_NDARRAY_EVERY_ND_LOOP_PREAMBLE { \ |
| 146 | + const tin x = *(tin *)px1; \ |
| 147 | + if ( !( f( (fin)x ) ) ) { \ |
| 148 | + *px2 = false; \ |
| 149 | + return; \ |
| 150 | + } \ |
| 151 | + } \ |
| 152 | + STDLIB_NDARRAY_EVERY_ND_LOOP_EPILOGUE |
| 153 | + |
| 154 | +/** |
| 155 | +* Macro for an n-dimensional ndarray loop which invokes a callback requiring arguments be cast to a different type via casting functions. |
| 156 | +* |
| 157 | +* ## Notes |
| 158 | +* |
| 159 | +* - Retrieves each ndarray element according to type `tin` via a pointer `px1`. |
| 160 | +* - Explicitly casts each function argument via `cin`. |
| 161 | +* - Stores the final result in an output ndarray via the pointer `px2`. |
| 162 | +* |
| 163 | +* @param tin input type |
| 164 | +* @param cin input casting function |
| 165 | +* |
| 166 | +* @example |
| 167 | +* #include "stdlib/complex/float64/ctor.h" |
| 168 | +* |
| 169 | +* // e.g., f_x_as_z_x |
| 170 | +* STDLIB_NDARRAY_EVERY_ND_LOOP_CLBK_ARG_CAST_FCN( float, stdlib_complex128_from_float32 ) |
| 171 | +*/ |
| 172 | +#define STDLIB_NDARRAY_EVERY_ND_LOOP_CLBK_ARG_CAST_FCN( tin, cin ) \ |
| 173 | + STDLIB_NDARRAY_EVERY_ND_LOOP_PREAMBLE { \ |
| 174 | + const tin x = *(tin *)px1; \ |
| 175 | + if ( !( f( cin( x ) ) ) ) { \ |
| 176 | + *px2 = false; \ |
| 177 | + return; \ |
| 178 | + } \ |
| 179 | + } \ |
| 180 | + STDLIB_NDARRAY_EVERY_ND_LOOP_EPILOGUE |
| 181 | + |
| 182 | +#endif // !STDLIB_NDARRAY_BASE_EVERY_MACROS_ND_H |
0 commit comments