From b3f61791f1e9095ce8ae9c6d6415b4ee94e2f7eb Mon Sep 17 00:00:00 2001 From: rsc Date: Sun, 21 Mar 2004 14:06:38 +0000 Subject: Add libmp. --- src/libmp/port/mpdigdiv.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/libmp/port/mpdigdiv.c (limited to 'src/libmp/port/mpdigdiv.c') diff --git a/src/libmp/port/mpdigdiv.c b/src/libmp/port/mpdigdiv.c new file mode 100644 index 00000000..4a73bb3a --- /dev/null +++ b/src/libmp/port/mpdigdiv.c @@ -0,0 +1,43 @@ +#include "os.h" +#include +#include "dat.h" + +// +// divide two digits by one and return quotient +// +void +mpdigdiv(mpdigit *dividend, mpdigit divisor, mpdigit *quotient) +{ + mpdigit hi, lo, q, x, y; + int i; + + hi = dividend[1]; + lo = dividend[0]; + + // return highest digit value if the result >= 2**32 + if(hi >= divisor || divisor == 0){ + divisor = 0; + *quotient = ~divisor; + return; + } + + // at this point we know that hi < divisor + // just shift and subtract till we're done + q = 0; + x = divisor; + for(i = Dbits-1; hi > 0 && i >= 0; i--){ + x >>= 1; + if(x > hi) + continue; + y = divisor< lo) + continue; + if(y > lo) + hi--; + lo -= y; + hi -= x; + q |= 1<