* 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>
+
+ 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.
--- /dev/null
+/* 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>
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 */
# 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,
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
])
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
@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 $@