Re: Macro that expand differently depending on the function calling it.
- From: Peter Nilsson <airia@xxxxxxxxxxx>
- Date: Tue, 30 Oct 2007 20:43:07 -0700
Fabrice <fabrice.gaut...@xxxxxxxxx> wrote:
Lets say I want to define a generic macro to swap bytes
in a integer:
#define swapbytes(x) ...
I have several implementation of the macros, one is
generic C, the other one will be an optimized assembly
version for a specific architecture.
So I do something like that in swapbytes.h:
#ifdef __SOME_IMPLEMENTATION_SPECIFIC_MACRO__
#define swapbytes(x) \
... some implementation defined assembly crap...
#else
#define swapbytes(x) \
... some generic C code ...
#endif
This works well and I can extend this to support many
different implementation dependent optimizations: MIPS,
ARM, x86,...
Now i have an additional issue. In some case, I have two
functions in the same source files that requires two
different implementations of the macro.
So use two macros.
#define swapbytes_special(a,b) ...
#if blah
#define swapbytes(a,b) swapbytes_special(a,b)
#elif ...
#define swapbytes(a,b) ...
#elif ...
#define swapbytes(a,b) ...
....
#endif
void foo() { ...swapbytes_special(a,b); ... }
void bar() { ...swapbytes(a,b); ...}
<snip>
PS: If you think this is off-topic because I mention
mips16 and thumb, dont even bother replying.
If it's off topic, don't bother posting. That way,
everyone wins.
I think the C standard was designed to cope with
implementation specific extensions and issues like
this.
Of course it is. But that doesn't mean that comp.lang.c
is the place to discus those extensions.
--
Peter
.
- References:
- Prev by Date: Re: how to optimize a for loop
- Next by Date: Re: how to optimize a for loop
- Previous by thread: Re: Macro that expand differently depending on the function calling it.
- Next by thread: how to optimize a for loop
- Index(es):
Relevant Pages
|