test/common/int4.cpp
| 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_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 | 3816663 | UInt4::operator int32_t() const { | |
| 32 | 3816663 | return _value; | |
| 33 | } | ||
| 34 | |||
| 35 | 1272221 | UInt4::operator float() const { | |
| 36 | 1272221 | return _value; | |
| 37 | } | ||
| 38 | |||
| 39 | 1272221 | UInt4 UInt4::operator+(UInt4 rhs) const { | |
| 40 | 1272221 | 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 | 320687818 | uint8_t UInt4::pack_u8(UInt4 low, UInt4 high) { | |
| 56 | 320687818 | return (low._value & 0x0F) | (high._value << 4); | |
| 57 | } | ||
| 58 | |||
| 59 | 389807521 | std::tuple<UInt4, UInt4> UInt4::unpack_u8(uint8_t value) { | |
| 60 | 389807521 | const uint8_t low = value & 0x0F; | |
| 61 | 389807521 | const uint8_t high = value >> 4; | |
| 62 | |||
| 63 | 389807521 | return {UInt4(low), UInt4(high)}; | |
| 64 | 389807521 | } | |
| 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 | 2318517716 | Int4::operator int32_t() const { | |
| 81 | 2318517716 | return _value; | |
| 82 | } | ||
| 83 | |||
| 84 | 5159859128 | Int4::operator float() const { | |
| 85 | 5159859128 | 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 | 252637969 | uint8_t Int4::pack_u8(Int4 low, Int4 high) { | |
| 105 | 252637969 | const uint8_t lo = low._value & 0x0F; | |
| 106 | 252637969 | const uint8_t hi = high._value & 0x0F; | |
| 107 | 505275938 | return (lo << 0) | (hi << 4); | |
| 108 | 252637969 | } | |
| 109 | |||
| 110 | 7427627254 | std::tuple<Int4, Int4> Int4::unpack_u8(uint8_t value) { | |
| 111 | // NOLINTBEGIN(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions) | ||
| 112 | 7427627254 | const int8_t low = static_cast<int8_t>(value << 4) >> 4; | |
| 113 | 7427627254 | const int8_t high = static_cast<int8_t>(value) >> 4; | |
| 114 | // NOLINTEND(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions) | ||
| 115 | |||
| 116 | 7427627254 | return {Int4(low), Int4(high)}; | |
| 117 | 7427627254 | } | |
| 118 | |||
| 119 | // ===================================================================================================================== | ||
| 120 | |||
| 121 | 21420 | Buffer convert_s0s1_s1s0(const Buffer& src) { | |
| 122 | 21420 | const auto length = src.size(); | |
| 123 | 21420 | Buffer dst(length); | |
| 124 | |||
| 125 |
2/2✓ Branch 0 taken 44432640 times.
✓ Branch 1 taken 21420 times.
|
44454060 | for (size_t i = 0; i < length; ++i) { |
| 126 |
2/4✓ Branch 0 taken 44432640 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 44432640 times.
✗ Branch 3 not taken.
|
44432640 | uint8_t val = read_array<uint8_t>(src.data(), i); |
| 127 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 44432640 times.
|
44432640 | const auto [low, high] = UInt4::unpack_u8(val); |
| 128 | 133297920 | auto rev_val = UInt4::pack_u8(high, low); | |
| 129 |
2/4✓ Branch 0 taken 44432640 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 44432640 times.
✗ Branch 3 not taken.
|
44432640 | write_array<uint8_t>(dst.data(), i, rev_val); |
| 130 | 44432640 | } | |
| 131 | 21420 | return dst; | |
| 132 | 21420 | } | |
| 133 | |||
| 134 | } // namespace kai::test | ||
| 135 |