1 // Boost common_factor_ct.hpp header file ----------------------------------//
3 // (C) Copyright Daryle Walker and Stephen Cleary 2001-2002.
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 // See http://www.boost.org for updates, documentation, and revision history.
10 #ifndef BOOST_MATH_COMMON_FACTOR_CT_HPP
11 #define BOOST_MATH_COMMON_FACTOR_CT_HPP
13 #include <boost/math_fwd.hpp> // self include
15 #include <boost/config.hpp> // for BOOST_STATIC_CONSTANT, etc.
24 // Implementation details --------------------------------------------------//
28 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
29 // Build GCD with Euclid's recursive algorithm
30 template < unsigned long Value1
, unsigned long Value2
>
31 struct static_gcd_helper_t
34 BOOST_STATIC_CONSTANT( unsigned long, new_value1
= Value2
);
35 BOOST_STATIC_CONSTANT( unsigned long, new_value2
= Value1
% Value2
);
38 #define BOOST_DETAIL_GCD_HELPER_VAL(Value) static_cast<unsigned long>(Value)
40 typedef static_gcd_helper_t self_type
;
41 #define BOOST_DETAIL_GCD_HELPER_VAL(Value) (self_type:: Value )
44 typedef static_gcd_helper_t
< BOOST_DETAIL_GCD_HELPER_VAL(new_value1
),
45 BOOST_DETAIL_GCD_HELPER_VAL(new_value2
) > next_step_type
;
47 #undef BOOST_DETAIL_GCD_HELPER_VAL
50 BOOST_STATIC_CONSTANT( unsigned long, value
= next_step_type::value
);
54 template < unsigned long Value1
>
55 struct static_gcd_helper_t
< Value1
, 0UL >
57 BOOST_STATIC_CONSTANT( unsigned long, value
= Value1
);
60 // Use inner class template workaround from Peter Dimov
61 template < unsigned long Value1
>
62 struct static_gcd_helper2_t
64 template < unsigned long Value2
>
67 BOOST_STATIC_CONSTANT( unsigned long, value
68 = static_gcd_helper2_t
<Value2
>::BOOST_NESTED_TEMPLATE
69 helper
<Value1
% Value2
>::value
);
75 BOOST_STATIC_CONSTANT( unsigned long, value
= Value1
);
81 struct static_gcd_helper2_t
< 0UL >
83 template < unsigned long Value2
>
86 BOOST_STATIC_CONSTANT( unsigned long, value
= Value2
);
90 // Build the GCD from the above template(s)
91 template < unsigned long Value1
, unsigned long Value2
>
92 struct static_gcd_helper_t
94 BOOST_STATIC_CONSTANT( unsigned long, value
95 = static_gcd_helper2_t
<Value1
>::BOOST_NESTED_TEMPLATE
96 helper
<Value2
>::value
);
100 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
101 // Build the LCM from the GCD
102 template < unsigned long Value1
, unsigned long Value2
>
103 struct static_lcm_helper_t
105 typedef static_gcd_helper_t
<Value1
, Value2
> gcd_type
;
107 BOOST_STATIC_CONSTANT( unsigned long, value
= Value1
/ gcd_type::value
111 // Special case for zero-GCD values
113 struct static_lcm_helper_t
< 0UL, 0UL >
115 BOOST_STATIC_CONSTANT( unsigned long, value
= 0UL );
118 // Adapt GCD's inner class template workaround for LCM
119 template < unsigned long Value1
>
120 struct static_lcm_helper2_t
122 template < unsigned long Value2
>
125 typedef static_gcd_helper_t
<Value1
, Value2
> gcd_type
;
127 BOOST_STATIC_CONSTANT( unsigned long, value
= Value1
128 / gcd_type::value
* Value2
);
134 BOOST_STATIC_CONSTANT( unsigned long, value
= 0UL );
140 struct static_lcm_helper2_t
< 0UL >
142 template < unsigned long Value2
>
145 BOOST_STATIC_CONSTANT( unsigned long, value
= 0UL );
149 // Build the LCM from the above template(s)
150 template < unsigned long Value1
, unsigned long Value2
>
151 struct static_lcm_helper_t
153 BOOST_STATIC_CONSTANT( unsigned long, value
154 = static_lcm_helper2_t
<Value1
>::BOOST_NESTED_TEMPLATE
155 helper
<Value2
>::value
);
159 } // namespace detail
162 // Compile-time greatest common divisor evaluator class declaration --------//
164 template < unsigned long Value1
, unsigned long Value2
>
167 BOOST_STATIC_CONSTANT( unsigned long, value
168 = (detail::static_gcd_helper_t
<Value1
, Value2
>::value
) );
170 }; // boost::math::static_gcd
173 // Compile-time least common multiple evaluator class declaration ----------//
175 template < unsigned long Value1
, unsigned long Value2
>
178 BOOST_STATIC_CONSTANT( unsigned long, value
179 = (detail::static_lcm_helper_t
<Value1
, Value2
>::value
) );
181 }; // boost::math::static_lcm
188 #endif // BOOST_MATH_COMMON_FACTOR_CT_HPP