1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright (c) 2022 Marvell. 3 */ 4 5 #ifndef RTE_MLDEV_UTILS_H 6 #define RTE_MLDEV_UTILS_H 7 8 /** 9 * @file 10 * 11 * ML Device PMD utility API 12 * 13 * These APIs for the use from ML drivers, user applications shouldn't use them. 14 */ 15 16 #include <rte_compat.h> 17 #include <rte_mldev.h> 18 19 #ifdef __cplusplus 20 extern "C" { 21 #endif 22 23 /** 24 * @internal 25 * 26 * Get the size an ML IO type in bytes. 27 * 28 * @param[in] type 29 * Enumeration of ML IO data type. 30 * 31 * @return 32 * - > 0, Size of the data type in bytes. 33 * - < 0, Error code on failure. 34 */ 35 __rte_internal 36 int 37 rte_ml_io_type_size_get(enum rte_ml_io_type type); 38 39 /** 40 * @internal 41 * 42 * Get the name of an ML IO type. 43 * 44 * @param[in] type 45 * Enumeration of ML IO data type. 46 * @param[in] str 47 * Address of character array. 48 * @param[in] len 49 * Length of character array. 50 */ 51 __rte_internal 52 void 53 rte_ml_io_type_to_str(enum rte_ml_io_type type, char *str, int len); 54 55 /** 56 * @internal 57 * 58 * Convert a buffer containing numbers in single precision floating format (float32) to signed 8-bit 59 * integer format (INT8). 60 * 61 * @param[in] scale 62 * Scale factor for conversion. 63 * @param[in] nb_elements 64 * Number of elements in the buffer. 65 * @param[in] input 66 * Input buffer containing float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 67 * @param[out] output 68 * Output buffer to store INT8 numbers. Size of buffer is equal to (nb_elements * 1) bytes. 69 * 70 * @return 71 * - 0, Success. 72 * - < 0, Error code on failure. 73 */ 74 __rte_internal 75 int 76 rte_ml_io_float32_to_int8(float scale, uint64_t nb_elements, void *input, void *output); 77 78 /** 79 * @internal 80 * 81 * Convert a buffer containing numbers in signed 8-bit integer format (INT8) to single precision 82 * floating format (float32). 83 * 84 * @param[in] scale 85 * Scale factor for conversion. 86 * @param[in] nb_elements 87 * Number of elements in the buffer. 88 * @param[in] input 89 * Input buffer containing INT8 numbers. Size of buffer is equal to (nb_elements * 1) bytes. 90 * @param[out] output 91 * Output buffer to store float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 92 * 93 * @return 94 * - 0, Success. 95 * - < 0, Error code on failure. 96 */ 97 __rte_internal 98 int 99 rte_ml_io_int8_to_float32(float scale, uint64_t nb_elements, void *input, void *output); 100 101 /** 102 * @internal 103 * 104 * Convert a buffer containing numbers in single precision floating format (float32) to unsigned 105 * 8-bit integer format (UINT8). 106 * 107 * @param[in] scale 108 * Scale factor for conversion. 109 * @param[in] nb_elements 110 * Number of elements in the buffer. 111 * @param[in] input 112 * Input buffer containing float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 113 * @param[out] output 114 * Output buffer to store UINT8 numbers. Size of buffer is equal to (nb_elements * 1) bytes. 115 * 116 * @return 117 * - 0, Success. 118 * - < 0, Error code on failure. 119 */ 120 __rte_internal 121 int 122 rte_ml_io_float32_to_uint8(float scale, uint64_t nb_elements, void *input, void *output); 123 124 /** 125 * @internal 126 * 127 * Convert a buffer containing numbers in unsigned 8-bit integer format (UINT8) to single precision 128 * floating format (float32). 129 * 130 * @param[in] scale 131 * Scale factor for conversion. 132 * @param[in] nb_elements 133 * Number of elements in the buffer. 134 * @param[in] input 135 * Input buffer containing UINT8 numbers. Size of buffer is equal to (nb_elements * 1) bytes. 136 * @param[out] output 137 * Output buffer to store float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 138 * 139 * @return 140 * - 0, Success. 141 * - < 0, Error code on failure. 142 */ 143 __rte_internal 144 int 145 rte_ml_io_uint8_to_float32(float scale, uint64_t nb_elements, void *input, void *output); 146 147 /** 148 * @internal 149 * 150 * Convert a buffer containing numbers in single precision floating format (float32) to signed 151 * 16-bit integer format (INT16). 152 * 153 * @param[in] scale 154 * Scale factor for conversion. 155 * @param[in] nb_elements 156 * Number of elements in the buffer. 157 * @param[in] input 158 * Input buffer containing float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 159 * @param[out] output 160 * Output buffer to store INT16 numbers. Size of buffer is equal to (nb_elements * 2) bytes. 161 * 162 * @return 163 * - 0, Success. 164 * - < 0, Error code on failure. 165 */ 166 __rte_internal 167 int 168 rte_ml_io_float32_to_int16(float scale, uint64_t nb_elements, void *input, void *output); 169 170 /** 171 * @internal 172 * 173 * Convert a buffer containing numbers in signed 16-bit integer format (INT16) to single precision 174 * floating format (float32). 175 * 176 * @param[in] scale 177 * Scale factor for conversion. 178 * @param[in] nb_elements 179 * Number of elements in the buffer. 180 * @param[in] input 181 * Input buffer containing INT16 numbers. Size of buffer is equal to (nb_elements * 2) bytes. 182 * @param[out] output 183 * Output buffer to store float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 184 * 185 * @return 186 * - 0, Success. 187 * - < 0, Error code on failure. 188 */ 189 __rte_internal 190 int 191 rte_ml_io_int16_to_float32(float scale, uint64_t nb_elements, void *input, void *output); 192 193 /** 194 * @internal 195 * 196 * Convert a buffer containing numbers in single precision floating format (float32) to unsigned 197 * 16-bit integer format (UINT16). 198 * 199 * @param[in] scale 200 * Scale factor for conversion. 201 * @param[in] nb_elements 202 * Number of elements in the buffer. 203 * @param[in] input 204 * Input buffer containing float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 205 * @param[out] output 206 * Output buffer to store UINT16 numbers. Size of buffer is equal to (nb_elements * 2) bytes. 207 * 208 * @return 209 * - 0, Success. 210 * - < 0, Error code on failure. 211 */ 212 __rte_internal 213 int 214 rte_ml_io_float32_to_uint16(float scale, uint64_t nb_elements, void *input, void *output); 215 216 /** 217 * @internal 218 * 219 * Convert a buffer containing numbers in unsigned 16-bit integer format (UINT16) to single 220 * precision floating format (float32). 221 * 222 * @param[in] scale 223 * Scale factor for conversion. 224 * @param[in] nb_elements 225 * Number of elements in the buffer. 226 * @param[in] input 227 * Input buffer containing UINT16 numbers. Size of buffer is equal to (nb_elements * 2) bytes. 228 * @param[out] output 229 * Output buffer to store float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 230 * 231 * @return 232 * - 0, Success. 233 * - < 0, Error code on failure. 234 */ 235 __rte_internal 236 int 237 rte_ml_io_uint16_to_float32(float scale, uint64_t nb_elements, void *input, void *output); 238 239 /** 240 * @internal 241 * 242 * Convert a buffer containing numbers in single precision floating format (float32) 243 * to signed 32-bit integer format (INT32). 244 * 245 * @param[in] scale 246 * Scale factor for conversion. 247 * @param[in] nb_elements 248 * Number of elements in the buffer. 249 * @param[in] input 250 * Input buffer containing float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 251 * @param[out] output 252 * Output buffer to store INT32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 253 * 254 * @return 255 * - 0, Success. 256 * - < 0, Error code on failure. 257 */ 258 __rte_internal 259 int 260 rte_ml_io_float32_to_int32(float scale, uint64_t nb_elements, void *input, void *output); 261 262 /** 263 * @internal 264 * 265 * Convert a buffer containing numbers in signed 32-bit integer format (INT32) 266 * to single precision floating format (float32). 267 * 268 * @param[in] scale 269 * Scale factor for conversion. 270 * @param[in] nb_elements 271 * Number of elements in the buffer. 272 * @param[in] input 273 * Input buffer containing INT32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 274 * @param[out] output 275 * Output buffer to store float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 276 * 277 * @return 278 * - 0, Success. 279 * - < 0, Error code on failure. 280 */ 281 __rte_internal 282 int 283 rte_ml_io_int32_to_float32(float scale, uint64_t nb_elements, void *input, void *output); 284 285 /** 286 * @internal 287 * 288 * Convert a buffer containing numbers in single precision floating format (float32) 289 * to unsigned 32-bit integer format (UINT32). 290 * 291 * @param[in] scale 292 * Scale factor for conversion. 293 * @param[in] nb_elements 294 * Number of elements in the buffer. 295 * @param[in] input 296 * Input buffer containing float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 297 * @param[out] output 298 * Output buffer to store UINT32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 299 * 300 * @return 301 * - 0, Success. 302 * - < 0, Error code on failure. 303 */ 304 __rte_internal 305 int 306 rte_ml_io_float32_to_uint32(float scale, uint64_t nb_elements, void *input, void *output); 307 308 /** 309 * @internal 310 * 311 * Convert a buffer containing numbers in unsigned 32-bit integer format (UINT32) 312 * to single precision floating format (float32). 313 * 314 * @param[in] scale 315 * Scale factor for conversion. 316 * @param[in] nb_elements 317 * Number of elements in the buffer. 318 * @param[in] input 319 * Input buffer containing UINT32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 320 * @param[out] output 321 * Output buffer to store float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 322 * 323 * @return 324 * - 0, Success. 325 * - < 0, Error code on failure. 326 */ 327 __rte_internal 328 int 329 rte_ml_io_uint32_to_float32(float scale, uint64_t nb_elements, void *input, void *output); 330 331 /** 332 * @internal 333 * 334 * Convert a buffer containing numbers in single precision floating format (float32) 335 * to signed 64-bit integer format (INT64). 336 * 337 * @param[in] scale 338 * Scale factor for conversion. 339 * @param[in] nb_elements 340 * Number of elements in the buffer. 341 * @param[in] input 342 * Input buffer containing float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 343 * @param[out] output 344 * Output buffer to store INT64 numbers. Size of buffer is equal to (nb_elements * 8) bytes. 345 * 346 * @return 347 * - 0, Success. 348 * - < 0, Error code on failure. 349 */ 350 __rte_internal 351 int 352 rte_ml_io_float32_to_int64(float scale, uint64_t nb_elements, void *input, void *output); 353 354 /** 355 * @internal 356 * 357 * Convert a buffer containing numbers in signed 64-bit integer format (INT64) 358 * to single precision floating format (float32). 359 * 360 * @param[in] scale 361 * Scale factor for conversion. 362 * @param[in] nb_elements 363 * Number of elements in the buffer. 364 * @param[in] input 365 * Input buffer containing INT64 numbers. Size of buffer is equal to (nb_elements * 8) bytes. 366 * @param[out] output 367 * Output buffer to store float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 368 * 369 * @return 370 * - 0, Success. 371 * - < 0, Error code on failure. 372 */ 373 __rte_internal 374 int 375 rte_ml_io_int64_to_float32(float scale, uint64_t nb_elements, void *input, void *output); 376 377 /** 378 * @internal 379 * 380 * Convert a buffer containing numbers in single precision floating format (float32) 381 * to unsigned 64-bit integer format (UINT64). 382 * 383 * @param[in] scale 384 * Scale factor for conversion. 385 * @param[in] nb_elements 386 * Number of elements in the buffer. 387 * @param[in] input 388 * Input buffer containing float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 389 * @param[out] output 390 * Output buffer to store UINT64 numbers. Size of buffer is equal to (nb_elements * 8) bytes. 391 * 392 * @return 393 * - 0, Success. 394 * - < 0, Error code on failure. 395 */ 396 __rte_internal 397 int 398 rte_ml_io_float32_to_uint64(float scale, uint64_t nb_elements, void *input, void *output); 399 400 /** 401 * @internal 402 * 403 * Convert a buffer containing numbers in unsigned 64-bit integer format (UINT64) 404 * to single precision floating format (float32). 405 * 406 * @param[in] scale 407 * Scale factor for conversion. 408 * @param[in] nb_elements 409 * Number of elements in the buffer. 410 * @param[in] input 411 * Input buffer containing UINT64 numbers. Size of buffer is equal to (nb_elements * 8) bytes. 412 * @param[out] output 413 * Output buffer to store float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 414 * 415 * @return 416 * - 0, Success. 417 * - < 0, Error code on failure. 418 */ 419 __rte_internal 420 int 421 rte_ml_io_uint64_to_float32(float scale, uint64_t nb_elements, void *input, void *output); 422 423 /** 424 * @internal 425 * 426 * Convert a buffer containing numbers in single precision floating format (float32) to half 427 * precision floating point format (FP16). 428 * 429 * @param[in] nb_elements 430 * Number of elements in the buffer. 431 * @param[in] input 432 * Input buffer containing float32 numbers. Size of buffer is equal to (nb_elements *4) bytes. 433 * @param[out] output 434 * Output buffer to store float16 numbers. Size of buffer is equal to (nb_elements * 2) bytes. 435 * 436 * @return 437 * - 0, Success. 438 * - < 0, Error code on failure. 439 */ 440 __rte_internal 441 int 442 rte_ml_io_float32_to_float16(uint64_t nb_elements, void *input, void *output); 443 444 /** 445 * @internal 446 * 447 * Convert a buffer containing numbers in half precision floating format (FP16) to single precision 448 * floating point format (float32). 449 * 450 * @param[in] nb_elements 451 * Number of elements in the buffer. 452 * @param[in] input 453 * Input buffer containing float16 numbers. Size of buffer is equal to (nb_elements * 2) bytes. 454 * @param[out] output 455 * Output buffer to store float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 456 * 457 * @return 458 * - 0, Success. 459 * - < 0, Error code on failure. 460 */ 461 __rte_internal 462 int 463 rte_ml_io_float16_to_float32(uint64_t nb_elements, void *input, void *output); 464 465 /** 466 * @internal 467 * 468 * Convert a buffer containing numbers in single precision floating format (float32) to brain 469 * floating point format (bfloat16). 470 * 471 * @param[in] nb_elements 472 * Number of elements in the buffer. 473 * @param[in] input 474 * Input buffer containing float32 numbers. Size of buffer is equal to (nb_elements *4) bytes. 475 * @param[out] output 476 * Output buffer to store bfloat16 numbers. Size of buffer is equal to (nb_elements * 2) bytes. 477 * 478 * @return 479 * - 0, Success. 480 * - < 0, Error code on failure. 481 */ 482 __rte_internal 483 int 484 rte_ml_io_float32_to_bfloat16(uint64_t nb_elements, void *input, void *output); 485 486 /** 487 * @internal 488 * 489 * Convert a buffer containing numbers in brain floating point format (bfloat16) to single precision 490 * floating point format (float32). 491 * 492 * @param[in] nb_elements 493 * Number of elements in the buffer. 494 * @param[in] input 495 * Input buffer containing bfloat16 numbers. Size of buffer is equal to (nb_elements * 2) 496 * bytes. 497 * @param[out] output 498 * Output buffer to store float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 499 * 500 * @return 501 * - 0, Success. 502 * - < 0, Error code on failure. 503 */ 504 __rte_internal 505 int 506 rte_ml_io_bfloat16_to_float32(uint64_t nb_elements, void *input, void *output); 507 508 #ifdef __cplusplus 509 } 510 #endif 511 512 #endif /* RTE_MLDEV_UTILS_H */ 513