crc32c-pcl-intel-asm_64.S 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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 <[email protected]>
  13. * James Guilford <[email protected]>
  14. * David Cote <[email protected]>
  15. * Tim Chen <[email protected]>
  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 <linux/linkage.h>
  46. #include <asm/nospec-branch.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. .quad crc_\i
  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. .text
  66. SYM_FUNC_START(crc_pcl)
  67. #define bufp rdi
  68. #define bufp_dw %edi
  69. #define bufp_w %di
  70. #define bufp_b %dil
  71. #define bufptmp %rcx
  72. #define block_0 %rcx
  73. #define block_1 %rdx
  74. #define block_2 %r11
  75. #define len %rsi
  76. #define len_dw %esi
  77. #define len_w %si
  78. #define len_b %sil
  79. #define crc_init_arg %rdx
  80. #define tmp %rbx
  81. #define crc_init %r8
  82. #define crc_init_dw %r8d
  83. #define crc1 %r9
  84. #define crc2 %r10
  85. pushq %rbx
  86. pushq %rdi
  87. pushq %rsi
  88. ## Move crc_init for Linux to a different
  89. mov crc_init_arg, crc_init
  90. ################################################################
  91. ## 1) ALIGN:
  92. ################################################################
  93. mov %bufp, bufptmp # rdi = *buf
  94. neg %bufp
  95. and $7, %bufp # calculate the unalignment amount of
  96. # the address
  97. je proc_block # Skip if aligned
  98. ## If len is less than 8 and we're unaligned, we need to jump
  99. ## to special code to avoid reading beyond the end of the buffer
  100. cmp $8, len
  101. jae do_align
  102. # less_than_8 expects length in upper 3 bits of len_dw
  103. # less_than_8_post_shl1 expects length = carryflag * 8 + len_dw[31:30]
  104. shl $32-3+1, len_dw
  105. jmp less_than_8_post_shl1
  106. do_align:
  107. #### Calculate CRC of unaligned bytes of the buffer (if any)
  108. movq (bufptmp), tmp # load a quadward from the buffer
  109. add %bufp, bufptmp # align buffer pointer for quadword
  110. # processing
  111. sub %bufp, len # update buffer length
  112. align_loop:
  113. crc32b %bl, crc_init_dw # compute crc32 of 1-byte
  114. shr $8, tmp # get next byte
  115. dec %bufp
  116. jne align_loop
  117. proc_block:
  118. ################################################################
  119. ## 2) PROCESS BLOCKS:
  120. ################################################################
  121. ## compute num of bytes to be processed
  122. movq len, tmp # save num bytes in tmp
  123. cmpq $128*24, len
  124. jae full_block
  125. continue_block:
  126. cmpq $SMALL_SIZE, len
  127. jb small
  128. ## len < 128*24
  129. movq $2731, %rax # 2731 = ceil(2^16 / 24)
  130. mul len_dw
  131. shrq $16, %rax
  132. ## eax contains floor(bytes / 24) = num 24-byte chunks to do
  133. ## process rax 24-byte chunks (128 >= rax >= 0)
  134. ## compute end address of each block
  135. ## block 0 (base addr + RAX * 8)
  136. ## block 1 (base addr + RAX * 16)
  137. ## block 2 (base addr + RAX * 24)
  138. lea (bufptmp, %rax, 8), block_0
  139. lea (block_0, %rax, 8), block_1
  140. lea (block_1, %rax, 8), block_2
  141. xor crc1, crc1
  142. xor crc2, crc2
  143. ## branch into array
  144. mov jump_table(,%rax,8), %bufp
  145. JMP_NOSPEC bufp
  146. ################################################################
  147. ## 2a) PROCESS FULL BLOCKS:
  148. ################################################################
  149. full_block:
  150. movl $128,%eax
  151. lea 128*8*2(block_0), block_1
  152. lea 128*8*3(block_0), block_2
  153. add $128*8*1, block_0
  154. xor crc1,crc1
  155. xor crc2,crc2
  156. # Fall thruogh into top of crc array (crc_128)
  157. ################################################################
  158. ## 3) CRC Array:
  159. ################################################################
  160. crc_array:
  161. i=128
  162. .rept 128-1
  163. .altmacro
  164. LABEL crc_ %i
  165. .noaltmacro
  166. ENDBR
  167. crc32q -i*8(block_0), crc_init
  168. crc32q -i*8(block_1), crc1
  169. crc32q -i*8(block_2), crc2
  170. i=(i-1)
  171. .endr
  172. .altmacro
  173. LABEL crc_ %i
  174. .noaltmacro
  175. ENDBR
  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-8)(%rip), %bufp # first entry is for idx 1
  184. shlq $3, %rax # rax *= 8
  185. pmovzxdq (%bufp,%rax), %xmm0 # 2 consts: K1:K2
  186. leal (%eax,%eax,2), %eax # rax *= 3 (total *24)
  187. subq %rax, tmp # tmp -= rax*24
  188. movq crc_init, %xmm1 # CRC for block 1
  189. pclmulqdq $0x00, %xmm0, %xmm1 # Multiply by K2
  190. movq crc1, %xmm2 # CRC for block 2
  191. pclmulqdq $0x10, %xmm0, %xmm2 # Multiply by K1
  192. pxor %xmm2,%xmm1
  193. movq %xmm1, %rax
  194. xor -i*8(block_2), %rax
  195. mov crc2, crc_init
  196. crc32 %rax, crc_init
  197. ################################################################
  198. ## 5) Check for end:
  199. ################################################################
  200. LABEL crc_ 0
  201. ENDBR
  202. mov tmp, len
  203. cmp $128*24, tmp
  204. jae full_block
  205. cmp $24, tmp
  206. jae continue_block
  207. less_than_24:
  208. shl $32-4, len_dw # less_than_16 expects length
  209. # in upper 4 bits of len_dw
  210. jnc less_than_16
  211. crc32q (bufptmp), crc_init
  212. crc32q 8(bufptmp), crc_init
  213. jz do_return
  214. add $16, bufptmp
  215. # len is less than 8 if we got here
  216. # less_than_8 expects length in upper 3 bits of len_dw
  217. # less_than_8_post_shl1 expects length = carryflag * 8 + len_dw[31:30]
  218. shl $2, len_dw
  219. jmp less_than_8_post_shl1
  220. #######################################################################
  221. ## 6) LESS THAN 256-bytes REMAIN AT THIS POINT (8-bits of len are full)
  222. #######################################################################
  223. small:
  224. shl $32-8, len_dw # Prepare len_dw for less_than_256
  225. j=256
  226. .rept 5 # j = {256, 128, 64, 32, 16}
  227. .altmacro
  228. LABEL less_than_ %j # less_than_j: Length should be in
  229. # upper lg(j) bits of len_dw
  230. j=(j/2)
  231. shl $1, len_dw # Get next MSB
  232. JNC_LESS_THAN %j
  233. .noaltmacro
  234. i=0
  235. .rept (j/8)
  236. crc32q i(bufptmp), crc_init # Compute crc32 of 8-byte data
  237. i=i+8
  238. .endr
  239. jz do_return # Return if remaining length is zero
  240. add $j, bufptmp # Advance buf
  241. .endr
  242. less_than_8: # Length should be stored in
  243. # upper 3 bits of len_dw
  244. shl $1, len_dw
  245. less_than_8_post_shl1:
  246. jnc less_than_4
  247. crc32l (bufptmp), crc_init_dw # CRC of 4 bytes
  248. jz do_return # return if remaining data is zero
  249. add $4, bufptmp
  250. less_than_4: # Length should be stored in
  251. # upper 2 bits of len_dw
  252. shl $1, len_dw
  253. jnc less_than_2
  254. crc32w (bufptmp), crc_init_dw # CRC of 2 bytes
  255. jz do_return # return if remaining data is zero
  256. add $2, bufptmp
  257. less_than_2: # Length should be stored in the MSB
  258. # of len_dw
  259. shl $1, len_dw
  260. jnc less_than_1
  261. crc32b (bufptmp), crc_init_dw # CRC of 1 byte
  262. less_than_1: # Length should be zero
  263. do_return:
  264. movq crc_init, %rax
  265. popq %rsi
  266. popq %rdi
  267. popq %rbx
  268. RET
  269. SYM_FUNC_END(crc_pcl)
  270. .section .rodata, "a", @progbits
  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. ################################################################
  284. ## PCLMULQDQ tables
  285. ## Table is 128 entries x 2 words (8 bytes) each
  286. ################################################################
  287. .align 8
  288. K_table:
  289. .long 0x493c7d27, 0x00000001
  290. .long 0xba4fc28e, 0x493c7d27
  291. .long 0xddc0152b, 0xf20c0dfe
  292. .long 0x9e4addf8, 0xba4fc28e
  293. .long 0x39d3b296, 0x3da6d0cb
  294. .long 0x0715ce53, 0xddc0152b
  295. .long 0x47db8317, 0x1c291d04
  296. .long 0x0d3b6092, 0x9e4addf8
  297. .long 0xc96cfdc0, 0x740eef02
  298. .long 0x878a92a7, 0x39d3b296
  299. .long 0xdaece73e, 0x083a6eec
  300. .long 0xab7aff2a, 0x0715ce53
  301. .long 0x2162d385, 0xc49f4f67
  302. .long 0x83348832, 0x47db8317
  303. .long 0x299847d5, 0x2ad91c30
  304. .long 0xb9e02b86, 0x0d3b6092
  305. .long 0x18b33a4e, 0x6992cea2
  306. .long 0xb6dd949b, 0xc96cfdc0
  307. .long 0x78d9ccb7, 0x7e908048
  308. .long 0xbac2fd7b, 0x878a92a7
  309. .long 0xa60ce07b, 0x1b3d8f29
  310. .long 0xce7f39f4, 0xdaece73e
  311. .long 0x61d82e56, 0xf1d0f55e
  312. .long 0xd270f1a2, 0xab7aff2a
  313. .long 0xc619809d, 0xa87ab8a8
  314. .long 0x2b3cac5d, 0x2162d385
  315. .long 0x65863b64, 0x8462d800
  316. .long 0x1b03397f, 0x83348832
  317. .long 0xebb883bd, 0x71d111a8
  318. .long 0xb3e32c28, 0x299847d5
  319. .long 0x064f7f26, 0xffd852c6
  320. .long 0xdd7e3b0c, 0xb9e02b86
  321. .long 0xf285651c, 0xdcb17aa4
  322. .long 0x10746f3c, 0x18b33a4e
  323. .long 0xc7a68855, 0xf37c5aee
  324. .long 0x271d9844, 0xb6dd949b
  325. .long 0x8e766a0c, 0x6051d5a2
  326. .long 0x93a5f730, 0x78d9ccb7
  327. .long 0x6cb08e5c, 0x18b0d4ff
  328. .long 0x6b749fb2, 0xbac2fd7b
  329. .long 0x1393e203, 0x21f3d99c
  330. .long 0xcec3662e, 0xa60ce07b
  331. .long 0x96c515bb, 0x8f158014
  332. .long 0xe6fc4e6a, 0xce7f39f4
  333. .long 0x8227bb8a, 0xa00457f7
  334. .long 0xb0cd4768, 0x61d82e56
  335. .long 0x39c7ff35, 0x8d6d2c43
  336. .long 0xd7a4825c, 0xd270f1a2
  337. .long 0x0ab3844b, 0x00ac29cf
  338. .long 0x0167d312, 0xc619809d
  339. .long 0xf6076544, 0xe9adf796
  340. .long 0x26f6a60a, 0x2b3cac5d
  341. .long 0xa741c1bf, 0x96638b34
  342. .long 0x98d8d9cb, 0x65863b64
  343. .long 0x49c3cc9c, 0xe0e9f351
  344. .long 0x68bce87a, 0x1b03397f
  345. .long 0x57a3d037, 0x9af01f2d
  346. .long 0x6956fc3b, 0xebb883bd
  347. .long 0x42d98888, 0x2cff42cf
  348. .long 0x3771e98f, 0xb3e32c28
  349. .long 0xb42ae3d9, 0x88f25a3a
  350. .long 0x2178513a, 0x064f7f26
  351. .long 0xe0ac139e, 0x4e36f0b0
  352. .long 0x170076fa, 0xdd7e3b0c
  353. .long 0x444dd413, 0xbd6f81f8
  354. .long 0x6f345e45, 0xf285651c
  355. .long 0x41d17b64, 0x91c9bd4b
  356. .long 0xff0dba97, 0x10746f3c
  357. .long 0xa2b73df1, 0x885f087b
  358. .long 0xf872e54c, 0xc7a68855
  359. .long 0x1e41e9fc, 0x4c144932
  360. .long 0x86d8e4d2, 0x271d9844
  361. .long 0x651bd98b, 0x52148f02
  362. .long 0x5bb8f1bc, 0x8e766a0c
  363. .long 0xa90fd27a, 0xa3c6f37a
  364. .long 0xb3af077a, 0x93a5f730
  365. .long 0x4984d782, 0xd7c0557f
  366. .long 0xca6ef3ac, 0x6cb08e5c
  367. .long 0x234e0b26, 0x63ded06a
  368. .long 0xdd66cbbb, 0x6b749fb2
  369. .long 0x4597456a, 0x4d56973c
  370. .long 0xe9e28eb4, 0x1393e203
  371. .long 0x7b3ff57a, 0x9669c9df
  372. .long 0xc9c8b782, 0xcec3662e
  373. .long 0x3f70cc6f, 0xe417f38a
  374. .long 0x93e106a4, 0x96c515bb
  375. .long 0x62ec6c6d, 0x4b9e0f71
  376. .long 0xd813b325, 0xe6fc4e6a
  377. .long 0x0df04680, 0xd104b8fc
  378. .long 0x2342001e, 0x8227bb8a
  379. .long 0x0a2a8d7e, 0x5b397730
  380. .long 0x6d9a4957, 0xb0cd4768
  381. .long 0xe8b6368b, 0xe78eb416
  382. .long 0xd2c3ed1a, 0x39c7ff35
  383. .long 0x995a5724, 0x61ff0e01
  384. .long 0x9ef68d35, 0xd7a4825c
  385. .long 0x0c139b31, 0x8d96551c
  386. .long 0xf2271e60, 0x0ab3844b
  387. .long 0x0b0bf8ca, 0x0bf80dd2
  388. .long 0x2664fd8b, 0x0167d312
  389. .long 0xed64812d, 0x8821abed
  390. .long 0x02ee03b2, 0xf6076544
  391. .long 0x8604ae0f, 0x6a45d2b2
  392. .long 0x363bd6b3, 0x26f6a60a
  393. .long 0x135c83fd, 0xd8d26619
  394. .long 0x5fabe670, 0xa741c1bf
  395. .long 0x35ec3279, 0xde87806c
  396. .long 0x00bcf5f6, 0x98d8d9cb
  397. .long 0x8ae00689, 0x14338754
  398. .long 0x17f27698, 0x49c3cc9c
  399. .long 0x58ca5f00, 0x5bd2011f
  400. .long 0xaa7c7ad5, 0x68bce87a
  401. .long 0xb5cfca28, 0xdd07448e
  402. .long 0xded288f8, 0x57a3d037
  403. .long 0x59f229bc, 0xdde8f5b9
  404. .long 0x6d390dec, 0x6956fc3b
  405. .long 0x37170390, 0xa3e3e02c
  406. .long 0x6353c1cc, 0x42d98888
  407. .long 0xc4584f5c, 0xd73c7bea
  408. .long 0xf48642e9, 0x3771e98f
  409. .long 0x531377e2, 0x80ff0093
  410. .long 0xdd35bc8d, 0xb42ae3d9
  411. .long 0xb25b29f2, 0x8fe4c34d
  412. .long 0x9a5ede41, 0x2178513a
  413. .long 0xa563905d, 0xdf99fc11
  414. .long 0x45cddf4e, 0xe0ac139e
  415. .long 0xacfa3103, 0x6c23e841
  416. .long 0xa51b6135, 0x170076fa