1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright 2011 John Maddock. Distributed under the Boost
3 // Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 #ifndef BOOST_MP_GENERIC_INTERCONVERT_HPP
7 #define BOOST_MP_GENERIC_INTERCONVERT_HPP
9 #include <boost/multiprecision/detail/default_ops.hpp>
13 #pragma warning(disable:4127 6326)
16 namespace boost{ namespace multiprecision{ namespace detail{
18 template <class To, class From>
19 inline To do_cast(const From & from)
21 return static_cast<To>(from);
23 template <class To, class B, ::boost::multiprecision::expression_template_option et>
24 inline To do_cast(const number<B, et>& from)
26 return from.template convert_to<To>();
29 template <class To, class From>
30 void generic_interconvert(To& to, const From& from, const mpl::int_<number_kind_floating_point>& /*to_type*/, const mpl::int_<number_kind_integer>& /*from_type*/)
32 using default_ops::eval_get_sign;
33 using default_ops::eval_bitwise_and;
34 using default_ops::eval_convert_to;
35 using default_ops::eval_right_shift;
36 using default_ops::eval_ldexp;
37 using default_ops::eval_add;
38 using default_ops::eval_is_zero;
39 // smallest unsigned type handled natively by "From" is likely to be it's limb_type:
40 typedef typename canonical<unsigned char, From>::type l_limb_type;
41 // get the corresponding type that we can assign to "To":
42 typedef typename canonical<l_limb_type, To>::type to_type;
44 bool is_neg = eval_get_sign(t) < 0;
47 // Pick off the first limb:
49 l_limb_type mask = static_cast<l_limb_type>(~static_cast<l_limb_type>(0));
51 eval_bitwise_and(fl, t, mask);
52 eval_convert_to(&limb, fl);
53 to = static_cast<to_type>(limb);
54 eval_right_shift(t, std::numeric_limits<l_limb_type>::digits);
56 // Then keep picking off more limbs until "t" is zero:
59 unsigned shift = std::numeric_limits<l_limb_type>::digits;
60 while(!eval_is_zero(t))
62 eval_bitwise_and(fl, t, mask);
63 eval_convert_to(&limb, fl);
64 l = static_cast<to_type>(limb);
65 eval_right_shift(t, std::numeric_limits<l_limb_type>::digits);
66 eval_ldexp(l, l, shift);
68 shift += std::numeric_limits<l_limb_type>::digits;
71 // Finish off by setting the sign:
77 template <class To, class From>
78 void generic_interconvert(To& to, const From& from, const mpl::int_<number_kind_integer>& /*to_type*/, const mpl::int_<number_kind_integer>& /*from_type*/)
80 using default_ops::eval_get_sign;
81 using default_ops::eval_bitwise_and;
82 using default_ops::eval_convert_to;
83 using default_ops::eval_right_shift;
84 using default_ops::eval_left_shift;
85 using default_ops::eval_bitwise_or;
86 using default_ops::eval_is_zero;
87 // smallest unsigned type handled natively by "From" is likely to be it's limb_type:
88 typedef typename canonical<unsigned char, From>::type limb_type;
89 // get the corresponding type that we can assign to "To":
90 typedef typename canonical<limb_type, To>::type to_type;
92 bool is_neg = eval_get_sign(t) < 0;
95 // Pick off the first limb:
97 limb_type mask = static_cast<limb_type>(~static_cast<limb_type>(0));
99 eval_bitwise_and(fl, t, mask);
100 eval_convert_to(&limb, fl);
101 to = static_cast<to_type>(limb);
102 eval_right_shift(t, std::numeric_limits<limb_type>::digits);
104 // Then keep picking off more limbs until "t" is zero:
107 unsigned shift = std::numeric_limits<limb_type>::digits;
108 while(!eval_is_zero(t))
110 eval_bitwise_and(fl, t, mask);
111 eval_convert_to(&limb, fl);
112 l = static_cast<to_type>(limb);
113 eval_right_shift(t, std::numeric_limits<limb_type>::digits);
114 eval_left_shift(l, shift);
115 eval_bitwise_or(to, l);
116 shift += std::numeric_limits<limb_type>::digits;
119 // Finish off by setting the sign:
125 template <class To, class From>
126 void generic_interconvert(To& to, const From& from, const mpl::int_<number_kind_floating_point>& /*to_type*/, const mpl::int_<number_kind_floating_point>& /*from_type*/)
129 #pragma warning(push)
130 #pragma warning(disable:4127)
133 // The code here only works when the radix of "From" is 2, we could try shifting by other
134 // radixes but it would complicate things.... use a string conversion when the radix is other
137 if(std::numeric_limits<number<From> >::radix != 2)
139 to = from.str(0, std::ios_base::fmtflags()).c_str();
144 typedef typename canonical<unsigned char, To>::type ui_type;
146 using default_ops::eval_fpclassify;
147 using default_ops::eval_add;
148 using default_ops::eval_subtract;
149 using default_ops::eval_convert_to;
150 using default_ops::eval_get_sign;
151 using default_ops::eval_is_zero;
154 // First classify the input, then handle the special cases:
156 int c = eval_fpclassify(from);
158 if(c == (int)FP_ZERO)
163 else if(c == (int)FP_NAN)
165 to = static_cast<const char*>("nan");
168 else if(c == (int)FP_INFINITE)
170 to = static_cast<const char*>("inf");
171 if(eval_get_sign(from) < 0)
176 typename From::exponent_type e;
180 eval_frexp(f, from, &e);
182 static const int shift = std::numeric_limits<boost::intmax_t>::digits - 1;
184 while(!eval_is_zero(f))
186 // extract int sized bits from f:
187 eval_ldexp(f, f, shift);
190 eval_ldexp(to, to, shift);
191 typename boost::multiprecision::detail::canonical<boost::intmax_t, To>::type ll;
192 eval_convert_to(&ll, term);
194 eval_subtract(f, term);
196 typedef typename To::exponent_type to_exponent;
197 if((e > (std::numeric_limits<to_exponent>::max)()) || (e < (std::numeric_limits<to_exponent>::min)()))
199 to = static_cast<const char*>("inf");
200 if(eval_get_sign(from) < 0)
204 eval_ldexp(to, to, static_cast<to_exponent>(e));
210 template <class To, class From>
211 void generic_interconvert(To& to, const From& from, const mpl::int_<number_kind_rational>& /*to_type*/, const mpl::int_<number_kind_rational>& /*from_type*/)
213 typedef typename component_type<number<To> >::type to_component_type;
215 number<From> t(from);
216 to_component_type n(numerator(t)), d(denominator(t));
217 using default_ops::assign_components;
218 assign_components(to, n.backend(), d.backend());
221 template <class To, class From>
222 void generic_interconvert(To& to, const From& from, const mpl::int_<number_kind_rational>& /*to_type*/, const mpl::int_<number_kind_integer>& /*from_type*/)
224 typedef typename component_type<number<To> >::type to_component_type;
226 number<From> t(from);
227 to_component_type n(t), d(1);
228 using default_ops::assign_components;
229 assign_components(to, n.backend(), d.backend());
232 template <class R, class LargeInteger>
233 R safe_convert_to_float(const LargeInteger& i)
238 if(std::numeric_limits<R>::is_specialized && std::numeric_limits<R>::max_exponent)
243 unsigned mb = msb(val);
244 if(mb >= std::numeric_limits<R>::max_exponent)
246 int scale_factor = (int)mb + 1 - std::numeric_limits<R>::max_exponent;
247 BOOST_ASSERT(scale_factor >= 1);
248 val >>= scale_factor;
249 R result = val.template convert_to<R>();
250 if(std::numeric_limits<R>::digits == 0 || std::numeric_limits<R>::digits >= std::numeric_limits<R>::max_exponent)
253 // Calculate and add on the remainder, only if there are more
254 // digits in the mantissa that the size of the exponent, in
255 // other words if we are dropping digits in the conversion
258 LargeInteger remainder(i);
259 remainder &= (LargeInteger(1) << scale_factor) - 1;
260 result += ldexp(safe_convert_to_float<R>(remainder), -scale_factor);
262 return i.sign() < 0 ? static_cast<R>(-result) : result;
265 return i.template convert_to<R>();
268 template <class To, class Integer>
269 inline typename disable_if_c<is_number<To>::value || is_floating_point<To>::value>::type
270 generic_convert_rational_to_float_imp(To& result, const Integer& n, const Integer& d, const mpl::true_&)
273 // If we get here, then there's something about one type or the other
274 // that prevents an exactly rounded result from being calculated
275 // (or at least it's not clear how to implement such a thing).
277 using default_ops::eval_divide;
278 number<To> fn(safe_convert_to_float<number<To> >(n)), fd(safe_convert_to_float<number<To> >(d));
279 eval_divide(result, fn.backend(), fd.backend());
281 template <class To, class Integer>
282 inline typename enable_if_c<is_number<To>::value || is_floating_point<To>::value>::type
283 generic_convert_rational_to_float_imp(To& result, const Integer& n, const Integer& d, const mpl::true_&)
286 // If we get here, then there's something about one type or the other
287 // that prevents an exactly rounded result from being calculated
288 // (or at least it's not clear how to implement such a thing).
290 To fd(safe_convert_to_float<To>(d));
291 result = safe_convert_to_float<To>(n);
295 template <class To, class Integer>
296 typename enable_if_c<is_number<To>::value || is_floating_point<To>::value>::type
297 generic_convert_rational_to_float_imp(To& result, Integer& num, Integer& denom, const mpl::false_&)
300 // If we get here, then the precision of type To is known, and the integer type is unbounded
301 // so we can use integer division plus manipulation of the remainder to get an exactly
315 int denom_bits = msb(denom);
316 int shift = std::numeric_limits<To>::digits + denom_bits - msb(num);
320 denom <<= boost::multiprecision::detail::unsigned_abs(shift);
322 divide_qr(num, denom, q, r);
324 if(q_bits == std::numeric_limits<To>::digits - 1)
327 // Round up if 2 * r > denom:
330 int c = r.compare(denom);
333 else if((c == 0) && (q & 1u))
340 BOOST_ASSERT(q_bits == std::numeric_limits<To>::digits);
342 // We basically already have the rounding info:
351 result = do_cast<To>(q);
352 result = ldexp(result, -shift);
356 template <class To, class Integer>
357 inline typename disable_if_c<is_number<To>::value || is_floating_point<To>::value>::type
358 generic_convert_rational_to_float_imp(To& result, Integer& num, Integer& denom, const mpl::false_& tag)
361 generic_convert_rational_to_float_imp(t, num, denom, tag);
362 result = t.backend();
365 template <class To, class From>
366 inline void generic_convert_rational_to_float(To& result, const From& f)
369 // Type From is always a Backend to number<>, or an
370 // instance of number<>, but we allow
371 // To to be either a Backend type, or a real number type,
372 // that way we can call this from generic conversions, and
373 // from specific conversions to built in types.
375 typedef typename mpl::if_c<is_number<From>::value, From, number<From> >::type actual_from_type;
376 typedef typename mpl::if_c<is_number<To>::value || is_floating_point<To>::value, To, number<To> >::type actual_to_type;
377 typedef typename component_type<actual_from_type>::type integer_type;
378 typedef mpl::bool_<!std::numeric_limits<integer_type>::is_specialized
379 || std::numeric_limits<integer_type>::is_bounded
380 || !std::numeric_limits<actual_to_type>::is_specialized
381 || !std::numeric_limits<actual_to_type>::is_bounded
382 || (std::numeric_limits<actual_to_type>::radix != 2)> dispatch_tag;
384 integer_type n(numerator(static_cast<actual_from_type>(f))), d(denominator(static_cast<actual_from_type>(f)));
385 generic_convert_rational_to_float_imp(result, n, d, dispatch_tag());
388 template <class To, class From>
389 inline void generic_interconvert(To& to, const From& from, const mpl::int_<number_kind_floating_point>& /*to_type*/, const mpl::int_<number_kind_rational>& /*from_type*/)
391 generic_convert_rational_to_float(to, from);
394 template <class To, class From>
395 void generic_interconvert_float2rational(To& to, const From& from, const mpl::int_<2>& /*radix*/)
397 typedef typename mpl::front<typename To::unsigned_types>::type ui_type;
398 static const int shift = std::numeric_limits<boost::long_long_type>::digits;
399 typename From::exponent_type e;
400 typename component_type<number<To> >::type num, denom;
401 number<From> val(from);
402 val = frexp(val, &e);
405 val = ldexp(val, shift);
407 boost::long_long_type ll = boost::math::lltrunc(val);
417 assign_components(to, num.backend(), denom.backend());
420 template <class To, class From, int Radix>
421 void generic_interconvert_float2rational(To& to, const From& from, const mpl::int_<Radix>& /*radix*/)
424 // This is almost the same as the binary case above, but we have to use
425 // scalbn and ilogb rather than ldexp and frexp, we also only extract
426 // one Radix digit at a time which is terribly inefficient!
428 typedef typename mpl::front<typename To::unsigned_types>::type ui_type;
429 typename From::exponent_type e;
430 typename component_type<number<To> >::type num, denom;
431 number<From> val(from);
433 val = scalbn(val, -e);
436 boost::long_long_type ll = boost::math::lltrunc(val);
438 val = scalbn(val, 1);
444 denom = ui_type(Radix);
445 denom = pow(denom, abs(e));
451 assign_components(to, num.backend(), denom.backend());
454 template <class To, class From>
455 void generic_interconvert(To& to, const From& from, const mpl::int_<number_kind_rational>& /*to_type*/, const mpl::int_<number_kind_floating_point>& /*from_type*/)
457 generic_interconvert_float2rational(to, from, mpl::int_<std::numeric_limits<number<From> >::radix>());
460 template <class To, class From>
461 void generic_interconvert(To& to, const From& from, const mpl::int_<number_kind_integer>& /*to_type*/, const mpl::int_<number_kind_rational>& /*from_type*/)
463 number<From> t(from);
464 number<To> result(numerator(t) / denominator(t));
465 to = result.backend();
468 template <class To, class From>
469 void generic_interconvert_float2int(To& to, const From& from, const mpl::int_<2>& /*radix*/)
471 typedef typename From::exponent_type exponent_type;
472 static const exponent_type shift = std::numeric_limits<boost::long_long_type>::digits;
475 number<From> val(from);
476 val = frexp(val, &e);
479 int s = (std::min)(e, shift);
482 boost::long_long_type ll = boost::math::lltrunc(val);
490 template <class To, class From, int Radix>
491 void generic_interconvert_float2int(To& to, const From& from, const mpl::int_<Radix>& /*radix*/)
494 // This is almost the same as the binary case above, but we have to use
495 // scalbn and ilogb rather than ldexp and frexp, we also only extract
496 // one Radix digit at a time which is terribly inefficient!
498 typename From::exponent_type e;
500 number<From> val(from);
502 val = scalbn(val, -e);
505 boost::long_long_type ll = boost::math::lltrunc(val);
507 val = scalbn(val, 1);
515 template <class To, class From>
516 void generic_interconvert(To& to, const From& from, const mpl::int_<number_kind_integer>& /*to_type*/, const mpl::int_<number_kind_floating_point>& /*from_type*/)
518 generic_interconvert_float2int(to, from, mpl::int_<std::numeric_limits<number<From> >::radix>());
529 #endif // BOOST_MP_GENERIC_INTERCONVERT_HPP