clad icon indicating copy to clipboard operation
clad copied to clipboard

Add support for differentiation of bitwise operations

Open parth-07 opened this issue 4 years ago • 5 comments

We allow differentiation of integral types and thus we should also allow differentiation of bitwise operators.

For example, we should support differentiation of statements such as these:

int i, j;
i <<=3;
i = i | j;
i ^= j;

parth-07 avatar Feb 21 '22 14:02 parth-07

Can you add several examples for this?

vgvassilev avatar Feb 21 '22 15:02 vgvassilev

Can you add several examples for this?

Yes, I have added a few examples in the description.

parth-07 avatar Feb 21 '22 15:02 parth-07

How do we define the differentiation operation for bitwise operators? I understand that left shift and right shift operators can be viewed as multiplying and dividing by 2 respectively and hence, we can replace those instructions with integer multiplication and division instructions and perform differentiation. But what about bitwise and/or/xor?

Also in case we need to implement this, we need to add these features in the "VisitBinaryOperator" function in both the Forward and Reverse mode differentiation classes right?

Nirhar avatar Mar 16 '22 03:03 Nirhar

I don't know how the |, & or ^ will be differentiated, but i would like to try adding support for the >> << and ~ I assume a>>b divides the integer a by 2^b for unsigned integers and ~a is the same as -(a+1) for signed integers. Not sure of how we handle other cases though

Baxi-codes avatar Jan 13 '24 18:01 Baxi-codes

Right and left shifts (>> and <<) should be handled somewhere in VisitBinaryOperator with operator kinds Shr and Shl. Not (~) -- in VisitUnaryOperator.

vgvassilev avatar Jan 13 '24 19:01 vgvassilev