]>
git.zerfleddert.de Git - proxmark3-svn/blob - common/mbedtls/bn_mul.h
4 * \brief Multi-precision integer library
7 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
8 * SPDX-License-Identifier: GPL-2.0
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * This file is part of mbed TLS (https://tls.mbed.org)
27 * Multiply source vector [s] with b, add result
28 * to destination vector [d] and set carry c.
32 * . IA-32 (386+) . AMD64 / EM64T
33 * . IA-32 (SSE2) . Motorola 68000
34 * . PowerPC, 32-bit . MicroBlaze
35 * . PowerPC, 64-bit . TriCore
36 * . SPARC v8 . ARM v3+
38 * . C, longlong . C, generic
40 #ifndef MBEDTLS_BN_MUL_H
41 #define MBEDTLS_BN_MUL_H
45 #if defined(MBEDTLS_HAVE_ASM)
51 /* armcc5 --gnu defines __GNUC__ but doesn't support GNU's extended asm */
52 #if defined(__GNUC__) && \
53 ( !defined(__ARMCC_VERSION) || __ARMCC_VERSION >= 6000000 )
56 * Disable use of the i386 assembly code below if option -O0, to disable all
57 * compiler optimisations, is passed, detected with __OPTIMIZE__
58 * This is done as the number of registers used in the assembly code doesn't
59 * work with the -O0 option.
61 #if defined(__i386__) && defined(__OPTIMIZE__)
63 #define MULADDC_INIT \
65 "movl %%ebx, %0 \n\t" \
66 "movl %5, %%esi \n\t" \
67 "movl %6, %%edi \n\t" \
68 "movl %7, %%ecx \n\t" \
71 #define MULADDC_CORE \
74 "addl %%ecx, %%eax \n\t" \
75 "adcl $0, %%edx \n\t" \
76 "addl (%%edi), %%eax \n\t" \
77 "adcl $0, %%edx \n\t" \
78 "movl %%edx, %%ecx \n\t" \
81 #if defined(MBEDTLS_HAVE_SSE2)
83 #define MULADDC_HUIT \
84 "movd %%ecx, %%mm1 \n\t" \
85 "movd %%ebx, %%mm0 \n\t" \
86 "movd (%%edi), %%mm3 \n\t" \
87 "paddq %%mm3, %%mm1 \n\t" \
88 "movd (%%esi), %%mm2 \n\t" \
89 "pmuludq %%mm0, %%mm2 \n\t" \
90 "movd 4(%%esi), %%mm4 \n\t" \
91 "pmuludq %%mm0, %%mm4 \n\t" \
92 "movd 8(%%esi), %%mm6 \n\t" \
93 "pmuludq %%mm0, %%mm6 \n\t" \
94 "movd 12(%%esi), %%mm7 \n\t" \
95 "pmuludq %%mm0, %%mm7 \n\t" \
96 "paddq %%mm2, %%mm1 \n\t" \
97 "movd 4(%%edi), %%mm3 \n\t" \
98 "paddq %%mm4, %%mm3 \n\t" \
99 "movd 8(%%edi), %%mm5 \n\t" \
100 "paddq %%mm6, %%mm5 \n\t" \
101 "movd 12(%%edi), %%mm4 \n\t" \
102 "paddq %%mm4, %%mm7 \n\t" \
103 "movd %%mm1, (%%edi) \n\t" \
104 "movd 16(%%esi), %%mm2 \n\t" \
105 "pmuludq %%mm0, %%mm2 \n\t" \
106 "psrlq $32, %%mm1 \n\t" \
107 "movd 20(%%esi), %%mm4 \n\t" \
108 "pmuludq %%mm0, %%mm4 \n\t" \
109 "paddq %%mm3, %%mm1 \n\t" \
110 "movd 24(%%esi), %%mm6 \n\t" \
111 "pmuludq %%mm0, %%mm6 \n\t" \
112 "movd %%mm1, 4(%%edi) \n\t" \
113 "psrlq $32, %%mm1 \n\t" \
114 "movd 28(%%esi), %%mm3 \n\t" \
115 "pmuludq %%mm0, %%mm3 \n\t" \
116 "paddq %%mm5, %%mm1 \n\t" \
117 "movd 16(%%edi), %%mm5 \n\t" \
118 "paddq %%mm5, %%mm2 \n\t" \
119 "movd %%mm1, 8(%%edi) \n\t" \
120 "psrlq $32, %%mm1 \n\t" \
121 "paddq %%mm7, %%mm1 \n\t" \
122 "movd 20(%%edi), %%mm5 \n\t" \
123 "paddq %%mm5, %%mm4 \n\t" \
124 "movd %%mm1, 12(%%edi) \n\t" \
125 "psrlq $32, %%mm1 \n\t" \
126 "paddq %%mm2, %%mm1 \n\t" \
127 "movd 24(%%edi), %%mm5 \n\t" \
128 "paddq %%mm5, %%mm6 \n\t" \
129 "movd %%mm1, 16(%%edi) \n\t" \
130 "psrlq $32, %%mm1 \n\t" \
131 "paddq %%mm4, %%mm1 \n\t" \
132 "movd 28(%%edi), %%mm5 \n\t" \
133 "paddq %%mm5, %%mm3 \n\t" \
134 "movd %%mm1, 20(%%edi) \n\t" \
135 "psrlq $32, %%mm1 \n\t" \
136 "paddq %%mm6, %%mm1 \n\t" \
137 "movd %%mm1, 24(%%edi) \n\t" \
138 "psrlq $32, %%mm1 \n\t" \
139 "paddq %%mm3, %%mm1 \n\t" \
140 "movd %%mm1, 28(%%edi) \n\t" \
141 "addl $32, %%edi \n\t" \
142 "addl $32, %%esi \n\t" \
143 "psrlq $32, %%mm1 \n\t" \
144 "movd %%mm1, %%ecx \n\t"
146 #define MULADDC_STOP \
148 "movl %4, %%ebx \n\t" \
149 "movl %%ecx, %1 \n\t" \
150 "movl %%edi, %2 \n\t" \
151 "movl %%esi, %3 \n\t" \
152 : "=m" (t), "=m" (c), "=m" (d), "=m" (s) \
153 : "m" (t), "m" (s), "m" (d), "m" (c), "m" (b) \
154 : "eax", "ebx", "ecx", "edx", "esi", "edi" \
159 #define MULADDC_STOP \
160 "movl %4, %%ebx \n\t" \
161 "movl %%ecx, %1 \n\t" \
162 "movl %%edi, %2 \n\t" \
163 "movl %%esi, %3 \n\t" \
164 : "=m" (t), "=m" (c), "=m" (d), "=m" (s) \
165 : "m" (t), "m" (s), "m" (d), "m" (c), "m" (b) \
166 : "eax", "ebx", "ecx", "edx", "esi", "edi" \
171 #if defined(__amd64__) || defined (__x86_64__)
173 #define MULADDC_INIT \
175 "xorq %%r8, %%r8 \n\t"
177 #define MULADDC_CORE \
178 "movq (%%rsi), %%rax \n\t" \
180 "addq $8, %%rsi \n\t" \
181 "addq %%rcx, %%rax \n\t" \
182 "movq %%r8, %%rcx \n\t" \
183 "adcq $0, %%rdx \n\t" \
185 "addq %%rax, (%%rdi) \n\t" \
186 "adcq %%rdx, %%rcx \n\t" \
187 "addq $8, %%rdi \n\t"
189 #define MULADDC_STOP \
190 : "+c" (c), "+D" (d), "+S" (s) \
192 : "rax", "rdx", "r8" \
197 #if defined(__mc68020__) || defined(__mcpu32__)
199 #define MULADDC_INIT \
201 "movl %3, %%a2 \n\t" \
202 "movl %4, %%a3 \n\t" \
203 "movl %5, %%d3 \n\t" \
204 "movl %6, %%d2 \n\t" \
205 "moveq #0, %%d0 \n\t"
207 #define MULADDC_CORE \
208 "movel %%a2@+, %%d1 \n\t" \
209 "mulul %%d2, %%d4:%%d1 \n\t" \
210 "addl %%d3, %%d1 \n\t" \
211 "addxl %%d0, %%d4 \n\t" \
212 "moveq #0, %%d3 \n\t" \
213 "addl %%d1, %%a3@+ \n\t" \
214 "addxl %%d4, %%d3 \n\t"
216 #define MULADDC_STOP \
217 "movl %%d3, %0 \n\t" \
218 "movl %%a3, %1 \n\t" \
219 "movl %%a2, %2 \n\t" \
220 : "=m" (c), "=m" (d), "=m" (s) \
221 : "m" (s), "m" (d), "m" (c), "m" (b) \
222 : "d0", "d1", "d2", "d3", "d4", "a2", "a3" \
225 #define MULADDC_HUIT \
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 "movel %%a2@+, %%d1 \n\t" \
257 "mulul %%d2, %%d4:%%d1 \n\t" \
258 "addxl %%d3, %%d1 \n\t" \
259 "addxl %%d0, %%d4 \n\t" \
260 "addl %%d1, %%a3@+ \n\t" \
261 "movel %%a2@+, %%d1 \n\t" \
262 "mulul %%d2, %%d3:%%d1 \n\t" \
263 "addxl %%d4, %%d1 \n\t" \
264 "addxl %%d0, %%d3 \n\t" \
265 "addl %%d1, %%a3@+ \n\t" \
266 "addxl %%d0, %%d3 \n\t"
270 #if defined(__powerpc64__) || defined(__ppc64__)
272 #if defined(__MACH__) && defined(__APPLE__)
274 #define MULADDC_INIT \
280 "addi r3, r3, -8 \n\t" \
281 "addi r4, r4, -8 \n\t" \
282 "addic r5, r5, 0 \n\t"
284 #define MULADDC_CORE \
285 "ldu r7, 8(r3) \n\t" \
286 "mulld r8, r7, r6 \n\t" \
287 "mulhdu r9, r7, r6 \n\t" \
288 "adde r8, r8, r5 \n\t" \
289 "ld r7, 8(r4) \n\t" \
290 "addze r5, r9 \n\t" \
291 "addc r8, r8, r7 \n\t" \
292 "stdu r8, 8(r4) \n\t"
294 #define MULADDC_STOP \
295 "addze r5, r5 \n\t" \
296 "addi r4, r4, 8 \n\t" \
297 "addi r3, r3, 8 \n\t" \
301 : "=m" (c), "=m" (d), "=m" (s) \
302 : "m" (s), "m" (d), "m" (c), "m" (b) \
303 : "r3", "r4", "r5", "r6", "r7", "r8", "r9" \
307 #else /* __MACH__ && __APPLE__ */
309 #define MULADDC_INIT \
315 "addi %%r3, %%r3, -8 \n\t" \
316 "addi %%r4, %%r4, -8 \n\t" \
317 "addic %%r5, %%r5, 0 \n\t"
319 #define MULADDC_CORE \
320 "ldu %%r7, 8(%%r3) \n\t" \
321 "mulld %%r8, %%r7, %%r6 \n\t" \
322 "mulhdu %%r9, %%r7, %%r6 \n\t" \
323 "adde %%r8, %%r8, %%r5 \n\t" \
324 "ld %%r7, 8(%%r4) \n\t" \
325 "addze %%r5, %%r9 \n\t" \
326 "addc %%r8, %%r8, %%r7 \n\t" \
327 "stdu %%r8, 8(%%r4) \n\t"
329 #define MULADDC_STOP \
330 "addze %%r5, %%r5 \n\t" \
331 "addi %%r4, %%r4, 8 \n\t" \
332 "addi %%r3, %%r3, 8 \n\t" \
333 "std %%r5, %0 \n\t" \
334 "std %%r4, %1 \n\t" \
335 "std %%r3, %2 \n\t" \
336 : "=m" (c), "=m" (d), "=m" (s) \
337 : "m" (s), "m" (d), "m" (c), "m" (b) \
338 : "r3", "r4", "r5", "r6", "r7", "r8", "r9" \
341 #endif /* __MACH__ && __APPLE__ */
343 #elif defined(__powerpc__) || defined(__ppc__) /* end PPC64/begin PPC32 */
345 #if defined(__MACH__) && defined(__APPLE__)
347 #define MULADDC_INIT \
353 "addi r3, r3, -4 \n\t" \
354 "addi r4, r4, -4 \n\t" \
355 "addic r5, r5, 0 \n\t"
357 #define MULADDC_CORE \
358 "lwzu r7, 4(r3) \n\t" \
359 "mullw r8, r7, r6 \n\t" \
360 "mulhwu r9, r7, r6 \n\t" \
361 "adde r8, r8, r5 \n\t" \
362 "lwz r7, 4(r4) \n\t" \
363 "addze r5, r9 \n\t" \
364 "addc r8, r8, r7 \n\t" \
365 "stwu r8, 4(r4) \n\t"
367 #define MULADDC_STOP \
368 "addze r5, r5 \n\t" \
369 "addi r4, r4, 4 \n\t" \
370 "addi r3, r3, 4 \n\t" \
374 : "=m" (c), "=m" (d), "=m" (s) \
375 : "m" (s), "m" (d), "m" (c), "m" (b) \
376 : "r3", "r4", "r5", "r6", "r7", "r8", "r9" \
379 #else /* __MACH__ && __APPLE__ */
381 #define MULADDC_INIT \
383 "lwz %%r3, %3 \n\t" \
384 "lwz %%r4, %4 \n\t" \
385 "lwz %%r5, %5 \n\t" \
386 "lwz %%r6, %6 \n\t" \
387 "addi %%r3, %%r3, -4 \n\t" \
388 "addi %%r4, %%r4, -4 \n\t" \
389 "addic %%r5, %%r5, 0 \n\t"
391 #define MULADDC_CORE \
392 "lwzu %%r7, 4(%%r3) \n\t" \
393 "mullw %%r8, %%r7, %%r6 \n\t" \
394 "mulhwu %%r9, %%r7, %%r6 \n\t" \
395 "adde %%r8, %%r8, %%r5 \n\t" \
396 "lwz %%r7, 4(%%r4) \n\t" \
397 "addze %%r5, %%r9 \n\t" \
398 "addc %%r8, %%r8, %%r7 \n\t" \
399 "stwu %%r8, 4(%%r4) \n\t"
401 #define MULADDC_STOP \
402 "addze %%r5, %%r5 \n\t" \
403 "addi %%r4, %%r4, 4 \n\t" \
404 "addi %%r3, %%r3, 4 \n\t" \
405 "stw %%r5, %0 \n\t" \
406 "stw %%r4, %1 \n\t" \
407 "stw %%r3, %2 \n\t" \
408 : "=m" (c), "=m" (d), "=m" (s) \
409 : "m" (s), "m" (d), "m" (c), "m" (b) \
410 : "r3", "r4", "r5", "r6", "r7", "r8", "r9" \
413 #endif /* __MACH__ && __APPLE__ */
418 * The Sparc(64) assembly is reported to be broken.
419 * Disable it for now, until we're able to fix it.
421 #if 0 && defined(__sparc__)
422 #if defined(__sparc64__)
424 #define MULADDC_INIT \
426 "ldx %3, %%o0 \n\t" \
427 "ldx %4, %%o1 \n\t" \
431 #define MULADDC_CORE \
432 "ld [%%o0], %%o4 \n\t" \
434 "ld [%%o1], %%o5 \n\t" \
435 "umul %%o3, %%o4, %%o4 \n\t" \
436 "addcc %%o4, %%o2, %%o4 \n\t" \
437 "rd %%y, %%g1 \n\t" \
438 "addx %%g1, 0, %%g1 \n\t" \
439 "addcc %%o4, %%o5, %%o4 \n\t" \
440 "st %%o4, [%%o1] \n\t" \
441 "addx %%g1, 0, %%o2 \n\t" \
444 #define MULADDC_STOP \
446 "stx %%o1, %1 \n\t" \
447 "stx %%o0, %2 \n\t" \
448 : "=m" (c), "=m" (d), "=m" (s) \
449 : "m" (s), "m" (d), "m" (c), "m" (b) \
450 : "g1", "o0", "o1", "o2", "o3", "o4", \
454 #else /* __sparc64__ */
456 #define MULADDC_INIT \
463 #define MULADDC_CORE \
464 "ld [%%o0], %%o4 \n\t" \
466 "ld [%%o1], %%o5 \n\t" \
467 "umul %%o3, %%o4, %%o4 \n\t" \
468 "addcc %%o4, %%o2, %%o4 \n\t" \
469 "rd %%y, %%g1 \n\t" \
470 "addx %%g1, 0, %%g1 \n\t" \
471 "addcc %%o4, %%o5, %%o4 \n\t" \
472 "st %%o4, [%%o1] \n\t" \
473 "addx %%g1, 0, %%o2 \n\t" \
476 #define MULADDC_STOP \
480 : "=m" (c), "=m" (d), "=m" (s) \
481 : "m" (s), "m" (d), "m" (c), "m" (b) \
482 : "g1", "o0", "o1", "o2", "o3", "o4", \
486 #endif /* __sparc64__ */
487 #endif /* __sparc__ */
489 #if defined(__microblaze__) || defined(microblaze)
491 #define MULADDC_INIT \
497 "andi r7, r6, 0xffff \n\t" \
498 "bsrli r6, r6, 16 \n\t"
500 #define MULADDC_CORE \
501 "lhui r8, r3, 0 \n\t" \
502 "addi r3, r3, 2 \n\t" \
503 "lhui r9, r3, 0 \n\t" \
504 "addi r3, r3, 2 \n\t" \
505 "mul r10, r9, r6 \n\t" \
506 "mul r11, r8, r7 \n\t" \
507 "mul r12, r9, r7 \n\t" \
508 "mul r13, r8, r6 \n\t" \
509 "bsrli r8, r10, 16 \n\t" \
510 "bsrli r9, r11, 16 \n\t" \
511 "add r13, r13, r8 \n\t" \
512 "add r13, r13, r9 \n\t" \
513 "bslli r10, r10, 16 \n\t" \
514 "bslli r11, r11, 16 \n\t" \
515 "add r12, r12, r10 \n\t" \
516 "addc r13, r13, r0 \n\t" \
517 "add r12, r12, r11 \n\t" \
518 "addc r13, r13, r0 \n\t" \
519 "lwi r10, r4, 0 \n\t" \
520 "add r12, r12, r10 \n\t" \
521 "addc r13, r13, r0 \n\t" \
522 "add r12, r12, r5 \n\t" \
523 "addc r5, r13, r0 \n\t" \
524 "swi r12, r4, 0 \n\t" \
525 "addi r4, r4, 4 \n\t"
527 #define MULADDC_STOP \
531 : "=m" (c), "=m" (d), "=m" (s) \
532 : "m" (s), "m" (d), "m" (c), "m" (b) \
533 : "r3", "r4", "r5", "r6", "r7", "r8", \
534 "r9", "r10", "r11", "r12", "r13" \
537 #endif /* MicroBlaze */
539 #if defined(__tricore__)
541 #define MULADDC_INIT \
543 "ld.a %%a2, %3 \n\t" \
544 "ld.a %%a3, %4 \n\t" \
545 "ld.w %%d4, %5 \n\t" \
546 "ld.w %%d1, %6 \n\t" \
547 "xor %%d5, %%d5 \n\t"
549 #define MULADDC_CORE \
550 "ld.w %%d0, [%%a2+] \n\t" \
551 "madd.u %%e2, %%e4, %%d0, %%d1 \n\t" \
552 "ld.w %%d0, [%%a3] \n\t" \
553 "addx %%d2, %%d2, %%d0 \n\t" \
554 "addc %%d3, %%d3, 0 \n\t" \
555 "mov %%d4, %%d3 \n\t" \
556 "st.w [%%a3+], %%d2 \n\t"
558 #define MULADDC_STOP \
559 "st.w %0, %%d4 \n\t" \
560 "st.a %1, %%a3 \n\t" \
561 "st.a %2, %%a2 \n\t" \
562 : "=m" (c), "=m" (d), "=m" (s) \
563 : "m" (s), "m" (d), "m" (c), "m" (b) \
564 : "d0", "d1", "e2", "d4", "a2", "a3" \
570 * gcc -O0 by default uses r7 for the frame pointer, so it complains about our
571 * use of r7 below, unless -fomit-frame-pointer is passed. Unfortunately,
572 * passing that option is not easy when building with yotta.
574 * On the other hand, -fomit-frame-pointer is implied by any -Ox options with
575 * x !=0, which we can detect using __OPTIMIZE__ (which is also defined by
576 * clang and armcc5 under the same conditions).
578 * So, only use the optimized assembly below for optimized build, which avoids
579 * the build error and is pretty reasonable anyway.
581 #if defined(__GNUC__) && !defined(__OPTIMIZE__)
582 #define MULADDC_CANNOT_USE_R7
585 #if defined(__arm__) && !defined(MULADDC_CANNOT_USE_R7)
587 #if defined(__thumb__) && !defined(__thumb2__)
589 #define MULADDC_INIT \
595 "lsr r7, r3, #16 \n\t" \
597 "lsl r7, r3, #16 \n\t" \
598 "lsr r7, r7, #16 \n\t" \
601 #define MULADDC_CORE \
602 "ldmia r0!, {r6} \n\t" \
603 "lsr r7, r6, #16 \n\t" \
604 "lsl r6, r6, #16 \n\t" \
605 "lsr r6, r6, #16 \n\t" \
614 "lsr r3, r6, #16 \n\t" \
615 "add r5, r5, r3 \n\t" \
616 "lsr r3, r7, #16 \n\t" \
617 "add r5, r5, r3 \n\t" \
618 "add r4, r4, r2 \n\t" \
621 "lsl r3, r6, #16 \n\t" \
622 "add r4, r4, r3 \n\t" \
624 "lsl r3, r7, #16 \n\t" \
625 "add r4, r4, r3 \n\t" \
627 "ldr r3, [r1] \n\t" \
628 "add r4, r4, r3 \n\t" \
630 "stmia r1!, {r4} \n\t"
632 #define MULADDC_STOP \
636 : "=m" (c), "=m" (d), "=m" (s) \
637 : "m" (s), "m" (d), "m" (c), "m" (b) \
638 : "r0", "r1", "r2", "r3", "r4", "r5", \
639 "r6", "r7", "r8", "r9", "cc" \
644 #define MULADDC_INIT \
651 #define MULADDC_CORE \
652 "ldr r4, [r0], #4 \n\t" \
654 "ldr r6, [r1] \n\t" \
655 "umlal r2, r5, r3, r4 \n\t" \
656 "adds r7, r6, r2 \n\t" \
657 "adc r2, r5, #0 \n\t" \
658 "str r7, [r1], #4 \n\t"
660 #define MULADDC_STOP \
664 : "=m" (c), "=m" (d), "=m" (s) \
665 : "m" (s), "m" (d), "m" (c), "m" (b) \
666 : "r0", "r1", "r2", "r3", "r4", "r5", \
674 #if defined(__alpha__)
676 #define MULADDC_INIT \
683 #define MULADDC_CORE \
684 "ldq $6, 0($1) \n\t" \
685 "addq $1, 8, $1 \n\t" \
686 "mulq $6, $4, $7 \n\t" \
687 "umulh $6, $4, $6 \n\t" \
688 "addq $7, $3, $7 \n\t" \
689 "cmpult $7, $3, $3 \n\t" \
690 "ldq $5, 0($2) \n\t" \
691 "addq $7, $5, $7 \n\t" \
692 "cmpult $7, $5, $5 \n\t" \
693 "stq $7, 0($2) \n\t" \
694 "addq $2, 8, $2 \n\t" \
695 "addq $6, $3, $3 \n\t" \
696 "addq $5, $3, $3 \n\t"
698 #define MULADDC_STOP \
702 : "=m" (c), "=m" (d), "=m" (s) \
703 : "m" (s), "m" (d), "m" (c), "m" (b) \
704 : "$1", "$2", "$3", "$4", "$5", "$6", "$7" \
708 #if defined(__mips__) && !defined(__mips64)
710 #define MULADDC_INIT \
717 #define MULADDC_CORE \
718 "lw $14, 0($10) \n\t" \
719 "multu $13, $14 \n\t" \
720 "addi $10, $10, 4 \n\t" \
723 "addu $14, $12, $14 \n\t" \
724 "lw $15, 0($11) \n\t" \
725 "sltu $12, $14, $12 \n\t" \
726 "addu $15, $14, $15 \n\t" \
727 "sltu $14, $15, $14 \n\t" \
728 "addu $12, $12, $9 \n\t" \
729 "sw $15, 0($11) \n\t" \
730 "addu $12, $12, $14 \n\t" \
731 "addi $11, $11, 4 \n\t"
733 #define MULADDC_STOP \
737 : "=m" (c), "=m" (d), "=m" (s) \
738 : "m" (s), "m" (d), "m" (c), "m" (b) \
739 : "$9", "$10", "$11", "$12", "$13", "$14", "$15" \
745 #if (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
747 #define MULADDC_INIT \
753 #define MULADDC_CORE \
758 __asm add eax, [edi] \
763 #if defined(MBEDTLS_HAVE_SSE2)
765 #define EMIT __asm _emit
767 #define MULADDC_HUIT \
768 EMIT 0x0F EMIT 0x6E EMIT 0xC9 \
769 EMIT 0x0F EMIT 0x6E EMIT 0xC3 \
770 EMIT 0x0F EMIT 0x6E EMIT 0x1F \
771 EMIT 0x0F EMIT 0xD4 EMIT 0xCB \
772 EMIT 0x0F EMIT 0x6E EMIT 0x16 \
773 EMIT 0x0F EMIT 0xF4 EMIT 0xD0 \
774 EMIT 0x0F EMIT 0x6E EMIT 0x66 EMIT 0x04 \
775 EMIT 0x0F EMIT 0xF4 EMIT 0xE0 \
776 EMIT 0x0F EMIT 0x6E EMIT 0x76 EMIT 0x08 \
777 EMIT 0x0F EMIT 0xF4 EMIT 0xF0 \
778 EMIT 0x0F EMIT 0x6E EMIT 0x7E EMIT 0x0C \
779 EMIT 0x0F EMIT 0xF4 EMIT 0xF8 \
780 EMIT 0x0F EMIT 0xD4 EMIT 0xCA \
781 EMIT 0x0F EMIT 0x6E EMIT 0x5F EMIT 0x04 \
782 EMIT 0x0F EMIT 0xD4 EMIT 0xDC \
783 EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x08 \
784 EMIT 0x0F EMIT 0xD4 EMIT 0xEE \
785 EMIT 0x0F EMIT 0x6E EMIT 0x67 EMIT 0x0C \
786 EMIT 0x0F EMIT 0xD4 EMIT 0xFC \
787 EMIT 0x0F EMIT 0x7E EMIT 0x0F \
788 EMIT 0x0F EMIT 0x6E EMIT 0x56 EMIT 0x10 \
789 EMIT 0x0F EMIT 0xF4 EMIT 0xD0 \
790 EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
791 EMIT 0x0F EMIT 0x6E EMIT 0x66 EMIT 0x14 \
792 EMIT 0x0F EMIT 0xF4 EMIT 0xE0 \
793 EMIT 0x0F EMIT 0xD4 EMIT 0xCB \
794 EMIT 0x0F EMIT 0x6E EMIT 0x76 EMIT 0x18 \
795 EMIT 0x0F EMIT 0xF4 EMIT 0xF0 \
796 EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x04 \
797 EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
798 EMIT 0x0F EMIT 0x6E EMIT 0x5E EMIT 0x1C \
799 EMIT 0x0F EMIT 0xF4 EMIT 0xD8 \
800 EMIT 0x0F EMIT 0xD4 EMIT 0xCD \
801 EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x10 \
802 EMIT 0x0F EMIT 0xD4 EMIT 0xD5 \
803 EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x08 \
804 EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
805 EMIT 0x0F EMIT 0xD4 EMIT 0xCF \
806 EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x14 \
807 EMIT 0x0F EMIT 0xD4 EMIT 0xE5 \
808 EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x0C \
809 EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
810 EMIT 0x0F EMIT 0xD4 EMIT 0xCA \
811 EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x18 \
812 EMIT 0x0F EMIT 0xD4 EMIT 0xF5 \
813 EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x10 \
814 EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
815 EMIT 0x0F EMIT 0xD4 EMIT 0xCC \
816 EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x1C \
817 EMIT 0x0F EMIT 0xD4 EMIT 0xDD \
818 EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x14 \
819 EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
820 EMIT 0x0F EMIT 0xD4 EMIT 0xCE \
821 EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x18 \
822 EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
823 EMIT 0x0F EMIT 0xD4 EMIT 0xCB \
824 EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x1C \
825 EMIT 0x83 EMIT 0xC7 EMIT 0x20 \
826 EMIT 0x83 EMIT 0xC6 EMIT 0x20 \
827 EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
828 EMIT 0x0F EMIT 0x7E EMIT 0xC9
830 #define MULADDC_STOP \
831 EMIT 0x0F EMIT 0x77 \
838 #define MULADDC_STOP \
846 #endif /* MBEDTLS_HAVE_ASM */
848 #if !defined(MULADDC_CORE)
849 #if defined(MBEDTLS_HAVE_UDBL)
851 #define MULADDC_INIT \
854 mbedtls_mpi_uint r0, r1;
856 #define MULADDC_CORE \
857 r = *(s++) * (mbedtls_t_udbl) b; \
858 r0 = (mbedtls_mpi_uint) r; \
859 r1 = (mbedtls_mpi_uint)( r >> biL ); \
860 r0 += c; r1 += (r0 < c); \
861 r0 += *d; r1 += (r0 < *d); \
864 #define MULADDC_STOP \
868 #define MULADDC_INIT \
870 mbedtls_mpi_uint s0, s1, b0, b1; \
871 mbedtls_mpi_uint r0, r1, rx, ry; \
872 b0 = ( b << biH ) >> biH; \
875 #define MULADDC_CORE \
876 s0 = ( *s << biH ) >> biH; \
877 s1 = ( *s >> biH ); s++; \
878 rx = s0 * b1; r0 = s0 * b0; \
879 ry = s1 * b0; r1 = s1 * b1; \
880 r1 += ( rx >> biH ); \
881 r1 += ( ry >> biH ); \
882 rx <<= biH; ry <<= biH; \
883 r0 += rx; r1 += (r0 < rx); \
884 r0 += ry; r1 += (r0 < ry); \
885 r0 += c; r1 += (r0 < c); \
886 r0 += *d; r1 += (r0 < *d); \
889 #define MULADDC_STOP \
892 #endif /* C (generic) */
893 #endif /* C (longlong) */
895 #endif /* bn_mul.h */