forked from Yasushi/putty
-
Notifications
You must be signed in to change notification settings - Fork 6
/
int64.c
175 lines (147 loc) · 3.41 KB
/
int64.c
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* Handling of the int64 and uint64 types. Done in 32-bit integers,
* for (pre-C99) portability. Hopefully once C99 becomes widespread
* we can kiss this lot goodbye...
*/
#include <assert.h>
#include <string.h>
#include "int64.h"
uint64 uint64_div10(uint64 x, int *remainder)
{
uint64 y;
unsigned int rem, r2;
y.hi = x.hi / 10;
y.lo = x.lo / 10;
rem = x.lo % 10;
/*
* Now we have to add in the remainder left over from x.hi.
*/
r2 = x.hi % 10;
y.lo += r2 * 429496729;
rem += r2 * 6;
y.lo += rem / 10;
rem %= 10;
if (remainder)
*remainder = rem;
return y;
}
void uint64_decimal(uint64 x, char *buffer)
{
char buf[20];
int start = 20;
int d;
do {
x = uint64_div10(x, &d);
assert(start > 0);
buf[--start] = d + '0';
} while (x.hi || x.lo);
memcpy(buffer, buf + start, sizeof(buf) - start);
buffer[sizeof(buf) - start] = '\0';
}
uint64 uint64_make(unsigned long hi, unsigned long lo)
{
uint64 y;
y.hi = hi & 0xFFFFFFFFU;
y.lo = lo & 0xFFFFFFFFU;
return y;
}
uint64 uint64_add(uint64 x, uint64 y)
{
x.lo = (x.lo + y.lo) & 0xFFFFFFFFU;
x.hi += y.hi + (x.lo < y.lo ? 1 : 0);
return x;
}
uint64 uint64_add32(uint64 x, unsigned long y)
{
uint64 yy;
yy.hi = 0;
yy.lo = y;
return uint64_add(x, yy);
}
int uint64_compare(uint64 x, uint64 y)
{
if (x.hi != y.hi)
return x.hi < y.hi ? -1 : +1;
if (x.lo != y.lo)
return x.lo < y.lo ? -1 : +1;
return 0;
}
uint64 uint64_subtract(uint64 x, uint64 y)
{
x.lo = (x.lo - y.lo) & 0xFFFFFFFFU;
x.hi = (x.hi - y.hi - (x.lo > (y.lo ^ 0xFFFFFFFFU) ? 1 : 0)) & 0xFFFFFFFFU;
return x;
}
double uint64_to_double(uint64 x)
{
return (4294967296.0 * x.hi) + (double)x.lo;
}
uint64 uint64_shift_right(uint64 x, int shift)
{
if (shift < 32) {
x.lo >>= shift;
x.lo |= (x.hi << (32-shift)) & 0xFFFFFFFFU;
x.hi >>= shift;
} else {
x.lo = x.hi >> (shift-32);
x.hi = 0;
}
return x;
}
uint64 uint64_shift_left(uint64 x, int shift)
{
if (shift < 32) {
x.hi = (x.hi << shift) & 0xFFFFFFFFU;
x.hi |= (x.lo >> (32-shift));
x.lo = (x.lo << shift) & 0xFFFFFFFFU;
} else {
x.hi = (x.lo << (shift-32)) & 0xFFFFFFFFU;
x.lo = 0;
}
return x;
}
uint64 uint64_from_decimal(char *str)
{
uint64 ret;
ret.hi = ret.lo = 0;
while (*str >= '0' && *str <= '9') {
ret = uint64_add(uint64_shift_left(ret, 3),
uint64_shift_left(ret, 1));
ret = uint64_add32(ret, *str - '0');
str++;
}
return ret;
}
#ifdef TESTMODE
#include <stdio.h>
int main(void)
{
uint64 x, y, z;
char buf[80];
x = uint64_make(0x3456789AUL, 0xDEF01234UL);
printf("%08lx.%08lx\n", x.hi, x.lo);
uint64_decimal(x, buf);
printf("%s\n", buf);
y = uint64_add32(x, 0xFFFFFFFFU);
printf("%08lx.%08lx\n", y.hi, y.lo);
uint64_decimal(y, buf);
printf("%s\n", buf);
z = uint64_subtract(y, x);
printf("%08lx.%08lx\n", z.hi, z.lo);
uint64_decimal(z, buf);
printf("%s\n", buf);
z = uint64_subtract(x, y);
printf("%08lx.%08lx\n", z.hi, z.lo);
uint64_decimal(z, buf);
printf("%s\n", buf);
y = uint64_shift_right(x, 4);
printf("%08lx.%08lx\n", y.hi, y.lo);
y = uint64_shift_right(x, 36);
printf("%08lx.%08lx\n", y.hi, y.lo);
y = uint64_shift_left(x, 4);
printf("%08lx.%08lx\n", x.hi, x.lo);
y = uint64_shift_left(x, 36);
printf("%08lx.%08lx\n", x.hi, x.lo);
return 0;
}
#endif