FabienM - 24 days ago 12
Scala Question

# What does '&' and '%' mean in operators -&, -%, +&, +% in Chisel3?

I'm trying to learn Chisel3 with the GCD example given in official web page. This example use operator named -%, what does that mean ?
It's not explained on Wiki operator page. And Cheatsheet says "substraction" as the normal substraction symbol '-'.

Then what is the difference between simple substraction '-' and percent substraction '-%' ?

Ok, I found the definitions of these functions under chisel3 code :

`````` // TODO: refactor to share documentation with Num or add independent scaladoc
def unary_- : UInt = UInt(0) - this
def unary_-% : UInt = UInt(0) -% this
def +& (other: UInt): UInt = binop(UInt((this.width max other.width) + 1), AddOp, other)
def + (other: UInt): UInt = this +% other
def +% (other: UInt): UInt = (this +& other) tail 1
def -& (other: UInt): UInt = binop(UInt((this.width max other.width) + 1), SubOp, other)
def - (other: UInt): UInt = this -% other
def -% (other: UInt): UInt = (this -& other) tail 1
def * (other: UInt): UInt = binop(UInt(this.width + other.width), TimesOp, other)
def * (other: SInt): SInt = other * this
def / (other: UInt): UInt = binop(UInt(this.width), DivideOp, other)
def % (other: UInt): UInt = binop(UInt(this.width), RemOp, other)

def & (other: UInt): UInt = binop(UInt(this.width max other.width), BitAndOp, other)
def | (other: UInt): UInt = binop(UInt(this.width max other.width), BitOrOp, other)
def ^ (other: UInt): UInt = binop(UInt(this.width max other.width), BitXorOp, other)
``````

With & operator the result of substraction or addition will be the size of the bigest operand plus one bit.
But with % operator the result of operation will be the size of the bigest operand ... as with normal + or -. Then what is the difference between - and -% and between + an +% ?

Answer

My apologies for not including this information on the Wiki operator page, I will add it shortly.

You have hit the nail on the head with your edit: `+&` and `-&` are expanding operators in that the width of the result is equal to the size of the widest operand plus 1. `+%` and `-%` are non-expanding operators in that the width of the result is equal to the widest operand.

`+` just aliases to `+%` while `-` aliases to `-%`.

Source (Stackoverflow)
Comments