-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.cpp
More file actions
97 lines (88 loc) · 1.74 KB
/
utils.cpp
File metadata and controls
97 lines (88 loc) · 1.74 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
#include <string>
#include <stdexcept>
#include <stdlib.h>
#include <gmpxx.h>
#include <vector>
#include "Linear.h"
using namespace std;
mpz_class shift_left(mpz_class x, int i) {
mpz_t result;
mpz_init(result);
mp_bitcnt_t bits = i;
mpz_mul_2exp(result, x.get_mpz_t(), bits);
return mpz_class(result);
}
mpz_class modinv(mpz_class x, mpz_class mod) {
mpz_t ret;
mpz_init(ret);
mpz_class n = mod - 2;
mpz_powm(ret, x.get_mpz_t(), n.get_mpz_t(), mod.get_mpz_t());
return mpz_class(ret);
}
unsigned long next_power_of_two(unsigned long v)
{
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
return v;
}
std::string var_str(int x) {
int type = x % 4;
int val = x / 4;
std::string result;
if (type == 0)
result = "L";
else if (type == 1)
result = "R";
else if (type == 2)
result = "O";
else if (type == 3)
result = "T";
result.append(to_string(val));
return result;
}
int var_idx(int x) {
return x / 4;
}
int var_offset(char type) {
if (type == 'L')
return 0;
else if (type == 'R')
return 1;
else if (type == 'O')
return 2;
else if (type == 'T')
return 3;
else
throw invalid_argument("Invalid variable type");
}
char var_type(int x) {
int offset = x % 4;
if (offset == 0)
return 'L';
else if (offset == 1)
return 'R';
else if (offset == 2)
return 'O';
else if (offset == 3)
return 'T';
else
throw invalid_argument("Modular arithmetic is broken");
}
//TODO: This is bad abstraction
int random_variable(unsigned int range) {
int offset = rand() % 3; //'L', 'R', or 'O'
int idx = rand() % range;
return 4*idx + offset;
}
bool all_const(vector<Linear> v) {
for (int i = 0; i < v.size(); i++) {
if (!v[i].is_const())
return false;
}
return true;
}