Elffuss BraidHub

kikocis/kleidiai public

main · 2.9 KiBRawPermalink
1//
2// SPDX-FileCopyrightText: Copyright 2024-2026 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/pad.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
19namespace kai::test {
20
21template <typename T>
22Buffer pad_row(
23 const void* data, const size_t height, const size_t width, const size_t src_stride, const size_t dst_stride,
24 const size_t dst_size, const uint8_t val) {
25 Buffer output(dst_size, val);
26
27 for (size_t y = 0; y < height; ++y) {
28 for (size_t x = 0; x < width; ++x) {
29 auto element = read_array<T>(data, (y * src_stride) + x);
30 write_array<T>(output.data(), (y * dst_stride) + x, element);
31 }
32 }
33 return output;
34}
35template Buffer pad_row<Int4>(
36 const void* data, const size_t height, const size_t width, const size_t src_stride, const size_t dst_stride,
37 const size_t dst_size, const uint8_t val);
38
39template Buffer pad_row<UInt4>(
40 const void* data, const size_t height, const size_t width, const size_t src_stride, const size_t dst_stride,
41 const size_t dst_size, const uint8_t val);
42
43template Buffer pad_row<UInt2>(
44 const void* data, const size_t height, const size_t width, const size_t src_stride, const size_t dst_stride,
45 const size_t dst_size, const uint8_t val);
46
47template <typename T>
48Buffer pad_matrix(
49 const void* data, size_t height, size_t width, size_t pad_left, size_t pad_top, size_t pad_right, size_t pad_bottom,
50 T pad_value) {
51 const size_t dst_height = height + pad_top + pad_bottom;
52 const size_t dst_width = width + pad_left + pad_right;
53 const size_t dst_size = round_up_multiple(dst_height * dst_width * size_in_bits<T>, 8);
54
55 Buffer dst(dst_size);
56
57 for (size_t row = 0; row < dst_height; ++row) {
58 for (size_t col = 0; col < dst_width; ++col) {
59 const bool valid_row = row >= pad_top && row < pad_top + height;
60 const bool valid_col = col >= pad_left && col < pad_left + width;
61 if (valid_row && valid_col) {
62 const T value = read_array<T>(data, (row - pad_top) * width + col - pad_left);
63 write_array<T>(dst.data(), row * dst_width + col, value);
64 } else {
65 write_array<T>(dst.data(), row * dst_width + col, pad_value);
66 }
67 }
68 }
69
70 return dst;
71}
72
73template Buffer pad_matrix(
74 const void* data, size_t height, size_t width, size_t pad_left, size_t pad_top, size_t pad_right, size_t pad_bottom,
75 float pad_value);
76template Buffer pad_matrix(
77 const void* data, size_t height, size_t width, size_t pad_left, size_t pad_top, size_t pad_right, size_t pad_bottom,
78 int32_t pad_value);
79
80} // namespace kai::test