]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - include/linux/math.h
New upstream release
[bcachefs-tools-debian] / include / linux / math.h
index 3cf6726d03848fc83675bf0ac0bc2cd58d71b705..85c8c8aa7d453f27b4908324fc7f46061ad78085 100644 (file)
@@ -2,6 +2,11 @@
 #ifndef _LINUX_MATH_H
 #define _LINUX_MATH_H
 
+#include <linux/kernel.h>
+
+/* abs() */
+#include <stdlib.h>
+
 /*
  * This looks more complex than it should be. But we need to
  * get the type for the ~ right in round_down (it needs to be
@@ -148,4 +153,19 @@ static inline u32 int_sqrt64(u64 x)
 }
 #endif
 
+#define abs(x) __abs_choose_expr(x, long long,                         \
+               __abs_choose_expr(x, long,                              \
+               __abs_choose_expr(x, int,                               \
+               __abs_choose_expr(x, short,                             \
+               __abs_choose_expr(x, char,                              \
+               __builtin_choose_expr(                                  \
+                       __builtin_types_compatible_p(typeof(x), char),  \
+                       (char)({ signed char __x = (x); __x<0?-__x:__x; }), \
+                       ((void)0)))))))
+
+#define __abs_choose_expr(x, type, other) __builtin_choose_expr(       \
+       __builtin_types_compatible_p(typeof(x),   signed type) ||       \
+       __builtin_types_compatible_p(typeof(x), unsigned type),         \
+       ({ signed type __x = (x); __x < 0 ? -__x : __x; }), other)
+
 #endif /* _LINUX_MATH_H */