]> Savannah Git Hosting - gnulib.git/commitdiff
byteswap: Use inline functions instead of macros.
authorCollin Funk <collin.funk1@gmail.com>
Fri, 17 May 2024 03:37:14 +0000 (20:37 -0700)
committerCollin Funk <collin.funk1@gmail.com>
Fri, 17 May 2024 03:37:14 +0000 (20:37 -0700)
* lib/byteswap.c: New file.
* lib/byteswap.in.h (bswap_16, bswap_32, bswap_64): Use inline functions
instead of macros.
* m4/byteswap.m4 (gl_BYTESWAP): Check that bswap functions can be used
on double values.
* modules/byteswap (Files): Add lib/byteswap.c.
(Depends-on): Add extern-inline and stdint as conditional dependencies.
(Makefile.am): Add lib/byteswap.c to lib_SOURCES.

ChangeLog
lib/byteswap.c [new file with mode: 0644]
lib/byteswap.in.h
m4/byteswap.m4
modules/byteswap

index ba52fbcdf18b054df7e0061667fb810d514b7afd..ddfe701dd408d1b81845346f305fa002788fde16 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2024-05-16  Collin Funk  <collin.funk1@gmail.com>
+
+       byteswap: Use inline functions instead of macros.
+       * lib/byteswap.c: New file.
+       * lib/byteswap.in.h (bswap_16, bswap_32, bswap_64): Use inline functions
+       instead of macros.
+       * m4/byteswap.m4 (gl_BYTESWAP): Check that bswap functions can be used
+       on double values.
+       * modules/byteswap (Files): Add lib/byteswap.c.
+       (Depends-on): Add extern-inline and stdint as conditional dependencies.
+       (Makefile.am): Add lib/byteswap.c to lib_SOURCES.
+
 2024-05-16  Collin Funk  <collin.funk1@gmail.com>
 
        gnulib-tool.py: Fix return value when exiting with Ctrl-C.
diff --git a/lib/byteswap.c b/lib/byteswap.c
new file mode 100644 (file)
index 0000000..6e3dad7
--- /dev/null
@@ -0,0 +1,21 @@
+/* Inline functions for <byteswap.h>.
+
+   Copyright 2024 Free Software Foundation, Inc.
+
+   This file is free software: you can redistribute it and/or modify
+   it under the terms of the GNU Lesser General Public License as
+   published by the Free Software Foundation; either version 2.1 of the
+   License, or (at your option) any later version.
+
+   This file is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public License
+   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
+
+#include <config.h>
+
+#define _GL_BYTESWAP_INLINE _GL_EXTERN_INLINE
+#include <byteswap.h>
index 8e49efad05adfb39e38cce1523af817526735d53..cfa3f04031b9232bd4022246ca98837b349c7160 100644 (file)
    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
 
 #ifndef _GL_BYTESWAP_H
-#define _GL_BYTESWAP_H
+#define _GL_BYTESWAP_H 1
+
+/* This file uses _GL_INLINE.  */
+#if !_GL_CONFIG_H_INCLUDED
+ #error "Please include config.h first."
+#endif
+
+#include <stdint.h>
+
+_GL_INLINE_HEADER_BEGIN
+#ifndef _GL_BYTESWAP_INLINE
+# define _GL_BYTESWAP_INLINE _GL_INLINE
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
 
 /* Given an unsigned 16-bit argument X, return the value corresponding to
    X with reversed byte order.  */
-#define bswap_16(x) ((((x) & 0x00FF) << 8) | \
-                     (((x) & 0xFF00) >> 8))
+_GL_BYTESWAP_INLINE uint16_t
+bswap_16 (uint16_t x)
+{
+#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8))            \
+     || (defined __has_builtin && __has_builtin (__builtin_bswap16))
+  return __builtin_bswap16 (x);
+#else
+  return (((((x) >> 8) & 0xff) | (((x) & 0xff) << 8)));
+#endif
+}
 
 /* Given an unsigned 32-bit argument X, return the value corresponding to
    X with reversed byte order.  */
-#define bswap_32(x) ((((x) & 0x000000FF) << 24) | \
-                     (((x) & 0x0000FF00) << 8) | \
-                     (((x) & 0x00FF0000) >> 8) | \
-                     (((x) & 0xFF000000) >> 24))
+_GL_BYTESWAP_INLINE uint32_t
+bswap_32 (uint32_t x)
+{
+#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))            \
+     || (defined __has_builtin && __has_builtin (__builtin_bswap32))
+  return __builtin_bswap32 (x);
+#else
+  return ((((x) & 0xff000000u) >> 24) | (((x) & 0x00ff0000u) >> 8)
+          | (((x) & 0x0000ff00u) << 8) | (((x) & 0x000000ffu) << 24));
+#endif
+}
 
 /* Given an unsigned 64-bit argument X, return the value corresponding to
    X with reversed byte order.  */
