]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
include/c: Add abs_diff macro
authorSean Anderson <seanga2@gmail.com>
Wed, 24 Nov 2021 18:26:13 +0000 (13:26 -0500)
committerKarel Zak <kzak@redhat.com>
Wed, 1 Dec 2021 12:35:59 +0000 (13:35 +0100)
This macro calculates abs(a - b). It is especially useful for unsigned
numbers.

Signed-off-by: Sean Anderson <seanga2@gmail.com>
include/c.h

index 354b59e299cddc120eff24e16bfce2f02e4e4b31..ba799d8fd344e92fb47d1ef2190351652c77ca4c 100644 (file)
        _max1 > _max2 ? _max1 : _max2; })
 #endif
 
+#ifndef abs_diff
+# define abs_diff(x, y) __extension__ ({        \
+       __typeof__(x) _a = (x);                 \
+       __typeof__(y) _b = (y);                 \
+       (void) (&_a == &_b);                    \
+       _a > _b ? _a - _b : _b - _a; })
+#endif
+
 #ifndef cmp_numbers
 # define cmp_numbers(x, y) __extension__ ({    \
        __typeof__(x) _a = (x);                 \