473,574 Members | 2,875 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Portable Test for long long Support

Problem: find a portable way to determine whether a compiler supports
the "long long" type of C99.

I thought I had this one solved with the following code:
#include <limits.h>
#ifdef ULONG_LONG_MAX

/* 64-bit code */

#endif
This seemed to work on most platforms until I compiled today under
Mandrake linux i586 (gcc 3.2.2). What's odd is that this compiler
supports the "long long" type, yet the 64-bit code didn't get included.

Any ideas? Is there a "standard" way to test whether a C compiler
supports the C99 types?

Mark

Nov 13 '05 #1
5 4798

On Thu, 23 Oct 2003, Mark Shelor wrote:

Problem: find a portable way to determine whether a compiler supports
the "long long" type of C99.

I thought I had this one solved with the following code:

#include <limits.h>

#ifdef ULONG_LONG_MAX
I believe the correct macro name is ULLONG_MAX (and the signed
equivalents would be LLONG_MAX and LLONG_MIN).
/* 64-bit code */

#endif This seemed to work on most platforms until I compiled today under
Mandrake linux i586 (gcc 3.2.2). What's odd is that this compiler
supports the "long long" type, yet the 64-bit code didn't get included.

Any ideas? Is there a "standard" way to test whether a C compiler
supports the C99 types?


That way looks like the safest way to me. But, as another thread
pointed out recently, just because your 'gcc' supports

long long int foo;

doesn't necessarily mean that your version of 'libc' supports

printf("%lld\n" , foo);

You'll need to test all the relevant library functions as well as
the core language support. And that will probably get painful.
It might be easier in some cases to simply let the guy compiling
the program set a macro through -DUSE_LONG_LONG or whatever, and
let *him* take the blame for incomplete 'long long' support. :-)
I.e., skip the <limits.h> test and just write

#if USE_LONG_LONG
[...stuff...]
#else
[...stuff using 'long' only...]
#endif
My $.02,
-Arthur
Nov 13 '05 #2
In article <Ce************ ********@comcas t.com>,
Mark Shelor <ms*****@comcas t.removeme.net> wrote:
Problem: find a portable way to determine whether a compiler supports
the "long long" type of C99.

I thought I had this one solved with the following code:

#include <limits.h>

#ifdef ULONG_LONG_MAX

[snippage]

Actually, the C99 name for the macros are LLONG_MIN, LLONG_MAX,
and ULLONG_MAX respectively.

You may encounter systems that have (partial or complete) support
for "long long" but do not have the defines, simply because those
systems are not (yet?) C99-conformant and no effort was put into
updating the headers.

In some ways this is related to the hypothetical situation of an
otherwise C89-conformant system that fails to define __STDC__ as
1, and therefore fails to conform. :-)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://67.40.109.61/torek/index.html (for the moment)
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 13 '05 #3
Mark Shelor <ms*****@comcas t.removeme.net> writes:
Problem: find a portable way to determine whether a compiler supports
the "long long" type of C99.

I thought I had this one solved with the following code:
#include <limits.h>
#ifdef ULONG_LONG_MAX

/* 64-bit code */

#endif
This seemed to work on most platforms until I compiled today under
Mandrake linux i586 (gcc 3.2.2). What's odd is that this compiler
supports the "long long" type, yet the 64-bit code didn't get included.

Any ideas? Is there a "standard" way to test whether a C compiler
supports the C99 types?


As others have pointed out, the relevant macros are LLONG_MAX and
ULLONG_MAX. But since "long long" appeared as an extension before it
was blessed by the C99 standard, there may be implementations that
provide long long but don't have LLONG_MAX and ULLONG_MAX.

You might consider testing during configuration rather than during
compilation. For example, you might write a small C program that
declares a long long variable and appends the line

#define HAVE_LONG_LONG

to one of your application header files. Try to compile and run the
program. If long long is supported, it will run and append the line;
if it's not, it will fail to compile, and HAVE_LONG_LONG won't be
defined for your application.

BTW, are you really looking for type long long or for a 64-bit type?
Type long long is guaranteed to be at least 64 bits, but there could
be implementations that don't define long long, but that have 64-bit
longs. Consider using uint64_t or uint_least64_t from <stdint.h> If
your implementation doesn't have <stdint.h>, Doug Gwyn has written a
reasonably portable C90-compatible implementation of it; see
<http://www.lysator.liu .se/c/q8/index.html>.

--
Keith Thompson (The_Other_Keit h) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 13 '05 #4
Keith Thompson wrote:
Mark Shelor <ms*****@comcas t.removeme.net> writes:
Problem: find a portable way to determine whether a compiler supports
the "long long" type of C99.

As others have pointed out, the relevant macros are LLONG_MAX and
ULLONG_MAX. But since "long long" appeared as an extension before it
was blessed by the C99 standard, there may be implementations that
provide long long but don't have LLONG_MAX and ULLONG_MAX.

BTW, are you really looking for type long long or for a 64-bit type?
Type long long is guaranteed to be at least 64 bits, but there could
be implementations that don't define long long, but that have 64-bit
longs. Consider using uint64_t or uint_least64_t from <stdint.h> If
your implementation doesn't have <stdint.h>, Doug Gwyn has written a
reasonably portable C90-compatible implementation of it; see
<http://www.lysator.liu .se/c/q8/index.html>.

Thanks Keith, Arthur, Chris for the helpful replies.

Yes, I did try using ULLONG_MAX earlier in the day, but had no luck
there either. Oddly, when I grep'd on ULONG_LONG_MAX in the
/usr/include subdirectories, the response I got was

