Re: [IDEA+RFC] Possible solution for min()/max() war

VDA (VDA@port.imtp.ilyichevsk.odessa.ua)
Tue, 4 Sep 2001 12:09:34 +0300


A test program.
min2 performs a very strict checking.
min3 gives you full control, but checks for too small target type.
Do anybody see any flaws? Any improvements?
For compiler folks: Why GCC compiles ...f(1);f(1);f(1)... to
...
pushl $1
call f
addl $32,%esp <-- My grandma optimizes better
addl $-12,%esp <-- ?
pushl $1
call f
addl $16,%esp <-- ?
addl $-12,%esp <-- ?
...
--------------------------------------------------------------------
#include <stdio.h>

#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; \
})

#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; \
})

#define BUG() printf("BUG at %i\n",__LINE__)
#define llong long long

// Uncomment {} for compile, comment for gcc -S -O2 optimizer test
void f(int v);
// void f(int v) {}

int main() {
signed char sc=1; unsigned char uc=1;
signed short ss=1; unsigned short us=1;
signed int si=1; unsigned int ui=1;
signed long sl=1; unsigned long ul=1;
signed llong su=1; unsigned llong uu=1;

f(min2(sc,sc)); // not a BUG
f(min2(sc,uc)); // not a BUG: (typeof(x))0 first expanded to signed int
f(1); // optimizer test: all the mins must be optimized to
f(1); // f(1) so do gcc -S -O2 and inspect .s file
f(min2(ss,ss)); // not a BUG
f(min2(ss,us)); // not a BUG: (typeof(x))0 first expanded to signed int
f(min2(si,si)); // not a BUG
f(min2(si,ui)); // BUG: signedness mismatch
f(min2(sl,sl)); // not a BUG
f(min2(sl,ul)); // BUG: signedness mismatch
f(min2(su,su)); // not a BUG
f(min2(su,uu)); // BUG: signedness mismatch
f(min2(sc,ss)); // BUG: size mismatch
f(min3(unsigned char, sc,uc)); // not a BUG
f(min3(int, sc,ss)); // not a BUG
f(min3(unsigned llong,ss,uu)); // not a BUG
f(min3(unsigned long, ss,uu)); // BUG: target type is too small

return 0;
}
-------------------------------------------------------------
Best regards, VDA
mailto:VDA@port.imtp.ilyichevsk.odessa.ua
http://port.imtp.ilyichevsk.odessa.ua/vda/

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/