Debian Codegraph @

mypyc/lib-rt/base64/arch/ssse3/enc_loop_asm.c

raw
1 // Apologies in advance for combining the preprocessor with inline assembly,
2 // two notoriously gnarly parts of C, but it was necessary to avoid a lot of
3 // code repetition. The preprocessor is used to template large sections of
4 // inline assembly that differ only in the registers used. If the code was
5 // written out by hand, it would become very large and hard to audit.
6
7 // Generate a block of inline assembly that loads register R0 from memory. The
8 // offset at which the register is loaded is set by the given round.
9 #define LOAD(R0, ROUND) \
10 "lddqu ("#ROUND" * 12)(%[src]), %["R0"] \n\t"
11
12 // Generate a block of inline assembly that deinterleaves and shuffles register
13 // R0 using preloaded constants. Outputs in R0 and R1.
14 #define SHUF(R0, R1) \
15 "pshufb %[lut0], %["R0"] \n\t" \
16 "movdqa %["R0"], %["R1"] \n\t" \
17 "pand %[msk0], %["R0"] \n\t" \
18 "pand %[msk2], %["R1"] \n\t" \
19 "pmulhuw %[msk1], %["R0"] \n\t" \
20 "pmullw %[msk3], %["R1"] \n\t" \
21 "por %["R1"], %["R0"] \n\t"
22
23 // Generate a block of inline assembly that takes R0 and R1 and translates
24 // their contents to the base64 alphabet, using preloaded constants.
25 #define TRAN(R0, R1, R2) \
26 "movdqa %["R0"], %["R1"] \n\t" \
27 "movdqa %["R0"], %["R2"] \n\t" \
28 "psubusb %[n51], %["R1"] \n\t" \
29 "pcmpgtb %[n25], %["R2"] \n\t" \
30 "psubb %["R2"], %["R1"] \n\t" \
31 "movdqa %[lut1], %["R2"] \n\t" \
32 "pshufb %["R1"], %["R2"] \n\t" \
33 "paddb %["R2"], %["R0"] \n\t"
34
35 // Generate a block of inline assembly that stores the given register R0 at an
36 // offset set by the given round.
37 #define STOR(R0, ROUND) \
38 "movdqu %["R0"], ("#ROUND" * 16)(%[dst]) \n\t"
39
40 // Generate a block of inline assembly that generates a single self-contained
41 // encoder round: fetch the data, process it, and store the result. Then update
42 // the source and destination pointers.
43 #define ROUND() \
44 LOAD("a", 0) \
45 SHUF("a", "b") \
46 TRAN("a", "b", "c") \
47 STOR("a", 0) \
48 "add $12, %[src] \n\t" \
49 "add $16, %[dst] \n\t"
50
51 // Define a macro that initiates a three-way interleaved encoding round by
52 // preloading registers a, b and c from memory.
53 // The register graph shows which registers are in use during each step, and
54 // is a visual aid for choosing registers for that step. Symbol index:
55 //
56 // + indicates that a register is loaded by that step.
57 // | indicates that a register is in use and must not be touched.
58 // - indicates that a register is decommissioned by that step.
59 // x indicates that a register is used as a temporary by that step.
60 // V indicates that a register is an input or output to the macro.
61 //
62 #define ROUND_3_INIT() /* a b c d e f */ \
63 LOAD("a", 0) /* + */ \
64 SHUF("a", "d") /* | + */ \
65 LOAD("b", 1) /* | + | */ \
66 TRAN("a", "d", "e") /* | | - x */ \
67 LOAD("c", 2) /* V V V */
68
69 // Define a macro that translates, shuffles and stores the input registers A, B
70 // and C, and preloads registers D, E and F for the next round.
71 // This macro can be arbitrarily daisy-chained by feeding output registers D, E
72 // and F back into the next round as input registers A, B and C. The macro
73 // carefully interleaves memory operations with data operations for optimal
74 // pipelined performance.
75
76 #define ROUND_3(ROUND, A,B,C,D,E,F) /* A B C D E F */ \
77 LOAD(D, (ROUND + 3)) /* V V V + */ \
78 SHUF(B, E) /* | | | | + */ \
79 STOR(A, (ROUND + 0)) /* - | | | | */ \
80 TRAN(B, E, F) /* | | | - x */ \
81 LOAD(E, (ROUND + 4)) /* | | | + */ \
82 SHUF(C, A) /* + | | | | */ \
83 STOR(B, (ROUND + 1)) /* | - | | | */ \
84 TRAN(C, A, F) /* - | | | x */ \
85 LOAD(F, (ROUND + 5)) /* | | | + */ \
86 SHUF(D, A) /* + | | | | */ \
87 STOR(C, (ROUND + 2)) /* | - | | | */ \
88 TRAN(D, A, B) /* - x V V V */
89
90 // Define a macro that terminates a ROUND_3 macro by taking pre-loaded
91 // registers D, E and F, and translating, shuffling and storing them.
92 #define ROUND_3_END(ROUND, A,B,C,D,E,F) /* A B C D E F */ \
93 SHUF(E, A) /* + V V V */ \
94 STOR(D, (ROUND + 3)) /* | - | | */ \
95 TRAN(E, A, B) /* - x | | */ \
96 SHUF(F, C) /* + | | */ \
97 STOR(E, (ROUND + 4)) /* | - | */ \
98 TRAN(F, C, D) /* - x | */ \
99 STOR(F, (ROUND + 5)) /* - */
100
101 // Define a type A round. Inputs are a, b, and c, outputs are d, e, and f.
102 #define ROUND_3_A(ROUND) \
103 ROUND_3(ROUND, "a", "b", "c", "d", "e", "f")
104
105 // Define a type B round. Inputs and outputs are swapped with regard to type A.
106 #define ROUND_3_B(ROUND) \
107 ROUND_3(ROUND, "d", "e", "f", "a", "b", "c")
108
109 // Terminating macro for a type A round.
110 #define ROUND_3_A_LAST(ROUND) \
111 ROUND_3_A(ROUND) \
112 ROUND_3_END(ROUND, "a", "b", "c", "d", "e", "f")
113
114 // Terminating macro for a type B round.
115 #define ROUND_3_B_LAST(ROUND) \
116 ROUND_3_B(ROUND) \
117 ROUND_3_END(ROUND, "d", "e", "f", "a", "b", "c")
118
119 // Suppress clang's warning that the literal string in the asm statement is
120 // overlong (longer than the ISO-mandated minimum size of 4095 bytes for C99
121 // compilers). It may be true, but the goal here is not C99 portability.
122 #pragma GCC diagnostic push
123 #pragma GCC diagnostic ignored "-Woverlength-strings"
124
125 static inline void
126 enc_loop_ssse3 (const uint8_t **s, size_t *slen, uint8_t **o, size_t *olen)
127 {
128 // For a clearer explanation of the algorithm used by this function,
129 // please refer to the plain (not inline assembly) implementation. This
130 // function follows the same basic logic.
131
132 if (*slen < 16) {
133 return;
134 }
135
136 // Process blocks of 12 bytes at a time. Input is read in blocks of 16
137 // bytes, so "reserve" four bytes from the input buffer to ensure that
138 // we never read beyond the end of the input buffer.
139 size_t rounds = (*slen - 4) / 12;
140
141 *slen -= rounds * 12; // 12 bytes consumed per round
142 *olen += rounds * 16; // 16 bytes produced per round
143
144 // Number of times to go through the 36x loop.
145 size_t loops = rounds / 36;
146
147 // Number of rounds remaining after the 36x loop.
148 rounds %= 36;
149
150 // Lookup tables.
151 const __m128i lut0 = _mm_set_epi8(
152 10, 11, 9, 10, 7, 8, 6, 7, 4, 5, 3, 4, 1, 2, 0, 1);
153
154 const __m128i lut1 = _mm_setr_epi8(
155 65, 71, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -19, -16, 0, 0);
156
157 // Temporary registers.
158 __m128i a, b, c, d, e, f;
159
160 __asm__ volatile (
161
162 // If there are 36 rounds or more, enter a 36x unrolled loop of
163 // interleaved encoding rounds. The rounds interleave memory
164 // operations (load/store) with data operations (table lookups,
165 // etc) to maximize pipeline throughput.
166 " test %[loops], %[loops] \n\t"
167 " jz 18f \n\t"
168 " jmp 36f \n\t"
169 " \n\t"
170 ".balign 64 \n\t"
171 "36: " ROUND_3_INIT()
172 " " ROUND_3_A( 0)
173 " " ROUND_3_B( 3)
174 " " ROUND_3_A( 6)
175 " " ROUND_3_B( 9)
176 " " ROUND_3_A(12)
177 " " ROUND_3_B(15)
178 " " ROUND_3_A(18)
179 " " ROUND_3_B(21)
180 " " ROUND_3_A(24)
181 " " ROUND_3_B(27)
182 " " ROUND_3_A_LAST(30)
183 " add $(12 * 36), %[src] \n\t"
184 " add $(16 * 36), %[dst] \n\t"
185 " dec %[loops] \n\t"
186 " jnz 36b \n\t"
187
188 // Enter an 18x unrolled loop for rounds of 18 or more.
189 "18: cmp $18, %[rounds] \n\t"
190 " jl 9f \n\t"
191 " " ROUND_3_INIT()
192 " " ROUND_3_A(0)
193 " " ROUND_3_B(3)
194 " " ROUND_3_A(6)
195 " " ROUND_3_B(9)
196 " " ROUND_3_A_LAST(12)
197 " sub $18, %[rounds] \n\t"
198 " add $(12 * 18), %[src] \n\t"
199 " add $(16 * 18), %[dst] \n\t"
200
201 // Enter a 9x unrolled loop for rounds of 9 or more.
202 "9: cmp $9, %[rounds] \n\t"
203 " jl 6f \n\t"
204 " " ROUND_3_INIT()
205 " " ROUND_3_A(0)
206 " " ROUND_3_B_LAST(3)
207 " sub $9, %[rounds] \n\t"
208 " add $(12 * 9), %[src] \n\t"
209 " add $(16 * 9), %[dst] \n\t"
210
211 // Enter a 6x unrolled loop for rounds of 6 or more.
212 "6: cmp $6, %[rounds] \n\t"
213 " jl 55f \n\t"
214 " " ROUND_3_INIT()
215 " " ROUND_3_A_LAST(0)
216 " sub $6, %[rounds] \n\t"
217 " add $(12 * 6), %[src] \n\t"
218 " add $(16 * 6), %[dst] \n\t"
219
220 // Dispatch the remaining rounds 0..5.
221 "55: cmp $3, %[rounds] \n\t"
222 " jg 45f \n\t"
223 " je 3f \n\t"
224 " cmp $1, %[rounds] \n\t"
225 " jg 2f \n\t"
226 " je 1f \n\t"
227 " jmp 0f \n\t"
228
229 "45: cmp $4, %[rounds] \n\t"
230 " je 4f \n\t"
231
232 // Block of non-interlaced encoding rounds, which can each
233 // individually be jumped to. Rounds fall through to the next.
234 "5: " ROUND()
235 "4: " ROUND()
236 "3: " ROUND()
237 "2: " ROUND()
238 "1: " ROUND()
239 "0: \n\t"
240
241 // Outputs (modified).
242 : [rounds] "+r" (rounds),
243 [loops] "+r" (loops),
244 [src] "+r" (*s),
245 [dst] "+r" (*o),
246 [a] "=&x" (a),
247 [b] "=&x" (b),
248 [c] "=&x" (c),
249 [d] "=&x" (d),
250 [e] "=&x" (e),
251 [f] "=&x" (f)
252
253 // Inputs (not modified).
254 : [lut0] "x" (lut0),
255 [lut1] "x" (lut1),
256 [msk0] "x" (_mm_set1_epi32(0x0FC0FC00)),
257 [msk1] "x" (_mm_set1_epi32(0x04000040)),
258 [msk2] "x" (_mm_set1_epi32(0x003F03F0)),
259 [msk3] "x" (_mm_set1_epi32(0x01000010)),
260 [n51] "x" (_mm_set1_epi8(51)),
261 [n25] "x" (_mm_set1_epi8(25))
262
263 // Clobbers.
264 : "cc", "memory"
265 );
266 }
267
268 #pragma GCC diagnostic pop