]> git.ipfire.org Git - thirdparty/u-boot.git/commitdiff
compat: math64: add abs_diff()
authorCasey Connolly <casey.connolly@linaro.org>
Wed, 1 Apr 2026 14:15:27 +0000 (16:15 +0200)
committerTom Rini <trini@konsulko.com>
Tue, 21 Apr 2026 17:19:49 +0000 (11:19 -0600)
Add the abs_diff() macro, copied directly from Linux 6.18.

Signed-off-by: Casey Connolly <casey.connolly@linaro.org>
include/linux/math64.h

index eaa9fd5b9685faee28a240c032ead689f8e4e1cc..70a7ee3ff1d303e4554efb8661bdefb67149fd09 100644 (file)
@@ -257,4 +257,23 @@ static inline u64 mul_u64_u32_div(u64 a, u32 mul, u32 divisor)
 }
 #endif /* mul_u64_u32_div */
 
+/**
+ * abs_diff - return absolute value of the difference between the arguments
+ * @a: the first argument
+ * @b: the second argument
+ *
+ * @a and @b have to be of the same type. With this restriction we compare
+ * signed to signed and unsigned to unsigned. The result is the subtraction
+ * the smaller of the two from the bigger, hence result is always a positive
+ * value.
+ *
+ * Return: an absolute value of the difference between the @a and @b.
+ */
+#define abs_diff(a, b) ({                      \
+       typeof(a) __a = (a);                    \
+       typeof(b) __b = (b);                    \
+       (void)(&__a == &__b);                   \
+       __a > __b ? (__a - __b) : (__b - __a);  \
+})
+
 #endif /* _LINUX_MATH64_H */