Initial commit

Change-Id: I26f6fdfd4962e2c724bf6b68893156f11d37d4b1
diff --git a/include/math_utils.h b/include/math_utils.h
new file mode 100644
index 0000000..6966287
--- /dev/null
+++ b/include/math_utils.h
@@ -0,0 +1,85 @@
+/*
+ * Copyright The Transfer List Library Contributors
+ *
+ * SPDX-License-Identifier: MIT OR GPL-2.0-or-later
+ */
+
+#ifndef MATH_UTILS_H
+#define MATH_UTILS_H
+
+#include <stdint.h>
+
+/**
+ * @brief Rounds up a given value to the nearest multiple of a boundary.
+ *
+ * @param value The value to round up.
+ * @param boundary The alignment boundary (must be a power of two).
+ * @return The smallest multiple of `boundary` that is greater than or equal to `value`.
+ */
+static inline uintptr_t align_up(uintptr_t value, uintptr_t boundary)
+{
+	return (value + boundary - 1) & ~(boundary - 1);
+}
+
+/**
+ * @brief Checks whether `value` is aligned to the specified `boundary`.
+ *
+ * @param value The value to check.
+ * @param boundary The alignment boundary (should be a power of two for efficiency).
+ * @return `1` if `value` is aligned, `0` otherwise.
+ */
+static inline int is_aligned(uintptr_t value, uintptr_t boundary) {
+	return (value % boundary) == 0;
+}
+
+/**
+ * @brief Safely adds `a` and `b`, detecting overflow.
+ *
+ * @param a First operand.
+ * @param b Second operand.
+ * @param res Pointer to store the result if no overflow occurs.
+ * @return `1` if overflow occurs, `0` otherwise.
+ */
+#define add_overflow(a, b, res) __builtin_add_overflow((a), (b), (res))
+
+/**
+ * @brief Rounds up `v` to the nearest multiple of `size`, detecting overflow.
+ *
+ * @param v The value to round up.
+ * @param size The alignment boundary (must be a power of two).
+ * @param res Pointer to store the rounded-up result.
+ * @return `1` if an overflow occurs, `0` otherwise.
+ */
+#define round_up_overflow(v, size, res)                                     \
+	(__extension__({                                                    \
+		typeof(res) __res = res;                                    \
+		typeof(*(__res)) __roundup_tmp = 0;                         \
+		typeof(v) __roundup_mask = (typeof(v))(size) - 1;           \
+                                                                            \
+		add_overflow((v), __roundup_mask, &__roundup_tmp) ?         \
+			1 :                                                 \
+			(void)(*(__res) = __roundup_tmp & ~__roundup_mask), \
+			0;                                                  \
+	}))
+
+/**
+  * @brief Adds `a` and `b`, then rounds up the result to the nearest multiple
+  * of `size`, detecting overflow.
+  *
+  * @param a First operand for addition.
+  * @param b Second operand for addition.
+  * @param size The alignment boundary (must be positive).
+  * @param res Pointer to store the final rounded result.
+  * @return `1` if an overflow occurs during addition or rounding, `0` otherwise.
+  */
+#define add_with_round_up_overflow(a, b, size, res)               \
+	(__extension__({                                          \
+		typeof(a) __a = (a);                              \
+		typeof(__a) __add_res = 0;                        \
+                                                                  \
+		add_overflow((__a), (b), &__add_res)	    ? 1 : \
+		round_up_overflow(__add_res, (size), (res)) ? 1 : \
+							      0;  \
+	}))
+
+#endif /* MATH_UTILS_H */