[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: exact-width C Integers and Autoconf
From: |
Paul Eggert |
Subject: |
Re: exact-width C Integers and Autoconf |
Date: |
Thu, 21 Feb 2002 05:50:57 -0800 (PST) |
> From: "Simon Waters" <address@hidden>
> Date: Thu, 21 Feb 2002 02:31:30 -0000
> I need to define 32 and 64 bit integer types in reasonably portable C.
I assume you mean exact-width 32 and 64 bit types. The C standard
does not require the existence of such types; for example, you might
be on a 36-bit host that has 36-bit int and 72-bit long. However,
such machines are admittedly rare these days.
> Linux can include "inttypes.h" these days, so I'm happy to rename to the =
> "standard" names.
Yes, that's the better way in the long run. The standard names will
take over eventually.
> Is there an autoconf example to follow?=20
Not that I know of. I would put this into configure.ac:
AC_CHECK_HEADERS(inttypes.h limits.h stdint.h)
and put something like the following into my C code:
#include <config.h>
#if HAVE_LIMITS_H
# include <limits.h>
#endif
#if HAVE_INTTYPES_H
# include <inttypes.h>
#else
# if HAVE_STDINT_H
# include <stdint.h>
# endif
#endif
#ifndef INT32_MAX
# if INT_MAX == 2147483647
# define int32_t int
# define INT32_MAX 2147483647
# else
# if LONG_MAX == 2147483647
# define int32_t long
# define INT32_MAX 2147483647L
# else
This code assumes the existence of 32-bit exact-width
integers, and will not work on machines that lack such a type.
# endif
# endif
#endif
/* Avoids integer overflow on 32-bit hosts. */
# define EQUALS_INT64_MAX(x) \
((x) / 65536 / 65536 == 2147483647 && (x) % 2147483647 == 1)
#ifndef INT64_MAX
# if EQUALS_INT64_MAX (INT_MAX)
# define int64_t int
# define INT64_MAX 9223372036854775807
# else
# if EQUALS_INT64_MAX (LONG_MAX)
# define int64_t long
# define INT64_MAX 9223372036854775807L
# else
# if EQUALS_INT64_MAX (LLONG_MAX)
# define int64_t long long
# define INT64_MAX 9223372036854775807LL
# else
This code assumes the existence of 64-bit exact-width
integers, and will not work on machines that lack such a type.
# endif
# endif
# endif
#endif
Admittedly EQUALS_INT64_MAX is a bit tricky. Perhaps something like
this should be turned into an autoconf macro, though we probably need
more experience. I'll CC this message to address@hidden to see if
there are other opinions.
> This is a multi-part message in MIME format.
Please send text mail as plaintext; it saves me time. Thanks.
- Re: exact-width C Integers and Autoconf,
Paul Eggert <=