| 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_ALWAYS(value >= 0 && value < 16); |
| 21 | _value = value; |
| 22 | return *this; |
| 23 | } |
| 24 | |
| 25 | UInt4& UInt4::operator=(int value) { |
| 26 | KAI_ASSUME_ALWAYS(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 | uint8_t UInt4::pack_u8(UInt4 low, UInt4 high) { |
| 56 | return (low._value & 0x0F) | (high._value << 4); |
| 57 | } |
| 58 | |
| 59 | std::tuple<UInt4, UInt4> UInt4::unpack_u8(uint8_t value) { |
| 60 | const uint8_t low = value & 0x0F; |
| 61 | const uint8_t high = value >> 4; |
| 62 | |
| 63 | return {UInt4(low), UInt4(high)}; |
| 64 | } |
| 65 | |
| 66 | // ===================================================================================================================== |
| 67 | |
| 68 | Int4& Int4::operator=(int8_t value) { |
| 69 | KAI_ASSUME_ALWAYS(value >= -8 && value < 8); |
| 70 | _value = value; |
| 71 | return *this; |
| 72 | } |
| 73 | |
| 74 | Int4& Int4::operator=(int value) { |
| 75 | KAI_ASSUME_ALWAYS(value >= -8 && value < 8); |
| 76 | _value = static_cast<int8_t>(value); |
| 77 | return *this; |
| 78 | } |
| 79 | |
| 80 | Int4::operator int32_t() const { |
| 81 | return _value; |
| 82 | } |
| 83 | |
| 84 | Int4::operator float() const { |
| 85 | 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 | uint8_t Int4::pack_u8(Int4 low, Int4 high) { |
| 105 | const uint8_t lo = low._value & 0x0F; |
| 106 | const uint8_t hi = high._value & 0x0F; |
| 107 | return (lo << 0) | (hi << 4); |
| 108 | } |
| 109 | |
| 110 | std::tuple<Int4, Int4> Int4::unpack_u8(uint8_t value) { |
| 111 | // NOLINTBEGIN(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions) |
| 112 | const int8_t low = static_cast<int8_t>(value << 4) >> 4; |
| 113 | const int8_t high = static_cast<int8_t>(value) >> 4; |
| 114 | // NOLINTEND(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions) |
| 115 | |
| 116 | return {Int4(low), Int4(high)}; |
| 117 | } |
| 118 | |
| 119 | // ===================================================================================================================== |
| 120 | |
| 121 | Buffer convert_s0s1_s1s0(const Buffer& src) { |
| 122 | const auto length = src.size(); |
| 123 | Buffer dst(length); |
| 124 | |
| 125 | for (size_t i = 0; i < length; ++i) { |
| 126 | uint8_t val = read_array<uint8_t>(src.data(), i); |
| 127 | const auto [low, high] = UInt4::unpack_u8(val); |
| 128 | auto rev_val = UInt4::pack_u8(high, low); |
| 129 | write_array<uint8_t>(dst.data(), i, rev_val); |
| 130 | } |
| 131 | return dst; |
| 132 | } |
| 133 | |
| 134 | } // namespace kai::test |