From 3732f083191184d4e4ff1f6232e12f5018b016ba Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 28 Jul 2023 11:33:38 +0200 Subject: [PATCH] std-aux: add _NM_INT_IS_SIGNED() and _NM_INT_SAME_SIGNEDNESS() macros --- src/libnm-glib-aux/tests/test-shared-general.c | 7 +++++++ src/libnm-std-aux/nm-std-aux.h | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/src/libnm-glib-aux/tests/test-shared-general.c b/src/libnm-glib-aux/tests/test-shared-general.c index 84d87d484..4a2dec0b2 100644 --- a/src/libnm-glib-aux/tests/test-shared-general.c +++ b/src/libnm-glib-aux/tests/test-shared-general.c @@ -32,6 +32,13 @@ G_STATIC_ASSERT(_nm_alignof(NMEtherAddr) <= _nm_alignof(NMIPAddr)); /*****************************************************************************/ +G_STATIC_ASSERT(_NM_INT_IS_SIGNED(1)); +G_STATIC_ASSERT(!_NM_INT_IS_SIGNED(1u)); +G_STATIC_ASSERT(_NM_INT_SAME_SIGNEDNESS((short) 1, 1l)); +G_STATIC_ASSERT(!_NM_INT_SAME_SIGNEDNESS((unsigned short) 1, 1l)); + +/*****************************************************************************/ + static void test_nm_static_assert(void) { diff --git a/src/libnm-std-aux/nm-std-aux.h b/src/libnm-std-aux/nm-std-aux.h index d310305e2..e65f2c417 100644 --- a/src/libnm-std-aux/nm-std-aux.h +++ b/src/libnm-std-aux/nm-std-aux.h @@ -154,6 +154,12 @@ typedef uint64_t _nm_bitwise nm_be64_t; /*****************************************************************************/ +#define _NM_INT_IS_SIGNED(arg) (!(((typeof(arg)) -1) > 0)) + +#define _NM_INT_SAME_SIGNEDNESS(arg1, arg2) (_NM_INT_IS_SIGNED(arg1) == _NM_INT_IS_SIGNED(arg2)) + +/*****************************************************************************/ + #define NM_PASTE_ARGS(identifier1, identifier2) identifier1##identifier2 #define NM_PASTE(identifier1, identifier2) NM_PASTE_ARGS(identifier1, identifier2)