Sprout/sprout/detail/integer.hpp

189 lines
5.9 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
2015-01-10 10:13:57 +00:00
Copyright (c) 2011-2015 Bolero MURAKAMI
2013-08-08 09:54:33 +00:00
https://github.com/bolero-MURAKAMI/Sprout
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)
=============================================================================*/
2011-10-03 09:27:50 +00:00
#ifndef SPROUT_DETAIL_INTEGER_HPP
#define SPROUT_DETAIL_INTEGER_HPP
#include <climits>
#include <sprout/config.hpp>
2013-08-06 15:15:09 +00:00
#include <sprout/limits.hpp>
2011-10-03 09:27:50 +00:00
namespace sprout {
namespace detail {
template<typename LeastInt>
struct int_fast_t {
typedef LeastInt fast;
2011-10-03 09:27:50 +00:00
typedef fast type;
};
template<int Category>
struct int_least_helper {};
template<>
struct int_least_helper<1> {
typedef long long least;
};
template<>
struct int_least_helper<2> {
typedef long least;
};
template<>
struct int_least_helper<3> {
typedef int least;
};
template<>
struct int_least_helper<4> {
typedef short least;
};
template<>
struct int_least_helper<5> {
typedef signed char least;
};
template<>
struct int_least_helper<6> {
typedef unsigned long long least;
};
template<>
struct int_least_helper<7> {
typedef unsigned long least;
};
template<>
struct int_least_helper<8> {
typedef unsigned int least;
};
template<>
struct int_least_helper<9> {
typedef unsigned short least;
};
template<>
struct int_least_helper<10> {
typedef unsigned char least;
};
template<int Bits>
struct exact_signed_base_helper {};
template<int Bits>
struct exact_unsigned_base_helper {};
template<>
2013-03-22 05:24:19 +00:00
struct exact_signed_base_helper<sizeof(signed char) * CHAR_BIT> {
2011-10-03 09:27:50 +00:00
typedef signed char exact;
};
template<>
2013-03-22 05:24:19 +00:00
struct exact_unsigned_base_helper<sizeof(unsigned char) * CHAR_BIT> {
2011-10-03 09:27:50 +00:00
typedef unsigned char exact;
};
#if USHRT_MAX != UCHAR_MAX
template<>
struct exact_signed_base_helper<sizeof(short) * CHAR_BIT> {
typedef short exact;
};
template<> struct exact_unsigned_base_helper<sizeof(unsigned short) * CHAR_BIT> {
typedef unsigned short exact;
};
#endif
#if UINT_MAX != USHRT_MAX
template<>
struct exact_signed_base_helper<sizeof(int) * CHAR_BIT> {
typedef int exact;
};
template<>
struct exact_unsigned_base_helper<sizeof(unsigned int) * CHAR_BIT> {
typedef unsigned int exact;
};
#endif
#if ULONG_MAX != UINT_MAX
template<>
struct exact_signed_base_helper<sizeof(long) * CHAR_BIT> {
typedef long exact;
};
template<>
struct exact_unsigned_base_helper<sizeof(unsigned long) * CHAR_BIT> {
typedef unsigned long exact;
};
#endif
#if ULLONG_MAX != ULONG_MAX
template<>
struct exact_signed_base_helper<sizeof(long long) * CHAR_BIT> {
typedef long long exact;
};
template<>
struct exact_unsigned_base_helper<sizeof(unsigned long long) * CHAR_BIT> {
typedef unsigned long long exact;
};
#endif
template<int Bits>
struct int_t
: public sprout::detail::exact_signed_base_helper<Bits>
{
typedef typename sprout::detail::int_least_helper<
0
2013-08-06 15:15:09 +00:00
+ (Bits - 1 <= sprout::numeric_limits<long long>::digits)
+ (Bits - 1 <= sprout::numeric_limits<long>::digits)
+ (Bits - 1 <= sprout::numeric_limits<int>::digits)
+ (Bits - 1 <= sprout::numeric_limits<short>::digits)
+ (Bits - 1 <= sprout::numeric_limits<signed char>::digits)
2011-10-03 09:27:50 +00:00
>::least least;
typedef typename sprout::detail::int_fast_t<least>::type fast;
};
template<int Bits>
struct uint_t
: public sprout::detail::exact_unsigned_base_helper<Bits>
{
typedef typename sprout::detail::int_least_helper<
5
2013-08-06 15:15:09 +00:00
+ (Bits <= sprout::numeric_limits<unsigned long long>::digits)
+ (Bits <= sprout::numeric_limits<unsigned long>::digits)
+ (Bits <= sprout::numeric_limits<unsigned int>::digits)
+ (Bits <= sprout::numeric_limits<unsigned short>::digits)
+ (Bits <= sprout::numeric_limits<unsigned char>::digits)
2011-10-03 09:27:50 +00:00
>::least least;
typedef typename sprout::detail::int_fast_t<least>::type fast;
};
template<long long MaxValue>
struct int_max_value_t {
typedef typename sprout::detail::int_least_helper<
0
2013-08-06 15:15:09 +00:00
+ (MaxValue <= sprout::numeric_limits<long long>::max())
+ (MaxValue <= sprout::numeric_limits<long>::max())
+ (MaxValue <= sprout::numeric_limits<int>::max())
+ (MaxValue <= sprout::numeric_limits<short>::max())
+ (MaxValue <= sprout::numeric_limits<signed char>::max())
2011-10-03 09:27:50 +00:00
>::least least;
typedef typename sprout::detail::int_fast_t<least>::type fast;
};
template<long long MinValue>
struct int_min_value_t {
typedef typename sprout::detail::int_least_helper<
0
2013-08-06 15:15:09 +00:00
+ (MinValue >= sprout::numeric_limits<long long>::min())
+ (MinValue >= sprout::numeric_limits<long>::min())
+ (MinValue >= sprout::numeric_limits<int>::min())
+ (MinValue >= sprout::numeric_limits<short>::min())
+ (MinValue >= sprout::numeric_limits<signed char>::min())
2011-10-03 09:27:50 +00:00
>::least least;
typedef typename sprout::detail::int_fast_t<least>::type fast;
};
template<unsigned long long MaxValue>
struct uint_value_t {
typedef typename sprout::detail::int_least_helper<
5
2015-12-09 10:54:35 +00:00
+ (MaxValue <= static_cast<unsigned long long>(sprout::numeric_limits<long long>::max()))
+ (MaxValue <= static_cast<unsigned long long>(sprout::numeric_limits<long>::max()))
+ (MaxValue <= static_cast<unsigned long long>(sprout::numeric_limits<int>::max()))
+ (MaxValue <= static_cast<unsigned long long>(sprout::numeric_limits<short>::max()))
+ (MaxValue <= static_cast<unsigned long long>(sprout::numeric_limits<signed char>::max()))
2011-10-03 09:27:50 +00:00
>::least least;
typedef typename sprout::detail::int_fast_t<least>::type fast;
};
} // namespace detail
} // namespace sprout
2013-03-22 05:24:19 +00:00
#endif // #ifndef SPROUT_DETAIL_INTEGER_HPP