-
Notifications
You must be signed in to change notification settings - Fork 1
/
polyval_amd64.s
271 lines (238 loc) · 4.94 KB
/
polyval_amd64.s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Code generated by command: go run asm.go -out out/polyval_amd64.s -stubs out/stub_amd64.go -pkg polyval. DO NOT EDIT.
//go:build gc && !purego
#include "textflag.h"
DATA polymask<>+0(SB)/8, $0xc200000000000000
DATA polymask<>+8(SB)/8, $0xc200000000000000
GLOBL polymask<>(SB), RODATA|NOPTR, $16
// func polymulAsm(acc *fieldElement, key *fieldElement)
// Requires: PCLMULQDQ, SSE, SSE2
TEXT ·polymulAsm(SB), NOSPLIT, $0-16
MOVQ acc+0(FP), AX
MOVQ key+8(FP), CX
MOVOU (AX), X0
MOVOU (CX), X1
MOVOU polymask<>+0(SB), X2
// Karatsuba 1
PSHUFD $0xee, X0, X3
PXOR X0, X3
PSHUFD $0xee, X1, X4
PXOR X1, X4
PCLMULQDQ $0x00, X3, X4
MOVOU X0, X3
PCLMULQDQ $0x11, X1, X3
PCLMULQDQ $0x00, X1, X0
// Karatsuba 2
MOVOU X0, X1
SHUFPS $0x4e, X3, X1
MOVOU X3, X5
PXOR X0, X5
PXOR X1, X5
PXOR X4, X5
MOVHLPS X5, X3
PUNPCKLQDQ X5, X0
// Montgomery reduce
MOVOU X2, X1
PCLMULQDQ $0x00, X0, X1
PSHUFD $0x4e, X1, X1
PXOR X0, X1
XORPS X1, X3
PCLMULQDQ $0x11, X2, X1
PXOR X3, X1
MOVOU X1, (AX)
RET
// func polymulBlocksAsm(acc *fieldElement, pow *[8]fieldElement, input *byte, nblocks int)
// Requires: PCLMULQDQ, SSE, SSE2
TEXT ·polymulBlocksAsm(SB), NOSPLIT, $0-32
MOVQ acc+0(FP), AX
MOVQ pow+8(FP), CX
MOVQ input+16(FP), DX
MOVQ nblocks+24(FP), BX
MOVOU polymask<>+0(SB), X0
MOVOU (AX), X1
MOVQ BX, SI
ANDQ $0x07, SI
JZ initWideLoop
MOVOU 112(CX), X2
singleLoop:
MOVOU (DX), X3
PXOR X1, X3
// Karatsuba 1
PSHUFD $0xee, X3, X4
PXOR X3, X4
PSHUFD $0xee, X2, X1
PXOR X2, X1
PCLMULQDQ $0x00, X4, X1
MOVOU X3, X4
PCLMULQDQ $0x11, X2, X4
PCLMULQDQ $0x00, X2, X3
// Karatsuba 2
MOVOU X3, X5
SHUFPS $0x4e, X4, X5
MOVOU X4, X6
PXOR X3, X6
PXOR X5, X6
PXOR X1, X6
MOVHLPS X6, X4
PUNPCKLQDQ X6, X3
// Montgomery reduce
MOVOU X0, X1
PCLMULQDQ $0x00, X3, X1
PSHUFD $0x4e, X1, X1
PXOR X3, X1
XORPS X1, X4
PCLMULQDQ $0x11, X0, X1
PXOR X4, X1
ADDQ $0x10, DX
SUBQ $0x01, SI
JNZ singleLoop
initWideLoop:
SHRQ $0x03, BX
JZ done
wideLoop:
PXOR X2, X2
PXOR X4, X4
PXOR X3, X3
// Block 7
MOVOU 112(DX), X5
MOVOU 112(CX), X6
// Karatsuba 1
PSHUFD $0xee, X5, X7
PXOR X5, X7
PSHUFD $0xee, X6, X8
PXOR X6, X8
PCLMULQDQ $0x00, X7, X8
MOVOU X5, X7
PCLMULQDQ $0x11, X6, X7
PCLMULQDQ $0x00, X6, X5
PXOR X7, X2
PXOR X5, X4
PXOR X8, X3
// Block 6
MOVOU 96(DX), X5
MOVOU 96(CX), X6
// Karatsuba 1
PSHUFD $0xee, X5, X7
PXOR X5, X7
PSHUFD $0xee, X6, X8
PXOR X6, X8
PCLMULQDQ $0x00, X7, X8
MOVOU X5, X7
PCLMULQDQ $0x11, X6, X7
PCLMULQDQ $0x00, X6, X5
PXOR X7, X2
PXOR X5, X4
PXOR X8, X3
// Block 5
MOVOU 80(DX), X5
MOVOU 80(CX), X6
// Karatsuba 1
PSHUFD $0xee, X5, X7
PXOR X5, X7
PSHUFD $0xee, X6, X8
PXOR X6, X8
PCLMULQDQ $0x00, X7, X8
MOVOU X5, X7
PCLMULQDQ $0x11, X6, X7
PCLMULQDQ $0x00, X6, X5
PXOR X7, X2
PXOR X5, X4
PXOR X8, X3
// Block 4
MOVOU 64(DX), X5
MOVOU 64(CX), X6
// Karatsuba 1
PSHUFD $0xee, X5, X7
PXOR X5, X7
PSHUFD $0xee, X6, X8
PXOR X6, X8
PCLMULQDQ $0x00, X7, X8
MOVOU X5, X7
PCLMULQDQ $0x11, X6, X7
PCLMULQDQ $0x00, X6, X5
PXOR X7, X2
PXOR X5, X4
PXOR X8, X3
// Block 3
MOVOU 48(DX), X5
MOVOU 48(CX), X6
// Karatsuba 1
PSHUFD $0xee, X5, X7
PXOR X5, X7
PSHUFD $0xee, X6, X8
PXOR X6, X8
PCLMULQDQ $0x00, X7, X8
MOVOU X5, X7
PCLMULQDQ $0x11, X6, X7
PCLMULQDQ $0x00, X6, X5
PXOR X7, X2
PXOR X5, X4
PXOR X8, X3
// Block 2
MOVOU 32(DX), X5
MOVOU 32(CX), X6
// Karatsuba 1
PSHUFD $0xee, X5, X7
PXOR X5, X7
PSHUFD $0xee, X6, X8
PXOR X6, X8
PCLMULQDQ $0x00, X7, X8
MOVOU X5, X7
PCLMULQDQ $0x11, X6, X7
PCLMULQDQ $0x00, X6, X5
PXOR X7, X2
PXOR X5, X4
PXOR X8, X3
// Block 1
MOVOU 16(DX), X5
MOVOU 16(CX), X6
// Karatsuba 1
PSHUFD $0xee, X5, X7
PXOR X5, X7
PSHUFD $0xee, X6, X8
PXOR X6, X8
PCLMULQDQ $0x00, X7, X8
MOVOU X5, X7
PCLMULQDQ $0x11, X6, X7
PCLMULQDQ $0x00, X6, X5
PXOR X7, X2
PXOR X5, X4
PXOR X8, X3
// Block 0
MOVOU (DX), X5
MOVOU (CX), X6
PXOR X1, X5
// Karatsuba 1
PSHUFD $0xee, X5, X1
PXOR X5, X1
PSHUFD $0xee, X6, X7
PXOR X6, X7
PCLMULQDQ $0x00, X1, X7
MOVOU X5, X1
PCLMULQDQ $0x11, X6, X1
PCLMULQDQ $0x00, X6, X5
PXOR X1, X2
PXOR X5, X4
PXOR X7, X3
// Karatsuba 2
MOVOU X4, X1
SHUFPS $0x4e, X2, X1
MOVOU X2, X5
PXOR X4, X5
PXOR X1, X5
PXOR X3, X5
MOVHLPS X5, X2
PUNPCKLQDQ X5, X4
// Montgomery reduce
MOVOU X0, X1
PCLMULQDQ $0x00, X4, X1
PSHUFD $0x4e, X1, X1
PXOR X4, X1
XORPS X1, X2
PCLMULQDQ $0x11, X0, X1
PXOR X2, X1
ADDQ $0x80, DX
SUBQ $0x01, BX
JNZ wideLoop
done:
MOVOU X1, (AX)
RET