slide_hash_simd.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* slide_hash_simd.h
  2. *
  3. * Copyright 2022 The Chromium Authors. All rights reserved.
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the Chromium source repository LICENSE file.
  6. */
  7. #ifndef SLIDE_HASH_SIMD_H
  8. #define SLIDE_HASH_SIMD_H
  9. #include "deflate.h"
  10. #ifndef INLINE
  11. #if defined(_MSC_VER) && !defined(__clang__)
  12. #define INLINE __inline
  13. #else
  14. #define INLINE inline
  15. #endif
  16. #endif
  17. #if defined(CPU_NO_SIMD)
  18. #error SIMD has been disabled for your build target
  19. #elif defined(DEFLATE_SLIDE_HASH_SSE2)
  20. #include <emmintrin.h> /* SSE2 */
  21. #define Z_SLIDE_INIT_SIMD(wsize) _mm_set1_epi16((ush)(wsize))
  22. #define Z_SLIDE_HASH_SIMD(table, size, vector_wsize) \
  23. for (const Posf* const end = table + size; table != end;) { \
  24. __m128i vO = _mm_loadu_si128((__m128i *)(table + 0)); \
  25. vO = _mm_subs_epu16(vO, vector_wsize); \
  26. _mm_storeu_si128((__m128i *)(table + 0), vO); \
  27. table += 8; \
  28. }
  29. typedef __m128i z_vec128i_u16x8_t;
  30. #elif defined(DEFLATE_SLIDE_HASH_NEON)
  31. #include <arm_neon.h> /* NEON */
  32. #define Z_SLIDE_INIT_SIMD(wsize) vdupq_n_u16((ush)(wsize))
  33. #define Z_SLIDE_HASH_SIMD(table, size, vector_wsize) \
  34. for (const Posf* const end = table + size; table != end;) { \
  35. uint16x8_t vO = vld1q_u16(table + 0); \
  36. uint16x8_t v8 = vld1q_u16(table + 8); \
  37. vO = vqsubq_u16(vO, vector_wsize); \
  38. v8 = vqsubq_u16(v8, vector_wsize); \
  39. vst1q_u16(table + 0, vO); \
  40. vst1q_u16(table + 8, v8); \
  41. table += 8 + 8; \
  42. }
  43. typedef uint16x8_t z_vec128i_u16x8_t;
  44. #else
  45. #error slide_hash_simd is not defined for your build target
  46. #endif
  47. /* ===========================================================================
  48. * Slide the hash table when sliding the window down (could be avoided with 32
  49. * bit values at the expense of memory usage). We slide even when level == 0 to
  50. * keep the hash table consistent if we switch back to level > 0 later.
  51. */
  52. local INLINE void slide_hash_simd(
  53. Posf *head, Posf *prev, const uInt w_size, const uInt hash_size) {
  54. /*
  55. * The SIMD implementation of the hash table slider assumes:
  56. *
  57. * 1. hash chain offset is 2 bytes. Should be true as Pos is "ush" type.
  58. */
  59. Assert(sizeof(Pos) == 2, "Pos type size error: should be 2 bytes");
  60. Assert(sizeof(ush) == 2, "ush type size error: should be 2 bytes");
  61. Assert(hash_size <= (1 << 16), "Hash table maximum size error");
  62. Assert(hash_size >= (1 << 8), "Hash table minimum size error");
  63. Assert(w_size == (ush)w_size, "Prev table size error");
  64. /*
  65. * 2. The hash & prev table sizes are a multiple of 32 bytes (256 bits),
  66. * since the NEON table slider moves two 128-bit items per loop (loop is
  67. * unrolled on NEON for performance, see http://crbug.com/863257).
  68. */
  69. Assert(!((hash_size * sizeof(head[0])) & (32 - 1)),
  70. "Hash table size error: should be a multiple of 32 bytes");
  71. Assert(!((w_size * sizeof(prev[0])) & (32 - 1)),
  72. "Prev table size error: should be a multiple of 32 bytes");
  73. /*
  74. * Duplicate (ush)w_size in each uint16_t component of a 128-bit vector.
  75. */
  76. const z_vec128i_u16x8_t vec_wsize = Z_SLIDE_INIT_SIMD(w_size);
  77. /*
  78. * Slide {head,prev} hash chain values: subtracts (ush)w_size from every
  79. * value with a saturating SIMD subtract, to clamp the result to 0(NIL),
  80. * to implement slide_hash() `(m >= wsize ? m - wsize : NIL);` code.
  81. */
  82. Z_SLIDE_HASH_SIMD(head, hash_size, vec_wsize);
  83. #ifndef FASTEST
  84. Z_SLIDE_HASH_SIMD(prev, w_size, vec_wsize);
  85. #endif
  86. }
  87. #undef z_vec128i_u16x8_t
  88. #undef Z_SLIDE_HASH_SIMD
  89. #undef Z_SLIDE_INIT_SIMD
  90. #endif /* SLIDE_HASH_SIMD_H */