This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Compiler/TMS320F28335: C-Runtime Division, I$$DIV, behavior

Part Number: TMS320F28335
Other Parts Discussed in Thread: TMS570LS3137

Tool/software: TI C/C++ Compiler

Why does I$$DIV(16-bit integer) return -1 for any positive dividend divided by 0 and +1 for any negative dividend divided by 0? I'm told by the testing team this is what occurs.

  • According to Harbison and Steele, the results of division by zero are unpredictable.
  • Robert Mumme said:
    Why does I$$DIV(16-bit integer) return -1 for any positive dividend divided by 0 and +1 for any negative dividend divided by 0?

    This behavior is not explicitly designed.  Thus, there is no rationale for it.

    The source for this divide routine is part of the compiler package.  If you have CCS installed, you can find it in the file i_div28.asm in a directory location similar to:

    C:\ti\ccsv8\tools\compiler\ti-cgt-c2000_18.1.4.LTS\lib\src

    There is no check for divide by 0.  When divide by 0 is executed, it happens to return the result you describe.  Since dividing by 0 not a defined operation, this is acceptable.

    Thanks and regards,

    -George

  • George,

    Thanks for the response.

    I have a safety critical application that uses the same code base for both the TMS320F28385 and TMS570LS3137 processors. The response on the TMS570 using the ARM compiler, though undefined as you state, returns what you would expect ... something close to infinity, which turns out to be max +int and max -int for +int / 0 and -int / 0.

    I have looked at i_div28.asm. It is truly the most optimized code possible. Before I go in and mess up that fine code for my application, I wanted to understand the rationale.

    I'm curious. For the C2000 family of compilers, is there a version of I_div28.asm that does return max +int and max -int for +int / 0 and -int / 0?

     

  • Robert Mumme said:
    For the C2000 family of compilers, is there a version of I_div28.asm that does return max +int and max -int for +int / 0 and -int / 0?

    Unfortunately, no.

    A word of caution ... If you make changes to i_div28.asm, then make that file yet another source file in your program.  This means the linker will always use the divide routine from your file, and not the RTS library.  Heavily comment what you did and why.  If you ever change compiler versions, your build will continue to use your custom divide routine.

    Thanks and regards,

    -George

  • I would think a safety critical routine would test for 0 before division and not rely on unspecified or "unpredictable" behavior.