On 24/09/2021 11:38, Andrew Pinski via Gcc wrote: > On Fri, Sep 24, 2021 at 2:35 AM Aldy Hernandez <al...@redhat.com> wrote: >> >> >> >> On 9/24/21 11:29 AM, Andrew Pinski wrote: >>> On Fri, Sep 24, 2021 at 1:05 AM Aldy Hernandez via Gcc <gcc@gcc.gnu.org> >>> wrote: >>>>
>>> Huh about c>=0 being always true? the expression, "c++" is really c= >>> (short)(((int)c)+1). So it will definitely wrap over when c is >>> SHRT_MAX. >> >> I see. >> >> Is this only for C++ or does it affect C as well? > > This is standard C code; promotion rules; that is if a type is less > than int, it will be promoted to int if all of the values fit into > int; otherwise it will be promoted to unsigned int. > But remember that for some gcc targets (msp430, AVR, and others), int is 16-bit and the same size as short. The short still gets promoted to int, but it will not longer wrap as SHORT_MAX + 1 is an int overflow. (I've no idea if this is relevant to the code in question, or if that code is only used on specific targets where short is smaller than int.)