| 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/reference/transpose.hpp" |
| 8 | |
| 9 | #include <cstddef> |
| 10 | #include <cstdint> |
| 11 | #include <cstring> |
| 12 | |
| 13 | #include "kai/kai_common.h" |
| 14 | #include "test/common/buffer.hpp" |
| 15 | #include "test/common/data_type.hpp" |
| 16 | #include "test/common/memory.hpp" |
| 17 | #include "test/common/round.hpp" |
| 18 | |
| 19 | namespace kai::test { |
| 20 | |
| 21 | Buffer transpose(const void* data, DataType data_type, size_t height, size_t width) { |
| 22 | KAI_ASSUME_ALWAYS(data_type_size_in_bits(data_type) % 8 == 0); |
| 23 | const auto element_size = data_type_size_in_bits(data_type) / 8; |
| 24 | |
| 25 | Buffer output(height * width * element_size); |
| 26 | |
| 27 | const auto* src_ptr = reinterpret_cast<const uint8_t*>(data); |
| 28 | |
| 29 | for (size_t y = 0; y < width; ++y) { |
| 30 | for (size_t x = 0; x < height; ++x) { |
| 31 | memcpy( |
| 32 | output.data() + (y * height + x) * element_size, src_ptr + (x * width + y) * element_size, |
| 33 | element_size); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | return output; |
| 38 | } |
| 39 | |
| 40 | template <typename T> |
| 41 | Buffer transpose_with_padding( |
| 42 | const void* data, const size_t height, const size_t width, const size_t src_stride, const size_t dst_stride, |
| 43 | const size_t dst_size) { |
| 44 | Buffer output(dst_size); |
| 45 | |
| 46 | for (size_t y = 0; y < width; ++y) { |
| 47 | for (size_t x = 0; x < height; ++x) { |
| 48 | auto element = read_array<T>(data, (x * src_stride) + y); |
| 49 | write_array<T>(output.data(), (y * dst_stride) + x, element); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | return output; |
| 54 | } |
| 55 | |
| 56 | template Buffer transpose_with_padding<Int4>( |
| 57 | const void* data, const size_t height, const size_t width, const size_t src_stride, const size_t dst_stride, |
| 58 | const size_t dst_size); |
| 59 | |
| 60 | template Buffer transpose_with_padding<int8_t>( |
| 61 | const void* data, const size_t height, const size_t width, const size_t src_stride, const size_t dst_stride, |
| 62 | const size_t dst_size); |
| 63 | |
| 64 | template <typename T> |
| 65 | Buffer transpose(const void* src, size_t height, size_t width) { |
| 66 | Buffer dst(round_up_division(height * width * size_in_bits<T>, 8)); |
| 67 | |
| 68 | for (size_t y = 0; y < width; ++y) { |
| 69 | for (size_t x = 0; x < height; ++x) { |
| 70 | write_array<T>(dst.data(), y * height + x, read_array<T>(src, x * width + y)); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return dst; |
| 75 | } |
| 76 | |
| 77 | template Buffer transpose<float>(const void* src, size_t height, size_t width); |
| 78 | template Buffer transpose<int8_t>(const void* src, size_t height, size_t width); |
| 79 | |
| 80 | } // namespace kai::test |