test/common/type_traits.hpp
| 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 | #pragma once | ||
| 8 | |||
| 9 | #include <type_traits> | ||
| 10 | |||
| 11 | namespace kai::test { | ||
| 12 | |||
| 13 | class UInt4; | ||
| 14 | class Int4; | ||
| 15 | class Float16; | ||
| 16 | |||
| 17 | template <bool hardware_support = true> | ||
| 18 | class BFloat16; | ||
| 19 | |||
| 20 | /// `true` if `T` is unsigned numeric type. | ||
| 21 | template <typename T> | ||
| 22 | inline constexpr bool is_unsigned = std::is_unsigned_v<T>; | ||
| 23 | |||
| 24 | /// `true` if `T` is unsigned numeric type. | ||
| 25 | template <> | ||
| 26 | inline constexpr bool is_unsigned<UInt4> = true; | ||
| 27 | |||
| 28 | /// `true` if `T` is unsigned numeric type. | ||
| 29 | template <> | ||
| 30 | inline constexpr bool is_unsigned<Int4> = false; | ||
| 31 | |||
| 32 | /// `true` if `T` is unsigned numeric type. | ||
| 33 | template <> | ||
| 34 | inline constexpr bool is_unsigned<BFloat16<>> = false; | ||
| 35 | |||
| 36 | /// `true` if `T` is signed numeric type. | ||
| 37 | template <typename T> | ||
| 38 | inline constexpr bool is_signed = std::is_signed_v<T>; | ||
| 39 | |||
| 40 | /// `true` if `T` is signed numeric type. | ||
| 41 | template <> | ||
| 42 | inline constexpr bool is_signed<UInt4> = false; | ||
| 43 | |||
| 44 | /// `true` if `T` is signed numeric type. | ||
| 45 | template <> | ||
| 46 | inline constexpr bool is_signed<Int4> = true; | ||
| 47 | |||
| 48 | /// `true` if `T` is signed numeric type. | ||
| 49 | template <> | ||
| 50 | inline constexpr bool is_signed<BFloat16<>> = true; | ||
| 51 | |||
| 52 | /// `true` if `T` is integral numeric type. | ||
| 53 | template <typename T> | ||
| 54 | inline constexpr bool is_integral = std::is_integral_v<T>; | ||
| 55 | |||
| 56 | /// `true` if `T` is integral numeric type. | ||
| 57 | template <> | ||
| 58 | inline constexpr bool is_integral<UInt4> = true; | ||
| 59 | |||
| 60 | /// `true` if `T` is integral numeric type. | ||
| 61 | template <> | ||
| 62 | inline constexpr bool is_integral<Int4> = true; | ||
| 63 | |||
| 64 | /// `true` if `T` is integral numeric type. | ||
| 65 | template <> | ||
| 66 | inline constexpr bool is_integral<BFloat16<>> = false; | ||
| 67 | |||
| 68 | /// `true` if `T` is floating-point type. | ||
| 69 | template <typename T> | ||
| 70 | inline constexpr bool is_floating_point = std::is_floating_point_v<T>; | ||
| 71 | |||
| 72 | /// `true` if `T` is floating-point type. | ||
| 73 | template <> | ||
| 74 | inline constexpr bool is_floating_point<Float16> = true; | ||
| 75 | |||
| 76 | /// `true` if `T` is floating-point type. | ||
| 77 | template <> | ||
| 78 | inline constexpr bool is_floating_point<BFloat16<>> = true; | ||
| 79 | |||
| 80 | /// `true` if `T` is integral or floating-point type. | ||
| 81 | template <typename T> | ||
| 82 | inline constexpr bool is_arithmetic = is_integral<T> || is_floating_point<T>; | ||
| 83 | |||
| 84 | /// Signed version of type `T`. | ||
| 85 | template <typename T> | ||
| 86 | struct make_signed { | ||
| 87 | using type = std::make_signed_t<T>; | ||
| 88 | }; | ||
| 89 | |||
| 90 | /// Signed version of type `T`. | ||
| 91 | template <> | ||
| 92 | struct make_signed<UInt4> { | ||
| 93 | using type = Int4; | ||
| 94 | }; | ||
| 95 | |||
| 96 | /// Signed version of type `T`. | ||
| 97 | template <> | ||
| 98 | struct make_signed<Int4> { | ||
| 99 | using type = Int4; | ||
| 100 | }; | ||
| 101 | |||
| 102 | /// Signed version of type `T`. | ||
| 103 | template <typename T> | ||
| 104 | using make_signed_t = typename make_signed<T>::type; | ||
| 105 | |||
| 106 | /// Gets the value in type suitable to write to the output stream. | ||
| 107 | /// | ||
| 108 | /// 8-bit integer type by default is written out as character, so we need to convert them | ||
| 109 | /// to other integer type so that the output stream treats them as number instead of character. | ||
| 110 | template <typename T, std::enable_if_t<is_arithmetic<T>, bool> = true> | ||
| 111 | ✗ | inline auto displayable(T value) { | |
| 112 | if constexpr (is_integral<T> && sizeof(T) == 1) { | ||
| 113 | ✗ | return static_cast<int>(value); | |
| 114 | } else { | ||
| 115 | ✗ | return value; | |
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | } // namespace kai::test | ||
| 120 |