[Cfp-interest] tgmath and macro suppression

Jim Thomas jaswthomas at sbcglobal.net
Sun Mar 25 15:34:19 PDT 2012


On Mar 24, 2012, at 6:22 PM, Joel C. Salomon wrote:

> Jim Thomas wrote:
>> 
>> On Mar 23, 2012, at 5:32 AM, Joel C. Salomon wrote:
>> 
>>> Jim Thomas <jaswthomas at sbcglobal.net> wrote:
>>>> 
>>>> What if, instead, it were
>>>> 
>>>>   #define exp(x) _Generic((x), float:expf(x), default:exp(x))
>>>> 
>>>> (I realize this isn't what C11 suggests.)
>>> 
>>> That would be subject to macro expansion of expf()—in fact, there
>>> would be no way to avoid this expansion—but not secondary expansion of
>>> exp(), leading to an inconsistency.
>> 
>> Please explain this in more detail.
> 
> // <math.h>
> double exp(double);
> float expf(float);
> #define exp(x) __builtin_exp(x)
> #define expf(x) __builtin_expf(x)
> 
> // <tgmath.h>, as suggested by Jim
> #define exp(x) _Generic((x), \
>    float: expf(x) /* macro expf(x) expands to __builtin_expf(x) */ \
>    default: exp(x) /* does NOT expand to __builtin_exp(x) */ \
>    )
> 
> (Actually, an “#undef exp” is needed before the macro redefinition.)

Right.

but you could have

   default: __builtin_exp(x) \

> 
> Aside from the inconsistency noted in the comments, can you give me an
> invocation of exp, applied to a float, that will resolve to (expf)(f)
> rather than to __builtin_expf(f)?

Doesn't "#undef expf" after the include of <tgmath.h> and before exp of a float do it?

I don't have a compiler with _Generic to try, but 

#include <stdio.h>
const int a = 1;
#define a 2
#define b a
#undef a
int main() { printf("%d\n", b); return 0; }

prints "1".

> 
>>> I'd suggest that the definition of type-generic macros given is
>>> implicitly intended to suppress macro expansion of expf() et al.
>>> within the <tgmath.h> functions.
>> 
>> Are you referring to the definition in 7.25? I don't recall that being the intention.
> 
> Looking at the Standard, it seems you’re right; the fact that the
> generic-selection example for cbrt in §6.5.1.1 ¶5 evaluates to the
> function name rather than to a function call is artifact of the
> discussion in N1330 & N1404, not an intention to demand macro
> suppression.
> 
> But they should have intended it. :-)

The  intention was that use of tgmath would still allow for good performance, which wouldn't be consistent with disallowing replacement of calls.

-Jim

> 
> —Joel




More information about the Cfp-interest mailing list