You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2022/06/29 03:33:05 UTC

[incubator-nuttx] branch master updated: Use builtins for byteswapping

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 903a186304 Use builtins for byteswapping
903a186304 is described below

commit 903a1863049f368694d68c0d490ce245ba7b465b
Author: Nimish Telang <ni...@users.noreply.github.com>
AuthorDate: Sun Jun 19 16:24:55 2022 -0400

    Use builtins for byteswapping
    
    Make use of XCHG/BSWAP on x86, REV16 and REV on ARMv6-m and above,
    and whatever other optimized instructions on other platforms.
    
    Defines extra CONFIG variables, and removes the unused functions for
    endian-swapping. Fixes some oversights in using the macros.
---
 binfmt/libnxflat/libnxflat_init.c |  2 +-
 include/endian.h                  | 44 ++++++++++++++++++-------------------
 include/netinet/in.h              | 21 +++++-------------
 include/nuttx/compiler.h          | 26 ++++++++++++----------
 libs/libc/Makefile                |  1 -
 libs/libc/endian/Make.defs        | 28 ------------------------
 libs/libc/endian/lib_swap16.c     | 38 --------------------------------
 libs/libc/endian/lib_swap32.c     | 40 ----------------------------------
 libs/libc/endian/lib_swap64.c     | 46 ---------------------------------------
 libs/libc/net/lib_htonl.c         | 15 ++-----------
 libs/libc/net/lib_htons.c         |  6 +----
 libs/libc/string/lib_ffs.c        |  4 +++-
 libs/libc/string/lib_ffsl.c       |  4 +++-
 libs/libc/string/lib_ffsll.c      |  4 +++-
 14 files changed, 54 insertions(+), 225 deletions(-)

diff --git a/binfmt/libnxflat/libnxflat_init.c b/binfmt/libnxflat/libnxflat_init.c
index 73c9820030..8c87b52a40 100644
--- a/binfmt/libnxflat/libnxflat_init.c
+++ b/binfmt/libnxflat/libnxflat_init.c
@@ -168,7 +168,7 @@ int nxflat_init(const char *filename, struct nxflat_loadinfo_s *loadinfo)
    */
 
   loadinfo->relocstart  = NTOHL(loadinfo->header.h_relocstart);
-  loadinfo->reloccount  = ntohs(loadinfo->header.h_reloccount);
+  loadinfo->reloccount  = NTOHS(loadinfo->header.h_reloccount);
 
   return 0;
 }
diff --git a/include/endian.h b/include/endian.h
index 76cc93c87b..fc2b3041a6 100644
--- a/include/endian.h
+++ b/include/endian.h
@@ -51,16 +51,17 @@
 
 /* Common byte swapping macros */
 
-#define __SWAP_UINT16_ISMACRO 1
-#undef  __SWAP_UINT32_ISMACRO
-
-#ifdef __SWAP_UINT16_ISMACRO
+#ifdef CONFIG_HAVE_BUILTIN_BSWAP16
+#  define __swap_uint16 __builtin_bswap16
+#else
 #  define __swap_uint16(n) \
     (uint16_t)(((((uint16_t)(n)) & 0x00ff) << 8) | \
                ((((uint16_t)(n)) >> 8) & 0x00ff))
 #endif
 
-#ifdef __SWAP_UINT32_ISMACRO
+#ifdef CONFIG_HAVE_BUILTIN_BSWAP32
+#  define __swap_uint32 __builtin_bswap32
+#else
 #  define __swap_uint32(n) \
     (uint32_t)(((((uint32_t)(n)) & 0x000000ffUL) << 24) | \
                ((((uint32_t)(n)) & 0x0000ff00UL) <<  8) | \
@@ -68,6 +69,22 @@
                ((((uint32_t)(n)) & 0xff000000UL) >> 24))
 #endif
 
+#ifdef CONFIG_HAVE_LONG_LONG
+#  ifdef CONFIG_HAVE_BUILTIN_BSWAP64
+#    define __swap_uint64 __builtin_bswap64
+#  else
+#    define __swap_uint64(n) \
+        (uint64_t)(((((uint64_t)(n)) & 0x00000000000000ffULL) << 56) | \
+                   ((((uint64_t)(n)) & 0x000000000000ff00ULL) << 40) | \
+                   ((((uint64_t)(n)) & 0x0000000000ff0000ULL) << 24) | \
+                   ((((uint64_t)(n)) & 0x00000000ff000000ULL) <<  8) | \
+                   ((((uint64_t)(n)) & 0x000000ff00000000ULL) >>  8) | \
+                   ((((uint64_t)(n)) & 0x0000ff0000000000ULL) >> 24) | \
+                   ((((uint64_t)(n)) & 0x00ff000000000000ULL) >> 40) | \
+                   ((((uint64_t)(n)) & 0xff00000000000000ULL) >> 56))
+#  endif
+#endif
+
 /* Endian-specific definitions */
 
 #ifdef CONFIG_ENDIAN_BIG
