multiword_64_64_gcc_i386_mmx.cc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // Copyright 2010 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Implements multiword CRC for GCC on i386.
  15. #include "generic_crc.h"
  16. #if defined(__GNUC__) && CRCUTIL_USE_ASM && HAVE_I386 && HAVE_MMX
  17. namespace crcutil {
  18. template<> uint64 GenericCrc<uint64, uint64, uint64, 4>::CrcMultiwordI386Mmx(
  19. const void *data, size_t bytes, const uint64 &start)
  20. const GCC_OMIT_FRAME_POINTER;
  21. template<> uint64 GenericCrc<uint64, uint64, uint64, 4>::CrcMultiword(
  22. const void *data, size_t bytes, const uint64 &start) const {
  23. if (bytes <= 7) {
  24. const uint8 *src = static_cast<const uint8 *>(data);
  25. uint64 crc = start ^ this->Base().Canonize();
  26. for (const uint8 *end = src + bytes; src < end; ++src) {
  27. CRC_BYTE(this, crc, *src);
  28. }
  29. return (crc ^ this->Base().Canonize());
  30. }
  31. return CrcMultiwordI386Mmx(data, bytes, start);
  32. }
  33. #define CRC_WORD_MMX() \
  34. "pxor %[crc0], %[buf0]\n" \
  35. "movd %[buf0], %[tmp0]\n" \
  36. "psrlq $32, %[buf0]\n" \
  37. "movzbl %b[tmp0], %[temp]\n" \
  38. "shrl $8, %[tmp0]\n" \
  39. "movq (%[table], %[temp], 8), %[crc0]\n" \
  40. "movzbl %b[tmp0], %[temp]\n" \
  41. "shrl $8, %[tmp0]\n" \
  42. "pxor 1*256*8(%[table], %[temp], 8), %[crc0]\n" \
  43. "movzbl %b[tmp0], %[temp]\n" \
  44. "shrl $8, %[tmp0]\n" \
  45. "pxor 2*256*8(%[table], %[temp], 8), %[crc0]\n" \
  46. "pxor 3*256*8(%[table], %[tmp0], 8), %[crc0]\n" \
  47. "movd %[buf0], %[tmp0]\n" \
  48. "movzbl %b[tmp0], %[temp]\n" \
  49. "shrl $8, %[tmp0]\n" \
  50. "pxor 4*256*8(%[table], %[temp], 8), %[crc0]\n" \
  51. "movzbl %b[tmp0], %[temp]\n" \
  52. "shrl $8, %[tmp0]\n" \
  53. "pxor 5*256*8(%[table], %[temp], 8), %[crc0]\n" \
  54. "movzbl %b[tmp0], %[temp]\n" \
  55. "shrl $8, %[tmp0]\n" \
  56. "pxor 6*256*8(%[table], %[temp], 8), %[crc0]\n" \
  57. "pxor 7*256*8(%[table], %[tmp0], 8), %[crc0]\n"
  58. template<> uint64 GenericCrc<uint64, uint64, uint64, 4>::CrcMultiwordI386Mmx(
  59. const void *data, size_t bytes, const uint64 &start) const {
  60. const uint8 *src = static_cast<const uint8 *>(data);
  61. const uint8 *end = src + bytes;
  62. uint64 crc0 = start ^ this->Base().Canonize();
  63. ALIGN_ON_WORD_BOUNDARY_IF_NEEDED(bytes, this, src, end, crc0, uint64);
  64. if (src >= end) {
  65. return (crc0 ^ this->Base().Canonize());
  66. }
  67. uint64 crc1;
  68. uint64 crc2;
  69. uint64 crc3;
  70. uint64 buf0;
  71. uint64 buf1;
  72. uint64 buf2;
  73. uint64 buf3;
  74. uint32 tmp0;
  75. uint32 tmp1;
  76. uint32 tmp2;
  77. uint32 tmp3;
  78. uint32 temp;
  79. void *table_ptr;
  80. const uint64 *table_interleaved = &this->crc_word_interleaved_[0][0];
  81. const uint64 *table_word = &this->crc_word_[0][0];
  82. asm(
  83. "subl $2*4*8 - 1, %[end]\n"
  84. "cmpl %[src], %[end]\n"
  85. "jbe 2f\n"
  86. "pxor %[crc1], %[crc1]\n"
  87. "pxor %[crc2], %[crc2]\n"
  88. "pxor %[crc3], %[crc3]\n"
  89. "movq (%[src]), %[buf0]\n"
  90. "movq 1*8(%[src]), %[buf1]\n"
  91. "movq 2*8(%[src]), %[buf2]\n"
  92. "movq 3*8(%[src]), %[buf3]\n"
  93. "movl %[table_interleaved], %[table]\n"
  94. "1:\n"
  95. #if HAVE_SSE && CRCUTIL_PREFETCH_WIDTH > 0
  96. "prefetcht0 " TO_STRING(CRCUTIL_PREFETCH_WIDTH) "(%[src])\n"
  97. #endif
  98. "addl $0x20, %[src]\n"
  99. "pxor %[crc0], %[buf0]\n"
  100. "pxor %[crc1], %[buf1]\n"
  101. "pxor %[crc2], %[buf2]\n"
  102. "pxor %[crc3], %[buf3]\n"
  103. "movd %[buf0], %[tmp0]\n"
  104. "psrlq $32, %[buf0]\n"
  105. "movd %[buf1], %[tmp1]\n"
  106. "psrlq $32, %[buf1]\n"
  107. "movd %[buf2], %[tmp2]\n"
  108. "psrlq $32, %[buf2]\n"
  109. "movd %[buf3], %[tmp3]\n"
  110. "psrlq $32, %[buf3]\n"
  111. "movzbl %b[tmp0], %[temp]\n"
  112. "shrl $8, %[tmp0]\n"
  113. "movq (%[table], %[temp], 8), %[crc0]\n"
  114. "movzbl %b[tmp1], %[temp]\n"
  115. "shrl $8, %[tmp1]\n"
  116. "movq (%[table], %[temp], 8), %[crc1]\n"
  117. "movzbl %b[tmp2], %[temp]\n"
  118. "shrl $8, %[tmp2]\n"
  119. "movq (%[table], %[temp], 8), %[crc2]\n"
  120. "movzbl %b[tmp3], %[temp]\n"
  121. "shrl $8, %[tmp3]\n"
  122. "movq (%[table], %[temp], 8), %[crc3]\n"
  123. #define XOR(byte) \
  124. "movzbl %b[tmp0], %[temp]\n" \
  125. "shrl $8, %[tmp0]\n" \
  126. "pxor " #byte "*256*8(%[table], %[temp], 8), %[crc0]\n" \
  127. "movzbl %b[tmp1], %[temp]\n" \
  128. "shrl $8, %[tmp1]\n" \
  129. "pxor " #byte "*256*8(%[table], %[temp], 8), %[crc1]\n" \
  130. "movzbl %b[tmp2], %[temp]\n" \
  131. "shrl $8, %[tmp2]\n" \
  132. "pxor " #byte "*256*8(%[table], %[temp], 8), %[crc2]\n" \
  133. "movzbl %b[tmp3], %[temp]\n" \
  134. "shrl $8, %[tmp3]\n" \
  135. "pxor " #byte "*256*8(%[table], %[temp], 8), %[crc3]\n"
  136. XOR(1)
  137. XOR(2)
  138. "pxor 3*256*8(%[table], %[tmp0], 8), %[crc0]\n"
  139. "movd %[buf0], %[tmp0]\n"
  140. "pxor 3*256*8(%[table], %[tmp1], 8), %[crc1]\n"
  141. "movd %[buf1], %[tmp1]\n"
  142. "pxor 3*256*8(%[table], %[tmp2], 8), %[crc2]\n"
  143. "movd %[buf2], %[tmp2]\n"
  144. "pxor 3*256*8(%[table], %[tmp3], 8), %[crc3]\n"
  145. "movd %[buf3], %[tmp3]\n"
  146. XOR(4)
  147. XOR(5)
  148. XOR(6)
  149. "pxor 7*256*8(%[table], %[tmp0], 8), %[crc0]\n"
  150. "movq (%[src]), %[buf0]\n"
  151. "pxor 7*256*8(%[table], %[tmp1], 8), %[crc1]\n"
  152. "movq 1*8(%[src]), %[buf1]\n"
  153. "pxor 7*256*8(%[table], %[tmp2], 8), %[crc2]\n"
  154. "movq 2*8(%[src]), %[buf2]\n"
  155. "pxor 7*256*8(%[table], %[tmp3], 8), %[crc3]\n"
  156. "movq 3*8(%[src]), %[buf3]\n"
  157. "cmpl %[src], %[end]\n"
  158. "ja 1b\n"
  159. #undef XOR
  160. "movl %[table_word], %[table]\n"
  161. CRC_WORD_MMX()
  162. "pxor %[crc1], %[buf1]\n"
  163. "movq %[buf1], %[buf0]\n"
  164. CRC_WORD_MMX()
  165. "pxor %[crc2], %[buf2]\n"
  166. "movq %[buf2], %[buf0]\n"
  167. CRC_WORD_MMX()
  168. "pxor %[crc3], %[buf3]\n"
  169. "movq %[buf3], %[buf0]\n"
  170. CRC_WORD_MMX()
  171. "add $4*8, %[src]\n"
  172. "2:\n"
  173. "movl %[table_word], %[table]\n"
  174. "addl $2*4*8 - 8, %[end]\n"
  175. "cmpl %[src], %[end]\n"
  176. "jbe 4f\n"
  177. "3:\n"
  178. "movq (%[src]), %[buf0]\n"
  179. "addl $0x8, %[src]\n"
  180. CRC_WORD_MMX()
  181. "cmpl %[src], %[end]\n"
  182. "ja 3b\n"
  183. "4:\n"
  184. "addl $7, %[end]\n"
  185. "cmpl %[src], %[end]\n"
  186. "jbe 6f\n"
  187. "5:\n"
  188. "movd %[crc0], %[tmp0]\n"
  189. "movzbl (%[src]), %[temp]\n"
  190. "movzbl %b[tmp0], %[tmp0]\n"
  191. "psrlq $8, %[crc0]\n"
  192. "xorl %[tmp0], %[temp]\n"
  193. "add $1, %[src]\n"
  194. "pxor 7*256*8(%[table], %[temp], 8), %[crc0]\n"
  195. "cmpl %[src], %[end]\n"
  196. "ja 5b\n"
  197. "6:\n"
  198. : // outputs
  199. [src] "+r" (src),
  200. [end] "+m" (end),
  201. [crc0] "+y" (crc0),
  202. [crc1] "=&y" (crc1),
  203. [crc2] "=&y" (crc2),
  204. [crc3] "=&y" (crc3),
  205. [buf0] "=&y" (buf0),
  206. [buf1] "=&y" (buf1),
  207. [buf2] "=&y" (buf2),
  208. [buf3] "=&y" (buf3),
  209. [tmp0] "=&q" (tmp0),
  210. [tmp1] "=&q" (tmp1),
  211. [tmp2] "=&q" (tmp2),
  212. [tmp3] "=&q" (tmp3),
  213. [temp] "=&r" (temp),
  214. [table] "=&r" (table_ptr)
  215. : // inputs
  216. [table_interleaved] "m" (table_interleaved),
  217. [table_word] "m" (table_word));
  218. asm volatile("emms");
  219. return (crc0 ^ this->Base().Canonize());
  220. }
  221. } // namespace crcutil
  222. #endif // defined(__GNUC__) && HAVE_AMD64 && CRCUTIL_USE_ASM