| 1 | // Boost.Units - A C++ library for zero-overhead dimensional analysis and |
| 2 | // unit/quantity manipulation and conversion |
| 3 | // |
| 4 | // Copyright (C) 2003-2008 Matthias Christian Schabel |
| 5 | // Copyright (C) 2007-2008 Steven Watanabe |
| 6 | // |
| 7 | // Distributed under the Boost Software License, Version 1.0. (See |
| 8 | // accompanying file LICENSE_1_0.txt or copy at |
| 9 | // http://www.boost.org/LICENSE_1_0.txt) |
| 10 | |
| 11 | #ifndef BOOST_UNITS_DETAIL_STATIC_RATIONAL_POWER_HPP |
| 12 | #define BOOST_UNITS_DETAIL_STATIC_RATIONAL_POWER_HPP |
| 13 | |
| 14 | #include <boost/config/no_tr1/cmath.hpp> |
| 15 | |
| 16 | #include <boost/units/detail/one.hpp> |
| 17 | #include <boost/units/operators.hpp> |
| 18 | |
| 19 | namespace boost { |
| 20 | |
| 21 | namespace units { |
| 22 | |
| 23 | template<long N,long D> |
| 24 | class static_rational; |
| 25 | |
| 26 | namespace detail { |
| 27 | |
| 28 | namespace typeof_pow_adl_barrier { |
| 29 | |
| 30 | using std::pow; |
| 31 | |
| 32 | template<class Y> |
| 33 | struct typeof_pow |
| 34 | { |
| 35 | #if defined(BOOST_UNITS_HAS_BOOST_TYPEOF) |
| 36 | BOOST_TYPEOF_NESTED_TYPEDEF_TPL(nested, pow(typeof_::make<Y>(), 0.0)) |
| 37 | typedef typename nested::type type; |
| 38 | #elif defined(BOOST_UNITS_HAS_MWERKS_TYPEOF) |
| 39 | typedef __typeof__(pow(typeof_::make<Y>(), 0.0)) type; |
| 40 | #elif defined(BOOST_UNITS_HAS_GNU_TYPEOF) |
| 41 | typedef typeof(pow(typeof_::make<Y>(), 0.0)) type; |
| 42 | #else |
| 43 | typedef Y type; |
| 44 | #endif |
| 45 | }; |
| 46 | |
| 47 | } |
| 48 | |
| 49 | template<class R, class Y> |
| 50 | struct static_rational_power_impl |
| 51 | { |
| 52 | typedef typename typeof_pow_adl_barrier::typeof_pow<Y>::type type; |
| 53 | static BOOST_CONSTEXPR type call(const Y& y) |
| 54 | { |
| 55 | using std::pow; |
| 56 | return(pow(y, static_cast<double>(R::Numerator) / static_cast<double>(R::Denominator))); |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | template<class R> |
| 61 | struct static_rational_power_impl<R, one> |
| 62 | { |
| 63 | typedef one type; |
| 64 | static BOOST_CONSTEXPR one call(const one&) |
| 65 | { |
| 66 | return(one()); |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | template<long N> |
| 71 | struct static_rational_power_impl<static_rational<N, 1>, one> |
| 72 | { |
| 73 | typedef one type; |
| 74 | static BOOST_CONSTEXPR one call(const one&) |
| 75 | { |
| 76 | return(one()); |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | template<long N, bool = (N % 2 == 0)> |
| 81 | struct static_int_power_impl; |
| 82 | |
| 83 | template<long N> |
| 84 | struct static_int_power_impl<N, true> |
| 85 | { |
| 86 | template<class Y, class R> |
| 87 | struct apply |
| 88 | { |
| 89 | typedef typename multiply_typeof_helper<Y, Y>::type square_type; |
| 90 | typedef typename static_int_power_impl<(N >> 1)>::template apply<square_type, R> next; |
| 91 | typedef typename next::type type; |
| 92 | static BOOST_CONSTEXPR type call(const Y& y, const R& r) |
| 93 | { |
| 94 | return(next::call(static_cast<square_type>(y * y), r)); |
| 95 | } |
| 96 | }; |
| 97 | }; |
| 98 | |
| 99 | template<long N> |
| 100 | struct static_int_power_impl<N, false> |
| 101 | { |
| 102 | template<class Y, class R> |
| 103 | struct apply |
| 104 | { |
| 105 | typedef typename multiply_typeof_helper<Y, Y>::type square_type; |
| 106 | typedef typename multiply_typeof_helper<Y, R>::type new_r; |
| 107 | typedef typename static_int_power_impl<(N >> 1)>::template apply<square_type, new_r> next; |
| 108 | typedef typename next::type type; |
| 109 | static BOOST_CONSTEXPR type call(const Y& y, const R& r) |
| 110 | { |
| 111 | return(next::call(static_cast<Y>(y * y), y * r)); |
| 112 | } |
| 113 | }; |
| 114 | }; |
| 115 | |
| 116 | template<> |
| 117 | struct static_int_power_impl<1, false> |
| 118 | { |
| 119 | template<class Y, class R> |
| 120 | struct apply |
| 121 | { |
| 122 | typedef typename multiply_typeof_helper<Y, R>::type type; |
| 123 | static BOOST_CONSTEXPR type call(const Y& y, const R& r) |
| 124 | { |
| 125 | return(y * r); |
| 126 | } |
| 127 | }; |
| 128 | }; |
| 129 | |
| 130 | template<> |
| 131 | struct static_int_power_impl<0, true> |
| 132 | { |
| 133 | template<class Y, class R> |
| 134 | struct apply |
| 135 | { |
| 136 | typedef R type; |
| 137 | static BOOST_CONSTEXPR R call(const Y&, const R& r) |
| 138 | { |
| 139 | return(r); |
| 140 | } |
| 141 | }; |
| 142 | }; |
| 143 | |
| 144 | template<int N, bool = (N < 0)> |
| 145 | struct static_int_power_sign_impl; |
| 146 | |
| 147 | template<int N> |
| 148 | struct static_int_power_sign_impl<N, false> |
| 149 | { |
| 150 | template<class Y> |
| 151 | struct apply |
| 152 | { |
| 153 | typedef typename static_int_power_impl<N>::template apply<Y, one> impl; |
| 154 | typedef typename impl::type type; |
| 155 | static BOOST_CONSTEXPR type call(const Y& y) |
| 156 | { |
| 157 | return(impl::call(y, one())); |
| 158 | } |
| 159 | }; |
| 160 | }; |
| 161 | |
| 162 | template<int N> |
| 163 | struct static_int_power_sign_impl<N, true> |
| 164 | { |
| 165 | template<class Y> |
| 166 | struct apply |
| 167 | { |
| 168 | typedef typename static_int_power_impl<-N>::template apply<Y, one> impl; |
| 169 | typedef typename divide_typeof_helper<one, typename impl::type>::type type; |
| 170 | static BOOST_CONSTEXPR type call(const Y& y) |
| 171 | { |
| 172 | return(one()/impl::call(y, one())); |
| 173 | } |
| 174 | }; |
| 175 | }; |
| 176 | |
| 177 | template<long N, class Y> |
| 178 | struct static_rational_power_impl<static_rational<N, 1>, Y> |
| 179 | { |
| 180 | typedef typename static_int_power_sign_impl<N>::template apply<Y> impl; |
| 181 | typedef typename impl::type type; |
| 182 | static BOOST_CONSTEXPR type call(const Y& y) |
| 183 | { |
| 184 | return(impl::call(y)); |
| 185 | } |
| 186 | }; |
| 187 | |
| 188 | template<class R, class Y> |
| 189 | BOOST_CONSTEXPR |
| 190 | typename detail::static_rational_power_impl<R, Y>::type static_rational_power(const Y& y) |
| 191 | { |
| 192 | return(detail::static_rational_power_impl<R, Y>::call(y)); |
| 193 | } |
| 194 | |
| 195 | } // namespace detail |
| 196 | |
| 197 | } // namespace units |
| 198 | |
| 199 | } // namespace boost |
| 200 | |
| 201 | #endif |
| 202 | |