lkml.org 
[lkml]   [2001]   [Aug]   [31]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
From
SubjectRe: [IDEA+RFC] Possible solution for min()/max() war
Date
> > only problem seems to be signed/unsigned int comparison.
>
> I don't know how you know that! You could be right - I don't know.

because I asked the only competent instanace around: my computer.

: ferkel:~ # more sc.c
: #include <stdio.h>
:
: int main() {
: signed char ca;
: unsigned char cb;
:
: signed int ia;
: unsigned int ib;
:
: ca=-1;
: cb=2;
: if (ca<cb) printf("ca<cb\n");
: else if (ca>cb) printf("ca>cb\n");
: else printf("ca==cb\n");
:
: ia=-1;
: ib=2;
: if (ia<ib) printf("ia<ib\n");
: else if (ia>ib) printf("ia>ib\n");
: else printf("ia==ib\n");
:
: }

this will give the result:

: ferkel:~ # ./sc
: ca<cb
: ia>ib


you see?

if you _explictely_ cast the signed value to unsigned, the result will
be wrong just the same way as if you did not cast it at all. if you
cast the unsigned value to signed, you will have the same problem if
the unsigned value is > 0x7fffffff. so the new macros are absolutely
unneccessary, because they do not fix anything.

> for me to be able to begin to formulate a generic solution. I've just
> seen some examples, some more convincing than others.
>
> > > const (typeof(a)) _a = ~(typeof(a))0
> > > const (typeof(b)) _b = ~(typeof(b))0
> > > if _a < 0 && _b > 0 || _a > 0 && b < 0
> > > BUG() // one signed, the other unsigned
> > > standard_max(a,b)
> >
> > if sizeof(typeof(a))==sizeof(int) && sizeof(typeof(b))==sizeof(int) &&
> > ( _a < 0 && _b > 0 || _a > 0 && b < 0 )
> > BUG() // signed unsigned int compare
>
> What makes sizeof(int) so magic? Are you saying that the problems
> don't arise between signed long long and unsigned long long? I saw

no, that was just an example. long long doesnt work either. and the
sizeof(int) is bound to 32bit platforms. unfortunately, I dont have a
64bit CPU around. the problem seems to arise where the size of a datatype
equals or extends the size of a machineword. so, really correct would
be:

: if sizeof(typeof(a))>=sizeof(machineword) &&
: sizeof(typeof(b))>=sizeof(machineword)


however I dont know where to get this "machineword".

-
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:0.580 / U:1.452 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site