]> git.ipfire.org Git - thirdparty/xz.git/commitdiff
tuklib_integer: Rename bswapXX to byteswapXX
authorLasse Collin <lasse.collin@tukaani.org>
Thu, 25 Apr 2024 11:00:57 +0000 (14:00 +0300)
committerLasse Collin <lasse.collin@tukaani.org>
Thu, 25 Apr 2024 11:00:57 +0000 (14:00 +0300)
The __builtin_bswapXX from GCC and Clang are preferred when
they are available. This can allow compilers to emit the x86 MOVBE
instruction instead of doing a load + byteswap as two instructions
(which would happen if the byteswapping is done in inline asm).

bswap16, bswap32, and bswap64 exist in system headers on *BSDs
and Darwin. #defining bswap16 on NetBSD results in a warning about
macro redefinition. It's safest to avoid this namespace conflict
completely.

No OS supported by tuklib_integer.h uses byteswapXX names and
a web search doesn't immediately find any obvious danger of
namespace conflicts. So let's try these still-pretty-short names
for the macros.

Thanks to Sam James for pointing out the compiler warning on
NetBSD 10.0.

src/common/tuklib_integer.h
src/liblzma/check/crc32_fast.c
src/liblzma/check/crc32_tablegen.c
src/liblzma/check/crc64_fast.c
src/liblzma/check/crc64_tablegen.c

index c0004531a7105f168fe5dfb4e09bbbfb0b47d5c2..93483b5ca4320023ab35607bc58494a855aad813 100644 (file)
@@ -16,7 +16,7 @@
 ///
 /// Endianness-converting integer operations (these can be macros!)
 /// (XX = 16, 32, or 64; Y = b or l):
-///   - Byte swapping: bswapXX(num)
+///   - Byte swapping: byteswapXX(num)
 ///   - Byte order conversions to/from native (byteswaps if Y isn't
 ///     the native endianness): convXXYe(num)
 ///   - Unaligned reads: readXXYe(ptr)
 
 #if defined(HAVE___BUILTIN_BSWAPXX)
        // GCC >= 4.8 and Clang
-#      define bswap16(n) __builtin_bswap16(n)
-#      define bswap32(n) __builtin_bswap32(n)
-#      define bswap64(n) __builtin_bswap64(n)
+#      define byteswap16(num) __builtin_bswap16(num)
+#      define byteswap32(num) __builtin_bswap32(num)
+#      define byteswap64(num) __builtin_bswap64(num)
 
 #elif defined(HAVE_BYTESWAP_H)
        // glibc, uClibc, dietlibc
 #      include <byteswap.h>
 #      ifdef HAVE_BSWAP_16
-#              define bswap16(num) bswap_16(num)
+#              define byteswap16(num) bswap_16(num)
 #      endif
 #      ifdef HAVE_BSWAP_32
-#              define bswap32(num) bswap_32(num)
+#              define byteswap32(num) bswap_32(num)
 #      endif
 #      ifdef HAVE_BSWAP_64
-#              define bswap64(num) bswap_64(num)
+#              define byteswap64(num) bswap_64(num)
 #      endif
 
 #elif defined(HAVE_SYS_ENDIAN_H)
        // *BSDs and Darwin
 #      include <sys/endian.h>
+#      define byteswap16(num) bswap16(num)
+#      define byteswap32(num) bswap32(num)
+#      define byteswap64(num) bswap64(num)
 
 #elif defined(HAVE_SYS_BYTEORDER_H)
        // Solaris
 #      include <sys/byteorder.h>
 #      ifdef BSWAP_16
-#              define bswap16(num) BSWAP_16(num)
+#              define byteswap16(num) BSWAP_16(num)
 #      endif
 #      ifdef BSWAP_32
-#              define bswap32(num) BSWAP_32(num)
+#              define byteswap32(num) BSWAP_32(num)
 #      endif
 #      ifdef BSWAP_64
-#              define bswap64(num) BSWAP_64(num)
+#              define byteswap64(num) BSWAP_64(num)
 #      endif
 #      ifdef BE_16
 #              define conv16be(num) BE_16(num)
 #      endif
 #endif
 
-#ifndef bswap16
-#      define bswap16(n) (uint16_t)( \
+#ifndef byteswap16
+#      define byteswap16(n) (uint16_t)( \
                  (((n) & 0x00FFU) << 8) \
                | (((n) & 0xFF00U) >> 8) \
        )
 #endif
 
