diff options
| author | Charles.Forsyth <devnull@localhost> | 2006-12-22 17:07:39 +0000 |
|---|---|---|
| committer | Charles.Forsyth <devnull@localhost> | 2006-12-22 17:07:39 +0000 |
| commit | 37da2899f40661e3e9631e497da8dc59b971cbd0 (patch) | |
| tree | cbc6d4680e347d906f5fa7fca73214418741df72 /libmp/port/mpdigdiv.c | |
| parent | 54bc8ff236ac10b3eaa928fd6bcfc0cdb2ba46ae (diff) | |
20060303a
Diffstat (limited to 'libmp/port/mpdigdiv.c')
| -rw-r--r-- | libmp/port/mpdigdiv.c | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/libmp/port/mpdigdiv.c b/libmp/port/mpdigdiv.c new file mode 100644 index 00000000..4a73bb3a --- /dev/null +++ b/libmp/port/mpdigdiv.c @@ -0,0 +1,43 @@ +#include "os.h" +#include <mp.h> +#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<<i; + if(x == hi && y > lo) + continue; + if(y > lo) + hi--; + lo -= y; + hi -= x; + q |= 1<<i; + } + q += lo/divisor; + *quotient = q; +} |
