Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Feb 1, 2026

Coverpoint expressions fail when using multiplication, division, modulo, or bitwise operators. Expressions with constants on the left side (e.g., 1 + self.field) also fail due to missing reverse operator implementations (__radd__, __rmul__, etc.).

@vsc.covergroup
class my_covergroup():
    def __init__(self):
        self.with_sample(dict(a=vsc.uint8_t(), b=vsc.uint8_t()))
        
        # Previously failed: TypeError - unsupported operand type(s)
        self.prod_cp = vsc.coverpoint(self.a * self.b, bins={...})
        self.sum_cp = vsc.coverpoint(1 + self.a + self.b, bins={...})

Changes

type_base class (src/vsc/types.py):

  • Added reverse operators: __radd__, __rsub__, __rmul__, __rtruediv__, __rfloordiv__, __rmod__, __rand__, __ror__, __rxor__, __rlshift__, __rrshift__

expr class (src/vsc/types.py):

  • Added same reverse operators for consistency

ValueScalar class (src/vsc/model/value_scalar.py):

  • Added forward operators: __mul__, __truediv__, __floordiv__, __mod__, __or__, __xor__, __lshift__, __rshift__
  • Added all reverse operators

Test coverage:

  • 12 test cases covering all operators, reverse operations, and complex expressions
Original prompt

This section details on the original issue you should resolve

<issue_title>Coverage on arithmetic expressions fails due to missing magic operator implementations (mul, truediv, radd, etc.)</issue_title>
<issue_description>## Description

When defining coverpoints using arithmetic expressions involving PyVSC fields, certain operations fail with a TypeError. Specifically, multiplication (*) between fields and expressions where a constant appears on the left side (e.g., 1 + self.field) are not supported.

Locally, I was able to resolve multiplication by implementing the __mul__ operator on ValueScalar, but I suspect other operators (e.g. //, %, /, etc.) are also missing for both ValueScalar and other PyVSC internals.

Minimal Reproducible Example

import vsc

@vsc.covergroup
class my_covergroup():
  def __init__(self):
    self.with_sample(dict(
      a = vsc.uint8_t(),
      b = vsc.uint8_t()
    ))
    
    # Case 1: Constant on left (Reverse Add) - Fails
    self.sum_cp = vsc.coverpoint(1 + self.a + self.b, bins={
      "sum" : vsc.bin_array([], 1, 2, 4, 8)
    })
    
    # Case 2: Multiplication - Fails
    self.prod_cp = vsc.coverpoint(self.a * self.b, bins={
      "prod": vsc.bin_array([], 1, 2, 4, 12)
    })

my_cg = my_covergroup()

try:
    my_cg.sample(2, 6)
    vsc.report_coverage()
except Exception as e:
    print("Exception:", type(e), e)

Expected Behavior

PyVSC should support standard arithmetic operators for coverpoint expressions, similar to how basic addition (self.a + self.b) currently works.

  • Multiplication: self.a * self.b should calculate the product without error.

  • Left-side Constants: 1 + self.a should work via __radd__ resolution.

Actual Behavior

Case 1 (Multiplication)

  • Throws TypeError because ValueScalar lacks __mul__:
    Exception: <class 'TypeError'> unsupported operand type(s) for *: 'ValueScalar' and 'ValueScalar'

Case 2 (Left-side Constants)

  • Throws TypeError because field types (uint8_t) lack __radd__.
    Exception: <class 'TypeError'> unsupported operand type(s) for +: 'int' and 'uint8_t'

Impact

This limitation hinders the creation of complex functional coverage models:

  • Users cannot define coverpoints on products, divisions, or complex formulas without implementing custom workarounds (like patching ValueScalar locally).
  • Syntax that is valid in standard Python (e.g., 1 + x) fails unexpectedly in PyVSC contexts.
  • It creates an inconsistent user experience where some operators work (addition) while others (multiplication) do not.

Suggestion

Implement all relevant arithmetic magic methods (__mul__, __rmul__, __radd__, __truediv__, etc.) in ValueScalar and the PyVSC field types to allow AST construction for these expressions.
</issue_description>

<agent_instructions>Create the tests based on the ones shown in this issue, as well as others that make sense to have. Investigate failures and add proper operator overloads. Ensure tests pass</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: mballance <1340805+mballance@users.noreply.github.com>
Copilot AI changed the title [WIP] Add magic operator implementations for arithmetic expressions Add missing arithmetic operator magic methods for coverpoint expressions Feb 1, 2026
Copilot AI requested a review from mballance February 1, 2026 17:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Coverage on arithmetic expressions fails due to missing magic operator implementations (__mul__, __truediv__, __radd__, etc.)

2 participants