csdigit package¶
Submodules¶
csdigit.cli module¶
Command-line interface for CSD conversion.
Provides a CLI wrapper around the csdigit library, allowing conversion between decimal numbers and Canonical Signed Digit (CSD) representation from the terminal.
- Supported conversions:
Decimal float to CSD (–to_csd / -c)
Decimal float to CSD with limited non-zero digits (–to_csdnnz / -f)
CSD string to decimal (–to_decimal / -d)
- Usage:
python -m csdigit.cli –to_csd 28.5 python -m csdigit.cli –to_decimal “+00-00.+” python -m csdigit.cli –to_csdnnz 28.5 –nnz 4
- csdigit.cli.main(args: List[str]) None[source]¶
Wrapper allowing
main()to be called with string arguments in a CLI fashionInstead of returning the value from
main(), it prints the result to thestdoutin a nicely formatted message.- Parameters:
args (List[str]) – command line parameters as list of strings (for example
["--verbose", "42"]).
- csdigit.cli.parse_args(args: List[str]) Namespace[source]¶
Parse command line parameters
- Parameters:
args (List[str]) – command line parameters as list of strings (for example
["--help"]).- Returns:
command line parameters namespace
- Return type:
csdigit.csd module¶
Canonical Signed Digit (CSD) conversion utilities.
Provides functions to convert between decimal numbers and Canonical Signed Digit
(CSD) representation — a signed-digit representation using only +, -,
and 0 symbols with no consecutive non-zero digits.
- Functions:
to_csd: Convert a decimal float to CSD string with specified precision. to_csd_i: Convert an integer to CSD string. to_decimal: Convert a CSD string to a decimal number. to_decimal_using_pow: Convert a CSD string to decimal using pow() (deprecated). to_csdnnz: Convert a decimal float to CSD with limited non-zero digits. to_csdnnz_i: Convert an integer to CSD with limited non-zero digits.
- csdigit.csd.to_csd(decimal_value: float, places: int) str[source]¶
The to_csd function converts a given decimal number to its Canonical Signed Digit (CSD) representation with a specified number of decimal places.
Original author: Harnesser <https://sourceforge.net/projects/pycsd/> License: GPL2
- Parameters:
decimal_value (float) – The decimal_value parameter is a double precision floating-point number that represents the value to be converted to CSD (Canonical Signed Digit) representation
places (int) – The places parameter in the to_csd function represents the number of decimal places to include in the CSD (Canonical Signed Digit) representation of the given decimal_value
- Returns:
The function to_csd returns a string representation of the given decimal_value in Canonical Signed Digit (CSD) format.
Examples
>>> to_csd(28.5, 2) '+00-00.+0' >>> to_csd(-0.5, 2) '0.-0' >>> to_csd(0.0, 2) '0.00' >>> to_csd(0.0, 0) '0.'
- csdigit.csd.to_csd_i(decimal_value: int) str[source]¶
The to_csd_i function converts a given integer into a Canonical Signed Digit (CSD) representation.
Original author: Harnesser <https://sourceforge.net/projects/pycsd/> License: GPL2
- Parameters:
decimal_value (int) – The decimal_value parameter is an integer that represents the decimal value to be converted to CSD format
- Returns:
The function to_csd_i returns a string containing the CSD (Canonical Signed Digit) value.
Examples
>>> to_csd_i(28) '+00-00' >>> to_csd_i(-0) '0' >>> to_csd_i(0) '0'
- csdigit.csd.to_csdnnz(decimal_value: float, nnz: int) str[source]¶
The to_csdnnz function converts a given decimal number into a Canonical Signed Digit (CSD) representation with a specified number of non-zero digits.
Original author: Harnesser <https://sourceforge.net/projects/pycsd/> License: GPL2
- Parameters:
decimal_value (float) – The decimal_value parameter is a double precision floating-point number that represents the input value for conversion to CSD (Canonic Signed Digit) fixed-point representation
nnz (int) – The parameter nnz stands for “number of non-zero bits”. It represents the maximum number of non-zero bits allowed in the output CSD (Canonical Signed Digit) representation of the given decimal_value
- Returns:
The function to_csdnnz returns a string representation of the given decimal_value in Canonical Signed Digit (CSD) format.
Examples
>>> to_csdnnz(28.5, 4) '+00-00.+' >>> to_csdnnz(-0.5, 4) '0.-' >>> to_csdnnz(0.0, 4) '0' >>> to_csdnnz(0.5, 4) '0.+'
- csdigit.csd.to_csdnnz_i(decimal_value: int, nnz: int) str[source]¶
The to_csdnnz_i function converts a given integer into a Canonical Signed Digit (CSD) representation with a specified number of non-zero digits.
Original author: Harnesser <https://sourceforge.net/projects/pycsd/> License: GPL2
- Parameters:
decimal_value (int) – The decimal_value parameter is an integer that represents the decimal value to be converted to CSD format
nnz (int) – The parameter nnz stands for “number of non-zero bits”. It represents the maximum number of non-zero bits allowed in the output CSD (Canonical Signed Digit) representation of the given decimal_value
- Returns:
The function to_csdnnz_i returns a string representation of the given decimal_value in Canonical Signed Digit (CSD) format.
Examples
>>> to_csdnnz_i(28, 4) '+00-00' >>> to_csdnnz_i(-0, 4) '0' >>> to_csdnnz_i(0, 4) '0' >>> to_csdnnz_i(37, 2) '+00+00' >>> to_csdnnz_i(158, 2) '+0+00000'
- csdigit.csd.to_decimal(csd: str) float[source]¶
Convert CSD string to decimal number.
Original author: Harnesser <https://sourceforge.net/projects/pycsd/> License: GPL2
- Parameters:
csd (str) – The csd parameter is a string containing the CSD (Canonical Signed Digit) value that we want to convert to a decimal number
Examples
>>> to_decimal("+00-00.+") 28.5 >>> to_decimal("0.-") -0.5 >>> to_decimal("0") 0 >>> to_decimal("0.0") 0.0 >>> to_decimal("0.+") 0.5
- csdigit.csd.to_decimal_using_pow(csd: str) float[source]¶
The to_decimal_using_pow function converts a Canonical Signed Digit (CSD) string to a decimal number using the pow function.
Deprecated since version 0.1.0: Use to_decimal instead.
Original author: Harnesser <https://sourceforge.net/projects/pycsd/> License: GPL2
- Parameters:
csd (str) – The csd parameter is a string containing the CSD (Canonical Signed Digit) value
Examples
>>> to_decimal_using_pow("+00-00.+") 28.5 >>> to_decimal_using_pow("0.-") -0.5 >>> to_decimal_using_pow("0") 0.0 >>> to_decimal_using_pow("0.0") 0.0 >>> to_decimal_using_pow("0.+") 0.5
csdigit.csd_multiplier module¶
CSD Multiplier — Verilog Code Generation
Translates a Canonical Signed Digit (CSD) string into a synthesizable Verilog module that implements constant multiplication using only shift-and-add/subtract operations. When the CSD string contains repeated patterns, LCSRe (Longest Common Substring with Repeated Elements) is used to share hardware via a sub-expression wire.
- csdigit.csd_multiplier.generate_csd_multiplier(csd: str, input_width: int, max_power: int) str[source]¶
Generate Verilog code for a CSD multiplier module with LCSRe optimization.
When the CSD string contains repeated non-overlapping patterns (detected via the longest_repeated_substring algorithm), the generated Verilog factors out a shared sub-expression wire — reducing the number of adders.
- Parameters:
csd – CSD string (e.g., “+00-00+0+”)
input_width – Bit width of the input signal x
max_power – Highest power of two in the CSD (must be len(csd)-1)
- Returns:
Verilog module code as a string
- Raises:
ValueError – If csd length doesn’t match max_power+1 or the string contains characters other than ‘+’, ‘-’, ‘0’
- csdigit.csd_multiplier.generate_csd_multipliers(coeffs: list[tuple[str, str, int, int]], module_name: str = 'csd_filter') str[source]¶
Generate a Verilog module with multiple CSD multipliers and cross-CSE.
When the same CSD substring appears in multiple coefficients, a shared sub-expression wire is created — reducing total adder count across the entire filter.
All coefficients must share the same
input_widthandmax_powerso that the same bit position encodes the same power of two across all multipliers. If a coefficient is narrower, pad its CSD with leading'0'characters.- Parameters:
coeffs – List of (output_name, csd_str, input_width, max_power) tuples. All entries must share the same input_width and max_power.
module_name – Name for the generated Verilog module.
- Returns:
Verilog module code as a string
csdigit.lcsre module¶
Longest Repeated Non-Overlapping Substring (LCSRe) finder.
Finds the longest substring that appears at least twice in the input string without overlapping occurrences. Uses dynamic programming with a space-optimized 2-row DP table (O(n) memory instead of O(nu00b2)).
This is used internally by the CSD multiplier generator to detect repeated patterns in CSD strings, enabling hardware sharing via sub-expression wires.
- csdigit.lcsre.longest_repeated_substring(csd_string: str) str[source]¶
Longest repeated non-overlapping substring
The longest_repeated_substring function takes a string as input and returns the longest repeated substring in the string.
- Parameters:
csd_string (str) – The parameter csd_string is a string containing the CSD value
- Returns:
The function longest_repeated_substring returns a string, which is the longest repeated substring in the given input string csd_string.
Examples
>>> longest_repeated_substring("+-00+-00+-00+-0") '+-00+-0'