chunkset_avx2.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* chunkset_avx2.c -- AVX2 inline functions to copy small data chunks.
  2. * For conditions of distribution and use, see copyright notice in zlib.h
  3. */
  4. #include "zbuild.h"
  5. #ifdef X86_AVX2
  6. #include <immintrin.h>
  7. #include "../generic/chunk_permute_table.h"
  8. typedef __m256i chunk_t;
  9. #define CHUNK_SIZE 32
  10. #define HAVE_CHUNKMEMSET_2
  11. #define HAVE_CHUNKMEMSET_4
  12. #define HAVE_CHUNKMEMSET_8
  13. #define HAVE_CHUNK_MAG
  14. /* Populate don't cares so that this is a direct lookup (with some indirection into the permute table), because dist can
  15. * never be 0 - 2, we'll start with an offset, subtracting 3 from the input */
  16. static const lut_rem_pair perm_idx_lut[29] = {
  17. { 0, 2}, /* 3 */
  18. { 0, 0}, /* don't care */
  19. { 1 * 32, 2}, /* 5 */
  20. { 2 * 32, 2}, /* 6 */
  21. { 3 * 32, 4}, /* 7 */
  22. { 0 * 32, 0}, /* don't care */
  23. { 4 * 32, 5}, /* 9 */
  24. { 5 * 32, 22}, /* 10 */
  25. { 6 * 32, 21}, /* 11 */
  26. { 7 * 32, 20}, /* 12 */
  27. { 8 * 32, 6}, /* 13 */
  28. { 9 * 32, 4}, /* 14 */
  29. {10 * 32, 2}, /* 15 */
  30. { 0 * 32, 0}, /* don't care */
  31. {11 * 32, 15}, /* 17 */
  32. {11 * 32 + 16, 14}, /* 18 */
  33. {11 * 32 + 16 * 2, 13}, /* 19 */
  34. {11 * 32 + 16 * 3, 12}, /* 20 */
  35. {11 * 32 + 16 * 4, 11}, /* 21 */
  36. {11 * 32 + 16 * 5, 10}, /* 22 */
  37. {11 * 32 + 16 * 6, 9}, /* 23 */
  38. {11 * 32 + 16 * 7, 8}, /* 24 */
  39. {11 * 32 + 16 * 8, 7}, /* 25 */
  40. {11 * 32 + 16 * 9, 6}, /* 26 */
  41. {11 * 32 + 16 * 10, 5}, /* 27 */
  42. {11 * 32 + 16 * 11, 4}, /* 28 */
  43. {11 * 32 + 16 * 12, 3}, /* 29 */
  44. {11 * 32 + 16 * 13, 2}, /* 30 */
  45. {11 * 32 + 16 * 14, 1} /* 31 */
  46. };
  47. static inline void chunkmemset_2(uint8_t *from, chunk_t *chunk) {
  48. int16_t tmp;
  49. memcpy(&tmp, from, sizeof(tmp));
  50. *chunk = _mm256_set1_epi16(tmp);
  51. }
  52. static inline void chunkmemset_4(uint8_t *from, chunk_t *chunk) {
  53. int32_t tmp;
  54. memcpy(&tmp, from, sizeof(tmp));
  55. *chunk = _mm256_set1_epi32(tmp);
  56. }
  57. static inline void chunkmemset_8(uint8_t *from, chunk_t *chunk) {
  58. int64_t tmp;
  59. memcpy(&tmp, from, sizeof(tmp));
  60. *chunk = _mm256_set1_epi64x(tmp);
  61. }
  62. static inline void loadchunk(uint8_t const *s, chunk_t *chunk) {
  63. *chunk = _mm256_loadu_si256((__m256i *)s);
  64. }
  65. static inline void storechunk(uint8_t *out, chunk_t *chunk) {
  66. _mm256_storeu_si256((__m256i *)out, *chunk);
  67. }
  68. static inline chunk_t GET_CHUNK_MAG(uint8_t *buf, uint32_t *chunk_rem, uint32_t dist) {
  69. lut_rem_pair lut_rem = perm_idx_lut[dist - 3];
  70. __m256i ret_vec;
  71. /* While technically we only need to read 4 or 8 bytes into this vector register for a lot of cases, GCC is
  72. * compiling this to a shared load for all branches, preferring the simpler code. Given that the buf value isn't in
  73. * GPRs to begin with the 256 bit load is _probably_ just as inexpensive */
  74. *chunk_rem = lut_rem.remval;
  75. /* See note in chunkset_ssse3.c for why this is ok */
  76. __msan_unpoison(buf + dist, 32 - dist);
  77. if (dist < 16) {
  78. /* This simpler case still requires us to shuffle in 128 bit lanes, so we must apply a static offset after
  79. * broadcasting the first vector register to both halves. This is _marginally_ faster than doing two separate
  80. * shuffles and combining the halves later */
  81. const __m256i permute_xform =
  82. _mm256_setr_epi8(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  83. 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16);
  84. __m256i perm_vec = _mm256_load_si256((__m256i*)(permute_table+lut_rem.idx));
  85. __m128i ret_vec0 = _mm_loadu_si128((__m128i*)buf);
  86. perm_vec = _mm256_add_epi8(perm_vec, permute_xform);
  87. ret_vec = _mm256_inserti128_si256(_mm256_castsi128_si256(ret_vec0), ret_vec0, 1);
  88. ret_vec = _mm256_shuffle_epi8(ret_vec, perm_vec);
  89. } else if (dist == 16) {
  90. __m128i ret_vec0 = _mm_loadu_si128((__m128i*)buf);
  91. return _mm256_inserti128_si256(_mm256_castsi128_si256(ret_vec0), ret_vec0, 1);
  92. } else {
  93. __m128i ret_vec0 = _mm_loadu_si128((__m128i*)buf);
  94. __m128i ret_vec1 = _mm_loadu_si128((__m128i*)(buf + 16));
  95. /* Take advantage of the fact that only the latter half of the 256 bit vector will actually differ */
  96. __m128i perm_vec1 = _mm_load_si128((__m128i*)(permute_table + lut_rem.idx));
  97. __m128i xlane_permutes = _mm_cmpgt_epi8(_mm_set1_epi8(16), perm_vec1);
  98. __m128i xlane_res = _mm_shuffle_epi8(ret_vec0, perm_vec1);
  99. /* Since we can't wrap twice, we can simply keep the later half exactly how it is instead of having to _also_
  100. * shuffle those values */
  101. __m128i latter_half = _mm_blendv_epi8(ret_vec1, xlane_res, xlane_permutes);
  102. ret_vec = _mm256_inserti128_si256(_mm256_castsi128_si256(ret_vec0), latter_half, 1);
  103. }
  104. return ret_vec;
  105. }
  106. #define CHUNKSIZE chunksize_avx2
  107. #define CHUNKCOPY chunkcopy_avx2
  108. #define CHUNKUNROLL chunkunroll_avx2
  109. #define CHUNKMEMSET chunkmemset_avx2
  110. #define CHUNKMEMSET_SAFE chunkmemset_safe_avx2
  111. #include "chunkset_tpl.h"
  112. #define INFLATE_FAST inflate_fast_avx2
  113. #include "inffast_tpl.h"
  114. #endif