Subject: Re: off_t and int/long
To: Hiroyuki Ito <hiroy@netcom.com>
From: Scott Reynolds <scottr@plexus.com>
List: port-i386
Date: 05/19/1996 19:51:55
On Sun, 19 May 1996, Hiroyuki Ito wrote:

> > All of the results you showed are correct.  What, specifically, did you
> > expect the code to produce as output?
> 
> a=0x0000000200000001
> a=0x0000000200000001
> a=0x0000000200000001
> a=0x0000000200000001
> a=0x0000000200000001
> a=0x00000001ffffffff
> a=0x00000001ffffffff
> a=0x00000001ffffffff
> a=0x00000001ffffffff
> a=0x00000001ffffffff

OK.  Here's the code again, for reference:

>               f(b * c + d);
>               f((off_t)(b * c + d));
>               f((off_t)b * c + d);
>               f(b * (off_t)c + d);
>               f(b * c + (off_t)d);

In the first two versions, the arithmetic is done using ints, and the
result is cast to an off_t (implicitly in the first case).  Since (b * c)
overflows a 32 bit int, the result is simply the third operand.

In the third and fourth versions, the multiplication is done after an
implicit cast of the operand without a cast; hence the expected result.

In the fifth version, the multiplication is done using ints, and then the
result of the multiplication is cast to an off_t to be added to the third
operand.  Since the same overflow applies as in the first two versions,
the result is simply the third operand extended to 64 bits.

Remember that the compiler will only do type casting when necessary,
according to the rules of operator precedence and implied casting.

--scott