1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ; Copyright(c) 2011-2016 Intel Corporation All rights reserved.
4 ; Redistribution and use in source and binary forms, with or without
5 ; modification, are permitted provided that the following conditions
7 ; * Redistributions of source code must retain the above copyright
8 ; notice, this list of conditions and the following disclaimer.
9 ; * Redistributions in binary form must reproduce the above copyright
10 ; notice, this list of conditions and the following disclaimer in
11 ; the documentation and/or other materials provided with the
13 ; * Neither the name of Intel Corporation nor the names of its
14 ; contributors may be used to endorse or promote products derived
15 ; from this software without specific prior written permission.
17 ; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 ; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 ; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 ; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 ; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 ; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 ; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 ; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 ; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 ; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 ; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
33 ; UINT32 init_crc, //initial CRC value, 32 bits
34 ; const unsigned char *buf, //buffer pointer to calculate CRC on
35 ; UINT64 len //buffer length in bytes (64-bit data)
43 ; Reference paper titled "Fast CRC Computation for Generic Polynomials Using PCLMULQDQ Instruction"
44 ; URL: http://download.intel.com/design/intarch/papers/323102.pdf
47 ; sample yasm command line:
48 ; yasm -f x64 -f elf64 -X gnu -g dwarf2 crc32_gzip
51 ; http://docs.oracle.com/javase/7/docs/api/java/util/zip/package-summary.html
52 ; CRC-32 checksum is described in RFC 1952
53 ; Implementing RFC 1952 CRC:
54 ; http://www.ietf.org/rfc/rfc1952.txt
56 %include "reg_sizes.asm"
64 %ifidn __OUTPUT_FORMAT__, win64
69 %xdefine arg1_low32 ecx
75 %xdefine arg1_low32 edi
79 %ifidn __OUTPUT_FORMAT__, win64
81 %define VARIABLE_OFFSET 16*10+8
83 %define VARIABLE_OFFSET 16*2+8
90 ; unsigned long c = crc ^ 0xffffffffL;
94 sub rsp, VARIABLE_OFFSET
95 %ifidn __OUTPUT_FORMAT__, win64
96 ; push the xmm registers into the stack to maintain
97 movdqa [rsp + XMM_SAVE + 16*0], xmm6
98 movdqa [rsp + XMM_SAVE + 16*1], xmm7
99 movdqa [rsp + XMM_SAVE + 16*2], xmm8
100 movdqa [rsp + XMM_SAVE + 16*3], xmm9
101 movdqa [rsp + XMM_SAVE + 16*4], xmm10
102 movdqa [rsp + XMM_SAVE + 16*5], xmm11
103 movdqa [rsp + XMM_SAVE + 16*6], xmm12
104 movdqa [rsp + XMM_SAVE + 16*7], xmm13
107 ; check if smaller than 256B
110 ; for sizes less than 256, we can't fold 128B at a time...
114 ; load the initial crc value
115 movd xmm10, arg1_low32 ; initial crc
117 ; receive the initial 64B data, xor the initial crc value
118 movdqu xmm0, [arg2+16*0]
119 movdqu xmm1, [arg2+16*1]
120 movdqu xmm2, [arg2+16*2]
121 movdqu xmm3, [arg2+16*3]
122 movdqu xmm4, [arg2+16*4]
123 movdqu xmm5, [arg2+16*5]
124 movdqu xmm6, [arg2+16*6]
125 movdqu xmm7, [arg2+16*7]
127 ; XOR the initial_crc value
129 movdqa xmm10, [rk3] ;xmm10 has rk3 and rk4
130 ;imm value of pclmulqdq instruction will determine which constant to use
131 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
132 ; we subtract 256 instead of 128 to save one instruction from the loop
135 ; at this section of the code, there is 128*x+y (0<=y<128) bytes of buffer. The _fold_128_B_loop
136 ; loop will fold 128B at a time until we have 128+y Bytes of buffer
139 ; fold 128B at a time. This section of the code folds 8 xmm registers in parallel
142 ; update the buffer pointer
145 movdqu xmm9, [arg2+16*0]
146 movdqu xmm12, [arg2+16*1]
149 pclmulqdq xmm0, xmm10, 0x10
150 pclmulqdq xmm8, xmm10 , 0x1
151 pclmulqdq xmm1, xmm10, 0x10
152 pclmulqdq xmm13, xmm10 , 0x1
158 movdqu xmm9, [arg2+16*2]
159 movdqu xmm12, [arg2+16*3]
162 pclmulqdq xmm2, xmm10, 0x10
163 pclmulqdq xmm8, xmm10 , 0x1
164 pclmulqdq xmm3, xmm10, 0x10
165 pclmulqdq xmm13, xmm10 , 0x1
171 movdqu xmm9, [arg2+16*4]
172 movdqu xmm12, [arg2+16*5]
175 pclmulqdq xmm4, xmm10, 0x10
176 pclmulqdq xmm8, xmm10 , 0x1
177 pclmulqdq xmm5, xmm10, 0x10
178 pclmulqdq xmm13, xmm10 , 0x1
184 movdqu xmm9, [arg2+16*6]
185 movdqu xmm12, [arg2+16*7]
188 pclmulqdq xmm6, xmm10, 0x10
189 pclmulqdq xmm8, xmm10 , 0x1
190 pclmulqdq xmm7, xmm10, 0x10
191 pclmulqdq xmm13, xmm10 , 0x1
199 ; check if there is another 128B in the buffer to be able to fold
201 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
205 ; at this point, the buffer pointer is pointing at the last y Bytes of the buffer, where 0 <= y < 128
206 ; the 128B of folded data is in 8 of the xmm registers: xmm0, xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7
209 ; fold the 8 xmm registers to 1 xmm register with different constants
213 pclmulqdq xmm0, xmm10, 0x1
214 pclmulqdq xmm8, xmm10, 0x10
220 pclmulqdq xmm1, xmm10, 0x1
221 pclmulqdq xmm8, xmm10, 0x10
227 pclmulqdq xmm2, xmm10, 0x1
228 pclmulqdq xmm8, xmm10, 0x10
234 pclmulqdq xmm3, xmm10, 0x1
235 pclmulqdq xmm8, xmm10, 0x10
241 pclmulqdq xmm4, xmm10, 0x1
242 pclmulqdq xmm8, xmm10, 0x10
248 pclmulqdq xmm5, xmm10, 0x1
249 pclmulqdq xmm8, xmm10, 0x10
255 pclmulqdq xmm6, xmm10, 0x1
256 pclmulqdq xmm8, xmm10, 0x10
261 ; instead of 128, we add 128-16 to the loop counter to save 1 instruction from the loop
262 ; instead of a cmp instruction, we use the negative flag with the jl instruction
264 jl _final_reduction_for_128
266 ; now we have 16+y bytes left to reduce. 16 Bytes is in register xmm7 and the rest is in memory
267 ; we can fold 16 bytes at a time if y>=16
268 ; continue folding 16B at a time
272 pclmulqdq xmm7, xmm10, 0x1
273 pclmulqdq xmm8, xmm10, 0x10
279 ; instead of a cmp instruction, we utilize the flags with the jge instruction
280 ; equivalent of: cmp arg3, 16-16
281 ; check if there is any more 16B in the buffer to be able to fold
282 jge _16B_reduction_loop
284 ;now we have 16+z bytes left to reduce, where 0<= z < 16.
285 ;first, we reduce the data in the xmm7 register
288 _final_reduction_for_128:
292 ; here we are getting data that is less than 16 bytes.
293 ; since we know that there was data before the pointer, we can offset the input pointer before the actual point, to receive exactly 16 bytes.
294 ; after that the registers need to be adjusted.
299 movdqu xmm1, [arg2 - 16 + arg3]
301 ; get rid of the extra data that was loaded before
302 ; load the shift constant
303 lea rax, [pshufb_shf_table]
312 pblendvb xmm2, xmm1 ;xmm0 is implicit
315 pclmulqdq xmm7, xmm10, 0x1
317 pclmulqdq xmm8, xmm10, 0x10
322 ; compute crc of a 128-bit value
327 pclmulqdq xmm7, xmm10, 0
334 pclmulqdq xmm7, xmm10, 0x10
346 pclmulqdq xmm7, xmm10, 0
350 pclmulqdq xmm7, xmm10, 0x10
356 ; return c ^ 0xffffffffL;
360 %ifidn __OUTPUT_FORMAT__, win64
361 movdqa xmm6, [rsp + XMM_SAVE + 16*0]
362 movdqa xmm7, [rsp + XMM_SAVE + 16*1]
363 movdqa xmm8, [rsp + XMM_SAVE + 16*2]
364 movdqa xmm9, [rsp + XMM_SAVE + 16*3]
365 movdqa xmm10, [rsp + XMM_SAVE + 16*4]
366 movdqa xmm11, [rsp + XMM_SAVE + 16*5]
367 movdqa xmm12, [rsp + XMM_SAVE + 16*6]
368 movdqa xmm13, [rsp + XMM_SAVE + 16*7]
370 add rsp, VARIABLE_OFFSET
374 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
375 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
376 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
377 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
382 ; check if there is enough buffer to be able to fold 16B at a time
386 ; if there is, load the constants
387 movdqa xmm10, [rk1] ; rk1 and rk2 in xmm10
389 movd xmm0, arg1_low32 ; get the initial crc value
390 movdqu xmm7, [arg2] ; load the plaintext
393 ; update the buffer pointer
396 ; update the counter. subtract 32 instead of 16 to save one instruction from the loop
399 jmp _16B_reduction_loop
404 ; mov initial crc to the return value. this is necessary for zero-length buffers.
409 movd xmm0, arg1_low32 ; get the initial crc value
413 jl _less_than_16_left
415 movdqu xmm7, [arg2] ; load the plaintext
416 pxor xmm7, xmm0 ; xor the initial crc value
419 movdqa xmm10, [rk1] ; rk1 and rk2 in xmm10
420 jmp _get_last_two_xmms
425 ; use stack space to load data less than 16 bytes, zero-out the 16B in memory first.
434 ; backup the counter value
477 pxor xmm7, xmm0 ; xor the initial crc value
479 lea rax,[pshufb_shf_table]
480 movdqu xmm0, [rax + r9]
490 pxor xmm7, xmm0 ; xor the initial crc value
509 pxor xmm7, xmm0 ; xor the initial crc value
526 pxor xmm7, xmm0 ; xor the initial crc value
538 pxor xmm7, xmm0 ; xor the initial crc value
546 ; precomputed constants
549 DQ 0x00000000ccaa009e
551 DQ 0x00000001751997d0
553 DQ 0x000000014a7fe880
555 DQ 0x00000001e88ef372
557 DQ 0x00000000ccaa009e
559 DQ 0x0000000163cd6124
561 DQ 0x00000001f7011640
563 DQ 0x00000001db710640
565 DQ 0x00000001d7cfc6ac
567 DQ 0x00000001ea89367e
569 DQ 0x000000018cb44e58
571 DQ 0x00000000df068dc2
573 DQ 0x00000000ae0b5394
575 DQ 0x00000001c7569e54
577 DQ 0x00000001c6e41596
579 DQ 0x0000000154442bd4
581 DQ 0x0000000174359406
583 DQ 0x000000003db1ecdc
585 DQ 0x000000015a546366
587 DQ 0x00000000f1da05aa
591 ; use these values for shift constants for the pshufb instruction
592 ; different alignments result in values as shown:
593 ; dq 0x8887868584838281, 0x008f8e8d8c8b8a89 ; shl 15 (16-1) / shr1
594 ; dq 0x8988878685848382, 0x01008f8e8d8c8b8a ; shl 14 (16-3) / shr2
595 ; dq 0x8a89888786858483, 0x0201008f8e8d8c8b ; shl 13 (16-4) / shr3
596 ; dq 0x8b8a898887868584, 0x030201008f8e8d8c ; shl 12 (16-4) / shr4
597 ; dq 0x8c8b8a8988878685, 0x04030201008f8e8d ; shl 11 (16-5) / shr5
598 ; dq 0x8d8c8b8a89888786, 0x0504030201008f8e ; shl 10 (16-6) / shr6
599 ; dq 0x8e8d8c8b8a898887, 0x060504030201008f ; shl 9 (16-7) / shr7
600 ; dq 0x8f8e8d8c8b8a8988, 0x0706050403020100 ; shl 8 (16-8) / shr8
601 ; dq 0x008f8e8d8c8b8a89, 0x0807060504030201 ; shl 7 (16-9) / shr9
602 ; dq 0x01008f8e8d8c8b8a, 0x0908070605040302 ; shl 6 (16-10) / shr10
603 ; dq 0x0201008f8e8d8c8b, 0x0a09080706050403 ; shl 5 (16-11) / shr11
604 ; dq 0x030201008f8e8d8c, 0x0b0a090807060504 ; shl 4 (16-12) / shr12
605 ; dq 0x04030201008f8e8d, 0x0c0b0a0908070605 ; shl 3 (16-13) / shr13
606 ; dq 0x0504030201008f8e, 0x0d0c0b0a09080706 ; shl 2 (16-14) / shr14
607 ; dq 0x060504030201008f, 0x0e0d0c0b0a090807 ; shl 1 (16-15) / shr15
608 dq 0x8786858483828100, 0x8f8e8d8c8b8a8988
609 dq 0x0706050403020100, 0x000e0d0c0b0a0908
613 dq 0xFFFFFFFFFFFFFFFF, 0x0000000000000000
615 dq 0xFFFFFFFF00000000, 0xFFFFFFFFFFFFFFFF
617 dq 0x8080808080808080, 0x8080808080808080