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 #ifdef __cplusplus 9 extern "C" { 10 #endif 11 12 /** 13 * @file 14 * 15 * ML Device PMD utility API 16 * 17 * These APIs for the use from ML drivers, user applications shouldn't use them. 18 */ 19 20 #include <rte_compat.h> 21 #include <rte_mldev.h> 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 * Get the name of an ML IO format. 59 * 60 * @param[in] type 61 * Enumeration of ML IO format. 62 * @param[in] str 63 * Address of character array. 64 * @param[in] len 65 * Length of character array. 66 */ 67 __rte_internal 68 void 69 rte_ml_io_format_to_str(enum rte_ml_io_format format, char *str, int len); 70 71 /** 72 * @internal 73 * 74 * Convert a buffer containing numbers in single precision floating format (float32) to signed 8-bit 75 * integer format (INT8). 76 * 77 * @param[in] scale 78 * Scale factor for conversion. 79 * @param[in] nb_elements 80 * Number of elements in the buffer. 81 * @param[in] input 82 * Input buffer containing float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 83 * @param[out] output 84 * Output buffer to store INT8 numbers. Size of buffer is equal to (nb_elements * 1) bytes. 85 * 86 * @return 87 * - 0, Success. 88 * - < 0, Error code on failure. 89 */ 90 __rte_internal 91 int 92 rte_ml_io_float32_to_int8(float scale, uint64_t nb_elements, void *input, void *output); 93 94 /** 95 * @internal 96 * 97 * Convert a buffer containing numbers in signed 8-bit integer format (INT8) to single precision 98 * floating format (float32). 99 * 100 * @param[in] scale 101 * Scale factor for conversion. 102 * @param[in] nb_elements 103 * Number of elements in the buffer. 104 * @param[in] input 105 * Input buffer containing INT8 numbers. Size of buffer is equal to (nb_elements * 1) bytes. 106 * @param[out] output 107 * Output buffer to store float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 108 * 109 * @return 110 * - 0, Success. 111 * - < 0, Error code on failure. 112 */ 113 __rte_internal 114 int 115 rte_ml_io_int8_to_float32(float scale, uint64_t nb_elements, void *input, void *output); 116 117 /** 118 * @internal 119 * 120 * Convert a buffer containing numbers in single precision floating format (float32) to unsigned 121 * 8-bit integer format (UINT8). 122 * 123 * @param[in] scale 124 * Scale factor for conversion. 125 * @param[in] nb_elements 126 * Number of elements in the buffer. 127 * @param[in] input 128 * Input buffer containing float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 129 * @param[out] output 130 * Output buffer to store UINT8 numbers. Size of buffer is equal to (nb_elements * 1) bytes. 131 * 132 * @return 133 * - 0, Success. 134 * - < 0, Error code on failure. 135 */ 136 __rte_internal 137 int 138 rte_ml_io_float32_to_uint8(float scale, uint64_t nb_elements, void *input, void *output); 139 140 /** 141 * @internal 142 * 143 * Convert a buffer containing numbers in unsigned 8-bit integer format (UINT8) to single precision 144 * floating format (float32). 145 * 146 * @param[in] scale 147 * Scale factor for conversion. 148 * @param[in] nb_elements 149 * Number of elements in the buffer. 150 * @param[in] input 151 * Input buffer containing UINT8 numbers. Size of buffer is equal to (nb_elements * 1) bytes. 152 * @param[out] output 153 * Output buffer to store float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 154 * 155 * @return 156 * - 0, Success. 157 * - < 0, Error code on failure. 158 */ 159 __rte_internal 160 int 161 rte_ml_io_uint8_to_float32(float scale, uint64_t nb_elements, void *input, void *output); 162 163 /** 164 * @internal 165 * 166 * Convert a buffer containing numbers in single precision floating format (float32) to signed 167 * 16-bit integer format (INT16). 168 * 169 * @param[in] scale 170 * Scale factor for conversion. 171 * @param[in] nb_elements 172 * Number of elements in the buffer. 173 * @param[in] input 174 * Input buffer containing float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 175 * @param[out] output 176 * Output buffer to store INT16 numbers. Size of buffer is equal to (nb_elements * 2) bytes. 177 * 178 * @return 179 * - 0, Success. 180 * - < 0, Error code on failure. 181 */ 182 __rte_internal 183 int 184 rte_ml_io_float32_to_int16(float scale, uint64_t nb_elements, void *input, void *output); 185 186 /** 187 * @internal 188 * 189 * Convert a buffer containing numbers in signed 16-bit integer format (INT16) to single precision 190 * floating format (float32). 191 * 192 * @param[in] scale 193 * Scale factor for conversion. 194 * @param[in] nb_elements 195 * Number of elements in the buffer. 196 * @param[in] input 197 * Input buffer containing INT16 numbers. Size of buffer is equal to (nb_elements * 2) bytes. 198 * @param[out] output 199 * Output buffer to store float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 200 * 201 * @return 202 * - 0, Success. 203 * - < 0, Error code on failure. 204 */ 205 __rte_internal 206 int 207 rte_ml_io_int16_to_float32(float scale, uint64_t nb_elements, void *input, void *output); 208 209 /** 210 * @internal 211 * 212 * Convert a buffer containing numbers in single precision floating format (float32) to unsigned 213 * 16-bit integer format (UINT16). 214 * 215 * @param[in] scale 216 * Scale factor for conversion. 217 * @param[in] nb_elements 218 * Number of elements in the buffer. 219 * @param[in] input 220 * Input buffer containing float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 221 * @param[out] output 222 * Output buffer to store UINT16 numbers. Size of buffer is equal to (nb_elements * 2) bytes. 223 * 224 * @return 225 * - 0, Success. 226 * - < 0, Error code on failure. 227 */ 228 __rte_internal 229 int 230 rte_ml_io_float32_to_uint16(float scale, uint64_t nb_elements, void *input, void *output); 231 232 /** 233 * @internal 234 * 235 * Convert a buffer containing numbers in unsigned 16-bit integer format (UINT16) to single 236 * precision floating format (float32). 237 * 238 * @param[in] scale 239 * Scale factor for conversion. 240 * @param[in] nb_elements 241 * Number of elements in the buffer. 242 * @param[in] input 243 * Input buffer containing UINT16 numbers. Size of buffer is equal to (nb_elements * 2) bytes. 244 * @param[out] output 245 * Output buffer to store float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 246 * 247 * @return 248 * - 0, Success. 249 * - < 0, Error code on failure. 250 */ 251 __rte_internal 252 int 253 rte_ml_io_uint16_to_float32(float scale, uint64_t nb_elements, void *input, void *output); 254 255 /** 256 * @internal 257 * 258 * Convert a buffer containing numbers in single precision floating format (float32) to half 259 * precision floating point format (FP16). 260 * 261 * @param[in] nb_elements 262 * Number of elements in the buffer. 263 * @param[in] input 264 * Input buffer containing float32 numbers. Size of buffer is equal to (nb_elements *4) bytes. 265 * @param[out] output 266 * Output buffer to store float16 numbers. Size of buffer is equal to (nb_elements * 2) bytes. 267 * 268 * @return 269 * - 0, Success. 270 * - < 0, Error code on failure. 271 */ 272 __rte_internal 273 int 274 rte_ml_io_float32_to_float16(uint64_t nb_elements, void *input, void *output); 275 276 /** 277 * @internal 278 * 279 * Convert a buffer containing numbers in half precision floating format (FP16) to single precision 280 * floating point format (float32). 281 * 282 * @param[in] nb_elements 283 * Number of elements in the buffer. 284 * @param[in] input 285 * Input buffer containing float16 numbers. Size of buffer is equal to (nb_elements * 2) bytes. 286 * @param[out] output 287 * Output buffer to store float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 288 * 289 * @return 290 * - 0, Success. 291 * - < 0, Error code on failure. 292 */ 293 __rte_internal 294 int 295 rte_ml_io_float16_to_float32(uint64_t nb_elements, void *input, void *output); 296 297 /** 298 * @internal 299 * 300 * Convert a buffer containing numbers in single precision floating format (float32) to brain 301 * floating point format (bfloat16). 302 * 303 * @param[in] nb_elements 304 * Number of elements in the buffer. 305 * @param[in] input 306 * Input buffer containing float32 numbers. Size of buffer is equal to (nb_elements *4) bytes. 307 * @param[out] output 308 * Output buffer to store bfloat16 numbers. Size of buffer is equal to (nb_elements * 2) bytes. 309 * 310 * @return 311 * - 0, Success. 312 * - < 0, Error code on failure. 313 */ 314 __rte_internal 315 int 316 rte_ml_io_float32_to_bfloat16(uint64_t nb_elements, void *input, void *output); 317 318 /** 319 * @internal 320 * 321 * Convert a buffer containing numbers in brain floating point format (bfloat16) to single precision 322 * floating point format (float32). 323 * 324 * @param[in] nb_elements 325 * Number of elements in the buffer. 326 * @param[in] input 327 * Input buffer containing bfloat16 numbers. Size of buffer is equal to (nb_elements * 2) 328 * bytes. 329 * @param[out] output 330 * Output buffer to store float32 numbers. Size of buffer is equal to (nb_elements * 4) bytes. 331 * 332 * @return 333 * - 0, Success. 334 * - < 0, Error code on failure. 335 */ 336 __rte_internal 337 int 338 rte_ml_io_bfloat16_to_float32(uint64_t nb_elements, void *input, void *output); 339 340 #ifdef __cplusplus 341 } 342 #endif 343 344 #endif /* RTE_MLDEV_UTILS_H */ 345