Re: const volatile

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



(Please keep the gcc help list on the copies here - it saves duplication of answers, and means everyone gets to see them.)

On 26/09/2018 17:24, Kalamatee wrote:


On Wed, 26 Sep 2018 at 16:00, David Brown <david@xxxxxxxxxxxxxxx <mailto:david@xxxxxxxxxxxxxxx>> wrote:

    On 26/09/18 14:54, Kalamatee wrote:
     > Hi
     >
     > I am wondering if there is a reason globals marked const volatile
    are put
     > into the data section and not read-only as const should imply?
     >

    "const" says /you/ promise not to change the value from your code.
    "volatile" says that something else might change its value unknown to
    the compiler.

    It is rare to define "const volatile" variables.  It is usually more
    useful to declare pointers to them.  They are used for things like
    read-only hardware registers in microcontrollers, or for data that is
    set from outside a program but is constant from within it (perhaps you
    patch your binaries with a serial number, or a checksum - you could use
    a pointer to const volatile for reading the number in the code).

    The only real use of defined "const volatile" variables I have had is
    for debugging - these would be variables that cannot be changed by the
    code, but you might change them via a debugger.  Usually such things are
    temporary during software development, and it's easier just to make them
    normal volatile variables.

     > I am trying to compile code for m68k AmigaOS which is resident
    using the
     > mathlib functions which need certain constants as volatile to prevent
     > incorrect sequence re-ordering in gcc's optimizations - however
    doing so
     > puts the value in bss data without const.
     >

    That is almost certainly not the best way to get the sequencing you
    want.  Post more details on what you are trying to do, and I am sure
    someone can give you advice.


This comes from the standard sun mathlib code.

Here’s one example where it isn’t for the sequence but to cause a controlled crash in the correct way -:
https://github.com/ezrec/AROS-mirror/blob/ABI_V1/AROS/compiler/stdc/math/e_log.c


I am not an expert in floating point details, especially not for non-finite things, but to my mind this code is clearly unnecessary. The compiler will give the same result for "-two54 / vzero" whether vzero is declared "static const volatile double = 0.0;", or without the volatile, or given just as the literal 0.0 in the code. Without the volatile, the code will be (marginally) shorter and faster as the construction of the infinity will be done at compile-time instead of run-time.

Wait for others to give an opinion before changing this, however.


     > How can we have a global that is both volatile (to prevent incorrect
     > sequencing) and const (so it is read only and stored in read only
    data
     > section) ?

    Why does it matter where the data is stored?


Because .data/.bss cannot exist in the rom image.

That is irrelevant. If the "const volatile" variable has an explicit initialisation, it will be allocated to .data and initialised at startup just like any other initialised data variable. If it is not explicitly initialised, it will be allocated to .bss and zeroed at startup - like any other uninitialised variable.

It is only from /your/ code - code run after "main()" starts - that "const" says you can't write to the variable. The C startup code will initialise it as normal before main() starts.



     >
     > Thanks for any insight that may be provided.
     >




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux