Hi,
I want to define a macro in C like this, but I wonder if these is too obscure and I'm starting to get too nasty.
This is the macro I want to use: - #define CompareXYAndDoActions(X, Y, greaterAction, equalAction, lowerAction) \
-
{ \
-
if (X>Y) { \
-
greaterAction; \
-
} \
-
if (X==Y) { \
-
equalAction; \
-
} \
-
if (X<Y) { \
-
lowerAction; \
-
} \
-
}
Later I plan to call it like this: - CompareXYAndDoActions(X, Y,
-
{
-
Action1;
-
},
-
{
-
Action2;
-
},
-
{
-
Action3;
-
}
-
);
So, do you think the call later is readable? Or does it make you want to puke?
Any idea will be greatly appreciated.
TIA & Regards ...
3 1739 JosAH 11,448
Recognized Expert MVP
Hi,
I want to define a macro in C like this, but I wonder if these is too obscure and I'm starting to get too nasty.
This is the macro I want to use: - #define CompareXYAndDoActions(X, Y, greaterAction, equalAction, lowerAction) \
-
{ \
-
if (X>Y) { \
-
greaterAction; \
-
} \
-
if (X==Y) { \
-
equalAction; \
-
} \
-
if (X<Y) { \
-
lowerAction; \
-
} \
-
}
Later I plan to call it like this: - CompareXYAndDoActions(X, Y,
-
{
-
Action1;
-
},
-
{
-
Action2;
-
},
-
{
-
Action3;
-
}
-
);
So, do you think the call later is readable? Or does it make you want to puke?
Any idea will be greatly appreciated.
TIA & Regards ...
That macro stinks, both syntactically and semantically; have a look: -
if (<some_other_condition>)
-
CompareXYAndDoActions(X, Y,
-
{
-
Action1;
-
},
-
{
-
Action2;
-
},
-
{
-
Action3;
-
}
-
); // <--- note this semi colon
-
else
-
<something_else>
-
Semantically it doesn't work either; better put parentheses around the X and Y
macro parameters. But, even better, if you want to use Fortran you know where
to find it.
kind regards,
Jos
That macro stinks, both syntactically and semantically.
Semantically it doesn't work either; better put parentheses around the X and Y macro parameters. But, even better, if you want to use Fortran you know where to find it.
kind regards,
Jos
Thanks Jos,
I thought I was getting too nasty in here, but to get that from the Chief Editor is a clear indication I've crossed the line by too much :-(
The problem is that I had already optimized the algorithm at the higher levels, so every extra performance gain I can get in here is very important. And I wanted also to be able to decide later how to do the comparisons, I think I've read once that some Intel Processor (I guess Pentium) tests at the same time a number for greater than zero, equal than zero or less than zero, so wanted to be able to change later the conditions for (X-Y>0), (X-Y==0) and (X-Y<0) to do one comparison instead of 3.
No other idea about how to do this? :-(
TIA & Regards ...
JosAH 11,448
Recognized Expert MVP
Thanks Jos,
I thought I was getting too nasty in here, but to get that from the Chief Editor is a clear indication I've crossed the line by too much :-(
Yep, implementing macros that seem to add a bit of different statement syntax
is almost never a good idea. Simply write out your statements and leave the
optimization to the compiler (esp. the code generator): it is much better at it.
The problem is that I had already optimized the algorithm at the higher levels, so every extra performance gain I can get in here is very important. And I wanted also to be able to decide later how to do the comparisons, I think I've read once that some Intel Processor (I guess Pentium) tests at the same time a number for greater than zero, equal than zero or less than zero, so wanted to be able to change later the conditions for (X-Y>0), (X-Y==0) and (X-Y<0) to do one comparison instead of 3.
No other idea about how to do this? :-(
TIA & Regards ...
Those 'micro optimizations' should only be applied (if ever) at the end of the
development phase: first make the entire thing work using an efficient algorithm.
As I wrote above: a compiler is a good code optimizer. If you want to use those
very low level processor trickery-dickeries it's even better to write a (small?)
assembly function that uses it and can be called from the rest of your code.
Your particular example can be implemented in a 'sign' macro as follows: -
#define sign(x, y) (((y) > (x)) - ((x) > (y)))
-
This little monster has result -1, 0 or 1 according to x > y, x == y or x < y, you
can even use that number as an index in a function pointer array if you are
absolutely possitively definitely sure that all three situations occur frequently
in your code; otherwise simply use a simple if statement for the comparison.
An optimizing compiler can even break the code for that thing down to one single
comparison and a bit of status flags (bits) fiddling.
But as a rule of thumb: if you feel the urge to optimize: optimize late.
kind regards,
Jos
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: qwweeeit |
last post by:
Hi all,
when I moved from Windows to Linux I choosed Python as my language of
reference and as GUI, Qt (not much investigated up to now).
Till now I didn't regret but one thing:
Python can't act as a macro language and so you are obliged to revert
to Windows to use programs like AutoIt, Macro Express or Macro
Scheduler or even the macro features of Office with VBA.
Claudio Grondi expressed almost the same ideas in answering to a post:...
|
by: arut |
last post by:
I would like to know when a const should be used and when a #define is necessary
in a program using a constant. What are the pros and cons?
|
by: Andrew |
last post by:
Hello,
Is it bad practice to use a function as a parameter of a macro? Will
the compiler create a full copy of the function's machine code for
each invocation of the macro? Or does it create some type of pointer
to the function? Also is there any benefit from placing a return at
the end of a function which returns void? Thanks for any advice!
|
by: sathyashrayan |
last post by:
/*
** PLURALTX.C - How to print proper plurals
** public domain - original algorithm by Bob Stout
*/
#include <stdio.h>
#define plural_text(n) &"s"
|
by: krbyxtrm |
last post by:
Hi
is there a way to 'manage' function execution using macros?
#define MY_CALL_MACRO(MacroName)
{ g_MacroStack.push_back(MacroName);
<some code here...>}
such that when i use the macro like this:
MY_CALL_MACRO("MyMacroName")
| |
by: Francois Grieu |
last post by:
Consider this macro
// check if x, assumed of type unsigned char, is in range
#define ISVALID(x) ((x)>=0x20 && (x)<=0x7E)
Of course, this can't be safely used as in
if (ISVALID(*p++)) foo();
where p is a pointer ot unsigned char.
|
by: NickP |
last post by:
Hi there,
I am trying to do a reflection only load of an assembly, within a new
appdomain, within a macro.
i.e.
create new app domain
instantiate class
run class
|
by: talkaboutquality |
last post by:
Need to define a macro as, say,
#ifdef NEED_FUNCTION
foo(x)
#else
#endif
|
by: todWulff |
last post by:
Good day folks.
Let me open with the statement that I am not a C++/C programmer. The environment that I am programming in is ARMbasic, an embedded BASIC targeted toward ARM-based micro-controllers. So why am I posting herein? Well, the ARMbasic environment makes use of a tool borrowed from your folk's environment - CPP. The build details are:
C:\Program Files\Coridium>cpp --version
cpp (GCC) 3.2.3 (mingw special 20030504-1)
Copyright...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |