From 2fb6cea457c1a57dd72acf639bcc6de0ee3522fe Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Sat, 5 May 2012 15:57:55 +0900 Subject: [PATCH] add sprout::abs, fabs add cinttypes --- sprout/cinttypes.hpp | 8 +++ sprout/cinttypes/abs.hpp | 23 +++++++ sprout/cinttypes/arithmetic.hpp | 8 +++ sprout/cinttypes/conversion.hpp | 8 +++ sprout/cinttypes/div.hpp | 63 +++++++++++++++++++ sprout/{cstdlib => cinttypes}/strtoimax.hpp | 6 +- sprout/{cstdlib => cinttypes}/strtoumax.hpp | 6 +- sprout/cstdlib/abs.hpp | 4 +- sprout/cstdlib/conversion.hpp | 2 - sprout/cstdlib/div.hpp | 46 ++++++-------- sprout/math/abs.hpp | 67 +++++++++++++++++++++ sprout/math/functions.hpp | 1 + sprout/math/operations.hpp | 6 ++ 13 files changed, 211 insertions(+), 37 deletions(-) create mode 100644 sprout/cinttypes.hpp create mode 100644 sprout/cinttypes/abs.hpp create mode 100644 sprout/cinttypes/arithmetic.hpp create mode 100644 sprout/cinttypes/conversion.hpp create mode 100644 sprout/cinttypes/div.hpp rename sprout/{cstdlib => cinttypes}/strtoimax.hpp (84%) rename sprout/{cstdlib => cinttypes}/strtoumax.hpp (84%) create mode 100644 sprout/math/abs.hpp create mode 100644 sprout/math/operations.hpp diff --git a/sprout/cinttypes.hpp b/sprout/cinttypes.hpp new file mode 100644 index 00000000..05b54dde --- /dev/null +++ b/sprout/cinttypes.hpp @@ -0,0 +1,8 @@ +#ifndef SPROUT_CINTTYPES_HPP +#define SPROUT_CINTTYPES_HPP + +#include +#include +#include + +#endif // #ifndef SPROUT_CINTTYPES_HPP diff --git a/sprout/cinttypes/abs.hpp b/sprout/cinttypes/abs.hpp new file mode 100644 index 00000000..4abb84d6 --- /dev/null +++ b/sprout/cinttypes/abs.hpp @@ -0,0 +1,23 @@ +#ifndef SPROUT_CINTTYPES_ABS_HPP +#define SPROUT_CINTTYPES_ABS_HPP + +#include +#include +#include +#include + +namespace sprout { + SPROUT_CONSTEXPR std::intmax_t imaxabs(std::intmax_t j) { + return j < 0 ? -j : j; + } + + template< + typename T, + typename sprout::enabler_if::value>::type = sprout::enabler + > + SPROUT_CONSTEXPR T abs(T j) { + return sprout::imaxabs(j); + } +} // namespace sprout + +#endif // #ifndef SPROUT_CINTTYPES_ABS_HPP diff --git a/sprout/cinttypes/arithmetic.hpp b/sprout/cinttypes/arithmetic.hpp new file mode 100644 index 00000000..95c0fa78 --- /dev/null +++ b/sprout/cinttypes/arithmetic.hpp @@ -0,0 +1,8 @@ +#ifndef SPROUT_CINTTYPES_ARITHMETIC_HPP +#define SPROUT_CINTTYPES_ARITHMETIC_HPP + +#include +#include +#include + +#endif // #ifndef SPROUT_CINTTYPES_ARITHMETIC_HPP diff --git a/sprout/cinttypes/conversion.hpp b/sprout/cinttypes/conversion.hpp new file mode 100644 index 00000000..6bb239ba --- /dev/null +++ b/sprout/cinttypes/conversion.hpp @@ -0,0 +1,8 @@ +#ifndef SPROUT_CINTTYPES_CONVERSION_HPP +#define SPROUT_CINTTYPES_CONVERSION_HPP + +#include +#include +#include + +#endif // #ifndef SPROUT_CINTTYPES_CONVERSION_HPP diff --git a/sprout/cinttypes/div.hpp b/sprout/cinttypes/div.hpp new file mode 100644 index 00000000..304b35c1 --- /dev/null +++ b/sprout/cinttypes/div.hpp @@ -0,0 +1,63 @@ +#ifndef SPROUT_CINTTYPES_DIV_HPP +#define SPROUT_CINTTYPES_DIV_HPP + +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace detail { + template + struct div_t_traits2 {}; + +# define SPROUT_DETAIL_DIV_T_TRAITS2_IMPL(INT_T, DIV_T) \ + template<> \ + struct div_t_traits2 { \ + public: \ + typedef DIV_T type; \ + static SPROUT_CONSTEXPR std::size_t offsetof_quot = offsetof(DIV_T, quot); \ + static SPROUT_CONSTEXPR std::size_t offsetof_rem = offsetof(DIV_T, rem); \ + } + + SPROUT_DETAIL_DIV_T_TRAITS2_IMPL(std::intmax_t, std::imaxdiv_t); +# undef SPROUT_DETAIL_DIV_T_TRAITS2_IMPL + + template< + typename T, + typename sprout::enabler_if< + sprout::detail::div_t_traits2::offsetof_quot == 0 + >::type = sprout::enabler + > + SPROUT_CONSTEXPR typename sprout::detail::div_t_traits2::type div_impl2(T const& numer, T const& denom) { + return {numer / denom, numer % denom}; + } + + template< + typename T, + typename sprout::enabler_if< + sprout::detail::div_t_traits2::offsetof_rem == 0 + >::type = sprout::enabler + > + SPROUT_CONSTEXPR typename sprout::detail::div_t_traits2::type div_impl2(T const &numer, T const& denom) { + return {numer % denom, numer / denom}; + } + } // namespace detail + + SPROUT_CONSTEXPR std::imaxdiv_t imaxdiv(std::intmax_t numer, std::intmax_t denom) { + return sprout::detail::div_impl2(numer, denom); + } + + template< + typename T, + typename sprout::enabler_if::value>::type = sprout::enabler + > + SPROUT_CONSTEXPR std::imaxdiv_t div(T numer, T denom) { + return sprout::imaxdiv(numer, denom); + } +} // namespace sprout + +#endif // #ifndef SPROUT_CINTTYPES_DIV_HPP diff --git a/sprout/cstdlib/strtoimax.hpp b/sprout/cinttypes/strtoimax.hpp similarity index 84% rename from sprout/cstdlib/strtoimax.hpp rename to sprout/cinttypes/strtoimax.hpp index 89d438b3..aa6c9f20 100644 --- a/sprout/cstdlib/strtoimax.hpp +++ b/sprout/cinttypes/strtoimax.hpp @@ -1,5 +1,5 @@ -#ifndef SPROUT_CSTDLIB_STRTOIMAX_HPP -#define SPROUT_CSTDLIB_STRTOIMAX_HPP +#ifndef SPROUT_CINTTYPES_STRTOIMAX_HPP +#define SPROUT_CINTTYPES_STRTOIMAX_HPP #include #include @@ -27,4 +27,4 @@ namespace sprout { } } // namespace sprout -#endif // #ifndef SPROUT_CSTDLIB_STRTOIMAX_HPP +#endif // #ifndef SPROUT_CINTTYPES_STRTOIMAX_HPP diff --git a/sprout/cstdlib/strtoumax.hpp b/sprout/cinttypes/strtoumax.hpp similarity index 84% rename from sprout/cstdlib/strtoumax.hpp rename to sprout/cinttypes/strtoumax.hpp index 017e412e..5f25543b 100644 --- a/sprout/cstdlib/strtoumax.hpp +++ b/sprout/cinttypes/strtoumax.hpp @@ -1,5 +1,5 @@ -#ifndef SPROUT_CSTDLIB_STRTTOUMAX_HPP -#define SPROUT_CSTDLIB_STRTTOUMAX_HPP +#ifndef SPROUT_CINTTYPES_STRTOUMAX_HPP +#define SPROUT_CINTTYPES_STRTOUMAX_HPP #include #include @@ -27,4 +27,4 @@ namespace sprout { } } // namespace sprout -#endif // #ifndef SPROUT_CSTDLIB_STRTTOUMAX_HPP +#endif // #ifndef SPROUT_CINTTYPES_STRTOUMAX_HPP diff --git a/sprout/cstdlib/abs.hpp b/sprout/cstdlib/abs.hpp index ba9f4f25..26666c5e 100644 --- a/sprout/cstdlib/abs.hpp +++ b/sprout/cstdlib/abs.hpp @@ -20,11 +20,11 @@ namespace sprout { } SPROUT_CONSTEXPR long abs(long j) { - return labs(j); + return sprout::labs(j); } SPROUT_CONSTEXPR long long abs(long long j) { - return llabs(j); + return sprout::llabs(j); } } // namespace sprout diff --git a/sprout/cstdlib/conversion.hpp b/sprout/cstdlib/conversion.hpp index e133d6f4..f1c3bc5d 100644 --- a/sprout/cstdlib/conversion.hpp +++ b/sprout/cstdlib/conversion.hpp @@ -16,7 +16,5 @@ #include #include #include -#include -#include #endif // #ifndef SPROUT_CSTDLIB_CONVERSION_HPP diff --git a/sprout/cstdlib/div.hpp b/sprout/cstdlib/div.hpp index ca83dbd0..5107e4fb 100644 --- a/sprout/cstdlib/div.hpp +++ b/sprout/cstdlib/div.hpp @@ -5,42 +5,34 @@ #include #include #include +#include namespace sprout { // Copyright (C) 2011 RiSK (sscrisk) namespace detail { -# define SPROUT_DIV_T_TRAITS_IMPL(DIV_T) \ - typedef DIV_T type; \ - static SPROUT_CONSTEXPR std::size_t offsetof_quot = offsetof(DIV_T, quot); \ - static SPROUT_CONSTEXPR std::size_t offsetof_rem = offsetof(DIV_T, rem) - template struct div_t_traits {}; - template<> - struct div_t_traits { - public: - SPROUT_DIV_T_TRAITS_IMPL(std::div_t); - }; - template<> - struct div_t_traits { - public: - SPROUT_DIV_T_TRAITS_IMPL(std::ldiv_t); - }; - template<> - struct div_t_traits { - public: - SPROUT_DIV_T_TRAITS_IMPL(std::lldiv_t); - }; -# undef SPROUT_DIV_T_TRAITS_IMPL - extern void* enabler; +# define SPROUT_DETAIL_DIV_T_TRAITS_IMPL(INT_T, DIV_T) \ + template<> \ + struct div_t_traits { \ + public: \ + typedef DIV_T type; \ + static SPROUT_CONSTEXPR std::size_t offsetof_quot = offsetof(DIV_T, quot); \ + static SPROUT_CONSTEXPR std::size_t offsetof_rem = offsetof(DIV_T, rem); \ + } + + 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 template< typename T, - typename std::enable_if< + typename sprout::enabler_if< sprout::detail::div_t_traits::offsetof_quot == 0 - >::type*& = sprout::detail::enabler + >::type = sprout::enabler > SPROUT_CONSTEXPR typename sprout::detail::div_t_traits::type div_impl(T const& numer, T const& denom) { return {numer / denom, numer % denom}; @@ -48,9 +40,9 @@ namespace sprout { template< typename T, - typename std::enable_if< + typename sprout::enabler_if< sprout::detail::div_t_traits::offsetof_rem == 0 - >::type*& = sprout::detail::enabler + >::type = sprout::enabler > SPROUT_CONSTEXPR typename sprout::detail::div_t_traits::type div_impl(T const &numer, T const& denom) { return {numer % denom, numer / denom}; @@ -66,7 +58,7 @@ namespace sprout { return sprout::detail::div_impl(numer, denom); } - SPROUT_CONSTEXPR std::lldiv_t lldiv(long long numer, long long denom) { + SPROUT_CONSTEXPR std::lldiv_t lldiv(long long numer, long long denom) { return sprout::detail::div_impl(numer, denom); } diff --git a/sprout/math/abs.hpp b/sprout/math/abs.hpp new file mode 100644 index 00000000..fcc5ed71 --- /dev/null +++ b/sprout/math/abs.hpp @@ -0,0 +1,67 @@ +#ifndef SPROUT_MATH_ABS_HPP +#define SPROUT_MATH_ABS_HPP + +#include +#include +#include +#if SPROUT_USE_BUILTIN_CMATH_FUNCTION +# include +#endif + +namespace sprout { + namespace math { + namespace detail { + template< + typename FloatType, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR FloatType + abs(FloatType x) { + return x < 0 ? -x : x; + } + + template< + typename FloatType, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR FloatType + fabs(FloatType x) { + return x < 0 ? -x : x; + } + + template< + typename IntType, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR double + fabs(IntType x) { + return sprout::math::detail::fabs(static_cast(x)); + } + } // namespace detail + + template< + typename FloatType, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR FloatType + abs(FloatType x) { +# if SPROUT_USE_BUILTIN_CMATH_FUNCTION + using std::abs; +# else + using sprout::math::detail::abs; +# endif + return abs(x); + } + +# if SPROUT_USE_BUILTIN_CMATH_FUNCTION + using std::fabs; +# else + using sprout::math::detail::fabs; +# endif + } // namespace math + + using sprout::math::abs; + using sprout::math::fabs; +} // namespace sprout + +#endif // #ifndef SPROUT_MATH_ABS_HPP diff --git a/sprout/math/functions.hpp b/sprout/math/functions.hpp index 5d984352..2ded5954 100644 --- a/sprout/math/functions.hpp +++ b/sprout/math/functions.hpp @@ -2,6 +2,7 @@ #define SPROUT_MATH_FUNCTIONS_HPP #include +#include #include #include #include diff --git a/sprout/math/operations.hpp b/sprout/math/operations.hpp new file mode 100644 index 00000000..b8ae419d --- /dev/null +++ b/sprout/math/operations.hpp @@ -0,0 +1,6 @@ +#ifndef SPROUT_MATH_OPERATIONS_HPP +#define SPROUT_MATH_OPERATIONS_HPP + +#include + +#endif // #ifndef SPROUT_MATH_OPERATIONS_HPP