Uploaded image for project: 'Embedded Software & Tools'
  1. Embedded Software & Tools
  2. EXT_EP-9621

Simple division expression optimized into incorrect complicated expression

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Medium Medium
    • Code Generation Tools
    • CODEGEN-7141
    • Hide
      MSP430_18.1.0.LTS
      C2000_18.1.0.LTS
      MSP430_20.2.0.LTS
      MSP430_18.12.0.LTS
      C2000_18.12.0.LTS
      C2000_20.2.0.LTS
      Show
      MSP430_18.1.0.LTS C2000_18.1.0.LTS MSP430_20.2.0.LTS MSP430_18.12.0.LTS C2000_18.12.0.LTS C2000_20.2.0.LTS
    • Hide
      C2000_20.2.1.LTS*
      MSP430_20.2.1.LTS*
      C2000_18.12.6.LTS*
      MSP430_18.12.6.LTS*
      C2000_18.1.8.LTS*
      MSP430_18.1.8.LTS*
      Show
      C2000_20.2.1.LTS* MSP430_20.2.1.LTS* C2000_18.12.6.LTS* MSP430_18.12.6.LTS* C2000_18.1.8.LTS* MSP430_18.1.8.LTS*
    • default
    • Use an --opt_for_speed value less than 3.
    • Hide
      When optimisation is enabled (--opt_level=0 or greater) and --opt_for_speed is 3 or greater, the compiler may convert integer divisions by known constants into a sequence of multiplies and shifts, which will produce the same answer but should be faster, at the cost of some code size.

      Converting a 16-bit division typically involves 32-bit operations. On targets with 16-bit ints, the compiler may end up doing one of the intermediate operations in 16 bits when it should use 32 bits, and thus will lose some bits and produce an incorrect expression.
      Show
      When optimisation is enabled (--opt_level=0 or greater) and --opt_for_speed is 3 or greater, the compiler may convert integer divisions by known constants into a sequence of multiplies and shifts, which will produce the same answer but should be faster, at the cost of some code size. Converting a 16-bit division typically involves 32-bit operations. On targets with 16-bit ints, the compiler may end up doing one of the intermediate operations in 16 bits when it should use 32 bits, and thus will lose some bits and produce an incorrect expression.

      The attached file contains this expression ...

      DataRes = (DatastructPtr->b*10)/3;
      

      The structure member b contains 15, so the correct result is 50.

      Build it ...

      % cl2000 -@options.txt try1.c
      

      By looking at the compiler generated comments in the assembly file, you can see this divide expression is changed into ...

      DataRes = ((long)(*DatastructPtr).b*5463L>>14)-((long)((*DatastructPtr).b*10)>>31);
      

      The result of this expression is 5, which is incorrect.

            syncuser TI User
            syncuser TI User
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: