| 1 | // |
| 2 | // SPDX-FileCopyrightText: Copyright 2026 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/int2.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 | UInt2& UInt2::operator=(uint8_t value) { |
| 20 | KAI_ASSUME_ALWAYS(value >= 0 && value < 4); |
| 21 | _value = value; |
| 22 | return *this; |
| 23 | } |
| 24 | |
| 25 | UInt2& UInt2::operator=(int value) { |
| 26 | KAI_ASSUME_ALWAYS(value >= 0 && value < 4); |
| 27 | _value = static_cast<uint8_t>(value); |
| 28 | return *this; |
| 29 | } |
| 30 | |
| 31 | UInt2::operator int32_t() const { |
| 32 | return _value; |
| 33 | } |
| 34 | |
| 35 | UInt2::operator float() const { |
| 36 | return _value; |
| 37 | } |
| 38 | |
| 39 | UInt2 UInt2::operator+(UInt2 rhs) const { |
| 40 | return UInt2(_value + rhs._value); |
| 41 | } |
| 42 | |
| 43 | UInt2 UInt2::operator-(UInt2 rhs) const { |
| 44 | return UInt2(_value - rhs._value); |
| 45 | } |
| 46 | |
| 47 | UInt2 UInt2::operator*(UInt2 rhs) const { |
| 48 | return UInt2(_value * rhs._value); |
| 49 | } |
| 50 | |
| 51 | UInt2 UInt2::operator/(UInt2 rhs) const { |
| 52 | return UInt2(_value / rhs._value); |
| 53 | } |
| 54 | |
| 55 | uint8_t UInt2::pack_u8(UInt2 first, UInt2 second, UInt2 third, UInt2 fourth) { |
| 56 | return (first._value & 0x3) | ((second._value << 2) & 0x0C) | ((third._value << 4 & 0x30)) | |
| 57 | ((fourth._value << 6 & 0xC0)); |
| 58 | } |
| 59 | |
| 60 | std::tuple<UInt2, UInt2, UInt2, UInt2> UInt2::unpack_u8(uint8_t value) { |
| 61 | const uint8_t v1 = (value) & 0x03; |
| 62 | const uint8_t v2 = (value >> 2) & 0x03; |
| 63 | const uint8_t v3 = (value >> 4) & 0x03; |
| 64 | const uint8_t v4 = (value >> 6) & 0x03; |
| 65 | |
| 66 | return {UInt2(v1), UInt2(v2), UInt2(v3), UInt2(v4)}; |
| 67 | } |
| 68 | |
| 69 | // ===================================================================================================================== |
| 70 | |
| 71 | Int2& Int2::operator=(int8_t value) { |
| 72 | KAI_ASSUME_ALWAYS(value >= -2 && value < 2); |
| 73 | _value = value; |
| 74 | return *this; |
| 75 | } |
| 76 | |
| 77 | Int2& Int2::operator=(int value) { |
| 78 | KAI_ASSUME_ALWAYS(value >= -2 && value < 2); |
| 79 | _value = static_cast<int8_t>(value); |
| 80 | return *this; |
| 81 | } |
| 82 | |
| 83 | Int2::operator int32_t() const { |
| 84 | return _value; |
| 85 | } |
| 86 | |
| 87 | Int2::operator float() const { |
| 88 | return _value; |
| 89 | } |
| 90 | |
| 91 | Int2 Int2::operator+(Int2 rhs) const { |
| 92 | return Int2(static_cast<int8_t>(_value + rhs._value)); |
| 93 | } |
| 94 | |
| 95 | Int2 Int2::operator-(Int2 rhs) const { |
| 96 | return Int2(static_cast<int8_t>(_value - rhs._value)); |
| 97 | } |
| 98 | |
| 99 | Int2 Int2::operator*(Int2 rhs) const { |
| 100 | return Int2(static_cast<int8_t>(_value * rhs._value)); |
| 101 | } |
| 102 | |
| 103 | Int2 Int2::operator/(Int2 rhs) const { |
| 104 | return Int2(static_cast<int8_t>(_value / rhs._value)); |
| 105 | } |
| 106 | |
| 107 | uint8_t Int2::pack_u8(Int2 first, Int2 second, Int2 third, Int2 fourth) { |
| 108 | const uint8_t v1 = first._value & 0x3; |
| 109 | const uint8_t v2 = second._value & 0x3; |
| 110 | const uint8_t v3 = third._value & 0x3; |
| 111 | const uint8_t v4 = fourth._value & 0x3; |
| 112 | return v1 | (v2 << 2) | (v3 << 4) | (v4 << 6); |
| 113 | } |
| 114 | |
| 115 | std::tuple<Int2, Int2, Int2, Int2> Int2::unpack_u8(uint8_t value) { |
| 116 | // NOLINTBEGIN(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions) |
| 117 | const int8_t v1 = static_cast<int8_t>(value << 6) >> 6; |
| 118 | const int8_t v2 = static_cast<int8_t>(value << 4) >> 6; |
| 119 | const int8_t v3 = static_cast<int8_t>(value << 2) >> 6; |
| 120 | const int8_t v4 = static_cast<int8_t>(value) >> 6; |
| 121 | // NOLINTEND(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions) |
| 122 | |
| 123 | return {Int2(v1), Int2(v2), Int2(v3), Int2(v4)}; |
| 124 | } |
| 125 | |
| 126 | // ===================================================================================================================== |
| 127 | |
| 128 | } // namespace kai::test |