test/common/data_type.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // | ||
| 2 | // SPDX-FileCopyrightText: Copyright 2024-2025 Arm Limited and/or its affiliates <open-source-office@arm.com> | ||
| 3 | // | ||
| 4 | // SPDX-License-Identifier: Apache-2.0 | ||
| 5 | // | ||
| 6 | |||
| 7 | #include "test/common/data_type.hpp" | ||
| 8 | |||
| 9 | #include <cstddef> | ||
| 10 | #include <cstdint> | ||
| 11 | |||
| 12 | #include "kai/kai_common.h" | ||
| 13 | |||
| 14 | namespace kai::test { | ||
| 15 | |||
| 16 | namespace { | ||
| 17 | |||
| 18 | 159126 | bool has_i(DataType dt) { | |
| 19 | 159126 | return (static_cast<uint16_t>(dt) & (1 << 15)) != 0; | |
| 20 | } | ||
| 21 | |||
| 22 | ✗ | bool has_s(DataType dt) { | |
| 23 | ✗ | return (static_cast<uint16_t>(dt) & (1 << 14)) != 0; | |
| 24 | } | ||
| 25 | |||
| 26 | 156340 | bool has_q(DataType dt) { | |
| 27 | 156340 | return (static_cast<uint16_t>(dt) & (1 << 13)) != 0; | |
| 28 | } | ||
| 29 | |||
| 30 | 78170 | bool has_a(DataType dt) { | |
| 31 | 78170 | return (static_cast<uint16_t>(dt) & (1 << 12)) != 0; | |
| 32 | } | ||
| 33 | |||
| 34 | 302955 | size_t bits(DataType dt) { | |
| 35 | 302955 | return static_cast<uint16_t>(dt) & 0xFF; | |
| 36 | } | ||
| 37 | |||
| 38 | } // namespace | ||
| 39 | |||
| 40 | 302955 | size_t data_type_size_in_bits(DataType dt) { | |
| 41 | 302955 | return bits(dt); | |
| 42 | } | ||
| 43 | |||
| 44 | 159126 | bool data_type_is_integral(DataType dt) { | |
| 45 | 159126 | return has_i(dt); | |
| 46 | } | ||
| 47 | |||
| 48 | ✗ | bool data_type_is_float(DataType dt) { | |
| 49 | − | KAI_ASSERT_ALWAYS(data_type_is_signed(dt)); | |
| 50 | ✗ | return !data_type_is_integral(dt); | |
| 51 | } | ||
| 52 | |||
| 53 | ✗ | bool data_type_is_float_fp(DataType dt) { | |
| 54 | − | KAI_ASSERT_ALWAYS(data_type_is_float(dt)); | |
| 55 | ✗ | return !has_q(dt); | |
| 56 | } | ||
| 57 | |||
| 58 | ✗ | bool data_type_is_float_bf(DataType dt) { | |
| 59 | − | KAI_ASSERT_ALWAYS(data_type_is_float(dt)); | |
| 60 | ✗ | return has_q(dt); | |
| 61 | } | ||
| 62 | |||
| 63 | ✗ | bool data_type_is_signed(DataType dt) { | |
| 64 | ✗ | if (!has_s(dt)) { | |
| 65 | − | KAI_ASSERT_ALWAYS(data_type_is_integral(dt)); | |
| 66 | ✗ | } | |
| 67 | |||
| 68 | ✗ | return has_s(dt); | |
| 69 | } | ||
| 70 | |||
| 71 | 159126 | bool data_type_is_quantized(DataType dt) { | |
| 72 |
2/2✓ Branch 0 taken 2786 times.
✓ Branch 1 taken 156340 times.
|
159126 | return data_type_is_integral(dt) && has_q(dt); |
| 73 | } | ||
| 74 | |||
| 75 | 78170 | bool data_type_is_quantized_asymm(DataType dt) { | |
| 76 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 78170 times.
|
78170 | return data_type_is_quantized(dt) && has_a(dt); |
| 77 | } | ||
| 78 | |||
| 79 | ✗ | bool data_type_is_quantized_int8(DataType dt) { | |
| 80 | ✗ | return data_type_is_quantized(dt) && data_type_size_in_bits(dt) == 8; | |
| 81 | } | ||
| 82 | |||
| 83 | ✗ | bool data_type_is_quantized_int4(DataType dt) { | |
| 84 | ✗ | return data_type_is_quantized(dt) && data_type_size_in_bits(dt) == 4; | |
| 85 | } | ||
| 86 | |||
| 87 | } // namespace kai::test | ||
| 88 |