Missed combining shr and shrx in collatz_f1()
#include <stdint.h>
#include <stdbool.h>
uint64_t
collatz_onlyoddstep (uint64_t oddnum){
return (3 * oddnum + 1);
}
uint64_t
collatz_oddstep (uint64_t oddnum)
{
return (3 * oddnum + 1) / 2;
}
uint64_t
collatz_div2tillodd (uint64_t num)
{
num >>= __builtin_ctzg (num);
return num;
}
uint64_t
collatz_f0 (uint64_t oddnum)
{
oddnum = collatz_onlyoddstep (oddnum);
return collatz_div2tillodd (oddnum);
}
uint64_t
collatz_f1 (uint64_t oddnum)
{
oddnum = collatz_oddstep (oddnum);
return collatz_div2tillodd (oddnum);
}
collatz_f1() uses shr then tzcnt then shrx. collatz_f0() uses only tzcnt then shrx.
collatz_f0() speeds up by 10% when I tested it.
@llvm/issue-subscribers-backend-x86
Author: None (BreadTom)
uint64_t collatz_onlyoddstep (uint64_t oddnum){ return (3 * oddnum + 1); }
uint64_t collatz_oddstep (uint64_t oddnum) { return (3 * oddnum + 1) / 2; }
uint64_t collatz_div2tillodd (uint64_t num) { num >>= __builtin_ctzg (num); return num; }
uint64_t collatz_f0 (uint64_t oddnum) { oddnum = collatz_onlyoddstep (oddnum); return collatz_div2tillodd (oddnum); }
uint64_t collatz_f1 (uint64_t oddnum) { oddnum = collatz_oddstep (oddnum); return collatz_div2tillodd (oddnum); }
collatz_f1() uses shr then tzcnt then shrx.
collatz_f0() uses only tzcnt then shrx.
collatz_f0() speeds up by 10% when I tested it.
</details>
btver2 (BMI1) vs icelake (BMI1+2): https://godbolt.org/z/baaG6Mo6E
shrx had been used to allow for a separate dst reg (and maybe to not touch eflags?) but we don't attempt to relax this back to regular shr - probably because of the dependency on the ECX reg for the shift amount that we might not be able to fold away in regalloc