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

            Bug ID: 84814
           Summary: Type of arithmetic expression
           Product: gcc
           Version: 7.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: smal.root at gmail dot com
  Target Milestone: ---

Created attachment 43620
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=43620&action=edit
Project archive

File: main.cpp, line: 20.

When used variant
> if((curtime - LastTime) >= Request)

then we get following assembly code (file: type_test_wo_conv.lss):

> 8000260:      8806            ldrh    r6, [r0, #0]
> 8000262:      7825            ldrb    r5, [r4, #0]
> 8000264:      b292            uxth    r2, r2
> 8000266:      1b92            subs    r2, r2, r6
> 8000268:      42aa            cmp     r2, r5

as you can see "subs" result is not converted to uint16_t, but must. Because
both arguments of subtraction has same type:

"— Otherwise, the integral promotions (4.5) shall be performed on both
operands. Then the following rules shall be applied to the promoted operands:

— If both operands have the same type, no further conversion is needed."

As result - arguments of "cmp" is invalid uint32_t values.

If we use additional "manual" type conversion for expression

> if((uint16_t)(curtime - LastTime) >= Request)

then we get right result:

> 8000260:      8806            ldrh    r6, [r0, #0]
> 8000262:      7825            ldrb    r5, [r4, #0]
> 8000264:      1b92            subs    r2, r2, r6
> 8000266:      b292            uxth    r2, r2
> 8000268:      42aa            cmp     r2, r5

after "subs" result is converted to uint16_t.

Reply via email to