64-bit datatype

J

Jake Langham

A program I'm writing requires a 64-bit datatype. On gcc 4.1.1 I can
compile with 'long long' integers but I'm aware that this is not an ANSI
standard type and am worried about portability issues - especially on
32-bit machines. Since I can't seem to find this sort of information
anywhere: is there a particular datatype I should be using and how do I
ensure that I choose the correct datatype for each machine?

Cheers,

-Jake
 
M

Martin Ambuhl

Jake said:
A program I'm writing requires a 64-bit datatype. On gcc 4.1.1 I can
compile with 'long long' integers but I'm aware that this is not an ANSI
standard type

Yes, it is. And has been for more than seven years.
It is not a standard C89 type.
and am worried about portability issues - especially on
32-bit machines. Since I can't seem to find this sort of information

32-bit machines are no drawback to using long long.
Hell, long long could easily be used on 8-bit, or 12-bit, or even 1-bit
addressable machines.
anywhere: is there a particular datatype I should be using and how do I
ensure that I choose the correct datatype for each machine?

If you are stuck with making programs portable to C89 compilers that
don't provide long long, then you're stuck with long.
 
E

Eric Sosman

Jake Langham wrote On 04/25/07 14:25,:
A program I'm writing requires a 64-bit datatype. On gcc 4.1.1 I can
compile with 'long long' integers but I'm aware that this is not an ANSI
standard type and am worried about portability issues - especially on
32-bit machines. Since I can't seem to find this sort of information
anywhere: is there a particular datatype I should be using and how do I
ensure that I choose the correct datatype for each machine?

`long long' *is* an ANSI C data type -- if your compiler
conforms to the current "C99" ISO/ANSI Standard. `long long'
is at least 64 bits wide, perhaps wider.

The uptake of C99 has been slower than that of C89/90,
but compilers that support it are becoming commoner than they
used to be. Also, many pre-C99 compilers provided `long long'
as an extension. It is fairly widely available.

Portability worries should be in regard to some well- or
ill-defined universe of likely target environments. If you
have a reasonable idea of what those environments are likely
to be, you could check whether C99 or C90-extended compilers
are available for them -- for example, how many of your targets
do not have a gcc implementation?

Finally, if you simply cannot rely on `long long' you
will need to emulate the necessary operations with narrower
types. How difficult that is depends on what operations you
need: Dead easy if all you need is bit-flipping, harder if
you need add/subtract/multiply/compare, harder still if you
need divide/modulus, and nasty in the extreme if you need
things like printf() and scanf() support.
 
K

Keith Thompson

Eric Sosman said:
Jake Langham wrote On 04/25/07 14:25,:

`long long' *is* an ANSI C data type -- if your compiler
conforms to the current "C99" ISO/ANSI Standard. `long long'
is at least 64 bits wide, perhaps wider.

The uptake of C99 has been slower than that of C89/90,
but compilers that support it are becoming commoner than they
used to be. Also, many pre-C99 compilers provided `long long'
as an extension. It is fairly widely available.
[...]

I seem to recall that some Microsoft compilers provide a 64-bit
integer type, but call it something other than "long long".

If you care about portability to such compilers, you can probably use
some kind of #ifdef with a typedef:

#ifdef _SOME_MS_SPECIFIC_SYMBOL
typedef _SOME_MS_SPECIFIC_TYPE int64;
#else
typedef long long int64;
#endif
 
I

Ian Collins

Jake said:
A program I'm writing requires a 64-bit datatype. On gcc 4.1.1 I can
compile with 'long long' integers but I'm aware that this is not an ANSI
standard type and am worried about portability issues - especially on
32-bit machines. Since I can't seem to find this sort of information
anywhere: is there a particular datatype I should be using and how do I
ensure that I choose the correct datatype for each machine?
In addition to what the other respondents have posted, for a good
example have a look at the GD image library source. It uses a lot of 64
bit maths and provides a cross-platform library and configuration header.
 
J

jacob navia

Keith said:
Eric Sosman said:
Jake Langham wrote On 04/25/07 14:25,:


`long long' *is* an ANSI C data type -- if your compiler
conforms to the current "C99" ISO/ANSI Standard. `long long'
is at least 64 bits wide, perhaps wider.

The uptake of C99 has been slower than that of C89/90,
but compilers that support it are becoming commoner than they
used to be. Also, many pre-C99 compilers provided `long long'
as an extension. It is fairly widely available.

[...]

I seem to recall that some Microsoft compilers provide a 64-bit
integer type, but call it something other than "long long".

If you care about portability to such compilers, you can probably use
some kind of #ifdef with a typedef:

#ifdef _SOME_MS_SPECIFIC_SYMBOL
typedef _SOME_MS_SPECIFIC_TYPE int64;
#else
typedef long long int64;
#endif

Microsoft provides long long in the latest versions of their
compilers (msvc 2005)
 
M

Malcolm McLean

Jake Langham said:
A program I'm writing requires a 64-bit datatype. On gcc 4.1.1 I can
compile with 'long long' integers but I'm aware that this is not an ANSI
standard type and am worried about portability issues - especially on
32-bit machines. Since I can't seem to find this sort of information
anywhere: is there a particular datatype I should be using and how do I
ensure that I choose the correct datatype for each machine?
long long is reasonably portable but it is by no means impossible that you
will have to run code through a compiler that doesn't support it. It
probably your best bet, in particular if it makes no sense to run your
program on a machine without 64 bit hardware support.
 
