1 ////////////////////////////////////////////////////////////////////
3 // Copyright Vicente J. Botet Escriba 2010
5 // Distributed under the Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 // See http://www.boost.org/libs/mpl for documentation.
11 ////////////////////////////////////////////////////////////////////
12 #ifndef BOOST_MPL_GCD_HPP_INCLUDED
13 #define BOOST_MPL_GCD_HPP_INCLUDED
15 #include <boost/mpl/integral_c.hpp>
16 #include <boost/ratio/detail/mpl/abs.hpp>
17 #include <boost/mpl/aux_/largest_int.hpp>
18 #include <boost/mpl/aux_/na_spec.hpp>
19 #include <boost/mpl/aux_/lambda_support.hpp>
20 #include <boost/mpl/aux_/config/integral.hpp>
21 #include <boost/mpl/aux_/config/static_constant.hpp>
22 #include <boost/mpl/aux_/config/dependent_nttp.hpp>
23 #include <boost/cstdint.hpp>
25 #if !defined(BOOST_MPL_CFG_NO_NESTED_VALUE_ARITHMETIC_2) \
26 && !defined(BOOST_MPL_PREPROCESSING_MODE) \
27 && !defined(__CUDACC__) \
28 && ( defined(BOOST_MSVC) \
29 || BOOST_WORKAROUND(__EDG_VERSION__, <= 238) \
32 # define BOOST_MPL_CFG_NO_NESTED_VALUE_ARITHMETIC_2
36 namespace boost { namespace mpl {
38 template< typename Tag1, typename Tag2 > struct gcd_impl;
40 template< typename T > struct gcd_tag
42 typedef typename T::tag type;
46 typename BOOST_MPL_AUX_NA_PARAM(N1)
47 , typename BOOST_MPL_AUX_NA_PARAM(N2)
51 typename gcd_tag<N1>::type
52 , typename gcd_tag<N2>::type
53 >::template apply<N1, N2>::type
55 BOOST_MPL_AUX_LAMBDA_SUPPORT(2, gcd, (N1, N2))
58 BOOST_MPL_AUX_NA_SPEC(2, gcd)
66 : gcd<integral_c<T,n1>,integral_c<T,n2> >
72 // Workaround for error: the type of partial specialization template parameter constant "n2"
73 // depends on another template parameter
74 // Note: this solution could be wrong for n1 or n2 = [2**63 .. 2**64-1]
75 #if defined(BOOST_MPL_CFG_NO_DEPENDENT_NONTYPE_PARAMETER_IN_PARTIAL_SPEC)
77 template< typename T1, boost::intmax_t n1, bool n1_is_0
78 , typename T2, boost::intmax_t n2, bool n2_is_0 >
80 : gcd_aux<T2, n2, n2==0, T1, (n1 % n2), (n1 % n2)==0>
83 template <typename T1, boost::intmax_t n1, typename T2, boost::intmax_t n2>
84 struct gcd_aux<T1, n1, false, T2, n2, true> : integral_c<T1, n1>
87 template <typename T1, boost::intmax_t n1, typename T2, boost::intmax_t n2, bool C>
88 struct gcd_aux<T1, n1, true, T2, n2, C> : integral_c<T2, n2>
91 #else // defined(BOOST_MPL_CFG_NO_DEPENDENT_NONTYPE_PARAMETER_IN_PARTIAL_SPEC)
93 template< typename T1, T1 n1, bool n1_is_0, typename T2, T2 n2, bool n2_is_0 >
96 : gcd_aux<T2, n2, n2==0,
97 typename aux::largest_int<T1, T2>::type,
99 (n1 % n2), (n1 % n2)==0>
102 template <typename T1, T1 n1, typename T2, T2 n2>
103 struct gcd_aux<T1, n1, false, T2, n2, true> : integral_c<T1, n1>
106 template <typename T1, T1 n1, typename T2, T2 n2, bool C>
107 struct gcd_aux<T1, n1, true, T2, n2, C> : integral_c<T2, n2>
109 #endif // defined(BOOST_MPL_CFG_NO_DEPENDENT_NONTYPE_PARAMETER_IN_PARTIAL_SPEC)
113 struct gcd_impl<integral_c_tag, integral_c_tag>
115 template< typename N1, typename N2 > struct apply
116 : abs<aux::gcd_aux< typename N1::value_type, N1::value, N1::value==0,
117 typename N2::value_type, N2::value, N2::value==0 > >
124 #endif // BOOST_MPL_GCD_HPP_INCLUDED