blob: e70337ba0f8ae63bdfdf6d95dba699d2527c66cb [file] [log] [blame]
Matthew Bentham043613f2023-05-30 16:43:14 +00001/*
2 * Copyright (c) 2017-2018, 2023 Arm Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#ifndef ARM_COMPUTE_UTILS_MATH_H
25#define ARM_COMPUTE_UTILS_MATH_H
26
27namespace arm_compute
28{
29/** Calculate the rounded up quotient of val / m.
30 *
31 * @param[in] val Value to divide and round up.
32 * @param[in] m Value to divide by.
33 *
34 * @return the result.
35 */
36template <typename S, typename T>
37constexpr auto DIV_CEIL(S val, T m) -> decltype((val + m - 1) / m)
38{
39 return (val + m - 1) / m;
40}
41
42/** Computes the smallest number larger or equal to value that is a multiple of divisor.
43 *
44 * @param[in] value Lower bound value
45 * @param[in] divisor Value to compute multiple of.
46 *
47 * @return the result.
48 */
49template <typename S, typename T>
50inline auto ceil_to_multiple(S value, T divisor) -> decltype(((value + divisor - 1) / divisor) * divisor)
51{
52 ARM_COMPUTE_ERROR_ON(value < 0 || divisor <= 0);
53 return DIV_CEIL(value, divisor) * divisor;
54}
55
56/** Computes the largest number smaller or equal to value that is a multiple of divisor.
57 *
58 * @param[in] value Upper bound value
59 * @param[in] divisor Value to compute multiple of.
60 *
61 * @return the result.
62 */
63template <typename S, typename T>
64inline auto floor_to_multiple(S value, T divisor) -> decltype((value / divisor) * divisor)
65{
66 ARM_COMPUTE_ERROR_ON(value < 0 || divisor <= 0);
67 return (value / divisor) * divisor;
68}
69
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010070} // namespace arm_compute
Matthew Bentham043613f2023-05-30 16:43:14 +000071#endif /*ARM_COMPUTE_UTILS_MATH_H */