ternary.c (15355B)
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 ternary 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/ternary.h" 66 #include "stdlib/strided/common/ternary_typedefs.h" 67 #include "stdlib/strided/common/ternary_macros.h" 68 #include <stdint.h> 69 70 /** 71 * Applies a ternary callback to strided input arrays. 72 * 73 * @param arrays array whose first three 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/ternary.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 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 }; 87 * 88 * // Define a pointer to an array containing pointers to strided arrays: 89 * uint8_t *arrays[] = { x, y, z, out }; 90 * 91 * // Define the strides: 92 * int64_t strides[] = { 8, 8, 8, 8 }; // 8 bytes per double 93 * 94 * // Define the number of elements over which to iterate: 95 * int64_t shape[] = { 3 }; 96 * 97 * // Define a callback: 98 * double add3( double x, double y, double z ) { 99 * return x + y + z; 100 * } 101 * 102 * // Apply the callback: 103 * stdlib_strided_ddd_d( arrays, shape, strides, (void *)add3 ); 104 */ 105 void stdlib_strided_ddd_d( uint8_t *arrays[], int64_t *shape, int64_t *strides, void *fcn ) { 106 TernaryFcnFloat64 *f = (TernaryFcnFloat64 *)fcn; 107 STDLIB_TERNARY_LOOP_CLBK( double, double ) 108 } 109 110 /** 111 * Applies a ternary callback to strided input arrays. 112 * 113 * @param arrays array whose first three elements are pointers to strided input arrays and whose last element is a pointer to a strided output array 114 * @param shape array whose only element is the number of elements over which to iterate 115 * @param strides array containing strides (in bytes) for each strided array 116 * @param fcn callback 117 * 118 * @example 119 * #include "stdlib/strided/common/ternary.h" 120 * #include <stdint.h> 121 * 122 * // Create underlying byte arrays: 123 * uint8_t x[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 124 * uint8_t y[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 125 * uint8_t z[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 126 * uint8_t out[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 127 * 128 * // Define a pointer to an array containing pointers to strided arrays: 129 * uint8_t *arrays[] = { x, y, z, out }; 130 * 131 * // Define the strides: 132 * int64_t strides[] = { 4, 4, 4, 4 }; // 4 bytes per float 133 * 134 * // Define the number of elements over which to iterate: 135 * int64_t shape[] = { 3 }; 136 * 137 * // Define a callback: 138 * float add3( float x, float y, float z ) { 139 * return x + y + z; 140 * } 141 * 142 * // Apply the callback: 143 * stdlib_strided_fff_f( arrays, shape, strides, (void *)add3 ); 144 */ 145 void stdlib_strided_fff_f( uint8_t *arrays[], int64_t *shape, int64_t *strides, void *fcn ) { 146 TernaryFcnFloat32 *f = (TernaryFcnFloat32 *)fcn; 147 STDLIB_TERNARY_LOOP_CLBK( float, float ) 148 } 149 150 /** 151 * Applies a ternary callback to strided input arrays, casting the callback's double-precision return value to a single-precision floating-point number. 152 * 153 * @param arrays array whose first three elements are pointers to strided input arrays and whose last element is a pointer to a strided output array 154 * @param shape array whose only element is the number of elements over which to iterate 155 * @param strides array containing strides (in bytes) for each strided array 156 * @param fcn callback 157 * 158 * @example 159 * #include "stdlib/strided/common/ternary.h" 160 * #include <stdint.h> 161 * 162 * // Create underlying byte arrays: 163 * uint8_t x[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 164 * uint8_t y[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 165 * uint8_t z[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 166 * uint8_t out[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 167 * 168 * // Define a pointer to an array containing pointers to strided arrays: 169 * uint8_t *arrays[] = { x, y, z, out }; 170 * 171 * // Define the strides: 172 * int64_t strides[] = { 4, 4, 4, 4 }; // 4 bytes per float 173 * 174 * // Define the number of elements over which to iterate: 175 * int64_t shape[] = { 3 }; 176 * 177 * // Define a callback: 178 * double add3( double x, double y, double z ) { 179 * return x + y + z; 180 * } 181 * 182 * // Apply the callback: 183 * stdlib_strided_fff_f_as_ddd_d( arrays, shape, strides, (void *)add3 ); 184 */ 185 void stdlib_strided_fff_f_as_ddd_d( uint8_t *arrays[], int64_t *shape, int64_t *strides, void *fcn ) { 186 TernaryFcnFloat64 *f = (TernaryFcnFloat64 *)fcn; 187 STDLIB_TERNARY_LOOP_CLBK_ARG_CAST( float, float, double ) 188 } 189 190 /** 191 * Applies a ternary callback to strided input arrays. 192 * 193 * @param arrays array whose first three elements are pointers to strided input arrays and whose last element is a pointer to a strided output array 194 * @param shape array whose only element is the number of elements over which to iterate 195 * @param strides array containing strides (in bytes) for each strided array 196 * @param fcn callback 197 * 198 * @example 199 * #include "stdlib/strided/common/ternary.h" 200 * #include <stdint.h> 201 * 202 * // Create underlying byte arrays: 203 * uint8_t x[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 204 * uint8_t y[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 205 * uint8_t z[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 206 * uint8_t out[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 207 * 208 * // Define a pointer to an array containing pointers to strided arrays: 209 * uint8_t *arrays[] = { x, y, z, out }; 210 * 211 * // Define the strides: 212 * int64_t strides[] = { 4, 4, 4, 4 }; // 4 bytes per uint32 213 * 214 * // Define the number of elements over which to iterate: 215 * int64_t shape[] = { 3 }; 216 * 217 * // Define a callback: 218 * uint32_t add3( uint32_t x, uint32_t y, uint32_t z ) { 219 * return x + y + z; 220 * } 221 * 222 * // Apply the callback: 223 * stdlib_strided_III_I( arrays, shape, strides, (void *)add3 ); 224 */ 225 void stdlib_strided_III_I( uint8_t *arrays[], int64_t *shape, int64_t *strides, void *fcn ) { 226 TernaryFcnUint32 *f = (TernaryFcnUint32 *)fcn; 227 STDLIB_TERNARY_LOOP_CLBK( uint32_t, uint32_t ) 228 } 229 230 /** 231 * Applies a ternary callback to strided input arrays. 232 * 233 * @param arrays array whose first three elements are pointers to strided input arrays and whose last element is a pointer to a strided output array 234 * @param shape array whose only element is the number of elements over which to iterate 235 * @param strides array containing strides (in bytes) for each strided array 236 * @param fcn callback 237 * 238 * @example 239 * #include "stdlib/strided/common/ternary.h" 240 * #include <stdint.h> 241 * 242 * // Create underlying byte arrays: 243 * uint8_t x[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 244 * uint8_t y[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 245 * uint8_t z[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 246 * uint8_t out[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 247 * 248 * // Define a pointer to an array containing pointers to strided arrays: 249 * uint8_t *arrays[] = { x, y, z, out }; 250 * 251 * // Define the strides: 252 * int64_t strides[] = { 4, 4, 4, 4 }; // 4 bytes per int32 253 * 254 * // Define the number of elements over which to iterate: 255 * int64_t shape[] = { 3 }; 256 * 257 * // Define a callback: 258 * int32_t add3( int32_t x, int32_t y, int32_t z ) { 259 * return x + y + z; 260 * } 261 * 262 * // Apply the callback: 263 * stdlib_strided_iii_i( arrays, shape, strides, (void *)add3 ); 264 */ 265 void stdlib_strided_iii_i( uint8_t *arrays[], int64_t *shape, int64_t *strides, void *fcn ) { 266 TernaryFcnInt32 *f = (TernaryFcnInt32 *)fcn; 267 STDLIB_TERNARY_LOOP_CLBK( int32_t, int32_t ) 268 } 269 270 /** 271 * Applies a ternary callback to strided input arrays. 272 * 273 * @param arrays array whose first three elements are pointers to strided input arrays and whose last element is a pointer to a strided output array 274 * @param shape array whose only element is the number of elements over which to iterate 275 * @param strides array containing strides (in bytes) for each strided array 276 * @param fcn callback 277 * 278 * @example 279 * #include "stdlib/strided/common/ternary.h" 280 * #include <stdint.h> 281 * 282 * // Create underlying byte arrays: 283 * uint8_t x[] = { 0, 0, 0, 0, 0, 0 }; 284 * uint8_t y[] = { 0, 0, 0, 0, 0, 0 }; 285 * uint8_t z[] = { 0, 0, 0, 0, 0, 0 }; 286 * uint8_t out[] = { 0, 0, 0, 0, 0, 0 }; 287 * 288 * // Define a pointer to an array containing pointers to strided arrays: 289 * uint8_t *arrays[] = { x, y, z, out }; 290 * 291 * // Define the strides: 292 * int64_t strides[] = { 2, 2, 2, 2 }; // 2 bytes per uint16 293 * 294 * // Define the number of elements over which to iterate: 295 * int64_t shape[] = { 3 }; 296 * 297 * // Define a callback: 298 * uint16_t add3( uint16_t x, uint16_t y, uint16_t z ) { 299 * return x + y + z; 300 * } 301 * 302 * // Apply the callback: 303 * stdlib_strided_HHH_H( arrays, shape, strides, (void *)add3 ); 304 */ 305 void stdlib_strided_HHH_H( uint8_t *arrays[], int64_t *shape, int64_t *strides, void *fcn ) { 306 TernaryFcnUint16 *f = (TernaryFcnUint16 *)fcn; 307 STDLIB_TERNARY_LOOP_CLBK( uint16_t, uint16_t ) 308 } 309 310 /** 311 * Applies a ternary callback to strided input arrays. 312 * 313 * @param arrays array whose first three elements are pointers to strided input arrays and whose last element is a pointer to a strided output array 314 * @param shape array whose only element is the number of elements over which to iterate 315 * @param strides array containing strides (in bytes) for each strided array 316 * @param fcn callback 317 * 318 * @example 319 * #include "stdlib/strided/common/ternary.h" 320 * #include <stdint.h> 321 * 322 * // Create underlying byte arrays: 323 * uint8_t x[] = { 0, 0, 0, 0, 0, 0 }; 324 * uint8_t y[] = { 0, 0, 0, 0, 0, 0 }; 325 * uint8_t z[] = { 0, 0, 0, 0, 0, 0 }; 326 * uint8_t out[] = { 0, 0, 0, 0, 0, 0 }; 327 * 328 * // Define a pointer to an array containing pointers to strided arrays: 329 * uint8_t *arrays[] = { x, y, z, out }; 330 * 331 * // Define the strides: 332 * int64_t strides[] = { 2, 2, 2, 2 }; // 2 bytes per int16 333 * 334 * // Define the number of elements over which to iterate: 335 * int64_t shape[] = { 3 }; 336 * 337 * // Define a callback: 338 * int16_t add3( int16_t x, int16_t y, int16_t z ) { 339 * return x + y + z; 340 * } 341 * 342 * // Apply the callback: 343 * stdlib_strided_hhh_h( arrays, shape, strides, (void *)add3 ); 344 */ 345 void stdlib_strided_hhh_h( uint8_t *arrays[], int64_t *shape, int64_t *strides, void *fcn ) { 346 TernaryFcnInt16 *f = (TernaryFcnInt16 *)fcn; 347 STDLIB_TERNARY_LOOP_CLBK( int16_t, int16_t ) 348 } 349 350 /** 351 * Applies a ternary callback to strided input arrays. 352 * 353 * @param arrays array whose first three elements are pointers to strided input arrays and whose last element is a pointer to a strided output array 354 * @param shape array whose only element is the number of elements over which to iterate 355 * @param strides array containing strides (in bytes) for each strided array 356 * @param fcn callback 357 * 358 * @example 359 * #include "stdlib/strided/common/ternary.h" 360 * #include <stdint.h> 361 * 362 * // Create underlying byte arrays: 363 * uint8_t x[] = { 0, 0, 0 }; 364 * uint8_t y[] = { 0, 0, 0 }; 365 * uint8_t z[] = { 0, 0, 0 }; 366 * uint8_t out[] = { 0, 0, 0 }; 367 * 368 * // Define a pointer to an array containing pointers to strided arrays: 369 * uint8_t *arrays[] = { x, y, z, out }; 370 * 371 * // Define the strides: 372 * int64_t strides[] = { 1, 1, 1, 1 }; // 1 byte per uint8 373 * 374 * // Define the number of elements over which to iterate: 375 * int64_t shape[] = { 3 }; 376 * 377 * // Define a callback: 378 * uint8_t add3( uint8_t x, uint8_t y, uint8_t z ) { 379 * return x + y + z; 380 * } 381 * 382 * // Apply the callback: 383 * stdlib_strided_BBB_B( arrays, shape, strides, (void *)add3 ); 384 */ 385 void stdlib_strided_BBB_B( uint8_t *arrays[], int64_t *shape, int64_t *strides, void *fcn ) { 386 TernaryFcnUint8 *f = (TernaryFcnUint8 *)fcn; 387 STDLIB_TERNARY_LOOP_CLBK( uint8_t, uint8_t ) 388 } 389 390 /** 391 * Applies a ternary callback to strided input arrays. 392 * 393 * @param arrays array whose first three elements are pointers to strided input arrays and whose last element is a pointer to a strided output array 394 * @param shape array whose only element is the number of elements over which to iterate 395 * @param strides array containing strides (in bytes) for each strided array 396 * @param fcn callback 397 * 398 * @example 399 * #include "stdlib/strided/common/ternary.h" 400 * #include <stdint.h> 401 * 402 * // Create underlying byte arrays: 403 * uint8_t x[] = { 0, 0, 0 }; 404 * uint8_t y[] = { 0, 0, 0 }; 405 * uint8_t z[] = { 0, 0, 0 }; 406 * uint8_t out[] = { 0, 0, 0 }; 407 * 408 * // Define a pointer to an array containing pointers to strided arrays: 409 * uint8_t *arrays[] = { x, y, z, out }; 410 * 411 * // Define the strides: 412 * int64_t strides[] = { 1, 1, 1, 1 }; // 1 byte per int8 413 * 414 * // Define the number of elements over which to iterate: 415 * int64_t shape[] = { 3 }; 416 * 417 * // Define a callback: 418 * int8_t add3( int8_t x, int8_t y, int8_t z ) { 419 * return x + y + z; 420 * } 421 * 422 * // Apply the callback: 423 * stdlib_strided_bbb_b( arrays, shape, strides, (void *)add3 ); 424 */ 425 void stdlib_strided_bbb_b( uint8_t *arrays[], int64_t *shape, int64_t *strides, void *fcn ) { 426 TernaryFcnInt8 *f = (TernaryFcnInt8 *)fcn; 427 STDLIB_TERNARY_LOOP_CLBK( int8_t, int8_t ) 428 }