$ grep ULONG_LONG_MAX `find /usr/include -print`
/usr/include/limits.h:# define ULLONG_MAX ULONG_LONG_MAX

However, since this #define was nested within other #ifdef's that
apparently get excluded during the compile, the token ULLONG_MAX didn't
show up as being defined.

If I remove my original conditional compilation directive, and simply go
ahead and include the 64-bit code (with unsigned long long's),
everything compiles and runs just fine. So, this has me scratching my
head a bit.

I'm using this #ifdef in a package I wrote for CPAN. It's a C
implementation (wrapped in a Perl module) of NIST's Secure Hash
Algorithms (SHA-1/256/384/512). Basically, the latter two (384/512) use
64-bit operations, so I note in my documentation that run-time code for
them won't exist if one's native compiler lacks support for long long's.
However, the package will still compile and run just fine.

Since this package gets downloaded, compiled, and installed by lots of
people with C compilers of unknown capability, I've written the code to
be as vanilla and portable as possible. What I DON'T want to have
happen is for a person's compile to crash if his compiler doesn't happen
to support long long's. It's acceptable in those circumstances for the
package to omit support for SHA-384 and SHA-512, which fortunately
aren't really crucial in the near-term anyway.

Yet another oddity is the fact that the 64-bit code gets included when I
compile it under the automake facility that's provided as part of the
Perl packaging utility (h2xs). However, when I compile it as a
standalone C program with a simple test driver, the 64-bit code is
missing. I don't have the patience to wade through the myriad compiler
options and define's used by h2xs to figure out what's going on! :)

Regards, Mark

Nov 13 '05 #5

On Fri, 24 Oct 2003, Mark Shelor wrote:

Keith Thompson wrote:

BTW, are you really looking for type long long or for a 64-bit type?
I'm using this #ifdef in a package I wrote for CPAN. It's a C
implementation (wrapped in a Perl module) of NIST's Secure Hash
Algorithms (SHA-1/256/384/512). Basically, the latter two (384/512) use
64-bit operations, so I note in my documentation that run-time code for
them won't exist if one's native compiler lacks support for long long's.
However, the package will still compile and run just fine.

Since this package gets downloaded, compiled, and installed by lots of
people with C compilers of unknown capability, I've written the code to
be as vanilla and portable as possible.


If you wanted to be really nice, you could write (or better, Google)
a couple of routines for 64-bit multiplication, rotation, or whatever
using pairs of 32-bit variables, and then write a 32-bit version of
your code that way, for people whose machines don't support 64-bit
stuff on a hardware level but who still want to use your module.

#ifdef HAVE_LONG_LONG
unsigned long long x = 0x238776a8f887e a60;
x <<= 6;
x += 17;
#else
unsigned int xh = 0x238776a8;
unsigned int xl = 0xf887ea60;
xh <<= 6;
xh |= xl >> (32-6);
xl <<= 6;
ull_add(&xh, &xl, 0, 17);
#endif
HTH,
-Arthur

Nov 13 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2856
by: nntp-service.ohio-state.edu | last post by:
Hey folks - I'm a newbie to java script. I'm trying to make a portable data-validator for fields in an HTML form. Ideally, it would work something like this: <input type="text" name="test" onBlur=check_data(this.value, "date")> The script would check the data against its data type (date, number, text)
13
2726
by: James Harris | last post by:
Hi, Can someone recommend a book that will teach me how to approach C programming so that code is modularised, will compile for different environments (such as variations of Unix and Windows), will be robust etc. As an example, I am developing a BSD Sockets suite which I want to run under various Unixes, including the Zaurus version of...
23
3161
by: Ken Turkowski | last post by:
The construct (void*)(((long)ptr + 3) & ~3) worked well until now to enforce alignment of the pointer to long boundaries. However, now VC++ warns about it, undoubtedly to help things work on 64 bit machines, i.e. with 64 bit pointers. In the early days of C, where there were problems with the size of int being 16 or 32 bits, the response...
5
2379
by: Kobu | last post by:
In embedded systems (programmed in C), often times structure declarations are used to group together several status/control/data registers of external hardware (or even internal registers). The example below (from GBD's THE C BOOK) uses this. Is this a portable method? . /*
7
1626
by: Sorav Bansal | last post by:
The C standard does not seem to support integer types which are 64-bits long. 'long long' and 'uint64_t' work only on limited platforms/architectures. What is the best way to use 64-bit values in compliance with the C-standard? Regards
131
6067
by: pemo | last post by:
Is C really portable? And, apologies, but this is possibly a little OT? In c.l.c we often see 'not portable' comments, but I wonder just how portable C apps really are. I don't write portable C code - *not only* because, in a 'C sense', I
28
2708
by: lovecreatesbeauty | last post by:
On gcc, which version of C standard has the most compliant: -c89, -ansi or c99? For more portable C code, which options should be applied to compilation? Can the following options guarantee the most portable C code for different environment? gcc -ansi -pedantic -Wall test.c
7
1784
by: Michael | last post by:
I'm new to PHP. Evidently my ISP's server does not recognize <?php ... ?>, but it does recognize <script language="php"... </script>, which would imply that <?php ... ?is not portable. If in fact <?php ... ?is not portable, why are so many PHP scripts using it?
7
2411
by: Chris Riesbeck | last post by:
What's the right way to make sure checkboxes stick with their labels when the window is resized? Right now I'm trying <label><input ...text</labelwith white-space:nowrap on label, but this only seems to work in Firefox and IE 7. In IE 8 and Opera I get one long line, as if the nowrap applied outside the label elements.
0
7835
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7753
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8095
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7847
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8132
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6500
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5645
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3769
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3787
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.