On Fri, 3 Apr 2009 08:03:22 -0700 (PDT) Linus Torvalds
<[email protected]> wrote:
> +static inline loff_t pos_from_hilo(unsigned long high, unsigned long low)
> +{
> +#define HALF_LONG_BITS (BITS_PER_LONG / 2)
> + return ((high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
> +}
Does C promote the `unsigned long high' beforehand, or will the
intermediate expression overflow?
<does it the old-fashioned way>
y:/home/akpm> cat t.c
#define HALF_LONG_BITS 16
main()
{
unsigned hi = 0x43;
unsigned lo = 0x42;
unsigned long long res;
res = ((hi << 16) << 16) | lo;
printf("%llx\n", res);
}
y:/home/akpm> gcc -O t.c
y:/home/akpm> ./a.out
42
I think it's wrong on 32-bit?
Also, HALF_LONG_BITS is 32 on 64-bit, so "high" gets shifted to zero.
It's unclear whether this was deliberate, but either way, it's a sneaky
trick and deserves a code comment!
--
To unsubscribe from this list: send the line "unsubscribe linux-api" in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html