Possible solution for min()/max() war
Quoted 11 lines
VDA
#define min2(a,b) ({ \ typeof(a) __a = (a); \ typeof(b) __b = (b); \ if( sizeof(a) != sizeof(b) ) BUG(); \ if( ~(typeof(a))0 > 0 && ~(typeof(b))0 < 0) BUG(); \ if( ~(typeof(a))0 < 0 && ~(typeof(b))0 > 0) BUG(); \ (__a < __b) ? __a : __b; \ })
Quoted 8 lines
#define min3(type,a,b) ({ \
type __a = (a); \
type __b = (b); \
if( sizeof(a) > sizeof(type) ) BUG(); \
if( sizeof(b) > sizeof(type) ) BUG(); \
(__a < __b) ? __a : __b; \
})DesJardin's argument is finely crafted, but does this support it? Is min3 intended to be what Linus was talking about? I usually use m4 when I need conditionals in macros, and I don't use C conditional statements much, but isn't what Linus was saying simply something like...
#define min(type,a,b) (type) a < (type) b ? (type) a : (type) b ;
Looking at the trade-offs should account for the simplicity. Even if my code is wrong, it's about reflective of the required complexity, y/n? The simple form doesn't catch things. That's OK for a default. Maybe that's what you want. If it's simple you know what you have, regardless. "programmer error" is frequently a sign of compiler mis-design.
One reason I'm speaking in support of Linus on this one is that there's something oddly Forth-like to a simple min(type,a,b). No coder nowhere knows simplicity like Chuck Moore. Linux is ripe for a bit of that. The other thing about the 3-arg min thing is that it's rather original, and it amuses me to see Linus get disrespected for a quantum of real creativity in his own forum. Linux is also ripe for a bit of free thinking. What's open source for if all it helps is closed minds?
Rick Hohensee
www.
cLIeNUX
.com
[email protected]
Quoted 9 lines
Quoted 9 lines
#define min2(a,b) ({ \ typeof(a) __a = (a); \ typeof(b) __b = (b); \ if( sizeof(a) != sizeof(b) ) BUG(); \ if( ~(typeof(a))0 > 0 && ~(typeof(b))0 < 0) BUG(); \ if( ~(typeof(a))0 < 0 && ~(typeof(b))0 > 0) BUG(); \ (__a < __b) ? __a : __b; \ })
Quoted 7 lines
Quoted 7 lines
#define min3(type,a,b) ({ \ type __a = (a); \ type __b = (b); \ if( sizeof(a) > sizeof(type) ) BUG(); \ if( sizeof(b) > sizeof(type) ) BUG(); \ (__a < __b) ? __a : __b; \ })
RH> DesJardin's argument is finely crafted, but does this support it? Is min3 RH> intended to be what Linus was talking about?
My min2 requires types to be the same size and sign (after char/short->int promotion - C always does that, seems we cannot control it - run my test program and you'll see). If type is changed elsewhere later, min2 will barf - and this is good for preventing hard to track bugs.
Min3 is more permissive but requires programmer to indicate type explicitly. However, it will bite you when you try convert long to int, so it will catch some bugs too when someone decide to change type of the variable.
Can you make better min? Another min addressing different type of bug? It could be interesting. (We have similar issues with "less than" and the like ops)
Don't know what Linus originally intended. I thought my two cents might be useful.
RH> isn't what Linus was saying simply something like...
RH> #define min(type,a,b) (type) a < (type) b ? (type) a : (type) b;
Looks too dangerous to me. Double evaluation of a and b, no () around them...
RH> Looking at the trade-offs should account for the simplicity.
Ifs inside my min2 and min3 optimize out to nothing. Checked that.
Signature · 4 lines
Best regards, VDA mailto:[email protected] http://port.imtp.ilyichevsk.odessa.ua/vda/