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