quaternary.c (16149B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 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 /** 20 * Strided array functions for quaternary callbacks. 21 * 22 * ## Notes 23 * 24 * - Character codes for data types: 25 * 26 * - d: float64 (double) 27 * - f: float32 (float) 28 * - b: int8 (signed char) 29 * - B: uint8 (unsigned char) 30 * - h: int16 (signed short) 31 * - H: uint16 (unsigned short) 32 * - i: int32 (signed int) 33 * - I: uint32 (unsigned int) 34 * 35 * - Function name suffix naming convention: 36 * 37 * ```text 38 * <base_function_name>[_<input_data_types>]_<output_data_type>[_as_<callback_arg_data_types>_<callback_return_data_type>] 39 * ``` 40 * 41 * For example, 42 * 43 * ```c 44 * void stdlib_strided_dd_d(...) {...} 45 * ``` 46 * 47 * is a function which accepts two strided input arrays containing double-precision floating-point numbers and whose results are cast as double-precision floating-point numbers. In other words, the suffix encodes the function type signature. 48 * 49 * - To support callbacks whose input arguments and/or return values are of a different data type than the input and/or output strided array data types, the naming convention supports appending an `as` suffix. For example, 50 * 51 * ```c 52 * void stdlib_strided_ff_f_as_dd_d(...) {...} 53 * ``` 54 * 55 * is a function which accepts two strided input arrays containing single-precision floating-point numbers and whose results are cast as single-precision floating-point numbers. However, the callback requires each pair of single-precision floating-point numbers be cast to double-precision floating-point numbers when provided as arguments, and the callback return value is a double-precision floating-point number. Accordingly, the conversion process would be 56 * 57 * ```text 58 * float in1 -> double in1 59 * float in2 -> double in2 60 * double out1 = f( in1, in2 ) 61 * float out1 <- double out1 62 * ``` 63 * 64 */ 65 #include "stdlib/strided/common/quaternary.h" 66 #include "stdlib/strided/common/quaternary_typedefs.h" 67 #include "stdlib/strided/common/quaternary_macros.h" 68 #include <stdint.h> 69 70 /** 71 * Applies a quaternary callback to strided input arrays. 72 * 73 * @param arrays array whose first four elements are pointers to strided input arrays and whose last element is a pointer to a strided output array 74 * @param shape array whose only element is the number of elements over which to iterate 75 * @param strides array containing strides (in bytes) for each strided array 76 * @param fcn callback 77 * 78 * @example 79 * #include "stdlib/strided/common/quaternary.h" 80 * #include <stdint.h> 81 * 82 * // Create underlying byte arrays: 83 * uint8_t x[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 84 * uint8_t y[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 85 * uint8_t z[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 86 * uint8_t w[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 87 * uint8_t out[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 88 * 89 * // Define a pointer to an array containing pointers to strided arrays: 90 * uint8_t *arrays[] = { x, y, z, w, out }; 91 * 92 * // Define the strides: 93 * int64_t strides[] = { 8, 8, 8, 8, 8 }; // 8 bytes per double 94 * 95 * // Define the number of elements over which to iterate: 96 * int64_t shape[] = { 3 }; 97 * 98 * // Define a callback: 99 * double add4( double x, double y, double z, double w ) { 100 * return x + y + z + w; 101 * } 102 * 103 * // Apply the callback: 104 * stdlib_strided_dddd_d( arrays, shape, strides, (void *)add4 ); 105 */ 106 void stdlib_strided_dddd_d( uint8_t *arrays[], int64_t *shape, int64_t *strides, void *fcn ) { 107 QuaternaryFcnFloat64 *f = (QuaternaryFcnFloat64 *)fcn; 108 STDLIB_QUATERNARY_LOOP_CLBK( double, double ) 109 } 110 111 /** 112 * Applies a quaternary callback to strided input arrays. 113 * 114 * @param arrays array whose first four elements are pointers to strided input arrays and whose last element is a pointer to a strided output array 115 * @param shape array whose only element is the number of elements over which to iterate 116 * @param strides array containing strides (in bytes) for each strided array 117 * @param fcn callback 118 * 119 * @example 120 * #include "stdlib/strided/common/quaternary.h" 121 * #include <stdint.h> 122 * 123 * // Create underlying byte arrays: 124 * uint8_t x[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 125 * uint8_t y[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 126 * uint8_t z[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 127 * uint8_t w[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 128 * uint8_t out[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 129 * 130 * // Define a pointer to an array containing pointers to strided arrays: 131 * uint8_t *arrays[] = { x, y, z, w, out }; 132 * 133 * // Define the strides: 134 * int64_t strides[] = { 4, 4, 4, 4, 4 }; // 4 bytes per float 135 * 136 * // Define the number of elements over which to iterate: 137 * int64_t shape[] = { 3 }; 138 * 139 * // Define a callback: 140 * float add4( float x, float y, float z, float w ) { 141 * return x + y + z + w; 142 * } 143 * 144 * // Apply the callback: 145 * stdlib_strided_ffff_f( arrays, shape, strides, (void *)add4 ); 146 */ 147 void stdlib_strided_ffff_f( uint8_t *arrays[], int64_t *shape, int64_t *strides, void *fcn ) { 148 QuaternaryFcnFloat32 *f = (QuaternaryFcnFloat32 *)fcn; 149 STDLIB_QUATERNARY_LOOP_CLBK( float, float ) 150 } 151 152 /** 153 * Applies a quaternary callback to strided input arrays, casting the callback's double-precision return value to a single-precision floating-point number. 154 * 155 * @param arrays array whose first four elements are pointers to strided input arrays and whose last element is a pointer to a strided output array 156 * @param shape array whose only element is the number of elements over which to iterate 157 * @param strides array containing strides (in bytes) for each strided array 158 * @param fcn callback 159 * 160 * @example 161 * #include "stdlib/strided/common/quaternary.h" 162 * #include <stdint.h> 163 * 164 * // Create underlying byte arrays: 165 * uint8_t x[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 166 * uint8_t y[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 167 * uint8_t z[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 168 * uint8_t w[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 169 * uint8_t out[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 170 * 171 * // Define a pointer to an array containing pointers to strided arrays: 172 * uint8_t *arrays[] = { x, y, z, w, out }; 173 * 174 * // Define the strides: 175 * int64_t strides[] = { 4, 4, 4, 4, 4 }; // 4 bytes per float 176 * 177 * // Define the number of elements over which to iterate: 178 * int64_t shape[] = { 3 }; 179 * 180 * // Define a callback: 181 * double add4( double x, double y, double z, double w ) { 182 * return x + y + z + w; 183 * } 184 * 185 * // Apply the callback: 186 * stdlib_strided_ffff_f_as_dddd_d( arrays, shape, strides, (void *)add4 ); 187 */ 188 void stdlib_strided_ffff_f_as_dddd_d( uint8_t *arrays[], int64_t *shape, int64_t *strides, void *fcn ) { 189 QuaternaryFcnFloat64 *f = (QuaternaryFcnFloat64 *)fcn; 190 STDLIB_QUATERNARY_LOOP_CLBK_ARG_CAST( float, float, double ) 191 } 192 193 /** 194 * Applies a quaternary callback to strided input arrays. 195 * 196 * @param arrays array whose first four elements are pointers to strided input arrays and whose last element is a pointer to a strided output array 197 * @param shape array whose only element is the number of elements over which to iterate 198 * @param strides array containing strides (in bytes) for each strided array 199 * @param fcn callback 200 * 201 * @example 202 * #include "stdlib/strided/common/quaternary.h" 203 * #include <stdint.h> 204 * 205 * // Create underlying byte arrays: 206 * uint8_t x[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 207 * uint8_t y[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 208 * uint8_t z[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 209 * uint8_t w[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 210 * uint8_t out[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 211 * 212 * // Define a pointer to an array containing pointers to strided arrays: 213 * uint8_t *arrays[] = { x, y, z, w, out }; 214 * 215 * // Define the strides: 216 * int64_t strides[] = { 4, 4, 4, 4, 4 }; // 4 bytes per uint32 217 * 218 * // Define the number of elements over which to iterate: 219 * int64_t shape[] = { 3 }; 220 * 221 * // Define a callback: 222 * uint32_t add4( uint32_t x, uint32_t y, uint32_t z, uint32_t w ) { 223 * return x + y + z + w; 224 * } 225 * 226 * // Apply the callback: 227 * stdlib_strided_IIII_I( arrays, shape, strides, (void *)add4 ); 228 */ 229 void stdlib_strided_IIII_I( uint8_t *arrays[], int64_t *shape, int64_t *strides, void *fcn ) { 230 QuaternaryFcnUint32 *f = (QuaternaryFcnUint32 *)fcn; 231 STDLIB_QUATERNARY_LOOP_CLBK( uint32_t, uint32_t ) 232 } 233 234 /** 235 * Applies a quaternary callback to strided input arrays. 236 * 237 * @param arrays array whose first four elements are pointers to strided input arrays and whose last element is a pointer to a strided output array 238 * @param shape array whose only element is the number of elements over which to iterate 239 * @param strides array containing strides (in bytes) for each strided array 240 * @param fcn callback 241 * 242 * @example 243 * #include "stdlib/strided/common/quaternary.h" 244 * #include <stdint.h> 245 * 246 * // Create underlying byte arrays: 247 * uint8_t x[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 248 * uint8_t y[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 249 * uint8_t z[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 250 * uint8_t w[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 251 * uint8_t out[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 252 * 253 * // Define a pointer to an array containing pointers to strided arrays: 254 * uint8_t *arrays[] = { x, y, z, w, out }; 255 * 256 * // Define the strides: 257 * int64_t strides[] = { 4, 4, 4, 4, 4 }; // 4 bytes per int32 258 * 259 * // Define the number of elements over which to iterate: 260 * int64_t shape[] = { 3 }; 261 * 262 * // Define a callback: 263 * int32_t add4( int32_t x, int32_t y, int32_t z, int32_t w ) { 264 * return x + y + z + w; 265 * } 266 * 267 * // Apply the callback: 268 * stdlib_strided_iiii_i( arrays, shape, strides, (void *)add4 ); 269 */ 270 void stdlib_strided_iiii_i( uint8_t *arrays[], int64_t *shape, int64_t *strides, void *fcn ) { 271 QuaternaryFcnInt32 *f = (QuaternaryFcnInt32 *)fcn; 272 STDLIB_QUATERNARY_LOOP_CLBK( int32_t, int32_t ) 273 } 274 275 /** 276 * Applies a quaternary callback to strided input arrays. 277 * 278 * @param arrays array whose first four elements are pointers to strided input arrays and whose last element is a pointer to a strided output array 279 * @param shape array whose only element is the number of elements over which to iterate 280 * @param strides array containing strides (in bytes) for each strided array 281 * @param fcn callback 282 * 283 * @example 284 * #include "stdlib/strided/common/quaternary.h" 285 * #include <stdint.h> 286 * 287 * // Create underlying byte arrays: 288 * uint8_t x[] = { 0, 0, 0, 0, 0, 0 }; 289 * uint8_t y[] = { 0, 0, 0, 0, 0, 0 }; 290 * uint8_t z[] = { 0, 0, 0, 0, 0, 0 }; 291 * uint8_t w[] = { 0, 0, 0, 0, 0, 0 }; 292 * uint8_t out[] = { 0, 0, 0, 0, 0, 0 }; 293 * 294 * // Define a pointer to an array containing pointers to strided arrays: 295 * uint8_t *arrays[] = { x, y, z, w, out }; 296 * 297 * // Define the strides: 298 * int64_t strides[] = { 2, 2, 2, 2, 2 }; // 2 bytes per uint16 299 * 300 * // Define the number of elements over which to iterate: 301 * int64_t shape[] = { 3 }; 302 * 303 * // Define a callback: 304 * uint16_t add4( uint16_t x, uint16_t y, uint16_t z, uint16_t w ) { 305 * return x + y + z + w; 306 * } 307 * 308 * // Apply the callback: 309 * stdlib_strided_HHHH_H( arrays, shape, strides, (void *)add4 ); 310 */ 311 void stdlib_strided_HHHH_H( uint8_t *arrays[], int64_t *shape, int64_t *strides, void *fcn ) { 312 QuaternaryFcnUint16 *f = (QuaternaryFcnUint16 *)fcn; 313 STDLIB_QUATERNARY_LOOP_CLBK( uint16_t, uint16_t ) 314 } 315 316 /** 317 * Applies a quaternary callback to strided input arrays. 318 * 319 * @param arrays array whose first four elements are pointers to strided input arrays and whose last element is a pointer to a strided output array 320 * @param shape array whose only element is the number of elements over which to iterate 321 * @param strides array containing strides (in bytes) for each strided array 322 * @param fcn callback 323 * 324 * @example 325 * #include "stdlib/strided/common/quaternary.h" 326 * #include <stdint.h> 327 * 328 * // Create underlying byte arrays: 329 * uint8_t x[] = { 0, 0, 0, 0, 0, 0 }; 330 * uint8_t y[] = { 0, 0, 0, 0, 0, 0 }; 331 * uint8_t z[] = { 0, 0, 0, 0, 0, 0 }; 332 * uint8_t w[] = { 0, 0, 0, 0, 0, 0 }; 333 * uint8_t out[] = { 0, 0, 0, 0, 0, 0 }; 334 * 335 * // Define a pointer to an array containing pointers to strided arrays: 336 * uint8_t *arrays[] = { x, y, z, w, out }; 337 * 338 * // Define the strides: 339 * int64_t strides[] = { 2, 2, 2, 2, 2 }; // 2 bytes per int16 340 * 341 * // Define the number of elements over which to iterate: 342 * int64_t shape[] = { 3 }; 343 * 344 * // Define a callback: 345 * int16_t add4( int16_t x, int16_t y, int16_t z, int16_t w ) { 346 * return x + y + z + w; 347 * } 348 * 349 * // Apply the callback: 350 * stdlib_strided_hhhh_h( arrays, shape, strides, (void *)add4 ); 351 */ 352 void stdlib_strided_hhhh_h( uint8_t *arrays[], int64_t *shape, int64_t *strides, void *fcn ) { 353 QuaternaryFcnInt16 *f = (QuaternaryFcnInt16 *)fcn; 354 STDLIB_QUATERNARY_LOOP_CLBK( int16_t, int16_t ) 355 } 356 357 /** 358 * Applies a quaternary callback to strided input arrays. 359 * 360 * @param arrays array whose first four elements are pointers to strided input arrays and whose last element is a pointer to a strided output array 361 * @param shape array whose only element is the number of elements over which to iterate 362 * @param strides array containing strides (in bytes) for each strided array 363 * @param fcn callback 364 * 365 * @example 366 * #include "stdlib/strided/common/quaternary.h" 367 * #include <stdint.h> 368 * 369 * // Create underlying byte arrays: 370 * uint8_t x[] = { 0, 0, 0 }; 371 * uint8_t y[] = { 0, 0, 0 }; 372 * uint8_t z[] = { 0, 0, 0 }; 373 * uint8_t w[] = { 0, 0, 0 }; 374 * uint8_t out[] = { 0, 0, 0 }; 375 * 376 * // Define a pointer to an array containing pointers to strided arrays: 377 * uint8_t *arrays[] = { x, y, z, w, out }; 378 * 379 * // Define the strides: 380 * int64_t strides[] = { 1, 1, 1, 1, 1 }; // 1 byte per uint8 381 * 382 * // Define the number of elements over which to iterate: 383 * int64_t shape[] = { 3 }; 384 * 385 * // Define a callback: 386 * uint8_t add4( uint8_t x, uint8_t y, uint8_t z, uint8_t w ) { 387 * return x + y + z + w; 388 * } 389 * 390 * // Apply the callback: 391 * stdlib_strided_BBBB_B( arrays, shape, strides, (void *)add4 ); 392 */ 393 void stdlib_strided_BBBB_B( uint8_t *arrays[], int64_t *shape, int64_t *strides, void *fcn ) { 394 QuaternaryFcnUint8 *f = (QuaternaryFcnUint8 *)fcn; 395 STDLIB_QUATERNARY_LOOP_CLBK( uint8_t, uint8_t ) 396 } 397 398 /** 399 * Applies a quaternary callback to strided input arrays. 400 * 401 * @param arrays array whose first four elements are pointers to strided input arrays and whose last element is a pointer to a strided output array 402 * @param shape array whose only element is the number of elements over which to iterate 403 * @param strides array containing strides (in bytes) for each strided array 404 * @param fcn callback 405 * 406 * @example 407 * #include "stdlib/strided/common/quaternary.h" 408 * #include <stdint.h> 409 * 410 * // Create underlying byte arrays: 411 * uint8_t x[] = { 0, 0, 0 }; 412 * uint8_t y[] = { 0, 0, 0 }; 413 * uint8_t z[] = { 0, 0, 0 }; 414 * uint8_t w[] = { 0, 0, 0 }; 415 * uint8_t out[] = { 0, 0, 0 }; 416 * 417 * // Define a pointer to an array containing pointers to strided arrays: 418 * uint8_t *arrays[] = { x, y, z, w, out }; 419 * 420 * // Define the strides: 421 * int64_t strides[] = { 1, 1, 1, 1, 1 }; // 1 byte per int8 422 * 423 * // Define the number of elements over which to iterate: 424 * int64_t shape[] = { 3 }; 425 * 426 * // Define a callback: 427 * int8_t add4( int8_t x, int8_t y, int8_t z, int8_t w ) { 428 * return x + y + z + w; 429 * } 430 * 431 * // Apply the callback: 432 * stdlib_strided_bbbb_b( arrays, shape, strides, (void *)add4 ); 433 */ 434 void stdlib_strided_bbbb_b( uint8_t *arrays[], int64_t *shape, int64_t *strides, void *fcn ) { 435 QuaternaryFcnInt8 *f = (QuaternaryFcnInt8 *)fcn; 436 STDLIB_QUATERNARY_LOOP_CLBK( int8_t, int8_t ) 437 }