[v5,07/39] net: use C11 alignas

Message ID 1708715054-22386-8-git-send-email-roretzla@linux.microsoft.com (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers
Series use C11 alignas |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Tyler Retzlaff Feb. 23, 2024, 7:03 p.m. UTC
  The current location used for __rte_aligned(a) for alignment of types
and variables is not compatible with MSVC. There is only a single
location accepted by both toolchains.

For variables standard C11 offers alignas(a) supported by conformant
compilers i.e. both MSVC and GCC.

For types the standard offers no alignment facility that compatibly
interoperates with C and C++ but may be achieved by relocating the
placement of __rte_aligned(a) to the aforementioned location accepted
by all currently supported toolchains.

To allow alignment for both compilers do the following:

* Move __rte_aligned from the end of {struct,union} definitions to
  be between {struct,union} and tag.

  The placement between {struct,union} and the tag allows the desired
  alignment to be imparted on the type regardless of the toolchain being
  used for all of GCC, LLVM, MSVC compilers building both C and C++.

* Replace use of __rte_aligned(a) on variables/fields with alignas(a).

Signed-off-by: Tyler Retzlaff <roretzla@linux.microsoft.com>
Acked-by: Morten Brørup <mb@smartsharesystems.com>
---
 lib/net/net_crc_avx512.c | 14 ++++++++------
 lib/net/net_crc_neon.c   | 11 ++++++-----
 lib/net/net_crc_sse.c    | 17 +++++++++--------
 lib/net/rte_arp.h        |  8 ++++----
 lib/net/rte_ether.h      |  8 ++++----
 5 files changed, 31 insertions(+), 27 deletions(-)
  

Patch

diff --git a/lib/net/net_crc_avx512.c b/lib/net/net_crc_avx512.c
index f6a3ce9..0f48ca0 100644
--- a/lib/net/net_crc_avx512.c
+++ b/lib/net/net_crc_avx512.c
@@ -3,6 +3,8 @@ 
  */
 
 
+#include <stdalign.h>
+
 #include <rte_common.h>
 
 #include "net_crc.h"
@@ -20,8 +22,8 @@  struct crc_vpclmulqdq_ctx {
 	__m128i fold_1x128b;
 };
 
-static struct crc_vpclmulqdq_ctx crc32_eth __rte_aligned(64);
-static struct crc_vpclmulqdq_ctx crc16_ccitt __rte_aligned(64);
+static alignas(64) struct crc_vpclmulqdq_ctx crc32_eth;
+static alignas(64) struct crc_vpclmulqdq_ctx crc16_ccitt;
 
 static uint16_t byte_len_to_mask_table[] = {
 	0x0000, 0x0001, 0x0003, 0x0007,
@@ -30,18 +32,18 @@  struct crc_vpclmulqdq_ctx {
 	0x0fff, 0x1fff, 0x3fff, 0x7fff,
 	0xffff};
 
-static const uint8_t shf_table[32] __rte_aligned(16) = {
+static const alignas(16) uint8_t shf_table[32] = {
 	0x00, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
 	0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
 	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
 	0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
 };
 
-static const uint32_t mask[4] __rte_aligned(16) = {
+static const alignas(16) uint32_t mask[4] = {
 	0xffffffff, 0xffffffff, 0x00000000, 0x00000000
 };
 
-static const uint32_t mask2[4] __rte_aligned(16) = {
+static const alignas(16) uint32_t mask2[4] = {
 	0x00000000, 0xffffffff, 0xffffffff, 0xffffffff
 };
 
@@ -93,7 +95,7 @@  struct crc_vpclmulqdq_ctx {
 	uint32_t offset;
 	__m128i res2, res3, res4, pshufb_shf;
 
-	const uint32_t mask3[4] __rte_aligned(16) = {
+	const alignas(16) uint32_t mask3[4] = {
 		   0x80808080, 0x80808080, 0x80808080, 0x80808080
 	};
 
diff --git a/lib/net/net_crc_neon.c b/lib/net/net_crc_neon.c
index f61d75a..cee75dd 100644
--- a/lib/net/net_crc_neon.c
+++ b/lib/net/net_crc_neon.c
@@ -2,6 +2,7 @@ 
  * Copyright(c) 2017 Cavium, Inc
  */
 
+#include <stdalign.h>
 #include <string.h>
 
 #include <rte_common.h>
@@ -19,8 +20,8 @@  struct crc_pmull_ctx {
 	uint64x2_t rk7_rk8;
 };
 
-struct crc_pmull_ctx crc32_eth_pmull __rte_aligned(16);
-struct crc_pmull_ctx crc16_ccitt_pmull __rte_aligned(16);
+alignas(16) struct crc_pmull_ctx crc32_eth_pmull;
+alignas(16) struct crc_pmull_ctx crc16_ccitt_pmull;
 
 /**
  * @brief Performs one folding round
@@ -96,10 +97,10 @@  struct crc_pmull_ctx {
 crcr32_reduce_64_to_32(uint64x2_t data64,
 	uint64x2_t precomp)
 {
-	static uint32_t mask1[4] __rte_aligned(16) = {
+	static alignas(16) uint32_t mask1[4] = {
 		0xffffffff, 0xffffffff, 0x00000000, 0x00000000
 	};
-	static uint32_t mask2[4] __rte_aligned(16) = {
+	static alignas(16) uint32_t mask2[4] = {
 		0x00000000, 0xffffffff, 0xffffffff, 0xffffffff
 	};
 	uint64x2_t tmp0, tmp1, tmp2;
@@ -148,7 +149,7 @@  struct crc_pmull_ctx {
 
 		if (unlikely(data_len < 16)) {
 			/* 0 to 15 bytes */
-			uint8_t buffer[16] __rte_aligned(16);
+			alignas(16) uint8_t buffer[16];
 
 			memset(buffer, 0, sizeof(buffer));
 			memcpy(buffer, data, data_len);
diff --git a/lib/net/net_crc_sse.c b/lib/net/net_crc_sse.c
index dd75845..d673ae3 100644
--- a/lib/net/net_crc_sse.c
+++ b/lib/net/net_crc_sse.c
@@ -2,6 +2,7 @@ 
  * Copyright(c) 2017-2020 Intel Corporation
  */
 
+#include <stdalign.h>
 #include <string.h>
 
 #include <rte_common.h>
@@ -18,8 +19,8 @@  struct crc_pclmulqdq_ctx {
 	__m128i rk7_rk8;
 };
 
-static struct crc_pclmulqdq_ctx crc32_eth_pclmulqdq __rte_aligned(16);
-static struct crc_pclmulqdq_ctx crc16_ccitt_pclmulqdq __rte_aligned(16);
+static alignas(16) struct crc_pclmulqdq_ctx crc32_eth_pclmulqdq;
+static alignas(16) struct crc_pclmulqdq_ctx crc16_ccitt_pclmulqdq;
 /**
  * @brief Performs one folding round
  *
@@ -96,11 +97,11 @@  struct crc_pclmulqdq_ctx {
 static __rte_always_inline uint32_t
 crcr32_reduce_64_to_32(__m128i data64, __m128i precomp)
 {
-	static const uint32_t mask1[4] __rte_aligned(16) = {
+	static const alignas(16) uint32_t mask1[4] = {
 		0xffffffff, 0xffffffff, 0x00000000, 0x00000000
 	};
 
-	static const uint32_t mask2[4] __rte_aligned(16) = {
+	static const alignas(16) uint32_t mask2[4] = {
 		0x00000000, 0xffffffff, 0xffffffff, 0xffffffff
 	};
 	__m128i tmp0, tmp1, tmp2;
@@ -118,7 +119,7 @@  struct crc_pclmulqdq_ctx {
 	return _mm_extract_epi32(tmp2, 2);
 }
 
-static const uint8_t crc_xmm_shift_tab[48] __rte_aligned(16) = {
+static const alignas(16) uint8_t crc_xmm_shift_tab[48] = {
 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
 	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
@@ -175,7 +176,7 @@  struct crc_pclmulqdq_ctx {
 
 		if (unlikely(data_len < 16)) {
 			/* 0 to 15 bytes */
-			uint8_t buffer[16] __rte_aligned(16);
+			alignas(16) uint8_t buffer[16];
 
 			memset(buffer, 0, sizeof(buffer));
 			memcpy(buffer, data, data_len);
@@ -212,11 +213,11 @@  struct crc_pclmulqdq_ctx {
 partial_bytes:
 	if (likely(n < data_len)) {
 
-		const uint32_t mask3[4] __rte_aligned(16) = {
+		const alignas(16) uint32_t mask3[4] = {
 			0x80808080, 0x80808080, 0x80808080, 0x80808080
 		};
 
-		const uint8_t shf_table[32] __rte_aligned(16) = {
+		const alignas(16) uint8_t shf_table[32] = {
 			0x00, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
 			0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
 			0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
diff --git a/lib/net/rte_arp.h b/lib/net/rte_arp.h
index c3cd0af..668cea1 100644
--- a/lib/net/rte_arp.h
+++ b/lib/net/rte_arp.h
@@ -21,17 +21,17 @@ 
 /**
  * ARP header IPv4 payload.
  */
-struct rte_arp_ipv4 {
+struct __rte_aligned(2) rte_arp_ipv4 {
 	struct rte_ether_addr arp_sha;  /**< sender hardware address */
 	rte_be32_t            arp_sip;  /**< sender IP address */
 	struct rte_ether_addr arp_tha;  /**< target hardware address */
 	rte_be32_t            arp_tip;  /**< target IP address */
-} __rte_packed __rte_aligned(2);
+} __rte_packed;
 
 /**
  * ARP header.
  */
-struct rte_arp_hdr {
+struct __rte_aligned(2) rte_arp_hdr {
 	rte_be16_t arp_hardware; /**< format of hardware address */
 #define RTE_ARP_HRD_ETHER     1  /**< ARP Ethernet address format */
 
@@ -47,7 +47,7 @@  struct rte_arp_hdr {
 #define	RTE_ARP_OP_INVREPLY   9  /**< response identifying peer */
 
 	struct rte_arp_ipv4 arp_data;
-} __rte_packed __rte_aligned(2);
+} __rte_packed;
 
 /**
  * Make a RARP packet based on MAC addr.
diff --git a/lib/net/rte_ether.h b/lib/net/rte_ether.h
index 75285bd..32ed515 100644
--- a/lib/net/rte_ether.h
+++ b/lib/net/rte_ether.h
@@ -71,9 +71,9 @@ 
  * administrator and does not contain OUIs.
  * See http://standards.ieee.org/regauth/groupmac/tutorial.html
  */
-struct rte_ether_addr {
+struct __rte_aligned(2) rte_ether_addr {
 	uint8_t addr_bytes[RTE_ETHER_ADDR_LEN]; /**< Addr bytes in tx order */
-} __rte_aligned(2);
+};
 
 #define RTE_ETHER_LOCAL_ADMIN_ADDR 0x02 /**< Locally assigned Eth. address. */
 #define RTE_ETHER_GROUP_ADDR  0x01 /**< Multicast or broadcast Eth. address. */
@@ -290,11 +290,11 @@  static inline int rte_is_valid_assigned_ether_addr(const struct rte_ether_addr *
  * Ethernet header: Contains the destination address, source address
  * and frame type.
  */
-struct rte_ether_hdr {
+struct __rte_aligned(2) rte_ether_hdr {
 	struct rte_ether_addr dst_addr; /**< Destination address. */
 	struct rte_ether_addr src_addr; /**< Source address. */
 	rte_be16_t ether_type; /**< Frame type. */
-} __rte_aligned(2);
+};
 
 /**
  * Ethernet VLAN Header.