| 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/round.hpp" |
| 8 | |
| 9 | #include <cstddef> |
| 10 | #include <cstdint> |
| 11 | |
| 12 | #include "kai/kai_common.h" |
| 13 | |
| 14 | extern "C" { |
| 15 | |
| 16 | /// Rounds the specified value to nearest with tie to even. |
| 17 | /// |
| 18 | /// @param[in] value The value to be rounded. |
| 19 | /// |
| 20 | /// @return The rounded value. |
| 21 | int32_t kai_test_round_to_nearest_even_i32_f32(float value); |
| 22 | |
| 23 | /// Rounds the specified value to nearest with tie to even. |
| 24 | /// |
| 25 | /// @param[in] value The value to be rounded. |
| 26 | /// |
| 27 | /// @return The rounded value. |
| 28 | int64_t kai_test_round_to_nearest_even_i64_f32(float value); |
| 29 | } |
| 30 | |
| 31 | namespace kai::test { |
| 32 | |
| 33 | int32_t round_to_nearest_even_i32(float value) { |
| 34 | return kai_test_round_to_nearest_even_i32_f32(value); |
| 35 | } |
| 36 | |
| 37 | size_t round_to_nearest_even_usize(float value) { |
| 38 | static_assert(sizeof(size_t) == sizeof(uint64_t)); |
| 39 | KAI_ASSUME_ALWAYS(value >= 0); |
| 40 | return static_cast<size_t>(kai_test_round_to_nearest_even_i64_f32(value)); |
| 41 | } |
| 42 | |
| 43 | template <> |
| 44 | int32_t round_to_nearest_even(float value) { |
| 45 | return round_to_nearest_even_i32(value); |
| 46 | } |
| 47 | |
| 48 | template <> |
| 49 | size_t round_to_nearest_even(float value) { |
| 50 | return round_to_nearest_even_usize(value); |
| 51 | } |
| 52 | |
| 53 | size_t round_up_multiple(size_t a, size_t b) { |
| 54 | KAI_ASSUME_ALWAYS(b != 0); |
| 55 | return ((a + b - 1) / b) * b; |
| 56 | } |
| 57 | |
| 58 | size_t round_up_division(size_t a, size_t b) { |
| 59 | KAI_ASSUME_ALWAYS(b != 0); |
| 60 | return (a + b - 1) / b; |
| 61 | } |
| 62 | |
| 63 | size_t round_down_multiple(size_t a, size_t b) { |
| 64 | KAI_ASSUME_ALWAYS(b != 0); |
| 65 | return (a / b) * b; |
| 66 | } |
| 67 | |
| 68 | } // namespace kai::test |