aboutsummaryrefslogtreecommitdiffstats
path: root/libs/bigint/BigIntegerUtils.cc
blob: 44073af65274d3286a4c84d67fbd5aa287464962 (plain)
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
#include "BigIntegerUtils.hh"
#include "BigUnsignedInABase.hh"

std::string bigUnsignedToString(const BigUnsigned &x) {
	return std::string(BigUnsignedInABase(x, 10));
}

std::string bigIntegerToString(const BigInteger &x) {
	return (x.getSign() == BigInteger::negative)
		? (std::string("-") + bigUnsignedToString(x.getMagnitude()))
		: (bigUnsignedToString(x.getMagnitude()));
}

BigUnsigned stringToBigUnsigned(const std::string &s) {
	return BigUnsigned(BigUnsignedInABase(s, 10));
}

BigInteger stringToBigInteger(const std::string &s) {
	// Recognize a sign followed by a BigUnsigned.
	return (s[0] == '-') ? BigInteger(stringToBigUnsigned(s.substr(1, s.length() - 1)), BigInteger::negative)
		: (s[0] == '+') ? BigInteger(stringToBigUnsigned(s.substr(1, s.length() - 1)))
		: BigInteger(stringToBigUnsigned(s));
}

std::ostream &operator <<(std::ostream &os, const BigUnsigned &x) {
	BigUnsignedInABase::Base base;
	long osFlags = os.flags();
	if (osFlags & os.dec)
		base = 10;
	else if (osFlags & os.hex) {
		base = 16;
		if (osFlags & os.showbase)
			os << "0x";
	} else if (osFlags & os.oct) {
		base = 8;
		if (osFlags & os.showbase)
			os << '0';
	} else
		throw "std::ostream << BigUnsigned: Could not determine the desired base from output-stream flags";
	std::string s = std::string(BigUnsignedInABase(x, base));
	os << s;
	return os;
}

std::ostream &operator <<(std::ostream &os, const BigInteger &x) {
	if (x.getSign() == BigInteger::negative)
		os << '-';
	os << x.getMagnitude();
	return os;
}
pan class="o">*> ff_cells; for (auto cell : module->selected_cells()) { if (sb_dff_types.count(cell->type)) { ff_cells.push_back(cell); continue; } if (cell->type != "$_MUX_") continue; SigBit bit_a = sigmap(cell->getPort("\\A")); SigBit bit_b = sigmap(cell->getPort("\\B")); if (bit_a.wire == nullptr || bit_b.wire == nullptr) sr_muxes[sigmap(cell->getPort("\\Y"))] = cell; } for (auto cell : ff_cells) { if (cell->get_bool_attribute("\\dont_touch")) continue; SigSpec sig_d = cell->getPort("\\D"); if (GetSize(sig_d) < 1) continue; SigBit bit_d = sigmap(sig_d[0]); if (sr_muxes.count(bit_d) == 0) continue; Cell *mux_cell = sr_muxes.at(bit_d); SigBit bit_a = sigmap(mux_cell->getPort("\\A")); SigBit bit_b = sigmap(mux_cell->getPort("\\B")); SigBit bit_s = sigmap(mux_cell->getPort("\\S")); log(" Merging %s (A=%s, B=%s, S=%s) into %s (%s).\n", log_id(mux_cell), log_signal(bit_a), log_signal(bit_b), log_signal(bit_s), log_id(cell), log_id(cell->type)); SigBit sr_val, sr_sig; if (bit_a.wire == nullptr) { bit_d = bit_b; sr_val = bit_a; sr_sig = module->NotGate(NEW_ID, bit_s); } else { log_assert(bit_b.wire == nullptr); bit_d = bit_a; sr_val = bit_b; sr_sig = bit_s; } if (sr_val == State::S1) { cell->type = cell->type.str() + "SS"; cell->setPort("\\S", sr_sig); cell->setPort("\\D", bit_d); } else { cell->type = cell->type.str() + "SR"; cell->setPort("\\R", sr_sig); cell->setPort("\\D", bit_d); } } } } } Ice40FfssrPass; PRIVATE_NAMESPACE_END