3 ms·
You can get close to the performance of a cmov instruction by generating a pair of all-1s and all-0s values: a=c, b=c-1, and a result z=a&x|b&y. Gcc will not u
by ncmncm 7y ago
You can get close to the performance of a cmov instruction by generating a pair of all-1s and all-0s values: a=c, b=c-1, and a result z=a&x|b&y.
Gcc will not under any circumstances produce two cmov instructions in a basic block, so that is your only alternative without dropping to asm. Clang is happy to produce two adjacent cmov instructions. Usually your ALUs are not otherwise so engaged as to make the number of operations involved costly.
- ncmncm 7y agoI should add that Clang is happy to turn c&x|(c-1)&y into cmov.
- deleted 7y ago[deleted]
- tom_mellior 7y agoI don't get this. If c can be 0 or 1, then a can be 0 or 1, so it can be all-0s but not all-1s. b can be -1 or 0, so that part is fine. Can you show actual C or C++ code for a function like this: int select(int x, int y, bool condition) { ... }
- ncmncm 7y agoSorry, brain freeze. See https://news.ycombinator.com/item?id=23237663 https://news.ycombinator.com/item?id=23237663 The bitwise thing is correctly -c&x|(c-1)&y.
- tom_mellior 7y agoOh, yes, I should have realized that -c is enough to transform from 0/1 to 0/-1. Thanks!