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

rsandifo at gcc dot gnu.org <rsandifo at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |rsandifo at gcc dot gnu.org

--- Comment #6 from rsandifo at gcc dot gnu.org <rsandifo at gcc dot gnu.org> 
---
(In reply to ktkachov from comment #4)
> (In reply to Wojciech Migda from comment #2)
> > Should there be at least a warning in case when the shift amount is a
> > compile-time known constant?
> 
> In an ideal world yes. Howevever, most such warnings are done before any
> optimisations are done. To detect this case the compiler would need to
> inline mask into pub and do the constant propagation to figure out the bogus
> shift count.
> 
> The compiler does warn about a more direct use of a negative shift count
> e.g.:
> static
> unsigned long long mask(const unsigned long long sz)
> {
>     auto const rv = (unsigned long long)-1 >> (8 - 9) * 8;
>     return rv;
> }

FWIW, it's also a hard error for:

static constexpr
unsigned long long mask(const unsigned long long sz)
{
    auto const rv = (unsigned long long)-1 >> (8 - sz) * 8;
    return rv;
}

void pub(unsigned long long & out1, unsigned long long & out2, unsigned long
long const sz)
{
    auto constexpr first = mask(9);
    out1 = first;

    auto const second = mask(sz); // sz equals 9 at runtime
    out2 = second;
}

Reply via email to