|
| 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_5D_BLOCKED_H |
| 20 | +#define STDLIB_NDARRAY_BASE_EVERY_MACROS_5D_BLOCKED_H |
| 21 | + |
| 22 | +#include "stdlib/ndarray/base/nullary/macros/constants.h" |
| 23 | +#include "stdlib/ndarray/base/nullary/internal/permute.h" |
| 24 | +#include "stdlib/ndarray/base/nullary/internal/range.h" |
| 25 | +#include "stdlib/ndarray/base/nullary/internal/sort2ins.h" |
| 26 | +#include "stdlib/ndarray/base/bytes_per_element.h" |
| 27 | +#include "stdlib/ndarray/ctor.h" |
| 28 | +#include <stdbool.h> |
| 29 | +#include <stdint.h> |
| 30 | +#include <string.h> |
| 31 | + |
| 32 | +/** |
| 33 | +* Macro containing the preamble for blocked nested loops which operate on elements of a five-dimensional ndarray. |
| 34 | +* |
| 35 | +* ## Notes |
| 36 | +* |
| 37 | +* - Variable naming conventions: |
| 38 | +* |
| 39 | +* - `sx#`, `pbx#`, `px#`, `ox#`, `nbx#`, and `d@x#` where `#` corresponds to the ndarray argument number, starting at `1`. |
| 40 | +* - `S@`, `i@`, `j@`, `o@x#`, and `d@x#` where `@` corresponds to the loop number, with `0` being the innermost loop. |
| 41 | +* |
| 42 | +* @example |
| 43 | +* STDLIB_NDARRAY_EVERY_5D_BLOCKED_LOOP_PREMABLE { |
| 44 | +* // Innermost loop body... |
| 45 | +* } |
| 46 | +* STDLIB_NDARRAY_EVERY_5D_BLOCKED_LOOP_EPILOGUE |
| 47 | +*/ |
| 48 | +#define STDLIB_NDARRAY_EVERY_5D_BLOCKED_LOOP_PREAMBLE \ |
| 49 | + const struct ndarray *x1 = arrays[ 0 ]; \ |
| 50 | + const struct ndarray *x2 = arrays[ 1 ]; \ |
| 51 | + bool *px2 = stdlib_ndarray_data( x2 ); \ |
| 52 | + int64_t shape[5]; \ |
| 53 | + int64_t sx1[5]; \ |
| 54 | + int64_t idx[5]; \ |
| 55 | + int64_t tmp[5]; \ |
| 56 | + int64_t bsize; \ |
| 57 | + uint8_t *pbx1; \ |
| 58 | + uint8_t *px1; \ |
| 59 | + int64_t d0x1; \ |
| 60 | + int64_t d1x1; \ |
| 61 | + int64_t d2x1; \ |
| 62 | + int64_t d3x1; \ |
| 63 | + int64_t d4x1; \ |
| 64 | + int64_t o1x1; \ |
| 65 | + int64_t o2x1; \ |
| 66 | + int64_t o3x1; \ |
| 67 | + int64_t o4x1; \ |
| 68 | + int64_t nbx1; \ |
| 69 | + int64_t ox1; \ |
| 70 | + int64_t s0; \ |
| 71 | + int64_t s1; \ |
| 72 | + int64_t s2; \ |
| 73 | + int64_t s3; \ |
| 74 | + int64_t s4; \ |
| 75 | + int64_t i0; \ |
| 76 | + int64_t i1; \ |
| 77 | + int64_t i2; \ |
| 78 | + int64_t i3; \ |
| 79 | + int64_t i4; \ |
| 80 | + int64_t j0; \ |
| 81 | + int64_t j1; \ |
| 82 | + int64_t j2; \ |
| 83 | + int64_t j3; \ |
| 84 | + int64_t j4; \ |
| 85 | + /* Copy strides to prevent mutation to the original ndarray: */ \ |
| 86 | + memcpy( sx1, stdlib_ndarray_strides( x1 ), sizeof sx1 ); \ |
| 87 | + /* Create a loop interchange index array for loop order permutation: */ \ |
| 88 | + stdlib_ndarray_base_nullary_internal_range( 5, idx ); \ |
| 89 | + /* Sort the input array strides in increasing order (of magnitude): */ \ |
| 90 | + stdlib_ndarray_base_nullary_internal_sort2ins( 5, sx1, idx ); \ |
| 91 | + /* Permute the shape (avoiding mutation) according to loop order: */ \ |
| 92 | + stdlib_ndarray_base_nullary_internal_permute( 5, stdlib_ndarray_shape( x1 ), idx, tmp ); \ |
| 93 | + memcpy( shape, tmp, sizeof shape ); \ |
| 94 | + /* Determine the block size... */ \ |
| 95 | + nbx1 = stdlib_ndarray_bytes_per_element( stdlib_ndarray_dtype( x1 ) ); \ |
| 96 | + if ( nbx1 == 0 ) { \ |
| 97 | + bsize = STDLIB_NDARRAY_EVERY_BLOCK_SIZE_IN_ELEMENTS; \ |
| 98 | + } else { \ |
| 99 | + bsize = STDLIB_NDARRAY_EVERY_BLOCK_SIZE_IN_BYTES / nbx1; \ |
| 100 | + } \ |
| 101 | + /* Cache a pointer to the ndarray buffer... */ \ |
| 102 | + pbx1 = stdlib_ndarray_data( x1 ); \ |
| 103 | + /* Cache the byte offset to the first indexed element... */ \ |
| 104 | + ox1 = stdlib_ndarray_offset( x1 ); \ |
| 105 | + /* Set a pointer to the first indexed element of the output ndarray... */ \ |
| 106 | + px2 += stdlib_ndarray_offset( x2 ); \ |
| 107 | + /* Cache the offset increment for the innermost loop... */ \ |
| 108 | + d0x1 = sx1[0]; \ |
| 109 | + /* Iterate over blocks... */ \ |
| 110 | + for ( j4 = shape[4]; j4 > 0; ) { \ |
| 111 | + if ( j4 < bsize ) { \ |
| 112 | + s4 = j4; \ |
| 113 | + j4 = 0; \ |
| 114 | + } else { \ |
| 115 | + s4 = bsize; \ |
| 116 | + j4 -= bsize; \ |
| 117 | + } \ |
| 118 | + o4x1 = ox1 + ( j4*sx1[4] ); \ |
| 119 | + for ( j3 = shape[3]; j3 > 0; ) { \ |
| 120 | + if ( j3 < bsize ) { \ |
| 121 | + s3 = j3; \ |
| 122 | + j3 = 0; \ |
| 123 | + } else { \ |
| 124 | + s3 = bsize; \ |
| 125 | + j3 -= bsize; \ |
| 126 | + } \ |
| 127 | + d4x1 = sx1[4] - ( s3*sx1[3] ); \ |
| 128 | + o3x1 = o4x1 + ( j3*sx1[3] ); \ |
| 129 | + for ( j2 = shape[2]; j2 > 0; ) { \ |
| 130 | + if ( j2 < bsize ) { \ |
| 131 | + s2 = j2; \ |
| 132 | + j2 = 0; \ |
| 133 | + } else { \ |
| 134 | + s2 = bsize; \ |
| 135 | + j2 -= bsize; \ |
| 136 | + } \ |
| 137 | + d3x1 = sx1[3] - ( s2*sx1[2] ); \ |
| 138 | + o2x1 = o3x1 + ( j2*sx1[2] ); \ |
| 139 | + for ( j1 = shape[1]; j1 > 0; ) { \ |
| 140 | + if ( j1 < bsize ) { \ |
| 141 | + s1 = j1; \ |
| 142 | + j1 = 0; \ |
| 143 | + } else { \ |
| 144 | + s1 = bsize; \ |
| 145 | + j1 -= bsize; \ |
| 146 | + } \ |
| 147 | + d2x1 = sx1[2] - ( s1*sx1[1] ); \ |
| 148 | + o1x1 = o2x1 + ( j1*sx1[1] ); \ |
| 149 | + for ( j0 = shape[0]; j0 > 0; ) { \ |
| 150 | + if ( j0 < bsize ) { \ |
| 151 | + s0 = j0; \ |
| 152 | + j0 = 0; \ |
| 153 | + } else { \ |
| 154 | + s0 = bsize; \ |
| 155 | + j0 -= bsize; \ |
| 156 | + } \ |
| 157 | + /* Compute a pointer to the first ndarray element in the current block... */ \ |
| 158 | + px1 = pbx1 + o1x1 + ( j0*sx1[0] ); \ |
| 159 | + /* Compute the loop offset increment... */ \ |
| 160 | + d1x1 = sx1[1] - ( s0*sx1[0] ); \ |
| 161 | + /* Iterate over the ndarray dimensions... */ \ |
| 162 | + for ( i4 = 0; i4 < s4; i4++, px1 += d4x1 ) { \ |
| 163 | + for ( i3 = 0; i3 < s3; i3++, px1 += d3x1 ) { \ |
| 164 | + for ( i2 = 0; i2 < s2; i2++, px1 += d2x1 ) { \ |
| 165 | + for ( i1 = 0; i1 < s1; i1++, px1 += d1x1 ) { \ |
| 166 | + for ( i0 = 0; i0 < s0; i0++, px1 += d0x1 ) |
| 167 | + |
| 168 | +/** |
| 169 | +* Macro containing the epilogue for blocked nested loops which operate on elements of a five-dimensional ndarray. |
| 170 | +* |
| 171 | +* @example |
| 172 | +* STDLIB_NDARRAY_EVERY_5D_BLOCKED_LOOP_PREMABLE { |
| 173 | +* // Innermost loop body... |
| 174 | +* } |
| 175 | +* STDLIB_NDARRAY_EVERY_5D_BLOCKED_LOOP_EPILOGUE |
| 176 | +*/ |
| 177 | +#define STDLIB_NDARRAY_EVERY_5D_BLOCKED_LOOP_EPILOGUE \ |
| 178 | + } \ |
| 179 | + } \ |
| 180 | + } \ |
| 181 | + } \ |
| 182 | + } \ |
| 183 | + } \ |
| 184 | + } \ |
| 185 | + } \ |
| 186 | + } \ |
| 187 | + *px2 = true; |
| 188 | + |
| 189 | +/** |
| 190 | +* Macro for a blocked five-dimensional ndarray loop which inlines an expression. |
| 191 | +* |
| 192 | +* ## Notes |
| 193 | +* |
| 194 | +* - Retrieves each input ndarray element according to type `tin` via the pointer `px1` as `in1`. |
| 195 | +* - Expects a provided expression to operate on `tin in1`. |
| 196 | +* - Stores the final result in an output ndarray via the pointer `px2`. |
| 197 | +* |
| 198 | +* @param tin input type |
| 199 | +* @param expr expression to inline |
| 200 | +* |
| 201 | +* @example |
| 202 | +* STDLIB_NDARRAY_EVERY_5D_BLOCKED_LOOP_INLINE( double, in1 ) |
| 203 | +*/ |
| 204 | +#define STDLIB_NDARRAY_EVERY_5D_BLOCKED_LOOP_INLINE( tin, expr ) \ |
| 205 | + STDLIB_NDARRAY_EVERY_5D_BLOCKED_LOOP_PREAMBLE { \ |
| 206 | + const tin in1 = *(tin *)px1; \ |
| 207 | + if ( !( expr ) ) { \ |
| 208 | + *px2 = false; \ |
| 209 | + return; \ |
| 210 | + } \ |
| 211 | + } \ |
| 212 | + STDLIB_NDARRAY_EVERY_5D_BLOCKED_LOOP_EPILOGUE |
| 213 | + |
| 214 | +/** |
| 215 | +* Macro for a blocked five-dimensional ndarray loop which invokes a callback. |
| 216 | +* |
| 217 | +* ## Notes |
| 218 | +* |
| 219 | +* - Retrieves each ndarray element according to type `tin` via the pointer `px1`. |
| 220 | +* - Stores the final result in an output ndarray via the pointer `px2`. |
| 221 | +* |
| 222 | +* @param tin input type |
| 223 | +* |
| 224 | +* @example |
| 225 | +* // e.g., d_x |
| 226 | +* STDLIB_NDARRAY_EVERY_5D_BLOCKED_LOOP_CLBK( double ) |
| 227 | +*/ |
| 228 | +#define STDLIB_NDARRAY_EVERY_5D_BLOCKED_LOOP_CLBK( tin ) \ |
| 229 | + STDLIB_NDARRAY_EVERY_5D_BLOCKED_LOOP_PREAMBLE { \ |
| 230 | + const tin x = *(tin *)px1; \ |
| 231 | + if ( !( f( x ) ) ) { \ |
| 232 | + *px2 = false; \ |
| 233 | + return; \ |
| 234 | + } \ |
| 235 | + } \ |
| 236 | + STDLIB_NDARRAY_EVERY_5D_BLOCKED_LOOP_EPILOGUE |
| 237 | + |
| 238 | +/** |
| 239 | +* Macro for a blocked five-dimensional ndarray loop which invokes a callback requiring arguments be explicitly cast to a different type. |
| 240 | +* |
| 241 | +* ## Notes |
| 242 | +* |
| 243 | +* - Retrieves each ndarray element according to type `tin` via the pointer `px1`. |
| 244 | +* - Explicitly casts each function argument to `fin`. |
| 245 | +* - Stores the final result in an output ndarray via the pointer `px2`. |
| 246 | +* |
| 247 | +* @param tin input type |
| 248 | +* @param fin callback argument type |
| 249 | +* |
| 250 | +* @example |
| 251 | +* // e.g., f_x_as_d_x |
| 252 | +* STDLIB_NDARRAY_EVERY_5D_BLOCKED_LOOP_CLBK_ARG_CAST( float, double ) |
| 253 | +*/ |
| 254 | +#define STDLIB_NDARRAY_EVERY_5D_BLOCKED_LOOP_CLBK_ARG_CAST( tin, fin ) \ |
| 255 | + STDLIB_NDARRAY_EVERY_5D_BLOCKED_LOOP_PREAMBLE { \ |
| 256 | + const tin x = *(tin *)px1; \ |
| 257 | + if ( !( f( (fin)x ) ) ) { \ |
| 258 | + *px2 = false; \ |
| 259 | + return; \ |
| 260 | + } \ |
| 261 | + } \ |
| 262 | + STDLIB_NDARRAY_EVERY_5D_BLOCKED_LOOP_EPILOGUE |
| 263 | + |
| 264 | +/** |
| 265 | +* Macro for a blocked five-dimensional ndarray loop which invokes a callback requiring arguments be cast to a different type via casting functions. |
| 266 | +* |
| 267 | +* ## Notes |
| 268 | +* |
| 269 | +* - Retrieves each ndarray element according to type `tin` via a pointer `px1`. |
| 270 | +* - Explicitly casts each function argument via `cin`. |
| 271 | +* - Stores the final result in an output ndarray via the pointer `px2`. |
| 272 | +* |
| 273 | +* @param tin input type |
| 274 | +* @param cin input casting function |
| 275 | +* |
| 276 | +* @example |
| 277 | +* #include "stdlib/complex/float64/ctor.h" |
| 278 | +* |
| 279 | +* // e.g., f_x_as_z_x |
| 280 | +* STDLIB_NDARRAY_EVERY_5D_BLOCKED_LOOP_CLBK_ARG_CAST_FCN( float, stdlib_complex128_from_float32 ) |
| 281 | +*/ |
| 282 | +#define STDLIB_NDARRAY_EVERY_5D_BLOCKED_LOOP_CLBK_ARG_CAST_FCN( tin, cin ) \ |
| 283 | + STDLIB_NDARRAY_EVERY_5D_BLOCKED_LOOP_PREAMBLE { \ |
| 284 | + const tin x = *(tin *)px1; \ |
| 285 | + if ( !( f( cin( x ) ) ) ) { \ |
| 286 | + *px2 = false; \ |
| 287 | + return; \ |
| 288 | + } \ |
| 289 | + } \ |
| 290 | + STDLIB_NDARRAY_EVERY_5D_BLOCKED_LOOP_EPILOGUE |
| 291 | + |
| 292 | +#endif // !STDLIB_NDARRAY_BASE_EVERY_MACROS_5D_BLOCKED_H |
0 commit comments