-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlint.cpp
More file actions
145 lines (111 loc) · 2.24 KB
/
lint.cpp
File metadata and controls
145 lines (111 loc) · 2.24 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//
//
// file : lint.cpp
// desc. : large integer class implementation for 128-bit arithmetic
//
// author : Cagdas Calik, ccalik@metu.edu.tr
// date : 14.03.2012
//
#include "lint.h"
#include <memory.h>
#include <stdio.h>
void LINT_Zero(LINT *a)
{
a->words[0] = 0;
a->words[1] = 0;
}
void LINT_Set(LINT *a, u64 val)
{
a->words[0] = val;
a->words[1] = 0;
}
void LINT_Add(LINT *a, LINT *b, LINT *c)
{
c->words[0] = a->words[0] + b->words[0];
c->words[1] = a->words[1] + b->words[1];
// carry
if((c->words[0] < a->words[0]) || (c->words[0] < b->words[0]))
c->words[1]++;
}
void LINT_Sub(LINT *a, LINT *b, LINT *c)
{
c->words[0] = a->words[0] - b->words[0];
c->words[1] = a->words[1] - b->words[1];
// borrow
if((c->words[0] > a->words[0]) && (c->words[0] > b->words[0]))
c->words[1]--;
}
void LINT_Shl1(LINT *a)
{
u64 msb;
msb = a->words[0] >> 63;
a->words[1] = (a->words[1] << 1) ^ msb;
a->words[0] <<= 1;
}
void LINT_Shr(LINT *a, u32 x)
{
u64 highpart;
if(x == 0)
return;
if(x >= 64)
{
x -= 64;
a->words[0] = a->words[1] >> x;
a->words[1] = 0;
}
else
{
highpart = a->words[1] << (64 - x);
a->words[1] >>= x;
a->words[0] = (a->words[0] >> x) ^ highpart;
}
}
void LINT_Negate(LINT *a)
{
a->words[0] = ~a->words[0];
a->words[1] = ~a->words[1];
// increment
a->words[0]++;
if(a->words[0] == 0)
a->words[1]++;
}
void LINT_2Pow(LINT *a, u32 x)
{
a->words[0] = 0;
a->words[1] = 0;
a->words[x / WORD_BITS] ^= (u64)1 << (x % WORD_BITS);
}
void LINT_Print(LINT *a)
{
//printf("%I64u:%I64u\n", a->words[1], a->words[0]);
int index = 0, i = 0;
char buffer[64];
LINT t, q, r;
if(a->words[0] == 0 && a->words[1] == 0)
{
printf("0\n");
return;
}
t = *a;
do
{
LINT_Zero(&q);
LINT_Zero(&r);
for(i = 127; i >= 0; i--)
{
LINT_Shl1(&r);
r.words[0] |= LINT_GETBIT(&t, i);
if(r.words[0] >= 10)
{
r.words[0] -= 10;
LINT_SETBIT(&q, i);
}
}
buffer[index++] = r.words[0] + '0';
t = q;
}while(!(q.words[0] == 0 && q.words[1] == 0));
// reverse print
for(i = index - 1; i >= 0; i--)
printf("%c", buffer[i]);
printf("\n");
}