diff options
author | rsc <devnull@localhost> | 2004-03-21 14:06:38 +0000 |
---|---|---|
committer | rsc <devnull@localhost> | 2004-03-21 14:06:38 +0000 |
commit | b3f61791f1e9095ce8ae9c6d6415b4ee94e2f7eb (patch) | |
tree | deaa85427ae73a045ddef39395bd286f9d3dc2ff /src/libmp/port/mptov.c | |
parent | 498bb22174aa2c76493e8c67b92949271131ebfb (diff) | |
download | plan9port-b3f61791f1e9095ce8ae9c6d6415b4ee94e2f7eb.tar.gz plan9port-b3f61791f1e9095ce8ae9c6d6415b4ee94e2f7eb.tar.bz2 plan9port-b3f61791f1e9095ce8ae9c6d6415b4ee94e2f7eb.zip |
Add libmp.
Diffstat (limited to 'src/libmp/port/mptov.c')
-rw-r--r-- | src/libmp/port/mptov.c | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/src/libmp/port/mptov.c b/src/libmp/port/mptov.c new file mode 100644 index 00000000..b09718ef --- /dev/null +++ b/src/libmp/port/mptov.c @@ -0,0 +1,69 @@ +#include "os.h" +#include <mp.h> +#include "dat.h" + +#define VLDIGITS (sizeof(vlong)/sizeof(mpdigit)) + +/* + * this code assumes that a vlong is an integral number of + * mpdigits long. + */ +mpint* +vtomp(vlong v, mpint *b) +{ + int s; + uvlong uv; + + if(b == nil) + b = mpnew(VLDIGITS*sizeof(mpdigit)); + else + mpbits(b, VLDIGITS*sizeof(mpdigit)); + mpassign(mpzero, b); + if(v == 0) + return b; + if(v < 0){ + b->sign = -1; + uv = -v; + } else + uv = v; + for(s = 0; s < VLDIGITS && uv != 0; s++){ + b->p[s] = uv; + uv >>= sizeof(mpdigit)*8; + } + b->top = s; + return b; +} + +vlong +mptov(mpint *b) +{ + uvlong v; + int s; + + if(b->top == 0) + return 0LL; + + mpnorm(b); + if(b->top > VLDIGITS){ + if(b->sign > 0) + return (vlong)MAXVLONG; + else + return (vlong)MINVLONG; + } + + v = 0ULL; + for(s = 0; s < b->top; s++) + v |= b->p[s]<<(s*sizeof(mpdigit)*8); + + if(b->sign > 0){ + if(v > MAXVLONG) + v = MAXVLONG; + } else { + if(v > MINVLONG) + v = MINVLONG; + else + v = -(vlong)v; + } + + return (vlong)v; +} |