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
11 #include <boost/multiprecision/detail/standalone_config.hpp>
12 #include <boost/multiprecision/detail/default_ops.hpp>
13 #include <boost/multiprecision/detail/no_exceptions_support.hpp>
14 #include <boost/multiprecision/detail/assert.hpp>
15 #include <boost/multiprecision/detail/functions/trunc.hpp>
19 #pragma warning(disable : 4127 6326)
22 namespace boost { namespace multiprecision { namespace detail {
24 template <class To, class From>
25 inline To do_cast(const From& from)
27 return static_cast<To>(from);
29 template <class To, class B, ::boost::multiprecision::expression_template_option et>
30 inline To do_cast(const number<B, et>& from)
32 return from.template convert_to<To>();
35 template <class To, class From>
36 void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_floating_point>& /*to_type*/, const std::integral_constant<int, number_kind_integer>& /*from_type*/)
38 using default_ops::eval_add;
39 using default_ops::eval_bitwise_and;
40 using default_ops::eval_convert_to;
41 using default_ops::eval_get_sign;
42 using default_ops::eval_is_zero;
43 using default_ops::eval_ldexp;
44 using default_ops::eval_right_shift;
45 // smallest unsigned type handled natively by "From" is likely to be it's limb_type:
46 using l_limb_type = typename canonical<unsigned char, From>::type;
47 // get the corresponding type that we can assign to "To":
48 using to_type = typename canonical<l_limb_type, To>::type;
50 bool is_neg = eval_get_sign(t) < 0;
53 // Pick off the first limb:
55 l_limb_type mask = static_cast<l_limb_type>(~static_cast<l_limb_type>(0));
57 eval_bitwise_and(fl, t, mask);
58 eval_convert_to(&limb, fl);
59 to = static_cast<to_type>(limb);
60 eval_right_shift(t, std::numeric_limits<l_limb_type>::digits);
62 // Then keep picking off more limbs until "t" is zero:
65 unsigned shift = std::numeric_limits<l_limb_type>::digits;
66 while (!eval_is_zero(t))
68 eval_bitwise_and(fl, t, mask);
69 eval_convert_to(&limb, fl);
70 l = static_cast<to_type>(limb);
71 eval_right_shift(t, std::numeric_limits<l_limb_type>::digits);
72 eval_ldexp(l, l, shift);
74 shift += std::numeric_limits<l_limb_type>::digits;
77 // Finish off by setting the sign:
83 template <class To, class From>
84 void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_integer>& /*to_type*/, const std::integral_constant<int, number_kind_integer>& /*from_type*/)
86 using default_ops::eval_bitwise_and;
87 using default_ops::eval_bitwise_or;
88 using default_ops::eval_convert_to;
89 using default_ops::eval_get_sign;
90 using default_ops::eval_is_zero;
91 using default_ops::eval_left_shift;
92 using default_ops::eval_right_shift;
93 // smallest unsigned type handled natively by "From" is likely to be it's limb_type:
94 using limb_type = typename canonical<unsigned char, From>::type;
95 // get the corresponding type that we can assign to "To":
96 using to_type = typename canonical<limb_type, To>::type;
98 bool is_neg = eval_get_sign(t) < 0;
101 // Pick off the first limb:
103 limb_type mask = static_cast<limb_type>(~static_cast<limb_type>(0));
105 eval_bitwise_and(fl, t, mask);
106 eval_convert_to(&limb, fl);
107 to = static_cast<to_type>(limb);
108 eval_right_shift(t, std::numeric_limits<limb_type>::digits);
110 // Then keep picking off more limbs until "t" is zero:
113 unsigned shift = std::numeric_limits<limb_type>::digits;
114 while (!eval_is_zero(t))
116 eval_bitwise_and(fl, t, mask);
117 eval_convert_to(&limb, fl);
118 l = static_cast<to_type>(limb);
119 eval_right_shift(t, std::numeric_limits<limb_type>::digits);
120 eval_left_shift(l, shift);
121 eval_bitwise_or(to, l);
122 shift += std::numeric_limits<limb_type>::digits;
125 // Finish off by setting the sign:
131 template <class To, class From>
132 void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_floating_point>& /*to_type*/, const std::integral_constant<int, number_kind_floating_point>& /*from_type*/)
135 #pragma warning(push)
136 //#pragma warning(disable : 4127)
139 // The code here only works when the radix of "From" is 2, we could try shifting by other
140 // radixes but it would complicate things.... use a string conversion when the radix is other
143 BOOST_IF_CONSTEXPR(std::numeric_limits<number<From> >::radix != 2)
145 to = from.str(0, std::ios_base::fmtflags()).c_str();
150 using ui_type = typename canonical<unsigned char, To>::type;
152 using default_ops::eval_add;
153 using default_ops::eval_convert_to;
154 using default_ops::eval_fpclassify;
155 using default_ops::eval_get_sign;
156 using default_ops::eval_is_zero;
157 using default_ops::eval_subtract;
160 // First classify the input, then handle the special cases:
162 int c = eval_fpclassify(from);
164 if (c == static_cast<int>(FP_ZERO))
169 else if (c == static_cast<int>(FP_NAN))
171 to = static_cast<const char*>("nan");
174 else if (c == static_cast<int>(FP_INFINITE))
176 to = static_cast<const char*>("inf");
177 if (eval_get_sign(from) < 0)
182 typename From::exponent_type e;
186 eval_frexp(f, from, &e);
188 constexpr const int shift = std::numeric_limits<std::intmax_t>::digits - 1;
190 while (!eval_is_zero(f))
192 // extract int sized bits from f:
193 eval_ldexp(f, f, shift);
196 eval_ldexp(to, to, shift);
197 typename boost::multiprecision::detail::canonical<std::intmax_t, To>::type ll;
198 eval_convert_to(&ll, term);
200 eval_subtract(f, term);
202 using to_exponent = typename To::exponent_type;
203 if (e > (std::numeric_limits<to_exponent>::max)())
205 to = static_cast<const char*>("inf");
206 if (eval_get_sign(from) < 0)
210 if (e < (std::numeric_limits<to_exponent>::min)())
213 if (eval_get_sign(from) < 0)
217 eval_ldexp(to, to, static_cast<to_exponent>(e));
224 template <class To, class From>
225 void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_rational>& /*to_type*/, const std::integral_constant<int, number_kind_rational>& /*from_type*/)
227 using to_component_type = typename component_type<number<To> >::type;
229 number<From> t(from);
230 to_component_type n(numerator(t)), d(denominator(t));
231 using default_ops::assign_components;
232 assign_components(to, n.backend(), d.backend());
235 template <class To, class From>
236 void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_rational>& /*to_type*/, const std::integral_constant<int, number_kind_integer>& /*from_type*/)
238 using to_component_type = typename component_type<number<To> >::type;
240 number<From> t(from);
241 to_component_type n(t), d(1);
242 using default_ops::assign_components;
243 assign_components(to, n.backend(), d.backend());
246 template <class LargeInteger>
247 inline typename std::enable_if<is_signed_number<LargeInteger>::value>::type make_positive(LargeInteger& val)
252 template <class LargeInteger>
253 inline typename std::enable_if<!is_signed_number<LargeInteger>::value>::type make_positive(LargeInteger&){}
255 template <class R, class LargeInteger>
256 R safe_convert_to_float(const LargeInteger& i)
261 BOOST_IF_CONSTEXPR(std::numeric_limits<R>::is_specialized && std::numeric_limits<R>::max_exponent)
265 std::size_t mb = msb(val);
266 if (mb >= std::numeric_limits<R>::max_exponent)
268 int scale_factor = static_cast<int>(mb) + 1 - std::numeric_limits<R>::max_exponent;
269 BOOST_MP_ASSERT(scale_factor >= 1);
270 val >>= scale_factor;
271 R result = val.template convert_to<R>();
272 BOOST_IF_CONSTEXPR(std::numeric_limits<R>::digits == 0 || std::numeric_limits<R>::digits >= std::numeric_limits<R>::max_exponent)
275 // Calculate and add on the remainder, only if there are more
276 // digits in the mantissa that the size of the exponent, in
277 // other words if we are dropping digits in the conversion
280 LargeInteger remainder(i);
281 remainder &= (LargeInteger(1) << scale_factor) - 1;
282 result += ldexp(safe_convert_to_float<R>(remainder), -scale_factor);
284 return i.sign() < 0 ? static_cast<R>(-result) : result;
287 return i.template convert_to<R>();
290 template <class To, class Integer>
291 inline typename std::enable_if<!(is_number<To>::value || std::is_floating_point<To>::value)>::type
292 generic_convert_rational_to_float_imp(To& result, const Integer& n, const Integer& d, const std::integral_constant<bool, true>&)
295 // If we get here, then there's something about one type or the other
296 // that prevents an exactly rounded result from being calculated
297 // (or at least it's not clear how to implement such a thing).
299 using default_ops::eval_divide;
300 number<To> fn(safe_convert_to_float<number<To> >(n)), fd(safe_convert_to_float<number<To> >(d));
301 eval_divide(result, fn.backend(), fd.backend());
303 template <class To, class Integer>
304 inline typename std::enable_if<is_number<To>::value || std::is_floating_point<To>::value>::type
305 generic_convert_rational_to_float_imp(To& result, const Integer& n, const Integer& d, const std::integral_constant<bool, true>&)
308 // If we get here, then there's something about one type or the other
309 // that prevents an exactly rounded result from being calculated
310 // (or at least it's not clear how to implement such a thing).
312 To fd(safe_convert_to_float<To>(d));
313 result = safe_convert_to_float<To>(n);
317 template <class To, class Integer>
318 typename std::enable_if<is_number<To>::value || std::is_floating_point<To>::value>::type
319 generic_convert_rational_to_float_imp(To& result, Integer& num, Integer& denom, const std::integral_constant<bool, false>&)
322 // If we get here, then the precision of type To is known, and the integer type is unbounded
323 // so we can use integer division plus manipulation of the remainder to get an exactly
337 std::ptrdiff_t denom_bits = msb(denom);
338 std::ptrdiff_t shift = std::numeric_limits<To>::digits + denom_bits - msb(num);
342 denom <<= boost::multiprecision::detail::unsigned_abs(shift);
344 divide_qr(num, denom, q, r);
345 std::ptrdiff_t q_bits = msb(q);
346 if (q_bits == std::numeric_limits<To>::digits - 1)
349 // Round up if 2 * r > denom:
352 int c = r.compare(denom);
355 else if ((c == 0) && (q & 1u))
362 BOOST_MP_ASSERT(q_bits == std::numeric_limits<To>::digits);
364 // We basically already have the rounding info:
373 result = do_cast<To>(q);
374 result = ldexp(result, static_cast<int>(-shift));
378 template <class To, class Integer>
379 inline typename std::enable_if<!(is_number<To>::value || std::is_floating_point<To>::value)>::type
380 generic_convert_rational_to_float_imp(To& result, Integer& num, Integer& denom, const std::integral_constant<bool, false>& tag)
383 generic_convert_rational_to_float_imp(t, num, denom, tag);
384 result = t.backend();
387 template <class To, class From>
388 inline void generic_convert_rational_to_float(To& result, const From& f)
391 // Type From is always a Backend to number<>, or an
392 // instance of number<>, but we allow
393 // To to be either a Backend type, or a real number type,
394 // that way we can call this from generic conversions, and
395 // from specific conversions to built in types.
397 using actual_from_type = typename std::conditional<is_number<From>::value, From, number<From> >::type ;
398 using actual_to_type = typename std::conditional<is_number<To>::value || std::is_floating_point<To>::value, To, number<To> >::type ;
399 using integer_type = typename component_type<actual_from_type>::type ;
400 using dispatch_tag = std::integral_constant<bool, !std::numeric_limits<integer_type>::is_specialized || std::numeric_limits<integer_type>::is_bounded || !std::numeric_limits<actual_to_type>::is_specialized || !std::numeric_limits<actual_to_type>::is_bounded || (std::numeric_limits<actual_to_type>::radix != 2)>;
402 integer_type n(numerator(static_cast<actual_from_type>(f))), d(denominator(static_cast<actual_from_type>(f)));
403 generic_convert_rational_to_float_imp(result, n, d, dispatch_tag());
406 template <class To, class From>
407 inline void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_floating_point>& /*to_type*/, const std::integral_constant<int, number_kind_rational>& /*from_type*/)
409 generic_convert_rational_to_float(to, from);
412 template <class To, class From>
413 void generic_interconvert_float2rational(To& to, const From& from, const std::integral_constant<int, 2>& /*radix*/)
417 using ui_type = typename std::tuple_element<0, typename To::unsigned_types>::type;
418 constexpr const int shift = std::numeric_limits<long long>::digits;
419 typename From::exponent_type e;
420 typename component_type<number<To>>::type num, denom;
421 number<From> val(from);
422 val = frexp(val, &e);
425 val = ldexp(val, shift);
427 long long ll = boost::multiprecision::detail::lltrunc(val);
437 assign_components(to, num.backend(), denom.backend());
440 template <class To, class From, int Radix>
441 void generic_interconvert_float2rational(To& to, const From& from, const std::integral_constant<int, Radix>& /*radix*/)
448 // This is almost the same as the binary case above, but we have to use
449 // scalbn and ilogb rather than ldexp and frexp, we also only extract
450 // one Radix digit at a time which is terribly inefficient!
452 using ui_type = typename std::tuple_element<0, typename To::unsigned_types>::type;
453 typename From::exponent_type e;
454 typename component_type<number<To>>::type num, denom;
455 number<From> val(from);
464 val = scalbn(val, -e);
467 long long ll = boost::multiprecision::detail::lltrunc(val);
469 val = scalbn(val, 1);
475 denom = ui_type(Radix);
476 denom = pow(denom, abs(e));
482 assign_components(to, num.backend(), denom.backend());
485 template <class To, class From>
486 void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_rational>& /*to_type*/, const std::integral_constant<int, number_kind_floating_point>& /*from_type*/)
488 generic_interconvert_float2rational(to, from, std::integral_constant<int, std::numeric_limits<number<From> >::is_specialized ? std::numeric_limits<number<From> >::radix : 2>());
491 template <class To, class From>
492 void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_integer>& /*to_type*/, const std::integral_constant<int, number_kind_rational>& /*from_type*/)
494 number<From> t(from);
495 number<To> result(numerator(t) / denominator(t));
496 to = result.backend();
499 template <class To, class From>
500 void generic_interconvert_float2int(To& to, const From& from, const std::integral_constant<int, 2>& /*radix*/)
505 using exponent_type = typename From::exponent_type;
506 constexpr const exponent_type shift = std::numeric_limits<long long>::digits;
509 number<From> val(from);
510 val = frexp(val, &e);
514 val.backend().negate();
519 exponent_type s = (std::min)(e, shift);
522 long long ll = boost::multiprecision::detail::lltrunc(val);
532 template <class To, class From, int Radix>
533 void generic_interconvert_float2int(To& to, const From& from, const std::integral_constant<int, Radix>& /*radix*/)
538 // This is almost the same as the binary case above, but we have to use
539 // scalbn and ilogb rather than ldexp and frexp, we also only extract
540 // one Radix digit at a time which is terribly inefficient!
542 typename From::exponent_type e;
544 number<From> val(from);
546 val = scalbn(val, -e);
549 long long ll = boost::multiprecision::detail::lltrunc(val);
551 val = scalbn(val, 1);
559 template <class To, class From>
560 void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_integer>& /*to_type*/, const std::integral_constant<int, number_kind_floating_point>& /*from_type*/)
562 generic_interconvert_float2int(to, from, std::integral_constant<int, (std::numeric_limits<number<From> >::is_specialized ? std::numeric_limits<number<From> >::radix : 2)>());
565 template <class To, class From, class tag>
566 void generic_interconvert_complex_to_scalar(To& to, const From& from, const std::integral_constant<bool, true>&, const tag&)
568 // We just want the real part, and "to" is the correct type already:
573 if (!eval_is_zero(im))
574 BOOST_MP_THROW_EXCEPTION(std::runtime_error("Could not convert imaginary number to scalar."));
576 template <class To, class From>
577 void generic_interconvert_complex_to_scalar(To& to, const From& from, const std::integral_constant<bool, false>&, const std::integral_constant<bool, true>&)
579 using component_number = typename component_type<number<From> >::type;
580 using component_backend = typename component_number::backend_type ;
582 // Get the real part and copy-construct the result from it:
584 scoped_precision_options<component_number> scope(from);
586 generic_interconvert_complex_to_scalar(r, from, std::integral_constant<bool, true>(), std::integral_constant<bool, true>());
589 template <class To, class From>
590 void generic_interconvert_complex_to_scalar(To& to, const From& from, const std::integral_constant<bool, false>&, const std::integral_constant<bool, false>&)
592 using component_number = typename component_type<number<From> >::type;
593 using component_backend = typename component_number::backend_type;
595 // Get the real part and use a generic_interconvert to type To:
597 scoped_precision_options<component_number> scope(from);
599 generic_interconvert_complex_to_scalar(r, from, std::integral_constant<bool, true>(), std::integral_constant<bool, true>());
600 generic_interconvert(to, r, std::integral_constant<int, number_category<To>::value>(), std::integral_constant<int, number_category<component_backend>::value>());
603 template <class To, class From>
604 void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_floating_point>& /*to_type*/, const std::integral_constant<int, number_kind_complex>& /*from_type*/)
606 using component_number = typename component_type<number<From> >::type;
607 using component_backend = typename component_number::backend_type ;
609 generic_interconvert_complex_to_scalar(to, from, std::integral_constant<bool, std::is_same<component_backend, To>::value>(), std::integral_constant<bool, std::is_constructible<To, const component_backend&>::value>());
611 template <class To, class From>
612 void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_integer>& /*to_type*/, const std::integral_constant<int, number_kind_complex>& /*from_type*/)
614 using component_number = typename component_type<number<From> >::type;
615 using component_backend = typename component_number::backend_type ;
617 generic_interconvert_complex_to_scalar(to, from, std::integral_constant<bool, std::is_same<component_backend, To>::value>(), std::integral_constant<bool, std::is_constructible<To, const component_backend&>::value>());
619 template <class To, class From>
620 void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_rational>& /*to_type*/, const std::integral_constant<int, number_kind_complex>& /*from_type*/)
622 using component_number = typename component_type<number<From> >::type;
623 using component_backend = typename component_number::backend_type ;
625 generic_interconvert_complex_to_scalar(to, from, std::integral_constant<bool, std::is_same<component_backend, To>::value>(), std::integral_constant<bool, std::is_constructible<To, const component_backend&>::value>());
627 template <class To, class From>
628 void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_complex>& /*to_type*/, const std::integral_constant<int, number_kind_integer>& /*from_type*/)
630 using component_number = typename component_type<number<To> >::type;
632 scoped_source_precision<number<From> > scope1;
633 scoped_precision_options<component_number> scope2(number<To>::thread_default_precision(), number<To>::thread_default_variable_precision_options());
637 number<From> f(from);
638 component_number scalar(f);
639 number<To> result(scalar);
640 to = result.backend();
642 template <class To, class From>
643 void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_complex>& /*to_type*/, const std::integral_constant<int, number_kind_rational>& /*from_type*/)
645 using component_number = typename component_type<number<To> >::type;
647 scoped_source_precision<number<From> > scope1;
648 scoped_precision_options<component_number> scope2(number<To>::thread_default_precision(), number<To>::thread_default_variable_precision_options());
652 number<From> f(from);
653 component_number scalar(f);
654 number<To> result(scalar);
655 to = result.backend();
657 template <class To, class From>
658 void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_complex>& /*to_type*/, const std::integral_constant<int, number_kind_floating_point>& /*from_type*/)
660 using component_number = typename component_type<number<To> >::type;
662 scoped_source_precision<number<From> > scope1;
663 scoped_precision_options<component_number> scope2(number<To>::thread_default_precision(), number<To>::thread_default_variable_precision_options());
667 number<From> f(from);
668 component_number scalar(f);
669 number<To> result(scalar);
670 to = result.backend();
672 template <class To, class From, int Tag1, int Tag2>
673 void generic_interconvert(To& /*to*/, const From& /*from*/, const std::integral_constant<int, Tag1>& /*to_type*/, const std::integral_constant<int, Tag2>& /*from_type*/)
675 static_assert(sizeof(To) == 0, "Sorry, you asked for a conversion bewteen types that hasn't been implemented yet!!");
680 } // namespace boost::multiprecision::detail
686 #endif // BOOST_MP_GENERIC_INTERCONVERT_HPP