Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Returning value from a macro which prints some debug messages alongwith expression evaluation

Reply
Thread Tools

Returning value from a macro which prints some debug messages alongwith expression evaluation

 
 
Srinivas Mudireddy
Guest
Posts: n/a
 
      10-01-2009
Hi,
I am running in to a problem and let me paraphrase it with a fake use
case.

Say, I have a macro like
#define MIN(x, y) ((x) < (y) ? (x) : (y))

Now how do I still return a value from this macro if I want to add
some debug messages? For example, I want to rewrite above macro as

#define MIN(x, y) \
{ \
if ((x) < (y)) \
{ \
printf("MIN is first arg"); \
} \
else \
{ \
printf("MIN is second arg"); \
} \
//Need something here which would return min value \
}

I could implement this macro as an inline function but we have ~15
such macros in the data path which get executed for each packet. We
are worried that performance will be impacted if compiler doesn't
actually make the functions inline.

Please let me know if there is a way to solve this problem.

Thx,
Sri
 
Reply With Quote
 
 
 
 
Antoninus Twink
Guest
Posts: n/a
 
      10-01-2009
On 1 Oct 2009 at 20:25, Srinivas Mudireddy wrote:
> Now how do I still return a value from this macro if I want to add
> some debug messages? For example, I want to rewrite above macro as
>
> #define MIN(x, y) \
> { \
> if ((x) < (y)) \
> { \
> printf("MIN is first arg"); \
> } \
> else \
> { \
> printf("MIN is second arg"); \
> } \
> }


Try using the comma operator:

#define MIN(x,y) ((x) < (y) ? printf("MIN is first arg"), (x) : \
printf("MIN is second arg"), (y))

 
Reply With Quote
 
 
 
 
Seebs
Guest
Posts: n/a
 
      10-01-2009
On 2009-10-01, Srinivas Mudireddy <(E-Mail Removed)> wrote:
> Say, I have a macro like
> #define MIN(x, y) ((x) < (y) ? (x) : (y))
>
> Now how do I still return a value from this macro if I want to add
> some debug messages? For example, I want to rewrite above macro as


Usually, this is the time to do inline functions.

> #define MIN(x, y) \
> { \
> if ((x) < (y)) \
> { \
> printf("MIN is first arg"); \
> } \
> else \
> { \
> printf("MIN is second arg"); \
> } \
> //Need something here which would return min value \
> }


You really can't do this, although.

#define MIN(x, y) ((x) < (y) ? ((printf("MIN is first arg\n"), (x)) : ((printf("MIN is second arg\n"), (y)))

In short, don't try to embed statements in a macro, but do be aware that
function calls are just expressions, and the comma operator is your friend.

> I could implement this macro as an inline function but we have ~15
> such macros in the data path which get executed for each packet. We
> are worried that performance will be impacted if compiler doesn't
> actually make the functions inline.


> Please let me know if there is a way to solve this problem.


Oh, come ON.

You're talking about *PRINTING DIAGNOSTICS*. The chances that the
function call overhead is going to be noticeable compared to the time
I/O takes are very small.

Here is my suggestion: Just do it with inline functions, because they
will almost certainly work and do what you want, and will be clear and
understandable. Then measure your performance. If it's not good enough,
THEN start looking at clever hacks.

But your first choice should probably be to think very hard about whether
calling printf at all is even the right choice. Consider:

extern char *queued_diagnostics[MAX_DIAGNOSTICS_PER_PACKET];
extern int queued_diagnostic_index;
/* Usenet posts want terser names than real code sometimes */
#define qd queued_diagnostics
#define qdi queued_diagnostics_index

#define MIN(x,y) ((x) < (y) ? (qd[qdi++] = "MIN: first is smaller", (x)) : (qd[qdi++] = "MIN: second is smaller", (y)))

/* now... */
MIN(2,3);
for (int i = 0; i < qdi; ++i) {
printf("%s\n", qd[i]);
}

Now, if you do this, you have to watch out -- you can't call MIN twice without
intervening sequence points, because the qdi++ could bite you.

The rationale of something like this is that you might want to do all
your operations first, then do the output later -- or possibly in another
thread, using some kind of mutex to check on when the processing thread
is busy doing something else.

Which is to say: If you seriously need to worry about the performance
difference of inline functions, don't call printf() inside a macro, and
try bringing up your issues in a newsgroup specific to your target platform,
because the right way to do this in Windows might be very different from
the right way to do it in Unix.

-s
--
Copyright 2009, all wrongs reversed. Peter Seebach / http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      10-01-2009
Antoninus Twink <(E-Mail Removed)> wrote:
> Srinivas Mudireddy wrote:
> > Now how do I still return a value from this macro if I
> > want to add some debug messages? For example, I want to
> > rewrite above macro as
> >
> > #define MIN(x, y) \
> > { \
> > * if ((x) < (y)) \
> > * { \
> > * * printf("MIN is first arg"); \
> > * } \
> > * else \
> > * { \
> > * * printf("MIN is second arg"); \
> > * } \
> > }

>
> Try using the comma operator:
>
> #define MIN(x,y) ((x) < (y) ? printf("MIN is first arg"), (x) : \
> * *printf("MIN is second arg"), (y))


The comma operator has lower precedence than the conditional
operator. Your macro will return (y) each time because it's
parsed as (<expr>, (y)).

Try...

#define MIN(x, y) \
( (x) < (y) ? puts("MIN is first arg"), (x) \
: (puts("MIN is second arg"), (y)) )

--
Peter
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
to get macro name from macro value sounak C Programming 17 11-22-2005 11:12 PM
Download HTML alongwith Images Snig ASP .Net 0 06-09-2005 09:06 AM
[EVALUATION] - E03 - jamLang Evaluation Case Applied to Python Ilias Lazaridis Python 2 04-24-2005 05:29 PM
[EVALUATION] - E04 - jamPersist Evaluation Case Applied to Ruby Ilias Lazaridis Ruby 18 04-09-2005 04:45 PM
[EVALUATION] - E03 - jamLang Evaluation Case Applied to Ruby Ilias Lazaridis Ruby 74 04-04-2005 05:29 PM



Advertisments