From: Mike Yuan Date: Tue, 6 May 2025 12:41:17 +0000 (+0200) Subject: macro: implement ABS via __builtin_imaxabs() X-Git-Tag: v258-rc1~684 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c0239e5f113335f49328286698d4a9f13fed983d;p=thirdparty%2Fsystemd.git macro: implement ABS via __builtin_imaxabs() --- diff --git a/src/basic/errno-util.h b/src/basic/errno-util.h index 10118c6901b..e7dcad3b448 100644 --- a/src/basic/errno-util.h +++ b/src/basic/errno-util.h @@ -95,6 +95,7 @@ static inline int errno_or_else(int fallback) { return -ABS(fallback); } +/* abs(3) says: Trying to take the absolute value of the most negative integer is not defined. */ #define _DEFINE_ABS_WRAPPER(name) \ static inline bool ERRNO_IS_##name(intmax_t r) { \ if (r == INTMAX_MIN) \ diff --git a/src/fundamental/macro-fundamental.h b/src/fundamental/macro-fundamental.h index 417a7853d4c..28fef06c993 100644 --- a/src/fundamental/macro-fundamental.h +++ b/src/fundamental/macro-fundamental.h @@ -176,12 +176,12 @@ UNIQ_T(A, aq) > UNIQ_T(B, bq) ? UNIQ_T(A, aq) : UNIQ_T(B, bq); \ }) -#define ABS(a) __ABS(UNIQ, (a)) -#define __ABS(aq, a) \ -({ \ - const typeof(a) UNIQ_T(A, aq) = (a); \ - UNIQ_T(A, aq) < 0 ? -UNIQ_T(A, aq) : UNIQ_T(A, aq); \ -}) +#ifdef __clang__ +# define ABS(a) __builtin_llabs(a) +#else +# define ABS(a) __builtin_imaxabs(a) +#endif +assert_cc(sizeof(intmax_t) <= sizeof(long long)); #define IS_UNSIGNED_INTEGER_TYPE(type) \ (__builtin_types_compatible_p(typeof(type), unsigned char) || \