From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr0-f175.google.com (mail-wr0-f175.google.com [209.85.128.175]) by dpdk.org (Postfix) with ESMTP id C51162BF1 for ; Thu, 15 Jun 2017 17:49:09 +0200 (CEST) Received: by mail-wr0-f175.google.com with SMTP id 77so24723861wrb.1 for ; Thu, 15 Jun 2017 08:49:09 -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:in-reply-to:references; bh=d792b0AQzYLGOKs2rpqN5Er9OX+tI87IfCDGeFScU1A=; b=1KpUqlhl+uvfY7XHauDyje4rHPkIkci2QoUByxacCxDAGhetM86AcXrh/XYPbepWWb WX2rWw3iXZcGGsnbPcU9YbQt2TgaGwKu6RMMTbXrKfxbAKREvbwR0iE2mNJ9J+906sTM Mf9RK7op4C0aADlIgq0hiCwbzQh7cGNy+MpAsMmM6FjgRF8epO3vltyVi1e9Hcz0YUJq pxVrokmYDIC3mPbzwkHjqMoGP3/4tytltvQWg8WW7hOyYvX1eEQcMI1jrB5B4IbrKgkx 4RmbxEqrfrEWiPEV1KjyF5So6ZbDK8QtABdO6sZ5H9B66IPYuI4DJlzoVz91OxzoT04k LBmA== 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:in-reply-to :references; bh=d792b0AQzYLGOKs2rpqN5Er9OX+tI87IfCDGeFScU1A=; b=fDWZ0ZcsHG8irIf1HpOKdPhTrUxMFk1FiID5al0MDfZ5Hfk6hAH65ZvzLqDi++8UOR aPQE3OnuU4OqUNnwftLNd5XEZ+htLLpCk+XC+a5RaOMf72X8Cy9WMCeX3/ToCEY7uCng 39Vvh386A+1iN7URIF4tKpLUds7Jt8usvBFn501R5EjVgZ5bztILGdAui6D1nVi2RIHU 8/qIPgYjLHsZ5CN5w/dKe+Brp9sSp8VFjvQnwejumm0h0hlAGR8fimdW3E5zKzpyxq5E 8G9CMT/cnTjnE75ZqT26WI+8q+ip1cqFwDpo6pXuc6X5tl0GW/jHXxEDZA2rdZlSHyNr XoYg== X-Gm-Message-State: AKS2vOzawSCYNmK7AUhuLPLjTAEQVpZu0TK5014YsHLXXmZGlAuB6cRy 0/QvzwDzh2SAblgk+cQ= X-Received: by 10.223.171.29 with SMTP id q29mr4041646wrc.12.1497541749380; Thu, 15 Jun 2017 08:49:09 -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 n92sm489938wrb.62.2017.06.15.08.49.08 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Thu, 15 Jun 2017 08:49:08 -0700 (PDT) From: Adrien Mazarguil To: dev@dpdk.org Cc: Thomas Monjalon Date: Thu, 15 Jun 2017 17:48:58 +0200 Message-Id: <2e1d93ea9c0e282d91c46ee91f534c38aaf58c74.1497541557.git.adrien.mazarguil@6wind.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <840342851720fc237214aeb30d38565615293b58.1495101988.git.adrien.mazarguil@6wind.com> References: <840342851720fc237214aeb30d38565615293b58.1495101988.git.adrien.mazarguil@6wind.com> Subject: [dpdk-dev] [PATCH v2 2/3] 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: , X-List-Received-Date: Thu, 15 Jun 2017 15:49:11 -0000 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 | 70 ++++++++++++++++---- 1 file changed, 56 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 df8cc84..e5e820d 100644 --- a/lib/librte_eal/common/include/generic/rte_byteorder.h +++ b/lib/librte_eal/common/include/generic/rte_byteorder.h @@ -74,6 +74,59 @@ #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)) + +/* + * These macros are functionally similar to rte_cpu_to_(be|le)(16|32|64)(), + * they take values in host CPU order and return them converted to the + * intended endianness. + * + * They resolve at compilation time to integer constants which can safely be + * used with static initializers, since those cannot involve function calls. + * + * On the other hand, they are not as optimized as their rte_cpu_to_*() + * counterparts, therefore applications should refrain from using them on + * variable values, particularly inside performance-sensitive code. + */ +#if RTE_BYTE_ORDER == RTE_BIG_ENDIAN +#define RTE_BE16(v) (rte_be16_t)(v) +#define RTE_BE32(v) (rte_be32_t)(v) +#define RTE_BE64(v) (rte_be64_t)(v) +#define RTE_LE16(v) (rte_le16_t)(RTE_STATIC_BSWAP16(v)) +#define RTE_LE32(v) (rte_le32_t)(RTE_STATIC_BSWAP32(v)) +#define RTE_LE64(v) (rte_le64_t)(RTE_STATIC_BSWAP64(v)) +#elif RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN +#define RTE_BE16(v) (rte_be16_t)(RTE_STATIC_BSWAP16(v)) +#define RTE_BE32(v) (rte_be32_t)(RTE_STATIC_BSWAP32(v)) +#define RTE_BE64(v) (rte_be64_t)(RTE_STATIC_BSWAP64(v)) +#define RTE_LE16(v) (rte_be16_t)(v) +#define RTE_LE32(v) (rte_be32_t)(v) +#define RTE_LE64(v) (rte_be64_t)(v) +#else +#error Unsupported endianness. +#endif /* * The following types should be used when handling values according to a @@ -98,8 +151,7 @@ typedef uint64_t rte_le64_t; /**< 64-bit little-endian value. */ static inline uint16_t rte_constant_bswap16(uint16_t x) { - return (uint16_t)(((x & 0x00ffU) << 8) | - ((x & 0xff00U) >> 8)); + return RTE_STATIC_BSWAP16(x); } /* @@ -111,10 +163,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); } /* @@ -126,14 +175,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); } -- 2.1.4