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