From: Andre Muezerie <andremue@linux.microsoft.com>
To: andremue@linux.microsoft.com
Cc: dev@dpdk.org, Chengwen Feng <fengchengwen@huawei.com>
Subject: [PATCH v6 01/10] eal: add workaround for __builtin_constant_p
Date: Mon, 24 Feb 2025 08:24:16 -0800 [thread overview]
Message-ID: <1740414265-12217-2-git-send-email-andremue@linux.microsoft.com> (raw)
In-Reply-To: <1740414265-12217-1-git-send-email-andremue@linux.microsoft.com>
There's no MSVC equivalent for compiler extension __builtin_constant_p,
but the same result can be obtained through a clever expression using
_Generic.
This patch redefines the macro __rte_constant when msvc is used and uses
it as a replacement for __builtin_constant_p.
Signed-off-by: Andre Muezerie <andremue@linux.microsoft.com>
Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
---
lib/eal/include/generic/rte_pause.h | 2 +-
lib/eal/include/rte_common.h | 34 ++++++++++++++++++++++++++++-
2 files changed, 34 insertions(+), 2 deletions(-)
diff --git a/lib/eal/include/generic/rte_pause.h b/lib/eal/include/generic/rte_pause.h
index 968c0886d3..9515caadbb 100644
--- a/lib/eal/include/generic/rte_pause.h
+++ b/lib/eal/include/generic/rte_pause.h
@@ -130,7 +130,7 @@ rte_wait_until_equal_64(volatile uint64_t *addr, uint64_t expected,
* rte_memory_order_acquire and rte_memory_order_relaxed.
*/
#define RTE_WAIT_UNTIL_MASKED(addr, mask, cond, expected, memorder) do { \
- RTE_BUILD_BUG_ON(!__builtin_constant_p(memorder)); \
+ RTE_BUILD_BUG_ON(!__rte_constant(memorder)); \
RTE_BUILD_BUG_ON((memorder) != rte_memory_order_acquire && \
(memorder) != rte_memory_order_relaxed); \
typeof(*(addr)) expected_value = (expected); \
diff --git a/lib/eal/include/rte_common.h b/lib/eal/include/rte_common.h
index 4be65376a5..386f11ae40 100644
--- a/lib/eal/include/rte_common.h
+++ b/lib/eal/include/rte_common.h
@@ -44,8 +44,40 @@ extern "C" {
#endif
#endif
+/*
+ * Macro __rte_constant checks if an expression's value can be determined at
+ * compile time. It takes a single argument, the expression to test, and
+ * returns 1 if the expression is a compile-time constant, and 0 otherwise.
+ * For most compilers it uses built-in function __builtin_constant_p, but for
+ * MSVC it uses a different method because MSVC does not have an equivalent
+ * to __builtin_constant_p.
+ *
+ * The trick used with MSVC relies on the way null pointer constants interact
+ * with the type of a ?: expression:
+ * An integer constant expression with the value 0, or such an expression cast
+ * to type void *, is called a null pointer constant.
+ * If both the second and third operands (of the ?: expression) are pointers or
+ * one is a null pointer constant and the other is a pointer, the result type
+ * is a pointer to a type qualified with all the type qualifiers of the types
+ * referenced by both operands. Furthermore, if both operands are pointers to
+ * compatible types or to differently qualified versions of compatible types,
+ * the result type is a pointer to an appropriately qualified version of the
+ * composite type; if one operand is a null pointer constant, the result has
+ * the type of the other operand; otherwise, one operand is a pointer to void
+ * or a qualified version of void, in which case the result type is a pointer
+ * to an appropriately qualified version of void.
+ *
+ * The _Generic keyword then checks the type of the expression
+ * (void *) ((e) * 0ll). It matches this type against the types listed in the
+ * _Generic construct:
+ * - If the type is int *, the result is 1.
+ * - If the type is void *, the result is 0.
+ *
+ * This explanation with some more details can be found at:
+ * https://stackoverflow.com/questions/49480442/detecting-integer-constant-expressions-in-macros
+ */
#ifdef RTE_TOOLCHAIN_MSVC
-#define __rte_constant(e) 0
+#define __rte_constant(e) _Generic((1 ? (void *) ((e) * 0ll) : (int *) 0), int * : 1, void * : 0)
#else
#define __rte_constant(e) __extension__(__builtin_constant_p(e))
#endif
--
2.48.1.vfs.0.0
next prev parent reply other threads:[~2025-02-24 16:24 UTC|newest]
Thread overview: 112+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-11 22:01 [PATCH 00/10] enable "app" to be compiled with MSVC Andre Muezerie
2025-02-11 22:01 ` [PATCH 01/10] eal: add workaround for __builtin_constant_p Andre Muezerie
2025-02-11 22:09 ` Stephen Hemminger
2025-02-12 0:59 ` fengchengwen
2025-02-11 22:01 ` [PATCH 02/10] test_alarm: avoid warning about different qualifiers Andre Muezerie
2025-02-12 0:59 ` fengchengwen
2025-02-11 22:01 ` [PATCH 03/10] test-pmd: fix printf format string mismatch Andre Muezerie
2025-02-11 22:10 ` Stephen Hemminger
2025-02-12 1:01 ` fengchengwen
2025-02-11 22:02 ` [PATCH 04/10] test-pmd: do explicit 64-bit shift to avoid implicit conversion Andre Muezerie
2025-02-12 1:03 ` fengchengwen
2025-02-11 22:02 ` [PATCH 05/10] test-pmd: avoid undefined behavior Andre Muezerie
2025-02-12 1:04 ` fengchengwen
2025-02-11 22:02 ` [PATCH 06/10] test-pmd: avoid non-constant initializer Andre Muezerie
2025-02-12 1:04 ` fengchengwen
2025-02-11 22:02 ` [PATCH 07/10] test-pmd: don't return value from void function Andre Muezerie
2025-02-12 1:10 ` fengchengwen
2025-02-11 22:02 ` [PATCH 08/10] test-pmd: declare lcore_count atomic when using C11 memory model Andre Muezerie
2025-02-11 22:12 ` Stephen Hemminger
2025-02-12 1:16 ` Andre Muezerie
2025-02-12 1:16 ` fengchengwen
2025-02-11 22:02 ` [PATCH 09/10] test: add workaround for __builtin_constant_p in test_memcpy_perf Andre Muezerie
2025-02-11 22:13 ` Stephen Hemminger
2025-02-12 2:07 ` Andre Muezerie
2025-02-18 15:35 ` [PATCH 00/10] enable "app" to be compiled with MSVC David Marchand
2025-02-18 16:31 ` [PATCH v2 " Andre Muezerie
2025-02-18 16:32 ` [PATCH v2 01/10] eal: add workaround for __builtin_constant_p Andre Muezerie
2025-02-18 17:12 ` Morten Brørup
2025-02-19 16:50 ` Andre Muezerie
2025-02-19 17:10 ` Stephen Hemminger
2025-02-20 1:59 ` Andre Muezerie
2025-02-18 16:32 ` [PATCH v2 02/10] test_alarm: avoid warning about different qualifiers Andre Muezerie
2025-02-18 16:32 ` [PATCH v2 03/10] test-pmd: fix printf format string mismatch Andre Muezerie
2025-02-18 16:46 ` Bruce Richardson
2025-02-18 17:03 ` Andre Muezerie
2025-02-18 17:07 ` Bruce Richardson
2025-02-19 17:08 ` Andre Muezerie
2025-02-18 16:32 ` [PATCH v2 04/10] test-pmd: do explicit 64-bit shift to avoid implicit conversion Andre Muezerie
2025-02-18 16:47 ` Bruce Richardson
2025-02-18 16:32 ` [PATCH v2 05/10] test-pmd: avoid undefined behavior Andre Muezerie
2025-02-18 16:41 ` Bruce Richardson
2025-02-19 17:09 ` Andre Muezerie
2025-02-18 16:32 ` [PATCH v2 06/10] test-pmd: avoid non-constant initializer Andre Muezerie
2025-02-18 16:32 ` [PATCH v2 07/10] test-pmd: don't return value from void function Andre Muezerie
2025-02-18 16:42 ` Bruce Richardson
2025-02-18 16:32 ` [PATCH v2 08/10] test-pmd: declare lcore_count atomic Andre Muezerie
2025-02-18 16:32 ` [PATCH v2 09/10] test: add workaround for __builtin_constant_p in test_memcpy_perf Andre Muezerie
2025-02-18 16:32 ` [PATCH v2 10/10] app: enable app directory to be compiled with MSVC Andre Muezerie
2025-02-18 16:49 ` Bruce Richardson
2025-02-19 9:15 ` David Marchand
2025-02-19 14:51 ` Andre Muezerie
2025-02-20 2:01 ` [PATCH v3 00/10] enable "app" " Andre Muezerie
2025-02-20 2:01 ` [PATCH v3 01/10] eal: add workaround for __builtin_constant_p Andre Muezerie
2025-02-20 2:27 ` Stephen Hemminger
2025-02-20 2:01 ` [PATCH v3 02/10] test_alarm: avoid warning about different qualifiers Andre Muezerie
2025-02-20 2:01 ` [PATCH v3 03/10] test-pmd: fix printf format string mismatch Andre Muezerie
2025-02-20 2:01 ` [PATCH v3 04/10] test-pmd: do explicit 64-bit shift to avoid implicit conversion Andre Muezerie
2025-02-20 2:01 ` [PATCH v3 05/10] test-pmd: avoid undefined behavior Andre Muezerie
2025-02-20 2:01 ` [PATCH v3 06/10] test-pmd: avoid non-constant initializer Andre Muezerie
2025-02-20 2:01 ` [PATCH v3 07/10] test-pmd: don't return value from void function Andre Muezerie
2025-02-20 2:01 ` [PATCH v3 08/10] test-pmd: declare lcore_count atomic Andre Muezerie
2025-02-20 2:01 ` [PATCH v3 09/10] test: add workaround for __builtin_constant_p in test_memcpy_perf Andre Muezerie
2025-02-20 2:01 ` [PATCH v3 10/10] app: enable app directory to be compiled with MSVC Andre Muezerie
2025-02-20 7:57 ` [PATCH v3 00/10] enable "app" " Morten Brørup
2025-02-20 21:30 ` [PATCH v4 " Andre Muezerie
2025-02-20 21:30 ` [PATCH v4 01/10] eal: add workaround for __builtin_constant_p Andre Muezerie
2025-02-21 15:31 ` Morten Brørup
2025-02-21 16:47 ` Andre Muezerie
2025-02-20 21:30 ` [PATCH v4 02/10] test_alarm: avoid warning about different qualifiers Andre Muezerie
2025-02-20 21:30 ` [PATCH v4 03/10] test-pmd: fix printf format string mismatch Andre Muezerie
2025-02-21 9:10 ` Bruce Richardson
2025-02-20 21:30 ` [PATCH v4 04/10] test-pmd: do explicit 64-bit shift to avoid implicit conversion Andre Muezerie
2025-02-20 21:30 ` [PATCH v4 05/10] test-pmd: avoid undefined behavior Andre Muezerie
2025-02-20 21:31 ` [PATCH v4 06/10] test-pmd: avoid non-constant initializer Andre Muezerie
2025-02-20 21:31 ` [PATCH v4 07/10] test-pmd: don't return value from void function Andre Muezerie
2025-02-20 21:31 ` [PATCH v4 08/10] test-pmd: declare lcore_count atomic Andre Muezerie
2025-02-21 8:40 ` Konstantin Ananyev
2025-02-21 19:47 ` Andre Muezerie
2025-02-20 21:31 ` [PATCH v4 09/10] test: add workaround for __builtin_constant_p in test_memcpy_perf Andre Muezerie
2025-02-20 21:31 ` [PATCH v4 10/10] app: enable app directory to be compiled with MSVC Andre Muezerie
2025-02-20 21:44 ` [PATCH v4 00/10] enable "app" " Stephen Hemminger
2025-02-21 19:52 ` [PATCH v5 " Andre Muezerie
2025-02-21 19:52 ` [PATCH v5 01/10] eal: add workaround for __builtin_constant_p Andre Muezerie
2025-02-21 19:52 ` [PATCH v5 02/10] test_alarm: avoid warning about different qualifiers Andre Muezerie
2025-02-21 19:52 ` [PATCH v5 03/10] test-pmd: fix printf format string mismatch Andre Muezerie
2025-02-21 19:52 ` [PATCH v5 04/10] test-pmd: do explicit 64-bit shift to avoid implicit conversion Andre Muezerie
2025-02-21 19:52 ` [PATCH v5 05/10] test-pmd: avoid undefined behavior Andre Muezerie
2025-02-21 19:52 ` [PATCH v5 06/10] test-pmd: avoid non-constant initializer Andre Muezerie
2025-02-21 19:52 ` [PATCH v5 07/10] test-pmd: don't return value from void function Andre Muezerie
2025-02-21 19:52 ` [PATCH v5 08/10] test-pmd: declare lcore_count atomic Andre Muezerie
2025-02-21 20:58 ` Morten Brørup
2025-02-24 16:22 ` Andre Muezerie
2025-02-24 10:04 ` Konstantin Ananyev
2025-02-21 19:52 ` [PATCH v5 09/10] test: add workaround for __builtin_constant_p in test_memcpy_perf Andre Muezerie
2025-02-22 2:13 ` fengchengwen
2025-02-21 19:52 ` [PATCH v5 10/10] app: enable app directory to be compiled with MSVC Andre Muezerie
2025-02-22 2:13 ` fengchengwen
2025-02-24 16:24 ` [PATCH v6 00/10] enable "app" " Andre Muezerie
2025-02-24 16:24 ` Andre Muezerie [this message]
2025-02-24 16:24 ` [PATCH v6 02/10] test_alarm: avoid warning about different qualifiers Andre Muezerie
2025-02-24 16:24 ` [PATCH v6 03/10] test-pmd: fix printf format string mismatch Andre Muezerie
2025-02-24 16:24 ` [PATCH v6 04/10] test-pmd: do explicit 64-bit shift to avoid implicit conversion Andre Muezerie
2025-02-24 16:24 ` [PATCH v6 05/10] test-pmd: avoid undefined behavior Andre Muezerie
2025-04-10 16:05 ` David Marchand
2025-04-11 0:06 ` Andre Muezerie
2025-02-24 16:24 ` [PATCH v6 06/10] test-pmd: avoid non-constant initializer Andre Muezerie
2025-02-24 16:24 ` [PATCH v6 07/10] test-pmd: don't return value from void function Andre Muezerie
2025-02-24 16:24 ` [PATCH v6 08/10] test/test-pmd: declare lcore_count atomic Andre Muezerie
2025-02-24 16:24 ` [PATCH v6 09/10] test: add workaround for __builtin_constant_p in test_memcpy_perf Andre Muezerie
2025-02-24 16:24 ` [PATCH v6 10/10] app: enable app directory to be compiled with MSVC Andre Muezerie
2025-04-11 6:49 ` [PATCH v6 00/10] enable "app" " David Marchand
2025-04-12 0:04 ` Andre Muezerie
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1740414265-12217-2-git-send-email-andremue@linux.microsoft.com \
--to=andremue@linux.microsoft.com \
--cc=dev@dpdk.org \
--cc=fengchengwen@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.