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: MISRA 2004 Warning for rule 10.2 in _I(0.0) format data

Tool/software: TI C/C++ Compiler

I selected the MISRA 2004 in ccs6.0,when i build the project,too many warning happed;

the _IQ(0.0) format data is not ineligible to the MISRA rule 10.2;

#define _IQ24(A)  (long)((A)*16777216.0L)

how i modified to pass the MISRA rule 10.2?

  • In this particular instance, you should not get a MISRA warning.  In the macro, the large constant is of type long double.  The MISRA diagnostic incorrectly complains that it is implicitly cast to a less precise type.  That does not occur.  Instead, the macro argument A is implicitly converted to long double, then the multiply occurs, then the result of that multiply is explicitly cast to long.  I filed CODEGEN-3794 in the SDOWP system to have this investigated.  You are welcome to follow it with the SDOWP link below in my signature.

    user4449264 said:
    how i modified to pass the MISRA rule 10.2?

    One workaround is to invoke the macro with a long double argument ...

    // L suffix on constant means type is long double
    variable = _IQ24(1.234L);
    

    Another workaround is to change the macro so that argument A is of type long double ...

    #define _IQ24(A) (long)((long double)(A) * 16777216.0L)
    

    Thanks and regards,

    -George

  • Thank you very much!

    I modified .#define _IQ24(A) (long)((long double)(A) * 16777216.0L)

    but in this case,I get a MISRA warning MISRA 10.4!

    float_t  variable ;

    _IQ(variable);

    how i modified to pass the MISRA rule 10.4?

  • user4449264 said:
    how i modified to pass the MISRA rule 10.4?

    You can't.  For those following along, here is the diagnostic for MISRA 10.4 ...

    "file.c", line 6: warning: (MISRA-C:2004 10.4/R) The value of a complex
              expression of floating type shall only be cast to a floating type
              that is narrower or of the same size
         return (long)((long double)(arg) * 16777216.0L);
                      ^

    The macro _IQ24 intentionally changes a long double expression to a long.  This is violation of MISRA 10.4.  You get away with it when the argument to the macro is a constant.  In that case, the entire expression is computed down to a long constant before any MISRA checks occur.  That compile time computation does not happen when the argument to the macro is a variable.  Then the MISRA check does take place, and this is a violation.  

    I suspect the IQmath code cannot be changed to build MISRA clean.

    Thanks and regards,

    -George