1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ; Copyright(c) 2011-2017 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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
32 ; UINT32 crc32_gzip_refl_by8(
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_refl_by8
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"
58 %define fetch_dist 1024
66 %ifidn __OUTPUT_FORMAT__, win64
71 %xdefine arg1_low32 ecx
77 %xdefine arg1_low32 edi
81 %ifidn __OUTPUT_FORMAT__, win64
83 %define VARIABLE_OFFSET 16*10+8
85 %define VARIABLE_OFFSET 16*2+8
89 global crc32_gzip_refl_by8:ISAL_SYM_TYPE_FUNCTION
92 ; unsigned long c = crc ^ 0xffffffffL;
96 sub rsp, VARIABLE_OFFSET
97 %ifidn __OUTPUT_FORMAT__, win64
98 ; push the xmm registers into the stack to maintain
99 movdqa [rsp + XMM_SAVE + 16*0], xmm6
100 movdqa [rsp + XMM_SAVE + 16*1], xmm7
101 movdqa [rsp + XMM_SAVE + 16*2], xmm8
102 movdqa [rsp + XMM_SAVE + 16*3], xmm9
103 movdqa [rsp + XMM_SAVE + 16*4], xmm10
104 movdqa [rsp + XMM_SAVE + 16*5], xmm11
105 movdqa [rsp + XMM_SAVE + 16*6], xmm12
106 movdqa [rsp + XMM_SAVE + 16*7], xmm13
109 ; check if smaller than 256B
112 ; for sizes less than 256, we can't fold 128B at a time...
116 ; load the initial crc value
117 movd xmm10, arg1_low32 ; initial crc
119 ; receive the initial 64B data, xor the initial crc value
120 movdqu xmm0, [arg2+16*0]
121 movdqu xmm1, [arg2+16*1]
122 movdqu xmm2, [arg2+16*2]
123 movdqu xmm3, [arg2+16*3]
124 movdqu xmm4, [arg2+16*4]
125 movdqu xmm5, [arg2+16*5]
126 movdqu xmm6, [arg2+16*6]
127 movdqu xmm7, [arg2+16*7]
129 ; XOR the initial_crc value
131 movdqa xmm10, [rk3] ;xmm10 has rk3 and rk4
132 ;imm value of pclmulqdq instruction will determine which constant to use
133 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
134 ; we subtract 256 instead of 128 to save one instruction from the loop
137 ; at this section of the code, there is 128*x+y (0<=y<128) bytes of buffer. The _fold_128_B_loop
138 ; loop will fold 128B at a time until we have 128+y Bytes of buffer
141 ; fold 128B at a time. This section of the code folds 8 xmm registers in parallel
144 ; update the buffer pointer
147 prefetchnta [arg2+fetch_dist+0]
148 movdqu xmm9, [arg2+16*0]
149 movdqu xmm12, [arg2+16*1]
152 pclmulqdq xmm0, xmm10, 0x10
153 pclmulqdq xmm8, xmm10 , 0x1
154 pclmulqdq xmm1, xmm10, 0x10
155 pclmulqdq xmm13, xmm10 , 0x1
161 prefetchnta [arg2+fetch_dist+32]
162 movdqu xmm9, [arg2+16*2]
163 movdqu xmm12, [arg2+16*3]
166 pclmulqdq xmm2, xmm10, 0x10
167 pclmulqdq xmm8, xmm10 , 0x1
168 pclmulqdq xmm3, xmm10, 0x10
169 pclmulqdq xmm13, xmm10 , 0x1
175 prefetchnta [arg2+fetch_dist+64]
176 movdqu xmm9, [arg2+16*4]
177 movdqu xmm12, [arg2+16*5]
180 pclmulqdq xmm4, xmm10, 0x10
181 pclmulqdq xmm8, xmm10 , 0x1
182 pclmulqdq xmm5, xmm10, 0x10
183 pclmulqdq xmm13, xmm10 , 0x1
189 prefetchnta [arg2+fetch_dist+96]
190 movdqu xmm9, [arg2+16*6]
191 movdqu xmm12, [arg2+16*7]
194 pclmulqdq xmm6, xmm10, 0x10
195 pclmulqdq xmm8, xmm10 , 0x1
196 pclmulqdq xmm7, xmm10, 0x10
197 pclmulqdq xmm13, xmm10 , 0x1
205 ; check if there is another 128B in the buffer to be able to fold
207 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
211 ; at this point, the buffer pointer is pointing at the last y Bytes of the buffer, where 0 <= y < 128
212 ; the 128B of folded data is in 8 of the xmm registers: xmm0, xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7
215 ; fold the 8 xmm registers to 1 xmm register with different constants
219 pclmulqdq xmm0, xmm10, 0x1
220 pclmulqdq xmm8, xmm10, 0x10
226 pclmulqdq xmm1, xmm10, 0x1
227 pclmulqdq xmm8, xmm10, 0x10
233 pclmulqdq xmm2, xmm10, 0x1
234 pclmulqdq xmm8, xmm10, 0x10
240 pclmulqdq xmm3, xmm10, 0x1
241 pclmulqdq xmm8, xmm10, 0x10
247 pclmulqdq xmm4, xmm10, 0x1
248 pclmulqdq xmm8, xmm10, 0x10
254 pclmulqdq xmm5, xmm10, 0x1
255 pclmulqdq xmm8, xmm10, 0x10
261 pclmulqdq xmm6, xmm10, 0x1
262 pclmulqdq xmm8, xmm10, 0x10
267 ; instead of 128, we add 128-16 to the loop counter to save 1 instruction from the loop
268 ; instead of a cmp instruction, we use the negative flag with the jl instruction
270 jl _final_reduction_for_128
272 ; now we have 16+y bytes left to reduce. 16 Bytes is in register xmm7 and the rest is in memory
273 ; we can fold 16 bytes at a time if y>=16
274 ; continue folding 16B at a time
278 pclmulqdq xmm7, xmm10, 0x1
279 pclmulqdq xmm8, xmm10, 0x10
285 ; instead of a cmp instruction, we utilize the flags with the jge instruction
286 ; equivalent of: cmp arg3, 16-16
287 ; check if there is any more 16B in the buffer to be able to fold
288 jge _16B_reduction_loop
290 ;now we have 16+z bytes left to reduce, where 0<= z < 16.
291 ;first, we reduce the data in the xmm7 register
294 _final_reduction_for_128:
298 ; here we are getting data that is less than 16 bytes.
299 ; 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.
300 ; after that the registers need to be adjusted.
305 movdqu xmm1, [arg2 - 16 + arg3]
307 ; get rid of the extra data that was loaded before
308 ; load the shift constant
309 lea rax, [pshufb_shf_table]
318 pblendvb xmm2, xmm1 ;xmm0 is implicit
321 pclmulqdq xmm7, xmm10, 0x1
323 pclmulqdq xmm8, xmm10, 0x10
328 ; compute crc of a 128-bit value
333 pclmulqdq xmm7, xmm10, 0
340 pclmulqdq xmm7, xmm10, 0x10
352 pclmulqdq xmm7, xmm10, 0
356 pclmulqdq xmm7, xmm10, 0x10
362 ; return c ^ 0xffffffffL;
366 %ifidn __OUTPUT_FORMAT__, win64
367 movdqa xmm6, [rsp + XMM_SAVE + 16*0]
368 movdqa xmm7, [rsp + XMM_SAVE + 16*1]
369 movdqa xmm8, [rsp + XMM_SAVE + 16*2]
370 movdqa xmm9, [rsp + XMM_SAVE + 16*3]
371 movdqa xmm10, [rsp + XMM_SAVE + 16*4]
372 movdqa xmm11, [rsp + XMM_SAVE + 16*5]
373 movdqa xmm12, [rsp + XMM_SAVE + 16*6]
374 movdqa xmm13, [rsp + XMM_SAVE + 16*7]
376 add rsp, VARIABLE_OFFSET
380 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
381 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
382 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
383 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
388 ; check if there is enough buffer to be able to fold 16B at a time
392 ; if there is, load the constants
393 movdqa xmm10, [rk1] ; rk1 and rk2 in xmm10
395 movd xmm0, arg1_low32 ; get the initial crc value
396 movdqu xmm7, [arg2] ; load the plaintext
399 ; update the buffer pointer
402 ; update the counter. subtract 32 instead of 16 to save one instruction from the loop
405 jmp _16B_reduction_loop
410 ; mov initial crc to the return value. this is necessary for zero-length buffers.
415 movd xmm0, arg1_low32 ; get the initial crc value
419 jl _less_than_16_left
421 movdqu xmm7, [arg2] ; load the plaintext
422 pxor xmm7, xmm0 ; xor the initial crc value
425 movdqa xmm10, [rk1] ; rk1 and rk2 in xmm10
426 jmp _get_last_two_xmms
431 ; use stack space to load data less than 16 bytes, zero-out the 16B in memory first.
440 ; backup the counter value
483 pxor xmm7, xmm0 ; xor the initial crc value
485 lea rax,[pshufb_shf_table]
486 movdqu xmm0, [rax + r9]
496 pxor xmm7, xmm0 ; xor the initial crc value
515 pxor xmm7, xmm0 ; xor the initial crc value
532 pxor xmm7, xmm0 ; xor the initial crc value
544 pxor xmm7, xmm0 ; xor the initial crc value
552 ; precomputed constants
555 DQ 0x00000000ccaa009e
557 DQ 0x00000001751997d0
559 DQ 0x000000014a7fe880
561 DQ 0x00000001e88ef372
563 DQ 0x00000000ccaa009e
565 DQ 0x0000000163cd6124
567 DQ 0x00000001f7011640
569 DQ 0x00000001db710640
571 DQ 0x00000001d7cfc6ac
573 DQ 0x00000001ea89367e
575 DQ 0x000000018cb44e58
577 DQ 0x00000000df068dc2
579 DQ 0x00000000ae0b5394
581 DQ 0x00000001c7569e54
583 DQ 0x00000001c6e41596
585 DQ 0x0000000154442bd4
587 DQ 0x0000000174359406
589 DQ 0x000000003db1ecdc
591 DQ 0x000000015a546366
593 DQ 0x00000000f1da05aa
596 dq 0xFFFFFFFFFFFFFFFF, 0x0000000000000000
598 dq 0xFFFFFFFF00000000, 0xFFFFFFFFFFFFFFFF
600 dq 0x8080808080808080, 0x8080808080808080
603 ; use these values for shift constants for the pshufb instruction
604 ; different alignments result in values as shown:
605 ; dq 0x8887868584838281, 0x008f8e8d8c8b8a89 ; shl 15 (16-1) / shr1
606 ; dq 0x8988878685848382, 0x01008f8e8d8c8b8a ; shl 14 (16-3) / shr2
607 ; dq 0x8a89888786858483, 0x0201008f8e8d8c8b ; shl 13 (16-4) / shr3
608 ; dq 0x8b8a898887868584, 0x030201008f8e8d8c ; shl 12 (16-4) / shr4
609 ; dq 0x8c8b8a8988878685, 0x04030201008f8e8d ; shl 11 (16-5) / shr5
610 ; dq 0x8d8c8b8a89888786, 0x0504030201008f8e ; shl 10 (16-6) / shr6
611 ; dq 0x8e8d8c8b8a898887, 0x060504030201008f ; shl 9 (16-7) / shr7
612 ; dq 0x8f8e8d8c8b8a8988, 0x0706050403020100 ; shl 8 (16-8) / shr8
613 ; dq 0x008f8e8d8c8b8a89, 0x0807060504030201 ; shl 7 (16-9) / shr9
614 ; dq 0x01008f8e8d8c8b8a, 0x0908070605040302 ; shl 6 (16-10) / shr10
615 ; dq 0x0201008f8e8d8c8b, 0x0a09080706050403 ; shl 5 (16-11) / shr11
616 ; dq 0x030201008f8e8d8c, 0x0b0a090807060504 ; shl 4 (16-12) / shr12
617 ; dq 0x04030201008f8e8d, 0x0c0b0a0908070605 ; shl 3 (16-13) / shr13
618 ; dq 0x0504030201008f8e, 0x0d0c0b0a09080706 ; shl 2 (16-14) / shr14
619 ; dq 0x060504030201008f, 0x0e0d0c0b0a090807 ; shl 1 (16-15) / shr15
620 dq 0x8786858483828100, 0x8f8e8d8c8b8a8988
621 dq 0x0706050403020100, 0x000e0d0c0b0a0908
623 ;;; func core, ver, snum
624 slversion crc32_gzip_refl_by8, 01, 00, 002c