[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: how to detect int64?
From: |
Paul Eggert |
Subject: |
Re: how to detect int64? |
Date: |
Mon, 19 Jul 2010 09:23:23 -0700 |
User-agent: |
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.10) Gecko/20100527 Thunderbird/3.0.5 |
On 07/18/10 19:01, Jay K wrote:
> So, question is, what is wrong with the following seems-simple algorithms:
>
>
> 1) Find the types that exist, char, short, int, long, long long,
> their sizes, #include <limits.h>, multiply by CHAR_BIT?
> Or probably assume all but long long exist.
> Or error out if anything other than long long doesn't exist
> (or __int64 instead)
This algorithm doesn't work on machines with padding bits,
that is, unused bits in the representation. It's possible, for example
for CHAR_BIT to be 8, sizeof (unsigned int) to be 4, but UINT_MAX
is 2**31 - 1 (not 2**62 - 1), because the hardware simply doesn't support
unsigned arithmetic and unsigned arithmetic is simulated with nonnegative
signed values, i.e., with a leading padding bit of zero.
This is not usually a practical problem, but it can occur on machines
that do not use two's-complement arithmetic. Such machines include
the Unisys ClearPath architecture, and possibly some DSPs. They're
not usually the target of autoconf apps but if they're easy to support
we might as well do it.
> 2) #include <limits.h>, compare UNSIGNED_FOO_MAX for equality to 255, 65535,
> 4GB, etc.?
> Before attempting to check for equal with the maximum 64bit number, since
> preprocessor might reject such a large number you would:
> See if long long exists, if so, ok.
limits.h might be missing the relevant UNSIGNED_FOO_MAX value.
This sort of problem is all too common, unfortunately.
The code you suggest will indeed work most of the time,
on common platforms. But autoconf attempts to be a bit
more portable than that.