From: Andy Polyakov Date: Sat, 31 Jul 1999 23:27:41 +0000 (+0000) Subject: Extra i386+gcc bn_div.c tune-up featuring inline division and saving X-Git-Tag: OpenSSL_0_9_4~44 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=4c22909e31bcb284973243c3b1622503455c667c;p=oweals%2Fopenssl.git Extra i386+gcc bn_div.c tune-up featuring inline division and saving the remainder left in %edx. Here is the resulting performance improvement matrix (improvement as a result of this *and* previous tune-up committed two days ago). The results were obtained by profiling the "div" part of the crypto/bn/bnspeed.c. CPU BN_div bn_div_words overall comment ------------------------------------------------------------------------ PII +16% accumulated by +2-3% PII multiplies damn fast! Taking inlining multiplication out of the loop didn't make too much difference. Eliminating of the multiplication involved in remainder calculation is the major factor. Pentium +45% accumulated by +7-9% mull isn't that fast and replacing inlining multiplications with additions in the loop has more visible effect:-) MIPS +75% +12% +20-25% In addition to the taking mults R10000 out of the loop (giving 12% in the asm/mips3.s) three mults were eliminated in BN_div. Alpha +30% +50% +10-15% Same as above. But remember that EV4 bn_div_words is a C implementation. It takes 4 Alpha mults in C to do the same thing as 1 MIPS mult in assembler does. So the effect (50%) is more impressive. But not the overall one... Well, if Alpha bn_mul_add would be implemented in assembler overall improvement would be closer to MIPS... --- diff --git a/crypto/bn/bn_div.c b/crypto/bn/bn_div.c index 03b9152241..e4253f6095 100644 --- a/crypto/bn/bn_div.c +++ b/crypto/bn/bn_div.c @@ -204,15 +204,41 @@ int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor, #ifdef BN_DIV3W q=bn_div_3_words(wnump,d0,d1); #else - BN_ULONG n0,n1,rem; + +#if !defined(NO_ASM) +# if defined(__GNUC__) && __GNUC__>=2 +# if defined(__i386) + /* + * There were two reasons for implementing this template: + * - GNU C generates a call to a function (__udivdi3 to be exact) + * in reply to ((((BN_ULLONG)n0)< + */ +# define bn_div_words(n0,n1,d0) \ + ({ asm volatile ( \ + "divl %4" \ + : "=a"(q), "=d"(rem) \ + : "a"(n1), "d"(n0), "g"(d0) \ + : "cc"); \ + q; \ + }) +# define REMINDER_IS_ALREADY_CALCULATED +# endif /* __ */ +# endif /* __GNUC__ */ +#endif /* NO_ASM */ + BN_ULONG n0,n1,rem=0; n0=wnump[0]; n1=wnump[-1]; if (n0 == d0) q=BN_MASK2; else -#if defined(BN_LLONG) && defined(BN_DIV2W) - q=((((BN_ULLONG)n0)<d,sdiv->d,div_n,q); tmp->d[div_n]=l0;