From 14e17dd9f68ef07c24da157b156bb5830106b8f1 Mon Sep 17 00:00:00 2001 From: John Maddock Date: Fri, 11 Dec 2009 17:46:10 +0000 Subject: [PATCH] Update cstdint.hpp to always define the INT#_C macros. Try again with Codegear workaround. [SVN r58292] --- include/boost/cstdint.hpp | 68 ++++++----- include/boost/integer.hpp | 1 + test/Jamfile.v2 | 4 +- test/cstdint_test.cpp | 12 +- test/cstdint_test2.cpp | 248 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 298 insertions(+), 35 deletions(-) create mode 100644 test/cstdint_test2.cpp diff --git a/include/boost/cstdint.hpp b/include/boost/cstdint.hpp index 030995f..47e6a16 100644 --- a/include/boost/cstdint.hpp +++ b/include/boost/cstdint.hpp @@ -23,6 +23,16 @@ #ifndef BOOST_CSTDINT_HPP #define BOOST_CSTDINT_HPP +// +// Since we always define the INT#_C macros as per C++0x, +// define __STDC_CONSTANT_MACROS so that does the right +// thing if possible, and so that the user knows that the macros +// are actually defined as per C99. +// +#ifndef __STDC_CONSTANT_MACROS +# define __STDC_CONSTANT_MACROS +#endif + #include // @@ -348,19 +358,16 @@ namespace boost Macro definition section: -Define various INTXX_C macros only if -__STDC_CONSTANT_MACROS is defined. - -Undefine the macros if __STDC_CONSTANT_MACROS is -not defined and the macros are (cf ). - Added 23rd September 2000 (John Maddock). Modified 11th September 2001 to be excluded when BOOST_HAS_STDINT_H is defined (John Maddock). +Modified 11th Dec 2009 to always define the +INT#_C macros if they're not already defined (John Maddock). ******************************************************/ -#if defined(__STDC_CONSTANT_MACROS) && !defined(BOOST__STDC_CONSTANT_MACROS_DEFINED) && !defined(BOOST_HAS_STDINT_H) +#if !defined(BOOST__STDC_CONSTANT_MACROS_DEFINED) && !defined(INT8_C) +#include # define BOOST__STDC_CONSTANT_MACROS_DEFINED # if defined(BOOST_HAS_MS_INT64) // @@ -412,27 +419,40 @@ BOOST_HAS_STDINT_H is defined (John Maddock). // 64-bit types + intmax_t and uintmax_t ----------------------------------// # if defined(BOOST_HAS_LONG_LONG) && \ - (defined(ULLONG_MAX) || defined(ULONG_LONG_MAX) || defined(ULONGLONG_MAX)) + (defined(ULLONG_MAX) || defined(ULONG_LONG_MAX) || defined(ULONGLONG_MAX) || defined(_LLONG_MAX)) # if defined(__hpux) - // HP-UX's value of ULONG_LONG_MAX is unusable in preprocessor expressions -# elif (defined(ULLONG_MAX) && ULLONG_MAX == 18446744073709551615U) || \ - (defined(ULONG_LONG_MAX) && ULONG_LONG_MAX == 18446744073709551615U) || \ - (defined(ULONGLONG_MAX) && ULONGLONG_MAX == 18446744073709551615U) + // HP-UX's value of ULONG_LONG_MAX is unusable in preprocessor expressions +# define INT64_C(value) value##LL +# define UINT64_C(value) value##uLL +# elif (defined(ULLONG_MAX) && ULLONG_MAX == 18446744073709551615ULL) || \ + (defined(ULONG_LONG_MAX) && ULONG_LONG_MAX == 18446744073709551615ULL) || \ + (defined(ULONGLONG_MAX) && ULONGLONG_MAX == 18446744073709551615ULL) || \ + (defined(_LLONG_MAX) && _LLONG_MAX == 18446744073709551615ULL) +# define INT64_C(value) value##LL +# define UINT64_C(value) value##uLL # else # error defaults not correct; you must hand modify boost/cstdint.hpp # endif -# define INT64_C(value) value##LL -# define UINT64_C(value) value##uLL # elif ULONG_MAX != 0xffffffff -# if ULONG_MAX == 18446744073709551615 // 2**64 - 1 +# if ULONG_MAX == 18446744073709551615U // 2**64 - 1 # define INT64_C(value) value##L # define UINT64_C(value) value##uL # else # error defaults not correct; you must hand modify boost/cstdint.hpp # endif +# elif defined(BOOST_HAS_LONG_LONG) + // Usual macros not defined, work things out for ourselves: +# if(~0uLL == 18446744073709551615ULL) +# define INT64_C(value) value##LL +# define UINT64_C(value) value##uLL +# else +# error defaults not correct; you must hand modify boost/cstdint.hpp +# endif +# else +# error defaults not correct; you must hand modify boost/cstdint.hpp # endif # ifdef BOOST_NO_INT64_T @@ -445,23 +465,7 @@ BOOST_HAS_STDINT_H is defined (John Maddock). # endif // Borland/Microsoft specific width suffixes - -#elif defined(BOOST__STDC_CONSTANT_MACROS_DEFINED) && !defined(__STDC_CONSTANT_MACROS) && !defined(BOOST_HAS_STDINT_H) -// -// undef all the macros: -// -# undef INT8_C -# undef INT16_C -# undef INT32_C -# undef INT64_C -# undef UINT8_C -# undef UINT16_C -# undef UINT32_C -# undef UINT64_C -# undef INTMAX_C -# undef UINTMAX_C - -#endif // __STDC_CONSTANT_MACROS_DEFINED etc. +#endif // INT#_C macros. diff --git a/include/boost/integer.hpp b/include/boost/integer.hpp index 69a3318..3393c81 100644 --- a/include/boost/integer.hpp +++ b/include/boost/integer.hpp @@ -234,6 +234,7 @@ namespace boost < 5 + #if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG) + (MaxValue <= ::boost::integer_traits::const_max) + #else 1 + #endif diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 602f864..690befa 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -7,7 +7,9 @@ import testing ; project : requirements all gcc:-Wextra ; test-suite integer - : [ run cstdint_test.cpp : : : gcc:-Wno-long-long darwin:-Wno-long-long ] + : + [ run cstdint_test.cpp : : : gcc:-Wno-long-long darwin:-Wno-long-long ] + [ run cstdint_test2.cpp : : : gcc:-Wno-long-long darwin:-Wno-long-long ] [ run integer_traits_test.cpp ] [ run integer_test.cpp : : : gcc:-Wno-long-long darwin:-Wno-long-long sun:"-Qoption ccfe -tmpldepth=128" ] [ run integer_mask_test.cpp ] diff --git a/test/cstdint_test.cpp b/test/cstdint_test.cpp index c329e8f..f3cbd9b 100644 --- a/test/cstdint_test.cpp +++ b/test/cstdint_test.cpp @@ -13,15 +13,23 @@ // 23 Sep 00 Added INTXX_C constant macro support + int64_t support (John Maddock). // 28 Jun 00 Initial version +// +// There are two ways to test this: in version 1, we include cstdint.hpp as the first +// include, which means we get decide whether __STDC_CONSTANT_MACROS is defined. +// In version two we include stdint.h with __STDC_CONSTANT_MACROS *NOT* defined first, +// and check that we still end up with compatible definitions for the INT#_C macros. +// +// This is version 1. +// + #if defined(__GNUC__) && (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 4)) // We can't suppress this warning on the command line as not all GCC versions support -Wno-type-limits : #pragma GCC diagnostic ignored "-Wtype-limits" #endif -#define __STDC_CONSTANT_MACROS -#include #include #include +#include #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION // diff --git a/test/cstdint_test2.cpp b/test/cstdint_test2.cpp new file mode 100644 index 0000000..91ff28f --- /dev/null +++ b/test/cstdint_test2.cpp @@ -0,0 +1,248 @@ +// boost cstdint.hpp test program ------------------------------------------// + +// Copyright Beman Dawes 2000. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + + +// See http://www.boost.org/libs/integer for documentation. + +// Revision History +// 11 Sep 01 Adapted to work with macros defined in native stdint.h (John Maddock) +// 12 Nov 00 Adapted to merged +// 23 Sep 00 Added INTXX_C constant macro support + int64_t support (John Maddock). +// 28 Jun 00 Initial version + +// +// There are two ways to test this: in version 1, we include cstdint.hpp as the first +// include, which means we get decide whether __STDC_CONSTANT_MACROS is defined. +// In version two we include stdint.h with __STDC_CONSTANT_MACROS *NOT* defined first, +// and check that we still end up with compatible definitions for the INT#_C macros. +// +// This is version 2. +// + +#if defined(__GNUC__) && (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 4)) +// We can't suppress this warning on the command line as not all GCC versions support -Wno-type-limits : +#pragma GCC diagnostic ignored "-Wtype-limits" +#endif + +#include + +#ifdef BOOST_HAS_STDINT_H +#ifdef __hpux +# include +#else +# include +#endif +#endif + +#include +#include +#include + +#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION +// +// the following class is designed to verify +// that the various INTXX_C macros can be used +// in integral constant expressions: +// +struct integral_constant_checker +{ + static const boost::int8_t int8 = INT8_C(-127); + static const boost::int_least8_t int_least8 = INT8_C(-127); + static const boost::int_fast8_t int_fast8 = INT8_C(-127); + + static const boost::uint8_t uint8 = UINT8_C(255); + static const boost::uint_least8_t uint_least8 = UINT8_C(255); + static const boost::uint_fast8_t uint_fast8 = UINT8_C(255); + + static const boost::int16_t int16 = INT16_C(-32767); + static const boost::int_least16_t int_least16 = INT16_C(-32767); + static const boost::int_fast16_t int_fast16 = INT16_C(-32767); + + static const boost::uint16_t uint16 = UINT16_C(65535); + static const boost::uint_least16_t uint_least16 = UINT16_C(65535); + static const boost::uint_fast16_t uint_fast16 = UINT16_C(65535); + + static const boost::int32_t int32 = INT32_C(-2147483647); + static const boost::int_least32_t int_least32 = INT32_C(-2147483647); + static const boost::int_fast32_t int_fast32 = INT32_C(-2147483647); + + static const boost::uint32_t uint32 = UINT32_C(4294967295); + static const boost::uint_least32_t uint_least32 = UINT32_C(4294967295); + static const boost::uint_fast32_t uint_fast32 = UINT32_C(4294967295); + + static void check(); +}; + +void integral_constant_checker::check() +{ + BOOST_TEST( int8 == -127 ); + BOOST_TEST( int_least8 == -127 ); + BOOST_TEST( int_fast8 == -127 ); + BOOST_TEST( uint8 == 255u ); + BOOST_TEST( uint_least8 == 255u ); + BOOST_TEST( uint_fast8 == 255u ); + BOOST_TEST( int16 == -32767 ); + BOOST_TEST( int_least16 == -32767 ); + BOOST_TEST( int_fast16 == -32767 ); + BOOST_TEST( uint16 == 65535u ); + BOOST_TEST( uint_least16 == 65535u ); + BOOST_TEST( uint_fast16 == 65535u ); + BOOST_TEST( int32 == -2147483647 ); + BOOST_TEST( int_least32 == -2147483647 ); + BOOST_TEST( int_fast32 == -2147483647 ); + BOOST_TEST( uint32 == 4294967295u ); + BOOST_TEST( uint_least32 == 4294967295u ); + BOOST_TEST( uint_fast32 == 4294967295u ); +} +#endif // BOOST_NO_INCLASS_MEMBER_INITIALIZATION + +// +// the following function simply verifies that the type +// of an integral constant is correctly defined: +// +#ifdef __BORLANDC__ +#pragma option -w-8008 +#pragma option -w-8066 +#endif +template +void integral_constant_type_check(T1, T2) +{ + // + // the types T1 and T2 may not be exactly + // the same type, but they should be the + // same size and signedness. We could use + // numeric_limits to verify this, but + // numeric_limits implementations currently + // vary too much, or are incomplete or missing. + // + T1 t1 = static_cast(-1); // cast suppresses warnings + T2 t2 = static_cast(-1); // ditto +#if defined(BOOST_HAS_STDINT_H) + // if we have a native stdint.h + // then the INTXX_C macros may define + // a type that's wider than required: + BOOST_TEST(sizeof(T1) <= sizeof(T2)); +#else + BOOST_TEST(sizeof(T1) == sizeof(T2)); + BOOST_TEST(t1 == t2); +#endif +#if defined(BOOST_HAS_STDINT_H) + // native headers are permitted to promote small + // unsigned types to type int: + if(sizeof(T1) >= sizeof(int)) + { + if(t1 > 0) + BOOST_TEST(t2 > 0); + else + BOOST_TEST(!(t2 > 0)); + } + else if(t1 < 0) + BOOST_TEST(!(t2 > 0)); +#else + if(t1 > 0) + BOOST_TEST(t2 > 0); + else + BOOST_TEST(!(t2 > 0)); +#endif +} + + +int main(int, char*[]) +{ +#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION + integral_constant_checker::check(); +#endif + // + // verify the types of the integral constants: + // + integral_constant_type_check(boost::int8_t(0), INT8_C(0)); + integral_constant_type_check(boost::uint8_t(0), UINT8_C(0)); + integral_constant_type_check(boost::int16_t(0), INT16_C(0)); + integral_constant_type_check(boost::uint16_t(0), UINT16_C(0)); + integral_constant_type_check(boost::int32_t(0), INT32_C(0)); + integral_constant_type_check(boost::uint32_t(0), UINT32_C(0)); +#ifndef BOOST_NO_INT64_T + integral_constant_type_check(boost::int64_t(0), INT64_C(0)); + integral_constant_type_check(boost::uint64_t(0), UINT64_C(0)); +#endif + // + boost::int8_t int8 = INT8_C(-127); + boost::int_least8_t int_least8 = INT8_C(-127); + boost::int_fast8_t int_fast8 = INT8_C(-127); + + boost::uint8_t uint8 = UINT8_C(255); + boost::uint_least8_t uint_least8 = UINT8_C(255); + boost::uint_fast8_t uint_fast8 = UINT8_C(255); + + boost::int16_t int16 = INT16_C(-32767); + boost::int_least16_t int_least16 = INT16_C(-32767); + boost::int_fast16_t int_fast16 = INT16_C(-32767); + + boost::uint16_t uint16 = UINT16_C(65535); + boost::uint_least16_t uint_least16 = UINT16_C(65535); + boost::uint_fast16_t uint_fast16 = UINT16_C(65535); + + boost::int32_t int32 = INT32_C(-2147483647); + boost::int_least32_t int_least32 = INT32_C(-2147483647); + boost::int_fast32_t int_fast32 = INT32_C(-2147483647); + + boost::uint32_t uint32 = UINT32_C(4294967295); + boost::uint_least32_t uint_least32 = UINT32_C(4294967295); + boost::uint_fast32_t uint_fast32 = UINT32_C(4294967295); + +#ifndef BOOST_NO_INT64_T + boost::int64_t int64 = INT64_C(-9223372036854775807); + boost::int_least64_t int_least64 = INT64_C(-9223372036854775807); + boost::int_fast64_t int_fast64 = INT64_C(-9223372036854775807); + + boost::uint64_t uint64 = UINT64_C(18446744073709551615); + boost::uint_least64_t uint_least64 = UINT64_C(18446744073709551615); + boost::uint_fast64_t uint_fast64 = UINT64_C(18446744073709551615); + + boost::intmax_t intmax = INTMAX_C(-9223372036854775807); + boost::uintmax_t uintmax = UINTMAX_C(18446744073709551615); +#else + boost::intmax_t intmax = INTMAX_C(-2147483647); + boost::uintmax_t uintmax = UINTMAX_C(4294967295); +#endif + + BOOST_TEST( int8 == -127 ); + BOOST_TEST( int_least8 == -127 ); + BOOST_TEST( int_fast8 == -127 ); + BOOST_TEST( uint8 == 255u ); + BOOST_TEST( uint_least8 == 255u ); + BOOST_TEST( uint_fast8 == 255u ); + BOOST_TEST( int16 == -32767 ); + BOOST_TEST( int_least16 == -32767 ); + BOOST_TEST( int_fast16 == -32767 ); + BOOST_TEST( uint16 == 65535u ); + BOOST_TEST( uint_least16 == 65535u ); + BOOST_TEST( uint_fast16 == 65535u ); + BOOST_TEST( int32 == -2147483647 ); + BOOST_TEST( int_least32 == -2147483647 ); + BOOST_TEST( int_fast32 == -2147483647 ); + BOOST_TEST( uint32 == 4294967295u ); + BOOST_TEST( uint_least32 == 4294967295u ); + BOOST_TEST( uint_fast32 == 4294967295u ); + +#ifndef BOOST_NO_INT64_T + BOOST_TEST( int64 == INT64_C(-9223372036854775807) ); + BOOST_TEST( int_least64 == INT64_C(-9223372036854775807) ); + BOOST_TEST( int_fast64 == INT64_C(-9223372036854775807) ); + BOOST_TEST( uint64 == UINT64_C(18446744073709551615) ); + BOOST_TEST( uint_least64 == UINT64_C(18446744073709551615) ); + BOOST_TEST( uint_fast64 == UINT64_C(18446744073709551615) ); + BOOST_TEST( intmax == INT64_C(-9223372036854775807) ); + BOOST_TEST( uintmax == UINT64_C(18446744073709551615) ); +#else + BOOST_TEST( intmax == -2147483647 ); + BOOST_TEST( uintmax == 4294967295u ); +#endif + + + std::cout << "OK\n"; + return boost::report_errors(); +}