1
// Copyright (C) 2000 Stephen Cleary (shammah@voyager.net)
3
// This file can be redistributed and/or modified under the terms found
5
// This software and its documentation is provided "as is" without express or
6
// implied warranty, and with no claim as to its suitability for any purpose.
8
// See http://www.boost.org for updates, documentation, and revision history.
10
#ifndef BOOST_POOL_CT_GCD_LCM_HPP
11
#define BOOST_POOL_CT_GCD_LCM_HPP
13
#include <boost/static_assert.hpp>
14
#include <boost/type_traits/ice.hpp>
21
// Compile-time calculation of greatest common divisor and least common multiple
24
// ct_gcd is a compile-time algorithm that calculates the greatest common
25
// divisor of two unsigned integers, using Euclid's algorithm.
27
// assumes: A != 0 && B != 0
30
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
33
template <unsigned A, unsigned B, bool Bis0>
35
template <unsigned A, unsigned B>
36
struct ct_gcd_helper<A, B, false>
38
BOOST_STATIC_CONSTANT(unsigned, A_mod_B_ = A % B);
39
BOOST_STATIC_CONSTANT(unsigned, value =
40
(::boost::details::pool::details::ct_gcd_helper<
41
B, static_cast<unsigned>(A_mod_B_),
42
::boost::type_traits::ice_eq<A_mod_B_, 0>::value
45
template <unsigned A, unsigned B>
46
struct ct_gcd_helper<A, B, true>
48
BOOST_STATIC_CONSTANT(unsigned, value = A);
50
} // namespace details
52
template <unsigned A, unsigned B>
55
BOOST_STATIC_ASSERT(A != 0 && B != 0);
56
BOOST_STATIC_CONSTANT(unsigned, value =
57
(::boost::details::pool::details::ct_gcd_helper<A, B, false>::value) );
62
// Thanks to Peter Dimov for providing this workaround!
64
template<unsigned A> struct ct_gcd2
69
BOOST_STATIC_CONSTANT(unsigned, value = ct_gcd2<B>::helper<A % B>::value);
74
BOOST_STATIC_CONSTANT(unsigned, value = A);
77
} // namespace details
79
template<unsigned A, unsigned B> struct ct_gcd
81
BOOST_STATIC_ASSERT(A != 0 && B != 0);
82
enum { value = details::ct_gcd2<A>::helper<B>::value };
88
// ct_lcm is a compile-time algorithm that calculates the least common
89
// multiple of two unsigned integers.
91
// assumes: A != 0 && B != 0
93
template <unsigned A, unsigned B>
96
BOOST_STATIC_CONSTANT(unsigned, value =
97
(A / ::boost::details::pool::ct_gcd<A, B>::value * B) );
101
} // namespace details