Optimize (x*(x+1)) & 1 ?
MoePus opened this issue · comments
Assuming the multiplication operator has been implemented
Expression (x + x) & 1
can be transformed to (x * 2) & 1
.
After x * 2
, the lowest known bit is 0(with lowest unknown 0), so (x * 2) & 1
should be 0.
But what about (x*(x+1)) & 1
? Multiplying odd numbers and even numbers must be even numbers.
But the lowest bit would be unknown after a multiply operation.
Do this kind of operation need an extra pass?
You can add it to the directives list, something like { (A*(A+1))&1, 0 }
should work, ideally something more generic would be better but can't think of any other properties.
thank you. make it
{ (A * (A + B)) & 1, __iff(urem(B,2) == 1, 0) },
{ (A * (A - B)) & 1, __iff(urem(B,2) == 1, 0) },
and here is my implementation of operator multiply
and umultiply
MoePus@9cc1a5c
if all thing is right.you can merge it.
{ (A * (A + U)) & 1, __iff(U&1,0) },
{ (A * (A - U)) & 1, __iff(U&1,0) },
Should be the fastest option, if you send a PR i'll merge it, looks good to me!
i finally made it this
{ (A * (A + U)), __iff(U & 1, (A * (A + U)) & 0xfffffffffffffffe) },
{ (A * (A - U)), __iff(U & 1, (A * (A + U)) & 0xfffffffffffffffe) },
since the compiled code is not always div
or and reg, 1
,it may be test reg, 1