Re: Defining int64

From: Eric Sosman (Eric.Sosman_at_sun.com)
Date: 11/13/03


Date: Thu, 13 Nov 2003 12:55:28 -0500

ESOJAY wrote:
>
> It allows long long. I'm just not sure if long long is always going to be 64
> bits on any machine I port the code to.

    The situation is a little bit screwy ...

    The C99 Standard requires `long long' to be at least 64
bits wide, but permits it to be wider. If you need 64 bits
or more but don't want to waste space if `long long' turns
out to be 128 bits, say, use `int_least64_t' from <stdint.h>.

    Still in C99, if you need *exactly* 64 bits you can use
`int64_t', also from <stdint.h>. However, C99 does not require
that this type be supported by all implementations, so you can't
count on it being available. It may be enough that your code
will fail to compile on such an implementation; the person
attempting to port it will at least be ade aware of the trouble.

    The earlier C90 Standard did not define `long long' --
in fact, using `long long' in a program was a syntax error.
Still, many compilers provided it as an extension when
invoked in a non-strict mode. About the best you can do
is to hope that such compilers also "advertise" the fact
through the <limits.h> macros, so you can test at compile
time whether a suitable `long long' exists. Making the
test is just a bit tricky because the preprocessor may
not be able to handle such large values; here's one way:

        #include <limits.h>
        #if (LLONG_MAX >> 31) >> 31 == 1
            typedef long long int64;
        #else
            #error "No 64-bit integer type!"
        #endif

(Obviously, you could change `==' to `>=' if you need an
"at least" rather than an "exactly" 64-bit type.)

    This isn't 100% certain to work, though. `LLONG_MAX'
is mandated by C99 but was (obviously) absent from C90,
and a compiler that decided to provide `long long' before
C99 appeared might not have described the type in exactly
the same way C99 would eventually require. Also, the
support for `long long' in things like printf() may not
be exactly what C99 eventually specified. Still, a test
like this is probably the best you can do for a pre-C99
implementation.

-- 
Eric.Sosman@sun.com


Relevant Pages

  • Re: Angband with an accent: displaying extended characters
    ... I'd certainly expect a port to be possible, ... >> About platform stuff, the ideal situation would be if all platform dependent ... >> We are at least not in a hurry to remove support for RISCOS, ... future and which ones are lost cases that none care or compile for anymore. ...
    (rec.games.roguelike.angband)
  • ANNOUNCE: DJGPP port of FreeBE/AF 1.2 uploaded.
    ... This is a port of FreeBE/AF 1.2 to MSDOS/DJGPP. ... DJGPP specific changes. ... Please note that I have no hardware to test if the drivers really work. ... To compile this program the allegro library and certain ...
    (comp.os.msdos.djgpp)
  • Re: upgrage from 4.1.2 to 4.3.3+?
    ... > every available RPM for the distro...and there isn't one for PHP. ... > And also, I uh, don't believe I've ever recompiled Apache or PHP. ... Well - You could compile your own version of Apache and PHP and place it ... port number - By this I mean, whenever you visit a website, it defaults ...
    (comp.lang.php)
  • Re: gcc 4.3: when will it become standard compiler?
    ... But I'm not exactly sure what you mean by "compile world with C99". ... In fact default mode of GCC accepts many C99 constructs like // comments and mixed declarations and code, which are not valid C89. ... my point is that in C89 mode *restrict* got expanded ...
    (freebsd-current)
  • Re: [9fans] Modularizing plan9port
    ... And a port of of plan9's awk? ... Right after compile several checks ... package is committed to the production systems. ...
    (comp.os.plan9)