xref: /reactos/drivers/filesystems/btrfs/crc32c.S (revision bbccad0e)
1/* Copyright (c) Mark Harmstone 2020
2 *
3 * This file is part of WinBtrfs.
4 *
5 * WinBtrfs is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public Licence as published by
7 * the Free Software Foundation, either version 3 of the Licence, or
8 * (at your option) any later version.
9 *
10 * WinBtrfs is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU Lesser General Public Licence for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public Licence
16 * along with WinBtrfs.  If not, see <http://www.gnu.org/licenses/>. */
17
18#include <asm.inc>
19
20#ifdef __x86_64__
21
22EXTERN crctable:QWORD
23
24.code64
25
26/* uint32_t __stdcall calc_crc32c_sw(uint32_t seed, uint8_t* msg, uint32_t msglen); */
27
28PUBLIC calc_crc32c_sw
29calc_crc32c_sw:
30
31/* rax = crc / seed
32 * rdx = buf
33 * r8 = len
34 * rcx = tmp
35 * r10 = tmp2 */
36
37mov rax, rcx
38
39crcloop:
40test r8, r8
41jz crcend
42
43mov rcx, rax
44shr rcx, 8
45mov r10b, byte ptr [rdx]
46xor al, r10b
47and rax, 255
48shl rax, 2
49lea r10, [rip+crctable]
50mov eax, dword ptr [r10 + rax]
51xor rax, rcx
52
53inc rdx
54dec r8
55
56jmp crcloop
57
58crcend:
59ret
60
61/****************************************************/
62
63/* uint32_t __stdcall calc_crc32c_hw(uint32_t seed, uint8_t* msg, uint32_t msglen); */
64
65PUBLIC calc_crc32c_hw
66
67calc_crc32c_hw:
68
69/* rax = crc / seed
70 * rdx = buf
71 * r8 = len */
72
73mov rax, rcx
74
75crchw_loop:
76cmp r8, 8
77jl crchw_stragglers
78
79crc32 rax, qword ptr [rdx]
80
81add rdx, 8
82sub r8, 8
83jmp crchw_loop
84
85crchw_stragglers:
86cmp r8, 4
87jl crchw_stragglers2
88
89crc32 eax, dword ptr [rdx]
90
91add rdx, 4
92sub r8, 4
93
94crchw_stragglers2:
95cmp r8, 2
96jl crchw_stragglers3
97
98crc32 eax, word ptr [rdx]
99
100add rdx, 2
101sub r8, 2
102
103crchw_stragglers3:
104test r8, r8
105jz crchw_end
106
107crc32 eax, byte ptr [rdx]
108inc rdx
109dec r8
110jmp crchw_stragglers3
111
112crchw_end:
113ret
114
115END
116#elif defined(_X86_)
117
118EXTERN _crctable:DWORD
119.code
120
121/* uint32_t __stdcall calc_crc32c_sw(uint32_t seed, uint8_t* msg, uint32_t msglen); */
122
123PUBLIC _calc_crc32c_sw@12
124_calc_crc32c_sw@12:
125
126push ebp
127mov ebp, esp
128
129push esi
130push ebx
131
132mov eax, [ebp+8]
133mov edx, [ebp+12]
134mov ebx, [ebp+16]
135
136/* eax = crc / seed
137 * ebx = len
138 * esi = tmp
139 * edx = buf
140 * ecx = tmp2 */
141
142crcloop:
143test ebx, ebx
144jz crcend
145
146mov esi, eax
147shr esi, 8
148mov cl, byte ptr [edx]
149xor al, cl
150and eax, 255
151shl eax, 2
152mov eax, [_crctable + eax]
153xor eax, esi
154
155inc edx
156dec ebx
157
158jmp crcloop
159
160crcend:
161pop ebx
162pop esi
163
164pop ebp
165
166ret 12
167
168/****************************************************/
169
170/* uint32_t __stdcall calc_crc32c_hw(uint32_t seed, uint8_t* msg, uint32_t msglen); */
171
172PUBLIC _calc_crc32c_hw@12
173_calc_crc32c_hw@12:
174
175push ebp
176mov ebp, esp
177
178mov eax, [ebp+8]
179mov edx, [ebp+12]
180mov ecx, [ebp+16]
181
182/* eax = crc / seed
183 * ecx = len
184 * edx = buf */
185
186crchw_loop:
187cmp ecx, 4
188jl crchw_stragglers
189
190crc32 eax, dword ptr [edx]
191
192add edx, 4
193sub ecx, 4
194jmp crchw_loop
195
196crchw_stragglers:
197cmp ecx, 2
198jl crchw_stragglers2
199
200crc32 eax, word ptr [edx]
201
202add edx, 2
203sub ecx, 2
204
205crchw_stragglers2:
206test ecx, ecx
207jz crchw_end
208
209crc32 eax, byte ptr [edx]
210inc edx
211dec ecx
212jmp crchw_stragglers2
213
214crchw_end:
215pop ebp
216
217ret 12
218
219END
220#endif
221