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/int4.hpp" | ||
8 | |||
9 | #include <cstdint> | ||
10 | #include <tuple> | ||
11 | #include <vector> | ||
12 | |||
13 | #include "kai/kai_common.h" | ||
14 | #include "test/common/buffer.hpp" | ||
15 | #include "test/common/memory.hpp" | ||
16 | |||
17 | namespace kai::test { | ||
18 | |||
19 | ✗ | UInt4& UInt4::operator=(uint8_t value) { | |
20 | − | KAI_ASSUME(value >= 0 && value < 16); | |
21 | ✗ | _value = value; | |
22 | ✗ | return *this; | |
23 | } | ||
24 | |||
25 | ✗ | UInt4& UInt4::operator=(int value) { | |
26 | − | KAI_ASSUME(value >= 0 && value < 16); | |
27 | ✗ | _value = static_cast<uint8_t>(value); | |
28 | ✗ | return *this; | |
29 | } | ||
30 | |||
31 | ✗ | UInt4::operator int32_t() const { | |
32 | ✗ | return _value; | |
33 | } | ||
34 | |||
35 | ✗ | UInt4::operator float() const { | |
36 | ✗ | return _value; | |
37 | } | ||
38 | |||
39 | ✗ | UInt4 UInt4::operator+(UInt4 rhs) const { | |
40 | ✗ | return UInt4(_value + rhs._value); | |
41 | } | ||
42 | |||
43 | ✗ | UInt4 UInt4::operator-(UInt4 rhs) const { | |
44 | ✗ | return UInt4(_value - rhs._value); | |
45 | } | ||
46 | |||
47 | ✗ | UInt4 UInt4::operator*(UInt4 rhs) const { | |
48 | ✗ | return UInt4(_value * rhs._value); | |
49 | } | ||
50 | |||
51 | ✗ | UInt4 UInt4::operator/(UInt4 rhs) const { | |
52 | ✗ | return UInt4(_value / rhs._value); | |
53 | } | ||
54 | |||
55 | 85904856 | uint8_t UInt4::pack_u8(UInt4 low, UInt4 high) { | |
56 | 85904856 | return (low._value & 0x0F) | (high._value << 4); | |
57 | } | ||
58 | |||
59 | 114030168 | std::tuple<UInt4, UInt4> UInt4::unpack_u8(uint8_t value) { | |
60 | 114030168 | const uint8_t low = value & 0x0F; | |
61 | 114030168 | const uint8_t high = value >> 4; | |
62 | |||
63 | 114030168 | return {UInt4(low), UInt4(high)}; | |
64 | 114030168 | } | |
65 | |||
66 | // ===================================================================================================================== | ||
67 | |||
68 | ✗ | Int4& Int4::operator=(int8_t value) { | |
69 | − | KAI_ASSUME(value >= -8 && value < 8); | |
70 | ✗ | _value = value; | |
71 | ✗ | return *this; | |
72 | } | ||
73 | |||
74 | ✗ | Int4& Int4::operator=(int value) { | |
75 | − | KAI_ASSUME(value >= -8 && value < 8); | |
76 | ✗ | _value = static_cast<int8_t>(value); | |
77 | ✗ | return *this; | |
78 | } | ||
79 | |||
80 | 486417508 | Int4::operator int32_t() const { | |
81 | 486417508 | return _value; | |
82 | } | ||
83 | |||
84 | 1711103008 | Int4::operator float() const { | |
85 | 1711103008 | return _value; | |
86 | } | ||
87 | |||
88 | ✗ | Int4 Int4::operator+(Int4 rhs) const { | |
89 | ✗ | return Int4(static_cast<int8_t>(_value + rhs._value)); | |
90 | } | ||
91 | |||
92 | ✗ | Int4 Int4::operator-(Int4 rhs) const { | |
93 | ✗ | return Int4(static_cast<int8_t>(_value - rhs._value)); | |
94 | } | ||
95 | |||
96 | ✗ | Int4 Int4::operator*(Int4 rhs) const { | |
97 | ✗ | return Int4(static_cast<int8_t>(_value * rhs._value)); | |
98 | } | ||
99 | |||
100 | ✗ | Int4 Int4::operator/(Int4 rhs) const { | |
101 | ✗ | return Int4(static_cast<int8_t>(_value / rhs._value)); | |
102 | } | ||
103 | |||
104 | 65000348 | uint8_t Int4::pack_u8(Int4 low, Int4 high) { | |
105 | 65000348 | const uint8_t lo = low._value & 0x0F; | |
106 | 65000348 | const uint8_t hi = high._value & 0x0F; | |
107 | 130000696 | return (lo << 0) | (hi << 4); | |
108 | 65000348 | } | |
109 | |||
110 | 2184608880 | std::tuple<Int4, Int4> Int4::unpack_u8(uint8_t value) { | |
111 | // NOLINTBEGIN(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions) | ||
112 | 2184608880 | const int8_t low = static_cast<int8_t>(value << 4) >> 4; | |
113 | 2184608880 | const int8_t high = static_cast<int8_t>(value) >> 4; | |
114 | // NOLINTEND(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions) | ||
115 | |||
116 | 2184608880 | return {Int4(low), Int4(high)}; | |
117 | 2184608880 | } | |
118 | |||
119 | // ===================================================================================================================== | ||
120 | |||
121 | 3924 | Buffer convert_s0s1_s1s0(const Buffer& src) { | |
122 | 3924 | const auto length = src.size(); | |
123 | 3924 | Buffer dst(length); | |
124 | |||
125 |
2/2✓ Branch 0 taken 8069376 times.
✓ Branch 1 taken 3924 times.
|
8073300 | for (size_t i = 0; i < length; ++i) { |
126 |
2/4✓ Branch 0 taken 8069376 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8069376 times.
✗ Branch 3 not taken.
|
8069376 | uint8_t val = read_array<uint8_t>(src.data(), i); |
127 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8069376 times.
|
8069376 | const auto [low, high] = UInt4::unpack_u8(val); |
128 | 24208128 | auto rev_val = UInt4::pack_u8(high, low); | |
129 |
2/4✓ Branch 0 taken 8069376 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8069376 times.
✗ Branch 3 not taken.
|
8069376 | write_array<uint8_t>(dst.data(), i, rev_val); |
130 | 8069376 | } | |
131 | 3924 | return dst; | |
132 | 3924 | } | |
133 | |||
134 | } // namespace kai::test | ||
135 |