Commit fd3c715 for zlib
commit fd3c7155de0e7cae0f129e2fc2fa4d2df07f170d
Author: Mark Adler <git@madler.net>
Date: Sun Nov 15 21:07:32 2026 -0800
Use a byte swap for inflate_fast() loads on big-endian machines.
Use __builtin_bswap64 where available to make 64-bit loads faster
on big-endian machines. Without this, the bytes were loaded one at
a time in the "fast" loop, which wasn't fast.
diff --git a/inffast.c b/inffast.c
index 2fde961..8a8d356 100644
--- a/inffast.c
+++ b/inffast.c
@@ -21,9 +21,20 @@
"inflate: literal '%c'\n" : \
"inflate: literal 0x%02x\n", (here)->val))
-/* The eight bytes at p, little-endian: one load on a little-endian machine,
- the bytes one by one on big-endian, though the compiler should turn that
- into a single load and a byte swap. */
+/* A byte swap of a 64-bit word, where the compiler has one. */
+#if defined(__has_builtin)
+# if __has_builtin(__builtin_bswap64)
+# define Z_BSWAP64 __builtin_bswap64
+# endif
+#elif defined(__GNUC__) && \
+ (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
+# define Z_BSWAP64 __builtin_bswap64
+#endif
+
+/* Return the eight bytes at p in little-endian order. This should compile to
+ one load on little-endian, or to one load and a swap (sometimes one
+ instruction) on big-endian. Either way, inlined with no function call and no
+ if condition surviving. */
local Z_U8 load64(z_const unsigned char FAR *p) {
Z_U8 v = 1;
@@ -31,6 +42,10 @@ local Z_U8 load64(z_const unsigned char FAR *p) {
zmemcpy(&v, p, 8);
return v;
}
+#ifdef Z_BSWAP64
+ zmemcpy(&v, p, 8);
+ return Z_BSWAP64(v);
+#endif
return (Z_U8)p[0] | ((Z_U8)p[1] << 8) | ((Z_U8)p[2] << 16) |
((Z_U8)p[3] << 24) | ((Z_U8)p[4] << 32) | ((Z_U8)p[5] << 40) |
((Z_U8)p[6] << 48) | ((Z_U8)p[7] << 56);