]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/bn/asm/x86_64-gcc.c
2 #if !(defined(__GNUC__) && __GNUC__>=2)
3 # include "../bn_asm.c" /* kind of dirty hack for Sun Studio */
6 * x86_64 BIGNUM accelerator version 0.1, December 2002.
8 * Implemented by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL
11 * Rights for redistribution and usage in source and binary forms are
12 * granted according to the OpenSSL license. Warranty of any kind is
15 * Q. Version 0.1? It doesn't sound like Andy, he used to assign real
16 * versions, like 1.0...
17 * A. Well, that's because this code is basically a quick-n-dirty
18 * proof-of-concept hack. As you can see it's implemented with
19 * inline assembler, which means that you're bound to GCC and that
20 * there might be enough room for further improvement.
22 * Q. Why inline assembler?
23 * A. x86_64 features own ABI which I'm not familiar with. This is
24 * why I decided to let the compiler take care of subroutine
25 * prologue/epilogue as well as register allocation. For reference.
26 * Win64 implements different ABI for AMD64, different from Linux.
28 * Q. How much faster does it get?
29 * A. 'apps/openssl speed rsa dsa' output with no-asm:
31 * sign verify sign/s verify/s
32 * rsa 512 bits 0.0006s 0.0001s 1683.8 18456.2
33 * rsa 1024 bits 0.0028s 0.0002s 356.0 6407.0
34 * rsa 2048 bits 0.0172s 0.0005s 58.0 1957.8
35 * rsa 4096 bits 0.1155s 0.0018s 8.7 555.6
36 * sign verify sign/s verify/s
37 * dsa 512 bits 0.0005s 0.0006s 2100.8 1768.3
38 * dsa 1024 bits 0.0014s 0.0018s 692.3 559.2
39 * dsa 2048 bits 0.0049s 0.0061s 204.7 165.0
41 * 'apps/openssl speed rsa dsa' output with this module:
43 * sign verify sign/s verify/s
44 * rsa 512 bits 0.0004s 0.0000s 2767.1 33297.9
45 * rsa 1024 bits 0.0012s 0.0001s 867.4 14674.7
46 * rsa 2048 bits 0.0061s 0.0002s 164.0 5270.0
47 * rsa 4096 bits 0.0384s 0.0006s 26.1 1650.8
48 * sign verify sign/s verify/s
49 * dsa 512 bits 0.0002s 0.0003s 4442.2 3786.3
50 * dsa 1024 bits 0.0005s 0.0007s 1835.1 1497.4
51 * dsa 2048 bits 0.0016s 0.0020s 620.4 504.6
53 * For the reference. IA-32 assembler implementation performs
54 * very much like 64-bit code compiled with no-asm on the same
58 # if defined(_WIN64) || !defined(__LP64__)
59 # define BN_ULONG unsigned long long
61 # define BN_ULONG unsigned long
68 * "m"(a), "+m"(r) is the way to favor DirectPath ยต-code;
69 * "g"(0) let the compiler to decide where does it
70 * want to keep the value of zero;
72 # define mul_add(r,a,word,carry) do { \
73 register BN_ULONG high,low; \
75 : "=a"(low),"=d"(high) \
78 asm ("addq %2,%0; adcq %3,%1" \
79 : "+r"(carry),"+d"(high)\
82 asm ("addq %2,%0; adcq %3,%1" \
83 : "+m"(r),"+d"(high) \
89 # define mul(r,a,word,carry) do { \
90 register BN_ULONG high,low; \
92 : "=a"(low),"=d"(high) \
95 asm ("addq %2,%0; adcq %3,%1" \
96 : "+r"(carry),"+d"(high)\
99 (r)=carry, carry=high; \
102 # define sqr(r0,r1,a) \
104 : "=a"(r0),"=d"(r1) \
108 BN_ULONG
bn_mul_add_words(BN_ULONG
*rp
, const BN_ULONG
*ap
, int num
,
117 mul_add(rp
[0], ap
[0], w
, c1
);
118 mul_add(rp
[1], ap
[1], w
, c1
);
119 mul_add(rp
[2], ap
[2], w
, c1
);
120 mul_add(rp
[3], ap
[3], w
, c1
);
126 mul_add(rp
[0], ap
[0], w
, c1
);
129 mul_add(rp
[1], ap
[1], w
, c1
);
132 mul_add(rp
[2], ap
[2], w
, c1
);
139 BN_ULONG
bn_mul_words(BN_ULONG
*rp
, const BN_ULONG
*ap
, int num
, BN_ULONG w
)
147 mul(rp
[0], ap
[0], w
, c1
);
148 mul(rp
[1], ap
[1], w
, c1
);
149 mul(rp
[2], ap
[2], w
, c1
);
150 mul(rp
[3], ap
[3], w
, c1
);
156 mul(rp
[0], ap
[0], w
, c1
);
159 mul(rp
[1], ap
[1], w
, c1
);
162 mul(rp
[2], ap
[2], w
, c1
);
167 void bn_sqr_words(BN_ULONG
*r
, const BN_ULONG
*a
, int n
)
173 sqr(r
[0], r
[1], a
[0]);
174 sqr(r
[2], r
[3], a
[1]);
175 sqr(r
[4], r
[5], a
[2]);
176 sqr(r
[6], r
[7], a
[3]);
182 sqr(r
[0], r
[1], a
[0]);
185 sqr(r
[2], r
[3], a
[1]);
188 sqr(r
[4], r
[5], a
[2]);
192 BN_ULONG
bn_div_words(BN_ULONG h
, BN_ULONG l
, BN_ULONG d
)
196 asm("divq %4":"=a"(ret
), "=d"(waste
)
197 : "a"(l
), "d"(h
), "g"(d
)
203 BN_ULONG
bn_add_words(BN_ULONG
*rp
, const BN_ULONG
*ap
, const BN_ULONG
*bp
,
212 asm volatile (" subq %0,%0 \n" /* clear carry */
215 "1: movq (%4,%2,8),%0 \n"
216 " adcq (%5,%2,8),%0 \n"
217 " movq %0,(%3,%2,8) \n"
220 " sbbq %0,%0 \n":"=&r" (ret
), "+c"(n
),
222 :"r"(rp
), "r"(ap
), "r"(bp
)
229 BN_ULONG
bn_sub_words(BN_ULONG
*rp
, const BN_ULONG
*ap
, const BN_ULONG
*bp
,
238 asm volatile (" subq %0,%0 \n" /* clear borrow */
241 "1: movq (%4,%2,8),%0 \n"
242 " sbbq (%5,%2,8),%0 \n"
243 " movq %0,(%3,%2,8) \n"
246 " sbbq %0,%0 \n":"=&r" (ret
), "+c"(n
),
248 :"r"(rp
), "r"(ap
), "r"(bp
)
254 /* Simics 1.4<7 has buggy sbbq:-( */
255 # define BN_MASK2 0xffffffffffffffffL
256 BN_ULONG
bn_sub_words(BN_ULONG
*r
, BN_ULONG
*a
, BN_ULONG
*b
, int n
)
262 return ((BN_ULONG
)0);
267 r
[0] = (t1
- t2
- c
) & BN_MASK2
;
275 r
[1] = (t1
- t2
- c
) & BN_MASK2
;
283 r
[2] = (t1
- t2
- c
) & BN_MASK2
;
291 r
[3] = (t1
- t2
- c
) & BN_MASK2
;
305 /* mul_add_c(a,b,c0,c1,c2) -- c+=a*b for three word number c=(c2,c1,c0) */
306 /* mul_add_c2(a,b,c0,c1,c2) -- c+=2*a*b for three word number c=(c2,c1,c0) */
307 /* sqr_add_c(a,i,c0,c1,c2) -- c+=a[i]^2 for three word number c=(c2,c1,c0) */
309 * sqr_add_c2(a,i,c0,c1,c2) -- c+=2*a[i]*a[j] for three word number
314 * Keep in mind that carrying into high part of multiplication result
315 * can not overflow, because it cannot be all-ones.
318 /* original macros are kept for reference purposes */
319 # define mul_add_c(a,b,c0,c1,c2) do { \
320 BN_ULONG ta = (a), tb = (b); \
322 BN_UMULT_LOHI(lo,hi,ta,tb); \
323 c0 += lo; hi += (c0<lo)?1:0; \
324 c1 += hi; c2 += (c1<hi)?1:0; \
327 # define mul_add_c2(a,b,c0,c1,c2) do { \
328 BN_ULONG ta = (a), tb = (b); \
329 BN_ULONG lo, hi, tt; \
330 BN_UMULT_LOHI(lo,hi,ta,tb); \
331 c0 += lo; tt = hi+((c0<lo)?1:0); \
332 c1 += tt; c2 += (c1<tt)?1:0; \
333 c0 += lo; hi += (c0<lo)?1:0; \
334 c1 += hi; c2 += (c1<hi)?1:0; \
337 # define sqr_add_c(a,i,c0,c1,c2) do { \
338 BN_ULONG ta = (a)[i]; \
340 BN_UMULT_LOHI(lo,hi,ta,ta); \
341 c0 += lo; hi += (c0<lo)?1:0; \
342 c1 += hi; c2 += (c1<hi)?1:0; \
345 # define mul_add_c(a,b,c0,c1,c2) do { \
348 : "=a"(t1),"=d"(t2) \
351 asm ("addq %3,%0; adcq %4,%1; adcq %5,%2" \
352 : "+r"(c0),"+r"(c1),"+r"(c2) \
353 : "r"(t1),"r"(t2),"g"(0) \
357 # define sqr_add_c(a,i,c0,c1,c2) do { \
360 : "=a"(t1),"=d"(t2) \
363 asm ("addq %3,%0; adcq %4,%1; adcq %5,%2" \
364 : "+r"(c0),"+r"(c1),"+r"(c2) \
365 : "r"(t1),"r"(t2),"g"(0) \
369 # define mul_add_c2(a,b,c0,c1,c2) do { \
372 : "=a"(t1),"=d"(t2) \
375 asm ("addq %3,%0; adcq %4,%1; adcq %5,%2" \
376 : "+r"(c0),"+r"(c1),"+r"(c2) \
377 : "r"(t1),"r"(t2),"g"(0) \
379 asm ("addq %3,%0; adcq %4,%1; adcq %5,%2" \
380 : "+r"(c0),"+r"(c1),"+r"(c2) \
381 : "r"(t1),"r"(t2),"g"(0) \
386 # define sqr_add_c2(a,i,j,c0,c1,c2) \
387 mul_add_c2((a)[i],(a)[j],c0,c1,c2)
389 void bn_mul_comba8(BN_ULONG
*r
, BN_ULONG
*a
, BN_ULONG
*b
)
396 mul_add_c(a
[0], b
[0], c1
, c2
, c3
);
399 mul_add_c(a
[0], b
[1], c2
, c3
, c1
);
400 mul_add_c(a
[1], b
[0], c2
, c3
, c1
);
403 mul_add_c(a
[2], b
[0], c3
, c1
, c2
);
404 mul_add_c(a
[1], b
[1], c3
, c1
, c2
);
405 mul_add_c(a
[0], b
[2], c3
, c1
, c2
);
408 mul_add_c(a
[0], b
[3], c1
, c2
, c3
);
409 mul_add_c(a
[1], b
[2], c1
, c2
, c3
);
410 mul_add_c(a
[2], b
[1], c1
, c2
, c3
);
411 mul_add_c(a
[3], b
[0], c1
, c2
, c3
);
414 mul_add_c(a
[4], b
[0], c2
, c3
, c1
);
415 mul_add_c(a
[3], b
[1], c2
, c3
, c1
);
416 mul_add_c(a
[2], b
[2], c2
, c3
, c1
);
417 mul_add_c(a
[1], b
[3], c2
, c3
, c1
);
418 mul_add_c(a
[0], b
[4], c2
, c3
, c1
);
421 mul_add_c(a
[0], b
[5], c3
, c1
, c2
);
422 mul_add_c(a
[1], b
[4], c3
, c1
, c2
);
423 mul_add_c(a
[2], b
[3], c3
, c1
, c2
);
424 mul_add_c(a
[3], b
[2], c3
, c1
, c2
);
425 mul_add_c(a
[4], b
[1], c3
, c1
, c2
);
426 mul_add_c(a
[5], b
[0], c3
, c1
, c2
);
429 mul_add_c(a
[6], b
[0], c1
, c2
, c3
);
430 mul_add_c(a
[5], b
[1], c1
, c2
, c3
);
431 mul_add_c(a
[4], b
[2], c1
, c2
, c3
);
432 mul_add_c(a
[3], b
[3], c1
, c2
, c3
);
433 mul_add_c(a
[2], b
[4], c1
, c2
, c3
);
434 mul_add_c(a
[1], b
[5], c1
, c2
, c3
);
435 mul_add_c(a
[0], b
[6], c1
, c2
, c3
);
438 mul_add_c(a
[0], b
[7], c2
, c3
, c1
);
439 mul_add_c(a
[1], b
[6], c2
, c3
, c1
);
440 mul_add_c(a
[2], b
[5], c2
, c3
, c1
);
441 mul_add_c(a
[3], b
[4], c2
, c3
, c1
);
442 mul_add_c(a
[4], b
[3], c2
, c3
, c1
);
443 mul_add_c(a
[5], b
[2], c2
, c3
, c1
);
444 mul_add_c(a
[6], b
[1], c2
, c3
, c1
);
445 mul_add_c(a
[7], b
[0], c2
, c3
, c1
);
448 mul_add_c(a
[7], b
[1], c3
, c1
, c2
);
449 mul_add_c(a
[6], b
[2], c3
, c1
, c2
);
450 mul_add_c(a
[5], b
[3], c3
, c1
, c2
);
451 mul_add_c(a
[4], b
[4], c3
, c1
, c2
);
452 mul_add_c(a
[3], b
[5], c3
, c1
, c2
);
453 mul_add_c(a
[2], b
[6], c3
, c1
, c2
);
454 mul_add_c(a
[1], b
[7], c3
, c1
, c2
);
457 mul_add_c(a
[2], b
[7], c1
, c2
, c3
);
458 mul_add_c(a
[3], b
[6], c1
, c2
, c3
);
459 mul_add_c(a
[4], b
[5], c1
, c2
, c3
);
460 mul_add_c(a
[5], b
[4], c1
, c2
, c3
);
461 mul_add_c(a
[6], b
[3], c1
, c2
, c3
);
462 mul_add_c(a
[7], b
[2], c1
, c2
, c3
);
465 mul_add_c(a
[7], b
[3], c2
, c3
, c1
);
466 mul_add_c(a
[6], b
[4], c2
, c3
, c1
);
467 mul_add_c(a
[5], b
[5], c2
, c3
, c1
);
468 mul_add_c(a
[4], b
[6], c2
, c3
, c1
);
469 mul_add_c(a
[3], b
[7], c2
, c3
, c1
);
472 mul_add_c(a
[4], b
[7], c3
, c1
, c2
);
473 mul_add_c(a
[5], b
[6], c3
, c1
, c2
);
474 mul_add_c(a
[6], b
[5], c3
, c1
, c2
);
475 mul_add_c(a
[7], b
[4], c3
, c1
, c2
);
478 mul_add_c(a
[7], b
[5], c1
, c2
, c3
);
479 mul_add_c(a
[6], b
[6], c1
, c2
, c3
);
480 mul_add_c(a
[5], b
[7], c1
, c2
, c3
);
483 mul_add_c(a
[6], b
[7], c2
, c3
, c1
);
484 mul_add_c(a
[7], b
[6], c2
, c3
, c1
);
487 mul_add_c(a
[7], b
[7], c3
, c1
, c2
);
492 void bn_mul_comba4(BN_ULONG
*r
, BN_ULONG
*a
, BN_ULONG
*b
)
499 mul_add_c(a
[0], b
[0], c1
, c2
, c3
);
502 mul_add_c(a
[0], b
[1], c2
, c3
, c1
);
503 mul_add_c(a
[1], b
[0], c2
, c3
, c1
);
506 mul_add_c(a
[2], b
[0], c3
, c1
, c2
);
507 mul_add_c(a
[1], b
[1], c3
, c1
, c2
);
508 mul_add_c(a
[0], b
[2], c3
, c1
, c2
);
511 mul_add_c(a
[0], b
[3], c1
, c2
, c3
);
512 mul_add_c(a
[1], b
[2], c1
, c2
, c3
);
513 mul_add_c(a
[2], b
[1], c1
, c2
, c3
);
514 mul_add_c(a
[3], b
[0], c1
, c2
, c3
);
517 mul_add_c(a
[3], b
[1], c2
, c3
, c1
);
518 mul_add_c(a
[2], b
[2], c2
, c3
, c1
);
519 mul_add_c(a
[1], b
[3], c2
, c3
, c1
);
522 mul_add_c(a
[2], b
[3], c3
, c1
, c2
);
523 mul_add_c(a
[3], b
[2], c3
, c1
, c2
);
526 mul_add_c(a
[3], b
[3], c1
, c2
, c3
);
531 void bn_sqr_comba8(BN_ULONG
*r
, const BN_ULONG
*a
)
538 sqr_add_c(a
, 0, c1
, c2
, c3
);
541 sqr_add_c2(a
, 1, 0, c2
, c3
, c1
);
544 sqr_add_c(a
, 1, c3
, c1
, c2
);
545 sqr_add_c2(a
, 2, 0, c3
, c1
, c2
);
548 sqr_add_c2(a
, 3, 0, c1
, c2
, c3
);
549 sqr_add_c2(a
, 2, 1, c1
, c2
, c3
);
552 sqr_add_c(a
, 2, c2
, c3
, c1
);
553 sqr_add_c2(a
, 3, 1, c2
, c3
, c1
);
554 sqr_add_c2(a
, 4, 0, c2
, c3
, c1
);
557 sqr_add_c2(a
, 5, 0, c3
, c1
, c2
);
558 sqr_add_c2(a
, 4, 1, c3
, c1
, c2
);
559 sqr_add_c2(a
, 3, 2, c3
, c1
, c2
);
562 sqr_add_c(a
, 3, c1
, c2
, c3
);
563 sqr_add_c2(a
, 4, 2, c1
, c2
, c3
);
564 sqr_add_c2(a
, 5, 1, c1
, c2
, c3
);
565 sqr_add_c2(a
, 6, 0, c1
, c2
, c3
);
568 sqr_add_c2(a
, 7, 0, c2
, c3
, c1
);
569 sqr_add_c2(a
, 6, 1, c2
, c3
, c1
);
570 sqr_add_c2(a
, 5, 2, c2
, c3
, c1
);
571 sqr_add_c2(a
, 4, 3, c2
, c3
, c1
);
574 sqr_add_c(a
, 4, c3
, c1
, c2
);
575 sqr_add_c2(a
, 5, 3, c3
, c1
, c2
);
576 sqr_add_c2(a
, 6, 2, c3
, c1
, c2
);
577 sqr_add_c2(a
, 7, 1, c3
, c1
, c2
);
580 sqr_add_c2(a
, 7, 2, c1
, c2
, c3
);
581 sqr_add_c2(a
, 6, 3, c1
, c2
, c3
);
582 sqr_add_c2(a
, 5, 4, c1
, c2
, c3
);
585 sqr_add_c(a
, 5, c2
, c3
, c1
);
586 sqr_add_c2(a
, 6, 4, c2
, c3
, c1
);
587 sqr_add_c2(a
, 7, 3, c2
, c3
, c1
);
590 sqr_add_c2(a
, 7, 4, c3
, c1
, c2
);
591 sqr_add_c2(a
, 6, 5, c3
, c1
, c2
);
594 sqr_add_c(a
, 6, c1
, c2
, c3
);
595 sqr_add_c2(a
, 7, 5, c1
, c2
, c3
);
598 sqr_add_c2(a
, 7, 6, c2
, c3
, c1
);
601 sqr_add_c(a
, 7, c3
, c1
, c2
);
606 void bn_sqr_comba4(BN_ULONG
*r
, const BN_ULONG
*a
)
613 sqr_add_c(a
, 0, c1
, c2
, c3
);
616 sqr_add_c2(a
, 1, 0, c2
, c3
, c1
);
619 sqr_add_c(a
, 1, c3
, c1
, c2
);
620 sqr_add_c2(a
, 2, 0, c3
, c1
, c2
);
623 sqr_add_c2(a
, 3, 0, c1
, c2
, c3
);
624 sqr_add_c2(a
, 2, 1, c1
, c2
, c3
);
627 sqr_add_c(a
, 2, c2
, c3
, c1
);
628 sqr_add_c2(a
, 3, 1, c2
, c3
, c1
);
631 sqr_add_c2(a
, 3, 2, c3
, c1
, c2
);
634 sqr_add_c(a
, 3, c1
, c2
, c3
);