[svn.haxx.se] · SVN Dev · SVN Users · SVN Org · TSVN Dev · TSVN Users · Subclipse Dev · Subclipse Users · this month's index

Re: apr_off_t is of an ambiguous size.

From: Ben Reser <ben_at_reser.org>
Date: 2004-01-17 02:19:29 CET

On Fri, Jan 16, 2004 at 07:30:53PM -0500, Russell Yanofsky wrote:
> There appear to be a few bugs above. In the first line >= should be replaced
> by <=. On the third line | should be &&. On the seventh line the second >
> should be < and an extra parenthesis should be added on the end.

err yeah.

> Yes. With the bugs fixed it should be equivalent to my first suggestion, but
> shorter.

And easier to understand if I wrote things right.

> Huh? How does endianness come into play here? Do you see me casting to a
> char array? declaring a union? calling a socket api? :)
> With an 8 byte apr_off_t the expression should be evaluated as follows on
> *any* architecture:
>
> min = ((apr_off_t)-1) << (sizeof(apr_off_t)*8-1)
> min = 0xFFFFFFFFFFFFFFFF << (sizeof(apr_off_t)*8-1)
> min = 0xFFFFFFFFFFFFFFFF << 63
> min = 0x8000000000000000
> max = ~min
> max = 0x7FFFFFFFFFFFFFFF
>
> In 2's complement representation
>
> min = 0x8000000000000000 = -9223372036854775808 = -2^63
> max = 0x7FFFFFFFFFFFFFFF = 9223372036854775807 = 2^63-1

You're right. And this does work even on 32-bit big endian archs
(except you need to cast what you want to shift or you'll get the wrong
output.

The following example code:

long long min, max;

min = (long long) (sizeof(long long) - 1) << (sizeof(long long)*8-1);
max = ~min;

printf("min=%lld\nmax=%lld\n",min,max);
printf("min-1=%lld\nmax+1=%lld\n",min-1,max+1);

Produces the following output:

min=-9223372036854775808
max=9223372036854775807
min-1=9223372036854775807
max+1=-9223372036854775808

This is true on the follow archs:
32-bit little endian (i586)
32-bit bit enndia (ppc)
64-bit little endian (amd64)
64-bit big endian (sun4u)

The min-1 and max+1 is there to prove that the 2s compliment is correct.
 
For whatever reason I always thought of the << and >> operators as
literally shifting the bits left or right. Rather they should be
described as shifting fromt he low bit to the high bit and the high bit
to the low bit respectively.

For whatever reason, everyone calls them right and left shifts. But
it's only really a right or left shift on big endian archs. On a little
endian arch the shift is actually reversed.

Anyway I'll fix the code to work that way. And to think I asked someone
else if they thought that was Endian safe and they told me no too. Next
time I'll just test it for myself.

-- 
Ben Reser <ben@reser.org>
http://ben.reser.org
"Conscience is the inner voice which warns us somebody may be looking."
- H.L. Mencken
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
Received on Sat Jan 17 02:19:56 2004

This is an archived mail posted to the Subversion Dev mailing list.

This site is subject to the Apache Privacy Policy and the Apache Public Forum Archive Policy.