A little debugging.
[openssl.git] / crypto / bn / bn_div.c
index bbd09940089cf38cabc3f014f04682c083413dca..580d1201bc253f7257f0abdd0be5497dac45922e 100644 (file)
@@ -130,7 +130,7 @@ int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d,
 #if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) \
     && !defined(PEDANTIC) && !defined(BN_DIV3W)
 # if defined(__GNUC__) && __GNUC__>=2
-#  if defined(__i386)
+#  if defined(__i386) || defined (__i386__)
    /*
     * There were two reasons for implementing this template:
     * - GNU C generates a call to a function (__udivdi3 to be exact)
@@ -150,6 +150,20 @@ int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d,
            q;                                  \
        })
 #  define REMAINDER_IS_ALREADY_CALCULATED
+#  elif defined(__x86_64) && defined(SIXTY_FOUR_BIT_LONG)
+   /*
+    * Same story here, but it's 128-bit by 64-bit division. Wow!
+    *                                  <appro@fy.chalmers.se>
+    */
+#  define bn_div_words(n0,n1,d0)               \
+       ({  asm volatile (                      \
+               "divq   %4"                     \
+               : "=a"(q), "=d"(rem)            \
+               : "a"(n1), "d"(n0), "g"(d0)     \
+               : "cc");                        \
+           q;                                  \
+       })
+#  define REMAINDER_IS_ALREADY_CALCULATED
 #  endif /* __<cpu> */
 # endif /* __GNUC__ */
 #endif /* OPENSSL_NO_ASM */
@@ -200,10 +214,10 @@ int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,
 
        /* First we normalise the numbers */
        norm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);
-       BN_lshift(sdiv,divisor,norm_shift);
+       if (!(BN_lshift(sdiv,divisor,norm_shift))) goto err;
        sdiv->neg=0;
        norm_shift+=BN_BITS2;
-       BN_lshift(snum,num,norm_shift);
+       if (!(BN_lshift(snum,num,norm_shift))) goto err;
        snum->neg=0;
        div_n=sdiv->top;
        num_n=snum->top;
@@ -250,6 +264,7 @@ int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,
                {
                BN_ULONG q,l0;
 #if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)
+               BN_ULONG bn_div_3_words(BN_ULONG*,BN_ULONG,BN_ULONG);
                q=bn_div_3_words(wnump,d1,d0);
 #else
                BN_ULONG n0,n1,rem=0;
@@ -267,6 +282,11 @@ int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,
                        q=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0);
 #else
                        q=bn_div_words(n0,n1,d0);
+#ifdef BN_DEBUG_LEVITTE
+                       fprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\
+X) -> 0x%08X\n",
+                               n0, n1, d0, q);
+#endif
 #endif
 
 #ifndef REMAINDER_IS_ALREADY_CALCULATED
@@ -291,11 +311,18 @@ int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,
                        BN_ULONG t2l,t2h,ql,qh;
 
                        q=bn_div_words(n0,n1,d0);
+#ifdef BN_DEBUG_LEVITTE
+                       fprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\
+X) -> 0x%08X\n",
+                               n0, n1, d0, q);
+#endif
 #ifndef REMAINDER_IS_ALREADY_CALCULATED
                        rem=(n1-q*d0)&BN_MASK2;
 #endif
 
-#ifdef BN_UMULT_HIGH
+#if defined(BN_UMULT_LOHI)
+                       BN_UMULT_LOHI(t2l,t2h,d1,q);
+#elif defined(BN_UMULT_HIGH)
                        t2l = d1 * q;
                        t2h = BN_UMULT_HIGH(d1,q);
 #else
@@ -326,7 +353,7 @@ int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,
                tmp->top=j;
 
                j=wnum.top;
-               BN_sub(&wnum,&wnum,tmp);
+               if (!BN_sub(&wnum,&wnum,tmp)) goto err;
 
                snum->top=snum->top+wnum.top-j;
 
@@ -334,7 +361,7 @@ int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,
                        {
                        q--;
                        j=wnum.top;
-                       BN_add(&wnum,&wnum,sdiv);
+                       if (!BN_add(&wnum,&wnum,sdiv)) goto err;
                        snum->top+=wnum.top-j;
                        }
                *(resp--)=q;