Blame


1 b3f61791 2004-03-21 devnull #include "os.h"
2 b3f61791 2004-03-21 devnull #include <mp.h>
3 b3f61791 2004-03-21 devnull #include "dat.h"
4 b3f61791 2004-03-21 devnull
5 b3f61791 2004-03-21 devnull #define VLDIGITS (sizeof(vlong)/sizeof(mpdigit))
6 b3f61791 2004-03-21 devnull
7 b3f61791 2004-03-21 devnull /*
8 b3f61791 2004-03-21 devnull * this code assumes that a vlong is an integral number of
9 b3f61791 2004-03-21 devnull * mpdigits long.
10 b3f61791 2004-03-21 devnull */
11 b3f61791 2004-03-21 devnull mpint*
12 b3f61791 2004-03-21 devnull vtomp(vlong v, mpint *b)
13 b3f61791 2004-03-21 devnull {
14 b3f61791 2004-03-21 devnull int s;
15 b3f61791 2004-03-21 devnull uvlong uv;
16 b3f61791 2004-03-21 devnull
17 b3f61791 2004-03-21 devnull if(b == nil)
18 b3f61791 2004-03-21 devnull b = mpnew(VLDIGITS*sizeof(mpdigit));
19 b3f61791 2004-03-21 devnull else
20 b3f61791 2004-03-21 devnull mpbits(b, VLDIGITS*sizeof(mpdigit));
21 b3f61791 2004-03-21 devnull mpassign(mpzero, b);
22 b3f61791 2004-03-21 devnull if(v == 0)
23 b3f61791 2004-03-21 devnull return b;
24 b3f61791 2004-03-21 devnull if(v < 0){
25 b3f61791 2004-03-21 devnull b->sign = -1;
26 b3f61791 2004-03-21 devnull uv = -v;
27 b3f61791 2004-03-21 devnull } else
28 b3f61791 2004-03-21 devnull uv = v;
29 b3f61791 2004-03-21 devnull for(s = 0; s < VLDIGITS && uv != 0; s++){
30 b3f61791 2004-03-21 devnull b->p[s] = uv;
31 b3f61791 2004-03-21 devnull uv >>= sizeof(mpdigit)*8;
32 b3f61791 2004-03-21 devnull }
33 b3f61791 2004-03-21 devnull b->top = s;
34 b3f61791 2004-03-21 devnull return b;
35 b3f61791 2004-03-21 devnull }
36 b3f61791 2004-03-21 devnull
37 b3f61791 2004-03-21 devnull vlong
38 b3f61791 2004-03-21 devnull mptov(mpint *b)
39 b3f61791 2004-03-21 devnull {
40 b3f61791 2004-03-21 devnull uvlong v;
41 b3f61791 2004-03-21 devnull int s;
42 b3f61791 2004-03-21 devnull
43 b3f61791 2004-03-21 devnull if(b->top == 0)
44 b3f61791 2004-03-21 devnull return 0LL;
45 b3f61791 2004-03-21 devnull
46 b3f61791 2004-03-21 devnull mpnorm(b);
47 b3f61791 2004-03-21 devnull if(b->top > VLDIGITS){
48 b3f61791 2004-03-21 devnull if(b->sign > 0)
49 b3f61791 2004-03-21 devnull return (vlong)MAXVLONG;
50 b3f61791 2004-03-21 devnull else
51 b3f61791 2004-03-21 devnull return (vlong)MINVLONG;
52 b3f61791 2004-03-21 devnull }
53 b3f61791 2004-03-21 devnull
54 b3f61791 2004-03-21 devnull v = 0ULL;
55 b3f61791 2004-03-21 devnull for(s = 0; s < b->top; s++)
56 b3f61791 2004-03-21 devnull v |= b->p[s]<<(s*sizeof(mpdigit)*8);
57 b3f61791 2004-03-21 devnull
58 b3f61791 2004-03-21 devnull if(b->sign > 0){
59 b3f61791 2004-03-21 devnull if(v > MAXVLONG)
60 b3f61791 2004-03-21 devnull v = MAXVLONG;
61 b3f61791 2004-03-21 devnull } else {
62 b3f61791 2004-03-21 devnull if(v > MINVLONG)
63 b3f61791 2004-03-21 devnull v = MINVLONG;
64 b3f61791 2004-03-21 devnull else
65 b3f61791 2004-03-21 devnull v = -(vlong)v;
66 b3f61791 2004-03-21 devnull }
67 b3f61791 2004-03-21 devnull
68 b3f61791 2004-03-21 devnull return (vlong)v;
69 b3f61791 2004-03-21 devnull }