From 18b479e3acce5f921c13e88332762cecc2a1c236 Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Sun, 2 Dec 2012 17:06:37 +0900 Subject: [PATCH] add math fmod fix math cos --- sprout/compost/effects/vocal_cancelled.hpp | 3 ++ sprout/compost/load/source.hpp | 16 +++++--- sprout/compost/waves/white_noise.hpp | 46 ++++++--------------- sprout/config/suffix.hpp | 6 +++ sprout/darkroom/materials/plaid.hpp | 14 +++---- sprout/math/ceil.hpp | 2 +- sprout/math/cos.hpp | 26 ++++++++---- sprout/math/detail/config.hpp | 6 +++ sprout/math/floor.hpp | 2 +- sprout/math/fmod.hpp | 47 ++++++++++++++++++++++ sprout/math/operations.hpp | 1 + sprout/math/round.hpp | 2 +- sprout/math/trunc.hpp | 2 +- sprout/numeric/dft/fixed/sawtooth.hpp | 2 +- sprout/range/adaptor/clamped.hpp | 37 +++++------------ 15 files changed, 123 insertions(+), 89 deletions(-) create mode 100644 sprout/math/fmod.hpp diff --git a/sprout/compost/effects/vocal_cancelled.hpp b/sprout/compost/effects/vocal_cancelled.hpp index 6e778ba0..7f776969 100644 --- a/sprout/compost/effects/vocal_cancelled.hpp +++ b/sprout/compost/effects/vocal_cancelled.hpp @@ -26,6 +26,8 @@ namespace sprout { // // operator| // + // ??? +#if !defined(__clang__) template inline SPROUT_CONSTEXPR auto operator|(Range&& lhs, sprout::compost::effects::vocal_cancelled_forwarder const& rhs) @@ -44,6 +46,7 @@ namespace sprout { ) ; } +#endif } // namespace effects using sprout::compost::effects::vocal_cancelled; diff --git a/sprout/compost/load/source.hpp b/sprout/compost/load/source.hpp index 5f38b06c..335e3e55 100644 --- a/sprout/compost/load/source.hpp +++ b/sprout/compost/load/source.hpp @@ -7,10 +7,6 @@ # error should define COMPOST_DEF_LOAD_SOURCE_FILE. #endif -#ifndef COMPOST_DEF_LOAD_SOURCE_DATA_INDEX -# define COMPOST_DEF_LOAD_SOURCE_DATA_INDEX -1 -#endif - #define COMPOST_LOAD_IDENTIFIER_DETAIL_CAT(id, suffix) COMPOST_LOAD_IDENTIFIER_DETAIL_CAT_I(id, suffix) #define COMPOST_LOAD_IDENTIFIER_DETAIL_CAT_I(id, suffix) COMPOST_LOAD_IDENTIFIER_DETAIL_CAT_II(id ## suffix) #define COMPOST_LOAD_IDENTIFIER_DETAIL_CAT_II(res) res @@ -27,6 +23,12 @@ # define COMPOST_LOAD_INFO_IDENTIFIER COMPOST_LOAD_DETAIL_IDENTIFIER(info) #endif +#ifdef COMPOST_DEF_LOAD_SOURCE_INDEX +# define COMPOST_LOAD_SOURCE_INDEX COMPOST_DEF_LOAD_SOURCE_INDEX +#else +# define COMPOST_LOAD_SOURCE_INDEX 0 +#endif + SPROUT_STATIC_CONSTEXPR sprout::compost::sources::version_type COMPOST_LOAD_DETAIL_IDENTIFIER(version) = # define COMPOST_LOADING_SOURCE_VERSION # include COMPOST_DEF_LOAD_SOURCE_FILE @@ -46,14 +48,13 @@ SPROUT_STATIC_CONSTEXPR sprout::compost::sources::sound_type< > COMPOST_LOAD_IDENTIFIER( COMPOST_LOAD_INFO_IDENTIFIER, # define COMPOST_LOADING_SOURCE_DATA -# define COMPOST_LOADING_SOURCE_DATA_INDEX COMPOST_DEF_LOAD_SOURCE_DATA_INDEX # include COMPOST_DEF_LOAD_SOURCE_FILE -# undef COMPOST_LOADING_SOURCE_DATA_INDEX # undef COMPOST_LOADING_SOURCE_DATA ); #undef COMPOST_LOAD_IDENTIFIER #undef COMPOST_LOAD_INFO_IDENTIFIER +#undef COMPOST_LOAD_SOURCE_INDEX #undef COMPOST_LOAD_DETAIL_IDENTIFIER @@ -66,3 +67,6 @@ SPROUT_STATIC_CONSTEXPR sprout::compost::sources::sound_type< #ifdef COMPOST_DEF_LOAD_INFO_IDENTIFIER # undef COMPOST_DEF_LOAD_INFO_IDENTIFIER #endif +#ifdef COMPOST_DEF_LOAD_SOURCE_INDEX +# undef COMPOST_DEF_LOAD_SOURCE_INDEX +#endif diff --git a/sprout/compost/waves/white_noise.hpp b/sprout/compost/waves/white_noise.hpp index 6a98f2b4..1a25d069 100644 --- a/sprout/compost/waves/white_noise.hpp +++ b/sprout/compost/waves/white_noise.hpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -23,24 +24,23 @@ namespace sprout { // template class white_noise_range - : public sprout::range::range_container< + : public sprout::adaptors::detail::adapted_range_default< + Range, sprout::generator_iterator< sprout::random::random_result > > > - , public sprout::detail::container_nosy_static_size - , public sprout::detail::container_nosy_fixed_size { public: - typedef Range range_type; - typedef sprout::range::range_container< + typedef sprout::adaptors::detail::adapted_range_default< + Range, sprout::generator_iterator< sprout::random::random_result > > > base_type; + typedef typename base_type::range_type range_type; typedef typename base_type::iterator iterator; typedef typename base_type::value_type value_type; - typedef typename base_type::difference_type difference_type; typedef typename iterator::generator_type generator_type; typedef typename generator_type::engine_type engine_type; typedef typename generator_type::distribution_type distribution_type; @@ -57,10 +57,10 @@ namespace sprout { iterator() ) {} - SPROUT_CONSTEXPR engine_type const& engine() const { + SPROUT_CONSTEXPR engine_type engine() const { return base_type::begin().generator().engine(); } - SPROUT_CONSTEXPR value_type const& sigma() const { + SPROUT_CONSTEXPR value_type sigma() const { return base_type::begin().generator().distribution().sigma(); } }; @@ -81,7 +81,6 @@ namespace sprout { > base_type; typedef typename base_type::iterator iterator; typedef typename base_type::value_type value_type; - typedef typename base_type::difference_type difference_type; typedef typename iterator::generator_type generator_type; typedef typename generator_type::engine_type engine_type; typedef typename generator_type::distribution_type distribution_type; @@ -97,10 +96,10 @@ namespace sprout { iterator() ) {} - SPROUT_CONSTEXPR engine_type const& engine() const { + SPROUT_CONSTEXPR engine_type engine() const { return base_type::begin().generator().engine(); } - SPROUT_CONSTEXPR value_type const& sigma() const { + SPROUT_CONSTEXPR value_type sigma() const { return base_type::begin().generator().distribution().sigma(); } }; @@ -154,28 +153,9 @@ namespace sprout { // container_construct_traits // template - struct container_construct_traits > { - public: - typedef typename sprout::container_construct_traits::copied_type copied_type; - public: - template - static SPROUT_CONSTEXPR copied_type deep_copy(Cont&& cont) { - return sprout::range::fixed::copy(sprout::forward(cont), sprout::pit()); - } - template - static SPROUT_CONSTEXPR copied_type make(Args&&... args) { - return sprout::make(sprout::forward(args)...); - } - template - static SPROUT_CONSTEXPR copied_type remake( - Cont&& cont, - typename sprout::container_traits >::difference_type size, - Args&&... args - ) - { - return sprout::remake(sprout::forward(cont), size, sprout::forward(args)...); - } - }; + struct container_construct_traits > + : public sprout::container_construct_traits::base_type> + {}; } // namespace sprout #endif // #ifndef SPROUT_COMPOST_WAVES_WHITE_NOISE_HPP diff --git a/sprout/config/suffix.hpp b/sprout/config/suffix.hpp index 460be48d..f70c6903 100644 --- a/sprout/config/suffix.hpp +++ b/sprout/config/suffix.hpp @@ -71,6 +71,12 @@ # define SPROUT_USE_BUILTIN_BIT_OPERATION 0 #endif // #ifndef SPROUT_CONFIG_DISABLE_BUILTIN_BIT_OPERATION +#ifndef SPROUT_CONFIG_DISABLE_THROW_INT_CONVERSION_OVERFLOW +# define SPROUT_NOTHROW_INT_CONVERSION_OVERFLOW 1 +#else // #ifndef SPROUT_CONFIG_DISABLE_THROW_INT_CONVERSION_OVERFLOW +# define SPROUT_NOTHROW_INT_CONVERSION_OVERFLOW 0 +#endif // #ifndef SPROUT_CONFIG_DISABLE_THROW_INT_CONVERSION_OVERFLOW + #ifndef SPROUT_CONFIG_DISABLE_SUPPORT_TEMPORARY_CONTAINER_ITERATION # define SPROUT_USE_INDEX_ITERATOR_IMPLEMENTATION 1 #else // #ifndef SPROUT_CONFIG_DISABLE_SUPPORT_TEMPORARY_CONTAINER_ITERATION diff --git a/sprout/darkroom/materials/plaid.hpp b/sprout/darkroom/materials/plaid.hpp index 754cbfdd..1f340986 100644 --- a/sprout/darkroom/materials/plaid.hpp +++ b/sprout/darkroom/materials/plaid.hpp @@ -4,6 +4,7 @@ #include #include #include +#include namespace sprout { namespace darkroom { @@ -16,11 +17,6 @@ namespace sprout { public: typedef Element result_type; typedef Scale unit_type; - private: - static SPROUT_CONSTEXPR unit_type - fmod(unit_type const& n, unit_type const& d) { - return n - std::intmax_t(n / d) * d; - } private: result_type elem1_; result_type elem2_; @@ -42,14 +38,14 @@ namespace sprout { operator()(Unit const& u, Unit const& v) const { return calc_1( (u < 0 - ? scale_ + fmod(u, scale_) - : fmod(u, scale_) + ? scale_ + sprout::fmod(u, scale_) + : sprout::fmod(u, scale_) ) - scale_ / 2 , (v < 0 - ? scale_ + fmod(v, scale_) - : fmod(v, scale_) + ? scale_ + sprout::fmod(v, scale_) + : sprout::fmod(v, scale_) ) - scale_ / 2 ); diff --git a/sprout/math/ceil.hpp b/sprout/math/ceil.hpp index 523f889f..b76808ad 100644 --- a/sprout/math/ceil.hpp +++ b/sprout/math/ceil.hpp @@ -29,7 +29,7 @@ namespace sprout { ceil(FloatType x) { return sprout::math::isinf(x) ? x : std::numeric_limits::max() < x || std::numeric_limits::max() < -x - ? throw std::domain_error("ceil: Sorry, not implemented.") + ? SPROUT_MATH_DETAIL_INT_CONVERSION_OVERFLOW(std::domain_error("ceil: Sorry, not implemented."), x) : x < 0 ? -static_cast(static_cast(-x)) : sprout::math::detail::ceil_impl(x, static_cast(static_cast(x))) ; diff --git a/sprout/math/cos.hpp b/sprout/math/cos.hpp index 93579abe..68a2bd63 100644 --- a/sprout/math/cos.hpp +++ b/sprout/math/cos.hpp @@ -7,6 +7,8 @@ #include #include #include +#include +#include #include namespace sprout { @@ -14,9 +16,9 @@ namespace sprout { namespace detail { template inline SPROUT_CONSTEXPR T - cos_impl(T x, T tmp, std::size_t n, T x2n) { + cos_impl_1(T x, T tmp, std::size_t n, T x2n) { return 2 * n > sprout::math::factorial_limit() ? tmp - : sprout::math::detail::cos_impl( + : sprout::math::detail::cos_impl_1( x, tmp + (n % 2 ? -1 : 1) * x2n / sprout::math::factorial(2 * n), n + 1, @@ -25,6 +27,19 @@ namespace sprout { ; } + template + inline SPROUT_CONSTEXPR FloatType + cos_impl(FloatType x) { + typedef double type; + return static_cast(sprout::math::detail::cos_impl_1( + static_cast(x), + type(1), + 1, + static_cast(x) * static_cast(x) + )) + ; + } + template< typename FloatType, typename sprout::enabler_if::value>::type = sprout::enabler @@ -35,12 +50,7 @@ namespace sprout { return x == std::numeric_limits::infinity() || x == -std::numeric_limits::infinity() ? std::numeric_limits::quiet_NaN() - : static_cast(sprout::math::detail::cos_impl( - static_cast(x), - type(1), - 1, - static_cast(x) * static_cast(x) - )) + : sprout::math::detail::cos_impl(sprout::math::fmod(x, 2 * sprout::math::pi())) ; } diff --git a/sprout/math/detail/config.hpp b/sprout/math/detail/config.hpp index 7ad9d474..1f2e3d29 100644 --- a/sprout/math/detail/config.hpp +++ b/sprout/math/detail/config.hpp @@ -12,4 +12,10 @@ # define NS_SPROUT_MATH_DETAIL sprout::math::detail #endif // #ifndef SPROUT_USE_BUILTIN_CMATH_FUNCTION +#if SPROUT_NOTHROW_INT_CONVERSION_OVERFLOW +# define SPROUT_MATH_DETAIL_INT_CONVERSION_OVERFLOW(e, x) (x) +#else +# define SPROUT_MATH_DETAIL_INT_CONVERSION_OVERFLOW(e, x) throw (e) +#endif + #endif // #ifndef SPROUT_MATH_DETAIL_CONFIG_HPP diff --git a/sprout/math/floor.hpp b/sprout/math/floor.hpp index 178c46bf..454867af 100644 --- a/sprout/math/floor.hpp +++ b/sprout/math/floor.hpp @@ -29,7 +29,7 @@ namespace sprout { floor(FloatType x) { return sprout::math::isinf(x) ? x : std::numeric_limits::max() < x || std::numeric_limits::max() < -x - ? throw std::domain_error("floor: Sorry, not implemented.") + ? SPROUT_MATH_DETAIL_INT_CONVERSION_OVERFLOW(std::domain_error("floor: Sorry, not implemented."), x) : x < 0 ? sprout::math::detail::floor_impl(x, -static_cast(static_cast(-x))) : static_cast(static_cast(x)) ; diff --git a/sprout/math/fmod.hpp b/sprout/math/fmod.hpp new file mode 100644 index 00000000..2fe0c4b3 --- /dev/null +++ b/sprout/math/fmod.hpp @@ -0,0 +1,47 @@ +#ifndef SPROUT_MATH_FMOD_HPP +#define SPROUT_MATH_FMOD_HPP + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace math { + namespace detail { + template< + typename FloatType, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR FloatType + fmod(FloatType x, FloatType y) { + return y == 0 ? throw std::domain_error("fmod: divide by zero.") + : x - sprout::math::trunc(x / y) * y + ; + } + + template< + typename ArithmeticType1, + typename ArithmeticType2, + typename sprout::enabler_if< + std::is_arithmetic::value && std::is_arithmetic::value + >::type = sprout::enabler + > + inline SPROUT_CONSTEXPR typename sprout::float_promote::type + fmod(ArithmeticType1 x, ArithmeticType2 y) { + typedef typename sprout::float_promote::type type; + return sprout::math::detail::fmod(static_cast(x), static_cast(y)); + } + } // namespace detail + + using sprout::math::detail::fmod; + } // namespace math + + using sprout::math::fmod; +} // namespace sprout + +#endif // #ifndef SPROUT_MATH_FMOD_HPP diff --git a/sprout/math/operations.hpp b/sprout/math/operations.hpp index 88c3616e..dd2c4e2e 100644 --- a/sprout/math/operations.hpp +++ b/sprout/math/operations.hpp @@ -8,5 +8,6 @@ #include #include #include +#include #endif // #ifndef SPROUT_MATH_OPERATIONS_HPP diff --git a/sprout/math/round.hpp b/sprout/math/round.hpp index 13748ce1..71d1402e 100644 --- a/sprout/math/round.hpp +++ b/sprout/math/round.hpp @@ -35,7 +35,7 @@ namespace sprout { round(FloatType x) { return sprout::math::isinf(x) ? x : std::numeric_limits::max() < x || std::numeric_limits::max() < -x - ? throw std::domain_error("round: Sorry, not implemented.") + ? SPROUT_MATH_DETAIL_INT_CONVERSION_OVERFLOW(std::domain_error("round: Sorry, not implemented."), x) : x < 0 ? sprout::math::detail::round_impl_nagative(x, -static_cast(static_cast(-x))) : sprout::math::detail::round_impl_positive(x, static_cast(static_cast(x))) ; diff --git a/sprout/math/trunc.hpp b/sprout/math/trunc.hpp index 898dfa17..284fd7c2 100644 --- a/sprout/math/trunc.hpp +++ b/sprout/math/trunc.hpp @@ -21,7 +21,7 @@ namespace sprout { trunc(FloatType x) { return sprout::math::isinf(x) ? x : std::numeric_limits::max() < x || std::numeric_limits::max() < -x - ? throw std::domain_error("trunc: Sorry, not implemented.") + ? SPROUT_MATH_DETAIL_INT_CONVERSION_OVERFLOW(std::domain_error("trunc: Sorry, not implemented."), x) : x < 0 ? -static_cast(static_cast(-x)) : static_cast(static_cast(x)) ; diff --git a/sprout/numeric/dft/fixed/sawtooth.hpp b/sprout/numeric/dft/fixed/sawtooth.hpp index e3d0196d..ebbfe8de 100644 --- a/sprout/numeric/dft/fixed/sawtooth.hpp +++ b/sprout/numeric/dft/fixed/sawtooth.hpp @@ -16,7 +16,7 @@ namespace sprout { inline SPROUT_CONSTEXPR T sawtooth_value(T const& t) { using sprout::floor; - return T(2) * (t - floor(t + T(0.5))); + return 2 * (t - floor(t + T(0.5))); } template diff --git a/sprout/range/adaptor/clamped.hpp b/sprout/range/adaptor/clamped.hpp index 31699e18..0f6e2027 100644 --- a/sprout/range/adaptor/clamped.hpp +++ b/sprout/range/adaptor/clamped.hpp @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include #include #include @@ -24,17 +24,17 @@ namespace sprout { typename Compare = NS_SSCRISK_CEL_OR_SPROUT::less::value_type> > class clamped_range - : public sprout::range::range_container< + : public sprout::adaptors::detail::adapted_range_default< + Range, sprout::clamp_iterator::iterator, Compare> > - , public sprout::detail::container_nosy_static_size - , public sprout::detail::container_nosy_fixed_size { public: - typedef Range range_type; - typedef sprout::range::range_container< + typedef sprout::adaptors::detail::adapted_range_default< + Range, sprout::clamp_iterator::iterator, Compare> > base_type; + typedef typename base_type::range_type range_type; typedef typename base_type::iterator iterator; typedef typename base_type::value_type value_type; typedef typename iterator::compare_type compare_type; @@ -137,28 +137,9 @@ namespace sprout { // container_construct_traits // template - struct container_construct_traits > { - public: - typedef typename sprout::container_construct_traits::copied_type copied_type; - public: - template - static SPROUT_CONSTEXPR copied_type deep_copy(Cont&& cont) { - return sprout::range::fixed::copy(sprout::forward(cont), sprout::pit()); - } - template - static SPROUT_CONSTEXPR copied_type make(Args&&... args) { - return sprout::make(sprout::forward(args)...); - } - template - static SPROUT_CONSTEXPR copied_type remake( - Cont&& cont, - typename sprout::container_traits >::difference_type size, - Args&&... args - ) - { - return sprout::remake(sprout::forward(cont), size, sprout::forward(args)...); - } - }; + struct container_construct_traits > + : public sprout::container_construct_traits::base_type> + {}; } // namespace sprout #endif // #ifndef SPROUT_RANGE_ADAPTOR_CLAMPED_HPP