J

jaysome

Eric Sosman said:
Jake Langham wrote On 04/25/07 14:25,:

`long long' *is* an ANSI C data type -- if your compiler
conforms to the current "C99" ISO/ANSI Standard. `long long'
is at least 64 bits wide, perhaps wider.

The uptake of C99 has been slower than that of C89/90,
but compilers that support it are becoming commoner than they
used to be. Also, many pre-C99 compilers provided `long long'
as an extension. It is fairly widely available.
[...]

I seem to recall that some Microsoft compilers provide a 64-bit
integer type, but call it something other than "long long".

If you care about portability to such compilers, you can probably use
some kind of #ifdef with a typedef:

#ifdef _SOME_MS_SPECIFIC_SYMBOL
typedef _SOME_MS_SPECIFIC_TYPE int64;
#else
typedef long long int64;
#endif

FWIW:

#ifdef _MSC_VER
typedef __int64 int64;
#else
typedef long long int64;
#endif
 
M

Michael

A program I'm writing requires a 64-bit datatype. On gcc 4.1.1 I can
compile with 'long long' integers but I'm aware that this is not an ANSI
standard type and am worried about portability issues - especially on
32-bit machines. Since I can't seem to find this sort of information
anywhere: is there a particular datatype I should be using and how do I
ensure that I choose the correct datatype for each machine?

I've always seen this done as preprocessor definitions. Basically,
you have a header file called machinedependent.h or something like
that, with a bunch of things of the form:
#if defined (MACHINE_TYPE_1)
typedef long long INT64;
#elif defined(MACHINE_TYPE_2)
typedef long int INT64;
/* ... */
#else
Hey, look. A syntax error. /* If compile breaks here, add a case. */
#endif

Then anywhere that you depend on the 64-bittednes, you'd use INT64 as
your type. And over time, you fill in the types you need on the
architectures you use.

Quite possibly you could find a file like this already on the net
somewhere.

Michael
 
J

Jake Langham

Martin said:
Yes, it is. And has been for more than seven years.
It is not a standard C89 type.


32-bit machines are no drawback to using long long.
Hell, long long could easily be used on 8-bit, or 12-bit, or even 1-bit
addressable machines.


If you are stuck with making programs portable to C89 compilers that
don't provide long long, then you're stuck with long.

Perhaps I should have phrased it 'I am led to believe that long long may
not be supported on all compilers'. I mistakenly remembered that the
compilation trouble was when passing -ansi flag to gcc. (I'm afraid I
don't know nearly enough about C standards). In fact, I get these
warning when I compile with -pedantic:

warning: ISO C90 does not support ‘long long’
warning: use of C99 long long integer constant

which was what made me consider portability issues.

I suppose what I was really looking for (which quite a few other people
have touched on) was a way to implement some conditionals in the
preprocessor that ensure that the datatype I'm using is always 64-bits
long. If I'm being really greedy, I suppose I'd also like a way to avoid
flooding my terminal screen with warnings when compiling with -pedantic
as well.
 
G

Guest

Jake said:
Martin Ambuhl wrote: [snip]
If you are stuck with making programs portable to C89 compilers that
don't provide long long, then you're stuck with long.

Perhaps I should have phrased it 'I am led to believe that long long may
not be supported on all compilers'. I mistakenly remembered that the
compilation trouble was when passing -ansi flag to gcc. (I'm afraid I
don't know nearly enough about C standards). In fact, I get these
warning when I compile with -pedantic:

warning: ISO C90 does not support ‘long long’
warning: use of C99 long long integer constant

which was what made me consider portability issues.

I suppose what I was really looking for (which quite a few other people
have touched on) was a way to implement some conditionals in the
preprocessor that ensure that the datatype I'm using is always 64-bits
long. If I'm being really greedy, I suppose I'd also like a way to avoid
flooding my terminal screen with warnings when compiling with -pedantic
as well.

<OT>
Well, you can, as a last resort only, RTFM. If you do, you will find
that gcc has a flag named -Wno-long-long which silences the compiler
even when you compile with -ansi -pedantic -Wextra -Wall
</OT>

HTH
Bjørn
 
W

websnarf

A program I'm writing requires a 64-bit datatype. On gcc 4.1.1 I can
compile with 'long long' integers but I'm aware that this is not an ANSI
standard type and am worried about portability issues - especially on
32-bit machines. Since I can't seem to find this sort of information
anywhere: is there a particular datatype I should be using and how do I
ensure that I choose the correct datatype for each machine?

You will achieve the widest possible portability by including
pstdint.h from here:

http://www.pobox.com/~qed/pstdint.h

And use the type int64_t. It uses long long when C99 compatibility is
available and has special case compatibility with a number of
compilers that are only C89 compliant. In the end there are very few
compilers (typically 16 bit ones, or esoteric embedded compilers) that
are not handled or which cannot be made to support 64 bit integers.
If you run into a platform which is ANSI C 89 compliant and can
support 64 bits, but which is not covered in pstdint.h please update
the file and let me know about it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top