2013-08-08 09:54:33 +00:00
|
|
|
|
/*=============================================================================
|
2014-01-08 07:48:12 +00:00
|
|
|
|
Copyright (c) 2011-2014 Bolero MURAKAMI
|
2013-08-08 09:54:33 +00:00
|
|
|
|
Copyright (C) 2011 RiSK (sscrisk)
|
|
|
|
|
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)
|
|
|
|
|
=============================================================================*/
|
2012-04-01 13:15:09 +00:00
|
|
|
|
#ifndef SPROUT_CSTDLIB_DIV_HPP
|
|
|
|
|
#define SPROUT_CSTDLIB_DIV_HPP
|
|
|
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
#include <type_traits>
|
|
|
|
|
#include <sprout/config.hpp>
|
2014-04-30 07:30:26 +00:00
|
|
|
|
#include <sprout/workaround/std/cstddef.hpp>
|
2012-04-01 13:15:09 +00:00
|
|
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
template<typename T>
|
|
|
|
|
struct div_t_traits {};
|
|
|
|
|
|
2012-05-05 06:57:55 +00:00
|
|
|
|
# define SPROUT_DETAIL_DIV_T_TRAITS_IMPL(INT_T, DIV_T) \
|
|
|
|
|
template<> \
|
|
|
|
|
struct div_t_traits<INT_T> { \
|
|
|
|
|
public: \
|
|
|
|
|
typedef DIV_T type; \
|
2012-06-15 15:08:42 +00:00
|
|
|
|
SPROUT_STATIC_CONSTEXPR std::size_t offsetof_quot = offsetof(DIV_T, quot); \
|
|
|
|
|
SPROUT_STATIC_CONSTEXPR std::size_t offsetof_rem = offsetof(DIV_T, rem); \
|
2012-05-05 06:57:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SPROUT_DETAIL_DIV_T_TRAITS_IMPL(int, std::div_t);
|
|
|
|
|
SPROUT_DETAIL_DIV_T_TRAITS_IMPL(long, std::ldiv_t);
|
|
|
|
|
SPROUT_DETAIL_DIV_T_TRAITS_IMPL(long long, std::lldiv_t);
|
|
|
|
|
# undef SPROUT_DETAIL_DIV_T_TRAITS_IMPL
|
2012-04-01 13:15:09 +00:00
|
|
|
|
|
2012-12-17 11:10:17 +00:00
|
|
|
|
template<typename T>
|
|
|
|
|
inline SPROUT_CONSTEXPR typename std::enable_if<
|
|
|
|
|
sprout::detail::div_t_traits<T>::offsetof_quot == 0,
|
|
|
|
|
typename sprout::detail::div_t_traits<T>::type
|
|
|
|
|
>::type
|
2012-10-05 15:58:56 +00:00
|
|
|
|
div_impl(T const& numer, T const& denom) {
|
2012-12-17 11:10:17 +00:00
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
|
typename sprout::detail::div_t_traits<T>::type result = {numer / denom, numer % denom};
|
|
|
|
|
return result;
|
|
|
|
|
#else
|
2012-04-01 13:15:09 +00:00
|
|
|
|
return {numer / denom, numer % denom};
|
2012-12-17 11:10:17 +00:00
|
|
|
|
#endif
|
2012-04-01 13:15:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-12-17 11:10:17 +00:00
|
|
|
|
template<typename T>
|
|
|
|
|
inline SPROUT_CONSTEXPR typename std::enable_if<
|
|
|
|
|
sprout::detail::div_t_traits<T>::offsetof_rem == 0,
|
|
|
|
|
typename sprout::detail::div_t_traits<T>::type
|
|
|
|
|
>::type
|
2012-10-05 15:58:56 +00:00
|
|
|
|
div_impl(T const &numer, T const& denom) {
|
2012-12-17 11:10:17 +00:00
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
|
typename sprout::detail::div_t_traits<T>::type result = {numer % denom, numer / denom};
|
|
|
|
|
return result;
|
|
|
|
|
#else
|
2012-04-01 13:15:09 +00:00
|
|
|
|
return {numer % denom, numer / denom};
|
2012-12-17 11:10:17 +00:00
|
|
|
|
#endif
|
2012-04-01 13:15:09 +00:00
|
|
|
|
}
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
2013-03-22 05:24:19 +00:00
|
|
|
|
// 7.20.6.2 div<69>Cldiv<69>C<EFBFBD>y<EFBFBD><79> lldiv <20><EFBFBD>
|
2012-10-05 15:58:56 +00:00
|
|
|
|
inline SPROUT_CONSTEXPR std::div_t
|
|
|
|
|
div(int numer, int denom) {
|
2012-04-01 13:15:09 +00:00
|
|
|
|
return sprout::detail::div_impl(numer, denom);
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-05 15:58:56 +00:00
|
|
|
|
inline SPROUT_CONSTEXPR std::ldiv_t
|
|
|
|
|
ldiv(long numer, long denom) {
|
2012-04-01 13:15:09 +00:00
|
|
|
|
return sprout::detail::div_impl(numer, denom);
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-05 15:58:56 +00:00
|
|
|
|
inline SPROUT_CONSTEXPR std::lldiv_t
|
|
|
|
|
lldiv(long long numer, long long denom) {
|
2012-04-01 13:15:09 +00:00
|
|
|
|
return sprout::detail::div_impl(numer, denom);
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-05 15:58:56 +00:00
|
|
|
|
inline SPROUT_CONSTEXPR std::ldiv_t
|
|
|
|
|
div(long numer, long denom) {
|
2012-04-01 13:15:09 +00:00
|
|
|
|
return sprout::ldiv(numer, denom);
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-05 15:58:56 +00:00
|
|
|
|
inline SPROUT_CONSTEXPR std::lldiv_t
|
|
|
|
|
div(long long numer, long long denom) {
|
2012-04-01 13:15:09 +00:00
|
|
|
|
return sprout::lldiv(numer, denom);
|
|
|
|
|
}
|
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_CSTDLIB_DIV_HPP
|