xref: /onnv-gate/usr/src/common/openssl/crypto/bn/asm/x86_64-gcc.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * x86_64 BIGNUM accelerator version 0.1, December 2002.
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * Implemented by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL
50Sstevel@tonic-gate  * project.
60Sstevel@tonic-gate  *
70Sstevel@tonic-gate  * Rights for redistribution and usage in source and binary forms are
80Sstevel@tonic-gate  * granted according to the OpenSSL license. Warranty of any kind is
90Sstevel@tonic-gate  * disclaimed.
100Sstevel@tonic-gate  *
110Sstevel@tonic-gate  * Q. Version 0.1? It doesn't sound like Andy, he used to assign real
120Sstevel@tonic-gate  *    versions, like 1.0...
130Sstevel@tonic-gate  * A. Well, that's because this code is basically a quick-n-dirty
140Sstevel@tonic-gate  *    proof-of-concept hack. As you can see it's implemented with
150Sstevel@tonic-gate  *    inline assembler, which means that you're bound to GCC and that
16*2139Sjp161948  *    there might be enough room for further improvement.
170Sstevel@tonic-gate  *
180Sstevel@tonic-gate  * Q. Why inline assembler?
19*2139Sjp161948  * A. x86_64 features own ABI which I'm not familiar with. This is
20*2139Sjp161948  *    why I decided to let the compiler take care of subroutine
21*2139Sjp161948  *    prologue/epilogue as well as register allocation. For reference.
22*2139Sjp161948  *    Win64 implements different ABI for AMD64, different from Linux.
230Sstevel@tonic-gate  *
240Sstevel@tonic-gate  * Q. How much faster does it get?
25*2139Sjp161948  * A. 'apps/openssl speed rsa dsa' output with no-asm:
26*2139Sjp161948  *
27*2139Sjp161948  *	                  sign    verify    sign/s verify/s
28*2139Sjp161948  *	rsa  512 bits   0.0006s   0.0001s   1683.8  18456.2
29*2139Sjp161948  *	rsa 1024 bits   0.0028s   0.0002s    356.0   6407.0
30*2139Sjp161948  *	rsa 2048 bits   0.0172s   0.0005s     58.0   1957.8
31*2139Sjp161948  *	rsa 4096 bits   0.1155s   0.0018s      8.7    555.6
32*2139Sjp161948  *	                  sign    verify    sign/s verify/s
33*2139Sjp161948  *	dsa  512 bits   0.0005s   0.0006s   2100.8   1768.3
34*2139Sjp161948  *	dsa 1024 bits   0.0014s   0.0018s    692.3    559.2
35*2139Sjp161948  *	dsa 2048 bits   0.0049s   0.0061s    204.7    165.0
36*2139Sjp161948  *
37*2139Sjp161948  *    'apps/openssl speed rsa dsa' output with this module:
38*2139Sjp161948  *
39*2139Sjp161948  *	                  sign    verify    sign/s verify/s
40*2139Sjp161948  *	rsa  512 bits   0.0004s   0.0000s   2767.1  33297.9
41*2139Sjp161948  *	rsa 1024 bits   0.0012s   0.0001s    867.4  14674.7
42*2139Sjp161948  *	rsa 2048 bits   0.0061s   0.0002s    164.0   5270.0
43*2139Sjp161948  *	rsa 4096 bits   0.0384s   0.0006s     26.1   1650.8
44*2139Sjp161948  *	                  sign    verify    sign/s verify/s
45*2139Sjp161948  *	dsa  512 bits   0.0002s   0.0003s   4442.2   3786.3
46*2139Sjp161948  *	dsa 1024 bits   0.0005s   0.0007s   1835.1   1497.4
47*2139Sjp161948  *	dsa 2048 bits   0.0016s   0.0020s    620.4    504.6
48*2139Sjp161948  *
49*2139Sjp161948  *    For the reference. IA-32 assembler implementation performs
50*2139Sjp161948  *    very much like 64-bit code compiled with no-asm on the same
51*2139Sjp161948  *    machine.
520Sstevel@tonic-gate  */
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #define BN_ULONG unsigned long
550Sstevel@tonic-gate 
560Sstevel@tonic-gate /*
570Sstevel@tonic-gate  * "m"(a), "+m"(r)	is the way to favor DirectPath �-code;
580Sstevel@tonic-gate  * "g"(0)		let the compiler to decide where does it
590Sstevel@tonic-gate  *			want to keep the value of zero;
600Sstevel@tonic-gate  */
610Sstevel@tonic-gate #define mul_add(r,a,word,carry) do {	\
620Sstevel@tonic-gate 	register BN_ULONG high,low;	\
630Sstevel@tonic-gate 	asm ("mulq %3"			\
640Sstevel@tonic-gate 		: "=a"(low),"=d"(high)	\
650Sstevel@tonic-gate 		: "a"(word),"m"(a)	\
660Sstevel@tonic-gate 		: "cc");		\
670Sstevel@tonic-gate 	asm ("addq %2,%0; adcq %3,%1"	\
680Sstevel@tonic-gate 		: "+r"(carry),"+d"(high)\
690Sstevel@tonic-gate 		: "a"(low),"g"(0)	\
700Sstevel@tonic-gate 		: "cc");		\
710Sstevel@tonic-gate 	asm ("addq %2,%0; adcq %3,%1"	\
720Sstevel@tonic-gate 		: "+m"(r),"+d"(high)	\
730Sstevel@tonic-gate 		: "r"(carry),"g"(0)	\
740Sstevel@tonic-gate 		: "cc");		\
750Sstevel@tonic-gate 	carry=high;			\
760Sstevel@tonic-gate 	} while (0)
770Sstevel@tonic-gate 
780Sstevel@tonic-gate #define mul(r,a,word,carry) do {	\
790Sstevel@tonic-gate 	register BN_ULONG high,low;	\
800Sstevel@tonic-gate 	asm ("mulq %3"			\
810Sstevel@tonic-gate 		: "=a"(low),"=d"(high)	\
820Sstevel@tonic-gate 		: "a"(word),"g"(a)	\
830Sstevel@tonic-gate 		: "cc");		\
840Sstevel@tonic-gate 	asm ("addq %2,%0; adcq %3,%1"	\
850Sstevel@tonic-gate 		: "+r"(carry),"+d"(high)\
860Sstevel@tonic-gate 		: "a"(low),"g"(0)	\
870Sstevel@tonic-gate 		: "cc");		\
880Sstevel@tonic-gate 	(r)=carry, carry=high;		\
890Sstevel@tonic-gate 	} while (0)
900Sstevel@tonic-gate 
910Sstevel@tonic-gate #define sqr(r0,r1,a)			\
920Sstevel@tonic-gate 	asm ("mulq %2"			\
930Sstevel@tonic-gate 		: "=a"(r0),"=d"(r1)	\
940Sstevel@tonic-gate 		: "a"(a)		\
950Sstevel@tonic-gate 		: "cc");
960Sstevel@tonic-gate 
bn_mul_add_words(BN_ULONG * rp,BN_ULONG * ap,int num,BN_ULONG w)970Sstevel@tonic-gate BN_ULONG bn_mul_add_words(BN_ULONG *rp, BN_ULONG *ap, int num, BN_ULONG w)
980Sstevel@tonic-gate 	{
990Sstevel@tonic-gate 	BN_ULONG c1=0;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	if (num <= 0) return(c1);
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	while (num&~3)
1040Sstevel@tonic-gate 		{
1050Sstevel@tonic-gate 		mul_add(rp[0],ap[0],w,c1);
1060Sstevel@tonic-gate 		mul_add(rp[1],ap[1],w,c1);
1070Sstevel@tonic-gate 		mul_add(rp[2],ap[2],w,c1);
1080Sstevel@tonic-gate 		mul_add(rp[3],ap[3],w,c1);
1090Sstevel@tonic-gate 		ap+=4; rp+=4; num-=4;
1100Sstevel@tonic-gate 		}
1110Sstevel@tonic-gate 	if (num)
1120Sstevel@tonic-gate 		{
1130Sstevel@tonic-gate 		mul_add(rp[0],ap[0],w,c1); if (--num==0) return c1;
1140Sstevel@tonic-gate 		mul_add(rp[1],ap[1],w,c1); if (--num==0) return c1;
1150Sstevel@tonic-gate 		mul_add(rp[2],ap[2],w,c1); return c1;
1160Sstevel@tonic-gate 		}
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	return(c1);
1190Sstevel@tonic-gate 	}
1200Sstevel@tonic-gate 
bn_mul_words(BN_ULONG * rp,BN_ULONG * ap,int num,BN_ULONG w)1210Sstevel@tonic-gate BN_ULONG bn_mul_words(BN_ULONG *rp, BN_ULONG *ap, int num, BN_ULONG w)
1220Sstevel@tonic-gate 	{
1230Sstevel@tonic-gate 	BN_ULONG c1=0;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	if (num <= 0) return(c1);
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	while (num&~3)
1280Sstevel@tonic-gate 		{
1290Sstevel@tonic-gate 		mul(rp[0],ap[0],w,c1);
1300Sstevel@tonic-gate 		mul(rp[1],ap[1],w,c1);
1310Sstevel@tonic-gate 		mul(rp[2],ap[2],w,c1);
1320Sstevel@tonic-gate 		mul(rp[3],ap[3],w,c1);
1330Sstevel@tonic-gate 		ap+=4; rp+=4; num-=4;
1340Sstevel@tonic-gate 		}
1350Sstevel@tonic-gate 	if (num)
1360Sstevel@tonic-gate 		{
1370Sstevel@tonic-gate 		mul(rp[0],ap[0],w,c1); if (--num == 0) return c1;
1380Sstevel@tonic-gate 		mul(rp[1],ap[1],w,c1); if (--num == 0) return c1;
1390Sstevel@tonic-gate 		mul(rp[2],ap[2],w,c1);
1400Sstevel@tonic-gate 		}
1410Sstevel@tonic-gate 	return(c1);
1420Sstevel@tonic-gate 	}
1430Sstevel@tonic-gate 
bn_sqr_words(BN_ULONG * r,BN_ULONG * a,int n)1440Sstevel@tonic-gate void bn_sqr_words(BN_ULONG *r, BN_ULONG *a, int n)
1450Sstevel@tonic-gate         {
1460Sstevel@tonic-gate 	if (n <= 0) return;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	while (n&~3)
1490Sstevel@tonic-gate 		{
1500Sstevel@tonic-gate 		sqr(r[0],r[1],a[0]);
1510Sstevel@tonic-gate 		sqr(r[2],r[3],a[1]);
1520Sstevel@tonic-gate 		sqr(r[4],r[5],a[2]);
1530Sstevel@tonic-gate 		sqr(r[6],r[7],a[3]);
1540Sstevel@tonic-gate 		a+=4; r+=8; n-=4;
1550Sstevel@tonic-gate 		}
1560Sstevel@tonic-gate 	if (n)
1570Sstevel@tonic-gate 		{
1580Sstevel@tonic-gate 		sqr(r[0],r[1],a[0]); if (--n == 0) return;
1590Sstevel@tonic-gate 		sqr(r[2],r[3],a[1]); if (--n == 0) return;
1600Sstevel@tonic-gate 		sqr(r[4],r[5],a[2]);
1610Sstevel@tonic-gate 		}
1620Sstevel@tonic-gate 	}
1630Sstevel@tonic-gate 
bn_div_words(BN_ULONG h,BN_ULONG l,BN_ULONG d)1640Sstevel@tonic-gate BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d)
1650Sstevel@tonic-gate {	BN_ULONG ret,waste;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	asm ("divq	%4"
1680Sstevel@tonic-gate 		: "=a"(ret),"=d"(waste)
1690Sstevel@tonic-gate 		: "a"(l),"d"(h),"g"(d)
1700Sstevel@tonic-gate 		: "cc");
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	return ret;
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate 
bn_add_words(BN_ULONG * rp,BN_ULONG * ap,BN_ULONG * bp,int n)1750Sstevel@tonic-gate BN_ULONG bn_add_words (BN_ULONG *rp, BN_ULONG *ap, BN_ULONG *bp,int n)
176*2139Sjp161948 { BN_ULONG ret=0,i=0;
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	if (n <= 0) return 0;
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	asm (
1810Sstevel@tonic-gate 	"	subq	%2,%2		\n"
1820Sstevel@tonic-gate 	".align 16			\n"
1830Sstevel@tonic-gate 	"1:	movq	(%4,%2,8),%0	\n"
1840Sstevel@tonic-gate 	"	adcq	(%5,%2,8),%0	\n"
1850Sstevel@tonic-gate 	"	movq	%0,(%3,%2,8)	\n"
1860Sstevel@tonic-gate 	"	leaq	1(%2),%2	\n"
1870Sstevel@tonic-gate 	"	loop	1b		\n"
1880Sstevel@tonic-gate 	"	sbbq	%0,%0		\n"
189*2139Sjp161948 		: "=&a"(ret),"+c"(n),"=&r"(i)
1900Sstevel@tonic-gate 		: "r"(rp),"r"(ap),"r"(bp)
1910Sstevel@tonic-gate 		: "cc"
1920Sstevel@tonic-gate 	);
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate   return ret&1;
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate #ifndef SIMICS
bn_sub_words(BN_ULONG * rp,BN_ULONG * ap,BN_ULONG * bp,int n)1980Sstevel@tonic-gate BN_ULONG bn_sub_words (BN_ULONG *rp, BN_ULONG *ap, BN_ULONG *bp,int n)
199*2139Sjp161948 { BN_ULONG ret=0,i=0;
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	if (n <= 0) return 0;
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	asm (
2040Sstevel@tonic-gate 	"	subq	%2,%2		\n"
2050Sstevel@tonic-gate 	".align 16			\n"
2060Sstevel@tonic-gate 	"1:	movq	(%4,%2,8),%0	\n"
2070Sstevel@tonic-gate 	"	sbbq	(%5,%2,8),%0	\n"
2080Sstevel@tonic-gate 	"	movq	%0,(%3,%2,8)	\n"
2090Sstevel@tonic-gate 	"	leaq	1(%2),%2	\n"
2100Sstevel@tonic-gate 	"	loop	1b		\n"
2110Sstevel@tonic-gate 	"	sbbq	%0,%0		\n"
212*2139Sjp161948 		: "=&a"(ret),"+c"(n),"=&r"(i)
2130Sstevel@tonic-gate 		: "r"(rp),"r"(ap),"r"(bp)
2140Sstevel@tonic-gate 		: "cc"
2150Sstevel@tonic-gate 	);
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate   return ret&1;
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate #else
2200Sstevel@tonic-gate /* Simics 1.4<7 has buggy sbbq:-( */
2210Sstevel@tonic-gate #define BN_MASK2 0xffffffffffffffffL
bn_sub_words(BN_ULONG * r,BN_ULONG * a,BN_ULONG * b,int n)2220Sstevel@tonic-gate BN_ULONG bn_sub_words(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n)
2230Sstevel@tonic-gate         {
2240Sstevel@tonic-gate 	BN_ULONG t1,t2;
2250Sstevel@tonic-gate 	int c=0;
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	if (n <= 0) return((BN_ULONG)0);
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	for (;;)
2300Sstevel@tonic-gate 		{
2310Sstevel@tonic-gate 		t1=a[0]; t2=b[0];
2320Sstevel@tonic-gate 		r[0]=(t1-t2-c)&BN_MASK2;
2330Sstevel@tonic-gate 		if (t1 != t2) c=(t1 < t2);
2340Sstevel@tonic-gate 		if (--n <= 0) break;
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 		t1=a[1]; t2=b[1];
2370Sstevel@tonic-gate 		r[1]=(t1-t2-c)&BN_MASK2;
2380Sstevel@tonic-gate 		if (t1 != t2) c=(t1 < t2);
2390Sstevel@tonic-gate 		if (--n <= 0) break;
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 		t1=a[2]; t2=b[2];
2420Sstevel@tonic-gate 		r[2]=(t1-t2-c)&BN_MASK2;
2430Sstevel@tonic-gate 		if (t1 != t2) c=(t1 < t2);
2440Sstevel@tonic-gate 		if (--n <= 0) break;
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 		t1=a[3]; t2=b[3];
2470Sstevel@tonic-gate 		r[3]=(t1-t2-c)&BN_MASK2;
2480Sstevel@tonic-gate 		if (t1 != t2) c=(t1 < t2);
2490Sstevel@tonic-gate 		if (--n <= 0) break;
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 		a+=4;
2520Sstevel@tonic-gate 		b+=4;
2530Sstevel@tonic-gate 		r+=4;
2540Sstevel@tonic-gate 		}
2550Sstevel@tonic-gate 	return(c);
2560Sstevel@tonic-gate 	}
2570Sstevel@tonic-gate #endif
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate /* mul_add_c(a,b,c0,c1,c2)  -- c+=a*b for three word number c=(c2,c1,c0) */
2600Sstevel@tonic-gate /* mul_add_c2(a,b,c0,c1,c2) -- c+=2*a*b for three word number c=(c2,c1,c0) */
2610Sstevel@tonic-gate /* sqr_add_c(a,i,c0,c1,c2)  -- c+=a[i]^2 for three word number c=(c2,c1,c0) */
2620Sstevel@tonic-gate /* sqr_add_c2(a,i,c0,c1,c2) -- c+=2*a[i]*a[j] for three word number c=(c2,c1,c0) */
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate #if 0
2650Sstevel@tonic-gate /* original macros are kept for reference purposes */
2660Sstevel@tonic-gate #define mul_add_c(a,b,c0,c1,c2) {	\
2670Sstevel@tonic-gate 	BN_ULONG ta=(a),tb=(b);		\
2680Sstevel@tonic-gate 	t1 = ta * tb;			\
2690Sstevel@tonic-gate 	t2 = BN_UMULT_HIGH(ta,tb);	\
2700Sstevel@tonic-gate 	c0 += t1; t2 += (c0<t1)?1:0;	\
2710Sstevel@tonic-gate 	c1 += t2; c2 += (c1<t2)?1:0;	\
2720Sstevel@tonic-gate 	}
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate #define mul_add_c2(a,b,c0,c1,c2) {	\
2750Sstevel@tonic-gate 	BN_ULONG ta=(a),tb=(b),t0;	\
2760Sstevel@tonic-gate 	t1 = BN_UMULT_HIGH(ta,tb);	\
2770Sstevel@tonic-gate 	t0 = ta * tb;			\
2780Sstevel@tonic-gate 	t2 = t1+t1; c2 += (t2<t1)?1:0;	\
2790Sstevel@tonic-gate 	t1 = t0+t0; t2 += (t1<t0)?1:0;	\
2800Sstevel@tonic-gate 	c0 += t1; t2 += (c0<t1)?1:0;	\
2810Sstevel@tonic-gate 	c1 += t2; c2 += (c1<t2)?1:0;	\
2820Sstevel@tonic-gate 	}
2830Sstevel@tonic-gate #else
2840Sstevel@tonic-gate #define mul_add_c(a,b,c0,c1,c2)	do {	\
2850Sstevel@tonic-gate 	asm ("mulq %3"			\
2860Sstevel@tonic-gate 		: "=a"(t1),"=d"(t2)	\
2870Sstevel@tonic-gate 		: "a"(a),"m"(b)		\
2880Sstevel@tonic-gate 		: "cc");		\
2890Sstevel@tonic-gate 	asm ("addq %2,%0; adcq %3,%1"	\
2900Sstevel@tonic-gate 		: "+r"(c0),"+d"(t2)	\
2910Sstevel@tonic-gate 		: "a"(t1),"g"(0)	\
2920Sstevel@tonic-gate 		: "cc");		\
2930Sstevel@tonic-gate 	asm ("addq %2,%0; adcq %3,%1"	\
2940Sstevel@tonic-gate 		: "+r"(c1),"+r"(c2)	\
2950Sstevel@tonic-gate 		: "d"(t2),"g"(0)	\
2960Sstevel@tonic-gate 		: "cc");		\
2970Sstevel@tonic-gate 	} while (0)
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate #define sqr_add_c(a,i,c0,c1,c2)	do {	\
3000Sstevel@tonic-gate 	asm ("mulq %2"			\
3010Sstevel@tonic-gate 		: "=a"(t1),"=d"(t2)	\
3020Sstevel@tonic-gate 		: "a"(a[i])		\
3030Sstevel@tonic-gate 		: "cc");		\
3040Sstevel@tonic-gate 	asm ("addq %2,%0; adcq %3,%1"	\
3050Sstevel@tonic-gate 		: "+r"(c0),"+d"(t2)	\
3060Sstevel@tonic-gate 		: "a"(t1),"g"(0)	\
3070Sstevel@tonic-gate 		: "cc");		\
3080Sstevel@tonic-gate 	asm ("addq %2,%0; adcq %3,%1"	\
3090Sstevel@tonic-gate 		: "+r"(c1),"+r"(c2)	\
3100Sstevel@tonic-gate 		: "d"(t2),"g"(0)	\
3110Sstevel@tonic-gate 		: "cc");		\
3120Sstevel@tonic-gate 	} while (0)
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate #define mul_add_c2(a,b,c0,c1,c2) do {	\
3150Sstevel@tonic-gate 	asm ("mulq %3"			\
3160Sstevel@tonic-gate 		: "=a"(t1),"=d"(t2)	\
3170Sstevel@tonic-gate 		: "a"(a),"m"(b)		\
3180Sstevel@tonic-gate 		: "cc");		\
3190Sstevel@tonic-gate 	asm ("addq %0,%0; adcq %2,%1"	\
3200Sstevel@tonic-gate 		: "+d"(t2),"+r"(c2)	\
3210Sstevel@tonic-gate 		: "g"(0)		\
3220Sstevel@tonic-gate 		: "cc");		\
3230Sstevel@tonic-gate 	asm ("addq %0,%0; adcq %2,%1"	\
3240Sstevel@tonic-gate 		: "+a"(t1),"+d"(t2)	\
3250Sstevel@tonic-gate 		: "g"(0)		\
3260Sstevel@tonic-gate 		: "cc");		\
3270Sstevel@tonic-gate 	asm ("addq %2,%0; adcq %3,%1"	\
3280Sstevel@tonic-gate 		: "+r"(c0),"+d"(t2)	\
3290Sstevel@tonic-gate 		: "a"(t1),"g"(0)	\
3300Sstevel@tonic-gate 		: "cc");		\
3310Sstevel@tonic-gate 	asm ("addq %2,%0; adcq %3,%1"	\
3320Sstevel@tonic-gate 		: "+r"(c1),"+r"(c2)	\
3330Sstevel@tonic-gate 		: "d"(t2),"g"(0)	\
3340Sstevel@tonic-gate 		: "cc");		\
3350Sstevel@tonic-gate 	} while (0)
3360Sstevel@tonic-gate #endif
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate #define sqr_add_c2(a,i,j,c0,c1,c2)	\
3390Sstevel@tonic-gate 	mul_add_c2((a)[i],(a)[j],c0,c1,c2)
3400Sstevel@tonic-gate 
bn_mul_comba8(BN_ULONG * r,BN_ULONG * a,BN_ULONG * b)3410Sstevel@tonic-gate void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
3420Sstevel@tonic-gate 	{
3430Sstevel@tonic-gate 	BN_ULONG t1,t2;
3440Sstevel@tonic-gate 	BN_ULONG c1,c2,c3;
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	c1=0;
3470Sstevel@tonic-gate 	c2=0;
3480Sstevel@tonic-gate 	c3=0;
3490Sstevel@tonic-gate 	mul_add_c(a[0],b[0],c1,c2,c3);
3500Sstevel@tonic-gate 	r[0]=c1;
3510Sstevel@tonic-gate 	c1=0;
3520Sstevel@tonic-gate 	mul_add_c(a[0],b[1],c2,c3,c1);
3530Sstevel@tonic-gate 	mul_add_c(a[1],b[0],c2,c3,c1);
3540Sstevel@tonic-gate 	r[1]=c2;
3550Sstevel@tonic-gate 	c2=0;
3560Sstevel@tonic-gate 	mul_add_c(a[2],b[0],c3,c1,c2);
3570Sstevel@tonic-gate 	mul_add_c(a[1],b[1],c3,c1,c2);
3580Sstevel@tonic-gate 	mul_add_c(a[0],b[2],c3,c1,c2);
3590Sstevel@tonic-gate 	r[2]=c3;
3600Sstevel@tonic-gate 	c3=0;
3610Sstevel@tonic-gate 	mul_add_c(a[0],b[3],c1,c2,c3);
3620Sstevel@tonic-gate 	mul_add_c(a[1],b[2],c1,c2,c3);
3630Sstevel@tonic-gate 	mul_add_c(a[2],b[1],c1,c2,c3);
3640Sstevel@tonic-gate 	mul_add_c(a[3],b[0],c1,c2,c3);
3650Sstevel@tonic-gate 	r[3]=c1;
3660Sstevel@tonic-gate 	c1=0;
3670Sstevel@tonic-gate 	mul_add_c(a[4],b[0],c2,c3,c1);
3680Sstevel@tonic-gate 	mul_add_c(a[3],b[1],c2,c3,c1);
3690Sstevel@tonic-gate 	mul_add_c(a[2],b[2],c2,c3,c1);
3700Sstevel@tonic-gate 	mul_add_c(a[1],b[3],c2,c3,c1);
3710Sstevel@tonic-gate 	mul_add_c(a[0],b[4],c2,c3,c1);
3720Sstevel@tonic-gate 	r[4]=c2;
3730Sstevel@tonic-gate 	c2=0;
3740Sstevel@tonic-gate 	mul_add_c(a[0],b[5],c3,c1,c2);
3750Sstevel@tonic-gate 	mul_add_c(a[1],b[4],c3,c1,c2);
3760Sstevel@tonic-gate 	mul_add_c(a[2],b[3],c3,c1,c2);
3770Sstevel@tonic-gate 	mul_add_c(a[3],b[2],c3,c1,c2);
3780Sstevel@tonic-gate 	mul_add_c(a[4],b[1],c3,c1,c2);
3790Sstevel@tonic-gate 	mul_add_c(a[5],b[0],c3,c1,c2);
3800Sstevel@tonic-gate 	r[5]=c3;
3810Sstevel@tonic-gate 	c3=0;
3820Sstevel@tonic-gate 	mul_add_c(a[6],b[0],c1,c2,c3);
3830Sstevel@tonic-gate 	mul_add_c(a[5],b[1],c1,c2,c3);
3840Sstevel@tonic-gate 	mul_add_c(a[4],b[2],c1,c2,c3);
3850Sstevel@tonic-gate 	mul_add_c(a[3],b[3],c1,c2,c3);
3860Sstevel@tonic-gate 	mul_add_c(a[2],b[4],c1,c2,c3);
3870Sstevel@tonic-gate 	mul_add_c(a[1],b[5],c1,c2,c3);
3880Sstevel@tonic-gate 	mul_add_c(a[0],b[6],c1,c2,c3);
3890Sstevel@tonic-gate 	r[6]=c1;
3900Sstevel@tonic-gate 	c1=0;
3910Sstevel@tonic-gate 	mul_add_c(a[0],b[7],c2,c3,c1);
3920Sstevel@tonic-gate 	mul_add_c(a[1],b[6],c2,c3,c1);
3930Sstevel@tonic-gate 	mul_add_c(a[2],b[5],c2,c3,c1);
3940Sstevel@tonic-gate 	mul_add_c(a[3],b[4],c2,c3,c1);
3950Sstevel@tonic-gate 	mul_add_c(a[4],b[3],c2,c3,c1);
3960Sstevel@tonic-gate 	mul_add_c(a[5],b[2],c2,c3,c1);
3970Sstevel@tonic-gate 	mul_add_c(a[6],b[1],c2,c3,c1);
3980Sstevel@tonic-gate 	mul_add_c(a[7],b[0],c2,c3,c1);
3990Sstevel@tonic-gate 	r[7]=c2;
4000Sstevel@tonic-gate 	c2=0;
4010Sstevel@tonic-gate 	mul_add_c(a[7],b[1],c3,c1,c2);
4020Sstevel@tonic-gate 	mul_add_c(a[6],b[2],c3,c1,c2);
4030Sstevel@tonic-gate 	mul_add_c(a[5],b[3],c3,c1,c2);
4040Sstevel@tonic-gate 	mul_add_c(a[4],b[4],c3,c1,c2);
4050Sstevel@tonic-gate 	mul_add_c(a[3],b[5],c3,c1,c2);
4060Sstevel@tonic-gate 	mul_add_c(a[2],b[6],c3,c1,c2);
4070Sstevel@tonic-gate 	mul_add_c(a[1],b[7],c3,c1,c2);
4080Sstevel@tonic-gate 	r[8]=c3;
4090Sstevel@tonic-gate 	c3=0;
4100Sstevel@tonic-gate 	mul_add_c(a[2],b[7],c1,c2,c3);
4110Sstevel@tonic-gate 	mul_add_c(a[3],b[6],c1,c2,c3);
4120Sstevel@tonic-gate 	mul_add_c(a[4],b[5],c1,c2,c3);
4130Sstevel@tonic-gate 	mul_add_c(a[5],b[4],c1,c2,c3);
4140Sstevel@tonic-gate 	mul_add_c(a[6],b[3],c1,c2,c3);
4150Sstevel@tonic-gate 	mul_add_c(a[7],b[2],c1,c2,c3);
4160Sstevel@tonic-gate 	r[9]=c1;
4170Sstevel@tonic-gate 	c1=0;
4180Sstevel@tonic-gate 	mul_add_c(a[7],b[3],c2,c3,c1);
4190Sstevel@tonic-gate 	mul_add_c(a[6],b[4],c2,c3,c1);
4200Sstevel@tonic-gate 	mul_add_c(a[5],b[5],c2,c3,c1);
4210Sstevel@tonic-gate 	mul_add_c(a[4],b[6],c2,c3,c1);
4220Sstevel@tonic-gate 	mul_add_c(a[3],b[7],c2,c3,c1);
4230Sstevel@tonic-gate 	r[10]=c2;
4240Sstevel@tonic-gate 	c2=0;
4250Sstevel@tonic-gate 	mul_add_c(a[4],b[7],c3,c1,c2);
4260Sstevel@tonic-gate 	mul_add_c(a[5],b[6],c3,c1,c2);
4270Sstevel@tonic-gate 	mul_add_c(a[6],b[5],c3,c1,c2);
4280Sstevel@tonic-gate 	mul_add_c(a[7],b[4],c3,c1,c2);
4290Sstevel@tonic-gate 	r[11]=c3;
4300Sstevel@tonic-gate 	c3=0;
4310Sstevel@tonic-gate 	mul_add_c(a[7],b[5],c1,c2,c3);
4320Sstevel@tonic-gate 	mul_add_c(a[6],b[6],c1,c2,c3);
4330Sstevel@tonic-gate 	mul_add_c(a[5],b[7],c1,c2,c3);
4340Sstevel@tonic-gate 	r[12]=c1;
4350Sstevel@tonic-gate 	c1=0;
4360Sstevel@tonic-gate 	mul_add_c(a[6],b[7],c2,c3,c1);
4370Sstevel@tonic-gate 	mul_add_c(a[7],b[6],c2,c3,c1);
4380Sstevel@tonic-gate 	r[13]=c2;
4390Sstevel@tonic-gate 	c2=0;
4400Sstevel@tonic-gate 	mul_add_c(a[7],b[7],c3,c1,c2);
4410Sstevel@tonic-gate 	r[14]=c3;
4420Sstevel@tonic-gate 	r[15]=c1;
4430Sstevel@tonic-gate 	}
4440Sstevel@tonic-gate 
bn_mul_comba4(BN_ULONG * r,BN_ULONG * a,BN_ULONG * b)4450Sstevel@tonic-gate void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
4460Sstevel@tonic-gate 	{
4470Sstevel@tonic-gate 	BN_ULONG t1,t2;
4480Sstevel@tonic-gate 	BN_ULONG c1,c2,c3;
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	c1=0;
4510Sstevel@tonic-gate 	c2=0;
4520Sstevel@tonic-gate 	c3=0;
4530Sstevel@tonic-gate 	mul_add_c(a[0],b[0],c1,c2,c3);
4540Sstevel@tonic-gate 	r[0]=c1;
4550Sstevel@tonic-gate 	c1=0;
4560Sstevel@tonic-gate 	mul_add_c(a[0],b[1],c2,c3,c1);
4570Sstevel@tonic-gate 	mul_add_c(a[1],b[0],c2,c3,c1);
4580Sstevel@tonic-gate 	r[1]=c2;
4590Sstevel@tonic-gate 	c2=0;
4600Sstevel@tonic-gate 	mul_add_c(a[2],b[0],c3,c1,c2);
4610Sstevel@tonic-gate 	mul_add_c(a[1],b[1],c3,c1,c2);
4620Sstevel@tonic-gate 	mul_add_c(a[0],b[2],c3,c1,c2);
4630Sstevel@tonic-gate 	r[2]=c3;
4640Sstevel@tonic-gate 	c3=0;
4650Sstevel@tonic-gate 	mul_add_c(a[0],b[3],c1,c2,c3);
4660Sstevel@tonic-gate 	mul_add_c(a[1],b[2],c1,c2,c3);
4670Sstevel@tonic-gate 	mul_add_c(a[2],b[1],c1,c2,c3);
4680Sstevel@tonic-gate 	mul_add_c(a[3],b[0],c1,c2,c3);
4690Sstevel@tonic-gate 	r[3]=c1;
4700Sstevel@tonic-gate 	c1=0;
4710Sstevel@tonic-gate 	mul_add_c(a[3],b[1],c2,c3,c1);
4720Sstevel@tonic-gate 	mul_add_c(a[2],b[2],c2,c3,c1);
4730Sstevel@tonic-gate 	mul_add_c(a[1],b[3],c2,c3,c1);
4740Sstevel@tonic-gate 	r[4]=c2;
4750Sstevel@tonic-gate 	c2=0;
4760Sstevel@tonic-gate 	mul_add_c(a[2],b[3],c3,c1,c2);
4770Sstevel@tonic-gate 	mul_add_c(a[3],b[2],c3,c1,c2);
4780Sstevel@tonic-gate 	r[5]=c3;
4790Sstevel@tonic-gate 	c3=0;
4800Sstevel@tonic-gate 	mul_add_c(a[3],b[3],c1,c2,c3);
4810Sstevel@tonic-gate 	r[6]=c1;
4820Sstevel@tonic-gate 	r[7]=c2;
4830Sstevel@tonic-gate 	}
4840Sstevel@tonic-gate 
bn_sqr_comba8(BN_ULONG * r,BN_ULONG * a)4850Sstevel@tonic-gate void bn_sqr_comba8(BN_ULONG *r, BN_ULONG *a)
4860Sstevel@tonic-gate 	{
4870Sstevel@tonic-gate 	BN_ULONG t1,t2;
4880Sstevel@tonic-gate 	BN_ULONG c1,c2,c3;
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 	c1=0;
4910Sstevel@tonic-gate 	c2=0;
4920Sstevel@tonic-gate 	c3=0;
4930Sstevel@tonic-gate 	sqr_add_c(a,0,c1,c2,c3);
4940Sstevel@tonic-gate 	r[0]=c1;
4950Sstevel@tonic-gate 	c1=0;
4960Sstevel@tonic-gate 	sqr_add_c2(a,1,0,c2,c3,c1);
4970Sstevel@tonic-gate 	r[1]=c2;
4980Sstevel@tonic-gate 	c2=0;
4990Sstevel@tonic-gate 	sqr_add_c(a,1,c3,c1,c2);
5000Sstevel@tonic-gate 	sqr_add_c2(a,2,0,c3,c1,c2);
5010Sstevel@tonic-gate 	r[2]=c3;
5020Sstevel@tonic-gate 	c3=0;
5030Sstevel@tonic-gate 	sqr_add_c2(a,3,0,c1,c2,c3);
5040Sstevel@tonic-gate 	sqr_add_c2(a,2,1,c1,c2,c3);
5050Sstevel@tonic-gate 	r[3]=c1;
5060Sstevel@tonic-gate 	c1=0;
5070Sstevel@tonic-gate 	sqr_add_c(a,2,c2,c3,c1);
5080Sstevel@tonic-gate 	sqr_add_c2(a,3,1,c2,c3,c1);
5090Sstevel@tonic-gate 	sqr_add_c2(a,4,0,c2,c3,c1);
5100Sstevel@tonic-gate 	r[4]=c2;
5110Sstevel@tonic-gate 	c2=0;
5120Sstevel@tonic-gate 	sqr_add_c2(a,5,0,c3,c1,c2);
5130Sstevel@tonic-gate 	sqr_add_c2(a,4,1,c3,c1,c2);
5140Sstevel@tonic-gate 	sqr_add_c2(a,3,2,c3,c1,c2);
5150Sstevel@tonic-gate 	r[5]=c3;
5160Sstevel@tonic-gate 	c3=0;
5170Sstevel@tonic-gate 	sqr_add_c(a,3,c1,c2,c3);
5180Sstevel@tonic-gate 	sqr_add_c2(a,4,2,c1,c2,c3);
5190Sstevel@tonic-gate 	sqr_add_c2(a,5,1,c1,c2,c3);
5200Sstevel@tonic-gate 	sqr_add_c2(a,6,0,c1,c2,c3);
5210Sstevel@tonic-gate 	r[6]=c1;
5220Sstevel@tonic-gate 	c1=0;
5230Sstevel@tonic-gate 	sqr_add_c2(a,7,0,c2,c3,c1);
5240Sstevel@tonic-gate 	sqr_add_c2(a,6,1,c2,c3,c1);
5250Sstevel@tonic-gate 	sqr_add_c2(a,5,2,c2,c3,c1);
5260Sstevel@tonic-gate 	sqr_add_c2(a,4,3,c2,c3,c1);
5270Sstevel@tonic-gate 	r[7]=c2;
5280Sstevel@tonic-gate 	c2=0;
5290Sstevel@tonic-gate 	sqr_add_c(a,4,c3,c1,c2);
5300Sstevel@tonic-gate 	sqr_add_c2(a,5,3,c3,c1,c2);
5310Sstevel@tonic-gate 	sqr_add_c2(a,6,2,c3,c1,c2);
5320Sstevel@tonic-gate 	sqr_add_c2(a,7,1,c3,c1,c2);
5330Sstevel@tonic-gate 	r[8]=c3;
5340Sstevel@tonic-gate 	c3=0;
5350Sstevel@tonic-gate 	sqr_add_c2(a,7,2,c1,c2,c3);
5360Sstevel@tonic-gate 	sqr_add_c2(a,6,3,c1,c2,c3);
5370Sstevel@tonic-gate 	sqr_add_c2(a,5,4,c1,c2,c3);
5380Sstevel@tonic-gate 	r[9]=c1;
5390Sstevel@tonic-gate 	c1=0;
5400Sstevel@tonic-gate 	sqr_add_c(a,5,c2,c3,c1);
5410Sstevel@tonic-gate 	sqr_add_c2(a,6,4,c2,c3,c1);
5420Sstevel@tonic-gate 	sqr_add_c2(a,7,3,c2,c3,c1);
5430Sstevel@tonic-gate 	r[10]=c2;
5440Sstevel@tonic-gate 	c2=0;
5450Sstevel@tonic-gate 	sqr_add_c2(a,7,4,c3,c1,c2);
5460Sstevel@tonic-gate 	sqr_add_c2(a,6,5,c3,c1,c2);
5470Sstevel@tonic-gate 	r[11]=c3;
5480Sstevel@tonic-gate 	c3=0;
5490Sstevel@tonic-gate 	sqr_add_c(a,6,c1,c2,c3);
5500Sstevel@tonic-gate 	sqr_add_c2(a,7,5,c1,c2,c3);
5510Sstevel@tonic-gate 	r[12]=c1;
5520Sstevel@tonic-gate 	c1=0;
5530Sstevel@tonic-gate 	sqr_add_c2(a,7,6,c2,c3,c1);
5540Sstevel@tonic-gate 	r[13]=c2;
5550Sstevel@tonic-gate 	c2=0;
5560Sstevel@tonic-gate 	sqr_add_c(a,7,c3,c1,c2);
5570Sstevel@tonic-gate 	r[14]=c3;
5580Sstevel@tonic-gate 	r[15]=c1;
5590Sstevel@tonic-gate 	}
5600Sstevel@tonic-gate 
bn_sqr_comba4(BN_ULONG * r,BN_ULONG * a)5610Sstevel@tonic-gate void bn_sqr_comba4(BN_ULONG *r, BN_ULONG *a)
5620Sstevel@tonic-gate 	{
5630Sstevel@tonic-gate 	BN_ULONG t1,t2;
5640Sstevel@tonic-gate 	BN_ULONG c1,c2,c3;
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	c1=0;
5670Sstevel@tonic-gate 	c2=0;
5680Sstevel@tonic-gate 	c3=0;
5690Sstevel@tonic-gate 	sqr_add_c(a,0,c1,c2,c3);
5700Sstevel@tonic-gate 	r[0]=c1;
5710Sstevel@tonic-gate 	c1=0;
5720Sstevel@tonic-gate 	sqr_add_c2(a,1,0,c2,c3,c1);
5730Sstevel@tonic-gate 	r[1]=c2;
5740Sstevel@tonic-gate 	c2=0;
5750Sstevel@tonic-gate 	sqr_add_c(a,1,c3,c1,c2);
5760Sstevel@tonic-gate 	sqr_add_c2(a,2,0,c3,c1,c2);
5770Sstevel@tonic-gate 	r[2]=c3;
5780Sstevel@tonic-gate 	c3=0;
5790Sstevel@tonic-gate 	sqr_add_c2(a,3,0,c1,c2,c3);
5800Sstevel@tonic-gate 	sqr_add_c2(a,2,1,c1,c2,c3);
5810Sstevel@tonic-gate 	r[3]=c1;
5820Sstevel@tonic-gate 	c1=0;
5830Sstevel@tonic-gate 	sqr_add_c(a,2,c2,c3,c1);
5840Sstevel@tonic-gate 	sqr_add_c2(a,3,1,c2,c3,c1);
5850Sstevel@tonic-gate 	r[4]=c2;
5860Sstevel@tonic-gate 	c2=0;
5870Sstevel@tonic-gate 	sqr_add_c2(a,3,2,c3,c1,c2);
5880Sstevel@tonic-gate 	r[5]=c3;
5890Sstevel@tonic-gate 	c3=0;
5900Sstevel@tonic-gate 	sqr_add_c(a,3,c1,c2,c3);
5910Sstevel@tonic-gate 	r[6]=c1;
5920Sstevel@tonic-gate 	r[7]=c2;
5930Sstevel@tonic-gate 	}
594