From 37da2899f40661e3e9631e497da8dc59b971cbd0 Mon Sep 17 00:00:00 2001 From: "Charles.Forsyth" Date: Fri, 22 Dec 2006 17:07:39 +0000 Subject: 20060303a --- libmp/port/mpleft.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 libmp/port/mpleft.c (limited to 'libmp/port/mpleft.c') diff --git a/libmp/port/mpleft.c b/libmp/port/mpleft.c new file mode 100644 index 00000000..cdcdff74 --- /dev/null +++ b/libmp/port/mpleft.c @@ -0,0 +1,52 @@ +#include "os.h" +#include +#include "dat.h" + +// res = b << shift +void +mpleft(mpint *b, int shift, mpint *res) +{ + int d, l, r, i, otop; + mpdigit this, last; + + res->sign = b->sign; + if(b->top==0){ + res->top = 0; + return; + } + + // a negative left shift is a right shift + if(shift < 0){ + mpright(b, -shift, res); + return; + } + + // b and res may be the same so remember the old top + otop = b->top; + + // shift + mpbits(res, otop*Dbits + shift); // overkill + res->top = DIGITS(otop*Dbits + shift); + d = shift/Dbits; + l = shift - d*Dbits; + r = Dbits - l; + + if(l == 0){ + for(i = otop-1; i >= 0; i--) + res->p[i+d] = b->p[i]; + } else { + last = 0; + for(i = otop-1; i >= 0; i--) { + this = b->p[i]; + res->p[i+d+1] = (last<>r); + last = this; + } + res->p[d] = last<p[i] = 0; + + // normalize + while(res->top > 0 && res->p[res->top-1] == 0) + res->top--; +} -- cgit v1.2.3