2

What is a good way to express the semantics "this function is always going to return a constant value" in C?

I'm thinking about inline assembly functions that reads read-only registers, and potentially shift and/or masks on them. Clearly, during run time, the function's return value isn't going to change; so the compiler can potentially avoid inlining or calling the function all the time, but instead aim to reuse the value from the first call in a given scope.

const int that_const_value()
{
  return (ro_register >> 16) & 0xff;
}

I could store the value and reuse it. But there could be indirect calls to this function, say, through other macro expansions.

#define that_bit() that_const_value() & 0x1
#define other_bit() that_const_value() & 0x2

...
if (that_bit()) {
  ...
}
...
if (other_bit()) {
  ...
}

Defining the original function as const doesn't seem to cut it, or at least in the examples I tried.

Jeenu
  • 2,109
  • 2
  • 20
  • 27
  • 4
    either inline your function or define a macro instead `#define that_const_value() ((ro_register >> 16) & 0xff)` – Jean-François Fabre Feb 24 '17 at 15:49
  • 2
    Because macros are just text replacement, all you need is just that. A macro, will allow the syntax you want with the result you really want. – Iharob Al Asimi Feb 24 '17 at 15:50
  • 3
    GCC has `__attribute__((__pure__))` for this. – melpomene Feb 24 '17 at 15:52
  • @melpomene Can/should it be used for a function doing some calculation first time it is called and then just storing the result in some static to return it every consecutive call? Edit: I think `pure` is not the right thing to do... – Eugene Sh. Feb 24 '17 at 15:58
  • @EugeneSh. see http://stackoverflow.com/questions/29117836/attribute-const-vs-attribute-pure-in-gnu-c – Garf365 Feb 24 '17 at 16:01
  • @Garf365 Looks like `__attribute__((const))` is actually denoting the mathematically *pure* function, while `__attribute__((pure))` is not. But neither of these are exactly the one OP is asking about. – Eugene Sh. Feb 24 '17 at 16:06
  • @EugeneSh. a function that modifies the value of an object with static duration is not pure. – John Bollinger Feb 24 '17 at 16:07
  • @EugeneSh. As far as I can see, yes, that should be fine. – melpomene Feb 24 '17 at 16:08
  • @JohnBollinger Right, that would be extra effect falling out of the definition of `gcc`s "pure".. – Eugene Sh. Feb 24 '17 at 16:08
  • @melpomene, it does sound like what I was after. Thanks. Maybe it's just my code, but shame that it doesn't seem to make any difference AFAICS from my disassembly. – Jeenu Feb 24 '17 at 16:09
  • @Jeenu These attributes are just "hints" for the compiler, they are not mandating anything. – Eugene Sh. Feb 24 '17 at 16:10
  • Nevertheless, I'm inclined to agree that `__attribute__((const))` is a more appropriate choice if one is going to take this general approach. – John Bollinger Feb 24 '17 at 16:10

1 Answers1

0

I am not 100 percent sure that I understand your question correctly but are you looking for a solution like this:

#define that_const_value ((ro_register >> 16) &0xff)
#define that_bit         (that_const_value & 0x1)
#define other_bit        (that_const_value & 0x2)

This would just 'replace' everything at compille time, so you can do:

if(that_bit)
{
    //Do That
}
if(other_bit)
{
    //Do Other
}
mame98
  • 1,271
  • 12
  • 26