[Bug c++/93070] New: std::__lg (and all functions that use it) generates suboptimal code
gcc at mailinator dot com
gcc-bugzilla@gcc.gnu.org
Wed Dec 25 06:04:00 GMT 2019
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93070
Bug ID: 93070
Summary: std::__lg (and all functions that use it) generates
suboptimal code
Product: gcc
Version: 9.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: gcc at mailinator dot com
Target Milestone: ---
On Godbolt, the following code:
```
#include<algorithm>
int f(int x){
return std::__lg(x);
}
int g(int x){
return 31-__builtin_clz(x);
}
int h(int x){
return 31^__builtin_clz(x);
}
```
compiles to:
```
f(int):
bsr edi, edi
mov eax, 31
xor edi, 31
sub eax, edi
ret
g(int):
bsr edi, edi
mov eax, 31
xor edi, 31
sub eax, edi
ret
h(int):
bsr eax, edi
ret
```
Only `h` are optimal. `f` and `g` has a tiny slowdown with redundant `31-` and
`31^`.
Suggestion: The better option would be to recognize `(power of 2 - 1) ^ x` and
`(power of 2 - 1) - x` as equal, but otherwise it's possible to just change the
`__lg` implementation to use `^` instead of `-` when the size is a power of 2.
More information about the Gcc-bugs
mailing list