mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2024-12-23 21:25:49 +00:00
add sprout::abs, fabs
add cinttypes
This commit is contained in:
parent
8dadcb359a
commit
2fb6cea457
13 changed files with 211 additions and 37 deletions
8
sprout/cinttypes.hpp
Normal file
8
sprout/cinttypes.hpp
Normal file
|
@ -0,0 +1,8 @@
|
|||
#ifndef SPROUT_CINTTYPES_HPP
|
||||
#define SPROUT_CINTTYPES_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/cstdlib/conversion.hpp>
|
||||
#include <sprout/cstdlib/arithmetic.hpp>
|
||||
|
||||
#endif // #ifndef SPROUT_CINTTYPES_HPP
|
23
sprout/cinttypes/abs.hpp
Normal file
23
sprout/cinttypes/abs.hpp
Normal file
|
@ -0,0 +1,23 @@
|
|||
#ifndef SPROUT_CINTTYPES_ABS_HPP
|
||||
#define SPROUT_CINTTYPES_ABS_HPP
|
||||
|
||||
#include <cstdint>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/utility/enabler_if.hpp>
|
||||
|
||||
namespace sprout {
|
||||
SPROUT_CONSTEXPR std::intmax_t imaxabs(std::intmax_t j) {
|
||||
return j < 0 ? -j : j;
|
||||
}
|
||||
|
||||
template<
|
||||
typename T,
|
||||
typename sprout::enabler_if<std::is_same<T, std::intmax_t>::value>::type = sprout::enabler
|
||||
>
|
||||
SPROUT_CONSTEXPR T abs(T j) {
|
||||
return sprout::imaxabs(j);
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_CINTTYPES_ABS_HPP
|
8
sprout/cinttypes/arithmetic.hpp
Normal file
8
sprout/cinttypes/arithmetic.hpp
Normal file
|
@ -0,0 +1,8 @@
|
|||
#ifndef SPROUT_CINTTYPES_ARITHMETIC_HPP
|
||||
#define SPROUT_CINTTYPES_ARITHMETIC_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/cinttypes/abs.hpp>
|
||||
#include <sprout/cinttypes/div.hpp>
|
||||
|
||||
#endif // #ifndef SPROUT_CINTTYPES_ARITHMETIC_HPP
|
8
sprout/cinttypes/conversion.hpp
Normal file
8
sprout/cinttypes/conversion.hpp
Normal file
|
@ -0,0 +1,8 @@
|
|||
#ifndef SPROUT_CINTTYPES_CONVERSION_HPP
|
||||
#define SPROUT_CINTTYPES_CONVERSION_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/cinttypes/strtoimax.hpp>
|
||||
#include <sprout/cinttypes/strtoumax.hpp>
|
||||
|
||||
#endif // #ifndef SPROUT_CINTTYPES_CONVERSION_HPP
|
63
sprout/cinttypes/div.hpp
Normal file
63
sprout/cinttypes/div.hpp
Normal file
|
@ -0,0 +1,63 @@
|
|||
#ifndef SPROUT_CINTTYPES_DIV_HPP
|
||||
#define SPROUT_CINTTYPES_DIV_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdlib>
|
||||
#include <cstdint>
|
||||
#include <cinttypes>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/utility/enabler_if.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace detail {
|
||||
template<typename T>
|
||||
struct div_t_traits2 {};
|
||||
|
||||
# define SPROUT_DETAIL_DIV_T_TRAITS2_IMPL(INT_T, DIV_T) \
|
||||
template<> \
|
||||
struct div_t_traits2<INT_T> { \
|
||||
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<T>::offsetof_quot == 0
|
||||
>::type = sprout::enabler
|
||||
>
|
||||
SPROUT_CONSTEXPR typename sprout::detail::div_t_traits2<T>::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<T>::offsetof_rem == 0
|
||||
>::type = sprout::enabler
|
||||
>
|
||||
SPROUT_CONSTEXPR typename sprout::detail::div_t_traits2<T>::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<std::is_same<T, std::intmax_t>::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
|
|
@ -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 <cstddef>
|
||||
#include <cstdint>
|
||||
|
@ -27,4 +27,4 @@ namespace sprout {
|
|||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_CSTDLIB_STRTOIMAX_HPP
|
||||
#endif // #ifndef SPROUT_CINTTYPES_STRTOIMAX_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 <cstddef>
|
||||
#include <cstdint>
|
||||
|
@ -27,4 +27,4 @@ namespace sprout {
|
|||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_CSTDLIB_STRTTOUMAX_HPP
|
||||
#endif // #ifndef SPROUT_CINTTYPES_STRTOUMAX_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
|
||||
|
||||
|
|
|
@ -16,7 +16,5 @@
|
|||
#include <sprout/cstdlib/strtof.hpp>
|
||||
#include <sprout/cstdlib/strtod.hpp>
|
||||
#include <sprout/cstdlib/strtold.hpp>
|
||||
#include <sprout/cstdlib/strtoimax.hpp>
|
||||
#include <sprout/cstdlib/strtoumax.hpp>
|
||||
|
||||
#endif // #ifndef SPROUT_CSTDLIB_CONVERSION_HPP
|
||||
|
|
|
@ -5,42 +5,34 @@
|
|||
#include <cstdlib>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/utility/enabler_if.hpp>
|
||||
|
||||
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<typename T>
|
||||
struct div_t_traits {};
|
||||
template<>
|
||||
struct div_t_traits<int> {
|
||||
public:
|
||||
SPROUT_DIV_T_TRAITS_IMPL(std::div_t);
|
||||
};
|
||||
template<>
|
||||
struct div_t_traits<long> {
|
||||
public:
|
||||
SPROUT_DIV_T_TRAITS_IMPL(std::ldiv_t);
|
||||
};
|
||||
template<>
|
||||
struct div_t_traits<long long> {
|
||||
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<INT_T> { \
|
||||
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<T>::offsetof_quot == 0
|
||||
>::type*& = sprout::detail::enabler
|
||||
>::type = sprout::enabler
|
||||
>
|
||||
SPROUT_CONSTEXPR typename sprout::detail::div_t_traits<T>::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<T>::offsetof_rem == 0
|
||||
>::type*& = sprout::detail::enabler
|
||||
>::type = sprout::enabler
|
||||
>
|
||||
SPROUT_CONSTEXPR typename sprout::detail::div_t_traits<T>::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);
|
||||
}
|
||||
|
||||
|
|
67
sprout/math/abs.hpp
Normal file
67
sprout/math/abs.hpp
Normal file
|
@ -0,0 +1,67 @@
|
|||
#ifndef SPROUT_MATH_ABS_HPP
|
||||
#define SPROUT_MATH_ABS_HPP
|
||||
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/utility/enabler_if.hpp>
|
||||
#if SPROUT_USE_BUILTIN_CMATH_FUNCTION
|
||||
# include <cmath>
|
||||
#endif
|
||||
|
||||
namespace sprout {
|
||||
namespace math {
|
||||
namespace detail {
|
||||
template<
|
||||
typename FloatType,
|
||||
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR FloatType
|
||||
abs(FloatType x) {
|
||||
return x < 0 ? -x : x;
|
||||
}
|
||||
|
||||
template<
|
||||
typename FloatType,
|
||||
typename sprout::enabler_if<std::is_floating_point<FloatType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR FloatType
|
||||
fabs(FloatType x) {
|
||||
return x < 0 ? -x : x;
|
||||
}
|
||||
|
||||
template<
|
||||
typename IntType,
|
||||
typename sprout::enabler_if<std::is_integral<IntType>::value>::type = sprout::enabler
|
||||
>
|
||||
inline SPROUT_CONSTEXPR double
|
||||
fabs(IntType x) {
|
||||
return sprout::math::detail::fabs(static_cast<double>(x));
|
||||
}
|
||||
} // namespace detail
|
||||
|
||||
template<
|
||||
typename FloatType,
|
||||
typename sprout::enabler_if<std::is_floating_point<FloatType>::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
|
|
@ -2,6 +2,7 @@
|
|||
#define SPROUT_MATH_FUNCTIONS_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/math/operations.hpp>
|
||||
#include <sprout/math/exponential.hpp>
|
||||
#include <sprout/math/power.hpp>
|
||||
#include <sprout/math/trigonometric.hpp>
|
||||
|
|
6
sprout/math/operations.hpp
Normal file
6
sprout/math/operations.hpp
Normal file
|
@ -0,0 +1,6 @@
|
|||
#ifndef SPROUT_MATH_OPERATIONS_HPP
|
||||
#define SPROUT_MATH_OPERATIONS_HPP
|
||||
|
||||
#include <sprout/math/abs.hpp>
|
||||
|
||||
#endif // #ifndef SPROUT_MATH_OPERATIONS_HPP
|
Loading…
Reference in a new issue