1 /// -*- mode: asm; asm-comment-char: ?/ -*-
3 /// Fancy SIMD implementation of ChaCha
5 /// (c) 2015 Straylight/Edgeware
8 ///----- Licensing notice ---------------------------------------------------
10 /// This file is part of Catacomb.
12 /// Catacomb is free software; you can redistribute it and/or modify
13 /// it under the terms of the GNU Library General Public License as
14 /// published by the Free Software Foundation; either version 2 of the
15 /// License, or (at your option) any later version.
17 /// Catacomb is distributed in the hope that it will be useful,
18 /// but WITHOUT ANY WARRANTY; without even the implied warranty of
19 /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 /// GNU Library General Public License for more details.
22 /// You should have received a copy of the GNU Library General Public
23 /// License along with Catacomb; if not, write to the Free
24 /// Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 /// MA 02111-1307, USA.
27 ///--------------------------------------------------------------------------
31 #include "asm-common.h"
35 ///--------------------------------------------------------------------------
38 FUNC(chacha_core_x86ish_avx)
47 FUNC(chacha_core_x86ish_sse2)
52 // Arguments come in on the stack, and will need to be collected. We
53 // can get away with just the scratch registers for integer work, but
54 // we'll run out of XMM registers and will need some properly aligned
55 // space which we'll steal from the stack. I don't trust the stack
56 // pointer's alignment, so I'll have to mask the stack pointer, which
57 // in turn means I'll need to keep track of the old value. Hence I'm
58 // making a full i386-style stack frame here.
60 // The Windows and SysV ABIs are sufficiently similar that we don't
61 // need to worry about the differences here.
80 #if CPUFAM_AMD64 && ABI_SYSV
81 // This is nice. We have plenty of XMM registers, and the arguments
82 // are in useful places. There's no need to spill anything and we
83 // can just get on with the code.
94 #if CPUFAM_AMD64 && ABI_WIN
95 // Arguments come in registers, but they're different between Windows
96 // and everyone else (and everyone else is saner).
98 // The Windows ABI insists that we preserve some of the XMM
99 // registers, but we want more than we can use as scratch space. We
100 // only need to save a copy of the input for the feedforward at the
101 // end, so we might as well use memory rather than spill extra
102 // registers. (We need an extra 8 bytes to align the stack.)
108 # define SAVE1 [SP + 0]
109 # define SAVE2 [SP + 16]
110 # define SAVE3 [SP + 32]
117 // First job is to slurp the matrix into XMM registers. Be careful:
118 // the input matrix isn't likely to be properly aligned.
120 // [ 0 1 2 3] (a, xmm0)
121 // [ 4 5 6 7] (b, xmm1)
122 // [ 8 9 10 11] (c, xmm2)
123 // [12 13 14 15] (d, xmm3)
124 movdqu xmm0, [IN + 0]
125 movdqu xmm1, [IN + 16]
126 movdqu xmm2, [IN + 32]
127 movdqu xmm3, [IN + 48]
129 // Take a copy for later. This one is aligned properly, by
137 // Apply a column quarterround to each of the columns simultaneously.
138 // Alas, there doesn't seem to be a packed doubleword rotate, so we
139 // have to synthesize it.
141 // a += b; d ^= a; d <<<= 16
149 // c += d; b ^= c; b <<<= 12
157 // a += b; d ^= a; d <<<= 8
165 // c += d; b ^= c; b <<<= 7
167 pshufd xmm3, xmm3, SHUF(3, 0, 1, 2)
169 pshufd xmm2, xmm2, SHUF(2, 3, 0, 1)
175 // The not-quite-transpose conveniently only involves reordering
176 // elements of individual rows, which can be done quite easily. It
177 // doesn't involve any movement of elements between rows, or even
178 // renaming of the rows.
180 // [ 0 1 2 3] [ 0 1 2 3] (a, xmm0)
181 // [ 4 5 6 7] --> [ 5 6 7 4] (b, xmm1)
182 // [ 8 9 10 11] [10 11 8 9] (c, xmm2)
183 // [12 13 14 15] [15 12 13 14] (d, xmm3)
185 // The shuffles have quite high latency, so they've mostly been
186 // pushed upwards. The remaining one can't be moved, though.
187 pshufd xmm1, xmm1, SHUF(1, 2, 3, 0)
189 // Apply the diagonal quarterround to each of the columns
192 // a += b; d ^= a; d <<<= 16
200 // c += d; b ^= c; b <<<= 12
208 // a += b; d ^= a; d <<<= 8
216 // c += d; b ^= c; b <<<= 7
218 pshufd xmm3, xmm3, SHUF(1, 2, 3, 0)
220 pshufd xmm2, xmm2, SHUF(2, 3, 0, 1)
226 // Finally, finish off undoing the transpose, and we're done for this
227 // doubleround. Again, most of this was done above so we don't have
228 // to wait for the shuffles.
229 pshufd xmm1, xmm1, SHUF(3, 0, 1, 2)
231 // Decrement the loop counter and see if we should go round again.
235 // Almost there. Firstly, the feedforward addition.
241 // And now we write out the result. This one won't be aligned
243 movdqu [OUT + 0], xmm0
244 movdqu [OUT + 16], xmm1
245 movdqu [OUT + 32], xmm2
246 movdqu [OUT + 48], xmm3
253 #if CPUFAM_AMD64 && ABI_WIN
257 // And with that, we're done.
262 FUNC(chacha_multi_i386_sse2)
263 // Arguments are on the stack:
265 // [sp + 4] pointer to state
266 // [sp + 8] input pointer (or null)
267 // [sp + 12] output pointer
268 // [sp + 16] number of blocks to process
269 // [sp + 20] number of rounds per block
277 // Load the arguments.
278 mov BX, [SP + 272] // = state pointer
279 mov SI, [SP + 276] // = source pointer
280 mov DI, [SP + 280] // = destination pointer
281 mov CX, [SP + 284] // = block count
282 mov DX, [SP + 288] // = (initial) round count
284 // Do chunks of four blocks at a time.
288 // Inhale the initial state.
289 movdqu xmm1, [BX + 0]
290 movdqu xmm3, [BX + 16]
291 movdqu xmm5, [BX + 32]
292 movdqu xmm0, [BX + 48]
294 // Set the counters and initialize the working blocks.
310 movdqa [SP + 16], xmm3
311 movdqa [SP + 32], xmm5
312 movdqa [SP + 48], xmm0
319 movdqu [BX + 48], xmm7
321 // a += b; d ^= a; d <<<= 16
322 paddd xmm1, xmm3 // a += b
324 movdqa [SP + 0], xmm1
326 pxor xmm0, xmm1 // d ^= a
336 pslld xmm0, 16 // d << 16
341 pslrd xmm1, 16 // d >> 16
346 por xmm0, xmm1 // d <<<= 16
347 movdqa xmm1, [SP + 32]
349 movdqa xmm3, [SP + 48]
353 movdqa [SP + 48], xmm0
354 movdqa [SP + 112], xmm2
355 movdqa [SP + 176], xmm4
356 movdqa [SP + 240], xmm6
358 // c += d; b ^= c; c <<<= 12
359 paddd xmm0, xmm1 // c += d
364 movdqa [SP + 32], xmm0
365 movdqa [SP + 96], xmm0
366 movdqa [SP + 160], xmm0
367 movdqa [SP + 224], xmm0
369 pxor xmm0, xmm3 // b ^= c
379 pslld xmm0, 16 // d << 16
384 pslrd xmm1, 16 // d >> 16
389 por xmm0, xmm1 // d <<<= 16
396 ///----- That's all, folks --------------------------------------------------