[Bug target/83210] __builtin_mul_overflow() generates suboptimal code when exactly one argument is the constant 2

lh_mouse at 126 dot com gcc-bugzilla@gcc.gnu.org
Wed Nov 29 09:51:00 GMT 2017


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83210

--- Comment #1 from Liu Hao <lh_mouse at 126 dot com> ---
FWIW, it can still be improved when the constant is something other than 2.

For example:

```
bool mul_8_and_check(unsigned *dst, unsigned src){
    return __builtin_mul_overflow(src, 8, dst);
}
```

can be rewritten as:

bool mul_8_and_check(unsigned *dst, unsigned src){
    unsigned res = src << 3;
    *dst = res;
    return (res >> 3) != src; // The result will have been truncated if
                              // dividing the result by 8 does not yield
                              // the original value.
}
```


More information about the Gcc-bugs mailing list