-#ifndef bswap32
-#      define bswap32(n) (uint32_t)( \
+#ifndef byteswap32
+#      define byteswap32(n) (uint32_t)( \
                  (((n) & UINT32_C(0x000000FF)) << 24) \
                | (((n) & UINT32_C(0x0000FF00)) << 8) \
                | (((n) & UINT32_C(0x00FF0000)) >> 8) \
        )
 #endif
 
-#ifndef bswap64
-#      define bswap64(n) (uint64_t)( \
+#ifndef byteswap64
+#      define byteswap64(n) (uint64_t)( \
                  (((n) & UINT64_C(0x00000000000000FF)) << 56) \
                | (((n) & UINT64_C(0x000000000000FF00)) << 40) \
                | (((n) & UINT64_C(0x0000000000FF0000)) << 24) \
 #              define conv64be(num) ((uint64_t)(num))
 #      endif
 #      ifndef conv16le
-#              define conv16le(num) bswap16(num)
+#              define conv16le(num) byteswap16(num)
 #      endif
 #      ifndef conv32le
-#              define conv32le(num) bswap32(num)
+#              define conv32le(num) byteswap32(num)
 #      endif
 #      ifndef conv64le
-#              define conv64le(num) bswap64(num)
+#              define conv64le(num) byteswap64(num)
 #      endif
 #else
 #      ifndef conv16be
-#              define conv16be(num) bswap16(num)
+#              define conv16be(num) byteswap16(num)
 #      endif
 #      ifndef conv32be
-#              define conv32be(num) bswap32(num)
+#              define conv32be(num) byteswap32(num)
 #      endif
 #      ifndef conv64be
-#              define conv64be(num) bswap64(num)
+#              define conv64be(num) byteswap64(num)
 #      endif
 #      ifndef conv16le
 #              define conv16le(num) ((uint16_t)(num))
index 103da947ff92785bd46cb741a07ef60b56df2ae1..16dbb7467513fb03c2fd94985f11f2cb06f1494a 100644 (file)
@@ -34,7 +34,7 @@ crc32_generic(const uint8_t *buf, size_t size, uint32_t crc)
        crc = ~crc;
 
 #ifdef WORDS_BIGENDIAN
-       crc = bswap32(crc);
+       crc = byteswap32(crc);
 #endif
 
        if (size > 8) {
@@ -80,7 +80,7 @@ crc32_generic(const uint8_t *buf, size_t size, uint32_t crc)
                crc = lzma_crc32_table[0][*buf++ ^ A(crc)] ^ S8(crc);
 
 #ifdef WORDS_BIGENDIAN
-       crc = bswap32(crc);
+       crc = byteswap32(crc);
 #endif
 
        return ~crc;
index 01047d3eca4716daa6488ad94f25582013d5571c..b8cf459f8e76693dd63c4a46756a046d2e40e209 100644 (file)
@@ -43,7 +43,7 @@ init_crc32_table(void)
 #ifdef WORDS_BIGENDIAN
        for (size_t s = 0; s < 8; ++s)
                for (size_t b = 0; b < 256; ++b)
-                       crc32_table[s][b] = bswap32(crc32_table[s][b]);
+                       crc32_table[s][b] = byteswap32(crc32_table[s][b]);
 #endif
 
        return;
index 1a1aedcbbf9ff7f69124783db89f67866424e4b3..0ce83fe4ad36915e9b0c44fa10dde66318e55166 100644 (file)
@@ -39,7 +39,7 @@ crc64_generic(const uint8_t *buf, size_t size, uint64_t crc)
        crc = ~crc;
 
 #ifdef WORDS_BIGENDIAN
-       crc = bswap64(crc);
+       crc = byteswap64(crc);
 #endif
 
        if (size > 4) {
@@ -73,7 +73,7 @@ crc64_generic(const uint8_t *buf, size_t size, uint64_t crc)
                crc = lzma_crc64_table[0][*buf++ ^ A1(crc)] ^ S8(crc);
 
 #ifdef WORDS_BIGENDIAN
-       crc = bswap64(crc);
+       crc = byteswap64(crc);
 #endif
 
        return ~crc;
index af93e011ca21d00f5bcbe2f2b21cf5f024482662..2035127a1123f3a0ef3bcabde5a35d1235de973c 100644 (file)
@@ -42,7 +42,7 @@ init_crc64_table(void)
 #ifdef WORDS_BIGENDIAN
        for (size_t s = 0; s < 4; ++s)
                for (size_t b = 0; b < 256; ++b)
-                       crc64_table[s][b] = bswap64(crc64_table[s][b]);
+                       crc64_table[s][b] = byteswap64(crc64_table[s][b]);
 #endif
 
        return;