Sprout/sprout/random/detail/signed_unsigned_tools.hpp

70 lines
2.1 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
Copyright (c) 2011-2017 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-12 20:28:33 +00:00
#ifndef SPROUT_RANDOM_DETAIL_SIGNED_UNSIGNED_TOOLS_HPP
#define SPROUT_RANDOM_DETAIL_SIGNED_UNSIGNED_TOOLS_HPP
#include <type_traits>
#include <sprout/config.hpp>
2013-08-06 15:15:09 +00:00
#include <sprout/limits.hpp>
2011-10-12 20:28:33 +00:00
namespace sprout {
namespace random {
namespace detail {
2013-08-06 15:15:09 +00:00
template<typename T, bool B = sprout::numeric_limits<T>::is_signed>
2011-10-12 20:28:33 +00:00
struct subtract {};
template<typename T>
struct subtract<T, false> {
public:
typedef T result_type;
public:
SPROUT_CONSTEXPR result_type operator()(T x, T y) const {
return x - y;
}
};
template<typename T>
struct subtract<T, true> {
public:
typedef typename std::make_unsigned<T>::type result_type;
public:
SPROUT_CONSTEXPR result_type operator()(T x, T y) const {
return y >= 0 ? result_type(x) - result_type(y)
: x >= 0 ? result_type(x) + result_type(-(y + 1)) + 1
: result_type(x - y)
;
}
};
2013-08-06 15:15:09 +00:00
template<typename T1, typename T2, bool B = sprout::numeric_limits<T2>::is_signed>
2011-10-12 20:28:33 +00:00
struct add {};
template<typename T1, typename T2>
struct add<T1, T2, false> {
public:
typedef T2 result_type;
public:
SPROUT_CONSTEXPR result_type operator()(T1 x, T2 y) const {
return T2(x) + y;
}
};
template<typename T1, typename T2>
struct add<T1, T2, true> {
public:
typedef T2 result_type;
public:
SPROUT_CONSTEXPR result_type operator()(T1 x, T2 y) const {
return y >= 0 ? T2(x) + y
: x >= T1(-(y + 1)) ? T2(x - T1(-(y + 1)) - 1)
: T2(x) + y
;
}
};
2013-03-22 05:24:19 +00:00
} // namespace detail
} // namespace random
} // namespace sprout
2011-10-12 20:28:33 +00:00
2013-03-22 05:24:19 +00:00
#endif // SPROUT_RANDOM_DETAIL_SIGNED_UNSIGNED_TOOLS_HPP