| 1 | // |
| 2 | // SPDX-FileCopyrightText: Copyright 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 <cmath> |
| 10 | #include <cstdint> |
| 11 | |
| 12 | namespace kai::test { |
| 13 | |
| 14 | /// Rounding mode. |
| 15 | enum class RoundMode : uint8_t { |
| 16 | CURRENT, ///< Using the current rounding mode from fegetround. |
| 17 | TIE_AWAY, ///< Rounding to the nearest with halfway rounded away from zero. |
| 18 | }; |
| 19 | |
| 20 | /// Rounds the value using the specified rounding mode. |
| 21 | template <typename T, RoundMode MODE> |
| 22 | [[nodiscard]] T round(T value) { |
| 23 | if constexpr (MODE == RoundMode::CURRENT) { |
| 24 | return nearbyint(value); |
| 25 | } else { |
| 26 | static_assert(MODE == RoundMode::TIE_AWAY); |
| 27 | return std::round(value); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | } // namespace kai::test |