]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Introduce zmemcpy to use unaligned access for architectures we know support unaligned...
authorNathan Moinvaziri <nathan@nathanm.com>
Sun, 9 Jan 2022 23:01:23 +0000 (15:01 -0800)
committerHans Kristian Rosbach <hk-github@circlestorm.org>
Thu, 10 Feb 2022 15:10:48 +0000 (16:10 +0100)
arch/arm/chunkset_neon.c
arch/power/chunkset_power8.c
chunkset.c
chunkset_tpl.h
compare256.c
deflate.h
inffast.c
insert_string_tpl.h
match_tpl.h
zbuild.h

index 1e173b90758375290733617ec741647d9549a7c6..2ca8ce07e5a69058e7434cfd7c64ab30a9137983 100644 (file)
@@ -25,19 +25,19 @@ static inline void chunkmemset_1(uint8_t *from, chunk_t *chunk) {
 
 static inline void chunkmemset_2(uint8_t *from, chunk_t *chunk) {
     uint16_t tmp;
-    memcpy(&tmp, from, 2);
+    zmemcpy_2(&tmp, from);
     *chunk = vreinterpretq_u8_u16(vdupq_n_u16(tmp));
 }
 
 static inline void chunkmemset_4(uint8_t *from, chunk_t *chunk) {
     uint32_t tmp;
-    memcpy(&tmp, from, 4);
+    zmemcpy_4(&tmp, from);
     *chunk = vreinterpretq_u8_u32(vdupq_n_u32(tmp));
 }
 
 static inline void chunkmemset_8(uint8_t *from, chunk_t *chunk) {
     uint64_t tmp;
-    memcpy(&tmp, from, 8);
+    zmemcpy_8(&tmp, from);
     *chunk = vreinterpretq_u8_u64(vdupq_n_u64(tmp));
 }
 
index e66de9ed295f69386b306e47fc4f06a0ccb88be9..52734cb2dfe217d0f5a61a8719a83f813b4d3091 100644 (file)
@@ -21,19 +21,19 @@ static inline void chunkmemset_1(uint8_t *from, chunk_t *chunk) {
 
 static inline void chunkmemset_2(uint8_t *from, chunk_t *chunk) {
     uint16_t tmp;
-    memcpy(&tmp, from, 2);
+    zmemcpy_2(&tmp, from);
     *chunk = (vector unsigned char)vec_splats(tmp);
 }
 
 static inline void chunkmemset_4(uint8_t *from, chunk_t *chunk) {
     uint32_t tmp;
-    memcpy(&tmp, from, 4);
+    zmemcpy_4(&tmp, from);
     *chunk = (vector unsigned char)vec_splats(tmp);
 }
 
 static inline void chunkmemset_8(uint8_t *from, chunk_t *chunk) {
     uint64_t tmp;
-    memcpy(&tmp, from, 8);
+    zmemcpy_8(&tmp, from);
     *chunk = (vector unsigned char)vec_splats(tmp);
 }
 
index cfc6e87f8b05f9aa02f83d62425655c21a9c7520..14c913ee6a90466bc7d1a1b0723cdcf39ecfe8f7 100644 (file)
@@ -18,20 +18,20 @@ static inline void chunkmemset_1(uint8_t *from, chunk_t *chunk) {
 
 static inline void chunkmemset_4(uint8_t *from, chunk_t *chunk) {
     uint8_t *dest = (uint8_t *)chunk;
-    memcpy(dest, from, sizeof(uint32_t));
-    memcpy(dest+4, from, sizeof(uint32_t));
+    zmemcpy_4(dest, from);
+    zmemcpy_4(dest+4, from);
 }
 
 static inline void chunkmemset_8(uint8_t *from, chunk_t *chunk) {
-    memcpy(chunk, from, sizeof(uint64_t));
+    zmemcpy_8(chunk, from);
 }
 
 static inline void loadchunk(uint8_t const *s, chunk_t *chunk) {
-    chunkmemset_8((uint8_t *)s, chunk);
+    zmemcpy_8(chunk, (uint8_t *)s);
 }
 
 static inline void storechunk(uint8_t *out, chunk_t *chunk) {
-    memcpy(out, chunk, sizeof(uint64_t));
+    zmemcpy_8(out, chunk);
 }
 
 #define CHUNKSIZE        chunksize_c
index dced2895d8a4a81f8711a8d85e63c656077a6689..8e6f566443c9861cd33fad09ae93dc7bb875ca19 100644 (file)
@@ -60,20 +60,20 @@ Z_INTERNAL uint8_t* CHUNKCOPY_SAFE(uint8_t *out, uint8_t const *from, unsigned l
 #endif
 #if CHUNK_SIZE >= 8
     while (len >= 8) {
-        memcpy(out, from, 8);
+        zmemcpy_8(out, from);
         out += 8;
         from += 8;
         len -= 8;
     }
 #endif
     if (len >= 4) {
-        memcpy(out, from, 4);
+        zmemcpy_4(out, from);
         out += 4;
         from += 4;
         len -= 4;
     }
     if (len >= 2) {
-        memcpy(out, from, 2);
+        zmemcpy_2(out, from);
         out += 2;
         from += 2;
         len -= 2;
index 68c0cf54315e9d479a8a43d4d64f5649c1611e0c..1b693f274f0ec09ae1f6cf818f861b769e45b1a9 100644 (file)
@@ -101,8 +101,8 @@ static inline uint32_t compare256_unaligned_32_static(const uint8_t *src0, const
     do {
         uint32_t sv, mv, diff;
 
-        memcpy(&sv, src0, sizeof(sv));
-        memcpy(&mv, src1, sizeof(mv));
+        zmemcpy_4(&sv, src0);
+        zmemcpy_4(&mv, src1);
 
         diff = sv ^ mv;
         if (diff) {
@@ -141,8 +141,8 @@ static inline uint32_t compare256_unaligned_64_static(const uint8_t *src0, const
     do {
         uint64_t sv, mv, diff;
 
-        memcpy(&sv, src0, sizeof(sv));
-        memcpy(&mv, src1, sizeof(mv));
+        zmemcpy_8(&sv, src0);
+        zmemcpy_8(&mv, src1);
 
         diff = sv ^ mv;
         if (diff) {
index 7f4eb43f9f47c0708a78bd6fdd0697ae46146ebe..87108914eeb08aa4ed0fe1983f8d1420f7585e86 100644 (file)
--- a/deflate.h
+++ b/deflate.h
@@ -305,7 +305,7 @@ static inline void put_short(deflate_state *s, uint16_t w) {
 #if BYTE_ORDER == BIG_ENDIAN
     w = ZSWAP16(w);
 #endif
-    memcpy(&s->pending_buf[s->pending], &w, sizeof(w));
+    zmemcpy_2(&s->pending_buf[s->pending], &w);
     s->pending += 2;
 }
 
@@ -317,7 +317,7 @@ static inline void put_short_msb(deflate_state *s, uint16_t w) {
 #if BYTE_ORDER == LITTLE_ENDIAN
     w = ZSWAP16(w);
 #endif
-    memcpy(&s->pending_buf[s->pending], &w, sizeof(w));
+    zmemcpy_2(&s->pending_buf[s->pending], &w);
     s->pending += 2;
 }
 
@@ -329,7 +329,7 @@ static inline void put_uint32(deflate_state *s, uint32_t dw) {
 #if BYTE_ORDER == BIG_ENDIAN
     dw = ZSWAP32(dw);
 #endif
-    memcpy(&s->pending_buf[s->pending], &dw, sizeof(dw));
+    zmemcpy_4(&s->pending_buf[s->pending], &dw);
     s->pending += 4;
 }
 
@@ -341,7 +341,7 @@ static inline void put_uint32_msb(deflate_state *s, uint32_t dw) {
 #if BYTE_ORDER == LITTLE_ENDIAN
     dw = ZSWAP32(dw);
 #endif
-    memcpy(&s->pending_buf[s->pending], &dw, sizeof(dw));
+    zmemcpy_4(&s->pending_buf[s->pending], &dw);
     s->pending += 4;
 }
 
@@ -353,7 +353,7 @@ static inline void put_uint64(deflate_state *s, uint64_t lld) {
 #if BYTE_ORDER == BIG_ENDIAN
     lld = ZSWAP64(lld);
 #endif
-    memcpy(&s->pending_buf[s->pending], &lld, sizeof(lld));
+    zmemcpy_8(&s->pending_buf[s->pending], &lld);
     s->pending += 8;
 }
 
index 899534f0dc0f6acd2580b0a39982112611b36d0f..58cbad7ef915ce5b9cfe85f8677000f3a501caef 100644 (file)
--- a/inffast.c
+++ b/inffast.c
@@ -15,7 +15,7 @@
 /* Load 64 bits from IN and place the bytes at offset BITS in the result. */
 static inline uint64_t load_64_bits(const unsigned char *in, unsigned bits) {
     uint64_t chunk;
-    memcpy(&chunk, in, sizeof(chunk));
+    zmemcpy_8(&chunk, in);
 
 #if BYTE_ORDER == LITTLE_ENDIAN
     return chunk << bits;
index 7d3e46c897e21bc0bd772715ef65f769b8175a07..643a5e0e316a0fc13844304d3f1e3184db7f6931 100644 (file)
@@ -29,9 +29,9 @@
 #  define HASH_CALC_MASK HASH_MASK
 #endif
 #ifndef HASH_CALC_READ
-#  ifdef UNALIGNED_OK
+#  if BYTE_ORDER == LITTLE_ENDIAN
 #    define HASH_CALC_READ \
-        memcpy(&val, strstart, sizeof(val));
+        zmemcpy_4(&val, strstart);
 #  else
 #    define HASH_CALC_READ \
         val  = ((uint32_t)(strstart[0])); \
index 853a4ad32ffec4f9f5a5466ef5aa69ec077a4e84..49b9e724aaec7cca54fdbcc318b700f99301d591 100644 (file)
@@ -74,11 +74,11 @@ Z_INTERNAL uint32_t LONGEST_MATCH(deflate_state *const s, Pos cur_match) {
 #endif
 
 #ifdef UNALIGNED64_OK
-    memcpy(scan_start, scan, sizeof(uint64_t));
-    memcpy(scan_end, scan+offset, sizeof(uint64_t));
+    zmemcpy_8(scan_start, scan);
+    zmemcpy_8(scan_end, scan+offset);
 #elif defined(UNALIGNED_OK)
-    memcpy(scan_start, scan, sizeof(uint32_t));
-    memcpy(scan_end, scan+offset, sizeof(uint32_t));
+    zmemcpy_4(scan_start, scan);
+    zmemcpy_4(scan_end, scan+offset);
 #else
     scan_end[0] = *(scan+offset);
     scan_end[1] = *(scan+offset+1);
@@ -201,9 +201,9 @@ Z_INTERNAL uint32_t LONGEST_MATCH(deflate_state *const s, Pos cur_match) {
 #endif
 
 #ifdef UNALIGNED64_OK
-            memcpy(scan_end, scan+offset, sizeof(uint64_t));
+            zmemcpy_8(scan_end, scan+offset);
 #elif defined(UNALIGNED_OK)
-            memcpy(scan_end, scan+offset, sizeof(uint32_t));
+            zmemcpy_4(scan_end, scan+offset);
 #else
             scan_end[0] = *(scan+offset);
             scan_end[1] = *(scan+offset+1);
index d2b6a922ddc1c57487a905fb61a387fb6ba3eafe..5789eb06d5bc58530f9dd1d04d72647383706974 100644 (file)
--- a/zbuild.h
+++ b/zbuild.h
 #  define Tracecv(c, x)
 #endif
 
+#ifdef UNALIGNED_OK
+#  define zmemcpy_2(dest, src) *((uint16_t *)dest) = *((uint16_t *)src)
+#  define zmemcpy_4(dest, src) *((uint32_t *)dest) = *((uint32_t *)src)
+#  if UINTPTR_MAX == UINT64_MAX
+#    define zmemcpy_8(dest, src) *((uint64_t *)dest) = *((uint64_t *)src)
+#  else
+#    define zmemcpy_8(dest, src) ((uint32_t *)dest)[0] = *((uint32_t *)src)[0] \
+                                 ((uint32_t *)dest)[1] = *((uint32_t *)src)[1]
+#  endif
+#else
+#  define zmemcpy_2(dest, src) memcpy(dest, src, 2)
+#  define zmemcpy_4(dest, src) memcpy(dest, src, 4)
+#  define zmemcpy_8(dest, src) memcpy(dest, src, 8)
+#endif
+
 #endif