/*
- * endianness.h
+ * endianness.h - macros and inline functions for endianness conversion
*
- * Macros and inline functions for endianness conversion.
+ * Copyright 2022 Eric Biggers
*
- * The author dedicates this file to the public domain.
- * You can do whatever you want with this file.
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef _WIMLIB_ENDIANNESS_H
#include "wimlib/compiler.h"
#include "wimlib/types.h"
+#ifdef HAVE_SYS_ENDIAN_H
+ /* Needed on NetBSD to stop system bswap macros from messing things up */
+# include <sys/endian.h>
+# undef bswap16
+# undef bswap32
+# undef bswap64
+#endif
+
/* Watch out for conflict with ntfs-3g/endians.h ... */
#ifndef _NTFS_ENDIANS_H
-static inline u16 bswap16(u16 n)
+#define bswap16_const(n) \
+ ((((u16)(n) & 0x00FF) << 8) | \
+ (((u16)(n) & 0xFF00) >> 8))
+
+#define bswap32_const(n) \
+ ((((u32)(n) & 0x000000FF) << 24) | \
+ (((u32)(n) & 0x0000FF00) << 8) | \
+ (((u32)(n) & 0x00FF0000) >> 8) | \
+ (((u32)(n) & 0xFF000000) >> 24))
+
+#define bswap64_const(n) \
+ ((((u64)(n) & 0x00000000000000FF) << 56) | \
+ (((u64)(n) & 0x000000000000FF00) << 40) | \
+ (((u64)(n) & 0x0000000000FF0000) << 24) | \
+ (((u64)(n) & 0x00000000FF000000) << 8) | \
+ (((u64)(n) & 0x000000FF00000000) >> 8) | \
+ (((u64)(n) & 0x0000FF0000000000) >> 24) | \
+ (((u64)(n) & 0x00FF000000000000) >> 40) | \
+ (((u64)(n) & 0xFF00000000000000) >> 56))
+
+static forceinline u16 do_bswap16(u16 n)
{
-#ifdef compiler_bswap16
- return compiler_bswap16(n);
+#if GCC_PREREQ(4, 8) || __has_builtin(__builtin_bswap16)
+ return __builtin_bswap16(n);
#else
- return (n << 8) | (n >> 8);
+ return bswap16_const(n);
#endif
}
-static inline u32 bswap32(u32 n)
+static forceinline u32 do_bswap32(u32 n)
{
-#ifdef compiler_bswap32
- return compiler_bswap32(n);
+#if GCC_PREREQ(4, 3) || __has_builtin(__builtin_bswap32)
+ return __builtin_bswap32(n);
#else
- return (n << 24) |
- ((n & 0xFF00) << 8) |
- ((n & 0xFF0000) >> 8) |
- (n >> 24);
+ return bswap32_const(n);
#endif
}
-static inline u64 bswap64(u64 n)
+static forceinline u64 do_bswap64(u64 n)
{
-#ifdef compiler_bswap64
- return compiler_bswap64(n);
+#if GCC_PREREQ(4, 3) || __has_builtin(__builtin_bswap64)
+ return __builtin_bswap64(n);
#else
- return (n << 56) |
- ((n & 0xFF00) << 40) |
- ((n & 0xFF0000) << 24) |
- ((n & 0xFF000000) << 8) |
- ((n & 0xFF00000000) >> 8) |
- ((n & 0xFF0000000000) >> 24) |
- ((n & 0xFF000000000000) >> 40) |
- (n >> 56);
+ return bswap64_const(n);
#endif
}
-#if CPU_IS_BIG_ENDIAN
+#define bswap16(n) (__builtin_constant_p(n) ? bswap16_const(n) : do_bswap16(n))
+#define bswap32(n) (__builtin_constant_p(n) ? bswap32_const(n) : do_bswap32(n))
+#define bswap64(n) (__builtin_constant_p(n) ? bswap64_const(n) : do_bswap64(n))
+
+#if CPU_IS_BIG_ENDIAN()
# define cpu_to_le16(n) ((_force_attr le16)bswap16(n))
# define cpu_to_le32(n) ((_force_attr le32)bswap32(n))
# define cpu_to_le64(n) ((_force_attr le64)bswap64(n))