-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
477 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
|
||
from ariths_gen.multi_bit_circuits.others.popcount import ( | ||
UnsignedPopCount | ||
) | ||
|
||
from ariths_gen.multi_bit_circuits.others.bit_reduce import ( | ||
BitReduce, AndReduce, OrReduce | ||
) | ||
|
||
from ariths_gen.multi_bit_circuits.others.compare import ( | ||
UnsignedCompareLT | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
""" | ||
""" | ||
|
||
from ariths_gen.wire_components import ( | ||
Wire, | ||
ConstantWireValue0, | ||
ConstantWireValue1, | ||
Bus, | ||
wires | ||
) | ||
from ariths_gen.core.arithmetic_circuits import ( | ||
ArithmeticCircuit, | ||
GeneralCircuit, | ||
MultiplierCircuit | ||
) | ||
from ariths_gen.core.logic_gate_circuits import ( | ||
MultipleInputLogicGate | ||
) | ||
from ariths_gen.one_bit_circuits.one_bit_components import ( | ||
HalfAdder, | ||
FullAdder, | ||
FullAdderP, | ||
TwoOneMultiplexer | ||
) | ||
from ariths_gen.one_bit_circuits.logic_gates import ( | ||
AndGate, | ||
NandGate, | ||
OrGate, | ||
NorGate, | ||
XorGate, | ||
XnorGate, | ||
NotGate | ||
) | ||
|
||
from ariths_gen.core.logic_gate_circuits import TwoInputLogicGate, TwoInputInvertedLogicGate, OneInputLogicGate | ||
from ariths_gen.multi_bit_circuits.adders import UnsignedRippleCarryAdder | ||
|
||
from math import log2, ceil | ||
|
||
class BitReduce(GeneralCircuit): | ||
"""Class representing tree reducer circuit. Doent work for NAND gate! | ||
""" | ||
|
||
def __init__(self, a: Bus, gate : TwoInputLogicGate, prefix : str = "", name : str = "bitreduce", **kwargs): | ||
self.N = a.N | ||
self.a = a | ||
|
||
outc = 1 | ||
super().__init__(name=name, prefix=prefix, inputs = [self.a], out_N=outc) | ||
|
||
# tree reduction | ||
def create_tree(a: Bus, depth: int, branch="A"): | ||
|
||
#print(a) | ||
if a.N == 1: | ||
return a[0] | ||
else: | ||
half = a.N // 2 | ||
b_in = Bus(N=half, prefix=f"b_inn{depth}A") | ||
c_in = Bus(N=a.N - half, prefix=f"b_inn{depth}B") | ||
#print(a, half, a.N) | ||
|
||
|
||
for i, j in enumerate(range(half)): | ||
b_in[i] = a[j] | ||
|
||
for i, j in enumerate(range(half, a.N)): | ||
c_in[i] = a[j] | ||
|
||
b = create_tree(b_in, depth=depth + 1, branch = "A") | ||
c = create_tree(c_in, depth= depth + 1, branch = "B") | ||
d = gate(a=b, b=c, prefix = f"{self.prefix}_red_{branch}_{depth}") | ||
self.add_component(d) | ||
return d.out | ||
|
||
sumwire = create_tree(self.a, 0, "X") | ||
#print(sumbus) | ||
self.out[0] = sumwire | ||
|
||
|
||
class OrReduce(BitReduce): | ||
def __init__(self, a: Bus, prefix : str = "", name : str = "orreduce", **kwargs): | ||
super().__init__(a=a, gate=OrGate, prefix=prefix, name=name, **kwargs) | ||
|
||
|
||
class AndReduce(BitReduce): | ||
def __init__(self, a: Bus, prefix : str = "", name : str = "orreduce", **kwargs): | ||
super().__init__(a=a, gate=AndGate, prefix=prefix, name=name, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
""" | ||
""" | ||
|
||
from ariths_gen.wire_components import ( | ||
Wire, | ||
ConstantWireValue0, | ||
ConstantWireValue1, | ||
Bus, | ||
wires | ||
) | ||
from ariths_gen.core.arithmetic_circuits import ( | ||
ArithmeticCircuit, | ||
GeneralCircuit, | ||
MultiplierCircuit | ||
) | ||
|
||
from ariths_gen.core.logic_gate_circuits import ( | ||
MultipleInputLogicGate | ||
) | ||
from ariths_gen.one_bit_circuits.one_bit_components import ( | ||
HalfAdder, | ||
FullAdder, | ||
FullAdderP, | ||
TwoOneMultiplexer | ||
) | ||
from ariths_gen.one_bit_circuits.logic_gates import ( | ||
AndGate, | ||
NandGate, | ||
OrGate, | ||
NorGate, | ||
XorGate, | ||
XnorGate, | ||
NotGate | ||
) | ||
|
||
from ariths_gen.multi_bit_circuits.others import OrReduce | ||
|
||
|
||
from math import log2, ceil | ||
|
||
class UnsignedCompareLT(GeneralCircuit): | ||
"""Class representing unsigned compare | ||
Returns true if a < b | ||
""" | ||
|
||
def __init__(self, a: Bus, b: Bus, prefix : str = "", name : str = "cmp_lt", **kwargs): | ||
self.a = a | ||
self.b = b | ||
self.N = max(a.N, b.N) | ||
|
||
#print("outc", outc) | ||
super().__init__(name=name, prefix=prefix, | ||
inputs = [self.a, self.b], out_N=1) | ||
|
||
|
||
self.a.bus_extend(self.N, prefix=a.prefix) | ||
self.b.bus_extend(self.N, prefix=b.prefix) | ||
|
||
|
||
# create wires | ||
psum = ConstantWireValue1() | ||
|
||
res = Bus(N = self.N, prefix=self.prefix + "res") | ||
|
||
|
||
for i in reversed(range(self.N)): | ||
|
||
i1 = self.add_component(NotGate(self.a[i], f"{self.prefix}_i1_{i}")).out | ||
i2 = self.b[i] | ||
|
||
and1 = self.add_component(AndGate(i1, i2, f"{self.prefix}_and1_{i}")).out | ||
res[i] = self.add_component(AndGate(and1, psum, f"{self.prefix}_and2_{i}")).out | ||
|
||
pi = self.add_component(XnorGate(self.a[i], self.b[i], f"{self.prefix}_pi_{i}")).out | ||
psum = self.add_component(AndGate(pi, psum, f"{self.prefix}_psum_{i}")).out | ||
|
||
|
||
self.out = self.add_component(OrReduce(res, prefix=f"{self.prefix}_orred")).out | ||
#self.out.connect_bus(sumbus ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
""" | ||
""" | ||
|
||
from ariths_gen.wire_components import ( | ||
Wire, | ||
ConstantWireValue0, | ||
ConstantWireValue1, | ||
Bus, | ||
wires | ||
) | ||
from ariths_gen.core.arithmetic_circuits import ( | ||
ArithmeticCircuit, | ||
GeneralCircuit, | ||
MultiplierCircuit | ||
) | ||
from ariths_gen.core.logic_gate_circuits import ( | ||
MultipleInputLogicGate | ||
) | ||
from ariths_gen.one_bit_circuits.one_bit_components import ( | ||
HalfAdder, | ||
FullAdder, | ||
FullAdderP, | ||
TwoOneMultiplexer | ||
) | ||
from ariths_gen.one_bit_circuits.logic_gates import ( | ||
AndGate, | ||
NandGate, | ||
OrGate, | ||
NorGate, | ||
XorGate, | ||
XnorGate, | ||
NotGate | ||
) | ||
|
||
from ariths_gen.multi_bit_circuits.adders import UnsignedRippleCarryAdder | ||
|
||
from math import log2, ceil | ||
|
||
class UnsignedPopCount(GeneralCircuit): | ||
"""Class representing unsigned popcount circuit. | ||
Popcount circuit is a circuit that counts the number of 1s in a binary number. | ||
""" | ||
|
||
def __init__(self, a: Bus, adder : ArithmeticCircuit|None = None, prefix : str = "", name : str = "popcnt", **kwargs): | ||
self.N = a.N | ||
self.a = a | ||
|
||
outc = ceil(log2(self.N + 1)) | ||
#print("outc", outc) | ||
super().__init__(name=name, prefix=prefix, inputs = [self.a], out_N=outc) | ||
|
||
|
||
self.a.bus_extend(2**(outc - 1), prefix=a.prefix) | ||
#print(self.a) | ||
self.adder = adder | ||
if not self.adder: | ||
self.adder = UnsignedRippleCarryAdder | ||
|
||
# tree reduction | ||
def create_tree(a: Bus, depth: int, branch="A"): | ||
|
||
#print(a) | ||
if a.N == 1: | ||
return a | ||
else: | ||
half = a.N // 2 | ||
b_in = Bus(N=half, prefix=f"b_inn{depth}A") | ||
c_in = Bus(N=a.N - half, prefix=f"b_inn{depth}B") | ||
#print(a, half, a.N) | ||
|
||
|
||
for i, j in enumerate(range(half)): | ||
b_in[i] = a[j] | ||
|
||
for i, j in enumerate(range(half, a.N)): | ||
c_in[i] = a[j] | ||
|
||
b = create_tree(b_in, depth=depth + 1, branch = "A") | ||
c = create_tree(c_in, depth= depth + 1, branch = "B") | ||
d = self.adder(a=b, b=c, prefix = f"{self.prefix}_add{branch}_{depth}") | ||
self.add_component(d) | ||
return d.out | ||
|
||
sumbus = create_tree(self.a,0, "X") | ||
#print(sumbus) | ||
self.out.connect_bus(sumbus ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.