crc32c-pcl-intel-asm_64.S 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /*
  2. * Implement fast CRC32C with PCLMULQDQ instructions. (x86_64)
  3. *
  4. * The white papers on CRC32C calculations with PCLMULQDQ instruction can be
  5. * downloaded from:
  6. * http://www.intel.com/content/dam/www/public/us/en/documents/white-papers/crc-iscsi-polynomial-crc32-instruction-paper.pdf
  7. * http://www.intel.com/content/dam/www/public/us/en/documents/white-papers/fast-crc-computation-paper.pdf
  8. *
  9. * Copyright (C) 2012 Intel Corporation.
  10. *
  11. * Authors:
  12. * Wajdi Feghali <wajdi.k.feghali@intel.com>
  13. * James Guilford <james.guilford@intel.com>
  14. * David Cote <david.m.cote@intel.com>
  15. * Tim Chen <tim.c.chen@linux.intel.com>
  16. *
  17. * This software is available to you under a choice of one of two
  18. * licenses. You may choose to be licensed under the terms of the GNU
  19. * General Public License (GPL) Version 2, available from the file
  20. * COPYING in the main directory of this source tree, or the
  21. * OpenIB.org BSD license below:
  22. *
  23. * Redistribution and use in source and binary forms, with or
  24. * without modification, are permitted provided that the following
  25. * conditions are met:
  26. *
  27. * - Redistributions of source code must retain the above
  28. * copyright notice, this list of conditions and the following
  29. * disclaimer.
  30. *
  31. * - Redistributions in binary form must reproduce the above
  32. * copyright notice, this list of conditions and the following
  33. * disclaimer in the documentation and/or other materials
  34. * provided with the distribution.
  35. *
  36. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  37. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  38. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  39. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  40. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  41. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  42. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  43. * SOFTWARE.
  44. */
  45. #include <asm/inst.h>
  46. #include <linux/linkage.h>
  47. ## ISCSI CRC 32 Implementation with crc32 and pclmulqdq Instruction
  48. .macro LABEL prefix n
  49. \prefix\n\():
  50. .endm
  51. .macro JMPTBL_ENTRY i
  52. .word crc_\i - crc_array
  53. .endm
  54. .macro JNC_LESS_THAN j
  55. jnc less_than_\j
  56. .endm
  57. # Define threshold where buffers are considered "small" and routed to more
  58. # efficient "by-1" code. This "by-1" code only handles up to 255 bytes, so
  59. # SMALL_SIZE can be no larger than 255.
  60. #define SMALL_SIZE 200
  61. .if (SMALL_SIZE > 255)
  62. .error "SMALL_ SIZE must be < 256"
  63. .endif
  64. # unsigned int crc_pcl(u8 *buffer, int len, unsigned int crc_init);
  65. ENTRY(crc_pcl)
  66. #define bufp %rdi
  67. #define bufp_dw %edi
  68. #define bufp_w %di
  69. #define bufp_b %dil
  70. #define bufptmp %rcx
  71. #define block_0 %rcx
  72. #define block_1 %rdx
  73. #define block_2 %r11
  74. #define len %rsi
  75. #define len_dw %esi
  76. #define len_w %si
  77. #define len_b %sil
  78. #define crc_init_arg %rdx
  79. #define tmp %rbx
  80. #define crc_init %r8
  81. #define crc_init_dw %r8d
  82. #define crc1 %r9
  83. #define crc2 %r10
  84. pushq %rbx
  85. pushq %rdi
  86. pushq %rsi
  87. ## Move crc_init for Linux to a different
  88. mov crc_init_arg, crc_init
  89. ################################################################
  90. ## 1) ALIGN:
  91. ################################################################
  92. mov bufp, bufptmp # rdi = *buf
  93. neg bufp
  94. and $7, bufp # calculate the unalignment amount of
  95. # the address
  96. je proc_block # Skip if aligned
  97. ## If len is less than 8 and we're unaligned, we need to jump
  98. ## to special code to avoid reading beyond the end of the buffer
  99. cmp $8, len
  100. jae do_align
  101. # less_than_8 expects length in upper 3 bits of len_dw
  102. # less_than_8_post_shl1 expects length = carryflag * 8 + len_dw[31:30]
  103. shl $32-3+1, len_dw
  104. jmp less_than_8_post_shl1
  105. do_align:
  106. #### Calculate CRC of unaligned bytes of the buffer (if any)
  107. movq (bufptmp), tmp # load a quadward from the buffer
  108. add bufp, bufptmp # align buffer pointer for quadword
  109. # processing
  110. sub bufp, len # update buffer length
  111. align_loop:
  112. crc32b %bl, crc_init_dw # compute crc32 of 1-byte
  113. shr $8, tmp # get next byte
  114. dec bufp
  115. jne align_loop
  116. proc_block:
  117. ################################################################
  118. ## 2) PROCESS BLOCKS:
  119. ################################################################
  120. ## compute num of bytes to be processed
  121. movq len, tmp # save num bytes in tmp
  122. cmpq $128*24, len
  123. jae full_block
  124. continue_block:
  125. cmpq $SMALL_SIZE, len
  126. jb small
  127. ## len < 128*24
  128. movq $2731, %rax # 2731 = ceil(2^16 / 24)
  129. mul len_dw
  130. shrq $16, %rax
  131. ## eax contains floor(bytes / 24) = num 24-byte chunks to do
  132. ## process rax 24-byte chunks (128 >= rax >= 0)
  133. ## compute end address of each block
  134. ## block 0 (base addr + RAX * 8)
  135. ## block 1 (base addr + RAX * 16)
  136. ## block 2 (base addr + RAX * 24)
  137. lea (bufptmp, %rax, 8), block_0
  138. lea (block_0, %rax, 8), block_1
  139. lea (block_1, %rax, 8), block_2
  140. xor crc1, crc1
  141. xor crc2, crc2
  142. ## branch into array
  143. lea jump_table(%rip), bufp
  144. movzxw (bufp, %rax, 2), len
  145. offset=crc_array-jump_table
  146. lea offset(bufp, len, 1), bufp
  147. jmp *bufp
  148. ################################################################
  149. ## 2a) PROCESS FULL BLOCKS:
  150. ################################################################
  151. full_block:
  152. movq $128,%rax
  153. lea 128*8*2(block_0), block_1
  154. lea 128*8*3(block_0), block_2
  155. add $128*8*1, block_0
  156. xor crc1,crc1
  157. xor crc2,crc2
  158. # Fall thruogh into top of crc array (crc_128)
  159. ################################################################
  160. ## 3) CRC Array:
  161. ################################################################
  162. crc_array:
  163. i=128
  164. .rept 128-1
  165. .altmacro
  166. LABEL crc_ %i
  167. .noaltmacro
  168. crc32q -i*8(block_0), crc_init
  169. crc32q -i*8(block_1), crc1
  170. crc32q -i*8(block_2), crc2
  171. i=(i-1)
  172. .endr
  173. .altmacro
  174. LABEL crc_ %i
  175. .noaltmacro
  176. crc32q -i*8(block_0), crc_init
  177. crc32q -i*8(block_1), crc1
  178. # SKIP crc32 -i*8(block_2), crc2 ; Don't do this one yet
  179. mov block_2, block_0
  180. ################################################################
  181. ## 4) Combine three results:
  182. ################################################################
  183. lea (K_table-16)(%rip), bufp # first entry is for idx 1
  184. shlq $3, %rax # rax *= 8
  185. subq %rax, tmp # tmp -= rax*8
  186. shlq $1, %rax
  187. subq %rax, tmp # tmp -= rax*16
  188. # (total tmp -= rax*24)
  189. addq %rax, bufp
  190. movdqa (bufp), %xmm0 # 2 consts: K1:K2
  191. movq crc_init, %xmm1 # CRC for block 1
  192. PCLMULQDQ 0x00,%xmm0,%xmm1 # Multiply by K2
  193. movq crc1, %xmm2 # CRC for block 2
  194. PCLMULQDQ 0x10, %xmm0, %xmm2 # Multiply by K1
  195. pxor %xmm2,%xmm1
  196. movq %xmm1, %rax
  197. xor -i*8(block_2), %rax
  198. mov crc2, crc_init
  199. crc32 %rax, crc_init
  200. ################################################################
  201. ## 5) Check for end:
  202. ################################################################
  203. LABEL crc_ 0
  204. mov tmp, len
  205. cmp $128*24, tmp
  206. jae full_block
  207. cmp $24, tmp
  208. jae continue_block
  209. less_than_24:
  210. shl $32-4, len_dw # less_than_16 expects length
  211. # in upper 4 bits of len_dw
  212. jnc less_than_16
  213. crc32q (bufptmp), crc_init
  214. crc32q 8(bufptmp), crc_init
  215. jz do_return
  216. add $16, bufptmp
  217. # len is less than 8 if we got here
  218. # less_than_8 expects length in upper 3 bits of len_dw
  219. # less_than_8_post_shl1 expects length = carryflag * 8 + len_dw[31:30]
  220. shl $2, len_dw
  221. jmp less_than_8_post_shl1
  222. #######################################################################
  223. ## 6) LESS THAN 256-bytes REMAIN AT THIS POINT (8-bits of len are full)
  224. #######################################################################
  225. small:
  226. shl $32-8, len_dw # Prepare len_dw for less_than_256
  227. j=256
  228. .rept 5 # j = {256, 128, 64, 32, 16}
  229. .altmacro
  230. LABEL less_than_ %j # less_than_j: Length should be in
  231. # upper lg(j) bits of len_dw
  232. j=(j/2)
  233. shl $1, len_dw # Get next MSB
  234. JNC_LESS_THAN %j
  235. .noaltmacro
  236. i=0
  237. .rept (j/8)
  238. crc32q i(bufptmp), crc_init # Compute crc32 of 8-byte data
  239. i=i+8
  240. .endr
  241. jz do_return # Return if remaining length is zero
  242. add $j, bufptmp # Advance buf
  243. .endr
  244. less_than_8: # Length should be stored in
  245. # upper 3 bits of len_dw
  246. shl $1, len_dw
  247. less_than_8_post_shl1:
  248. jnc less_than_4
  249. crc32l (bufptmp), crc_init_dw # CRC of 4 bytes
  250. jz do_return # return if remaining data is zero
  251. add $4, bufptmp
  252. less_than_4: # Length should be stored in
  253. # upper 2 bits of len_dw
  254. shl $1, len_dw
  255. jnc less_than_2
  256. crc32w (bufptmp), crc_init_dw # CRC of 2 bytes
  257. jz do_return # return if remaining data is zero
  258. add $2, bufptmp
  259. less_than_2: # Length should be stored in the MSB
  260. # of len_dw
  261. shl $1, len_dw
  262. jnc less_than_1
  263. crc32b (bufptmp), crc_init_dw # CRC of 1 byte
  264. less_than_1: # Length should be zero
  265. do_return:
  266. movq crc_init, %rax
  267. popq %rsi
  268. popq %rdi
  269. popq %rbx
  270. ret
  271. ################################################################
  272. ## jump table Table is 129 entries x 2 bytes each
  273. ################################################################
  274. .align 4
  275. jump_table:
  276. i=0
  277. .rept 129
  278. .altmacro
  279. JMPTBL_ENTRY %i
  280. .noaltmacro
  281. i=i+1
  282. .endr
  283. ENDPROC(crc_pcl)
  284. ################################################################
  285. ## PCLMULQDQ tables
  286. ## Table is 128 entries x 2 quad words each
  287. ################################################################
  288. .data
  289. .align 64
  290. K_table:
  291. .quad 0x14cd00bd6,0x105ec76f0
  292. .quad 0x0ba4fc28e,0x14cd00bd6
  293. .quad 0x1d82c63da,0x0f20c0dfe
  294. .quad 0x09e4addf8,0x0ba4fc28e
  295. .quad 0x039d3b296,0x1384aa63a
  296. .quad 0x102f9b8a2,0x1d82c63da
  297. .quad 0x14237f5e6,0x01c291d04
  298. .quad 0x00d3b6092,0x09e4addf8
  299. .quad 0x0c96cfdc0,0x0740eef02
  300. .quad 0x18266e456,0x039d3b296
  301. .quad 0x0daece73e,0x0083a6eec
  302. .quad 0x0ab7aff2a,0x102f9b8a2
  303. .quad 0x1248ea574,0x1c1733996
  304. .quad 0x083348832,0x14237f5e6
  305. .quad 0x12c743124,0x02ad91c30
  306. .quad 0x0b9e02b86,0x00d3b6092
  307. .quad 0x018b33a4e,0x06992cea2
  308. .quad 0x1b331e26a,0x0c96cfdc0
  309. .quad 0x17d35ba46,0x07e908048
  310. .quad 0x1bf2e8b8a,0x18266e456
  311. .quad 0x1a3e0968a,0x11ed1f9d8
  312. .quad 0x0ce7f39f4,0x0daece73e
  313. .quad 0x061d82e56,0x0f1d0f55e
  314. .quad 0x0d270f1a2,0x0ab7aff2a
  315. .quad 0x1c3f5f66c,0x0a87ab8a8
  316. .quad 0x12ed0daac,0x1248ea574
  317. .quad 0x065863b64,0x08462d800
  318. .quad 0x11eef4f8e,0x083348832
  319. .quad 0x1ee54f54c,0x071d111a8
  320. .quad 0x0b3e32c28,0x12c743124
  321. .quad 0x0064f7f26,0x0ffd852c6
  322. .quad 0x0dd7e3b0c,0x0b9e02b86
  323. .quad 0x0f285651c,0x0dcb17aa4
  324. .quad 0x010746f3c,0x018b33a4e
  325. .quad 0x1c24afea4,0x0f37c5aee
  326. .quad 0x0271d9844,0x1b331e26a
  327. .quad 0x08e766a0c,0x06051d5a2
  328. .quad 0x093a5f730,0x17d35ba46
  329. .quad 0x06cb08e5c,0x11d5ca20e
  330. .quad 0x06b749fb2,0x1bf2e8b8a
  331. .quad 0x1167f94f2,0x021f3d99c
  332. .quad 0x0cec3662e,0x1a3e0968a
  333. .quad 0x19329634a,0x08f158014
  334. .quad 0x0e6fc4e6a,0x0ce7f39f4
  335. .quad 0x08227bb8a,0x1a5e82106
  336. .quad 0x0b0cd4768,0x061d82e56
  337. .quad 0x13c2b89c4,0x188815ab2
  338. .quad 0x0d7a4825c,0x0d270f1a2
  339. .quad 0x10f5ff2ba,0x105405f3e
  340. .quad 0x00167d312,0x1c3f5f66c
  341. .quad 0x0f6076544,0x0e9adf796
  342. .quad 0x026f6a60a,0x12ed0daac
  343. .quad 0x1a2adb74e,0x096638b34
  344. .quad 0x19d34af3a,0x065863b64
  345. .quad 0x049c3cc9c,0x1e50585a0
  346. .quad 0x068bce87a,0x11eef4f8e
  347. .quad 0x1524fa6c6,0x19f1c69dc
  348. .quad 0x16cba8aca,0x1ee54f54c
  349. .quad 0x042d98888,0x12913343e
  350. .quad 0x1329d9f7e,0x0b3e32c28
  351. .quad 0x1b1c69528,0x088f25a3a
  352. .quad 0x02178513a,0x0064f7f26
  353. .quad 0x0e0ac139e,0x04e36f0b0
  354. .quad 0x0170076fa,0x0dd7e3b0c
  355. .quad 0x141a1a2e2,0x0bd6f81f8
  356. .quad 0x16ad828b4,0x0f285651c
  357. .quad 0x041d17b64,0x19425cbba
  358. .quad 0x1fae1cc66,0x010746f3c
  359. .quad 0x1a75b4b00,0x18db37e8a
  360. .quad 0x0f872e54c,0x1c24afea4
  361. .quad 0x01e41e9fc,0x04c144932
  362. .quad 0x086d8e4d2,0x0271d9844
  363. .quad 0x160f7af7a,0x052148f02
  364. .quad 0x05bb8f1bc,0x08e766a0c
  365. .quad 0x0a90fd27a,0x0a3c6f37a
  366. .quad 0x0b3af077a,0x093a5f730
  367. .quad 0x04984d782,0x1d22c238e
  368. .quad 0x0ca6ef3ac,0x06cb08e5c
  369. .quad 0x0234e0b26,0x063ded06a
  370. .quad 0x1d88abd4a,0x06b749fb2
  371. .quad 0x04597456a,0x04d56973c
  372. .quad 0x0e9e28eb4,0x1167f94f2
  373. .quad 0x07b3ff57a,0x19385bf2e
  374. .quad 0x0c9c8b782,0x0cec3662e
  375. .quad 0x13a9cba9e,0x0e417f38a
  376. .quad 0x093e106a4,0x19329634a
  377. .quad 0x167001a9c,0x14e727980
  378. .quad 0x1ddffc5d4,0x0e6fc4e6a
  379. .quad 0x00df04680,0x0d104b8fc
  380. .quad 0x02342001e,0x08227bb8a
  381. .quad 0x00a2a8d7e,0x05b397730
  382. .quad 0x168763fa6,0x0b0cd4768
  383. .quad 0x1ed5a407a,0x0e78eb416
  384. .quad 0x0d2c3ed1a,0x13c2b89c4
  385. .quad 0x0995a5724,0x1641378f0
  386. .quad 0x19b1afbc4,0x0d7a4825c
  387. .quad 0x109ffedc0,0x08d96551c
  388. .quad 0x0f2271e60,0x10f5ff2ba
  389. .quad 0x00b0bf8ca,0x00bf80dd2
  390. .quad 0x123888b7a,0x00167d312
  391. .quad 0x1e888f7dc,0x18dcddd1c
  392. .quad 0x002ee03b2,0x0f6076544
  393. .quad 0x183e8d8fe,0x06a45d2b2
  394. .quad 0x133d7a042,0x026f6a60a
  395. .quad 0x116b0f50c,0x1dd3e10e8
  396. .quad 0x05fabe670,0x1a2adb74e
  397. .quad 0x130004488,0x0de87806c
  398. .quad 0x000bcf5f6,0x19d34af3a
  399. .quad 0x18f0c7078,0x014338754
  400. .quad 0x017f27698,0x049c3cc9c
  401. .quad 0x058ca5f00,0x15e3e77ee
  402. .quad 0x1af900c24,0x068bce87a
  403. .quad 0x0b5cfca28,0x0dd07448e
  404. .quad 0x0ded288f8,0x1524fa6c6
  405. .quad 0x059f229bc,0x1d8048348
  406. .quad 0x06d390dec,0x16cba8aca
  407. .quad 0x037170390,0x0a3e3e02c
  408. .quad 0x06353c1cc,0x042d98888
  409. .quad 0x0c4584f5c,0x0d73c7bea
  410. .quad 0x1f16a3418,0x1329d9f7e
  411. .quad 0x0531377e2,0x185137662
  412. .quad 0x1d8d9ca7c,0x1b1c69528
  413. .quad 0x0b25b29f2,0x18a08b5bc
  414. .quad 0x19fb2a8b0,0x02178513a
  415. .quad 0x1a08fe6ac,0x1da758ae0
  416. .quad 0x045cddf4e,0x0e0ac139e
  417. .quad 0x1a91647f2,0x169cf9eb0
  418. .quad 0x1a0f717c4,0x0170076fa