-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbint.cpp
66 lines (54 loc) · 2.12 KB
/
bint.cpp
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
#include<bint.h>
using base_int_t = uint32_t;
inline std::pair<base_int_t,base_int_t> base_int_add(base_int_t x,base_int_t y,base_int_t carry=0)
{
uint64_t r=uint64_t(x)+y+carry;
return std::make_pair(r,r>>32);
}
inline std::pair<base_int_t,base_int_t> base_int_sub(base_int_t x,base_int_t y,base_int_t carry=0)
{
uint64_t r=uint64_t(x)-y-carry;
return std::make_pair(r,(r>>32)&1);
}
inline std::pair<base_int_t,base_int_t> base_int_mul(base_int_t x,base_int_t y,base_int_t carry=0)
{
uint64_t r=uint64_t(x)*y+carry;
return std::make_pair(r,r>>32);
}
inline std::pair<base_int_t,base_int_t> base_int_divmod(base_int_t x,base_int_t y,base_int_t carry=0)
{
uint64_t t=uint64_t(x)+(uint64_t(carry)<<32);
return std::make_pair(t/y,t%y);
}
#include<cstdio>
int main(int argc, char const *argv[])
{
//sub
printf("sub\n");
{auto [x,y]=base_int_sub(1,0,1);printf("%u;%u\n",x,y);}
{auto [x,y]=base_int_sub(1,1,1);printf("%u;%u\n",x,y);}
{auto [x,y]=base_int_sub(1,2,0);printf("%u;%u\n",x,y);}
{auto [x,y]=base_int_sub(0,0,1);printf("%u;%u\n",x,y);}
{auto [x,y]=base_int_sub(0,0,0);printf("%u;%u\n",x,y);}
//add
printf("add\n");
{auto [x,y]=base_int_add(1,~0,0);printf("%u;%u\n",x,y);}
{auto [x,y]=base_int_add(1,~1,1);printf("%u;%u\n",x,y);}
{auto [x,y]=base_int_add(0,0,1);printf("%u;%u\n",x,y);}
{auto [x,y]=base_int_add(1,0,0);printf("%u;%u\n",x,y);}
//mul
printf("mul\n");
{auto [x,y]=base_int_mul(1,0,1);printf("%u;%u\n",x,y);}
{auto [x,y]=base_int_mul(1,3,2);printf("%u;%u\n",x,y);}
{auto [x,y]=base_int_mul(1000000000,1000000000,1);printf("%u;%u\n",x,y);}
{auto [x,y]=base_int_mul(~0,~0,~0);printf("%u;%u\n",x,y);}
{auto [x,y]=base_int_mul(~0,~0,0);printf("%u;%u\n",x,y);}
//divmod
printf("divmod\n");
{auto [x,y]=base_int_divmod(0,2,1);printf("%u;%u\n",x,y);}
{auto [x,y]=base_int_divmod(0,2,0);printf("%u;%u\n",x,y);}
{auto [x,y]=base_int_divmod(65536,2,1);printf("%u;%u\n",x,y);}
{auto [x,y]=base_int_divmod(0,2,~0);printf("%u;%u\n",x,y);}
{auto [x,y]=base_int_divmod(1,2,1);printf("%u;%u\n",x,y);}
return 0;
}