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

Martin Liška <marxin at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |INVALID
                 CC|                            |marxin at gcc dot gnu.org
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #1 from Martin Liška <marxin at gcc dot gnu.org> ---
Note that each switch contains an implicit default label which is missing in
your code. So your function invokes an undefined behavior (when you use the
return value of the function) for values different from 0 and 1.
I recommend:

int f1(unsigned x)
{
    switch (x)
    {
        case 0:
            return 1;
        case 1:
            return 2;
        default:
            __builtin_unreachable ();
    }
}

which is fully optimized to:

        cmpl    $1, %edi
        movl    $1, %eax
        sbbl    $-1, %eax
        ret

Reply via email to