lkml.org 
[lkml]   [2001]   [Aug]   [30]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
    /
    From
    SubjectRe: [IDEA+RFC] Possible solution for min()/max() war
    Date
    "A month of sundays ago Patrick J. LoPresti wrote:"
    > This is a MUCH nicer solution. max() is a well-defined mathematical
    > concept; it is simply the larger of its two arguments, period. It is
    > C's *promotion* rules that kill you, especially signed->unsigned
    > promotion. So just forbid them, at least when they implicit.
    >
    > You can argue about whether the "differing sizes" case should be a
    > BUG(), since the output will still be mathematically correct. It

    To give you all something definite to look at, here's some test code:

    // standard good 'ol faithful version
    #define __MIN(x,y) ({\
    typeof(x) _x = x; \
    typeof(y) _y = y; \
    _x < _y ? _x : _y ; \
    })

    // possible implemetation with type sanity checks - alter to taste
    #define MIN(x,y) ({\
    const typeof(x) _x = ~(typeof(x))0; \
    const typeof(y) _y = ~(typeof(y))0; \
    void MIN_BUG(); \
    if (sizeof(_x) != sizeof(_y)) \
    MIN_BUG(); \
    if ((_x > 0 && _y < 0) || (_x < 0 && _y > 0)) \
    MIN_BUG(); \
    __MIN(x,y); \
    })

    // test code that compiles with no complaints with -O1 -Wall
    int main() {
    unsigned i = 1;
    unsigned j = -2;
    return MIN(i,j);
    }

    // test code that complains at link time (in this version) with ..
    // gcc -o test -O1 -Wall test.c
    // /tmp/cczJbwv5.o: In function `main':
    // /tmp/cczJbwv5.o(.text+0x7): undefined reference to `MIN_BUG'
    // collect2: ld returned 1 exit status
    //
    int main() {
    unsigned i = 1;
    signed j = -2;
    return MIN(i,j);
    }

    > depends on how often it is useful to compare (say) unsigned chars
    > against ints, and on whether the compiler warns about cases where you
    > try to stuff the return value into a too-small container. I bet that
    > just forbidding signed->unsigned promotion would be enough.

    Possibly. I have little clue as to the real extent of the problem.

    Peter
    -
    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/

    \
     
     \ /
      Last update: 2005-03-22 13:01    [W:4.155 / U:0.028 seconds]
    ©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site