63#ifndef MBEDTLS_BN_MUL_H
64#define MBEDTLS_BN_MUL_H
66#if !defined(MBEDTLS_CONFIG_FILE)
69#include MBEDTLS_CONFIG_FILE
79#if defined(MBEDTLS_HAVE_INT32)
81#define MBEDTLS_BYTES_TO_T_UINT_4( a, b, c, d ) \
82 ( (mbedtls_mpi_uint) (a) << 0 ) | \
83 ( (mbedtls_mpi_uint) (b) << 8 ) | \
84 ( (mbedtls_mpi_uint) (c) << 16 ) | \
85 ( (mbedtls_mpi_uint) (d) << 24 )
87#define MBEDTLS_BYTES_TO_T_UINT_2( a, b ) \
88 MBEDTLS_BYTES_TO_T_UINT_4( a, b, 0, 0 )
90#define MBEDTLS_BYTES_TO_T_UINT_8( a, b, c, d, e, f, g, h ) \
91 MBEDTLS_BYTES_TO_T_UINT_4( a, b, c, d ), \
92 MBEDTLS_BYTES_TO_T_UINT_4( e, f, g, h )
96#define MBEDTLS_BYTES_TO_T_UINT_8( a, b, c, d, e, f, g, h ) \
97 ( (mbedtls_mpi_uint) (a) << 0 ) | \
98 ( (mbedtls_mpi_uint) (b) << 8 ) | \
99 ( (mbedtls_mpi_uint) (c) << 16 ) | \
100 ( (mbedtls_mpi_uint) (d) << 24 ) | \
101 ( (mbedtls_mpi_uint) (e) << 32 ) | \
102 ( (mbedtls_mpi_uint) (f) << 40 ) | \
103 ( (mbedtls_mpi_uint) (g) << 48 ) | \
104 ( (mbedtls_mpi_uint) (h) << 56 )
106#define MBEDTLS_BYTES_TO_T_UINT_4( a, b, c, d ) \
107 MBEDTLS_BYTES_TO_T_UINT_8( a, b, c, d, 0, 0, 0, 0 )
109#define MBEDTLS_BYTES_TO_T_UINT_2( a, b ) \
110 MBEDTLS_BYTES_TO_T_UINT_8( a, b, 0, 0, 0, 0, 0, 0 )
114#if defined(MBEDTLS_HAVE_ASM)
121#if defined(__GNUC__) && \
122 ( !defined(__ARMCC_VERSION) || __ARMCC_VERSION >= 6000000 )
130#if defined(__i386__) && defined(__OPTIMIZE__)
132#define MULADDC_INIT \
134 "movl %%ebx, %0 \n\t" \
135 "movl %5, %%esi \n\t" \
136 "movl %6, %%edi \n\t" \
137 "movl %7, %%ecx \n\t" \
138 "movl %8, %%ebx \n\t"
140#define MULADDC_CORE \
143 "addl %%ecx, %%eax \n\t" \
144 "adcl $0, %%edx \n\t" \
145 "addl (%%edi), %%eax \n\t" \
146 "adcl $0, %%edx \n\t" \
147 "movl %%edx, %%ecx \n\t" \
150#if defined(MBEDTLS_HAVE_SSE2)
152#define MULADDC_HUIT \
153 "movd %%ecx, %%mm1 \n\t" \
154 "movd %%ebx, %%mm0 \n\t" \
155 "movd (%%edi), %%mm3 \n\t" \
156 "paddq %%mm3, %%mm1 \n\t" \
157 "movd (%%esi), %%mm2 \n\t" \
158 "pmuludq %%mm0, %%mm2 \n\t" \
159 "movd 4(%%esi), %%mm4 \n\t" \
160 "pmuludq %%mm0, %%mm4 \n\t" \
161 "movd 8(%%esi), %%mm6 \n\t" \
162 "pmuludq %%mm0, %%mm6 \n\t" \
163 "movd 12(%%esi), %%mm7 \n\t" \
164 "pmuludq %%mm0, %%mm7 \n\t" \
165 "paddq %%mm2, %%mm1 \n\t" \
166 "movd 4(%%edi), %%mm3 \n\t" \
167 "paddq %%mm4, %%mm3 \n\t" \
168 "movd 8(%%edi), %%mm5 \n\t" \
169 "paddq %%mm6, %%mm5 \n\t" \
170 "movd 12(%%edi), %%mm4 \n\t" \
171 "paddq %%mm4, %%mm7 \n\t" \
172 "movd %%mm1, (%%edi) \n\t" \
173 "movd 16(%%esi), %%mm2 \n\t" \
174 "pmuludq %%mm0, %%mm2 \n\t" \
175 "psrlq $32, %%mm1 \n\t" \
176 "movd 20(%%esi), %%mm4 \n\t" \
177 "pmuludq %%mm0, %%mm4 \n\t" \
178 "paddq %%mm3, %%mm1 \n\t" \
179 "movd 24(%%esi), %%mm6 \n\t" \
180 "pmuludq %%mm0, %%mm6 \n\t" \
181 "movd %%mm1, 4(%%edi) \n\t" \
182 "psrlq $32, %%mm1 \n\t" \
183 "movd 28(%%esi), %%mm3 \n\t" \
184 "pmuludq %%mm0, %%mm3 \n\t" \
185 "paddq %%mm5, %%mm1 \n\t" \
186 "movd 16(%%edi), %%mm5 \n\t" \
187 "paddq %%mm5, %%mm2 \n\t" \
188 "movd %%mm1, 8(%%edi) \n\t" \
189 "psrlq $32, %%mm1 \n\t" \
190 "paddq %%mm7, %%mm1 \n\t" \
191 "movd 20(%%edi), %%mm5 \n\t" \
192 "paddq %%mm5, %%mm4 \n\t" \
193 "movd %%mm1, 12(%%edi) \n\t" \
194 "psrlq $32, %%mm1 \n\t" \
195 "paddq %%mm2, %%mm1 \n\t" \
196 "movd 24(%%edi), %%mm5 \n\t" \
197 "paddq %%mm5, %%mm6 \n\t" \
198 "movd %%mm1, 16(%%edi) \n\t" \
199 "psrlq $32, %%mm1 \n\t" \
200 "paddq %%mm4, %%mm1 \n\t" \
201 "movd 28(%%edi), %%mm5 \n\t" \
202 "paddq %%mm5, %%mm3 \n\t" \
203 "movd %%mm1, 20(%%edi) \n\t" \
204 "psrlq $32, %%mm1 \n\t" \
205 "paddq %%mm6, %%mm1 \n\t" \
206 "movd %%mm1, 24(%%edi) \n\t" \
207 "psrlq $32, %%mm1 \n\t" \
208 "paddq %%mm3, %%mm1 \n\t" \
209 "movd %%mm1, 28(%%edi) \n\t" \
210 "addl $32, %%edi \n\t" \
211 "addl $32, %%esi \n\t" \
212 "psrlq $32, %%mm1 \n\t" \
213 "movd %%mm1, %%ecx \n\t"
215#define MULADDC_STOP \
217 "movl %4, %%ebx \n\t" \
218 "movl %%ecx, %1 \n\t" \
219 "movl %%edi, %2 \n\t" \
220 "movl %%esi, %3 \n\t" \
221 : "=m" (t), "=m" (c), "=m" (d), "=m" (s) \
222 : "m" (t), "m" (s), "m" (d), "m" (c), "m" (b) \
223 : "eax", "ebx", "ecx", "edx", "esi", "edi" \
228#define MULADDC_STOP \
229 "movl %4, %%ebx \n\t" \
230 "movl %%ecx, %1 \n\t" \
231 "movl %%edi, %2 \n\t" \
232 "movl %%esi, %3 \n\t" \
233 : "=m" (t), "=m" (c), "=m" (d), "=m" (s) \
234 : "m" (t), "m" (s), "m" (d), "m" (c), "m" (b) \
235 : "eax", "ebx", "ecx", "edx", "esi", "edi" \
240#if defined(__amd64__) || defined (__x86_64__)
242#define MULADDC_INIT \
246#define MULADDC_CORE \
247 "movq (%%rsi), %%rax\n" \
250 "addq %%rcx, %%rax\n" \
251 "movq %%r8, %%rcx\n" \
254 "addq %%rax, (%%rdi)\n" \
255 "adcq %%rdx, %%rcx\n" \
258#define MULADDC_STOP \
259 : "+c" (c), "+D" (d), "+S" (s) \
261 : "rax", "rdx", "r8" \
266#if defined(__mc68020__) || defined(__mcpu32__)
268#define MULADDC_INIT \
270 "movl %3, %%a2 \n\t" \
271 "movl %4, %%a3 \n\t" \
272 "movl %5, %%d3 \n\t" \
273 "movl %6, %%d2 \n\t" \
274 "moveq #0, %%d0 \n\t"
276#define MULADDC_CORE \
277 "movel %%a2@+, %%d1 \n\t" \
278 "mulul %%d2, %%d4:%%d1 \n\t" \
279 "addl %%d3, %%d1 \n\t" \
280 "addxl %%d0, %%d4 \n\t" \
281 "moveq #0, %%d3 \n\t" \
282 "addl %%d1, %%a3@+ \n\t" \
283 "addxl %%d4, %%d3 \n\t"
285#define MULADDC_STOP \
286 "movl %%d3, %0 \n\t" \
287 "movl %%a3, %1 \n\t" \
288 "movl %%a2, %2 \n\t" \
289 : "=m" (c), "=m" (d), "=m" (s) \
290 : "m" (s), "m" (d), "m" (c), "m" (b) \
291 : "d0", "d1", "d2", "d3", "d4", "a2", "a3" \
294#define MULADDC_HUIT \
295 "movel %%a2@+, %%d1 \n\t" \
296 "mulul %%d2, %%d4:%%d1 \n\t" \
297 "addxl %%d3, %%d1 \n\t" \
298 "addxl %%d0, %%d4 \n\t" \
299 "addl %%d1, %%a3@+ \n\t" \
300 "movel %%a2@+, %%d1 \n\t" \
301 "mulul %%d2, %%d3:%%d1 \n\t" \
302 "addxl %%d4, %%d1 \n\t" \
303 "addxl %%d0, %%d3 \n\t" \
304 "addl %%d1, %%a3@+ \n\t" \
305 "movel %%a2@+, %%d1 \n\t" \
306 "mulul %%d2, %%d4:%%d1 \n\t" \
307 "addxl %%d3, %%d1 \n\t" \
308 "addxl %%d0, %%d4 \n\t" \
309 "addl %%d1, %%a3@+ \n\t" \
310 "movel %%a2@+, %%d1 \n\t" \
311 "mulul %%d2, %%d3:%%d1 \n\t" \
312 "addxl %%d4, %%d1 \n\t" \
313 "addxl %%d0, %%d3 \n\t" \
314 "addl %%d1, %%a3@+ \n\t" \
315 "movel %%a2@+, %%d1 \n\t" \
316 "mulul %%d2, %%d4:%%d1 \n\t" \
317 "addxl %%d3, %%d1 \n\t" \
318 "addxl %%d0, %%d4 \n\t" \
319 "addl %%d1, %%a3@+ \n\t" \
320 "movel %%a2@+, %%d1 \n\t" \
321 "mulul %%d2, %%d3:%%d1 \n\t" \
322 "addxl %%d4, %%d1 \n\t" \
323 "addxl %%d0, %%d3 \n\t" \
324 "addl %%d1, %%a3@+ \n\t" \
325 "movel %%a2@+, %%d1 \n\t" \
326 "mulul %%d2, %%d4:%%d1 \n\t" \
327 "addxl %%d3, %%d1 \n\t" \
328 "addxl %%d0, %%d4 \n\t" \
329 "addl %%d1, %%a3@+ \n\t" \
330 "movel %%a2@+, %%d1 \n\t" \
331 "mulul %%d2, %%d3:%%d1 \n\t" \
332 "addxl %%d4, %%d1 \n\t" \
333 "addxl %%d0, %%d3 \n\t" \
334 "addl %%d1, %%a3@+ \n\t" \
335 "addxl %%d0, %%d3 \n\t"
339#if defined(__powerpc64__) || defined(__ppc64__)
341#if defined(__MACH__) && defined(__APPLE__)
343#define MULADDC_INIT \
349 "addi r3, r3, -8 \n\t" \
350 "addi r4, r4, -8 \n\t" \
351 "addic r5, r5, 0 \n\t"
353#define MULADDC_CORE \
354 "ldu r7, 8(r3) \n\t" \
355 "mulld r8, r7, r6 \n\t" \
356 "mulhdu r9, r7, r6 \n\t" \
357 "adde r8, r8, r5 \n\t" \
358 "ld r7, 8(r4) \n\t" \
359 "addze r5, r9 \n\t" \
360 "addc r8, r8, r7 \n\t" \
361 "stdu r8, 8(r4) \n\t"
363#define MULADDC_STOP \
364 "addze r5, r5 \n\t" \
365 "addi r4, r4, 8 \n\t" \
366 "addi r3, r3, 8 \n\t" \
370 : "=m" (c), "=m" (d), "=m" (s) \
371 : "m" (s), "m" (d), "m" (c), "m" (b) \
372 : "r3", "r4", "r5", "r6", "r7", "r8", "r9" \
378#define MULADDC_INIT \
384 "addi %%r3, %%r3, -8 \n\t" \
385 "addi %%r4, %%r4, -8 \n\t" \
386 "addic %%r5, %%r5, 0 \n\t"
388#define MULADDC_CORE \
389 "ldu %%r7, 8(%%r3) \n\t" \
390 "mulld %%r8, %%r7, %%r6 \n\t" \
391 "mulhdu %%r9, %%r7, %%r6 \n\t" \
392 "adde %%r8, %%r8, %%r5 \n\t" \
393 "ld %%r7, 8(%%r4) \n\t" \
394 "addze %%r5, %%r9 \n\t" \
395 "addc %%r8, %%r8, %%r7 \n\t" \
396 "stdu %%r8, 8(%%r4) \n\t"
398#define MULADDC_STOP \
399 "addze %%r5, %%r5 \n\t" \
400 "addi %%r4, %%r4, 8 \n\t" \
401 "addi %%r3, %%r3, 8 \n\t" \
402 "std %%r5, %0 \n\t" \
403 "std %%r4, %1 \n\t" \
404 "std %%r3, %2 \n\t" \
405 : "=m" (c), "=m" (d), "=m" (s) \
406 : "m" (s), "m" (d), "m" (c), "m" (b) \
407 : "r3", "r4", "r5", "r6", "r7", "r8", "r9" \
412#elif defined(__powerpc__) || defined(__ppc__)
414#if defined(__MACH__) && defined(__APPLE__)
416#define MULADDC_INIT \
422 "addi r3, r3, -4 \n\t" \
423 "addi r4, r4, -4 \n\t" \
424 "addic r5, r5, 0 \n\t"
426#define MULADDC_CORE \
427 "lwzu r7, 4(r3) \n\t" \
428 "mullw r8, r7, r6 \n\t" \
429 "mulhwu r9, r7, r6 \n\t" \
430 "adde r8, r8, r5 \n\t" \
431 "lwz r7, 4(r4) \n\t" \
432 "addze r5, r9 \n\t" \
433 "addc r8, r8, r7 \n\t" \
434 "stwu r8, 4(r4) \n\t"
436#define MULADDC_STOP \
437 "addze r5, r5 \n\t" \
438 "addi r4, r4, 4 \n\t" \
439 "addi r3, r3, 4 \n\t" \
443 : "=m" (c), "=m" (d), "=m" (s) \
444 : "m" (s), "m" (d), "m" (c), "m" (b) \
445 : "r3", "r4", "r5", "r6", "r7", "r8", "r9" \
450#define MULADDC_INIT \
452 "lwz %%r3, %3 \n\t" \
453 "lwz %%r4, %4 \n\t" \
454 "lwz %%r5, %5 \n\t" \
455 "lwz %%r6, %6 \n\t" \
456 "addi %%r3, %%r3, -4 \n\t" \
457 "addi %%r4, %%r4, -4 \n\t" \
458 "addic %%r5, %%r5, 0 \n\t"
460#define MULADDC_CORE \
461 "lwzu %%r7, 4(%%r3) \n\t" \
462 "mullw %%r8, %%r7, %%r6 \n\t" \
463 "mulhwu %%r9, %%r7, %%r6 \n\t" \
464 "adde %%r8, %%r8, %%r5 \n\t" \
465 "lwz %%r7, 4(%%r4) \n\t" \
466 "addze %%r5, %%r9 \n\t" \
467 "addc %%r8, %%r8, %%r7 \n\t" \
468 "stwu %%r8, 4(%%r4) \n\t"
470#define MULADDC_STOP \
471 "addze %%r5, %%r5 \n\t" \
472 "addi %%r4, %%r4, 4 \n\t" \
473 "addi %%r3, %%r3, 4 \n\t" \
474 "stw %%r5, %0 \n\t" \
475 "stw %%r4, %1 \n\t" \
476 "stw %%r3, %2 \n\t" \
477 : "=m" (c), "=m" (d), "=m" (s) \
478 : "m" (s), "m" (d), "m" (c), "m" (b) \
479 : "r3", "r4", "r5", "r6", "r7", "r8", "r9" \
490#if 0 && defined(__sparc__)
491#if defined(__sparc64__)
493#define MULADDC_INIT \
495 "ldx %3, %%o0 \n\t" \
496 "ldx %4, %%o1 \n\t" \
500#define MULADDC_CORE \
501 "ld [%%o0], %%o4 \n\t" \
503 "ld [%%o1], %%o5 \n\t" \
504 "umul %%o3, %%o4, %%o4 \n\t" \
505 "addcc %%o4, %%o2, %%o4 \n\t" \
506 "rd %%y, %%g1 \n\t" \
507 "addx %%g1, 0, %%g1 \n\t" \
508 "addcc %%o4, %%o5, %%o4 \n\t" \
509 "st %%o4, [%%o1] \n\t" \
510 "addx %%g1, 0, %%o2 \n\t" \
513 #define MULADDC_STOP \
515 "stx %%o1, %1 \n\t" \
516 "stx %%o0, %2 \n\t" \
517 : "=m" (c), "=m" (d), "=m" (s) \
518 : "m" (s), "m" (d), "m" (c), "m" (b) \
519 : "g1", "o0", "o1", "o2", "o3", "o4", \
525#define MULADDC_INIT \
532#define MULADDC_CORE \
533 "ld [%%o0], %%o4 \n\t" \
535 "ld [%%o1], %%o5 \n\t" \
536 "umul %%o3, %%o4, %%o4 \n\t" \
537 "addcc %%o4, %%o2, %%o4 \n\t" \
538 "rd %%y, %%g1 \n\t" \
539 "addx %%g1, 0, %%g1 \n\t" \
540 "addcc %%o4, %%o5, %%o4 \n\t" \
541 "st %%o4, [%%o1] \n\t" \
542 "addx %%g1, 0, %%o2 \n\t" \
545#define MULADDC_STOP \
549 : "=m" (c), "=m" (d), "=m" (s) \
550 : "m" (s), "m" (d), "m" (c), "m" (b) \
551 : "g1", "o0", "o1", "o2", "o3", "o4", \
558#if defined(__microblaze__) || defined(microblaze)
560#define MULADDC_INIT \
566 "andi r7, r6, 0xffff \n\t" \
567 "bsrli r6, r6, 16 \n\t"
569#define MULADDC_CORE \
570 "lhui r8, r3, 0 \n\t" \
571 "addi r3, r3, 2 \n\t" \
572 "lhui r9, r3, 0 \n\t" \
573 "addi r3, r3, 2 \n\t" \
574 "mul r10, r9, r6 \n\t" \
575 "mul r11, r8, r7 \n\t" \
576 "mul r12, r9, r7 \n\t" \
577 "mul r13, r8, r6 \n\t" \
578 "bsrli r8, r10, 16 \n\t" \
579 "bsrli r9, r11, 16 \n\t" \
580 "add r13, r13, r8 \n\t" \
581 "add r13, r13, r9 \n\t" \
582 "bslli r10, r10, 16 \n\t" \
583 "bslli r11, r11, 16 \n\t" \
584 "add r12, r12, r10 \n\t" \
585 "addc r13, r13, r0 \n\t" \
586 "add r12, r12, r11 \n\t" \
587 "addc r13, r13, r0 \n\t" \
588 "lwi r10, r4, 0 \n\t" \
589 "add r12, r12, r10 \n\t" \
590 "addc r13, r13, r0 \n\t" \
591 "add r12, r12, r5 \n\t" \
592 "addc r5, r13, r0 \n\t" \
593 "swi r12, r4, 0 \n\t" \
594 "addi r4, r4, 4 \n\t"
596#define MULADDC_STOP \
600 : "=m" (c), "=m" (d), "=m" (s) \
601 : "m" (s), "m" (d), "m" (c), "m" (b) \
602 : "r3", "r4", "r5", "r6", "r7", "r8", \
603 "r9", "r10", "r11", "r12", "r13" \
608#if defined(__tricore__)
610#define MULADDC_INIT \
612 "ld.a %%a2, %3 \n\t" \
613 "ld.a %%a3, %4 \n\t" \
614 "ld.w %%d4, %5 \n\t" \
615 "ld.w %%d1, %6 \n\t" \
616 "xor %%d5, %%d5 \n\t"
618#define MULADDC_CORE \
619 "ld.w %%d0, [%%a2+] \n\t" \
620 "madd.u %%e2, %%e4, %%d0, %%d1 \n\t" \
621 "ld.w %%d0, [%%a3] \n\t" \
622 "addx %%d2, %%d2, %%d0 \n\t" \
623 "addc %%d3, %%d3, 0 \n\t" \
624 "mov %%d4, %%d3 \n\t" \
625 "st.w [%%a3+], %%d2 \n\t"
627#define MULADDC_STOP \
628 "st.w %0, %%d4 \n\t" \
629 "st.a %1, %%a3 \n\t" \
630 "st.a %2, %%a2 \n\t" \
631 : "=m" (c), "=m" (d), "=m" (s) \
632 : "m" (s), "m" (d), "m" (c), "m" (b) \
633 : "d0", "d1", "e2", "d4", "a2", "a3" \
649#if defined(__GNUC__) && !defined(__OPTIMIZE__)
650#define MULADDC_CANNOT_USE_R7
653#if defined(__arm__) && !defined(MULADDC_CANNOT_USE_R7)
655#if defined(__thumb__) && !defined(__thumb2__)
657#define MULADDC_INIT \
663 "lsr r7, r3, #16 \n\t" \
665 "lsl r7, r3, #16 \n\t" \
666 "lsr r7, r7, #16 \n\t" \
669#define MULADDC_CORE \
670 "ldmia r0!, {r6} \n\t" \
671 "lsr r7, r6, #16 \n\t" \
672 "lsl r6, r6, #16 \n\t" \
673 "lsr r6, r6, #16 \n\t" \
682 "lsr r3, r6, #16 \n\t" \
683 "add r5, r5, r3 \n\t" \
684 "lsr r3, r7, #16 \n\t" \
685 "add r5, r5, r3 \n\t" \
686 "add r4, r4, r2 \n\t" \
689 "lsl r3, r6, #16 \n\t" \
690 "add r4, r4, r3 \n\t" \
692 "lsl r3, r7, #16 \n\t" \
693 "add r4, r4, r3 \n\t" \
695 "ldr r3, [r1] \n\t" \
696 "add r4, r4, r3 \n\t" \
698 "stmia r1!, {r4} \n\t"
700#define MULADDC_STOP \
704 : "=m" (c), "=m" (d), "=m" (s) \
705 : "m" (s), "m" (d), "m" (c), "m" (b) \
706 : "r0", "r1", "r2", "r3", "r4", "r5", \
707 "r6", "r7", "r8", "r9", "cc" \
710#elif (__ARM_ARCH >= 6) && \
711 defined (__ARM_FEATURE_DSP) && (__ARM_FEATURE_DSP == 1)
713#define MULADDC_INIT \
716#define MULADDC_CORE \
717 "ldr r0, [%0], #4 \n\t" \
718 "ldr r1, [%1] \n\t" \
719 "umaal r1, %2, %3, r0 \n\t" \
720 "str r1, [%1], #4 \n\t"
722#define MULADDC_STOP \
723 : "=r" (s), "=r" (d), "=r" (c) \
724 : "r" (b), "0" (s), "1" (d), "2" (c) \
725 : "r0", "r1", "memory" \
730#define MULADDC_INIT \
737#define MULADDC_CORE \
738 "ldr r4, [r0], #4 \n\t" \
740 "ldr r6, [r1] \n\t" \
741 "umlal r2, r5, r3, r4 \n\t" \
742 "adds r7, r6, r2 \n\t" \
743 "adc r2, r5, #0 \n\t" \
744 "str r7, [r1], #4 \n\t"
746#define MULADDC_STOP \
750 : "=m" (c), "=m" (d), "=m" (s) \
751 : "m" (s), "m" (d), "m" (c), "m" (b) \
752 : "r0", "r1", "r2", "r3", "r4", "r5", \
760#if defined(__alpha__)
762#define MULADDC_INIT \
769#define MULADDC_CORE \
770 "ldq $6, 0($1) \n\t" \
771 "addq $1, 8, $1 \n\t" \
772 "mulq $6, $4, $7 \n\t" \
773 "umulh $6, $4, $6 \n\t" \
774 "addq $7, $3, $7 \n\t" \
775 "cmpult $7, $3, $3 \n\t" \
776 "ldq $5, 0($2) \n\t" \
777 "addq $7, $5, $7 \n\t" \
778 "cmpult $7, $5, $5 \n\t" \
779 "stq $7, 0($2) \n\t" \
780 "addq $2, 8, $2 \n\t" \
781 "addq $6, $3, $3 \n\t" \
782 "addq $5, $3, $3 \n\t"
784#define MULADDC_STOP \
788 : "=m" (c), "=m" (d), "=m" (s) \
789 : "m" (s), "m" (d), "m" (c), "m" (b) \
790 : "$1", "$2", "$3", "$4", "$5", "$6", "$7" \
794#if defined(__mips__) && !defined(__mips64)
796#define MULADDC_INIT \
803#define MULADDC_CORE \
804 "lw $14, 0($10) \n\t" \
805 "multu $13, $14 \n\t" \
806 "addi $10, $10, 4 \n\t" \
809 "addu $14, $12, $14 \n\t" \
810 "lw $15, 0($11) \n\t" \
811 "sltu $12, $14, $12 \n\t" \
812 "addu $15, $14, $15 \n\t" \
813 "sltu $14, $15, $14 \n\t" \
814 "addu $12, $12, $9 \n\t" \
815 "sw $15, 0($11) \n\t" \
816 "addu $12, $12, $14 \n\t" \
817 "addi $11, $11, 4 \n\t"
819#define MULADDC_STOP \
823 : "=m" (c), "=m" (d), "=m" (s) \
824 : "m" (s), "m" (d), "m" (c), "m" (b) \
825 : "$9", "$10", "$11", "$12", "$13", "$14", "$15", "lo", "hi" \
831#if (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
833#define MULADDC_INIT \
839#define MULADDC_CORE \
844 __asm add eax, [edi] \
849#if defined(MBEDTLS_HAVE_SSE2)
851#define EMIT __asm _emit
853#define MULADDC_HUIT \
854 EMIT 0x0F EMIT 0x6E EMIT 0xC9 \
855 EMIT 0x0F EMIT 0x6E EMIT 0xC3 \
856 EMIT 0x0F EMIT 0x6E EMIT 0x1F \
857 EMIT 0x0F EMIT 0xD4 EMIT 0xCB \
858 EMIT 0x0F EMIT 0x6E EMIT 0x16 \
859 EMIT 0x0F EMIT 0xF4 EMIT 0xD0 \
860 EMIT 0x0F EMIT 0x6E EMIT 0x66 EMIT 0x04 \
861 EMIT 0x0F EMIT 0xF4 EMIT 0xE0 \
862 EMIT 0x0F EMIT 0x6E EMIT 0x76 EMIT 0x08 \
863 EMIT 0x0F EMIT 0xF4 EMIT 0xF0 \
864 EMIT 0x0F EMIT 0x6E EMIT 0x7E EMIT 0x0C \
865 EMIT 0x0F EMIT 0xF4 EMIT 0xF8 \
866 EMIT 0x0F EMIT 0xD4 EMIT 0xCA \
867 EMIT 0x0F EMIT 0x6E EMIT 0x5F EMIT 0x04 \
868 EMIT 0x0F EMIT 0xD4 EMIT 0xDC \
869 EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x08 \
870 EMIT 0x0F EMIT 0xD4 EMIT 0xEE \
871 EMIT 0x0F EMIT 0x6E EMIT 0x67 EMIT 0x0C \
872 EMIT 0x0F EMIT 0xD4 EMIT 0xFC \
873 EMIT 0x0F EMIT 0x7E EMIT 0x0F \
874 EMIT 0x0F EMIT 0x6E EMIT 0x56 EMIT 0x10 \
875 EMIT 0x0F EMIT 0xF4 EMIT 0xD0 \
876 EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
877 EMIT 0x0F EMIT 0x6E EMIT 0x66 EMIT 0x14 \
878 EMIT 0x0F EMIT 0xF4 EMIT 0xE0 \
879 EMIT 0x0F EMIT 0xD4 EMIT 0xCB \
880 EMIT 0x0F EMIT 0x6E EMIT 0x76 EMIT 0x18 \
881 EMIT 0x0F EMIT 0xF4 EMIT 0xF0 \
882 EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x04 \
883 EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
884 EMIT 0x0F EMIT 0x6E EMIT 0x5E EMIT 0x1C \
885 EMIT 0x0F EMIT 0xF4 EMIT 0xD8 \
886 EMIT 0x0F EMIT 0xD4 EMIT 0xCD \
887 EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x10 \
888 EMIT 0x0F EMIT 0xD4 EMIT 0xD5 \
889 EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x08 \
890 EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
891 EMIT 0x0F EMIT 0xD4 EMIT 0xCF \
892 EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x14 \
893 EMIT 0x0F EMIT 0xD4 EMIT 0xE5 \
894 EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x0C \
895 EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
896 EMIT 0x0F EMIT 0xD4 EMIT 0xCA \
897 EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x18 \
898 EMIT 0x0F EMIT 0xD4 EMIT 0xF5 \
899 EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x10 \
900 EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
901 EMIT 0x0F EMIT 0xD4 EMIT 0xCC \
902 EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x1C \
903 EMIT 0x0F EMIT 0xD4 EMIT 0xDD \
904 EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x14 \
905 EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
906 EMIT 0x0F EMIT 0xD4 EMIT 0xCE \
907 EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x18 \
908 EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
909 EMIT 0x0F EMIT 0xD4 EMIT 0xCB \
910 EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x1C \
911 EMIT 0x83 EMIT 0xC7 EMIT 0x20 \
912 EMIT 0x83 EMIT 0xC6 EMIT 0x20 \
913 EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
914 EMIT 0x0F EMIT 0x7E EMIT 0xC9
916#define MULADDC_STOP \
917 EMIT 0x0F EMIT 0x77 \
924#define MULADDC_STOP \
934#if !defined(MULADDC_CORE)
935#if defined(MBEDTLS_HAVE_UDBL)
937#define MULADDC_INIT \
940 mbedtls_mpi_uint r0, r1;
942#define MULADDC_CORE \
943 r = *(s++) * (mbedtls_t_udbl) b; \
944 r0 = (mbedtls_mpi_uint) r; \
945 r1 = (mbedtls_mpi_uint)( r >> biL ); \
946 r0 += c; r1 += (r0 < c); \
947 r0 += *d; r1 += (r0 < *d); \
950#define MULADDC_STOP \
954#define MULADDC_INIT \
956 mbedtls_mpi_uint s0, s1, b0, b1; \
957 mbedtls_mpi_uint r0, r1, rx, ry; \
958 b0 = ( b << biH ) >> biH; \
961#define MULADDC_CORE \
962 s0 = ( *s << biH ) >> biH; \
963 s1 = ( *s >> biH ); s++; \
964 rx = s0 * b1; r0 = s0 * b0; \
965 ry = s1 * b0; r1 = s1 * b1; \
966 r1 += ( rx >> biH ); \
967 r1 += ( ry >> biH ); \
968 rx <<= biH; ry <<= biH; \
969 r0 += rx; r1 += (r0 < rx); \
970 r0 += ry; r1 += (r0 < ry); \
971 r0 += c; r1 += (r0 < c); \
972 r0 += *d; r1 += (r0 < *d); \
975#define MULADDC_STOP \
Multi-precision integer library.