Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use complex expressions in "vsc.if_then" and “vsc.else_if” constraint? #228

Open
Hsunagwen opened this issue Nov 27, 2024 · 1 comment

Comments

@Hsunagwen
Copy link

Hsunagwen commented Nov 27, 2024

The vsc.if_then constraint does not seem to support complex expressions. Is this problem caused by my use error?And is there a solution to use complex expressions in "vsc.if_then" and “vsc.else_if” constraint?

import vsc

@vsc.randobj
class my_s:
    def __init__(self):
        self.height = vsc.rand_bit_t(14)
        self.width = vsc.rand_bit_t(14)
        self.stride= vsc.rand_bit_t(3)
        self.mode= vsc.rand_bit_t(1)

    @vsc.constraint
    def s_cons(self):
        with vsc.if_then(((self.width / self.stride) % 32 != 0) | ((self.height / self.stride) % 32 != 0)):
            self.mode.inside(vsc.rangelist(1))
        with vsc.else_then:
            self.mode.inside(vsc.rangelist(0))

s = my_s()
s.width = 768
s.height = 800
s.stride = 2
width vsc.raw_mode():
    s.width.rand_mode = False
width vsc.raw_mode():
    s.height.rand_mode = False
width vsc.raw_mode():
    s.stride.rand_mode = False
s.randomize(solve_fail_debug=1)

This will prompt an error:
TypeError: unsupported operand type(s) for /: 'ValueScalar' and 'ValueScalar'

pyvsc version: 0.8.8

@alwilson
Copy link
Contributor

I think this is a real bug. It looks like a number of operator overloads, /*|&%, are missing for ValueScalar, and setting multiple rand vars to rand_mode=False uncovered this. I implemented some of them locally and your example passed.

You could work around this for now by pinning those variables in a with constraint so they stay rand vars for that call. You could probably also use a dynamic constraint to turn the whole set of constraints on and off:

s = my_s()
with s.randomize_with(solve_fail_debug=1):
    s.width  == 768
    s.height == 800
    s.stride == 2

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

No branches or pull requests

2 participants