From patchwork Thu May 18 10:14:07 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adrien Mazarguil X-Patchwork-Id: 24387 Return-Path: X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [IPv6:::1]) by dpdk.org (Postfix) with ESMTP id 803A42C38; Thu, 18 May 2017 12:14:16 +0200 (CEST) Received: from mail-wm0-f48.google.com (mail-wm0-f48.google.com [74.125.82.48]) by dpdk.org (Postfix) with ESMTP id 75B262901 for ; Thu, 18 May 2017 12:14:14 +0200 (CEST) Received: by mail-wm0-f48.google.com with SMTP id 70so9672290wmq.1 for ; Thu, 18 May 2017 03:14:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=6wind-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id; bh=+Q8VuQx2Hc3cqVCLglrD6xQLV5+Nx/NDC7LHVVU3xN4=; b=LsjBebRAqcPcpA4gtE4nhCDEfudlSSukNLdqnzjuCeEbD74tv+5NiiNKzigOPTfacI OI+nepxN4+fY0dU8iAvuqKxKbCJfkZ7OPJLEEKGLrvvSFB/4HC3U2TYd1EbLe1Q/LTdm /Cl1DL8WxWIEYHtkfAFdqQBhXND2GkmVoMMaRNpMQO1qDYi+i3FF2baRIPlRJsF9xOcg yNOx4pAGXyVRo8+fMYlWUG9YhPFe7qWk2xhap/tuODxqtHo5XtnVDMpS7RTPC2gBHjME 9ZnQ/0uKWQed9YMO0r4v6YQR3lyzI3/F5x/f6oJVrCFKlPE76fqB8qSislCQrAWyOk/B +dDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=+Q8VuQx2Hc3cqVCLglrD6xQLV5+Nx/NDC7LHVVU3xN4=; b=KfVkiDKVDveGQe+O2AZheZG3+6KBtHmTUS1jUiufASQbgOnrd4KzROQgA2UlzIOACh c+zDfpYTiYkMEP7WyufZEEKgMPTqGzt3nQKgunoNESNo9CQ6RHcuEnp3o3z49/jPHbN0 y8BjmFlTA0hqfm089ZxuLZsiVpnjAGjYOZKb9hwkRFgYJ3VK5QyDgS8YOSnwz2gdkemy ZEKp7vIO51Ap3Wzx7/MJKLIOoeH0n8J+J+LoL5PBp3C4z3AO4zjv/Tqw0+kL9Oyb9ogf l90NuJ9iZJimCGNZL0ue0JbUx5jGGLrO4vGbYl3rMa4scyBJgkIP+poQvRkYPl5Iajcc WePQ== X-Gm-Message-State: AODbwcC3GoET2xIvy1e59nersoyuuREjunPWgJ1EP5JV2jmaChob+ly2 tMWLpA/EbZkiN+Hzpfg= X-Received: by 10.28.191.26 with SMTP id p26mr11275419wmf.54.1495102453837; Thu, 18 May 2017 03:14:13 -0700 (PDT) Received: from 6wind.com (host.78.145.23.62.rev.coltfrance.com. [62.23.145.78]) by smtp.gmail.com with ESMTPSA id h199sm17147904wme.4.2017.05.18.03.14.13 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Thu, 18 May 2017 03:14:13 -0700 (PDT) From: Adrien Mazarguil To: Thomas Monjalon Cc: dev@dpdk.org Date: Thu, 18 May 2017 12:14:07 +0200 Message-Id: <840342851720fc237214aeb30d38565615293b58.1495101988.git.adrien.mazarguil@6wind.com> X-Mailer: git-send-email 2.1.4 Subject: [dpdk-dev] [PATCH 1/2] eal: add static endianness conversion macros X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" These macros resolve to constant expressions that allow developers to perform endianness conversion on static/const objects, even outside of function scope as they do not translate to function calls. This is most useful for static initializers and constant values (whenever it has to be performed at compilation time). Run-time endianness conversion of variable values should keep using rte_*_to_*() calls for best performance. Signed-off-by: Adrien Mazarguil --- .../common/include/generic/rte_byteorder.h | 58 +++++++++++++++----- 1 file changed, 44 insertions(+), 14 deletions(-) diff --git a/lib/librte_eal/common/include/generic/rte_byteorder.h b/lib/librte_eal/common/include/generic/rte_byteorder.h index e00bccb..8408872 100644 --- a/lib/librte_eal/common/include/generic/rte_byteorder.h +++ b/lib/librte_eal/common/include/generic/rte_byteorder.h @@ -74,6 +74,47 @@ #elif defined __LITTLE_ENDIAN__ #define RTE_BYTE_ORDER RTE_LITTLE_ENDIAN #endif +#if !defined(RTE_BYTE_ORDER) +#error Unknown endianness. +#endif + +#define RTE_STATIC_BSWAP16(v) \ + ((((uint16_t)(v) & UINT16_C(0x00ff)) << 8) | \ + (((uint16_t)(v) & UINT16_C(0xff00)) >> 8)) + +#define RTE_STATIC_BSWAP32(v) \ + ((((uint32_t)(v) & UINT32_C(0x000000ff)) << 24) | \ + (((uint32_t)(v) & UINT32_C(0x0000ff00)) << 8) | \ + (((uint32_t)(v) & UINT32_C(0x00ff0000)) >> 8) | \ + (((uint32_t)(v) & UINT32_C(0xff000000)) >> 24)) + +#define RTE_STATIC_BSWAP64(v) \ + ((((uint64_t)(v) & UINT64_C(0x00000000000000ff)) << 56) | \ + (((uint64_t)(v) & UINT64_C(0x000000000000ff00)) << 40) | \ + (((uint64_t)(v) & UINT64_C(0x0000000000ff0000)) << 24) | \ + (((uint64_t)(v) & UINT64_C(0x00000000ff000000)) << 8) | \ + (((uint64_t)(v) & UINT64_C(0x000000ff00000000)) >> 8) | \ + (((uint64_t)(v) & UINT64_C(0x0000ff0000000000)) >> 24) | \ + (((uint64_t)(v) & UINT64_C(0x00ff000000000000)) >> 40) | \ + (((uint64_t)(v) & UINT64_C(0xff00000000000000)) >> 56)) + +#if RTE_BYTE_ORDER == RTE_BIG_ENDIAN +#define RTE_BE16(v) (uint16_t)(v) +#define RTE_BE32(v) (uint32_t)(v) +#define RTE_BE64(v) (uint64_t)(v) +#define RTE_LE16(v) RTE_STATIC_BSWAP16(v) +#define RTE_LE32(v) RTE_STATIC_BSWAP32(v) +#define RTE_LE64(v) RTE_STATIC_BSWAP64(v) +#elif RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN +#define RTE_BE16(v) RTE_STATIC_BSWAP16(v) +#define RTE_BE32(v) RTE_STATIC_BSWAP32(v) +#define RTE_BE64(v) RTE_STATIC_BSWAP64(v) +#define RTE_LE16(v) (uint16_t)(v) +#define RTE_LE32(v) (uint32_t)(v) +#define RTE_LE64(v) (uint64_t)(v) +#else +#error Unsupported endianness. +#endif /* * An internal function to swap bytes in a 16-bit value. @@ -84,8 +125,7 @@ static inline uint16_t rte_constant_bswap16(uint16_t x) { - return (uint16_t)(((x & 0x00ffU) << 8) | - ((x & 0xff00U) >> 8)); + return RTE_STATIC_BSWAP16(x); } /* @@ -97,10 +137,7 @@ rte_constant_bswap16(uint16_t x) static inline uint32_t rte_constant_bswap32(uint32_t x) { - return ((x & 0x000000ffUL) << 24) | - ((x & 0x0000ff00UL) << 8) | - ((x & 0x00ff0000UL) >> 8) | - ((x & 0xff000000UL) >> 24); + return RTE_STATIC_BSWAP32(x); } /* @@ -112,14 +149,7 @@ rte_constant_bswap32(uint32_t x) static inline uint64_t rte_constant_bswap64(uint64_t x) { - return ((x & 0x00000000000000ffULL) << 56) | - ((x & 0x000000000000ff00ULL) << 40) | - ((x & 0x0000000000ff0000ULL) << 24) | - ((x & 0x00000000ff000000ULL) << 8) | - ((x & 0x000000ff00000000ULL) >> 8) | - ((x & 0x0000ff0000000000ULL) >> 24) | - ((x & 0x00ff000000000000ULL) >> 40) | - ((x & 0xff00000000000000ULL) >> 56); + return RTE_STATIC_BSWAP64(x); }