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