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 [rsp + 0]
109 # define SAVE2 [rsp + 16]
110 # define SAVE3 [rsp + 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 ///----- That's all, folks --------------------------------------------------