blob: ba1bb9c235b8d5865fc0f4102fba9ba5f5e9bf31 [file] [log] [blame]
Manish Pandey65fe3642025-03-21 12:44:42 +00001/*
2 * Copyright The Transfer List Library Contributors
3 *
4 * SPDX-License-Identifier: MIT OR GPL-2.0-or-later
5 */
6
7#ifndef MATH_UTILS_H
8#define MATH_UTILS_H
9
10#include <stdint.h>
11
12/**
13 * @brief Rounds up a given value to the nearest multiple of a boundary.
14 *
15 * @param value The value to round up.
16 * @param boundary The alignment boundary (must be a power of two).
17 * @return The smallest multiple of `boundary` that is greater than or equal to `value`.
18 */
Harrison Mutaibd05e572025-04-16 14:20:35 +000019static inline uintptr_t libtl_align_up(uintptr_t value, uintptr_t boundary)
Manish Pandey65fe3642025-03-21 12:44:42 +000020{
21 return (value + boundary - 1) & ~(boundary - 1);
22}
23
24/**
25 * @brief Checks whether `value` is aligned to the specified `boundary`.
26 *
27 * @param value The value to check.
28 * @param boundary The alignment boundary (should be a power of two for efficiency).
29 * @return `1` if `value` is aligned, `0` otherwise.
30 */
Harrison Mutaibd05e572025-04-16 14:20:35 +000031static inline int libtl_is_aligned(uintptr_t value, uintptr_t boundary)
32{
Manish Pandey65fe3642025-03-21 12:44:42 +000033 return (value % boundary) == 0;
34}
35
36/**
37 * @brief Safely adds `a` and `b`, detecting overflow.
38 *
39 * @param a First operand.
40 * @param b Second operand.
41 * @param res Pointer to store the result if no overflow occurs.
42 * @return `1` if overflow occurs, `0` otherwise.
43 */
Harrison Mutaibd05e572025-04-16 14:20:35 +000044#define libtl_add_overflow(a, b, res) __builtin_add_overflow((a), (b), (res))
Manish Pandey65fe3642025-03-21 12:44:42 +000045
46/**
47 * @brief Rounds up `v` to the nearest multiple of `size`, detecting overflow.
48 *
49 * @param v The value to round up.
50 * @param size The alignment boundary (must be a power of two).
51 * @param res Pointer to store the rounded-up result.
52 * @return `1` if an overflow occurs, `0` otherwise.
53 */
Harrison Mutaibd05e572025-04-16 14:20:35 +000054#define libtl_round_up_overflow(v, size, res) \
Manish Pandey65fe3642025-03-21 12:44:42 +000055 (__extension__({ \
56 typeof(res) __res = res; \
57 typeof(*(__res)) __roundup_tmp = 0; \
58 typeof(v) __roundup_mask = (typeof(v))(size) - 1; \
59 \
Harrison Mutaibd05e572025-04-16 14:20:35 +000060 libtl_add_overflow((v), __roundup_mask, &__roundup_tmp) ? \
Manish Pandey65fe3642025-03-21 12:44:42 +000061 1 : \
62 (void)(*(__res) = __roundup_tmp & ~__roundup_mask), \
63 0; \
64 }))
65
66/**
67 * @brief Adds `a` and `b`, then rounds up the result to the nearest multiple
68 * of `size`, detecting overflow.
69 *
70 * @param a First operand for addition.
71 * @param b Second operand for addition.
72 * @param size The alignment boundary (must be positive).
73 * @param res Pointer to store the final rounded result.
74 * @return `1` if an overflow occurs during addition or rounding, `0` otherwise.
75 */
Harrison Mutaibd05e572025-04-16 14:20:35 +000076#define libtl_add_with_round_up_overflow(a, b, size, res) \
77 (__extension__({ \
78 typeof(a) __a = (a); \
79 typeof(__a) __add_res = 0; \
80 \
81 libtl_add_overflow((__a), (b), &__add_res) ? 1 : \
82 libtl_round_up_overflow(__add_res, (size), (res)) ? 1 : \
83 0; \
Manish Pandey65fe3642025-03-21 12:44:42 +000084 }))
85
86#endif /* MATH_UTILS_H */