Elffuss BraidHub

kikocis/kleidiai public

main · 3.9 KiBRawPermalink
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
11namespace kai::test {
12
13class UInt2;
14class Int2;
15class UInt4;
16class Int4;
17class Float16;
18
19template <bool hardware_support = true>
20class BFloat16;
21
22/// `true` if `T` is unsigned numeric type.
23template <typename T>
24inline constexpr bool is_unsigned = std::is_unsigned_v<T>;
25
26/// `true` if `T` is unsigned numeric type.
27template <>
28inline constexpr bool is_unsigned<UInt4> = true;
29
30/// `true` if `T` is unsigned numeric type.
31template <>
32inline constexpr bool is_unsigned<Int4> = false;
33
34/// `true` if `T` is unsigned numeric type.
35template <>
36inline constexpr bool is_unsigned<UInt2> = true;
37
38/// `true` if `T` is unsigned numeric type.
39template <>
40inline constexpr bool is_unsigned<Int2> = false;
41
42/// `true` if `T` is unsigned numeric type.
43template <>
44inline constexpr bool is_unsigned<BFloat16<>> = false;
45
46/// `true` if `T` is signed numeric type.
47template <typename T>
48inline constexpr bool is_signed = std::is_signed_v<T>;
49
50/// `true` if `T` is signed numeric type.
51template <>
52inline constexpr bool is_signed<UInt4> = false;
53
54/// `true` if `T` is signed numeric type.
55template <>
56inline constexpr bool is_signed<Int4> = true;
57
58/// `true` if `T` is signed numeric type.
59template <>
60inline constexpr bool is_signed<UInt2> = false;
61
62/// `true` if `T` is signed numeric type.
63template <>
64inline constexpr bool is_signed<Int2> = true;
65
66/// `true` if `T` is signed numeric type.
67template <>
68inline constexpr bool is_signed<BFloat16<>> = true;
69
70/// `true` if `T` is integral numeric type.
71template <typename T>
72inline constexpr bool is_integral = std::is_integral_v<T>;
73
74/// `true` if `T` is integral numeric type.
75template <>
76inline constexpr bool is_integral<UInt4> = true;
77
78/// `true` if `T` is integral numeric type.
79template <>
80inline constexpr bool is_integral<Int4> = true;
81
82/// `true` if `T` is integral numeric type.
83template <>
84inline constexpr bool is_integral<UInt2> = true;
85
86/// `true` if `T` is integral numeric type.
87template <>
88inline constexpr bool is_integral<Int2> = true;
89
90/// `true` if `T` is integral numeric type.
91template <>
92inline constexpr bool is_integral<BFloat16<>> = false;
93
94/// `true` if `T` is floating-point type.
95template <typename T>
96inline constexpr bool is_floating_point = std::is_floating_point_v<T>;
97
98/// `true` if `T` is floating-point type.
99template <>
100inline constexpr bool is_floating_point<Float16> = true;
101
102/// `true` if `T` is floating-point type.
103template <>
104inline constexpr bool is_floating_point<BFloat16<>> = true;
105
106/// `true` if `T` is integral or floating-point type.
107template <typename T>
108inline constexpr bool is_arithmetic = is_integral<T> || is_floating_point<T>;
109
110/// Signed version of type `T`.
111template <typename T>
112struct make_signed {
113 using type = std::make_signed_t<T>;
114};
115
116/// Signed version of type `T`.
117template <>
118struct make_signed<UInt4> {
119 using type = Int4;
120};
121
122/// Signed version of type `T`.
123template <>
124struct make_signed<Int4> {
125 using type = Int4;
126};
127
128/// Signed version of type `T`.
129template <>
130struct make_signed<UInt2> {
131 using type = Int2;
132};
133
134/// Signed version of type `T`.
135template <>
136struct make_signed<Int2> {
137 using type = Int2;
138};
139
140/// Signed version of type `T`.
141template <typename T>
142using 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.
148template <typename T, std::enable_if_t<is_arithmetic<T>, bool> = true>
149inline 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