RetroArch
bn_mul.h
Go to the documentation of this file.
1 
23 /*
24  * Multiply source vector [s] with b, add result
25  * to destination vector [d] and set carry c.
26  *
27  * Currently supports:
28  *
29  * . IA-32 (386+) . AMD64 / EM64T
30  * . IA-32 (SSE2) . Motorola 68000
31  * . PowerPC, 32-bit . MicroBlaze
32  * . PowerPC, 64-bit . TriCore
33  * . SPARC v8 . ARM v3+
34  * . Alpha . MIPS32
35  * . C, longlong . C, generic
36  */
37 #ifndef MBEDTLS_BN_MUL_H
38 #define MBEDTLS_BN_MUL_H
39 
40 #include "bignum.h"
41 
42 #if defined(MBEDTLS_HAVE_ASM)
43 
44 #ifndef asm
45 #define asm __asm
46 #endif
47 
48 /* armcc5 --gnu defines __GNUC__ but doesn't support GNU's extended asm */
49 #if defined(__GNUC__) && \
50  ( !defined(__ARMCC_VERSION) || __ARMCC_VERSION >= 6000000 )
51 #if defined(__i386__)
52 
53 #define MULADDC_INIT \
54  asm( \
55  "movl %%ebx, %0 \n\t" \
56  "movl %5, %%esi \n\t" \
57  "movl %6, %%edi \n\t" \
58  "movl %7, %%ecx \n\t" \
59  "movl %8, %%ebx \n\t"
60 
61 #define MULADDC_CORE \
62  "lodsl \n\t" \
63  "mull %%ebx \n\t" \
64  "addl %%ecx, %%eax \n\t" \
65  "adcl $0, %%edx \n\t" \
66  "addl (%%edi), %%eax \n\t" \
67  "adcl $0, %%edx \n\t" \
68  "movl %%edx, %%ecx \n\t" \
69  "stosl \n\t"
70 
71 #if defined(MBEDTLS_HAVE_SSE2)
72 
73 #define MULADDC_HUIT \
74  "movd %%ecx, %%mm1 \n\t" \
75  "movd %%ebx, %%mm0 \n\t" \
76  "movd (%%edi), %%mm3 \n\t" \
77  "paddq %%mm3, %%mm1 \n\t" \
78  "movd (%%esi), %%mm2 \n\t" \
79  "pmuludq %%mm0, %%mm2 \n\t" \
80  "movd 4(%%esi), %%mm4 \n\t" \
81  "pmuludq %%mm0, %%mm4 \n\t" \
82  "movd 8(%%esi), %%mm6 \n\t" \
83  "pmuludq %%mm0, %%mm6 \n\t" \
84  "movd 12(%%esi), %%mm7 \n\t" \
85  "pmuludq %%mm0, %%mm7 \n\t" \
86  "paddq %%mm2, %%mm1 \n\t" \
87  "movd 4(%%edi), %%mm3 \n\t" \
88  "paddq %%mm4, %%mm3 \n\t" \
89  "movd 8(%%edi), %%mm5 \n\t" \
90  "paddq %%mm6, %%mm5 \n\t" \
91  "movd 12(%%edi), %%mm4 \n\t" \
92  "paddq %%mm4, %%mm7 \n\t" \
93  "movd %%mm1, (%%edi) \n\t" \
94  "movd 16(%%esi), %%mm2 \n\t" \
95  "pmuludq %%mm0, %%mm2 \n\t" \
96  "psrlq $32, %%mm1 \n\t" \
97  "movd 20(%%esi), %%mm4 \n\t" \
98  "pmuludq %%mm0, %%mm4 \n\t" \
99  "paddq %%mm3, %%mm1 \n\t" \
100  "movd 24(%%esi), %%mm6 \n\t" \
101  "pmuludq %%mm0, %%mm6 \n\t" \
102  "movd %%mm1, 4(%%edi) \n\t" \
103  "psrlq $32, %%mm1 \n\t" \
104  "movd 28(%%esi), %%mm3 \n\t" \
105  "pmuludq %%mm0, %%mm3 \n\t" \
106  "paddq %%mm5, %%mm1 \n\t" \
107  "movd 16(%%edi), %%mm5 \n\t" \
108  "paddq %%mm5, %%mm2 \n\t" \
109  "movd %%mm1, 8(%%edi) \n\t" \
110  "psrlq $32, %%mm1 \n\t" \
111  "paddq %%mm7, %%mm1 \n\t" \
112  "movd 20(%%edi), %%mm5 \n\t" \
113  "paddq %%mm5, %%mm4 \n\t" \
114  "movd %%mm1, 12(%%edi) \n\t" \
115  "psrlq $32, %%mm1 \n\t" \
116  "paddq %%mm2, %%mm1 \n\t" \
117  "movd 24(%%edi), %%mm5 \n\t" \
118  "paddq %%mm5, %%mm6 \n\t" \
119  "movd %%mm1, 16(%%edi) \n\t" \
120  "psrlq $32, %%mm1 \n\t" \
121  "paddq %%mm4, %%mm1 \n\t" \
122  "movd 28(%%edi), %%mm5 \n\t" \
123  "paddq %%mm5, %%mm3 \n\t" \
124  "movd %%mm1, 20(%%edi) \n\t" \
125  "psrlq $32, %%mm1 \n\t" \
126  "paddq %%mm6, %%mm1 \n\t" \
127  "movd %%mm1, 24(%%edi) \n\t" \
128  "psrlq $32, %%mm1 \n\t" \
129  "paddq %%mm3, %%mm1 \n\t" \
130  "movd %%mm1, 28(%%edi) \n\t" \
131  "addl $32, %%edi \n\t" \
132  "addl $32, %%esi \n\t" \
133  "psrlq $32, %%mm1 \n\t" \
134  "movd %%mm1, %%ecx \n\t"
135 
136 #define MULADDC_STOP \
137  "emms \n\t" \
138  "movl %4, %%ebx \n\t" \
139  "movl %%ecx, %1 \n\t" \
140  "movl %%edi, %2 \n\t" \
141  "movl %%esi, %3 \n\t" \
142  : "=m" (t), "=m" (c), "=m" (d), "=m" (s) \
143  : "m" (t), "m" (s), "m" (d), "m" (c), "m" (b) \
144  : "eax", "ecx", "edx", "esi", "edi" \
145  );
146 
147 #else
148 
149 #define MULADDC_STOP \
150  "movl %4, %%ebx \n\t" \
151  "movl %%ecx, %1 \n\t" \
152  "movl %%edi, %2 \n\t" \
153  "movl %%esi, %3 \n\t" \
154  : "=m" (t), "=m" (c), "=m" (d), "=m" (s) \
155  : "m" (t), "m" (s), "m" (d), "m" (c), "m" (b) \
156  : "eax", "ecx", "edx", "esi", "edi" \
157  );
158 #endif /* SSE2 */
159 #endif /* i386 */
160 
161 #if defined(__amd64__) || defined (__x86_64__)
162 
163 #define MULADDC_INIT \
164  asm( \
165  "xorq %%r8, %%r8 \n\t"
166 
167 #define MULADDC_CORE \
168  "movq (%%rsi), %%rax \n\t" \
169  "mulq %%rbx \n\t" \
170  "addq $8, %%rsi \n\t" \
171  "addq %%rcx, %%rax \n\t" \
172  "movq %%r8, %%rcx \n\t" \
173  "adcq $0, %%rdx \n\t" \
174  "nop \n\t" \
175  "addq %%rax, (%%rdi) \n\t" \
176  "adcq %%rdx, %%rcx \n\t" \
177  "addq $8, %%rdi \n\t"
178 
179 #define MULADDC_STOP \
180  : "+c" (c), "+D" (d), "+S" (s) \
181  : "b" (b) \
182  : "rax", "rdx", "r8" \
183  );
184 
185 #endif /* AMD64 */
186 
187 #if defined(__mc68020__) || defined(__mcpu32__)
188 
189 #define MULADDC_INIT \
190  asm( \
191  "movl %3, %%a2 \n\t" \
192  "movl %4, %%a3 \n\t" \
193  "movl %5, %%d3 \n\t" \
194  "movl %6, %%d2 \n\t" \
195  "moveq #0, %%d0 \n\t"
196 
197 #define MULADDC_CORE \
198  "movel %%a2@+, %%d1 \n\t" \
199  "mulul %%d2, %%d4:%%d1 \n\t" \
200  "addl %%d3, %%d1 \n\t" \
201  "addxl %%d0, %%d4 \n\t" \
202  "moveq #0, %%d3 \n\t" \
203  "addl %%d1, %%a3@+ \n\t" \
204  "addxl %%d4, %%d3 \n\t"
205 
206 #define MULADDC_STOP \
207  "movl %%d3, %0 \n\t" \
208  "movl %%a3, %1 \n\t" \
209  "movl %%a2, %2 \n\t" \
210  : "=m" (c), "=m" (d), "=m" (s) \
211  : "m" (s), "m" (d), "m" (c), "m" (b) \
212  : "d0", "d1", "d2", "d3", "d4", "a2", "a3" \
213  );
214 
215 #define MULADDC_HUIT \
216  "movel %%a2@+, %%d1 \n\t" \
217  "mulul %%d2, %%d4:%%d1 \n\t" \
218  "addxl %%d3, %%d1 \n\t" \
219  "addxl %%d0, %%d4 \n\t" \
220  "addl %%d1, %%a3@+ \n\t" \
221  "movel %%a2@+, %%d1 \n\t" \
222  "mulul %%d2, %%d3:%%d1 \n\t" \
223  "addxl %%d4, %%d1 \n\t" \
224  "addxl %%d0, %%d3 \n\t" \
225  "addl %%d1, %%a3@+ \n\t" \
226  "movel %%a2@+, %%d1 \n\t" \
227  "mulul %%d2, %%d4:%%d1 \n\t" \
228  "addxl %%d3, %%d1 \n\t" \
229  "addxl %%d0, %%d4 \n\t" \
230  "addl %%d1, %%a3@+ \n\t" \
231  "movel %%a2@+, %%d1 \n\t" \
232  "mulul %%d2, %%d3:%%d1 \n\t" \
233  "addxl %%d4, %%d1 \n\t" \
234  "addxl %%d0, %%d3 \n\t" \
235  "addl %%d1, %%a3@+ \n\t" \
236  "movel %%a2@+, %%d1 \n\t" \
237  "mulul %%d2, %%d4:%%d1 \n\t" \
238  "addxl %%d3, %%d1 \n\t" \
239  "addxl %%d0, %%d4 \n\t" \
240  "addl %%d1, %%a3@+ \n\t" \
241  "movel %%a2@+, %%d1 \n\t" \
242  "mulul %%d2, %%d3:%%d1 \n\t" \
243  "addxl %%d4, %%d1 \n\t" \
244  "addxl %%d0, %%d3 \n\t" \
245  "addl %%d1, %%a3@+ \n\t" \
246  "movel %%a2@+, %%d1 \n\t" \
247  "mulul %%d2, %%d4:%%d1 \n\t" \
248  "addxl %%d3, %%d1 \n\t" \
249  "addxl %%d0, %%d4 \n\t" \
250  "addl %%d1, %%a3@+ \n\t" \
251  "movel %%a2@+, %%d1 \n\t" \
252  "mulul %%d2, %%d3:%%d1 \n\t" \
253  "addxl %%d4, %%d1 \n\t" \
254  "addxl %%d0, %%d3 \n\t" \
255  "addl %%d1, %%a3@+ \n\t" \
256  "addxl %%d0, %%d3 \n\t"
257 
258 #endif /* MC68000 */
259 
260 #if defined(__powerpc64__) || defined(__ppc64__)
261 
262 #if defined(__MACH__) && defined(__APPLE__)
263 
264 #define MULADDC_INIT \
265  asm( \
266  "ld r3, %3 \n\t" \
267  "ld r4, %4 \n\t" \
268  "ld r5, %5 \n\t" \
269  "ld r6, %6 \n\t" \
270  "addi r3, r3, -8 \n\t" \
271  "addi r4, r4, -8 \n\t" \
272  "addic r5, r5, 0 \n\t"
273 
274 #define MULADDC_CORE \
275  "ldu r7, 8(r3) \n\t" \
276  "mulld r8, r7, r6 \n\t" \
277  "mulhdu r9, r7, r6 \n\t" \
278  "adde r8, r8, r5 \n\t" \
279  "ld r7, 8(r4) \n\t" \
280  "addze r5, r9 \n\t" \
281  "addc r8, r8, r7 \n\t" \
282  "stdu r8, 8(r4) \n\t"
283 
284 #define MULADDC_STOP \
285  "addze r5, r5 \n\t" \
286  "addi r4, r4, 8 \n\t" \
287  "addi r3, r3, 8 \n\t" \
288  "std r5, %0 \n\t" \
289  "std r4, %1 \n\t" \
290  "std r3, %2 \n\t" \
291  : "=m" (c), "=m" (d), "=m" (s) \
292  : "m" (s), "m" (d), "m" (c), "m" (b) \
293  : "r3", "r4", "r5", "r6", "r7", "r8", "r9" \
294  );
295 
296 
297 #else /* __MACH__ && __APPLE__ */
298 
299 #define MULADDC_INIT \
300  asm( \
301  "ld %%r3, %3 \n\t" \
302  "ld %%r4, %4 \n\t" \
303  "ld %%r5, %5 \n\t" \
304  "ld %%r6, %6 \n\t" \
305  "addi %%r3, %%r3, -8 \n\t" \
306  "addi %%r4, %%r4, -8 \n\t" \
307  "addic %%r5, %%r5, 0 \n\t"
308 
309 #define MULADDC_CORE \
310  "ldu %%r7, 8(%%r3) \n\t" \
311  "mulld %%r8, %%r7, %%r6 \n\t" \
312  "mulhdu %%r9, %%r7, %%r6 \n\t" \
313  "adde %%r8, %%r8, %%r5 \n\t" \
314  "ld %%r7, 8(%%r4) \n\t" \
315  "addze %%r5, %%r9 \n\t" \
316  "addc %%r8, %%r8, %%r7 \n\t" \
317  "stdu %%r8, 8(%%r4) \n\t"
318 
319 #define MULADDC_STOP \
320  "addze %%r5, %%r5 \n\t" \
321  "addi %%r4, %%r4, 8 \n\t" \
322  "addi %%r3, %%r3, 8 \n\t" \
323  "std %%r5, %0 \n\t" \
324  "std %%r4, %1 \n\t" \
325  "std %%r3, %2 \n\t" \
326  : "=m" (c), "=m" (d), "=m" (s) \
327  : "m" (s), "m" (d), "m" (c), "m" (b) \
328  : "r3", "r4", "r5", "r6", "r7", "r8", "r9" \
329  );
330 
331 #endif /* __MACH__ && __APPLE__ */
332 
333 #elif defined(__powerpc__) || defined(__ppc__) /* end PPC64/begin PPC32 */
334 
335 #if defined(__MACH__) && defined(__APPLE__)
336 
337 #define MULADDC_INIT \
338  asm( \
339  "lwz r3, %3 \n\t" \
340  "lwz r4, %4 \n\t" \
341  "lwz r5, %5 \n\t" \
342  "lwz r6, %6 \n\t" \
343  "addi r3, r3, -4 \n\t" \
344  "addi r4, r4, -4 \n\t" \
345  "addic r5, r5, 0 \n\t"
346 
347 #define MULADDC_CORE \
348  "lwzu r7, 4(r3) \n\t" \
349  "mullw r8, r7, r6 \n\t" \
350  "mulhwu r9, r7, r6 \n\t" \
351  "adde r8, r8, r5 \n\t" \
352  "lwz r7, 4(r4) \n\t" \
353  "addze r5, r9 \n\t" \
354  "addc r8, r8, r7 \n\t" \
355  "stwu r8, 4(r4) \n\t"
356 
357 #define MULADDC_STOP \
358  "addze r5, r5 \n\t" \
359  "addi r4, r4, 4 \n\t" \
360  "addi r3, r3, 4 \n\t" \
361  "stw r5, %0 \n\t" \
362  "stw r4, %1 \n\t" \
363  "stw r3, %2 \n\t" \
364  : "=m" (c), "=m" (d), "=m" (s) \
365  : "m" (s), "m" (d), "m" (c), "m" (b) \
366  : "r3", "r4", "r5", "r6", "r7", "r8", "r9" \
367  );
368 
369 #else /* __MACH__ && __APPLE__ */
370 
371 #define MULADDC_INIT \
372  asm( \
373  "lwz %%r3, %3 \n\t" \
374  "lwz %%r4, %4 \n\t" \
375  "lwz %%r5, %5 \n\t" \
376  "lwz %%r6, %6 \n\t" \
377  "addi %%r3, %%r3, -4 \n\t" \
378  "addi %%r4, %%r4, -4 \n\t" \
379  "addic %%r5, %%r5, 0 \n\t"
380 
381 #define MULADDC_CORE \
382  "lwzu %%r7, 4(%%r3) \n\t" \
383  "mullw %%r8, %%r7, %%r6 \n\t" \
384  "mulhwu %%r9, %%r7, %%r6 \n\t" \
385  "adde %%r8, %%r8, %%r5 \n\t" \
386  "lwz %%r7, 4(%%r4) \n\t" \
387  "addze %%r5, %%r9 \n\t" \
388  "addc %%r8, %%r8, %%r7 \n\t" \
389  "stwu %%r8, 4(%%r4) \n\t"
390 
391 #define MULADDC_STOP \
392  "addze %%r5, %%r5 \n\t" \
393  "addi %%r4, %%r4, 4 \n\t" \
394  "addi %%r3, %%r3, 4 \n\t" \
395  "stw %%r5, %0 \n\t" \
396  "stw %%r4, %1 \n\t" \
397  "stw %%r3, %2 \n\t" \
398  : "=m" (c), "=m" (d), "=m" (s) \
399  : "m" (s), "m" (d), "m" (c), "m" (b) \
400  : "r3", "r4", "r5", "r6", "r7", "r8", "r9" \
401  );
402 
403 #endif /* __MACH__ && __APPLE__ */
404 
405 #endif /* PPC32 */
406 
407 /*
408  * The Sparc(64) assembly is reported to be broken.
409  * Disable it for now, until we're able to fix it.
410  */
411 #if 0 && defined(__sparc__)
412 #if defined(__sparc64__)
413 
414 #define MULADDC_INIT \
415  asm( \
416  "ldx %3, %%o0 \n\t" \
417  "ldx %4, %%o1 \n\t" \
418  "ld %5, %%o2 \n\t" \
419  "ld %6, %%o3 \n\t"
420 
421 #define MULADDC_CORE \
422  "ld [%%o0], %%o4 \n\t" \
423  "inc 4, %%o0 \n\t" \
424  "ld [%%o1], %%o5 \n\t" \
425  "umul %%o3, %%o4, %%o4 \n\t" \
426  "addcc %%o4, %%o2, %%o4 \n\t" \
427  "rd %%y, %%g1 \n\t" \
428  "addx %%g1, 0, %%g1 \n\t" \
429  "addcc %%o4, %%o5, %%o4 \n\t" \
430  "st %%o4, [%%o1] \n\t" \
431  "addx %%g1, 0, %%o2 \n\t" \
432  "inc 4, %%o1 \n\t"
433 
434  #define MULADDC_STOP \
435  "st %%o2, %0 \n\t" \
436  "stx %%o1, %1 \n\t" \
437  "stx %%o0, %2 \n\t" \
438  : "=m" (c), "=m" (d), "=m" (s) \
439  : "m" (s), "m" (d), "m" (c), "m" (b) \
440  : "g1", "o0", "o1", "o2", "o3", "o4", \
441  "o5" \
442  );
443 
444 #else /* __sparc64__ */
445 
446 #define MULADDC_INIT \
447  asm( \
448  "ld %3, %%o0 \n\t" \
449  "ld %4, %%o1 \n\t" \
450  "ld %5, %%o2 \n\t" \
451  "ld %6, %%o3 \n\t"
452 
453 #define MULADDC_CORE \
454  "ld [%%o0], %%o4 \n\t" \
455  "inc 4, %%o0 \n\t" \
456  "ld [%%o1], %%o5 \n\t" \
457  "umul %%o3, %%o4, %%o4 \n\t" \
458  "addcc %%o4, %%o2, %%o4 \n\t" \
459  "rd %%y, %%g1 \n\t" \
460  "addx %%g1, 0, %%g1 \n\t" \
461  "addcc %%o4, %%o5, %%o4 \n\t" \
462  "st %%o4, [%%o1] \n\t" \
463  "addx %%g1, 0, %%o2 \n\t" \
464  "inc 4, %%o1 \n\t"
465 
466 #define MULADDC_STOP \
467  "st %%o2, %0 \n\t" \
468  "st %%o1, %1 \n\t" \
469  "st %%o0, %2 \n\t" \
470  : "=m" (c), "=m" (d), "=m" (s) \
471  : "m" (s), "m" (d), "m" (c), "m" (b) \
472  : "g1", "o0", "o1", "o2", "o3", "o4", \
473  "o5" \
474  );
475 
476 #endif /* __sparc64__ */
477 #endif /* __sparc__ */
478 
479 #if defined(__microblaze__) || defined(microblaze)
480 
481 #define MULADDC_INIT \
482  asm( \
483  "lwi r3, %3 \n\t" \
484  "lwi r4, %4 \n\t" \
485  "lwi r5, %5 \n\t" \
486  "lwi r6, %6 \n\t" \
487  "andi r7, r6, 0xffff \n\t" \
488  "bsrli r6, r6, 16 \n\t"
489 
490 #define MULADDC_CORE \
491  "lhui r8, r3, 0 \n\t" \
492  "addi r3, r3, 2 \n\t" \
493  "lhui r9, r3, 0 \n\t" \
494  "addi r3, r3, 2 \n\t" \
495  "mul r10, r9, r6 \n\t" \
496  "mul r11, r8, r7 \n\t" \
497  "mul r12, r9, r7 \n\t" \
498  "mul r13, r8, r6 \n\t" \
499  "bsrli r8, r10, 16 \n\t" \
500  "bsrli r9, r11, 16 \n\t" \
501  "add r13, r13, r8 \n\t" \
502  "add r13, r13, r9 \n\t" \
503  "bslli r10, r10, 16 \n\t" \
504  "bslli r11, r11, 16 \n\t" \
505  "add r12, r12, r10 \n\t" \
506  "addc r13, r13, r0 \n\t" \
507  "add r12, r12, r11 \n\t" \
508  "addc r13, r13, r0 \n\t" \
509  "lwi r10, r4, 0 \n\t" \
510  "add r12, r12, r10 \n\t" \
511  "addc r13, r13, r0 \n\t" \
512  "add r12, r12, r5 \n\t" \
513  "addc r5, r13, r0 \n\t" \
514  "swi r12, r4, 0 \n\t" \
515  "addi r4, r4, 4 \n\t"
516 
517 #define MULADDC_STOP \
518  "swi r5, %0 \n\t" \
519  "swi r4, %1 \n\t" \
520  "swi r3, %2 \n\t" \
521  : "=m" (c), "=m" (d), "=m" (s) \
522  : "m" (s), "m" (d), "m" (c), "m" (b) \
523  : "r3", "r4" "r5", "r6", "r7", "r8", \
524  "r9", "r10", "r11", "r12", "r13" \
525  );
526 
527 #endif /* MicroBlaze */
528 
529 #if defined(__tricore__)
530 
531 #define MULADDC_INIT \
532  asm( \
533  "ld.a %%a2, %3 \n\t" \
534  "ld.a %%a3, %4 \n\t" \
535  "ld.w %%d4, %5 \n\t" \
536  "ld.w %%d1, %6 \n\t" \
537  "xor %%d5, %%d5 \n\t"
538 
539 #define MULADDC_CORE \
540  "ld.w %%d0, [%%a2+] \n\t" \
541  "madd.u %%e2, %%e4, %%d0, %%d1 \n\t" \
542  "ld.w %%d0, [%%a3] \n\t" \
543  "addx %%d2, %%d2, %%d0 \n\t" \
544  "addc %%d3, %%d3, 0 \n\t" \
545  "mov %%d4, %%d3 \n\t" \
546  "st.w [%%a3+], %%d2 \n\t"
547 
548 #define MULADDC_STOP \
549  "st.w %0, %%d4 \n\t" \
550  "st.a %1, %%a3 \n\t" \
551  "st.a %2, %%a2 \n\t" \
552  : "=m" (c), "=m" (d), "=m" (s) \
553  : "m" (s), "m" (d), "m" (c), "m" (b) \
554  : "d0", "d1", "e2", "d4", "a2", "a3" \
555  );
556 
557 #endif /* TriCore */
558 
559 /*
560  * gcc -O0 by default uses r7 for the frame pointer, so it complains about our
561  * use of r7 below, unless -fomit-frame-pointer is passed. Unfortunately,
562  * passing that option is not easy when building with yotta.
563  *
564  * On the other hand, -fomit-frame-pointer is implied by any -Ox options with
565  * x !=0, which we can detect using __OPTIMIZE__ (which is also defined by
566  * clang and armcc5 under the same conditions).
567  *
568  * So, only use the optimized assembly below for optimized build, which avoids
569  * the build error and is pretty reasonable anyway.
570  */
571 #if defined(__GNUC__) && !defined(__OPTIMIZE__)
572 #define MULADDC_CANNOT_USE_R7
573 #endif
574 
575 #if defined(__arm__) && !defined(MULADDC_CANNOT_USE_R7)
576 
577 #if defined(__thumb__) && !defined(__thumb2__)
578 
579 #define MULADDC_INIT \
580  asm( \
581  "ldr r0, %3 \n\t" \
582  "ldr r1, %4 \n\t" \
583  "ldr r2, %5 \n\t" \
584  "ldr r3, %6 \n\t" \
585  "lsr r7, r3, #16 \n\t" \
586  "mov r9, r7 \n\t" \
587  "lsl r7, r3, #16 \n\t" \
588  "lsr r7, r7, #16 \n\t" \
589  "mov r8, r7 \n\t"
590 
591 #define MULADDC_CORE \
592  "ldmia r0!, {r6} \n\t" \
593  "lsr r7, r6, #16 \n\t" \
594  "lsl r6, r6, #16 \n\t" \
595  "lsr r6, r6, #16 \n\t" \
596  "mov r4, r8 \n\t" \
597  "mul r4, r6 \n\t" \
598  "mov r3, r9 \n\t" \
599  "mul r6, r3 \n\t" \
600  "mov r5, r9 \n\t" \
601  "mul r5, r7 \n\t" \
602  "mov r3, r8 \n\t" \
603  "mul r7, r3 \n\t" \
604  "lsr r3, r6, #16 \n\t" \
605  "add r5, r5, r3 \n\t" \
606  "lsr r3, r7, #16 \n\t" \
607  "add r5, r5, r3 \n\t" \
608  "add r4, r4, r2 \n\t" \
609  "mov r2, #0 \n\t" \
610  "adc r5, r2 \n\t" \
611  "lsl r3, r6, #16 \n\t" \
612  "add r4, r4, r3 \n\t" \
613  "adc r5, r2 \n\t" \
614  "lsl r3, r7, #16 \n\t" \
615  "add r4, r4, r3 \n\t" \
616  "adc r5, r2 \n\t" \
617  "ldr r3, [r1] \n\t" \
618  "add r4, r4, r3 \n\t" \
619  "adc r2, r5 \n\t" \
620  "stmia r1!, {r4} \n\t"
621 
622 #define MULADDC_STOP \
623  "str r2, %0 \n\t" \
624  "str r1, %1 \n\t" \
625  "str r0, %2 \n\t" \
626  : "=m" (c), "=m" (d), "=m" (s) \
627  : "m" (s), "m" (d), "m" (c), "m" (b) \
628  : "r0", "r1", "r2", "r3", "r4", "r5", \
629  "r6", "r7", "r8", "r9", "cc" \
630  );
631 
632 #else
633 
634 #define MULADDC_INIT \
635  asm( \
636  "ldr r0, %3 \n\t" \
637  "ldr r1, %4 \n\t" \
638  "ldr r2, %5 \n\t" \
639  "ldr r3, %6 \n\t"
640 
641 #define MULADDC_CORE \
642  "ldr r4, [r0], #4 \n\t" \
643  "mov r5, #0 \n\t" \
644  "ldr r6, [r1] \n\t" \
645  "umlal r2, r5, r3, r4 \n\t" \
646  "adds r7, r6, r2 \n\t" \
647  "adc r2, r5, #0 \n\t" \
648  "str r7, [r1], #4 \n\t"
649 
650 #define MULADDC_STOP \
651  "str r2, %0 \n\t" \
652  "str r1, %1 \n\t" \
653  "str r0, %2 \n\t" \
654  : "=m" (c), "=m" (d), "=m" (s) \
655  : "m" (s), "m" (d), "m" (c), "m" (b) \
656  : "r0", "r1", "r2", "r3", "r4", "r5", \
657  "r6", "r7", "cc" \
658  );
659 
660 #endif /* Thumb */
661 
662 #endif /* ARMv3 */
663 
664 #if defined(__alpha__)
665 
666 #define MULADDC_INIT \
667  asm( \
668  "ldq $1, %3 \n\t" \
669  "ldq $2, %4 \n\t" \
670  "ldq $3, %5 \n\t" \
671  "ldq $4, %6 \n\t"
672 
673 #define MULADDC_CORE \
674  "ldq $6, 0($1) \n\t" \
675  "addq $1, 8, $1 \n\t" \
676  "mulq $6, $4, $7 \n\t" \
677  "umulh $6, $4, $6 \n\t" \
678  "addq $7, $3, $7 \n\t" \
679  "cmpult $7, $3, $3 \n\t" \
680  "ldq $5, 0($2) \n\t" \
681  "addq $7, $5, $7 \n\t" \
682  "cmpult $7, $5, $5 \n\t" \
683  "stq $7, 0($2) \n\t" \
684  "addq $2, 8, $2 \n\t" \
685  "addq $6, $3, $3 \n\t" \
686  "addq $5, $3, $3 \n\t"
687 
688 #define MULADDC_STOP \
689  "stq $3, %0 \n\t" \
690  "stq $2, %1 \n\t" \
691  "stq $1, %2 \n\t" \
692  : "=m" (c), "=m" (d), "=m" (s) \
693  : "m" (s), "m" (d), "m" (c), "m" (b) \
694  : "$1", "$2", "$3", "$4", "$5", "$6", "$7" \
695  );
696 #endif /* Alpha */
697 
698 #if defined(__mips__) && !defined(__mips64)
699 
700 #define MULADDC_INIT \
701  asm( \
702  "lw $10, %3 \n\t" \
703  "lw $11, %4 \n\t" \
704  "lw $12, %5 \n\t" \
705  "lw $13, %6 \n\t"
706 
707 #define MULADDC_CORE \
708  "lw $14, 0($10) \n\t" \
709  "multu $13, $14 \n\t" \
710  "addi $10, $10, 4 \n\t" \
711  "mflo $14 \n\t" \
712  "mfhi $9 \n\t" \
713  "addu $14, $12, $14 \n\t" \
714  "lw $15, 0($11) \n\t" \
715  "sltu $12, $14, $12 \n\t" \
716  "addu $15, $14, $15 \n\t" \
717  "sltu $14, $15, $14 \n\t" \
718  "addu $12, $12, $9 \n\t" \
719  "sw $15, 0($11) \n\t" \
720  "addu $12, $12, $14 \n\t" \
721  "addi $11, $11, 4 \n\t"
722 
723 #define MULADDC_STOP \
724  "sw $12, %0 \n\t" \
725  "sw $11, %1 \n\t" \
726  "sw $10, %2 \n\t" \
727  : "=m" (c), "=m" (d), "=m" (s) \
728  : "m" (s), "m" (d), "m" (c), "m" (b) \
729  : "$9", "$10", "$11", "$12", "$13", "$14", "$15" \
730  );
731 
732 #endif /* MIPS */
733 #endif /* GNUC */
734 
735 #if (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
736 
737 #define MULADDC_INIT \
738  __asm mov esi, s \
739  __asm mov edi, d \
740  __asm mov ecx, c \
741  __asm mov ebx, b
742 
743 #define MULADDC_CORE \
744  __asm lodsd \
745  __asm mul ebx \
746  __asm add eax, ecx \
747  __asm adc edx, 0 \
748  __asm add eax, [edi] \
749  __asm adc edx, 0 \
750  __asm mov ecx, edx \
751  __asm stosd
752 
753 #if defined(MBEDTLS_HAVE_SSE2)
754 
755 #define EMIT __asm _emit
756 
757 #define MULADDC_HUIT \
758  EMIT 0x0F EMIT 0x6E EMIT 0xC9 \
759  EMIT 0x0F EMIT 0x6E EMIT 0xC3 \
760  EMIT 0x0F EMIT 0x6E EMIT 0x1F \
761  EMIT 0x0F EMIT 0xD4 EMIT 0xCB \
762  EMIT 0x0F EMIT 0x6E EMIT 0x16 \
763  EMIT 0x0F EMIT 0xF4 EMIT 0xD0 \
764  EMIT 0x0F EMIT 0x6E EMIT 0x66 EMIT 0x04 \
765  EMIT 0x0F EMIT 0xF4 EMIT 0xE0 \
766  EMIT 0x0F EMIT 0x6E EMIT 0x76 EMIT 0x08 \
767  EMIT 0x0F EMIT 0xF4 EMIT 0xF0 \
768  EMIT 0x0F EMIT 0x6E EMIT 0x7E EMIT 0x0C \
769  EMIT 0x0F EMIT 0xF4 EMIT 0xF8 \
770  EMIT 0x0F EMIT 0xD4 EMIT 0xCA \
771  EMIT 0x0F EMIT 0x6E EMIT 0x5F EMIT 0x04 \
772  EMIT 0x0F EMIT 0xD4 EMIT 0xDC \
773  EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x08 \
774  EMIT 0x0F EMIT 0xD4 EMIT 0xEE \
775  EMIT 0x0F EMIT 0x6E EMIT 0x67 EMIT 0x0C \
776  EMIT 0x0F EMIT 0xD4 EMIT 0xFC \
777  EMIT 0x0F EMIT 0x7E EMIT 0x0F \
778  EMIT 0x0F EMIT 0x6E EMIT 0x56 EMIT 0x10 \
779  EMIT 0x0F EMIT 0xF4 EMIT 0xD0 \
780  EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
781  EMIT 0x0F EMIT 0x6E EMIT 0x66 EMIT 0x14 \
782  EMIT 0x0F EMIT 0xF4 EMIT 0xE0 \
783  EMIT 0x0F EMIT 0xD4 EMIT 0xCB \
784  EMIT 0x0F EMIT 0x6E EMIT 0x76 EMIT 0x18 \
785  EMIT 0x0F EMIT 0xF4 EMIT 0xF0 \
786  EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x04 \
787  EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
788  EMIT 0x0F EMIT 0x6E EMIT 0x5E EMIT 0x1C \
789  EMIT 0x0F EMIT 0xF4 EMIT 0xD8 \
790  EMIT 0x0F EMIT 0xD4 EMIT 0xCD \
791  EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x10 \
792  EMIT 0x0F EMIT 0xD4 EMIT 0xD5 \
793  EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x08 \
794  EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
795  EMIT 0x0F EMIT 0xD4 EMIT 0xCF \
796  EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x14 \
797  EMIT 0x0F EMIT 0xD4 EMIT 0xE5 \
798  EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x0C \
799  EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
800  EMIT 0x0F EMIT 0xD4 EMIT 0xCA \
801  EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x18 \
802  EMIT 0x0F EMIT 0xD4 EMIT 0xF5 \
803  EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x10 \
804  EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
805  EMIT 0x0F EMIT 0xD4 EMIT 0xCC \
806  EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x1C \
807  EMIT 0x0F EMIT 0xD4 EMIT 0xDD \
808  EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x14 \
809  EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
810  EMIT 0x0F EMIT 0xD4 EMIT 0xCE \
811  EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x18 \
812  EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
813  EMIT 0x0F EMIT 0xD4 EMIT 0xCB \
814  EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x1C \
815  EMIT 0x83 EMIT 0xC7 EMIT 0x20 \
816  EMIT 0x83 EMIT 0xC6 EMIT 0x20 \
817  EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
818  EMIT 0x0F EMIT 0x7E EMIT 0xC9
819 
820 #define MULADDC_STOP \
821  EMIT 0x0F EMIT 0x77 \
822  __asm mov c, ecx \
823  __asm mov d, edi \
824  __asm mov s, esi \
825 
826 #else
827 
828 #define MULADDC_STOP \
829  __asm mov c, ecx \
830  __asm mov d, edi \
831  __asm mov s, esi \
832 
833 #endif /* SSE2 */
834 #endif /* MSVC */
835 
836 #endif /* MBEDTLS_HAVE_ASM */
837 
838 #if !defined(MULADDC_CORE)
839 #if defined(MBEDTLS_HAVE_UDBL)
840 
841 #define MULADDC_INIT \
842 { \
843  mbedtls_t_udbl r; \
844  mbedtls_mpi_uint r0, r1;
845 
846 #define MULADDC_CORE \
847  r = *(s++) * (mbedtls_t_udbl) b; \
848  r0 = (mbedtls_mpi_uint) r; \
849  r1 = (mbedtls_mpi_uint)( r >> biL ); \
850  r0 += c; r1 += (r0 < c); \
851  r0 += *d; r1 += (r0 < *d); \
852  c = r1; *(d++) = r0;
853 
854 #define MULADDC_STOP \
855 }
856 
857 #else
858 #define MULADDC_INIT \
859 { \
860  mbedtls_mpi_uint s0, s1, b0, b1; \
861  mbedtls_mpi_uint r0, r1, rx, ry; \
862  b0 = ( b << biH ) >> biH; \
863  b1 = ( b >> biH );
864 
865 #define MULADDC_CORE \
866  s0 = ( *s << biH ) >> biH; \
867  s1 = ( *s >> biH ); s++; \
868  rx = s0 * b1; r0 = s0 * b0; \
869  ry = s1 * b0; r1 = s1 * b1; \
870  r1 += ( rx >> biH ); \
871  r1 += ( ry >> biH ); \
872  rx <<= biH; ry <<= biH; \
873  r0 += rx; r1 += (r0 < rx); \
874  r0 += ry; r1 += (r0 < ry); \
875  r0 += c; r1 += (r0 < c); \
876  r0 += *d; r1 += (r0 < *d); \
877  c = r1; *(d++) = r0;
878 
879 #define MULADDC_STOP \
880 }
881 
882 #endif /* C (generic) */
883 #endif /* C (longlong) */
884 
885 #endif /* bn_mul.h */
Multi-precision integer library.