| 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 | #pragma once |
| 8 | |
| 9 | #include <cstdint> |
| 10 | #include <tuple> |
| 11 | |
| 12 | #include "test/common/buffer.hpp" |
| 13 | |
| 14 | namespace kai::test { |
| 15 | |
| 16 | /// 2-bit unsigned integer. |
| 17 | class UInt2 { |
| 18 | public: |
| 19 | /// Creates a new 2-bit unsigned integer value. |
| 20 | /// |
| 21 | /// @param[in] value Value. |
| 22 | constexpr explicit UInt2(uint8_t value) : _value(value) { |
| 23 | } |
| 24 | |
| 25 | /// Assignment operator. |
| 26 | UInt2& operator=(uint8_t value); |
| 27 | |
| 28 | /// Assignment operator. |
| 29 | UInt2& operator=(int value); |
| 30 | |
| 31 | /// Conversion operator. |
| 32 | operator int32_t() const; |
| 33 | |
| 34 | /// Conversion operator. |
| 35 | operator float() const; |
| 36 | |
| 37 | /// Addition operator. |
| 38 | [[nodiscard]] UInt2 operator+(UInt2 rhs) const; |
| 39 | |
| 40 | /// Subtraction operator. |
| 41 | [[nodiscard]] UInt2 operator-(UInt2 rhs) const; |
| 42 | |
| 43 | /// Multiplication operator. |
| 44 | [[nodiscard]] UInt2 operator*(UInt2 rhs) const; |
| 45 | |
| 46 | /// Division operator. |
| 47 | [[nodiscard]] UInt2 operator/(UInt2 rhs) const; |
| 48 | |
| 49 | /// Packs four 2-bit unsigned integer values into one byte. |
| 50 | /// |
| 51 | /// @param[in] first First 2-bit value (low). |
| 52 | /// @param[in] second Second 2-bit value. |
| 53 | /// @param[in] third Third 2-bit value. |
| 54 | /// @param[in] fourth Fourth 2-bit value (high). |
| 55 | /// |
| 56 | /// @return The packed byte. |
| 57 | [[nodiscard]] static uint8_t pack_u8(UInt2 first, UInt2 second, UInt2 third, UInt2 fourth); |
| 58 | |
| 59 | /// Unpacks one byte to four 2-bit unsigned integer values. |
| 60 | /// |
| 61 | /// @param[in] value 8-bit packed value. |
| 62 | /// |
| 63 | /// @return The four 2-bit values. |
| 64 | [[nodiscard]] static std::tuple<UInt2, UInt2, UInt2, UInt2> unpack_u8(uint8_t value); |
| 65 | |
| 66 | private: |
| 67 | uint8_t _value; |
| 68 | }; |
| 69 | |
| 70 | /// 2-bit signed integer. |
| 71 | class Int2 { |
| 72 | public: |
| 73 | /// Creates a new 2-bit signed integer value. |
| 74 | /// |
| 75 | /// @param[in] value Value. |
| 76 | constexpr explicit Int2(int8_t value) : _value(value) { |
| 77 | } |
| 78 | |
| 79 | /// Assignment operator. |
| 80 | Int2& operator=(int8_t value); |
| 81 | |
| 82 | /// Assignment operator. |
| 83 | Int2& operator=(int value); |
| 84 | |
| 85 | /// Conversion operator. |
| 86 | operator int32_t() const; |
| 87 | |
| 88 | /// Conversion operator. |
| 89 | operator float() const; |
| 90 | |
| 91 | /// Addition operator. |
| 92 | [[nodiscard]] Int2 operator+(Int2 rhs) const; |
| 93 | |
| 94 | /// Subtraction operator. |
| 95 | [[nodiscard]] Int2 operator-(Int2 rhs) const; |
| 96 | |
| 97 | /// Multiplication operator. |
| 98 | [[nodiscard]] Int2 operator*(Int2 rhs) const; |
| 99 | |
| 100 | /// Division operator. |
| 101 | [[nodiscard]] Int2 operator/(Int2 rhs) const; |
| 102 | |
| 103 | /// Packs four 2-bit signed integer values into one byte. |
| 104 | /// |
| 105 | /// @param[in] first First 2-bit value (low). |
| 106 | /// @param[in] second Second 2-bit value. |
| 107 | /// @param[in] third Third 2-bit value. |
| 108 | /// @param[in] fourth Fourth 2-bit value (high). |
| 109 | /// |
| 110 | /// @return The packed byte. |
| 111 | [[nodiscard]] static uint8_t pack_u8(Int2 first, Int2 second, Int2 third, Int2 fourth); |
| 112 | |
| 113 | /// Unpacks one byte to four 2-bit signed integer values. |
| 114 | /// |
| 115 | /// @param[in] value 8-bit packed value. |
| 116 | /// |
| 117 | /// @return The four 2-bit values. |
| 118 | [[nodiscard]] static std::tuple<Int2, Int2, Int2, Int2> unpack_u8(uint8_t value); |
| 119 | |
| 120 | private: |
| 121 | int8_t _value; |
| 122 | }; |
| 123 | |
| 124 | } // namespace kai::test |