| 1 | // |
| 2 | // SPDX-FileCopyrightText: Copyright 2024 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 <cstddef> |
| 10 | #include <cstdint> |
| 11 | |
| 12 | namespace kai::test { |
| 13 | |
| 14 | /// Rounds the specified value to nearest with tie to even. |
| 15 | /// |
| 16 | /// For example: |
| 17 | /// |
| 18 | /// * 0.4 is rounded to 0. |
| 19 | /// * 0.5 is rounded to 0 (as 0 is the nearest even value). |
| 20 | /// * 0.6 is rounded to 1. |
| 21 | /// * 1.4 is rounded to 1. |
| 22 | /// * 1.5 is rounded to 2 (as 2 is the nearest even value). |
| 23 | /// * 1.6 is rounded to 2. |
| 24 | /// |
| 25 | /// @param[in] value Value to be rounded. |
| 26 | /// |
| 27 | /// @return The rounded value. |
| 28 | int32_t round_to_nearest_even_i32(float value); |
| 29 | |
| 30 | /// Rounds the specified value to nearest with tie to even. |
| 31 | /// |
| 32 | /// For example: |
| 33 | /// |
| 34 | /// * 0.4 is rounded to 0. |
| 35 | /// * 0.5 is rounded to 0 (as 0 is the nearest even value). |
| 36 | /// * 0.6 is rounded to 1. |
| 37 | /// * 1.4 is rounded to 1. |
| 38 | /// * 1.5 is rounded to 2 (as 2 is the nearest even value). |
| 39 | /// * 1.6 is rounded to 2. |
| 40 | /// |
| 41 | /// @param[in] value Value to be rounded. |
| 42 | /// |
| 43 | /// @return The rounded value. |
| 44 | size_t round_to_nearest_even_usize(float value); |
| 45 | |
| 46 | /// Rounds the specified value to nearest with tie to even. |
| 47 | /// |
| 48 | /// For example: |
| 49 | /// |
| 50 | /// * 0.4 is rounded to 0. |
| 51 | /// * 0.5 is rounded to 0 (as 0 is the nearest even value). |
| 52 | /// * 0.6 is rounded to 1. |
| 53 | /// * 1.4 is rounded to 1. |
| 54 | /// * 1.5 is rounded to 2 (as 2 is the nearest even value). |
| 55 | /// * 1.6 is rounded to 2. |
| 56 | /// |
| 57 | /// @tparam T The target data type (must be integer). |
| 58 | /// |
| 59 | /// @param[in] value Value to be rounded. |
| 60 | /// |
| 61 | /// @return The rounded value. |
| 62 | template <typename T> |
| 63 | T round_to_nearest_even(float value); |
| 64 | |
| 65 | /// Rounds up the input value to the multiple of the unit value. |
| 66 | /// |
| 67 | /// @param[in] a Input value. |
| 68 | /// @param[in] b Unit value. |
| 69 | /// |
| 70 | /// @return The rounded value. |
| 71 | size_t round_up_multiple(size_t a, size_t b); |
| 72 | |
| 73 | /// Divides and rounds up. |
| 74 | /// |
| 75 | /// @param[in] a The dividend. |
| 76 | /// @param[in] b The divisor. |
| 77 | /// |
| 78 | /// @return The division of a to b rounding up. |
| 79 | size_t round_up_division(size_t a, size_t b); |
| 80 | |
| 81 | /// Rounds down the input value to the multiple of the unit value. |
| 82 | /// |
| 83 | /// @param[in] a Input value. |
| 84 | /// @param[in] b Unit value. |
| 85 | /// |
| 86 | /// @return The rounded value. |
| 87 | size_t round_down_multiple(size_t a, size_t b); |
| 88 | |
| 89 | } // namespace kai::test |