blob: b09718ef07a9df771ae8fba4bf281b59a8608731 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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;
}
|