|
| 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_10D_H |
| 20 | +#define STDLIB_NDARRAY_BASE_EVERY_MACROS_10D_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 ten-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_10D_LOOP_PREMABLE { |
| 39 | +* // Innermost loop body... |
| 40 | +* } |
| 41 | +* STDLIB_NDARRAY_EVERY_10D_LOOP_EPILOGUE |
| 42 | +*/ |
| 43 | +#define STDLIB_NDARRAY_EVERY_10D_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 d4x1; \ |
| 55 | + int64_t d5x1; \ |
| 56 | + int64_t d6x1; \ |
| 57 | + int64_t d7x1; \ |
| 58 | + int64_t d8x1; \ |
| 59 | + int64_t d9x1; \ |
| 60 | + int64_t S0; \ |
| 61 | + int64_t S1; \ |
| 62 | + int64_t S2; \ |
| 63 | + int64_t S3; \ |
| 64 | + int64_t S4; \ |
| 65 | + int64_t S5; \ |
| 66 | + int64_t S6; \ |
| 67 | + int64_t S7; \ |
| 68 | + int64_t S8; \ |
| 69 | + int64_t S9; \ |
| 70 | + int64_t i0; \ |
| 71 | + int64_t i1; \ |
| 72 | + int64_t i2; \ |
| 73 | + int64_t i3; \ |
| 74 | + int64_t i4; \ |
| 75 | + int64_t i5; \ |
| 76 | + int64_t i6; \ |
| 77 | + int64_t i7; \ |
| 78 | + int64_t i8; \ |
| 79 | + int64_t i9; \ |
| 80 | + /* Extract loop variables for purposes of loop interchange: dimensions and loop offset (pointer) increments... */ \ |
| 81 | + if ( stdlib_ndarray_order( x1 ) == STDLIB_NDARRAY_ROW_MAJOR ) { \ |
| 82 | + /* For row-major ndarrays, the last dimensions have the fastest changing indices... */ \ |
| 83 | + S0 = shape[ 9 ]; \ |
| 84 | + S1 = shape[ 8 ]; \ |
| 85 | + S2 = shape[ 7 ]; \ |
| 86 | + S3 = shape[ 6 ]; \ |
| 87 | + S4 = shape[ 5 ]; \ |
| 88 | + S5 = shape[ 4 ]; \ |
| 89 | + S6 = shape[ 3 ]; \ |
| 90 | + S7 = shape[ 2 ]; \ |
| 91 | + S8 = shape[ 1 ]; \ |
| 92 | + S9 = shape[ 0 ]; \ |
| 93 | + d0x1 = sx1[ 9 ]; \ |
| 94 | + d1x1 = sx1[ 8 ] - ( S0*sx1[9] ); \ |
| 95 | + d2x1 = sx1[ 7 ] - ( S1*sx1[8] ); \ |
| 96 | + d3x1 = sx1[ 6 ] - ( S2*sx1[7] ); \ |
| 97 | + d4x1 = sx1[ 5 ] - ( S3*sx1[6] ); \ |
| 98 | + d5x1 = sx1[ 4 ] - ( S4*sx1[5] ); \ |
| 99 | + d6x1 = sx1[ 3 ] - ( S5*sx1[4] ); \ |
| 100 | + d7x1 = sx1[ 2 ] - ( S6*sx1[3] ); \ |
| 101 | + d8x1 = sx1[ 1 ] - ( S7*sx1[2] ); \ |
| 102 | + d9x1 = sx1[ 0 ] - ( S8*sx1[1] ); \ |
| 103 | + } else { \ |
| 104 | + /* For column-major ndarrays, the first dimensions have the fastest changing indices... */ \ |
| 105 | + S0 = shape[ 0 ]; \ |
| 106 | + S1 = shape[ 1 ]; \ |
| 107 | + S2 = shape[ 2 ]; \ |
| 108 | + S3 = shape[ 3 ]; \ |
| 109 | + S4 = shape[ 4 ]; \ |
| 110 | + S5 = shape[ 5 ]; \ |
| 111 | + S6 = shape[ 6 ]; \ |
| 112 | + S7 = shape[ 7 ]; \ |
| 113 | + S8 = shape[ 8 ]; \ |
| 114 | + S9 = shape[ 9 ]; \ |
| 115 | + d0x1 = sx1[ 0 ]; \ |
| 116 | + d1x1 = sx1[ 1 ] - ( S0*sx1[0] ); \ |
| 117 | + d2x1 = sx1[ 2 ] - ( S1*sx1[1] ); \ |
| 118 | + d3x1 = sx1[ 3 ] - ( S2*sx1[2] ); \ |
| 119 | + d4x1 = sx1[ 4 ] - ( S3*sx1[3] ); \ |
| 120 | + d5x1 = sx1[ 5 ] - ( S4*sx1[4] ); \ |
| 121 | + d6x1 = sx1[ 6 ] - ( S5*sx1[5] ); \ |
| 122 | + d7x1 = sx1[ 7 ] - ( S6*sx1[6] ); \ |
| 123 | + d8x1 = sx1[ 8 ] - ( S7*sx1[7] ); \ |
| 124 | + d9x1 = sx1[ 9 ] - ( S8*sx1[8] ); \ |
| 125 | + } \ |
| 126 | + /* Set a pointer to the first indexed elements... */ \ |
| 127 | + px1 += stdlib_ndarray_offset( x1 ); \ |
| 128 | + px2 += stdlib_ndarray_offset( x2 ); \ |
| 129 | + /* Iterate over the ndarray dimensions... */ \ |
| 130 | + for ( i9 = 0; i9 < S9; i9++, px1 += d9x1 ) { \ |
| 131 | + for ( i8 = 0; i8 < S8; i8++, px1 += d8x1 ) { \ |
| 132 | + for ( i7 = 0; i7 < S7; i7++, px1 += d7x1 ) { \ |
| 133 | + for ( i6 = 0; i6 < S6; i6++, px1 += d6x1 ) { \ |
| 134 | + for ( i5 = 0; i5 < S5; i5++, px1 += d5x1 ) { \ |
| 135 | + for ( i4 = 0; i4 < S4; i4++, px1 += d4x1 ) { \ |
| 136 | + for ( i3 = 0; i3 < S3; i3++, px1 += d3x1 ) { \ |
| 137 | + for ( i2 = 0; i2 < S2; i2++, px1 += d2x1 ) { \ |
| 138 | + for ( i1 = 0; i1 < S1; i1++, px1 += d1x1 ) { \ |
| 139 | + for ( i0 = 0; i0 < S0; i0++, px1 += d0x1 ) |
| 140 | + |
| 141 | +/** |
| 142 | +* Macro containing the epilogue for nested loops which operate on elements of a ten-dimensional ndarray. |
| 143 | +* |
| 144 | +* @example |
| 145 | +* STDLIB_NDARRAY_EVERY_10D_LOOP_PREMABLE { |
| 146 | +* // Innermost loop body... |
| 147 | +* } |
| 148 | +* STDLIB_NDARRAY_EVERY_10D_LOOP_EPILOGUE |
| 149 | +*/ |
| 150 | +#define STDLIB_NDARRAY_EVERY_10D_LOOP_EPILOGUE \ |
| 151 | + } \ |
| 152 | + } \ |
| 153 | + } \ |
| 154 | + } \ |
| 155 | + } \ |
| 156 | + } \ |
| 157 | + } \ |
| 158 | + } \ |
| 159 | + } \ |
| 160 | + *px2 = true; |
| 161 | + |
| 162 | +/** |
| 163 | +* Macro for a ten-dimensional ndarray loop which inlines an expression. |
| 164 | +* |
| 165 | +* ## Notes |
| 166 | +* |
| 167 | +* - Retrieves each input ndarray element according to type `tin` via the pointer `px1` as `in1`. |
| 168 | +* - Expects a provided expression to operate on `tin in1`. |
| 169 | +* - Stores the final result in an output ndarray via the pointer `px2`. |
| 170 | +* |
| 171 | +* @param tin input type |
| 172 | +* @param expr expression to inline |
| 173 | +* |
| 174 | +* @example |
| 175 | +* STDLIB_NDARRAY_EVERY_10D_LOOP_INLINE( double, in1 ) |
| 176 | +*/ |
| 177 | +#define STDLIB_NDARRAY_EVERY_10D_LOOP_INLINE( tin, expr ) \ |
| 178 | + STDLIB_NDARRAY_EVERY_10D_LOOP_PREAMBLE { \ |
| 179 | + const tin in1 = *(tin *)px1; \ |
| 180 | + if ( !( expr ) ) { \ |
| 181 | + *px2 = false; \ |
| 182 | + return; \ |
| 183 | + } \ |
| 184 | + } \ |
| 185 | + STDLIB_NDARRAY_EVERY_10D_LOOP_EPILOGUE |
| 186 | + |
| 187 | +/** |
| 188 | +* Macro for a ten-dimensional ndarray loop which invokes a callback. |
| 189 | +* |
| 190 | +* ## Notes |
| 191 | +* |
| 192 | +* - Retrieves each ndarray element according to type `tin` via the pointer `px1`. |
| 193 | +* - Stores the final result in an output ndarray via the pointer `px2`. |
| 194 | +* |
| 195 | +* @param tin input type |
| 196 | +* |
| 197 | +* @example |
| 198 | +* // e.g., d_x |
| 199 | +* STDLIB_NDARRAY_EVERY_10D_LOOP_CLBK( double ) |
| 200 | +*/ |
| 201 | +#define STDLIB_NDARRAY_EVERY_10D_LOOP_CLBK( tin ) \ |
| 202 | + STDLIB_NDARRAY_EVERY_10D_LOOP_PREAMBLE { \ |
| 203 | + const tin x = *(tin *)px1; \ |
| 204 | + if ( !( f( x ) ) ) { \ |
| 205 | + *px2 = false; \ |
| 206 | + return; \ |
| 207 | + } \ |
| 208 | + } \ |
| 209 | + STDLIB_NDARRAY_EVERY_10D_LOOP_EPILOGUE |
| 210 | + |
| 211 | +/** |
| 212 | +* Macro for a ten-dimensional ndarray loop which invokes a callback requiring arguments be explicitly cast to a different type. |
| 213 | +* |
| 214 | +* ## Notes |
| 215 | +* |
| 216 | +* - Retrieves each ndarray element according to type `tin` via the pointer `px1`. |
| 217 | +* - Explicitly casts each function argument to `fin`. |
| 218 | +* - Stores the final result in an output ndarray via the pointer `px2`. |
| 219 | +* |
| 220 | +* @param tin input type |
| 221 | +* @param fin callback argument type |
| 222 | +* |
| 223 | +* @example |
| 224 | +* // e.g., f_x_as_d_x |
| 225 | +* STDLIB_NDARRAY_EVERY_10D_LOOP_CLBK_ARG_CAST( float, double ) |
| 226 | +*/ |
| 227 | +#define STDLIB_NDARRAY_EVERY_10D_LOOP_CLBK_ARG_CAST( tin, fin ) \ |
| 228 | + STDLIB_NDARRAY_EVERY_10D_LOOP_PREAMBLE { \ |
| 229 | + const tin x = *(tin *)px1; \ |
| 230 | + if ( !( f( (fin)x ) ) ) { \ |
| 231 | + *px2 = false; \ |
| 232 | + return; \ |
| 233 | + } \ |
| 234 | + } \ |
| 235 | + STDLIB_NDARRAY_EVERY_10D_LOOP_EPILOGUE |
| 236 | + |
| 237 | +/** |
| 238 | +* Macro for a ten-dimensional ndarray loop which invokes a callback requiring arguments be cast to a different type via casting functions. |
| 239 | +* |
| 240 | +* ## Notes |
| 241 | +* |
| 242 | +* - Retrieves each ndarray element according to type `tin` via a pointer `px1`. |
| 243 | +* - Explicitly casts each function argument via `cin`. |
| 244 | +* - Stores the final result in an output ndarray via the pointer `px2`. |
| 245 | +* |
| 246 | +* @param tin input type |
| 247 | +* @param cin input casting function |
| 248 | +* |
| 249 | +* @example |
| 250 | +* #include "stdlib/complex/float64/ctor.h" |
| 251 | +* |
| 252 | +* // e.g., f_x_as_z_x |
| 253 | +* STDLIB_NDARRAY_EVERY_10D_LOOP_CLBK_ARG_CAST_FCN( float, stdlib_complex128_from_float32 ) |
| 254 | +*/ |
| 255 | +#define STDLIB_NDARRAY_EVERY_10D_LOOP_CLBK_ARG_CAST_FCN( tin, cin ) \ |
| 256 | + STDLIB_NDARRAY_EVERY_10D_LOOP_PREAMBLE { \ |
| 257 | + const tin x = *(tin *)px1; \ |
| 258 | + if ( !( f( cin( x ) ) ) ) { \ |
| 259 | + *px2 = false; \ |
| 260 | + return; \ |
| 261 | + } \ |
| 262 | + } \ |
| 263 | + STDLIB_NDARRAY_EVERY_10D_LOOP_EPILOGUE |
| 264 | + |
| 265 | +#endif // !STDLIB_NDARRAY_BASE_EVERY_MACROS_10D_H |
0 commit comments