3 ms·
Yeah, this sort of conversion is pretty common for the core of arithmetic/range decoders, and I definitely don't expect a compiler to be capable of the sort of
by brigade 5y ago
Yeah, this sort of conversion is pretty common for the core of arithmetic/range decoders, and I definitely don't expect a compiler to be capable of the sort of transformations needed to convert the branchy form to branchless.
Though I'd expect compilers to be at least 90% as good with the ternary equivalent to the asm, e.g.:
tmp = code < bound ? ~0 : 0;
range = code < bound ? bound : range;
compiles tmp to xor -> setl -> neg (clang) or setge -> movzx -> sub (gcc) instead of sbb, but on arm64 they use just one csetm (clang) or csinv (gcc) which is basically optimal, and in all cases the comparison is reused for the cmov.
https://godbolt.org/z/jPbo3qc1Y https://godbolt.org/z/jPbo3qc1Y