macro: implement ABS via __builtin_imaxabs()

This commit is contained in:
Mike Yuan
2025-05-06 17:39:01 +02:00
committed by Daan De Meyer
parent e9f9ddbfa8
commit c0239e5f11
2 changed files with 7 additions and 6 deletions
+1
View File
@@ -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) \
+6 -6
View File
@@ -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) || \