更新一部分 ls 代码

This commit is contained in:
YuhangQ 2023-02-12 18:14:12 +08:00
parent d3b23cbfb4
commit d613341d1c
7 changed files with 116 additions and 7 deletions

BIN
atpg

Binary file not shown.

View File

@ -27,4 +27,57 @@ void Circuit::cal_topo_index() {
}
}
}
}
int Circuit::cal_gate_value(const Gate* &gate) {
int res;
switch(gate->type) {
case Gate::NOT:
res = !gate->inputs[0]->value;
break;
case Gate::BUF:
res = gate->inputs[0]->value;
break;
case Gate::AND:
res = gate->inputs[0]->value;
for(int i=1; i<gate->inputs.size(); i++) {
res &= gate->inputs[i]->value;
}
break;
case Gate::NAND:
res = gate->inputs[0]->value;
for(int i=1; i<gate->inputs.size(); i++) {
res &= gate->inputs[i]->value;
}
res = !res;
break;
case Gate::OR:
res = gate->inputs[0]->value;
for(int i=1; i<gate->inputs.size(); i++) {
res |= gate->inputs[i]->value;
}
break;
case Gate::NOR:
res = gate->inputs[0]->value;
for(int i=1; i<gate->inputs.size(); i++) {
res |= gate->inputs[i]->value;
}
res = !res;
break;
case Gate::XOR:
res = gate->inputs[0]->value;
for(int i=1; i<gate->inputs.size(); i++) {
res ^= gate->inputs[i]->value;
}
break;
case Gate::XNOR:
res = gate->inputs[0]->value;
for(int i=1; i<gate->inputs.size(); i++) {
res ^= gate->inputs[i]->value;
}
res = !res;
break;
}
return res;
}

View File

@ -1,3 +1,5 @@
#pragma once
#include <string>
#include <vector>
#include <unordered_map>
@ -6,8 +8,8 @@ class Gate {
public:
int topo;
std::string name;
enum { AND, NAND, OR, NOR, XOR, XNOR, NOT, BUF, INPUT, OUTPUT } type;
enum { VAL_ZERO, VAL_ONE, VAL_X } value;
enum Type { AND, NAND, OR, NOR, XOR, XNOR, NOT, BUF, INPUT, OUTPUT } type;
int value;
std::vector<Gate*> outputs;
std::vector<Gate*> inputs;
};
@ -19,6 +21,7 @@ std::vector<Gate*> POs;
std::vector<Gate*> gates;
std::unordered_map<std::string, Gate*> name2gate;
void parse_from_file(const char *filename);
int cal_gate_value(const Gate* &gate);
void cal_topo_index();
void cal_headlines();
};

35
ls.cpp Normal file
View File

@ -0,0 +1,35 @@
#include "ls.h"
std::vector<int> local_search(Circuit* circuit) {
// local search vector
std::vector<Gate*> vec;
init_vector(circuit, &vec);
init_circuit(circuit);
printf("local search!\n");
return std::vector<int>();
}
void init_circuit(Circuit* circuit) {
for(auto pi : circuit->PIs) {
pi->value = rand() % 2;
}
}
void init_vector(Circuit* circuit, std::vector<Gate*> *vec) {
for(auto pi : circuit->PIs) {
vec->push_back(pi);
}
for(auto gate : circuit->gates) {
// fanout stem
if(gate->outputs.size() >= 2) {
vec->push_back(gate);
}
}
}

9
ls.h Normal file
View File

@ -0,0 +1,9 @@
#pragma once
#include "circuit.h"
std::vector<int> local_search(Circuit* circuit);
// private
void init_circuit(Circuit* circuit);
void init_vector(Circuit* circuit, std::vector<Gate*> *vec);

View File

@ -2,8 +2,8 @@
#include <cstdlib>
#include <assert.h>
#include "circuit.h"
#include "ls.h"
int main(int args, char* argv[]) {
@ -21,11 +21,22 @@ int main(int args, char* argv[]) {
printf("PO:\t%ld\n", circuit.POs.size());
printf("Gates:\t%ld\n", circuit.name2gate.size());
int cnt = 0;
for(auto& gate: circuit.gates) {
if(gate->outputs.size() > 2) {
cnt++;
}
}
printf("Stem:\t%d\n", cnt);
printf("cal topo index ...\n");
circuit.cal_topo_index();
for(Gate* gate : circuit.gates) {
assert(gate->topo > 0);
}
srand(19260817);
local_search(&circuit);
return 0;
}

View File

@ -73,7 +73,6 @@ void Circuit::parse_from_file(const char *filename) {
Gate* gate = new Gate();
gate->name = tokens[0];
gate->value = Gate::VAL_X;
for(auto &in : ins) {
if(!name2gate.count(in)) {
@ -109,10 +108,9 @@ void Circuit::parse_from_file(const char *filename) {
Gate* gate = new Gate();
gate->name = tokens[2];
gate->type = Gate::INPUT;
gate->value = Gate::VAL_X;
name2gate.insert(std::make_pair(gate->name, gate));
gates.push_back(gate);
// gates.push_back(gate);
PIs.push_back(gate);
}
// gate