-
Notifications
You must be signed in to change notification settings - Fork 358
Expand file tree
/
Copy pathInstruction.cpp
More file actions
149 lines (136 loc) · 3.85 KB
/
Instruction.cpp
File metadata and controls
149 lines (136 loc) · 3.85 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
/*
* Instruction.cpp
*
*/
#include "Instruction.h"
#include "instructions.h"
#include "Processor.h"
#include "Memory.h"
#include "Math/gf2n.h"
#include "GC/instructions.h"
#include "Memory.hpp"
#include "Instruction.hpp"
#include <iomanip>
template<class cgf2n>
void Instruction::execute_clear_gf2n(StackedVector<cgf2n>& registers,
MemoryPart<cgf2n>& memory, ArithmeticProcessor& Proc) const
{
auto& C2 = registers;
auto& M2C = memory;
int active_size = get_effective_vector_size(Proc, size);
switch (opcode)
{
#define X(NAME, PRE, CODE) \
case NAME: { PRE; for (int i = 0; i < active_size; i++) { CODE; } } break;
CLEAR_GF2N_INSTRUCTIONS
#undef X
}
}
template<class cgf2n>
void Instruction::gbitdec(StackedVector<cgf2n>& registers) const
{
for (int j = 0; j < size; j++)
{
typename cgf2n::internal_type a = registers.at(r[0] + j).get();
for (unsigned int i = 0; i < start.size(); i++)
{
registers.at(start[i] + j) = a & 1;
a >>= n;
}
}
}
template<class cgf2n>
void Instruction::gbitcom(StackedVector<cgf2n>& registers) const
{
for (int j = 0; j < size; j++)
{
typename cgf2n::internal_type a = 0;
for (unsigned int i = 0; i < start.size(); i++)
{
a ^= registers.at(start[i] + j).get() << (i * n);
}
registers.at(r[0] + j) = a;
}
}
void Instruction::execute_regint(ArithmeticProcessor& Proc, MemoryPart<Integer>& Mi) const
{
(void) Mi;
auto& Ci = Proc.get_Ci();
int active_size = get_effective_vector_size(Proc, size);
switch (opcode)
{
#define X(NAME, PRE, CODE) \
case NAME: { PRE; for (int i = 0; i < active_size; i++) { CODE; } } break;
REGINT_INSTRUCTIONS
#undef X
}
}
void Instruction::shuffle(ArithmeticProcessor& Proc) const
{
int active_size = get_effective_vector_size(Proc, size);
for (int i = 0; i < active_size; i++)
Proc.write_Ci(r[0] + i, Proc.read_Ci(r[1] + i));
for (int i = 0; i < active_size; i++)
{
int j = Proc.shared_prng.get_uint(active_size - i);
swap(Proc.get_Ci_ref(r[0] + i), Proc.get_Ci_ref(r[0] + i + j));
}
}
void Instruction::bitdecint(ArithmeticProcessor& Proc) const
{
int active_size = get_effective_vector_size(Proc, size);
for (int j = 0; j < active_size; j++)
{
long a = Proc.read_Ci(r[0] + j);
for (unsigned int i = 0; i < start.size(); i++)
{
Proc.get_Ci_ref(start[i] + j) = (a >> i) & 1;
}
}
}
string BaseInstruction::get_name() const
{
switch (get_opcode())
{
#define X(NAME, PRE, CODE) \
case NAME: return #NAME;
ALL_INSTRUCTIONS
#undef X
#define X(NAME, CODE) \
case NAME: return #NAME;
COMBI_INSTRUCTIONS
default:
stringstream ss;
ss << showbase << hex << get_opcode();
return ss.str();
}
}
void BaseInstruction::bytecode_assert(bool condition) const
{
if (not condition)
throw runtime_error("bytecode assertion violated");
}
ostream& operator<<(ostream& s, const Instruction& instr)
{
s << instr.get_name();
s << " size=" << instr.get_size();
s << " n=" << instr.get_n();
s << " r=(";
for (int i = 0; i < 3; i++)
s << instr.get_r(i) << ", ";
s << instr.get_r(3);
s << ")";
if (not instr.get_start().empty())
{
s << " args=(";
for (unsigned i = 0; i < instr.get_start().size() - 1; i++)
s << instr.get_start()[i] << ", ";
s << instr.get_start().back();
s << ")";
}
return s;
}
template void Instruction::execute_clear_gf2n(StackedVector<gf2n_short>& registers,
MemoryPart<gf2n_short>& memory, ArithmeticProcessor& Proc) const;
template void Instruction::execute_clear_gf2n(StackedVector<gf2n_long>& registers,
MemoryPart<gf2n_long>& memory, ArithmeticProcessor& Proc) const;