mbed TLS library

Dependents:   HTTPClient-SSL WS_SERVER

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers bn_mul.h Source File

bn_mul.h

Go to the documentation of this file.
00001 /**
00002  * \file bn_mul.h
00003  *
00004  * \brief  Multi-precision integer library
00005  *
00006  *  Copyright (C) 2006-2010, ARM Limited, All Rights Reserved
00007  *
00008  *  This file is part of mbed TLS (https://tls.mbed.org)
00009  *
00010  *  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU General Public License for more details.
00019  *
00020  *  You should have received a copy of the GNU General Public License along
00021  *  with this program; if not, write to the Free Software Foundation, Inc.,
00022  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00023  */
00024 /*
00025  *      Multiply source vector [s] with b, add result
00026  *       to destination vector [d] and set carry c.
00027  *
00028  *      Currently supports:
00029  *
00030  *         . IA-32 (386+)         . AMD64 / EM64T
00031  *         . IA-32 (SSE2)         . Motorola 68000
00032  *         . PowerPC, 32-bit      . MicroBlaze
00033  *         . PowerPC, 64-bit      . TriCore
00034  *         . SPARC v8             . ARM v3+
00035  *         . Alpha                . MIPS32
00036  *         . C, longlong          . C, generic
00037  */
00038 #ifndef POLARSSL_BN_MUL_H
00039 #define POLARSSL_BN_MUL_H
00040 
00041 #include "bignum.h"
00042 
00043 #if defined(POLARSSL_HAVE_ASM)
00044 
00045 #if defined(__GNUC__)
00046 #if defined(__i386__)
00047 
00048 #define MULADDC_INIT                        \
00049     asm(                                    \
00050         "movl   %%ebx, %0           \n\t"   \
00051         "movl   %5, %%esi           \n\t"   \
00052         "movl   %6, %%edi           \n\t"   \
00053         "movl   %7, %%ecx           \n\t"   \
00054         "movl   %8, %%ebx           \n\t"
00055 
00056 #define MULADDC_CORE                        \
00057         "lodsl                      \n\t"   \
00058         "mull   %%ebx               \n\t"   \
00059         "addl   %%ecx,   %%eax      \n\t"   \
00060         "adcl   $0,      %%edx      \n\t"   \
00061         "addl   (%%edi), %%eax      \n\t"   \
00062         "adcl   $0,      %%edx      \n\t"   \
00063         "movl   %%edx,   %%ecx      \n\t"   \
00064         "stosl                      \n\t"
00065 
00066 #if defined(POLARSSL_HAVE_SSE2)
00067 
00068 #define MULADDC_HUIT                            \
00069         "movd     %%ecx,     %%mm1      \n\t"   \
00070         "movd     %%ebx,     %%mm0      \n\t"   \
00071         "movd     (%%edi),   %%mm3      \n\t"   \
00072         "paddq    %%mm3,     %%mm1      \n\t"   \
00073         "movd     (%%esi),   %%mm2      \n\t"   \
00074         "pmuludq  %%mm0,     %%mm2      \n\t"   \
00075         "movd     4(%%esi),  %%mm4      \n\t"   \
00076         "pmuludq  %%mm0,     %%mm4      \n\t"   \
00077         "movd     8(%%esi),  %%mm6      \n\t"   \
00078         "pmuludq  %%mm0,     %%mm6      \n\t"   \
00079         "movd     12(%%esi), %%mm7      \n\t"   \
00080         "pmuludq  %%mm0,     %%mm7      \n\t"   \
00081         "paddq    %%mm2,     %%mm1      \n\t"   \
00082         "movd     4(%%edi),  %%mm3      \n\t"   \
00083         "paddq    %%mm4,     %%mm3      \n\t"   \
00084         "movd     8(%%edi),  %%mm5      \n\t"   \
00085         "paddq    %%mm6,     %%mm5      \n\t"   \
00086         "movd     12(%%edi), %%mm4      \n\t"   \
00087         "paddq    %%mm4,     %%mm7      \n\t"   \
00088         "movd     %%mm1,     (%%edi)    \n\t"   \
00089         "movd     16(%%esi), %%mm2      \n\t"   \
00090         "pmuludq  %%mm0,     %%mm2      \n\t"   \
00091         "psrlq    $32,       %%mm1      \n\t"   \
00092         "movd     20(%%esi), %%mm4      \n\t"   \
00093         "pmuludq  %%mm0,     %%mm4      \n\t"   \
00094         "paddq    %%mm3,     %%mm1      \n\t"   \
00095         "movd     24(%%esi), %%mm6      \n\t"   \
00096         "pmuludq  %%mm0,     %%mm6      \n\t"   \
00097         "movd     %%mm1,     4(%%edi)   \n\t"   \
00098         "psrlq    $32,       %%mm1      \n\t"   \
00099         "movd     28(%%esi), %%mm3      \n\t"   \
00100         "pmuludq  %%mm0,     %%mm3      \n\t"   \
00101         "paddq    %%mm5,     %%mm1      \n\t"   \
00102         "movd     16(%%edi), %%mm5      \n\t"   \
00103         "paddq    %%mm5,     %%mm2      \n\t"   \
00104         "movd     %%mm1,     8(%%edi)   \n\t"   \
00105         "psrlq    $32,       %%mm1      \n\t"   \
00106         "paddq    %%mm7,     %%mm1      \n\t"   \
00107         "movd     20(%%edi), %%mm5      \n\t"   \
00108         "paddq    %%mm5,     %%mm4      \n\t"   \
00109         "movd     %%mm1,     12(%%edi)  \n\t"   \
00110         "psrlq    $32,       %%mm1      \n\t"   \
00111         "paddq    %%mm2,     %%mm1      \n\t"   \
00112         "movd     24(%%edi), %%mm5      \n\t"   \
00113         "paddq    %%mm5,     %%mm6      \n\t"   \
00114         "movd     %%mm1,     16(%%edi)  \n\t"   \
00115         "psrlq    $32,       %%mm1      \n\t"   \
00116         "paddq    %%mm4,     %%mm1      \n\t"   \
00117         "movd     28(%%edi), %%mm5      \n\t"   \
00118         "paddq    %%mm5,     %%mm3      \n\t"   \
00119         "movd     %%mm1,     20(%%edi)  \n\t"   \
00120         "psrlq    $32,       %%mm1      \n\t"   \
00121         "paddq    %%mm6,     %%mm1      \n\t"   \
00122         "movd     %%mm1,     24(%%edi)  \n\t"   \
00123         "psrlq    $32,       %%mm1      \n\t"   \
00124         "paddq    %%mm3,     %%mm1      \n\t"   \
00125         "movd     %%mm1,     28(%%edi)  \n\t"   \
00126         "addl     $32,       %%edi      \n\t"   \
00127         "addl     $32,       %%esi      \n\t"   \
00128         "psrlq    $32,       %%mm1      \n\t"   \
00129         "movd     %%mm1,     %%ecx      \n\t"
00130 
00131 #define MULADDC_STOP                    \
00132         "emms                   \n\t"   \
00133         "movl   %4, %%ebx       \n\t"   \
00134         "movl   %%ecx, %1       \n\t"   \
00135         "movl   %%edi, %2       \n\t"   \
00136         "movl   %%esi, %3       \n\t"   \
00137         : "=m" (t), "=m" (c), "=m" (d), "=m" (s)        \
00138         : "m" (t), "m" (s), "m" (d), "m" (c), "m" (b)   \
00139         : "eax", "ecx", "edx", "esi", "edi"             \
00140     );
00141 
00142 #else
00143 
00144 #define MULADDC_STOP                    \
00145         "movl   %4, %%ebx       \n\t"   \
00146         "movl   %%ecx, %1       \n\t"   \
00147         "movl   %%edi, %2       \n\t"   \
00148         "movl   %%esi, %3       \n\t"   \
00149         : "=m" (t), "=m" (c), "=m" (d), "=m" (s)        \
00150         : "m" (t), "m" (s), "m" (d), "m" (c), "m" (b)   \
00151         : "eax", "ecx", "edx", "esi", "edi"             \
00152     );
00153 #endif /* SSE2 */
00154 #endif /* i386 */
00155 
00156 #if defined(__amd64__) || defined (__x86_64__)
00157 
00158 #define MULADDC_INIT                        \
00159     asm(                                    \
00160         "movq   %3, %%rsi           \n\t"   \
00161         "movq   %4, %%rdi           \n\t"   \
00162         "movq   %5, %%rcx           \n\t"   \
00163         "movq   %6, %%rbx           \n\t"   \
00164         "xorq   %%r8, %%r8          \n\t"
00165 
00166 #define MULADDC_CORE                        \
00167         "movq   (%%rsi), %%rax      \n\t"   \
00168         "mulq   %%rbx               \n\t"   \
00169         "addq   $8,      %%rsi      \n\t"   \
00170         "addq   %%rcx,   %%rax      \n\t"   \
00171         "movq   %%r8,    %%rcx      \n\t"   \
00172         "adcq   $0,      %%rdx      \n\t"   \
00173         "nop                        \n\t"   \
00174         "addq   %%rax,   (%%rdi)    \n\t"   \
00175         "adcq   %%rdx,   %%rcx      \n\t"   \
00176         "addq   $8,      %%rdi      \n\t"
00177 
00178 #define MULADDC_STOP                        \
00179         "movq   %%rcx, %0           \n\t"   \
00180         "movq   %%rdi, %1           \n\t"   \
00181         "movq   %%rsi, %2           \n\t"   \
00182         : "=m" (c), "=m" (d), "=m" (s)                      \
00183         : "m" (s), "m" (d), "m" (c), "m" (b)                \
00184         : "rax", "rcx", "rdx", "rbx", "rsi", "rdi", "r8"    \
00185     );
00186 
00187 #endif /* AMD64 */
00188 
00189 #if defined(__mc68020__) || defined(__mcpu32__)
00190 
00191 #define MULADDC_INIT                    \
00192     asm(                                \
00193         "movl   %3, %%a2        \n\t"   \
00194         "movl   %4, %%a3        \n\t"   \
00195         "movl   %5, %%d3        \n\t"   \
00196         "movl   %6, %%d2        \n\t"   \
00197         "moveq  #0, %%d0        \n\t"
00198 
00199 #define MULADDC_CORE                    \
00200         "movel  %%a2@+, %%d1    \n\t"   \
00201         "mulul  %%d2, %%d4:%%d1 \n\t"   \
00202         "addl   %%d3, %%d1      \n\t"   \
00203         "addxl  %%d0, %%d4      \n\t"   \
00204         "moveq  #0,   %%d3      \n\t"   \
00205         "addl   %%d1, %%a3@+    \n\t"   \
00206         "addxl  %%d4, %%d3      \n\t"
00207 
00208 #define MULADDC_STOP                    \
00209         "movl   %%d3, %0        \n\t"   \
00210         "movl   %%a3, %1        \n\t"   \
00211         "movl   %%a2, %2        \n\t"   \
00212         : "=m" (c), "=m" (d), "=m" (s)              \
00213         : "m" (s), "m" (d), "m" (c), "m" (b)        \
00214         : "d0", "d1", "d2", "d3", "d4", "a2", "a3"  \
00215     );
00216 
00217 #define MULADDC_HUIT                        \
00218         "movel  %%a2@+,  %%d1       \n\t"   \
00219         "mulul  %%d2,    %%d4:%%d1  \n\t"   \
00220         "addxl  %%d3,    %%d1       \n\t"   \
00221         "addxl  %%d0,    %%d4       \n\t"   \
00222         "addl   %%d1,    %%a3@+     \n\t"   \
00223         "movel  %%a2@+,  %%d1       \n\t"   \
00224         "mulul  %%d2,    %%d3:%%d1  \n\t"   \
00225         "addxl  %%d4,    %%d1       \n\t"   \
00226         "addxl  %%d0,    %%d3       \n\t"   \
00227         "addl   %%d1,    %%a3@+     \n\t"   \
00228         "movel  %%a2@+,  %%d1       \n\t"   \
00229         "mulul  %%d2,    %%d4:%%d1  \n\t"   \
00230         "addxl  %%d3,    %%d1       \n\t"   \
00231         "addxl  %%d0,    %%d4       \n\t"   \
00232         "addl   %%d1,    %%a3@+     \n\t"   \
00233         "movel  %%a2@+,  %%d1       \n\t"   \
00234         "mulul  %%d2,    %%d3:%%d1  \n\t"   \
00235         "addxl  %%d4,    %%d1       \n\t"   \
00236         "addxl  %%d0,    %%d3       \n\t"   \
00237         "addl   %%d1,    %%a3@+     \n\t"   \
00238         "movel  %%a2@+,  %%d1       \n\t"   \
00239         "mulul  %%d2,    %%d4:%%d1  \n\t"   \
00240         "addxl  %%d3,    %%d1       \n\t"   \
00241         "addxl  %%d0,    %%d4       \n\t"   \
00242         "addl   %%d1,    %%a3@+     \n\t"   \
00243         "movel  %%a2@+,  %%d1       \n\t"   \
00244         "mulul  %%d2,    %%d3:%%d1  \n\t"   \
00245         "addxl  %%d4,    %%d1       \n\t"   \
00246         "addxl  %%d0,    %%d3       \n\t"   \
00247         "addl   %%d1,    %%a3@+     \n\t"   \
00248         "movel  %%a2@+,  %%d1       \n\t"   \
00249         "mulul  %%d2,    %%d4:%%d1  \n\t"   \
00250         "addxl  %%d3,    %%d1       \n\t"   \
00251         "addxl  %%d0,    %%d4       \n\t"   \
00252         "addl   %%d1,    %%a3@+     \n\t"   \
00253         "movel  %%a2@+,  %%d1       \n\t"   \
00254         "mulul  %%d2,    %%d3:%%d1  \n\t"   \
00255         "addxl  %%d4,    %%d1       \n\t"   \
00256         "addxl  %%d0,    %%d3       \n\t"   \
00257         "addl   %%d1,    %%a3@+     \n\t"   \
00258         "addxl  %%d0,    %%d3       \n\t"
00259 
00260 #endif /* MC68000 */
00261 
00262 #if defined(__powerpc64__) || defined(__ppc64__)
00263 
00264 #if defined(__MACH__) && defined(__APPLE__)
00265 
00266 #define MULADDC_INIT                        \
00267     asm(                                    \
00268         "ld     r3, %3              \n\t"   \
00269         "ld     r4, %4              \n\t"   \
00270         "ld     r5, %5              \n\t"   \
00271         "ld     r6, %6              \n\t"   \
00272         "addi   r3, r3, -8          \n\t"   \
00273         "addi   r4, r4, -8          \n\t"   \
00274         "addic  r5, r5,  0          \n\t"
00275 
00276 #define MULADDC_CORE                        \
00277         "ldu    r7, 8(r3)           \n\t"   \
00278         "mulld  r8, r7, r6          \n\t"   \
00279         "mulhdu r9, r7, r6          \n\t"   \
00280         "adde   r8, r8, r5          \n\t"   \
00281         "ld     r7, 8(r4)           \n\t"   \
00282         "addze  r5, r9              \n\t"   \
00283         "addc   r8, r8, r7          \n\t"   \
00284         "stdu   r8, 8(r4)           \n\t"
00285 
00286 #define MULADDC_STOP                        \
00287         "addze  r5, r5              \n\t"   \
00288         "addi   r4, r4, 8           \n\t"   \
00289         "addi   r3, r3, 8           \n\t"   \
00290         "std    r5, %0              \n\t"   \
00291         "std    r4, %1              \n\t"   \
00292         "std    r3, %2              \n\t"   \
00293         : "=m" (c), "=m" (d), "=m" (s)              \
00294         : "m" (s), "m" (d), "m" (c), "m" (b)        \
00295         : "r3", "r4", "r5", "r6", "r7", "r8", "r9"  \
00296     );
00297 
00298 
00299 #else /* __MACH__ && __APPLE__ */
00300 
00301 #define MULADDC_INIT                        \
00302     asm(                                    \
00303         "ld     %%r3, %3            \n\t"   \
00304         "ld     %%r4, %4            \n\t"   \
00305         "ld     %%r5, %5            \n\t"   \
00306         "ld     %%r6, %6            \n\t"   \
00307         "addi   %%r3, %%r3, -8      \n\t"   \
00308         "addi   %%r4, %%r4, -8      \n\t"   \
00309         "addic  %%r5, %%r5,  0      \n\t"
00310 
00311 #define MULADDC_CORE                        \
00312         "ldu    %%r7, 8(%%r3)       \n\t"   \
00313         "mulld  %%r8, %%r7, %%r6    \n\t"   \
00314         "mulhdu %%r9, %%r7, %%r6    \n\t"   \
00315         "adde   %%r8, %%r8, %%r5    \n\t"   \
00316         "ld     %%r7, 8(%%r4)       \n\t"   \
00317         "addze  %%r5, %%r9          \n\t"   \
00318         "addc   %%r8, %%r8, %%r7    \n\t"   \
00319         "stdu   %%r8, 8(%%r4)       \n\t"
00320 
00321 #define MULADDC_STOP                        \
00322         "addze  %%r5, %%r5          \n\t"   \
00323         "addi   %%r4, %%r4, 8       \n\t"   \
00324         "addi   %%r3, %%r3, 8       \n\t"   \
00325         "std    %%r5, %0            \n\t"   \
00326         "std    %%r4, %1            \n\t"   \
00327         "std    %%r3, %2            \n\t"   \
00328         : "=m" (c), "=m" (d), "=m" (s)              \
00329         : "m" (s), "m" (d), "m" (c), "m" (b)        \
00330         : "r3", "r4", "r5", "r6", "r7", "r8", "r9"  \
00331     );
00332 
00333 #endif /* __MACH__ && __APPLE__ */
00334 
00335 #elif defined(__powerpc__) || defined(__ppc__) /* end PPC64/begin PPC32  */
00336 
00337 #if defined(__MACH__) && defined(__APPLE__)
00338 
00339 #define MULADDC_INIT                    \
00340     asm(                                \
00341         "lwz    r3, %3          \n\t"   \
00342         "lwz    r4, %4          \n\t"   \
00343         "lwz    r5, %5          \n\t"   \
00344         "lwz    r6, %6          \n\t"   \
00345         "addi   r3, r3, -4      \n\t"   \
00346         "addi   r4, r4, -4      \n\t"   \
00347         "addic  r5, r5,  0      \n\t"
00348 
00349 #define MULADDC_CORE                    \
00350         "lwzu   r7, 4(r3)       \n\t"   \
00351         "mullw  r8, r7, r6      \n\t"   \
00352         "mulhwu r9, r7, r6      \n\t"   \
00353         "adde   r8, r8, r5      \n\t"   \
00354         "lwz    r7, 4(r4)       \n\t"   \
00355         "addze  r5, r9          \n\t"   \
00356         "addc   r8, r8, r7      \n\t"   \
00357         "stwu   r8, 4(r4)       \n\t"
00358 
00359 #define MULADDC_STOP                    \
00360         "addze  r5, r5          \n\t"   \
00361         "addi   r4, r4, 4       \n\t"   \
00362         "addi   r3, r3, 4       \n\t"   \
00363         "stw    r5, %0          \n\t"   \
00364         "stw    r4, %1          \n\t"   \
00365         "stw    r3, %2          \n\t"   \
00366         : "=m" (c), "=m" (d), "=m" (s)              \
00367         : "m" (s), "m" (d), "m" (c), "m" (b)        \
00368         : "r3", "r4", "r5", "r6", "r7", "r8", "r9"  \
00369     );
00370 
00371 #else /* __MACH__ && __APPLE__ */
00372 
00373 #define MULADDC_INIT                        \
00374     asm(                                    \
00375         "lwz    %%r3, %3            \n\t"   \
00376         "lwz    %%r4, %4            \n\t"   \
00377         "lwz    %%r5, %5            \n\t"   \
00378         "lwz    %%r6, %6            \n\t"   \
00379         "addi   %%r3, %%r3, -4      \n\t"   \
00380         "addi   %%r4, %%r4, -4      \n\t"   \
00381         "addic  %%r5, %%r5,  0      \n\t"
00382 
00383 #define MULADDC_CORE                        \
00384         "lwzu   %%r7, 4(%%r3)       \n\t"   \
00385         "mullw  %%r8, %%r7, %%r6    \n\t"   \
00386         "mulhwu %%r9, %%r7, %%r6    \n\t"   \
00387         "adde   %%r8, %%r8, %%r5    \n\t"   \
00388         "lwz    %%r7, 4(%%r4)       \n\t"   \
00389         "addze  %%r5, %%r9          \n\t"   \
00390         "addc   %%r8, %%r8, %%r7    \n\t"   \
00391         "stwu   %%r8, 4(%%r4)       \n\t"
00392 
00393 #define MULADDC_STOP                        \
00394         "addze  %%r5, %%r5          \n\t"   \
00395         "addi   %%r4, %%r4, 4       \n\t"   \
00396         "addi   %%r3, %%r3, 4       \n\t"   \
00397         "stw    %%r5, %0            \n\t"   \
00398         "stw    %%r4, %1            \n\t"   \
00399         "stw    %%r3, %2            \n\t"   \
00400         : "=m" (c), "=m" (d), "=m" (s)              \
00401         : "m" (s), "m" (d), "m" (c), "m" (b)        \
00402         : "r3", "r4", "r5", "r6", "r7", "r8", "r9"  \
00403     );
00404 
00405 #endif /* __MACH__ && __APPLE__ */
00406 
00407 #endif /* PPC32 */
00408 
00409 /*
00410  * The Sparc64 assembly is reported to be broken.
00411  * Disable it for now, until we're able to fix it.
00412  */
00413 #if 0 && defined(__sparc__) && defined(__sparc64__)
00414 
00415 #define MULADDC_INIT                                    \
00416     asm(                                                \
00417                 "ldx     %3, %%o0               \n\t"   \
00418                 "ldx     %4, %%o1               \n\t"   \
00419                 "ld      %5, %%o2               \n\t"   \
00420                 "ld      %6, %%o3               \n\t"
00421 
00422 #define MULADDC_CORE                                    \
00423                 "ld      [%%o0], %%o4           \n\t"   \
00424                 "inc     4, %%o0                \n\t"   \
00425                 "ld      [%%o1], %%o5           \n\t"   \
00426                 "umul    %%o3, %%o4, %%o4       \n\t"   \
00427                 "addcc   %%o4, %%o2, %%o4       \n\t"   \
00428                 "rd      %%y, %%g1              \n\t"   \
00429                 "addx    %%g1, 0, %%g1          \n\t"   \
00430                 "addcc   %%o4, %%o5, %%o4       \n\t"   \
00431                 "st      %%o4, [%%o1]           \n\t"   \
00432                 "addx    %%g1, 0, %%o2          \n\t"   \
00433                 "inc     4, %%o1                \n\t"
00434 
00435         #define MULADDC_STOP                            \
00436                 "st      %%o2, %0               \n\t"   \
00437                 "stx     %%o1, %1               \n\t"   \
00438                 "stx     %%o0, %2               \n\t"   \
00439         : "=m" (c), "=m" (d), "=m" (s)          \
00440         : "m" (s), "m" (d), "m" (c), "m" (b)    \
00441         : "g1", "o0", "o1", "o2", "o3", "o4",   \
00442           "o5"                                  \
00443         );
00444 #endif /* SPARCv9 */
00445 
00446 #if defined(__sparc__) && !defined(__sparc64__)
00447 
00448 #define MULADDC_INIT                                    \
00449     asm(                                                \
00450                 "ld      %3, %%o0               \n\t"   \
00451                 "ld      %4, %%o1               \n\t"   \
00452                 "ld      %5, %%o2               \n\t"   \
00453                 "ld      %6, %%o3               \n\t"
00454 
00455 #define MULADDC_CORE                                    \
00456                 "ld      [%%o0], %%o4           \n\t"   \
00457                 "inc     4, %%o0                \n\t"   \
00458                 "ld      [%%o1], %%o5           \n\t"   \
00459                 "umul    %%o3, %%o4, %%o4       \n\t"   \
00460                 "addcc   %%o4, %%o2, %%o4       \n\t"   \
00461                 "rd      %%y, %%g1              \n\t"   \
00462                 "addx    %%g1, 0, %%g1          \n\t"   \
00463                 "addcc   %%o4, %%o5, %%o4       \n\t"   \
00464                 "st      %%o4, [%%o1]           \n\t"   \
00465                 "addx    %%g1, 0, %%o2          \n\t"   \
00466                 "inc     4, %%o1                \n\t"
00467 
00468 #define MULADDC_STOP                                    \
00469                 "st      %%o2, %0               \n\t"   \
00470                 "st      %%o1, %1               \n\t"   \
00471                 "st      %%o0, %2               \n\t"   \
00472         : "=m" (c), "=m" (d), "=m" (s)          \
00473         : "m" (s), "m" (d), "m" (c), "m" (b)    \
00474         : "g1", "o0", "o1", "o2", "o3", "o4",   \
00475           "o5"                                  \
00476         );
00477 
00478 #endif /* SPARCv8 */
00479 
00480 #if defined(__microblaze__) || defined(microblaze)
00481 
00482 #define MULADDC_INIT                    \
00483     asm(                                \
00484         "lwi   r3,   %3         \n\t"   \
00485         "lwi   r4,   %4         \n\t"   \
00486         "lwi   r5,   %5         \n\t"   \
00487         "lwi   r6,   %6         \n\t"   \
00488         "andi  r7,   r6, 0xffff \n\t"   \
00489         "bsrli r6,   r6, 16     \n\t"
00490 
00491 #define MULADDC_CORE                    \
00492         "lhui  r8,   r3,   0    \n\t"   \
00493         "addi  r3,   r3,   2    \n\t"   \
00494         "lhui  r9,   r3,   0    \n\t"   \
00495         "addi  r3,   r3,   2    \n\t"   \
00496         "mul   r10,  r9,  r6    \n\t"   \
00497         "mul   r11,  r8,  r7    \n\t"   \
00498         "mul   r12,  r9,  r7    \n\t"   \
00499         "mul   r13,  r8,  r6    \n\t"   \
00500         "bsrli  r8, r10,  16    \n\t"   \
00501         "bsrli  r9, r11,  16    \n\t"   \
00502         "add   r13, r13,  r8    \n\t"   \
00503         "add   r13, r13,  r9    \n\t"   \
00504         "bslli r10, r10,  16    \n\t"   \
00505         "bslli r11, r11,  16    \n\t"   \
00506         "add   r12, r12, r10    \n\t"   \
00507         "addc  r13, r13,  r0    \n\t"   \
00508         "add   r12, r12, r11    \n\t"   \
00509         "addc  r13, r13,  r0    \n\t"   \
00510         "lwi   r10,  r4,   0    \n\t"   \
00511         "add   r12, r12, r10    \n\t"   \
00512         "addc  r13, r13,  r0    \n\t"   \
00513         "add   r12, r12,  r5    \n\t"   \
00514         "addc   r5, r13,  r0    \n\t"   \
00515         "swi   r12,  r4,   0    \n\t"   \
00516         "addi   r4,  r4,   4    \n\t"
00517 
00518 #define MULADDC_STOP                    \
00519         "swi   r5,   %0         \n\t"   \
00520         "swi   r4,   %1         \n\t"   \
00521         "swi   r3,   %2         \n\t"   \
00522         : "=m" (c), "=m" (d), "=m" (s)              \
00523         : "m" (s), "m" (d), "m" (c), "m" (b)        \
00524         : "r3", "r4"  "r5", "r6", "r7", "r8",       \
00525           "r9", "r10", "r11", "r12", "r13"          \
00526     );
00527 
00528 #endif /* MicroBlaze */
00529 
00530 #if defined(__tricore__)
00531 
00532 #define MULADDC_INIT                            \
00533     asm(                                        \
00534         "ld.a   %%a2, %3                \n\t"   \
00535         "ld.a   %%a3, %4                \n\t"   \
00536         "ld.w   %%d4, %5                \n\t"   \
00537         "ld.w   %%d1, %6                \n\t"   \
00538         "xor    %%d5, %%d5              \n\t"
00539 
00540 #define MULADDC_CORE                            \
00541         "ld.w   %%d0,   [%%a2+]         \n\t"   \
00542         "madd.u %%e2, %%e4, %%d0, %%d1  \n\t"   \
00543         "ld.w   %%d0,   [%%a3]          \n\t"   \
00544         "addx   %%d2,    %%d2,  %%d0    \n\t"   \
00545         "addc   %%d3,    %%d3,    0     \n\t"   \
00546         "mov    %%d4,    %%d3           \n\t"   \
00547         "st.w  [%%a3+],  %%d2           \n\t"
00548 
00549 #define MULADDC_STOP                            \
00550         "st.w   %0, %%d4                \n\t"   \
00551         "st.a   %1, %%a3                \n\t"   \
00552         "st.a   %2, %%a2                \n\t"   \
00553         : "=m" (c), "=m" (d), "=m" (s)          \
00554         : "m" (s), "m" (d), "m" (c), "m" (b)    \
00555         : "d0", "d1", "e2", "d4", "a2", "a3"    \
00556     );
00557 
00558 #endif /* TriCore */
00559 
00560 #if defined(__arm__)
00561 
00562 #if defined(__thumb__) && !defined(__thumb2__)
00563 
00564 #define MULADDC_INIT                                    \
00565     asm(                                                \
00566             "ldr    r0, %3                      \n\t"   \
00567             "ldr    r1, %4                      \n\t"   \
00568             "ldr    r2, %5                      \n\t"   \
00569             "ldr    r3, %6                      \n\t"   \
00570             "lsr    r7, r3, #16                 \n\t"   \
00571             "mov    r9, r7                      \n\t"   \
00572             "lsl    r7, r3, #16                 \n\t"   \
00573             "lsr    r7, r7, #16                 \n\t"   \
00574             "mov    r8, r7                      \n\t"
00575 
00576 #define MULADDC_CORE                                    \
00577             "ldmia  r0!, {r6}                   \n\t"   \
00578             "lsr    r7, r6, #16                 \n\t"   \
00579             "lsl    r6, r6, #16                 \n\t"   \
00580             "lsr    r6, r6, #16                 \n\t"   \
00581             "mov    r4, r8                      \n\t"   \
00582             "mul    r4, r6                      \n\t"   \
00583             "mov    r3, r9                      \n\t"   \
00584             "mul    r6, r3                      \n\t"   \
00585             "mov    r5, r9                      \n\t"   \
00586             "mul    r5, r7                      \n\t"   \
00587             "mov    r3, r8                      \n\t"   \
00588             "mul    r7, r3                      \n\t"   \
00589             "lsr    r3, r6, #16                 \n\t"   \
00590             "add    r5, r5, r3                  \n\t"   \
00591             "lsr    r3, r7, #16                 \n\t"   \
00592             "add    r5, r5, r3                  \n\t"   \
00593             "add    r4, r4, r2                  \n\t"   \
00594             "mov    r2, #0                      \n\t"   \
00595             "adc    r5, r2                      \n\t"   \
00596             "lsl    r3, r6, #16                 \n\t"   \
00597             "add    r4, r4, r3                  \n\t"   \
00598             "adc    r5, r2                      \n\t"   \
00599             "lsl    r3, r7, #16                 \n\t"   \
00600             "add    r4, r4, r3                  \n\t"   \
00601             "adc    r5, r2                      \n\t"   \
00602             "ldr    r3, [r1]                    \n\t"   \
00603             "add    r4, r4, r3                  \n\t"   \
00604             "adc    r2, r5                      \n\t"   \
00605             "stmia  r1!, {r4}                   \n\t"
00606 
00607 #define MULADDC_STOP                                    \
00608             "str    r2, %0                      \n\t"   \
00609             "str    r1, %1                      \n\t"   \
00610             "str    r0, %2                      \n\t"   \
00611          : "=m" (c),  "=m" (d), "=m" (s)        \
00612          : "m" (s), "m" (d), "m" (c), "m" (b)   \
00613          : "r0", "r1", "r2", "r3", "r4", "r5",  \
00614            "r6", "r7", "r8", "r9", "cc"         \
00615          );
00616 
00617 #else
00618 
00619 #define MULADDC_INIT                                    \
00620     asm(                                                \
00621             "ldr    r0, %3                      \n\t"   \
00622             "ldr    r1, %4                      \n\t"   \
00623             "ldr    r2, %5                      \n\t"   \
00624             "ldr    r3, %6                      \n\t"
00625 
00626 #define MULADDC_CORE                                    \
00627             "ldr    r4, [r0], #4                \n\t"   \
00628             "mov    r5, #0                      \n\t"   \
00629             "ldr    r6, [r1]                    \n\t"   \
00630             "umlal  r2, r5, r3, r4              \n\t"   \
00631             "adds   r7, r6, r2                  \n\t"   \
00632             "adc    r2, r5, #0                  \n\t"   \
00633             "str    r7, [r1], #4                \n\t"
00634 
00635 #define MULADDC_STOP                                    \
00636             "str    r2, %0                      \n\t"   \
00637             "str    r1, %1                      \n\t"   \
00638             "str    r0, %2                      \n\t"   \
00639          : "=m" (c),  "=m" (d), "=m" (s)        \
00640          : "m" (s), "m" (d), "m" (c), "m" (b)   \
00641          : "r0", "r1", "r2", "r3", "r4", "r5",  \
00642            "r6", "r7", "cc"                     \
00643          );
00644 
00645 #endif /* Thumb */
00646 
00647 #endif /* ARMv3 */
00648 
00649 #if defined(__alpha__)
00650 
00651 #define MULADDC_INIT                    \
00652     asm(                                \
00653         "ldq    $1, %3          \n\t"   \
00654         "ldq    $2, %4          \n\t"   \
00655         "ldq    $3, %5          \n\t"   \
00656         "ldq    $4, %6          \n\t"
00657 
00658 #define MULADDC_CORE                    \
00659         "ldq    $6,  0($1)      \n\t"   \
00660         "addq   $1,  8, $1      \n\t"   \
00661         "mulq   $6, $4, $7      \n\t"   \
00662         "umulh  $6, $4, $6      \n\t"   \
00663         "addq   $7, $3, $7      \n\t"   \
00664         "cmpult $7, $3, $3      \n\t"   \
00665         "ldq    $5,  0($2)      \n\t"   \
00666         "addq   $7, $5, $7      \n\t"   \
00667         "cmpult $7, $5, $5      \n\t"   \
00668         "stq    $7,  0($2)      \n\t"   \
00669         "addq   $2,  8, $2      \n\t"   \
00670         "addq   $6, $3, $3      \n\t"   \
00671         "addq   $5, $3, $3      \n\t"
00672 
00673 #define MULADDC_STOP                                    \
00674         "stq    $3, %0          \n\t"   \
00675         "stq    $2, %1          \n\t"   \
00676         "stq    $1, %2          \n\t"   \
00677         : "=m" (c), "=m" (d), "=m" (s)              \
00678         : "m" (s), "m" (d), "m" (c), "m" (b)        \
00679         : "$1", "$2", "$3", "$4", "$5", "$6", "$7"  \
00680     );
00681 #endif /* Alpha */
00682 
00683 #if defined(__mips__) && !defined(__mips64)
00684 
00685 #define MULADDC_INIT                    \
00686     asm(                                \
00687         "lw     $10, %3         \n\t"   \
00688         "lw     $11, %4         \n\t"   \
00689         "lw     $12, %5         \n\t"   \
00690         "lw     $13, %6         \n\t"
00691 
00692 #define MULADDC_CORE                    \
00693         "lw     $14, 0($10)     \n\t"   \
00694         "multu  $13, $14        \n\t"   \
00695         "addi   $10, $10, 4     \n\t"   \
00696         "mflo   $14             \n\t"   \
00697         "mfhi   $9              \n\t"   \
00698         "addu   $14, $12, $14   \n\t"   \
00699         "lw     $15, 0($11)     \n\t"   \
00700         "sltu   $12, $14, $12   \n\t"   \
00701         "addu   $15, $14, $15   \n\t"   \
00702         "sltu   $14, $15, $14   \n\t"   \
00703         "addu   $12, $12, $9    \n\t"   \
00704         "sw     $15, 0($11)     \n\t"   \
00705         "addu   $12, $12, $14   \n\t"   \
00706         "addi   $11, $11, 4     \n\t"
00707 
00708 #define MULADDC_STOP                    \
00709         "sw     $12, %0         \n\t"   \
00710         "sw     $11, %1         \n\t"   \
00711         "sw     $10, %2         \n\t"   \
00712         : "=m" (c), "=m" (d), "=m" (s)                      \
00713         : "m" (s), "m" (d), "m" (c), "m" (b)                \
00714         : "$9", "$10", "$11", "$12", "$13", "$14", "$15"    \
00715     );
00716 
00717 #endif /* MIPS */
00718 #endif /* GNUC */
00719 
00720 #if (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
00721 
00722 #define MULADDC_INIT                            \
00723     __asm   mov     esi, s                      \
00724     __asm   mov     edi, d                      \
00725     __asm   mov     ecx, c                      \
00726     __asm   mov     ebx, b
00727 
00728 #define MULADDC_CORE                            \
00729     __asm   lodsd                               \
00730     __asm   mul     ebx                         \
00731     __asm   add     eax, ecx                    \
00732     __asm   adc     edx, 0                      \
00733     __asm   add     eax, [edi]                  \
00734     __asm   adc     edx, 0                      \
00735     __asm   mov     ecx, edx                    \
00736     __asm   stosd
00737 
00738 #if defined(POLARSSL_HAVE_SSE2)
00739 
00740 #define EMIT __asm _emit
00741 
00742 #define MULADDC_HUIT                            \
00743     EMIT 0x0F  EMIT 0x6E  EMIT 0xC9             \
00744     EMIT 0x0F  EMIT 0x6E  EMIT 0xC3             \
00745     EMIT 0x0F  EMIT 0x6E  EMIT 0x1F             \
00746     EMIT 0x0F  EMIT 0xD4  EMIT 0xCB             \
00747     EMIT 0x0F  EMIT 0x6E  EMIT 0x16             \
00748     EMIT 0x0F  EMIT 0xF4  EMIT 0xD0             \
00749     EMIT 0x0F  EMIT 0x6E  EMIT 0x66  EMIT 0x04  \
00750     EMIT 0x0F  EMIT 0xF4  EMIT 0xE0             \
00751     EMIT 0x0F  EMIT 0x6E  EMIT 0x76  EMIT 0x08  \
00752     EMIT 0x0F  EMIT 0xF4  EMIT 0xF0             \
00753     EMIT 0x0F  EMIT 0x6E  EMIT 0x7E  EMIT 0x0C  \
00754     EMIT 0x0F  EMIT 0xF4  EMIT 0xF8             \
00755     EMIT 0x0F  EMIT 0xD4  EMIT 0xCA             \
00756     EMIT 0x0F  EMIT 0x6E  EMIT 0x5F  EMIT 0x04  \
00757     EMIT 0x0F  EMIT 0xD4  EMIT 0xDC             \
00758     EMIT 0x0F  EMIT 0x6E  EMIT 0x6F  EMIT 0x08  \
00759     EMIT 0x0F  EMIT 0xD4  EMIT 0xEE             \
00760     EMIT 0x0F  EMIT 0x6E  EMIT 0x67  EMIT 0x0C  \
00761     EMIT 0x0F  EMIT 0xD4  EMIT 0xFC             \
00762     EMIT 0x0F  EMIT 0x7E  EMIT 0x0F             \
00763     EMIT 0x0F  EMIT 0x6E  EMIT 0x56  EMIT 0x10  \
00764     EMIT 0x0F  EMIT 0xF4  EMIT 0xD0             \
00765     EMIT 0x0F  EMIT 0x73  EMIT 0xD1  EMIT 0x20  \
00766     EMIT 0x0F  EMIT 0x6E  EMIT 0x66  EMIT 0x14  \
00767     EMIT 0x0F  EMIT 0xF4  EMIT 0xE0             \
00768     EMIT 0x0F  EMIT 0xD4  EMIT 0xCB             \
00769     EMIT 0x0F  EMIT 0x6E  EMIT 0x76  EMIT 0x18  \
00770     EMIT 0x0F  EMIT 0xF4  EMIT 0xF0             \
00771     EMIT 0x0F  EMIT 0x7E  EMIT 0x4F  EMIT 0x04  \
00772     EMIT 0x0F  EMIT 0x73  EMIT 0xD1  EMIT 0x20  \
00773     EMIT 0x0F  EMIT 0x6E  EMIT 0x5E  EMIT 0x1C  \
00774     EMIT 0x0F  EMIT 0xF4  EMIT 0xD8             \
00775     EMIT 0x0F  EMIT 0xD4  EMIT 0xCD             \
00776     EMIT 0x0F  EMIT 0x6E  EMIT 0x6F  EMIT 0x10  \
00777     EMIT 0x0F  EMIT 0xD4  EMIT 0xD5             \
00778     EMIT 0x0F  EMIT 0x7E  EMIT 0x4F  EMIT 0x08  \
00779     EMIT 0x0F  EMIT 0x73  EMIT 0xD1  EMIT 0x20  \
00780     EMIT 0x0F  EMIT 0xD4  EMIT 0xCF             \
00781     EMIT 0x0F  EMIT 0x6E  EMIT 0x6F  EMIT 0x14  \
00782     EMIT 0x0F  EMIT 0xD4  EMIT 0xE5             \
00783     EMIT 0x0F  EMIT 0x7E  EMIT 0x4F  EMIT 0x0C  \
00784     EMIT 0x0F  EMIT 0x73  EMIT 0xD1  EMIT 0x20  \
00785     EMIT 0x0F  EMIT 0xD4  EMIT 0xCA             \
00786     EMIT 0x0F  EMIT 0x6E  EMIT 0x6F  EMIT 0x18  \
00787     EMIT 0x0F  EMIT 0xD4  EMIT 0xF5             \
00788     EMIT 0x0F  EMIT 0x7E  EMIT 0x4F  EMIT 0x10  \
00789     EMIT 0x0F  EMIT 0x73  EMIT 0xD1  EMIT 0x20  \
00790     EMIT 0x0F  EMIT 0xD4  EMIT 0xCC             \
00791     EMIT 0x0F  EMIT 0x6E  EMIT 0x6F  EMIT 0x1C  \
00792     EMIT 0x0F  EMIT 0xD4  EMIT 0xDD             \
00793     EMIT 0x0F  EMIT 0x7E  EMIT 0x4F  EMIT 0x14  \
00794     EMIT 0x0F  EMIT 0x73  EMIT 0xD1  EMIT 0x20  \
00795     EMIT 0x0F  EMIT 0xD4  EMIT 0xCE             \
00796     EMIT 0x0F  EMIT 0x7E  EMIT 0x4F  EMIT 0x18  \
00797     EMIT 0x0F  EMIT 0x73  EMIT 0xD1  EMIT 0x20  \
00798     EMIT 0x0F  EMIT 0xD4  EMIT 0xCB             \
00799     EMIT 0x0F  EMIT 0x7E  EMIT 0x4F  EMIT 0x1C  \
00800     EMIT 0x83  EMIT 0xC7  EMIT 0x20             \
00801     EMIT 0x83  EMIT 0xC6  EMIT 0x20             \
00802     EMIT 0x0F  EMIT 0x73  EMIT 0xD1  EMIT 0x20  \
00803     EMIT 0x0F  EMIT 0x7E  EMIT 0xC9
00804 
00805 #define MULADDC_STOP                            \
00806     EMIT 0x0F  EMIT 0x77                        \
00807     __asm   mov     c, ecx                      \
00808     __asm   mov     d, edi                      \
00809     __asm   mov     s, esi                      \
00810 
00811 #else
00812 
00813 #define MULADDC_STOP                            \
00814     __asm   mov     c, ecx                      \
00815     __asm   mov     d, edi                      \
00816     __asm   mov     s, esi                      \
00817 
00818 #endif /* SSE2 */
00819 #endif /* MSVC */
00820 
00821 #endif /* POLARSSL_HAVE_ASM */
00822 
00823 #if !defined(MULADDC_CORE)
00824 #if defined(POLARSSL_HAVE_UDBL)
00825 
00826 #define MULADDC_INIT                    \
00827 {                                       \
00828     t_udbl r;                           \
00829     t_uint r0, r1;
00830 
00831 #define MULADDC_CORE                    \
00832     r   = *(s++) * (t_udbl) b;          \
00833     r0  = (t_uint) r;                   \
00834     r1  = (t_uint)( r >> biL );         \
00835     r0 += c;  r1 += (r0 <  c);          \
00836     r0 += *d; r1 += (r0 < *d);          \
00837     c = r1; *(d++) = r0;
00838 
00839 #define MULADDC_STOP                    \
00840 }
00841 
00842 #else
00843 #define MULADDC_INIT                    \
00844 {                                       \
00845     t_uint s0, s1, b0, b1;              \
00846     t_uint r0, r1, rx, ry;              \
00847     b0 = ( b << biH ) >> biH;           \
00848     b1 = ( b >> biH );
00849 
00850 #define MULADDC_CORE                    \
00851     s0 = ( *s << biH ) >> biH;          \
00852     s1 = ( *s >> biH ); s++;            \
00853     rx = s0 * b1; r0 = s0 * b0;         \
00854     ry = s1 * b0; r1 = s1 * b1;         \
00855     r1 += ( rx >> biH );                \
00856     r1 += ( ry >> biH );                \
00857     rx <<= biH; ry <<= biH;             \
00858     r0 += rx; r1 += (r0 < rx);          \
00859     r0 += ry; r1 += (r0 < ry);          \
00860     r0 +=  c; r1 += (r0 <  c);          \
00861     r0 += *d; r1 += (r0 < *d);          \
00862     c = r1; *(d++) = r0;
00863 
00864 #define MULADDC_STOP                    \
00865 }
00866 
00867 #endif /* C (generic)  */
00868 #endif /* C (longlong) */
00869 
00870 #endif /* bn_mul.h */
00871