crc32_x86.S 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Speed-optimized CRC32 using slicing-by-eight algorithm
  3. *
  4. * This uses only i386 instructions, but it is optimized for i686 and later
  5. * (including e.g. Pentium II/III/IV, Athlon XP, and Core 2). For i586
  6. * (e.g. Pentium), slicing-by-four would be better, and even the C version
  7. * of slicing-by-eight built with gcc -march=i586 tends to be a little bit
  8. * better than this. Very few probably run this code on i586 or older x86
  9. * so this shouldn't be a problem in practice.
  10. *
  11. * Authors: Igor Pavlov (original version)
  12. * Lasse Collin (AT&T syntax, PIC support, better portability)
  13. *
  14. * This file has been put into the public domain.
  15. * You can do whatever you want with this file.
  16. *
  17. * This code needs lzma_crc32_table, which can be created using the
  18. * following C code:
  19. uint32_t lzma_crc32_table[8][256];
  20. void
  21. init_table(void)
  22. {
  23. // IEEE-802.3
  24. static const uint32_t poly32 = UINT32_C(0xEDB88320);
  25. // Castagnoli
  26. // static const uint32_t poly32 = UINT32_C(0x82F63B78);
  27. // Koopman
  28. // static const uint32_t poly32 = UINT32_C(0xEB31D82E);
  29. for (size_t s = 0; s < 8; ++s) {
  30. for (size_t b = 0; b < 256; ++b) {
  31. uint32_t r = s == 0 ? b : lzma_crc32_table[s - 1][b];
  32. for (size_t i = 0; i < 8; ++i) {
  33. if (r & 1)
  34. r = (r >> 1) ^ poly32;
  35. else
  36. r >>= 1;
  37. }
  38. lzma_crc32_table[s][b] = r;
  39. }
  40. }
  41. }
  42. * The prototype of the CRC32 function:
  43. * extern uint32_t lzma_crc32(const uint8_t *buf, size_t size, uint32_t crc);
  44. */
  45. /*
  46. * On some systems, the functions need to be prefixed. The prefix is
  47. * usually an underscore.
  48. */
  49. #ifndef __USER_LABEL_PREFIX__
  50. # define __USER_LABEL_PREFIX__
  51. #endif
  52. #define MAKE_SYM_CAT(prefix, sym) prefix ## sym
  53. #define MAKE_SYM(prefix, sym) MAKE_SYM_CAT(prefix, sym)
  54. #define LZMA_CRC32 MAKE_SYM(__USER_LABEL_PREFIX__, lzma_crc32)
  55. #define LZMA_CRC32_TABLE MAKE_SYM(__USER_LABEL_PREFIX__, lzma_crc32_table)
  56. /*
  57. * Solaris assembler doesn't have .p2align, and Darwin uses .align
  58. * differently than GNU/Linux and Solaris.
  59. */
  60. #if defined(__APPLE__) || defined(__MSDOS__)
  61. # define ALIGN(pow2, abs) .align pow2
  62. #else
  63. # define ALIGN(pow2, abs) .align abs
  64. #endif
  65. .text
  66. .globl LZMA_CRC32
  67. #if !defined(__APPLE__) && !defined(_WIN32) && !defined(__CYGWIN__) \
  68. && !defined(__MSDOS__)
  69. .type LZMA_CRC32, @function
  70. #endif
  71. ALIGN(4, 16)
  72. LZMA_CRC32:
  73. /*
  74. * Register usage:
  75. * %eax crc
  76. * %esi buf
  77. * %edi size or buf + size
  78. * %ebx lzma_crc32_table
  79. * %ebp Table index
  80. * %ecx Temporary
  81. * %edx Temporary
  82. */
  83. pushl %ebx
  84. pushl %esi
  85. pushl %edi
  86. pushl %ebp
  87. movl 0x14(%esp), %esi /* buf */
  88. movl 0x18(%esp), %edi /* size */
  89. movl 0x1C(%esp), %eax /* crc */
  90. /*
  91. * Store the address of lzma_crc32_table to %ebx. This is needed to
  92. * get position-independent code (PIC).
  93. *
  94. * The PIC macro is defined by libtool, while __PIC__ is defined
  95. * by GCC but only on some systems. Testing for both makes it simpler
  96. * to test this code without libtool, and keeps the code working also
  97. * when built with libtool but using something else than GCC.
  98. *
  99. * I understood that libtool may define PIC on Windows even though
  100. * the code in Windows DLLs is not PIC in sense that it is in ELF
  101. * binaries, so we need a separate check to always use the non-PIC
  102. * code on Windows.
  103. */
  104. #if (!defined(PIC) && !defined(__PIC__)) \
  105. || (defined(_WIN32) || defined(__CYGWIN__))
  106. /* Not PIC */
  107. movl $ LZMA_CRC32_TABLE, %ebx
  108. #elif defined(__APPLE__)
  109. /* Mach-O */
  110. call .L_get_pc
  111. .L_pic:
  112. leal .L_lzma_crc32_table$non_lazy_ptr-.L_pic(%ebx), %ebx
  113. movl (%ebx), %ebx
  114. #else
  115. /* ELF */
  116. call .L_get_pc
  117. addl $_GLOBAL_OFFSET_TABLE_, %ebx
  118. movl LZMA_CRC32_TABLE@GOT(%ebx), %ebx
  119. #endif
  120. /* Complement the initial value. */
  121. notl %eax
  122. ALIGN(4, 16)
  123. .L_align:
  124. /*
  125. * Check if there is enough input to use slicing-by-eight.
  126. * We need 16 bytes, because the loop pre-reads eight bytes.
  127. */
  128. cmpl $16, %edi
  129. jb .L_rest
  130. /* Check if we have reached alignment of eight bytes. */
  131. testl $7, %esi
  132. jz .L_slice
  133. /* Calculate CRC of the next input byte. */
  134. movzbl (%esi), %ebp
  135. incl %esi
  136. movzbl %al, %ecx
  137. xorl %ecx, %ebp
  138. shrl $8, %eax
  139. xorl (%ebx, %ebp, 4), %eax
  140. decl %edi
  141. jmp .L_align
  142. ALIGN(2, 4)
  143. .L_slice:
  144. /*
  145. * If we get here, there's at least 16 bytes of aligned input
  146. * available. Make %edi multiple of eight bytes. Store the possible
  147. * remainder over the "size" variable in the argument stack.
  148. */
  149. movl %edi, 0x18(%esp)
  150. andl $-8, %edi
  151. subl %edi, 0x18(%esp)
  152. /*
  153. * Let %edi be buf + size - 8 while running the main loop. This way
  154. * we can compare for equality to determine when exit the loop.
  155. */
  156. addl %esi, %edi
  157. subl $8, %edi
  158. /* Read in the first eight aligned bytes. */
  159. xorl (%esi), %eax
  160. movl 4(%esi), %ecx
  161. movzbl %cl, %ebp
  162. .L_loop:
  163. movl 0x0C00(%ebx, %ebp, 4), %edx
  164. movzbl %ch, %ebp
  165. xorl 0x0800(%ebx, %ebp, 4), %edx
  166. shrl $16, %ecx
  167. xorl 8(%esi), %edx
  168. movzbl %cl, %ebp
  169. xorl 0x0400(%ebx, %ebp, 4), %edx
  170. movzbl %ch, %ebp
  171. xorl (%ebx, %ebp, 4), %edx
  172. movzbl %al, %ebp
  173. /*
  174. * Read the next four bytes, for which the CRC is calculated
  175. * on the next interation of the loop.
  176. */
  177. movl 12(%esi), %ecx
  178. xorl 0x1C00(%ebx, %ebp, 4), %edx
  179. movzbl %ah, %ebp
  180. shrl $16, %eax
  181. xorl 0x1800(%ebx, %ebp, 4), %edx
  182. movzbl %ah, %ebp
  183. movzbl %al, %eax
  184. movl 0x1400(%ebx, %eax, 4), %eax
  185. addl $8, %esi
  186. xorl %edx, %eax
  187. xorl 0x1000(%ebx, %ebp, 4), %eax
  188. /* Check for end of aligned input. */
  189. cmpl %edi, %esi
  190. movzbl %cl, %ebp
  191. jne .L_loop
  192. /*
  193. * Process the remaining eight bytes, which we have already
  194. * copied to %ecx and %edx.
  195. */
  196. movl 0x0C00(%ebx, %ebp, 4), %edx
  197. movzbl %ch, %ebp
  198. xorl 0x0800(%ebx, %ebp, 4), %edx
  199. shrl $16, %ecx
  200. movzbl %cl, %ebp
  201. xorl 0x0400(%ebx, %ebp, 4), %edx
  202. movzbl %ch, %ebp
  203. xorl (%ebx, %ebp, 4), %edx
  204. movzbl %al, %ebp
  205. xorl 0x1C00(%ebx, %ebp, 4), %edx
  206. movzbl %ah, %ebp
  207. shrl $16, %eax
  208. xorl 0x1800(%ebx, %ebp, 4), %edx
  209. movzbl %ah, %ebp
  210. movzbl %al, %eax
  211. movl 0x1400(%ebx, %eax, 4), %eax
  212. addl $8, %esi
  213. xorl %edx, %eax
  214. xorl 0x1000(%ebx, %ebp, 4), %eax
  215. /* Copy the number of remaining bytes to %edi. */
  216. movl 0x18(%esp), %edi
  217. .L_rest:
  218. /* Check for end of input. */
  219. testl %edi, %edi
  220. jz .L_return
  221. /* Calculate CRC of the next input byte. */
  222. movzbl (%esi), %ebp
  223. incl %esi
  224. movzbl %al, %ecx
  225. xorl %ecx, %ebp
  226. shrl $8, %eax
  227. xorl (%ebx, %ebp, 4), %eax
  228. decl %edi
  229. jmp .L_rest
  230. .L_return:
  231. /* Complement the final value. */
  232. notl %eax
  233. popl %ebp
  234. popl %edi
  235. popl %esi
  236. popl %ebx
  237. ret
  238. #if defined(PIC) || defined(__PIC__)
  239. ALIGN(4, 16)
  240. .L_get_pc:
  241. movl (%esp), %ebx
  242. ret
  243. #endif
  244. #if defined(__APPLE__) && (defined(PIC) || defined(__PIC__))
  245. /* Mach-O PIC */
  246. .section __IMPORT,__pointers,non_lazy_symbol_pointers
  247. .L_lzma_crc32_table$non_lazy_ptr:
  248. .indirect_symbol LZMA_CRC32_TABLE
  249. .long 0
  250. #elif defined(_WIN32) || defined(__CYGWIN__)
  251. # ifdef DLL_EXPORT
  252. /* This is equivalent of __declspec(dllexport). */
  253. .section .drectve
  254. .ascii " -export:lzma_crc32"
  255. # endif
  256. #elif !defined(__MSDOS__)
  257. /* ELF */
  258. .size LZMA_CRC32, .-LZMA_CRC32
  259. #endif
  260. /*
  261. * This is needed to support non-executable stack. It's ugly to
  262. * use __linux__ here, but I don't know a way to detect when
  263. * we are using GNU assembler.
  264. */
  265. #if defined(__ELF__) && defined(__linux__)
  266. .section .note.GNU-stack,"",@progbits
  267. #endif