[FFmpeg-devel] Moving if(constant expression) to preprocessor?
Måns Rullgård
mans
Sun Sep 19 18:59:48 CEST 2010
"Axel Holzinger" <aholzinger at gmx.de> writes:
> M?ns Rullg?rd and nice are mutually exclusive. Does this help?
Please refrain from ad hominem attacks.
>> > I got inspired by their BOOST_PP_IF macro.
>> >
>> > It's more generic as it would be needed here. So reduced to what's
>> > needed it could look like this (compiler specific #fdefs removed for
>> > clarity):
>>
>> What compiler-specifics would be involved here? FFmpeg uses
>> only standard C99 features in non-optional code.
You did not answer this question.
> Here is the code that works and that's tested with gcc:
>
> #define AV_COND_IIF(bit, t, f) AV_COND_IIF_I(bit, t, f)
> #define AV_COND_IIF_I(bit, t, f) AV_COND_IIF_II(AV_COND_IIF_ ## bit(t, f))
> #define AV_COND_IIF_II(id) id
> #define AV_COND_IIF_0(t, f) f
> #define AV_COND_IIF_1(t, f) t
> #define AV_COND_BOOL(x) AV_COND_BOOL_I(x)
> #define AV_COND_BOOL_I(x) AV_COND_BOOL_ ## x
> #define AV_COND_BOOL_0 0
> #define AV_COND_BOOL_1 1
> #define AV_COND_IF(cond, t, f) AV_COND_IIF(AV_COND_BOOL(cond), t, f)
That is quite a bit more obfuscated than it needs to be. The
following actually works:
$ cat foo.c
#define AV_IF_0(t, f) f
#define AV_IF_1(t, f) t
#define AV_IF(c, t, f) AV_IF2(c, t, f)
#define AV_IF2(c, t, f) AV_IF_##c(t, f)
#define REGISTER_THING(X, x) { extern int x##_thing; AV_IF(CONFIG_##X, register_thing(&x##_thing),); }
#define CONFIG_FOO 1
#define CONFIG_BAR 0
void init(void) {
REGISTER_THING(FOO, foo);
REGISTER_THING(BAR, bar);
}
$ gcc -E foo.c
void init(void) {
{ extern int foo_thing; register_thing(&foo_thing); };
{ extern int bar_thing; ; };
}
$
Even so, there is no good reason to change the code. We have already
told you why.
> Usage:
> Before:
> #define REGISTER_HWACCEL(X,x) { \
> extern AVHWAccel x##_hwaccel; \
> if(CONFIG_##X##_HWACCEL) av_register_hwaccel(&x##_hwaccel);
> }
>
> With the macro:
> #define REGISTER_HWACCEL(X,x) { \
> extern AVHWAccel x##_hwaccel; \
> AV_COND_IF(CONFIG_##X##_HWACCEL,
> (av_register_hwaccel(&x##_hwaccel)),) ; }
>
>> However, it is far more convoluted than the current code
>
> What?
10 obfuscated macros are more convoluted than none.
>> for no gain,
>
> It makes it possible to completely disable optimisations.
I do not consider that a gain.
> To date the configure script offers to disable optimisations, but
> doesn't do so. One could call this a bug, at least it relies on
> compiler specifics that are not mandated by C99.
We rely on a lot of things outside the scope of C99, for example linkers.
>> and Reimar's comments are valid too.
>
> I think Reimar's comments are beside the point. The only thing the
> compiler does, if a runtime if is used instead of a macro, is that it
> checks the syntax of the call, nothing else.
I consider it a good thing having the syntax checked.
--
M?ns Rullg?rd
mans at mansr.com
More information about the ffmpeg-devel
mailing list