@@ -120,21 +137,4 @@
 #    define le64toh(n)        (n)
 #  endif
 #endif
-
-/****************************************************************************
- * Public Function Prototypes
- ****************************************************************************/
-
-#ifndef __SWAP_UINT16_ISMACRO
-uint16_t __swap_uint16(uint16_t n);
-#endif
-
-#ifndef __SWAP_UINT32_ISMACRO
-uint32_t __swap_uint32(uint32_t n);
-#endif
-
-#if CONFIG_HAVE_LONG_LONG
-uint64_t __swap_uint64(uint64_t n);
-#endif
-
 #endif /* __INCLUDE_ENDIAN_H */
diff --git a/include/netinet/in.h b/include/netinet/in.h
index 428769e971..59af8f807d 100644
--- a/include/netinet/in.h
+++ b/include/netinet/in.h
@@ -30,6 +30,7 @@
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <stdint.h>
+#include <endian.h>
 
 /****************************************************************************
  * Pre-processor Definitions
@@ -217,26 +218,14 @@
 /* This macro to convert a 16/32-bit constant values quantity from host byte
  * order to network byte order.  The 16-bit version of this macro is required
  * for uIP:
- *
- *   Author Adam Dunkels <ad...@dunkels.com>
- *   Copyright (c) 2001-2003, Adam Dunkels.
- *   All rights reserved.
  */
 
 #ifdef CONFIG_ENDIAN_BIG
-# define HTONS(ns) (ns)
-# define HTONL(nl) (nl)
+#  define HTONS(ns) (ns)
+#  define HTONL(nl) (nl)
 #else
-# define HTONS(ns) \
-  (unsigned short) \
-    (((((unsigned short)(ns)) & 0x00ff) << 8) | \
-     ((((unsigned short)(ns)) >> 8) & 0x00ff))
-# define HTONL(nl) \
-  (unsigned long) \
-    (((((unsigned long)(nl)) & 0x000000ffUL) << 24) | \
-     ((((unsigned long)(nl)) & 0x0000ff00UL) <<  8) | \
-     ((((unsigned long)(nl)) & 0x00ff0000UL) >>  8) | \
-     ((((unsigned long)(nl)) & 0xff000000UL) >> 24))
+#  define HTONS __swap_uint16
+#  define HTONL __swap_uint32
 #endif
 
 #define NTOHS(hs) HTONS(hs)
diff --git a/include/nuttx/compiler.h b/include/nuttx/compiler.h
index 7839538c8b..e428aefb87 100644
--- a/include/nuttx/compiler.h
+++ b/include/nuttx/compiler.h
@@ -73,6 +73,20 @@
 
 #ifdef __GNUC__
 
+/* Built-ins */
+#  if __GNUC__ >= 4
+#    define CONFIG_HAVE_BUILTIN_BSWAP16 1
+#    define CONFIG_HAVE_BUILTIN_BSWAP32 1
+#    define CONFIG_HAVE_BUILTIN_BSWAP64 1
+#    define CONFIG_HAVE_BUILTIN_CTZ 1
+#    define CONFIG_HAVE_BUILTIN_CLZ 1
+#    define CONFIG_HAVE_BUILTIN_POPCOUNT 1
+#    define CONFIG_HAVE_BUILTIN_POPCOUNTLL 1
+#    define CONFIG_HAVE_BUILTIN_FFS 1
+#    define CONFIG_HAVE_BUILTIN_FFSL 1
+#    define CONFIG_HAVE_BUILTIN_FFSLL 1
+#  endif
+
 /* Pre-processor */
 
 #  define CONFIG_CPP_HAVE_VARARGS 1 /* Supports variable argument macros */
@@ -103,18 +117,6 @@
 
 #  define offsetof(a, b) __builtin_offsetof(a, b)
 
