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.

Peculiar code generation for C++ files

Former Member
Former Member

Hi,
There's something peculiar I've observed with C++ files.

I have attached the cpp file for which the compiler generates a small piece of code which initializes ONLY the negative numbers in the array. This piece of code gets executed in the context of pinit, and after the cinit is handled. Now the question is, why such negative numbers are handled diffrerently? The positive numbers are initialized as part of cinit initialization.

I'm using the cgtools_530.

-Somnath 

  • Former Member
    Former Member

    Attached the file.

    dummy_coef.cpp
  • Partial answer...

    A "negative number" is different because it is an expression (unary minus applied to a floating constant) not a constant.

    I don't know offhand why the compiler did not fold that expression into a constant at compile-time.  Probably something to do with having to do with simulating the target ISA's floating point arithmetic at compile-time.  Or maybe there are issues because C++ lets the user define operators (like unary minus).  Somebody (Archaeologist I'm looking at you) will probably be able to tell us.

     

     

  • Paul Knueven said:

    A "negative number" is different because it is an expression (unary minus applied to a floating constant) not a constant.

    That's a fun, confusing bit of trivia about integer constants, but it's not true for floating-point constants.  For float, the sign actually is part of the token, so a negative float constant really is a constant and not an expression.

    I don't know why right off the top of my head C6000 compiler version 5.3.0 did it that way (Paul is probably on the right path), but the compiler doesn't do it that way anymore (since 6.0.0).  5.3.0 is pretty old, I recommend upgrading if possible.

  • Undoing and/or possibly adding to the confusion and in no way contributing to answering the original question...

    In terms of the C standard a "floating constant" does not include a prefix sign, so the negative numbers in the code are indeed expressions.  However, they are  (as per section 6.6 of ISO/IEC 9899:1999, the C99 standard) "arithmetic constant expressions" and therefore allowed in the initialization of a non-local static object.  Similarly a C++ "floating-literal" does not include the sign, but again all we are concerned with are "arithmetic constant expressions" and again they are just that.

    The older compiler was probably tip-toeing around the compile-time conversion of those floating-point expressions since the various standards have some rules about maintaining (execution-time) precision if the compiler chooses to evaluate the expression at compile-time.  Later compilers got bolder and smarter.

    Sorry for the confusion.

     

  • Paul Knueven said:

    In terms of the C standard a "floating constant" does not include a prefix sign, so the negative numbers in the code are indeed expressions.

    You are correct, I'm not sure why I thought otherwise.