macros and functions

Jason Moxham J.L.Moxham@maths.soton.ac.uk
Mon, 3 Mar 2003 16:10:25 +0000


On Monday 03 Mar 2003 3:12 pm, delta trinity wrote:
> I think you either have a choice of having a function OR having a macro
> (unless I am mistaken).  A macro is not a function in itself.  It rathe=
r
> insert itself at every places where you invoke it in your code.  So whe=
n
> declaring a macro, no code is actually created (so you can't take a mac=
ro's
> address).  When using a macro, code is generated at every places where =
you
> invoke it.
>
> Well, one way of doing what you may want is to wrap the macro in a
> function, like this:
>
> #define Macro1(Param1, Param2) \
> {                              \
>    //Do some stuff             \
> }
>
> void Func_Macro1(mpz_t Param1, mpz_t Param2)
> {
>    Macro1(Param1, Param2)
> }
>
> So, in your code, you can call Func_Macro1 or simply use the Macro1 mac=
ro.
>
> Eric
>
> >I'm experimenting with adding some new functions to gmp and ideally I
> > would like some of them to be macros , but also availible as function=
s if
> > required
> >(say if address is taken). What existing function (mpn if possible) ha=
s
> >this
> >property , a nice simple one please. Also I would like this to be
> > availible to the library itself (internal) and external (users of the
> > lib) , hopefully
> >under the same name
> >
> >thanks
> >
> >Jason

I was under the impression that you could do something like I describe ,=20

say I have the code fragment

=2E..
mpn_add_1(x , x , xn , v);
=2E..

then when this is compiled , gmp.h and libgmp would conspire to make this=
 a=20
macro call , however if I took the address of mpn_add_1 then I could take=
 the=20
address and use it to get a function call . As I say , I thought gmp allr=
eady=20
had stuff like this ? perhaps it doesn't ?

I know you can do a function mpn_add_1 and a macro MPN_ADD_1=20

A simple example of what I want is a fn like

mpz_sizeinbase(mpz_srcptr Z , unsigned long BASE)

most of the time we call this with BASE=3D2 , so ideally we want a macro =
which=20
can recognize this is a compile time constant and equal to two  and so=20
replace this with ABSIZ(Z)*GMP_NUMB_BITS+some_bits , however when BASE is=
 not=20
a compile time constant equal to two , then you get the general code , an=
d=20
also mpz_sizeinbase() is a function so I can take the address of it ,stor=
e it=20
in a stucture , and call it later (although without the benefits of the=20
special cases above)