-/* GCC 4.x have __builtin_ctz(|l|ll) and __builtin_clz(|l|ll). These count
- * trailing/leading zeros of input number and typically will generate few
- * fast bit-counting instructions. Inputting zero to these functions is
- * undefined and needs to be taken care of by the caller.
- */
-
-#  if __GNUC__ >= 4
-#    define CONFIG_HAVE_BUILTIN_CTZ      1
-#    define CONFIG_HAVE_BUILTIN_CLZ      1
-#    define CONFIG_HAVE_BUILTIN_POPCOUNT 1
-#  endif
-
 /* Attributes
  *
  * GCC supports weak symbols which can be used to reduce code size because
diff --git a/libs/libc/Makefile b/libs/libc/Makefile
index a94d4a97bf..3b9258efa7 100644
--- a/libs/libc/Makefile
+++ b/libs/libc/Makefile
@@ -27,7 +27,6 @@ include builtin/Make.defs
 include ctype/Make.defs
 include dirent/Make.defs
 include dlfcn/Make.defs
-include endian/Make.defs
 include errno/Make.defs
 include eventfd/Make.defs
 include fixedmath/Make.defs
diff --git a/libs/libc/endian/Make.defs b/libs/libc/endian/Make.defs
deleted file mode 100644
index 3b18dfa51a..0000000000
--- a/libs/libc/endian/Make.defs
+++ /dev/null
@@ -1,28 +0,0 @@
-############################################################################
-# libs/libc/endian/Make.defs
-#
-# Licensed to the Apache Software Foundation (ASF) under one or more
-# contributor license agreements.  See the NOTICE file distributed with
-# this work for additional information regarding copyright ownership.  The
-# ASF licenses this file to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance with the
-# License.  You may obtain a copy of the License at
-#
-#   http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
-# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
-# License for the specific language governing permissions and limitations
-# under the License.
-#
-############################################################################
-
-# Add the endian.h C files to the build
-
-CSRCS += lib_swap16.c  lib_swap32.c  lib_swap64.c
-
-# Add the endian directory to the build
-
-DEPPATH += --dep-path endian
-VPATH += :endian
diff --git a/libs/libc/endian/lib_swap16.c b/libs/libc/endian/lib_swap16.c
deleted file mode 100644
index 9e41648368..0000000000
--- a/libs/libc/endian/lib_swap16.c
+++ /dev/null
@@ -1,38 +0,0 @@
-/****************************************************************************
- * libs/libc/endian/lib_swap16.c
- *
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.  The
- * ASF licenses this file to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance with the
- * License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
- * License for the specific language governing permissions and limitations
- * under the License.
- *
- ****************************************************************************/
-
-/****************************************************************************
- * Included Files
- ****************************************************************************/
-
-#include <stdint.h>
-#include <endian.h>
-
-/****************************************************************************
- * Public Functions
- ****************************************************************************/
-
-#ifndef __SWAP_UINT16_ISMACRO
-uint16_t __swap_uint16(uint16_t n)
-{
-  return (uint16_t)(((((uint16_t)(n)) & 0x00ff) << 8) |
-                    ((((uint16_t)(n)) >> 8) & 0x00ff));
-}
-#endif
diff --git a/libs/libc/endian/lib_swap32.c b/libs/libc/endian/lib_swap32.c
deleted file mode 100644
index c23bacba36..0000000000
--- a/libs/libc/endian/lib_swap32.c
+++ /dev/null
@@ -1,40 +0,0 @@
-/****************************************************************************
- * libs/libc/endian/lib_swap32.c
- *
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.  The
- * ASF licenses this file to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance with the
- * License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
- * License for the specific language governing permissions and limitations
- * under the License.
- *
- ****************************************************************************/
-
-/****************************************************************************
- * Included Files
- ****************************************************************************/
-
-#include <stdint.h>
-#include <endian.h>
-
-/****************************************************************************
- * Public Functions
- ****************************************************************************/
-
-#ifndef __SWAP_UINT32_ISMACRO
-uint32_t __swap_uint32(uint32_t n)
-{
-  return (uint32_t)(((((uint32_t)(n)) & 0x000000fful) << 24) |
-                    ((((uint32_t)(n)) & 0x0000ff00ul) <<  8) |
-                    ((((uint32_t)(n)) & 0x00ff0000ul) >>  8) |
-                    ((((uint32_t)(n)) & 0xff000000ul) >> 24));
-}
-#endif
diff --git a/libs/libc/endian/lib_swap64.c b/libs/libc/endian/lib_swap64.c
deleted file mode 100644
index e89fb86ec1..0000000000
--- a/libs/libc/endian/lib_swap64.c
+++ /dev/null
@@ -1,46 +0,0 @@
-/****************************************************************************
- * libs/libc/endian/lib_swap64.c
- *
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.  The
- * ASF licenses this file to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance with the
- * License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
- * License for the specific language governing permissions and limitations
- * under the License.
- *
- ****************************************************************************/
-
-/****************************************************************************
- * Included Files
- ****************************************************************************/
-
-#include <nuttx/compiler.h>
-
-#include <stdint.h>
-#include <endian.h>
-
-/****************************************************************************
- * Public Functions
- ****************************************************************************/
-
-#ifdef CONFIG_HAVE_LONG_LONG
-uint64_t __swap_uint64(uint64_t n)
-{
-  return (uint64_t)(((((uint64_t)(n)) & 0x00000000000000ffull) << 56) |
-                    ((((uint64_t)(n)) & 0x000000000000ff00ull) << 40) |
-                    ((((uint64_t)(n)) & 0x0000000000ff0000ull) << 24) |
-                    ((((uint64_t)(n)) & 0x00000000ff000000ull) <<  8) |
-                    ((((uint64_t)(n)) & 0x000000ff00000000ull) >>  8) |
-                    ((((uint64_t)(n)) & 0x0000ff0000000000ull) >> 24) |
-                    ((((uint64_t)(n)) & 0x00ff000000000000ull) >> 40) |
-                    ((((uint64_t)(n)) & 0xff00000000000000ull) >> 56));
-}
-#endif
diff --git a/libs/libc/net/lib_htonl.c b/libs/libc/net/lib_htonl.c
index 4dfc69c54e..8f1a00bc0a 100644
--- a/libs/libc/net/lib_htonl.c
+++ b/libs/libc/net/lib_htonl.c
@@ -33,21 +33,10 @@
 
 uint32_t htonl(uint32_t hl)
 {
-#ifdef CONFIG_ENDIAN_BIG
-  return hl;
-#else
-  return (((hl) >> 24) |
-          (((hl) >>  8) & 0x0000ff00) |
-          (((hl) <<  8) & 0x00ff0000) |
-           ((hl) << 24));
-#endif
+  return HTONL(hl);
 }
 
 uint32_t ntohl(uint32_t nl)
 {
-#ifdef CONFIG_ENDIAN_BIG
-  return nl;
-#else
-  return htonl(nl);
-#endif
+  return NTOHL(nl);
 }
