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