| 1 | // |
| 2 | // SPDX-FileCopyrightText: Copyright 2024-2026 Arm Limited and/or its affiliates <open-source-office@arm.com> |
| 3 | // |
| 4 | // SPDX-License-Identifier: Apache-2.0 |
| 5 | // |
| 6 | |
| 7 | #pragma once |
| 8 | |
| 9 | #include <type_traits> |
| 10 | |
| 11 | namespace kai::test { |
| 12 | |
| 13 | class UInt2; |
| 14 | class Int2; |
| 15 | class UInt4; |
| 16 | class Int4; |
| 17 | class Float16; |
| 18 | |
| 19 | template <bool hardware_support = true> |
| 20 | class BFloat16; |
| 21 | |
| 22 | /// `true` if `T` is unsigned numeric type. |
| 23 | template <typename T> |
| 24 | inline constexpr bool is_unsigned = std::is_unsigned_v<T>; |
| 25 | |
| 26 | /// `true` if `T` is unsigned numeric type. |
| 27 | template <> |
| 28 | inline constexpr bool is_unsigned<UInt4> = true; |
| 29 | |
| 30 | /// `true` if `T` is unsigned numeric type. |
| 31 | template <> |
| 32 | inline constexpr bool is_unsigned<Int4> = false; |
| 33 | |
| 34 | /// `true` if `T` is unsigned numeric type. |
| 35 | template <> |
| 36 | inline constexpr bool is_unsigned<UInt2> = true; |
| 37 | |
| 38 | /// `true` if `T` is unsigned numeric type. |
| 39 | template <> |
| 40 | inline constexpr bool is_unsigned<Int2> = false; |
| 41 | |
| 42 | /// `true` if `T` is unsigned numeric type. |
| 43 | template <> |
| 44 | inline constexpr bool is_unsigned<BFloat16<>> = false; |
| 45 | |
| 46 | /// `true` if `T` is signed numeric type. |
| 47 | template <typename T> |
| 48 | inline constexpr bool is_signed = std::is_signed_v<T>; |
| 49 | |
| 50 | /// `true` if `T` is signed numeric type. |
| 51 | template <> |
| 52 | inline constexpr bool is_signed<UInt4> = false; |
| 53 | |
| 54 | /// `true` if `T` is signed numeric type. |
| 55 | template <> |
| 56 | inline constexpr bool is_signed<Int4> = true; |
| 57 | |
| 58 | /// `true` if `T` is signed numeric type. |
| 59 | template <> |
| 60 | inline constexpr bool is_signed<UInt2> = false; |
| 61 | |
| 62 | /// `true` if `T` is signed numeric type. |
| 63 | template <> |
| 64 | inline constexpr bool is_signed<Int2> = true; |
| 65 | |
| 66 | /// `true` if `T` is signed numeric type. |
| 67 | template <> |
| 68 | inline constexpr bool is_signed<BFloat16<>> = true; |
| 69 | |
| 70 | /// `true` if `T` is integral numeric type. |
| 71 | template <typename T> |
| 72 | inline constexpr bool is_integral = std::is_integral_v<T>; |
| 73 | |
| 74 | /// `true` if `T` is integral numeric type. |
| 75 | template <> |
| 76 | inline constexpr bool is_integral<UInt4> = true; |
| 77 | |
| 78 | /// `true` if `T` is integral numeric type. |
| 79 | template <> |
| 80 | inline constexpr bool is_integral<Int4> = true; |
| 81 | |
| 82 | /// `true` if `T` is integral numeric type. |
| 83 | template <> |
| 84 | inline constexpr bool is_integral<UInt2> = true; |
| 85 | |
| 86 | /// `true` if `T` is integral numeric type. |
| 87 | template <> |
| 88 | inline constexpr bool is_integral<Int2> = true; |
| 89 | |
| 90 | /// `true` if `T` is integral numeric type. |
| 91 | template <> |
| 92 | inline constexpr bool is_integral<BFloat16<>> = false; |
| 93 | |
| 94 | /// `true` if `T` is floating-point type. |
| 95 | template <typename T> |
| 96 | inline constexpr bool is_floating_point = std::is_floating_point_v<T>; |
| 97 | |
| 98 | /// `true` if `T` is floating-point type. |
| 99 | template <> |
| 100 | inline constexpr bool is_floating_point<Float16> = true; |
| 101 | |
| 102 | /// `true` if `T` is floating-point type. |
| 103 | template <> |
| 104 | inline constexpr bool is_floating_point<BFloat16<>> = true; |
| 105 | |
| 106 | /// `true` if `T` is integral or floating-point type. |
| 107 | template <typename T> |
| 108 | inline constexpr bool is_arithmetic = is_integral<T> || is_floating_point<T>; |
| 109 | |
| 110 | /// Signed version of type `T`. |
| 111 | template <typename T> |
| 112 | struct make_signed { |
| 113 | using type = std::make_signed_t<T>; |
| 114 | }; |
| 115 | |
| 116 | /// Signed version of type `T`. |
| 117 | template <> |
| 118 | struct make_signed<UInt4> { |
| 119 | using type = Int4; |
| 120 | }; |
| 121 | |
| 122 | /// Signed version of type `T`. |
| 123 | template <> |
| 124 | struct make_signed<Int4> { |
| 125 | using type = Int4; |
| 126 | }; |
| 127 | |
| 128 | /// Signed version of type `T`. |
| 129 | template <> |
| 130 | struct make_signed<UInt2> { |
| 131 | using type = Int2; |
| 132 | }; |
| 133 | |
| 134 | /// Signed version of type `T`. |
| 135 | template <> |
| 136 | struct make_signed<Int2> { |
| 137 | using type = Int2; |
| 138 | }; |
| 139 | |
| 140 | /// Signed version of type `T`. |
| 141 | template <typename T> |
| 142 | using make_signed_t = typename make_signed<T>::type; |
| 143 | |
| 144 | /// Gets the value in type suitable to write to the output stream. |
| 145 | /// |
| 146 | /// 8-bit integer type by default is written out as character, so we need to convert them |
| 147 | /// to other integer type so that the output stream treats them as number instead of character. |
| 148 | template <typename T, std::enable_if_t<is_arithmetic<T>, bool> = true> |
| 149 | inline auto displayable(T value) { |
| 150 | if constexpr (is_integral<T> && sizeof(T) == 1) { |
| 151 | return static_cast<int>(value); |
| 152 | } else { |
| 153 | return value; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | } // namespace kai::test |