diff --git a/libs/libc/net/lib_htons.c b/libs/libc/net/lib_htons.c
index dea7e27317..ebef3c6472 100644
--- a/libs/libc/net/lib_htons.c
+++ b/libs/libc/net/lib_htons.c
@@ -38,9 +38,5 @@ uint16_t htons(uint16_t hs)
 
 uint16_t ntohs(uint16_t ns)
 {
-#ifdef CONFIG_ENDIAN_BIG
-  return ns;
-#else
-  return HTONS(ns);
-#endif
+  return NTOHS(ns);
 }
diff --git a/libs/libc/string/lib_ffs.c b/libs/libc/string/lib_ffs.c
index 1b42c4fc0a..30f185b038 100644
--- a/libs/libc/string/lib_ffs.c
+++ b/libs/libc/string/lib_ffs.c
@@ -55,7 +55,9 @@ int ffs(int j)
 
   if (j != 0)
     {
-#ifdef CONFIG_HAVE_BUILTIN_CTZ
+#ifdef CONFIG_HAVE_BUILTIN_FFS
+      ret = __builtin_ffs(j);
+#elif defined (CONFIG_HAVE_BUILTIN_CTZ)
       /* Count trailing zeros function can be used to implement ffs. */
 
       ret = __builtin_ctz(j) + 1;
diff --git a/libs/libc/string/lib_ffsl.c b/libs/libc/string/lib_ffsl.c
index a5b5f877e6..63acc95064 100644
--- a/libs/libc/string/lib_ffsl.c
+++ b/libs/libc/string/lib_ffsl.c
@@ -55,7 +55,9 @@ int ffsl(long j)
 
   if (j != 0)
     {
-#ifdef CONFIG_HAVE_BUILTIN_CTZ
+#ifdef CONFIG_HAVE_BUILTIN_FFSL
+      ret = __builtin_ffsl(j);
+#elif defined (CONFIG_HAVE_BUILTIN_CTZ)
       /* Count trailing zeros function can be used to implement ffs. */
 
       ret = __builtin_ctzl(j) + 1;
diff --git a/libs/libc/string/lib_ffsll.c b/libs/libc/string/lib_ffsll.c
index d2d71b485c..6fc45a880e 100644
--- a/libs/libc/string/lib_ffsll.c
+++ b/libs/libc/string/lib_ffsll.c
@@ -57,7 +57,9 @@ int ffsll(long long j)
 
   if (j != 0)
     {
-#ifdef CONFIG_HAVE_BUILTIN_CTZ
+#ifdef CONFIG_HAVE_BUILTIN_FFSLL
+      ret = __builtin_ffsll(j);
+#elif defined (CONFIG_HAVE_BUILTIN_CTZ)
       /* Count trailing zeros function can be used to implement ffs. */
 
       ret = __builtin_ctzll(j) + 1;