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.