1 | // Copyright (C) 2000 Stephen Cleary
|
---|
2 | //
|
---|
3 | // Distributed under the Boost Software License, Version 1.0. (See
|
---|
4 | // accompanying file LICENSE_1_0.txt or copy at
|
---|
5 | // http://www.boost.org/LICENSE_1_0.txt)
|
---|
6 | //
|
---|
7 | // See http://www.boost.org for updates, documentation, and revision history.
|
---|
8 |
|
---|
9 | #ifndef BOOST_POOL_CT_GCD_LCM_HPP
|
---|
10 | #define BOOST_POOL_CT_GCD_LCM_HPP
|
---|
11 |
|
---|
12 | #include <boost/static_assert.hpp>
|
---|
13 | #include <boost/type_traits/ice.hpp>
|
---|
14 |
|
---|
15 | namespace boost {
|
---|
16 |
|
---|
17 | namespace details {
|
---|
18 | namespace pool {
|
---|
19 |
|
---|
20 | // Compile-time calculation of greatest common divisor and least common multiple
|
---|
21 |
|
---|
22 | //
|
---|
23 | // ct_gcd is a compile-time algorithm that calculates the greatest common
|
---|
24 | // divisor of two unsigned integers, using Euclid's algorithm.
|
---|
25 | //
|
---|
26 | // assumes: A != 0 && B != 0
|
---|
27 | //
|
---|
28 |
|
---|
29 | #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
|
---|
30 |
|
---|
31 | namespace details {
|
---|
32 | template <unsigned A, unsigned B, bool Bis0>
|
---|
33 | struct ct_gcd_helper;
|
---|
34 | template <unsigned A, unsigned B>
|
---|
35 | struct ct_gcd_helper<A, B, false>
|
---|
36 | {
|
---|
37 | BOOST_STATIC_CONSTANT(unsigned, A_mod_B_ = A % B);
|
---|
38 | BOOST_STATIC_CONSTANT(unsigned, value =
|
---|
39 | (::boost::details::pool::details::ct_gcd_helper<
|
---|
40 | B, static_cast<unsigned>(A_mod_B_),
|
---|
41 | ::boost::type_traits::ice_eq<A_mod_B_, 0>::value
|
---|
42 | >::value) );
|
---|
43 | };
|
---|
44 | template <unsigned A, unsigned B>
|
---|
45 | struct ct_gcd_helper<A, B, true>
|
---|
46 | {
|
---|
47 | BOOST_STATIC_CONSTANT(unsigned, value = A);
|
---|
48 | };
|
---|
49 | } // namespace details
|
---|
50 |
|
---|
51 | template <unsigned A, unsigned B>
|
---|
52 | struct ct_gcd
|
---|
53 | {
|
---|
54 | BOOST_STATIC_ASSERT(A != 0 && B != 0);
|
---|
55 | BOOST_STATIC_CONSTANT(unsigned, value =
|
---|
56 | (::boost::details::pool::details::ct_gcd_helper<A, B, false>::value) );
|
---|
57 | };
|
---|
58 |
|
---|
59 | #else
|
---|
60 |
|
---|
61 | // Thanks to Peter Dimov for providing this workaround!
|
---|
62 | namespace details {
|
---|
63 | template<unsigned A> struct ct_gcd2
|
---|
64 | {
|
---|
65 | template<unsigned B>
|
---|
66 | struct helper
|
---|
67 | {
|
---|
68 | BOOST_STATIC_CONSTANT(unsigned, value = ct_gcd2<B>::helper<A % B>::value);
|
---|
69 | };
|
---|
70 | template<>
|
---|
71 | struct helper<0>
|
---|
72 | {
|
---|
73 | BOOST_STATIC_CONSTANT(unsigned, value = A);
|
---|
74 | };
|
---|
75 | };
|
---|
76 | } // namespace details
|
---|
77 |
|
---|
78 | template<unsigned A, unsigned B> struct ct_gcd
|
---|
79 | {
|
---|
80 | BOOST_STATIC_ASSERT(A != 0 && B != 0);
|
---|
81 | enum { value = details::ct_gcd2<A>::helper<B>::value };
|
---|
82 | };
|
---|
83 |
|
---|
84 | #endif
|
---|
85 |
|
---|
86 | //
|
---|
87 | // ct_lcm is a compile-time algorithm that calculates the least common
|
---|
88 | // multiple of two unsigned integers.
|
---|
89 | //
|
---|
90 | // assumes: A != 0 && B != 0
|
---|
91 | //
|
---|
92 | template <unsigned A, unsigned B>
|
---|
93 | struct ct_lcm
|
---|
94 | {
|
---|
95 | BOOST_STATIC_CONSTANT(unsigned, value =
|
---|
96 | (A / ::boost::details::pool::ct_gcd<A, B>::value * B) );
|
---|
97 | };
|
---|
98 |
|
---|
99 | } // namespace pool
|
---|
100 | } // namespace details
|
---|
101 |
|
---|
102 | } // namespace boost
|
---|
103 |
|
---|
104 | #endif
|
---|