Text file
src/hash/crc32/crc32_riscv64.s
1 // Copyright 2026 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 #include "textflag.h"
6
7 // IEEE CRC32 constants (reflected polynomial 0xEDB88320)
8
9 // Fold-by-128-bit (16-byte) constants for IEEE CRC32 reflected.
10 // K1 and K2 are used to fold a 128-bit accumulator with new 16-byte data.
11 DATA ieee_fold<>+0(SB)/8, $0xccaa009e // K2 (fold high)
12 DATA ieee_fold<>+8(SB)/8, $0xae689191 // K1 (fold low)
13 GLOBL ieee_fold<>(SB), RODATA, $16
14
15 // Final reduction constants for IEEE CRC32 reflected.
16 // const_low: 128->64 bit fold constant (low)
17 // const_high: 128->64 bit fold constant (high)
18 // const_quo: Barrett reduction quotient mu = floor(x^64 / P(x))
19 // const_poly: CRC32-IEEE polynomial P(x) with implicit x^32 term
20 DATA ieee_reduce<>+0(SB)/8, $0xb8bc6765 // const_low
21 DATA ieee_reduce<>+8(SB)/8, $0xccaa009e // const_high
22 DATA ieee_reduce<>+16(SB)/8, $0x1f7011641 // const_quo (mu)
23 DATA ieee_reduce<>+24(SB)/8, $0x1db710641 // const_poly (P)
24 GLOBL ieee_reduce<>(SB), RODATA, $32
25
26 // Castagnoli CRC32-C constants (reflected polynomial 0x82F63B78)
27 // Fold-by-128-bit (16-byte) constants for Castagnoli CRC32-C reflected.
28 DATA cast_fold<>+0(SB)/8, $0x493c7d27 // K2 (fold high)
29 DATA cast_fold<>+8(SB)/8, $0xf20c0dfe // K1 (fold low)
30 GLOBL cast_fold<>(SB), RODATA, $16
31
32 // Final reduction constants for Castagnoli CRC32-C reflected.
33 DATA cast_reduce<>+0(SB)/8, $0xdd45aab8 // const_low
34 DATA cast_reduce<>+8(SB)/8, $0x493c7d27 // const_high
35 DATA cast_reduce<>+16(SB)/8, $0x0dea713f1 // const_quo (mu)
36 DATA cast_reduce<>+24(SB)/8, $0x105ec76f1 // const_poly (P)
37 GLOBL cast_reduce<>(SB), RODATA, $32
38
39 // 32-bit mask constant
40 DATA mask32<>+0(SB)/8, $0xffffffff
41 GLOBL mask32<>(SB), RODATA, $8
42
43 // Consume one byte at (X6): update the CRC in X5, then advance the data
44 // pointer X6 by 1 and decrement the remaining length X7 by 1.
45 // Table pointer is in X30. Clobbers X13, X14, X15.
46 //
47 // Formula (matches simpleUpdate, on the non-inverted crc held in X5):
48 // crc = tab[(crc ^ data) & 0xff] ^ (crc >> 8)
49 // Table entries are 32-bit; use MOVWU (zero-extending) so an entry with its
50 // high bit set doesn't sign-extend and pollute crc's high 32 bits.
51 #define CRC_BYTE() \
52 MOVBU (X6), X13; \
53 AND $0xff, X5, X14; \
54 XOR X13, X14, X14; \
55 SLL $2, X14, X14; \
56 ADD X30, X14, X14; \
57 MOVWU (X14), X15; \
58 SRLI $8, X5, X5; \
59 XOR X15, X5, X5; \
60 ADD $1, X6; \
61 SUB $1, X7
62
63 // Carry-less multiply fold step on the 128-bit accumulator (X8, X9) with a
64 // freshly loaded 16-byte block (X13=low, X14=high), producing the new
65 // accumulator in X8, X9. Constants K2 (X10) and K1 (X11) must already be
66 // loaded. Clobbers X15-X18.
67 #define FOLD_STEP() \
68 CLMUL X11, X8, X15; /* clmul(K1, t0) -> low result */ \
69 CLMULH X11, X8, X16; /* clmulh(K1, t0) -> high result */ \
70 CLMUL X10, X9, X17; /* clmul(K2, t1) -> low result */ \
71 CLMULH X10, X9, X18; /* clmulh(K2, t1) -> high result */ \
72 XOR X15, X17, X8; /* t0 = fold_low */ \
73 XOR X8, X13, X8; /* t0 ^= d0 */ \
74 XOR X16, X18, X9; /* t1 = fold_high */ \
75 XOR X9, X14, X9 /* t1 ^= d1 */
76
77 // Computes CRC32-IEEE using carry-less multiplication (Zbc extension).
78 // Expects non-inverted CRC input. Returns non-inverted CRC.
79 //
80 // Only handles len(p) >= 16: the caller archUpdateIEEE passes complete
81 // 16-byte blocks (len(p) &^ 15); shorter inputs go through slicingUpdate.
82 // A misaligned p is byte-aligned to a 16-byte boundary first (CRC_BYTE), so
83 // every LD in the fold loop is at least 8-byte aligned.
84 TEXT ·ieeeUpdateCLMUL(SB), NOSPLIT, $0-44
85 MOVWU crc+0(FP), X5 // CRC value (non-inverted)
86 MOV tab+8(FP), X30 // table pointer
87 MOV p+16(FP), X6 // data pointer
88 MOV p_len+24(FP), X7 // len(p)
89
90 // Byte-align to a 16-byte boundary: (-X6)&15 is both the alignment check
91 // (0 when already aligned) and the number of bytes to consume.
92 SUB X0, X6, X24 // X24 = -X6
93 ANDI $15, X24, X24 // X24 = bytes until next 16-byte boundary
94 BEQ X24, ZERO, ieee_aligned
95
96 ieee_align_head:
97 CRC_BYTE()
98 SUB $1, X24
99 BNE X24, ZERO, ieee_align_head
100
101 ieee_aligned:
102 // If fewer than 16 bytes remain, skip the fold loop entirely and go
103 // straight to the byte-at-a-time tail (X5 still holds the CRC).
104 MOV $16, X12
105 BLT X7, X12, ieee_tail
106
107 // Load first 16 bytes (X6 is 16-byte aligned).
108 MOV (X6), X8 // t0 = low 64 bits
109 MOV 8(X6), X9 // t1 = high 64 bits
110 ADD $16, X6
111 SUB $16, X7
112
113 // XOR CRC seed into the low word of the first block
114 XOR X5, X8, X8
115
116 // Load fold constants: K2 (high), K1 (low)
117 MOV ieee_fold<>+0(SB), X10 // K2
118 MOV ieee_fold<>+8(SB), X11 // K1
119
120 // If fewer than 16 bytes remain after the first block, skip the fold
121 // loop; the tail is handled byte-at-a-time below.
122 BLT X7, X12, ieee_fold_done
123
124 ieee_fold_loop:
125 // Process the next 16-byte block (X6 stays 16-byte aligned).
126 MOV (X6), X13 // d0 = new low 64 bits
127 MOV 8(X6), X14 // d1 = new high 64 bits
128 ADD $16, X6
129 SUB $16, X7
130
131 FOLD_STEP()
132
133 // Repeat while at least 16 bytes remain.
134 BGE X7, X12, ieee_fold_loop
135
136 ieee_fold_done:
137 // (X8, X9) = (t0, t1) holds the folded 128-bit accumulator.
138 // Now reduce 128 bits -> 64 bits -> 32 bits.
139
140 // Load final reduction constants
141 MOV ieee_reduce<>+0(SB), X10 // const_low
142 MOV ieee_reduce<>+8(SB), X11 // const_high
143
144 // 128-bit -> 64-bit folding
145 // Formula (from ISA-L reflected CRC):
146 // t4 = clmul(t0, const_high)
147 // t3h = clmulh(t0, const_high)
148 // t1 = t1 XOR t4
149 // low32 = t1 & 0xFFFFFFFF
150 // high32 = t1 >> 32
151 // f = clmul(low32, const_low)
152 // result = (t3h << 32) XOR high32 XOR f
153 CLMUL X11, X8, X12 // t4 = clmul(t0, const_high)
154 CLMULH X11, X8, X13 // t3h = clmulh(t0, const_high)
155 XOR X9, X12, X9 // t1 = t1 XOR t4
156
157 // Extract low 32 and high 32 bits from t1
158 MOV mask32<>(SB), X20 // X20 = 0xFFFFFFFF
159 AND X9, X20, X14 // low32 = t1 & 0xFFFFFFFF
160 SRLI $32, X9, X15 // high32 = t1 >> 32
161
162 CLMUL X10, X14, X16 // f = clmul(low32, const_low)
163 SLLI $32, X13, X13 // t3h <<= 32
164 XOR X13, X15, X17 // result = (t3h << 32) XOR high32
165 XOR X17, X16, X17 // result ^= f
166
167 // Barrett reduction: 64 bits -> 32 bits
168 // Formula:
169 // tmp = clmul(result_low32, mu)
170 // tmp = clmul(tmp_low32, P)
171 // crc = (result XOR tmp) >> 32
172 AND X17, X20, X18 // low32 = result & 0xFFFFFFFF
173 MOV ieee_reduce<>+16(SB), X21 // const_quo (mu)
174 MOV ieee_reduce<>+24(SB), X22 // const_poly (P)
175 CLMUL X21, X18, X18 // tmp = clmul(low32, mu)
176 AND X18, X20, X18 // tmp = tmp & 0xFFFFFFFF
177 CLMUL X22, X18, X18 // tmp = clmul(tmp, P)
178 XOR X17, X18, X18 // result = result XOR tmp
179 SRLI $32, X18, X5 // CRC = upper 32 bits
180
181 // Process the tail (< 16 bytes) one byte at a time.
182 ieee_tail:
183 BEQ X7, ZERO, ieee_ret
184 CRC_BYTE()
185 JMP ieee_tail
186 ieee_ret:
187 MOVW X5, ret+40(FP)
188 RET
189
190
191 // Computes CRC32-C (Castagnoli) using carry-less multiplication (Zbc extension).
192 // Expects non-inverted CRC input. Returns non-inverted CRC.
193 //
194 // Only handles len(p) >= 16: the caller archUpdateCastagnoli passes complete
195 // 16-byte blocks (len(p) &^ 15); shorter inputs go through slicingUpdate.
196 // A misaligned p is byte-aligned to a 16-byte boundary first (CRC_BYTE), so
197 // every LD in the fold loop is at least 8-byte aligned.
198 TEXT ·castagnoliUpdateCLMUL(SB), NOSPLIT, $0-44
199 MOVWU crc+0(FP), X5 // CRC value (non-inverted)
200 MOV tab+8(FP), X30 // table pointer
201 MOV p+16(FP), X6 // data pointer
202 MOV p_len+24(FP), X7 // len(p)
203
204 // Byte-align to a 16-byte boundary: (-X6)&15 is both the alignment check
205 // (0 when already aligned) and the number of bytes to consume.
206 SUB X0, X6, X24 // X24 = -X6
207 ANDI $15, X24, X24 // X24 = bytes until next 16-byte boundary
208 BEQ X24, ZERO, cast_aligned
209
210 cast_align_head:
211 CRC_BYTE()
212 SUB $1, X24
213 BNE X24, ZERO, cast_align_head
214
215 cast_aligned:
216 // If fewer than 16 bytes remain, skip the fold loop entirely and go
217 // straight to the byte-at-a-time tail (X5 still holds the CRC).
218 MOV $16, X12
219 BLT X7, X12, cast_tail
220
221 // Load first 16 bytes (X6 is 16-byte aligned).
222 MOV (X6), X8 // t0 = low 64 bits
223 MOV 8(X6), X9 // t1 = high 64 bits
224 ADD $16, X6
225 SUB $16, X7
226
227 // XOR CRC seed into the low word of the first block
228 XOR X5, X8, X8
229
230 // Load fold constants: K2 (high), K1 (low)
231 MOV cast_fold<>+0(SB), X10 // K2
232 MOV cast_fold<>+8(SB), X11 // K1
233
234 // If fewer than 16 bytes remain after the first block, skip the fold
235 // loop; the tail is handled byte-at-a-time below.
236 BLT X7, X12, cast_fold_done
237
238 cast_fold_loop:
239 // Process the next 16-byte block (X6 stays 16-byte aligned).
240 MOV (X6), X13 // d0 = new low 64 bits
241 MOV 8(X6), X14 // d1 = new high 64 bits
242 ADD $16, X6
243 SUB $16, X7
244
245 FOLD_STEP()
246
247 // Repeat while at least 16 bytes remain.
248 BGE X7, X12, cast_fold_loop
249
250 cast_fold_done:
251 // Load final reduction constants
252 MOV cast_reduce<>+0(SB), X10 // const_low
253 MOV cast_reduce<>+8(SB), X11 // const_high
254
255 // 128-bit -> 64-bit folding
256 CLMUL X11, X8, X12 // t4 = clmul(t0, const_high)
257 CLMULH X11, X8, X13 // t3h = clmulh(t0, const_high)
258 XOR X9, X12, X9 // t1 = t1 XOR t4
259
260 MOV mask32<>(SB), X20 // X20 = 0xFFFFFFFF
261 AND X9, X20, X14 // low32 = t1 & 0xFFFFFFFF
262 SRLI $32, X9, X15 // high32 = t1 >> 32
263
264 CLMUL X10, X14, X16 // f = clmul(low32, const_low)
265 SLLI $32, X13, X13 // t3h <<= 32
266 XOR X13, X15, X17 // result = (t3h << 32) XOR high32
267 XOR X17, X16, X17 // result ^= f
268
269 // Barrett reduction: 64 bits -> 32 bits
270 AND X17, X20, X18 // low32 = result & 0xFFFFFFFF
271 MOV cast_reduce<>+16(SB), X21 // const_quo (mu)
272 MOV cast_reduce<>+24(SB), X22 // const_poly (P)
273 CLMUL X21, X18, X18 // tmp = clmul(low32, mu)
274 AND X18, X20, X18 // tmp = tmp & 0xFFFFFFFF
275 CLMUL X22, X18, X18 // tmp = clmul(tmp, P)
276 XOR X17, X18, X18 // result = result XOR tmp
277 SRLI $32, X18, X5 // CRC = upper 32 bits
278
279 // Process the tail (< 16 bytes) one byte at a time.
280 cast_tail:
281 BEQ X7, ZERO, cast_ret
282 CRC_BYTE()
283 JMP cast_tail
284 cast_ret:
285 MOVW X5, ret+40(FP)
286 RET
287
View as plain text