-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBI_InOut.c
More file actions
149 lines (132 loc) · 3.67 KB
/
BI_InOut.c
File metadata and controls
149 lines (132 loc) · 3.67 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
146
147
148
149
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "BI_Structure.h"
#include "BI_BasicOps.h"
#include "BI_Logic.h"
#include "BI_BitwiseOps.h"
#include "BI_Math.h"
void BI_UPrintHex(BigInteger bi) {
printf("0x%016llx%016llx", bi.MSBs, bi.LSBs);
}
void BI_PrintHex(BigInteger bi) {
if (BI_Sign(bi)) {
BigInteger abs = BI_Neg(bi);
printf("-0x%016llx%016llx", abs.MSBs, abs.LSBs);
} else {
BI_UPrintHex(bi);
}
}
void BI_UPrint(BigInteger bi) {
if (BI_IsZero(bi)) printf("0");
char revDigits[39];
int i = 0;
while (!BI_IsZero(bi)) {
BigInteger *res = BI_Div(bi, BI_Create(0, 10));
BigInteger rem = res[1];
revDigits[i++] = (char) (rem.LSBs + 0x30);
bi = res[0];
}
char digits[i];
for (int j = 0; j < i; j++) digits[j] = revDigits[i - j - 1];
digits[i] = '\0';
printf("%s", digits);
}
void BI_Print(BigInteger bi) {
if (BI_IsZero(bi)) printf("0");
if (!BI_Sign(bi)) BI_UPrint(bi);
else {
bi = BI_Neg(bi);
printf("-");
BI_UPrint(bi);
}
}
BigInteger BI_FromStr(char *str) {
BigInteger res = BI_Create(0, 0);
BigInteger curPow = BI_Create(0, 1);
BigInteger max = BI_Create(0x7fffffffffffffff, 0xffffffffffffffff);
for (int i = strlen(str) - 1; i >= (str[0] == '-'); i--) {
if (str[i] < 0x30 || str[i] > 0x39) {
fprintf(stderr, "Invalid digit. (Allowed digits: 0-9)\n");
exit(1);
}
BigInteger pos = BI_Create(0, (uint64) (str[i] - 0x30));
pos = BI_Mul(pos, curPow);
res = BI_Add(res, pos);
BigInteger newMax = BI_Sub(max, pos);
if (BI_Above(newMax, max)) {
fprintf(stderr, "Too large number! (Min: -2^127 + 1 = -170141183460469231731687303715884105727, Max: 2^127 - 1 = 170141183460469231731687303715884105727)\n");
exit(1);
}
max = newMax;
curPow = BI_Mul(curPow, BI_Create(0, 10));
}
if (str[0] == '-') res = BI_Neg(res);
return res;
}
BigInteger BI_UFromStr(char *str) {
BigInteger res = BI_Create(0, 0);
BigInteger curPow = BI_Create(0, 1);
BigInteger max = BI_Create(0xffffffffffffffff, 0xffffffffffffffff);
for (int i = strlen(str) - 1; i >= 0; i--) {
if (str[i] < 0x30 || str[i] > 0x39) {
fprintf(stderr, "Invalid digit. (Allowed digits: 0-9)\n");
exit(1);
}
BigInteger pos = BI_Create(0, (uint64) (str[i] - 0x30));
pos = BI_Mul(pos, curPow);
res = BI_Add(res, pos);
BigInteger newMax = BI_Sub(max, pos);
if (BI_Above(newMax, max)) {
fprintf(stderr, "Too large number! (Min: 0, Max: 2^128 - 1 = 340282366920938463463374607431768211455)\n");
exit(1);
}
max = newMax;
curPow = BI_Mul(curPow, BI_Create(0, 10));
}
return res;
}
char* BI_UToStrHex(BigInteger bi) {
char* hex = malloc(35);
sprintf(hex, "0x%016llx%016llx", bi.MSBs, bi.LSBs);
return hex;
}
char* BI_ToStrHex(BigInteger bi) {
char* hex = malloc(36);
if (BI_Sign(bi))
sprintf(hex, "0x%016llx%016llx", bi.MSBs, bi.LSBs);
else {
BigInteger neg = BI_Neg(bi);
sprintf(hex, "-0x%016llx%016llx", neg.MSBs, neg.LSBs);
}
return hex;
}
char* BI_UToStr(BigInteger bi) {
char* revDigits = malloc(40);
int i = 0;
do {
BigInteger *divRes = BI_Div(bi, BI_Create(0, 10));
bi = divRes[0];
revDigits[i++] = (char) (divRes[1].LSBs + 0x30);
} while(!BI_IsZero(bi));
char* digits = malloc(40);
for (int j = 0; j < i; j++) digits[j] = revDigits[i - j - 1];
digits[i] = '\0';
return digits;
}
char* BI_ToStr(BigInteger bi) {
char* revDigits = malloc(40);
if (!BI_Sign(bi)) return BI_UToStr(bi);
BigInteger abs = BI_Neg(bi);
int i = 0;
do {
BigInteger *divRes = BI_Div(abs, BI_Create(0, 10));
abs = divRes[0];
revDigits[i++] = (char) (divRes[1].LSBs + 0x30);
} while(!BI_IsZero(abs));
revDigits[i++] = '-';
char* digits = malloc(40);
for (int j = 0; j < i; j++) digits[j] = revDigits[i - j - 1];
digits[i] = '\0';
return digits;
}