Elffuss BraidHub

kikocis/kleidiai public

main · 1.6 KiBRawPermalink
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
14extern "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.
21int32_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.
28int64_t kai_test_round_to_nearest_even_i64_f32(float value);
29}
30
31namespace kai::test {
32
33int32_t round_to_nearest_even_i32(float value) {
34 return kai_test_round_to_nearest_even_i32_f32(value);
35}
36
37size_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
43template <>
44int32_t round_to_nearest_even(float value) {
45 return round_to_nearest_even_i32(value);
46}
47
48template <>
49size_t round_to_nearest_even(float value) {
50 return round_to_nearest_even_usize(value);
51}
52
53size_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
58size_t round_up_division(size_t a, size_t b) {
59 KAI_ASSUME_ALWAYS(b != 0);
60 return (a + b - 1) / b;
61}
62
63size_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