-#define bswap_64(x) ((((x) & 0x00000000000000FFULL) << 56) | \
-                     (((x) & 0x000000000000FF00ULL) << 40) | \
-                     (((x) & 0x0000000000FF0000ULL) << 24) | \
-                     (((x) & 0x00000000FF000000ULL) << 8) | \
-                     (((x) & 0x000000FF00000000ULL) >> 8) | \
-                     (((x) & 0x0000FF0000000000ULL) >> 24) | \
-                     (((x) & 0x00FF000000000000ULL) >> 40) | \
-                     (((x) & 0xFF00000000000000ULL) >> 56))
+_GL_BYTESWAP_INLINE uint64_t
+bswap_64 (uint64_t x)
+{
+#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))            \
+     || (defined __has_builtin && __has_builtin (__builtin_bswap64))
+  return __builtin_bswap64 (x);
+#else
+  return ((((x) & 0xff00000000000000ull) >> 56)
+          | (((x) & 0x00ff000000000000ull) >> 40)
+          | (((x) & 0x0000ff0000000000ull) >> 24)
+          | (((x) & 0x000000ff00000000ull) >> 8)
+          | (((x) & 0x00000000ff000000ull) << 8)
+          | (((x) & 0x0000000000ff0000ull) << 24)
+          | (((x) & 0x000000000000ff00ull) << 40)
+          | (((x) & 0x00000000000000ffull) << 56));
+#endif
+}
+
+#ifdef __cplusplus
+}
+#endif
+
+_GL_INLINE_HEADER_END
 
 #endif /* _GL_BYTESWAP_H */
index 0c76fe9312d08f129fa031ae3f5d9ed5e81fc6dc..3f5ef45cfe6896b1f14c4705e80d3efe47ddca83 100644 (file)
@@ -1,5 +1,5 @@
 # byteswap.m4
-# serial 5
+# serial 6
 dnl Copyright (C) 2005, 2007, 2009-2024 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -10,9 +10,28 @@ dnl Written by Oskar Liljeblad.
 AC_DEFUN([gl_BYTESWAP],
 [
   dnl Prerequisites of lib/byteswap.in.h.
-  AC_CHECK_HEADERS([byteswap.h], [
+  AC_CHECK_HEADERS_ONCE([byteswap.h])
+  if test $ac_cv_header_byteswap_h = yes; then
+    AC_CACHE_CHECK([for working bswap_16, bswap_32, bswap_64],
+      [gl_cv_header_working_byteswap_h],
+      [gl_cv_header_working_byteswap_h=no
+       AC_COMPILE_IFELSE(
+         [AC_LANG_PROGRAM(
+            [[#include <byteswap.h>
+            ]],
+            [[int value_16 = bswap_16 (0.0);
+              int value_32 = bswap_32 (0.0);
+              int value_64 = bswap_64 (0.0);
+              return !(value_16 + value_32 + value_64);
+            ]])
+         ],
+         [gl_cv_header_working_byteswap_h=yes],
+         [gl_cv_header_working_byteswap_h=no])
+      ])
+  fi
+  if test $gl_cv_header_working_byteswap_h = yes; then
     GL_GENERATE_BYTESWAP_H=false
-  ], [
+  else
     GL_GENERATE_BYTESWAP_H=true
-  ])
+  fi
 ])
index 68fe6b78ae41990ea000cbd53a0702782d027b42..82fe424a614ab74a9ff37cb3c88c6ca83a4af23b 100644 (file)
@@ -3,10 +3,13 @@ Swap bytes of 16, 32 and 64 bit values.
 
 Files:
 lib/byteswap.in.h
+lib/byteswap.c
 m4/byteswap.m4
 
 Depends-on:
 gen-header
+extern-inline           [$GL_GENERATE_BYTESWAP_H]
+stdint                  [$GL_GENERATE_BYTESWAP_H]
 
 configure.ac:
 gl_BYTESWAP
@@ -23,6 +26,7 @@ byteswap.h: byteswap.in.h $(top_builddir)/config.status
 @NMD@  $(AM_V_GEN)$(MKDIR_P) '%reldir%'
        $(gl_V_at)$(SED_HEADER_TO_AT_t) $(srcdir)/byteswap.in.h
        $(AM_V_at)mv $@-t $@
+lib_SOURCES += byteswap.c
 else
 byteswap.h: $(top_builddir)/config.status
        rm -f $@