]> wimlib.net Git - wimlib/blobdiff - include/wimlib/endianness.h
mount_image.c: add fallback definitions of RENAME_* constants
[wimlib] / include / wimlib / endianness.h
index ed0b7ec443eb26cf679810a26820278306d6db25..d72d50a940ecc0db2d7809f5c3b72b996ea9eb91 100644 (file)
@@ -1,21 +1,28 @@
 /*
  * endianness.h - macros and inline functions for endianness conversion
  *
- * The following copying information applies to this specific source code file:
+ * Copyright 2022 Eric Biggers
  *
- * Written in 2014-2015 by Eric Biggers <ebiggers3@gmail.com>
+ * 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:
  *
- * To the extent possible under law, the author(s) have dedicated all copyright
- * and related and neighboring rights to this software to the public domain
- * worldwide via the Creative Commons Zero 1.0 Universal Public Domain
- * Dedication (the "CC0").
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
  *
- * This software 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 CC0 for more details.
- *
- * You should have received a copy of the CC0 along with this software; if not
- * see <http://creativecommons.org/publicdomain/zero/1.0/>.
+ * 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
 
@@ -49,8 +64,8 @@
 
 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 bswap16_const(n);
 #endif
@@ -58,8 +73,8 @@ static forceinline u16 do_bswap16(u16 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 bswap32_const(n);
 #endif
@@ -67,8 +82,8 @@ static forceinline u32 do_bswap32(u32 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 bswap64_const(n);
 #endif
@@ -78,7 +93,7 @@ static forceinline u64 do_bswap64(u64 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
+#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))