Skip to content

Wide logical operators#5408

Open
maartenboersma wants to merge 1 commit into
chipsalliance:mainfrom
Aurora7913:wide-logicals
Open

Wide logical operators#5408
maartenboersma wants to merge 1 commit into
chipsalliance:mainfrom
Aurora7913:wide-logicals

Conversation

@maartenboersma

Copy link
Copy Markdown
Contributor

Summary

In our RTL code, we frequently qualify an arbitrary Data type with a Bool.

In Chisel we have to write
Mux(enable, value, 0.U) // if value is an UInt
or
Mux(enable, value, 0.U.asTypeOf(value)) // if value is an arbitrary Data type

For conciseness, we like to write enable && value or value && enable. WIth proper signal names, this is intuitive.

Today, Chisel defines the logical && operator only on a pair of Bools.

This pull request adds support for this operator, if one side of the expression is a Bool and the other is subtype of Data. For completeness, we also implement || for OR and ^^ for XOR. The function is intuitive: the Bool is extended to the full width of the other operand, then the logical function is applied on every bit. The result of the expression has the same type as the non-Bool operand.

In existing Chisel projects like ours, designers may have added logical operators to their Data classes, to already allow the short-hand notation. This pull request makes this unnecessary.

Release Notes

Enhance logical operators &&, ||, ^^ to have a Bool on one side of the epxression and wider Data on the other.

@seldridge seldridge added the Feature New feature, will be included in release notes label Jun 10, 2026
@adkian-sifive

Copy link
Copy Markdown
Member

The feature looks good. You'll need to get this working with Scala 3 by moving the impls into scala/ and intf overrides in scala-2/BitsIntf.scala and scala-3/BitsIntf.scala

@jackkoenig

Copy link
Copy Markdown
Contributor

We already support bitwise versions of these operators, &, | and ^ on UInt and Bool is a subtype of UInt that behave the same as your proposed && but quite differently for |/|| and ^/^^1.

I think these are interesting functions but we should give them names instead since they are inconsistent with the existing symbolic methods.

Footnotes

  1. All Chisel operators on UInt or SInt sign extend (0 for UInt) the narrower argument.

@jackkoenig

Copy link
Copy Markdown
Contributor

Discussed in Chisel dev today. We don't define operators on Data the same way we do on UInt, SInt, and Bool, so we resolved to call it AndEach as a special case of Mux (it is after all a half-mux). I guess we could even call it Mux.andEach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Feature New feature, will be included in release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants