|
| 1 | +#include <executorch/runtime/core/exec_aten/util/scalar_type_util.h> |
| 2 | +#include <executorch/runtime/platform/log.h> |
| 3 | + |
| 4 | +namespace executorch { |
| 5 | +namespace runtime { |
| 6 | + |
| 7 | +/** |
| 8 | + * Convert a scalar type value to a string representation. If |
| 9 | + * ET_ENABLE_ENUM_STRINGS is set (it is on bby default), this will return a |
| 10 | + * string name (for example, "Float"). Otherwise, it will return a string |
| 11 | + * representation of the index value ("6"). |
| 12 | + * |
| 13 | + * If the user buffer is not large enough to hold the string representation, the |
| 14 | + * string will be truncated. |
| 15 | + * |
| 16 | + * The return value is the number of characters written, or in the case of |
| 17 | + * truncation, the number of characters that would be written if the buffer was |
| 18 | + * large enough. |
| 19 | + */ |
| 20 | +size_t scalar_type_to_string( |
| 21 | + ::executorch::aten::ScalarType t, |
| 22 | + char* buffer, |
| 23 | + size_t buffer_size) { |
| 24 | +#if ET_ENABLE_ENUM_STRINGS |
| 25 | + const char* name_str; |
| 26 | +#define DEFINE_CASE(unused, name) \ |
| 27 | + case ::executorch::aten::ScalarType::name: \ |
| 28 | + name_str = #name; \ |
| 29 | + break; |
| 30 | + |
| 31 | + switch (t) { |
| 32 | + ET_FORALL_SCALAR_TYPES(DEFINE_CASE) |
| 33 | + default: |
| 34 | + name_str = "Unknown"; |
| 35 | + break; |
| 36 | + } |
| 37 | + |
| 38 | + return snprintf(buffer, buffer_size, "%s", name_str); |
| 39 | +#undef DEFINE_CASE |
| 40 | +#else |
| 41 | + return snprintf(buffer, buffer_size, "%d", static_cast<int>(t)); |
| 42 | +#endif // ET_ENABLE_ENUM_TO_STRING |
| 43 | +} |
| 44 | + |
| 45 | +} // namespace runtime |
| 46 | +} // namespace executorch |
0 commit comments