Manish Pandey | 65fe364 | 2025-03-21 12:44:42 +0000 | [diff] [blame] | 1 | /* |
| 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 Mutai | bd05e57 | 2025-04-16 14:20:35 +0000 | [diff] [blame] | 19 | static inline uintptr_t libtl_align_up(uintptr_t value, uintptr_t boundary) |
Manish Pandey | 65fe364 | 2025-03-21 12:44:42 +0000 | [diff] [blame] | 20 | { |
| 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 Mutai | bd05e57 | 2025-04-16 14:20:35 +0000 | [diff] [blame] | 31 | static inline int libtl_is_aligned(uintptr_t value, uintptr_t boundary) |
| 32 | { |
Manish Pandey | 65fe364 | 2025-03-21 12:44:42 +0000 | [diff] [blame] | 33 | 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 Mutai | bd05e57 | 2025-04-16 14:20:35 +0000 | [diff] [blame] | 44 | #define libtl_add_overflow(a, b, res) __builtin_add_overflow((a), (b), (res)) |
Manish Pandey | 65fe364 | 2025-03-21 12:44:42 +0000 | [diff] [blame] | 45 | |
| 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 Mutai | bd05e57 | 2025-04-16 14:20:35 +0000 | [diff] [blame] | 54 | #define libtl_round_up_overflow(v, size, res) \ |
Manish Pandey | 65fe364 | 2025-03-21 12:44:42 +0000 | [diff] [blame] | 55 | (__extension__({ \ |
| 56 | typeof(res) __res = res; \ |
| 57 | typeof(*(__res)) __roundup_tmp = 0; \ |
| 58 | typeof(v) __roundup_mask = (typeof(v))(size) - 1; \ |
| 59 | \ |
Harrison Mutai | bd05e57 | 2025-04-16 14:20:35 +0000 | [diff] [blame] | 60 | libtl_add_overflow((v), __roundup_mask, &__roundup_tmp) ? \ |
Manish Pandey | 65fe364 | 2025-03-21 12:44:42 +0000 | [diff] [blame] | 61 | 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 Mutai | bd05e57 | 2025-04-16 14:20:35 +0000 | [diff] [blame] | 76 | #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 Pandey | 65fe364 | 2025-03-21 12:44:42 +0000 | [diff] [blame] | 84 | })) |
| 85 | |
| 86 | #endif /* MATH_UTILS_H */ |