Skip to main content

ArithsGen – tool for arithmetic circuits generation

made-with-python PyPI version fury.io Documentation arXiv

Description

ArithsGen presents an open source tool that enables generation of various arithmetic circuits along with the possibility to export them to various formats which all serve their specific purpose. C language for easy simulation, Verilog for logic synthesis, BLIF for formal verification possibilities and CGP to enable further global optimization.

In contrast to standard HDL languages Python supports

  • Multiple output formats (BLIF, Verilog, C, Integer netlist)
  • Advanced language construction (better configuration, inheritance, etc.)
  • Support of various PDKs (for using library cells as half-adders and full-adders)

Reference

When you use this tool in your work/research, please cite the following article: KLHUFEK Jan and MRAZEK Vojtech. ArithsGen: Arithmetics Circuit Generator for HW Accelerators. In: 2022 25th International Symposium on Design and Diagnostics of Electronic Circuits and Systems (DDECS '22). Prague, 2022, p. 4.

@INPROCEEDINGS{klhufek:DDECS22,
   author = "Jan Klhufek and Vojtech Mrazek",
   title = "ArithsGen: Arithmetics Circuit Generator for HW Accelerators",
   pages = 4,
   booktitle = "2022 25th International Symposium on Design and Diagnostics of Electronic Circuits and Systems (DDECS '22)",
   year = 2022,
   location = "Prague, CZ"
}

Installation

pip install ariths-gen

python << EOF
import ariths_gen
print(ariths_gen.__version__)
EOF

Prebuild circuits

To enable fast work with the circuits, we published pre-build arithmetic circuits in various formats in generated_circuits folder and as a Release.

Usage

python3 generate_test.py
cd test_circuits
ls

Example of generation

#Example generation of Verilog representation of 8-bit unsigned dadda multiplier that uses cla to provide the final product
a = Bus(N=8, prefix="a_bus")
b = Bus(N=8, prefix="b_bus")

u_dadda = UnsignedDaddaMultiplier(a=a, b=b, prefix="h_u_dadda_cla8", unsigned_adder_class_name=UnsignedCarryLookaheadAdder)
u_dadda.get_v_code_hier(open("h_u_dadda_cla8.v", "w"))

Simple arithmetic circuits

See Ripple Carry Adder file for a basic example.

Complex circuits

It is possible to combine some basic circuits to generate more complex circuits (such as MAC). The design can be parametrised (i.e., you can pass UnsignedArraymultiplier as an input parameter).

from ariths_gen.core.arithmetic_circuits.arithmetic_circuit import ArithmeticCircuit
from ariths_gen.core.arithmetic_circuits import GeneralCircuit
from ariths_gen.wire_components import Bus, Wire
from ariths_gen.multi_bit_circuits.adders import UnsignedRippleCarryAdder
from ariths_gen.multi_bit_circuits.multipliers import UnsignedArrayMultiplier, UnsignedDaddaMultiplier
import os

class MAC(GeneralCircuit):
    def __init__(self, a: Bus, b: Bus, r: Bus, prefix: str = "", name: str = "mac", **kwargs):
        super().__init__(prefix=prefix, name=name, out_N=2*a.N+1, inputs=[a, b, r], **kwargs)
        assert a.N == b.N
        assert r.N == 2 * a.N

        self.mul = self.add_component(UnsignedArrayMultiplier(a=a, b=b, prefix=self.prefix, name=f"u_arrmul{a.N}", inner_component=True))
        self.add = self.add_component(UnsignedRippleCarryAdder(a=r, b=self.mul.out, prefix=self.prefix, name=f"u_rca{r.N}", inner_component=True))
        self.out.connect_bus(connecting_bus=self.add.out)

# usage
if __name__ == "__main__":
    os.makedirs("test_circuits/mac", exist_ok=True)
    mymac = MAC(Bus("a", 8), Bus("b", 8), Bus("acc", 16))
    mymac.get_v_code_hier(open("test_circuits/mac/mac_hier.v", "w"))
    mymac.get_c_code_hier(open("test_circuits/mac/mac_hier.c", "w"))
    mymac.get_c_code_flat(open("test_circuits/mac/mac_flat.c", "w"))

Documentation

The automatically generated documentation is available at https://ehw-fit.github.io/ariths-gen/ .

Supporting various PDK kits

When one uses a specific process design kit (PDK), it is not effective to implement half- and full-adders using two-inputs logic gates. These circuits are directly implemented as CMOS modules and are more effective than heuristic optimization by synthesis tool. If you want to use for example FreePDK45 library, you can call a following function before verilog code generating.

from ariths_gen.tools.pdk import set_pdk45_library
set_pdk45_library()

You can add a support of arbitrary PDK (see an example ).

Approximate circuits

Besides the accurate arithmetic circuits you can generate some approximate circuits. Currently we support Broken Array Multiplier and Truncated Multiplier both with fully connected architectures composed from half/full adders as well as faster implementations using carry save multiplier. For more details please follow files in folder approximate_multipliers. You can simply run

python3 generate_axmuls.py

to get the approximate circuits.

The module also supports evaluation of the proposed circuits. You can call the instation as a function (even with numpy-array input) to obtain the results of multiplication operation.

from ariths_gen.wire_components.buses import Bus
from ariths_gen.multi_bit_circuits.approximate_multipliers import UnsignedBrokenArrayMultiplier
a = Bus(N=8, prefix="a_bus")
b = Bus(N=8, prefix="b_bus")

# Create BAM 
bam = UnsignedBrokenArrayMultiplier(a, b, horizontal_cut=4, vertical_cut=4)

print("43 * 84 = ", bam(43, 84), " expected: ", 43 * 84)
# 43 * 84 =  3440  expected:  3612

for all of the possible combinations.

# Evaluate all using b'casting
import numpy as np
import matplotlib.pyplot as plt
va = np.arange(256).reshape(1, -1)
vb = va.reshape(-1, 1)
r = bam(va, vb)

cax = plt.imshow(np.abs(r - (va * vb)))
plt.colorbar(cax)
plt.title("Absolute difference")
plt.xlabel("a")
plt.ylabel("b")

print("Mean average error", np.abs(r - (va * vb)).mean())
# Mean average error 956.25

bam (v=4,h=4)

Formal verification

The yosys_equiv_check.sh script enables to formally check the equivalence of generated Verilog and BLIF representations of the same circuit. It uses the Yosys Open SYnthesis Suite tool by Claire Xenia Wolf. For further information, please visit: https://yosyshq.readthedocs.io/projects/yosys/en/latest/index.html.

Install Yosys

sudo apt-get install yosys

Execute permission

chmod +x yosys_equiv_check.sh

Usage

./yosys_equiv_check.sh -v "verilog_file" -b "blif_file" -m "method" [-H]

For more detailed description of script's usage, use help.

./yosys_equiv_check.sh -h|--help

CGP testing

The chr2c.py script converts the input CGP chromosome generated by ArithsGen to the corresponding C code and prints it to standard output.

Usage

python3 chr2c.py input.chr > output.c

Metadata

Release files for ariths-gen 1.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ariths-gen 1.1.4
File Size Uploaded
ariths_gen-1.1.4.tar.gz 88.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ariths-gen 1.1.4
File Interpreter ABI Platform
ariths_gen-1.1.4-py3-none-any.whl Python 3 none any Details

Total release size: 217.6 kB

Release files / ariths_gen-1.1.4.tar.gz

Download URL ariths_gen-1.1.4.tar.gz
Size 88.8 kB
Tags Source
SHA-256 checksum
How to use checksums
ed8d1441924c3834d52de3ef0e58122f56be24c5f9c907e1b3a3b7ab78f5888c
BLAKE2b-256 checksum
How to use checksums
0316e329084a12371251e65e611bda82f65553ac896d4e304ab9df7209275ff0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 15, 2025.

Transparency log

Release files / ariths_gen-1.1.4-py3-none-any.whl

Download URL ariths_gen-1.1.4-py3-none-any.whl
Size 128.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
88df4b795f468bfd6faba1a40fb2706e113d7107f9da69bec3cf7d302feb619d
BLAKE2b-256 checksum
How to use checksums
7d59d1293196f94bfe7fa47031b7de26bae52400399951bb00e66cc7c27ad5f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 15, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

1.1.4 This release

2 release files

1.1.3

2 release files

1.1.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page