From 46f12960aad2d48fc6742470f718e147e5c85d06 Mon Sep 17 00:00:00 2001 From: Andy Shevchenko Date: Thu, 3 Aug 2023 16:19:18 +0300 Subject: drm/i915: Move abs_diff() to math.h abs_diff() belongs to math.h. Move it there. This will allow others to use it. [andriy.shevchenko@linux.intel.com: add abs_diff() documentation] Link: https://lkml.kernel.org/r/20230804050934.83223-1-andriy.shevchenko@linux.intel.com [akpm@linux-foundation.org: fix comment, per Randy] Link: https://lkml.kernel.org/r/20230803131918.53727-1-andriy.shevchenko@linux.intel.com Signed-off-by: Andy Shevchenko Reviewed-by: Jiri Slaby # tty/serial Acked-by: Jani Nikula Acked-by: Greg Kroah-Hartman Reviewed-by: Andi Shyti Reviewed-by: Philipp Zabel # gpu/ipu-v3 Cc: Alexey Dobriyan Cc: Daniel Vetter Cc: David Airlie Cc: Helge Deller Cc: Imre Deak Cc: Jani Nikula Cc: Joonas Lahtinen Cc: Rasmus Villemoes Cc: Rodrigo Vivi Cc: Tvrtko Ursulin Cc: Randy Dunlap Signed-off-by: Andrew Morton --- include/linux/math.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'include/linux/math.h') diff --git a/include/linux/math.h b/include/linux/math.h index 2d388650c556..dd4152711de7 100644 --- a/include/linux/math.h +++ b/include/linux/math.h @@ -155,6 +155,25 @@ __STRUCT_FRACT(u32) __builtin_types_compatible_p(typeof(x), unsigned type), \ ({ signed type __x = (x); __x < 0 ? -__x : __x; }), other) +/** + * 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); \ +}) + /** * reciprocal_scale - "scale" a value into range [0, ep_ro) * @val: value -- cgit v1.2.3