Re: Macro that expand differently depending on the function calling it.



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

.



Relevant Pages

  • Re: Runtime macros
    ... Since other people have complained that macros in Common Lisp are already first class, I wonder what term they would like us to use for macros that may be passed around as functions may be and used in that way? ... use") the runtime constraints are never violated and I never ... But the minute somebody assigns to source code I have to throw out everything that depended on the constant-sourcecode constraint and fall back on the unoptimized versions. ... And, unfortunately, I haven't got any optimizations working ...
    (comp.lang.lisp)
  • Re: Runtime macros
    ... Since other people have complained that macros in Common ... Lisp are already first class, I wonder what term they would like us to ... the compiler writer, it seems to me that it carries only the same problems as having EVAL in the language. ... you can't make optimizations that depend on one or ...
    (comp.lang.lisp)
  • Re: Improve automatically generated code
    ... And which compiler optimizations will ... optimizations be preferred (e.g. by changing default optimization ... Then make them macros and store them in a .h file, ... On x86 systems, you can generate _asm code "in the raw", without an ...
    (comp.dsp)
  • Re: The lack of a boolean data type in C
    ... The names I chose for my macros make it perfectly clear what ... to do any optimizations specifically designed for bit arrays. ... compiler worry about how best to optimise it. ...
    (comp.lang.c)
  • Re: Word 2007
    ... _Most_ macros from older versions will run with no problems. ... extensions when opening or saving files, ... look at Content Controls as a replacement. ... Word work in Word 2007? ...
    (microsoft.public.word.vba.general)