Sindbad~EG File Manager

Current Path : /usr/home/beeson/MathXpert/bignums/
Upload File :
Current File : /usr/home/beeson/MathXpert/bignums/longmult64.c

/* M. Beeson */
/*  Multiply two 32 bit integers and get the two digits of the 64-bit answer.
  Divide a 64-bit integer by a large enough 32-bit integer and get 32-bit quotients and remainders. */
/*  5.22.13,  adapted from earlier 16/32-bit code.   */
/*  long longs are now 8 bytes = 64 bits, longs are 32 bits, no assembly language is needed.
*/
// #include <assert.h>

#include "bignum.h"

 void longmult(unsigned x, unsigned y, unsigned *lo, unsigned *hi)
{ unsigned long long X = x;
  unsigned long long Y = y;
  unsigned long long ans = X*Y;
  *lo = (unsigned) ans;
  *hi = (unsigned) (ans >> 32);
}
  
 
#ifdef _WIN32
#ifndef _WIN64  //  this is the code for 32-bit Windows
/* the assembly-language MUL instruction multiplies register EAX by its
argument and leaves the high word in EDX and the low word in EAX */

void longmult(unsigned x, unsigned y, unsigned *lo, unsigned *hi)
{ unsigned a,b;
  __asm   
   { mov eax, x;
     mul dword ptr y;
     mov a, eax;
     mov b, edx;
   }
  *lo = a;
  *hi = b;
}
#endif
#endif

 
 

/*______________________________________________________________________*/
  void longdiv(unsigned x, unsigned y, unsigned z, unsigned *q, unsigned *r)
/* divide a 64 bit number [x,y] by a 32 bit number z producing a 32-bit quotient
and remainder.  Warning: overflow can occur if the true quotient is more
than 32 bits.
x = hi 32 bits of the dividend
y = lo 32 bits of the dividend
z = divisor
*q = quotient
*r = remainder
*/

{ unsigned long long X = x;
  unsigned long long Y= y;
  unsigned long long P = (X << (sizeof(unsigned)*8) ) + Y;  // maybe | is more efficient than +
  unsigned long long quo =  P/z;
  unsigned long long rem =   P - quo*z;
  *q = (unsigned) quo;
  *r = (unsigned) rem;
}

/*  This was the 32-bit code
#ifndef _WIN64
{ unsigned a,b;
  __asm 
   { mov edx, x;
     mov eax, y;
     div dword ptr z;
     mov a, eax;
     mov b, edx;
   }
  *q = a;
  *r = b;
}
*/ 



Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists