test/reference/fill.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 <functional> | ||
| 10 | #include <type_traits> | ||
| 11 | #include <utility> | ||
| 12 | |||
| 13 | #include "kai/kai_common.h" | ||
| 14 | #include "test/common/buffer.hpp" | ||
| 15 | #include "test/common/memory.hpp" | ||
| 16 | namespace kai::test { | ||
| 17 | |||
| 18 | class DataFormat; | ||
| 19 | |||
| 20 | /// Creates a new matrix filled with random data. | ||
| 21 | /// | ||
| 22 | /// @param[in] height Number of rows. | ||
| 23 | /// @param[in] width Number of columns. | ||
| 24 | /// @param[in] format Data format. | ||
| 25 | /// @param[in] seed Random seed. | ||
| 26 | /// | ||
| 27 | /// @return The data buffer for the matrix. | ||
| 28 | Buffer fill_matrix_random(size_t height, size_t width, const DataFormat& format, uint32_t seed); | ||
| 29 | |||
| 30 | /// Creates a new data buffer filled with random data. | ||
| 31 | /// | ||
| 32 | /// @tparam Value The data type. | ||
| 33 | /// | ||
| 34 | /// @param[in] length The number of elements. | ||
| 35 | /// @param[in] seed The random seed. | ||
| 36 | /// | ||
| 37 | /// @return The data buffer. | ||
| 38 | template <typename Value> | ||
| 39 | Buffer fill_random(size_t length, uint32_t seed); | ||
| 40 | |||
| 41 | /// Creates a new matrix filled with data produced by a generator function. | ||
| 42 | /// | ||
| 43 | /// @tparam T Element type. | ||
| 44 | /// @tparam Generator Callable returning values convertible to `T`. | ||
| 45 | /// | ||
| 46 | /// @param[in] height Number of rows. | ||
| 47 | /// @param[in] width Number of columns. | ||
| 48 | /// @param[in] gen Generator function or functor. | ||
| 49 | /// | ||
| 50 | /// @return The data buffer for the matrix. | ||
| 51 | template <typename T, typename Generator> | ||
| 52 | 146176 | Buffer fill_matrix_raw(size_t height, size_t width, Generator&& gen) { | |
| 53 | − | KAI_ASSUME_ALWAYS(width * size_in_bits<T> % 8 == 0); | |
| 54 | 146176 | const auto row_bytes = width * size_in_bits<T> / 8; | |
| 55 | |||
| 56 | 146176 | Buffer data(height * row_bytes); | |
| 57 |
1/2✓ Branch 0 taken 33720 times.
✗ Branch 1 not taken.
|
146176 | auto* ptr = reinterpret_cast<T*>(data.data()); |
| 58 | 146176 | auto&& generator = std::forward<Generator>(gen); | |
| 59 | |||
| 60 |
12/16✓ Branch 0 taken 2508 times.
✓ Branch 1 taken 4254 times.
✓ Branch 2 taken 3102 times.
✓ Branch 3 taken 14862 times.
✓ Branch 4 taken 36915 times.
✓ Branch 5 taken 233806 times.
✓ Branch 6 taken 2424 times.
✓ Branch 7 taken 2424 times.
✓ Branch 8 taken 240 times.
✓ Branch 9 taken 3888 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✓ Branch 14 taken 100987 times.
✓ Branch 15 taken 352829 times.
|
758239 | for (size_t y = 0; y < height; ++y) { |
| 61 |
12/16✓ Branch 0 taken 4254 times.
✓ Branch 1 taken 10391238 times.
✓ Branch 2 taken 14862 times.
✓ Branch 3 taken 11679612 times.
✓ Branch 4 taken 233806 times.
✓ Branch 5 taken 45321725 times.
✓ Branch 6 taken 2424 times.
✓ Branch 7 taken 11991156 times.
✓ Branch 8 taken 3888 times.
✓ Branch 9 taken 205968 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✓ Branch 14 taken 352829 times.
✓ Branch 15 taken 332649465 times.
|
412851227 | for (size_t x = 0; x < width; ++x) { |
| 62 |
10/28✓ Branch 0 taken 10391238 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10391238 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 45707432 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 45321725 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 14003409 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 2711796 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 205968 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 203676 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✓ Branch 24 taken 332649465 times.
✗ Branch 25 not taken.
✓ Branch 26 taken 332649465 times.
✗ Branch 27 not taken.
|
412239164 | write_array<T>(ptr, y * width + x, generator(y, x)); |
| 63 | 412239164 | } | |
| 64 | 612063 | } | |
| 65 | |||
| 66 | 146176 | return data; | |
| 67 | 146176 | } | |
| 68 | |||
| 69 | /// Convenience overload to maintain the legacy std::function signature. | ||
| 70 | template <typename T> | ||
| 71 | ✗ | Buffer fill_matrix_raw(size_t height, size_t width, std::function<T(size_t, size_t)> gen) { | |
| 72 | // Wrap std::function into a generic generator to prevent self-recursion. | ||
| 73 | ✗ | return fill_matrix_raw<T>(height, width, [&gen](size_t y, size_t x) { return gen(y, x); }); | |
| 74 | } | ||
| 75 | |||
| 76 | /// Creates a new matrix using a generator that returns a Buffer. | ||
| 77 | /// | ||
| 78 | /// @tparam T Element type. | ||
| 79 | /// @tparam Generator Callable returning `Buffer`. | ||
| 80 | /// | ||
| 81 | /// @param[in] height Number of rows. | ||
| 82 | /// @param[in] width Number of columns. | ||
| 83 | /// @param[in] generator Generator instance. | ||
| 84 | /// | ||
| 85 | /// @return The data buffer for the matrix. | ||
| 86 | template <typename Generator> | ||
| 87 | Buffer fill_matrix_generate(size_t height, size_t width, const Generator& generator) { | ||
| 88 | auto buffer = generator(height, width); | ||
| 89 | static_assert(std::is_same_v<std::decay_t<decltype(buffer)>, Buffer>, "Generator must return Buffer"); | ||
| 90 | |||
| 91 | return buffer; | ||
| 92 | } | ||
| 93 | |||
| 94 | } // namespace kai::test | ||
| 95 |