generic_interconvert.hpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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)
  5. #ifndef BOOST_MP_GENERIC_INTERCONVERT_HPP
  6. #define BOOST_MP_GENERIC_INTERCONVERT_HPP
  7. #include <cmath>
  8. #include <limits>
  9. #include <boost/multiprecision/detail/standalone_config.hpp>
  10. #include <boost/multiprecision/detail/default_ops.hpp>
  11. #include <boost/multiprecision/detail/no_exceptions_support.hpp>
  12. #include <boost/multiprecision/detail/assert.hpp>
  13. #include <boost/multiprecision/detail/functions/trunc.hpp>
  14. #ifdef BOOST_MSVC
  15. #pragma warning(push)
  16. #pragma warning(disable : 4127 6326)
  17. #endif
  18. namespace boost { namespace multiprecision { namespace detail {
  19. template <class To, class From>
  20. inline To do_cast(const From& from)
  21. {
  22. return static_cast<To>(from);
  23. }
  24. template <class To, class B, ::boost::multiprecision::expression_template_option et>
  25. inline To do_cast(const number<B, et>& from)
  26. {
  27. return from.template convert_to<To>();
  28. }
  29. template <class To, class From>
  30. 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*/)
  31. {
  32. using default_ops::eval_add;
  33. using default_ops::eval_bitwise_and;
  34. using default_ops::eval_convert_to;
  35. using default_ops::eval_get_sign;
  36. using default_ops::eval_is_zero;
  37. using default_ops::eval_ldexp;
  38. using default_ops::eval_right_shift;
  39. // smallest unsigned type handled natively by "From" is likely to be it's limb_type:
  40. using l_limb_type = typename canonical<unsigned char, From>::type;
  41. // get the corresponding type that we can assign to "To":
  42. using to_type = typename canonical<l_limb_type, To>::type;
  43. From t(from);
  44. bool is_neg = eval_get_sign(t) < 0;
  45. if (is_neg)
  46. t.negate();
  47. // Pick off the first limb:
  48. l_limb_type limb;
  49. l_limb_type mask = static_cast<l_limb_type>(~static_cast<l_limb_type>(0));
  50. From fl;
  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);
  55. //
  56. // Then keep picking off more limbs until "t" is zero:
  57. //
  58. To l;
  59. unsigned shift = std::numeric_limits<l_limb_type>::digits;
  60. while (!eval_is_zero(t))
  61. {
  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);
  67. eval_add(to, l);
  68. shift += std::numeric_limits<l_limb_type>::digits;
  69. }
  70. //
  71. // Finish off by setting the sign:
  72. //
  73. if (is_neg)
  74. to.negate();
  75. }
  76. template <class To, class From>
  77. 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*/)
  78. {
  79. using default_ops::eval_bitwise_and;
  80. using default_ops::eval_bitwise_or;
  81. using default_ops::eval_convert_to;
  82. using default_ops::eval_get_sign;
  83. using default_ops::eval_is_zero;
  84. using default_ops::eval_left_shift;
  85. using default_ops::eval_right_shift;
  86. // smallest unsigned type handled natively by "From" is likely to be it's limb_type:
  87. using limb_type = typename canonical<unsigned char, From>::type;
  88. // get the corresponding type that we can assign to "To":
  89. using to_type = typename canonical<limb_type, To>::type;
  90. From t(from);
  91. bool is_neg = eval_get_sign(t) < 0;
  92. if (is_neg)
  93. t.negate();
  94. // Pick off the first limb:
  95. limb_type limb;
  96. limb_type mask = static_cast<limb_type>(~static_cast<limb_type>(0));
  97. From fl;
  98. eval_bitwise_and(fl, t, mask);
  99. eval_convert_to(&limb, fl);
  100. to = static_cast<to_type>(limb);
  101. eval_right_shift(t, std::numeric_limits<limb_type>::digits);
  102. //
  103. // Then keep picking off more limbs until "t" is zero:
  104. //
  105. To l;
  106. unsigned shift = std::numeric_limits<limb_type>::digits;
  107. while (!eval_is_zero(t))
  108. {
  109. eval_bitwise_and(fl, t, mask);
  110. eval_convert_to(&limb, fl);
  111. l = static_cast<to_type>(limb);
  112. eval_right_shift(t, std::numeric_limits<limb_type>::digits);
  113. eval_left_shift(l, shift);
  114. eval_bitwise_or(to, l);
  115. shift += std::numeric_limits<limb_type>::digits;
  116. }
  117. //
  118. // Finish off by setting the sign:
  119. //
  120. if (is_neg)
  121. to.negate();
  122. }
  123. template <class To, class From>
  124. 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*/)
  125. {
  126. #ifdef BOOST_MSVC
  127. #pragma warning(push)
  128. //#pragma warning(disable : 4127)
  129. #endif
  130. //
  131. // The code here only works when the radix of "From" is 2, we could try shifting by other
  132. // radixes but it would complicate things.... use a string conversion when the radix is other
  133. // than 2:
  134. //
  135. BOOST_IF_CONSTEXPR(std::numeric_limits<number<From> >::radix != 2)
  136. {
  137. to = from.str(0, std::ios_base::fmtflags()).c_str();
  138. return;
  139. }
  140. else
  141. {
  142. using ui_type = typename canonical<unsigned char, To>::type;
  143. using default_ops::eval_add;
  144. using default_ops::eval_convert_to;
  145. using default_ops::eval_fpclassify;
  146. using default_ops::eval_get_sign;
  147. using default_ops::eval_is_zero;
  148. using default_ops::eval_subtract;
  149. //
  150. // First classify the input, then handle the special cases:
  151. //
  152. int c = eval_fpclassify(from);
  153. if (c == static_cast<int>(FP_ZERO))
  154. {
  155. to = ui_type(0);
  156. return;
  157. }
  158. else if (c == static_cast<int>(FP_NAN))
  159. {
  160. to = static_cast<const char*>("nan");
  161. return;
  162. }
  163. else if (c == static_cast<int>(FP_INFINITE))
  164. {
  165. to = static_cast<const char*>("inf");
  166. if (eval_get_sign(from) < 0)
  167. to.negate();
  168. return;
  169. }
  170. typename From::exponent_type e;
  171. From f, term;
  172. to = ui_type(0);
  173. eval_frexp(f, from, &e);
  174. constexpr int shift = std::numeric_limits<std::intmax_t>::digits - 1;
  175. while (!eval_is_zero(f))
  176. {
  177. // extract int sized bits from f:
  178. eval_ldexp(f, f, shift);
  179. eval_floor(term, f);
  180. e -= shift;
  181. eval_ldexp(to, to, shift);
  182. typename boost::multiprecision::detail::canonical<std::intmax_t, To>::type ll;
  183. eval_convert_to(&ll, term);
  184. eval_add(to, ll);
  185. eval_subtract(f, term);
  186. }
  187. using to_exponent = typename To::exponent_type;
  188. if (e > (std::numeric_limits<to_exponent>::max)())
  189. {
  190. to = static_cast<const char*>("inf");
  191. if (eval_get_sign(from) < 0)
  192. to.negate();
  193. return;
  194. }
  195. if (e < (std::numeric_limits<to_exponent>::min)())
  196. {
  197. to = ui_type(0);
  198. if (eval_get_sign(from) < 0)
  199. to.negate();
  200. return;
  201. }
  202. eval_ldexp(to, to, static_cast<to_exponent>(e));
  203. }
  204. #ifdef BOOST_MSVC
  205. #pragma warning(pop)
  206. #endif
  207. }
  208. template <class To, class From>
  209. 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*/)
  210. {
  211. using to_component_type = typename component_type<number<To> >::type;
  212. number<From> t(from);
  213. to_component_type n(numerator(t)), d(denominator(t));
  214. using default_ops::assign_components;
  215. assign_components(to, n.backend(), d.backend());
  216. }
  217. template <class To, class From>
  218. 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*/)
  219. {
  220. using to_component_type = typename component_type<number<To> >::type;
  221. number<From> t(from);
  222. to_component_type n(t), d(1);
  223. using default_ops::assign_components;
  224. assign_components(to, n.backend(), d.backend());
  225. }
  226. template <class LargeInteger>
  227. inline typename std::enable_if<is_signed_number<LargeInteger>::value>::type make_positive(LargeInteger& val)
  228. {
  229. if (val.sign() < 0)
  230. val = -val;
  231. }
  232. template <class LargeInteger>
  233. inline typename std::enable_if<!is_signed_number<LargeInteger>::value>::type make_positive(LargeInteger&){}
  234. template <class R, class LargeInteger>
  235. R safe_convert_to_float(const LargeInteger& i)
  236. {
  237. if (!i)
  238. return R(0);
  239. BOOST_IF_CONSTEXPR(std::numeric_limits<R>::is_specialized && std::numeric_limits<R>::max_exponent)
  240. {
  241. using std::ldexp;
  242. LargeInteger val(i);
  243. make_positive(val);
  244. std::size_t mb = msb(val);
  245. if (mb >= std::numeric_limits<R>::max_exponent)
  246. {
  247. int scale_factor = static_cast<int>(mb) + 1 - std::numeric_limits<R>::max_exponent;
  248. BOOST_MP_ASSERT(scale_factor >= 1);
  249. val >>= scale_factor;
  250. R result = val.template convert_to<R>();
  251. BOOST_IF_CONSTEXPR(std::numeric_limits<R>::digits == 0 || std::numeric_limits<R>::digits >= std::numeric_limits<R>::max_exponent)
  252. {
  253. //
  254. // Calculate and add on the remainder, only if there are more
  255. // digits in the mantissa that the size of the exponent, in
  256. // other words if we are dropping digits in the conversion
  257. // otherwise:
  258. //
  259. LargeInteger remainder(i);
  260. remainder &= (LargeInteger(1) << scale_factor) - 1;
  261. result += ldexp(safe_convert_to_float<R>(remainder), -scale_factor);
  262. }
  263. return i.sign() < 0 ? static_cast<R>(-result) : result;
  264. }
  265. }
  266. return i.template convert_to<R>();
  267. }
  268. template <class To, class Integer>
  269. inline typename std::enable_if<!(is_number<To>::value || std::is_floating_point<To>::value)>::type
  270. generic_convert_rational_to_float_imp(To& result, const Integer& n, const Integer& d, const std::integral_constant<bool, true>&)
  271. {
  272. //
  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).
  276. //
  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());
  280. }
  281. template <class To, class Integer>
  282. inline typename std::enable_if<is_number<To>::value || std::is_floating_point<To>::value>::type
  283. generic_convert_rational_to_float_imp(To& result, const Integer& n, const Integer& d, const std::integral_constant<bool, true>&)
  284. {
  285. //
  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).
  289. //
  290. To fd(safe_convert_to_float<To>(d));
  291. result = safe_convert_to_float<To>(n);
  292. result /= fd;
  293. }
  294. template <class To, class Integer>
  295. typename std::enable_if<is_number<To>::value || std::is_floating_point<To>::value>::type
  296. generic_convert_rational_to_float_imp(To& result, Integer& num, Integer& denom, const std::integral_constant<bool, false>&)
  297. {
  298. //
  299. // If we get here, then the precision of type To is known, and the integer type is unbounded
  300. // so we can use integer division plus manipulation of the remainder to get an exactly
  301. // rounded result.
  302. //
  303. if (num == 0)
  304. {
  305. result = 0;
  306. return;
  307. }
  308. bool s = false;
  309. if (num < 0)
  310. {
  311. s = true;
  312. num = -num;
  313. }
  314. std::ptrdiff_t denom_bits = msb(denom);
  315. std::ptrdiff_t shift = std::numeric_limits<To>::digits + denom_bits - msb(num);
  316. if (shift > 0)
  317. num <<= shift;
  318. else if (shift < 0)
  319. denom <<= boost::multiprecision::detail::unsigned_abs(shift);
  320. Integer q, r;
  321. divide_qr(num, denom, q, r);
  322. std::ptrdiff_t q_bits = msb(q);
  323. if (q_bits == std::numeric_limits<To>::digits - 1)
  324. {
  325. //
  326. // Round up if 2 * r > denom:
  327. //
  328. r <<= 1;
  329. int c = r.compare(denom);
  330. if (c > 0)
  331. ++q;
  332. else if ((c == 0) && (q & 1u))
  333. {
  334. ++q;
  335. }
  336. }
  337. else
  338. {
  339. BOOST_MP_ASSERT(q_bits == std::numeric_limits<To>::digits);
  340. //
  341. // We basically already have the rounding info:
  342. //
  343. if (q & 1u)
  344. {
  345. if (r || (q & 2u))
  346. ++q;
  347. }
  348. }
  349. using std::ldexp;
  350. result = do_cast<To>(q);
  351. result = ldexp(result, static_cast<int>(-shift));
  352. if (s)
  353. result = -result;
  354. }
  355. template <class To, class Integer>
  356. inline typename std::enable_if<!(is_number<To>::value || std::is_floating_point<To>::value)>::type
  357. generic_convert_rational_to_float_imp(To& result, Integer& num, Integer& denom, const std::integral_constant<bool, false>& tag)
  358. {
  359. number<To> t;
  360. generic_convert_rational_to_float_imp(t, num, denom, tag);
  361. result = t.backend();
  362. }
  363. template <class To, class From>
  364. inline void generic_convert_rational_to_float(To& result, const From& f)
  365. {
  366. //
  367. // Type From is always a Backend to number<>, or an
  368. // instance of number<>, but we allow
  369. // To to be either a Backend type, or a real number type,
  370. // that way we can call this from generic conversions, and
  371. // from specific conversions to built in types.
  372. //
  373. using actual_from_type = typename std::conditional<is_number<From>::value, From, number<From> >::type ;
  374. using actual_to_type = typename std::conditional<is_number<To>::value || std::is_floating_point<To>::value, To, number<To> >::type ;
  375. using integer_type = typename component_type<actual_from_type>::type ;
  376. 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)>;
  377. integer_type n(numerator(static_cast<actual_from_type>(f))), d(denominator(static_cast<actual_from_type>(f)));
  378. generic_convert_rational_to_float_imp(result, n, d, dispatch_tag());
  379. }
  380. template <class To, class From>
  381. 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*/)
  382. {
  383. generic_convert_rational_to_float(to, from);
  384. }
  385. template <class To, class From>
  386. void generic_interconvert_float2rational(To& to, const From& from, const std::integral_constant<int, 2>& /*radix*/)
  387. {
  388. using std::ldexp;
  389. using std::frexp;
  390. using ui_type = typename std::tuple_element<0, typename To::unsigned_types>::type;
  391. constexpr int shift = std::numeric_limits<long long>::digits;
  392. typename From::exponent_type e;
  393. typename component_type<number<To>>::type num, denom;
  394. number<From> val(from);
  395. val = frexp(val, &e);
  396. while (val)
  397. {
  398. val = ldexp(val, shift);
  399. e -= shift;
  400. long long ll = boost::multiprecision::detail::lltrunc(val);
  401. val -= ll;
  402. num <<= shift;
  403. num += ll;
  404. }
  405. denom = ui_type(1u);
  406. if (e < 0)
  407. denom <<= -e;
  408. else if (e > 0)
  409. num <<= e;
  410. assign_components(to, num.backend(), denom.backend());
  411. }
  412. template <class To, class From, int Radix>
  413. void generic_interconvert_float2rational(To& to, const From& from, const std::integral_constant<int, Radix>& /*radix*/)
  414. {
  415. using std::ilogb;
  416. using std::scalbn;
  417. using std::pow;
  418. using std::abs;
  419. //
  420. // This is almost the same as the binary case above, but we have to use
  421. // scalbn and ilogb rather than ldexp and frexp, we also only extract
  422. // one Radix digit at a time which is terribly inefficient!
  423. //
  424. using ui_type = typename std::tuple_element<0, typename To::unsigned_types>::type;
  425. typename From::exponent_type e;
  426. typename component_type<number<To>>::type num, denom;
  427. number<From> val(from);
  428. if (!val)
  429. {
  430. to = ui_type(0u);
  431. return;
  432. }
  433. e = ilogb(val);
  434. val = scalbn(val, -e);
  435. while (val)
  436. {
  437. long long ll = boost::multiprecision::detail::lltrunc(val);
  438. val -= ll;
  439. val = scalbn(val, 1);
  440. num *= Radix;
  441. num += ll;
  442. --e;
  443. }
  444. ++e;
  445. denom = ui_type(Radix);
  446. denom = pow(denom, abs(e));
  447. if (e > 0)
  448. {
  449. num *= denom;
  450. denom = 1;
  451. }
  452. assign_components(to, num.backend(), denom.backend());
  453. }
  454. template <class To, class From>
  455. 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*/)
  456. {
  457. generic_interconvert_float2rational(to, from, std::integral_constant<int, std::numeric_limits<number<From> >::is_specialized ? std::numeric_limits<number<From> >::radix : 2>());
  458. }
  459. template <class To, class From>
  460. 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*/)
  461. {
  462. number<From> t(from);
  463. number<To> result(numerator(t) / denominator(t));
  464. to = result.backend();
  465. }
  466. template <class To, class From>
  467. void generic_interconvert_float2int(To& to, const From& from, const std::integral_constant<int, 2>& /*radix*/)
  468. {
  469. using std::frexp;
  470. using std::ldexp;
  471. using exponent_type = typename From::exponent_type;
  472. constexpr exponent_type shift = std::numeric_limits<long long>::digits;
  473. exponent_type e;
  474. number<To> num(0u);
  475. number<From> val(from);
  476. val = frexp(val, &e);
  477. bool neg = false;
  478. if (val.sign() < 0)
  479. {
  480. val.backend().negate();
  481. neg = true;
  482. }
  483. while (e > 0)
  484. {
  485. exponent_type s = (std::min)(e, shift);
  486. val = ldexp(val, s);
  487. e -= s;
  488. long long ll = boost::multiprecision::detail::lltrunc(val);
  489. val -= ll;
  490. num <<= s;
  491. num += ll;
  492. }
  493. to = num.backend();
  494. if (neg)
  495. to.negate();
  496. }
  497. template <class To, class From, int Radix>
  498. void generic_interconvert_float2int(To& to, const From& from, const std::integral_constant<int, Radix>& /*radix*/)
  499. {
  500. using std::ilogb;
  501. using std::scalbn;
  502. //
  503. // This is almost the same as the binary case above, but we have to use
  504. // scalbn and ilogb rather than ldexp and frexp, we also only extract
  505. // one Radix digit at a time which is terribly inefficient!
  506. //
  507. typename From::exponent_type e;
  508. number<To> num(0u);
  509. number<From> val(from);
  510. e = ilogb(val);
  511. val = scalbn(val, -e);
  512. while (e >= 0)
  513. {
  514. long long ll = boost::multiprecision::detail::lltrunc(val);
  515. val -= ll;
  516. val = scalbn(val, 1);
  517. num *= Radix;
  518. num += ll;
  519. --e;
  520. }
  521. to = num.backend();
  522. }
  523. template <class To, class From>
  524. 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*/)
  525. {
  526. generic_interconvert_float2int(to, from, std::integral_constant<int, (std::numeric_limits<number<From> >::is_specialized ? std::numeric_limits<number<From> >::radix : 2)>());
  527. }
  528. template <class To, class From, class tag>
  529. void generic_interconvert_complex_to_scalar(To& to, const From& from, const std::integral_constant<bool, true>&, const tag&)
  530. {
  531. // We just want the real part, and "to" is the correct type already:
  532. eval_real(to, from);
  533. To im;
  534. eval_imag(im, from);
  535. if (!eval_is_zero(im))
  536. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Could not convert imaginary number to scalar."));
  537. }
  538. template <class To, class From>
  539. void generic_interconvert_complex_to_scalar(To& to, const From& from, const std::integral_constant<bool, false>&, const std::integral_constant<bool, true>&)
  540. {
  541. using component_number = typename component_type<number<From> >::type;
  542. using component_backend = typename component_number::backend_type ;
  543. //
  544. // Get the real part and copy-construct the result from it:
  545. //
  546. scoped_precision_options<component_number> scope(from);
  547. component_backend r;
  548. generic_interconvert_complex_to_scalar(r, from, std::integral_constant<bool, true>(), std::integral_constant<bool, true>());
  549. to = r;
  550. }
  551. template <class To, class From>
  552. void generic_interconvert_complex_to_scalar(To& to, const From& from, const std::integral_constant<bool, false>&, const std::integral_constant<bool, false>&)
  553. {
  554. using component_number = typename component_type<number<From> >::type;
  555. using component_backend = typename component_number::backend_type;
  556. //
  557. // Get the real part and use a generic_interconvert to type To:
  558. //
  559. scoped_precision_options<component_number> scope(from);
  560. component_backend r;
  561. generic_interconvert_complex_to_scalar(r, from, std::integral_constant<bool, true>(), std::integral_constant<bool, true>());
  562. generic_interconvert(to, r, std::integral_constant<int, number_category<To>::value>(), std::integral_constant<int, number_category<component_backend>::value>());
  563. }
  564. template <class To, class From>
  565. 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*/)
  566. {
  567. using component_number = typename component_type<number<From> >::type;
  568. using component_backend = typename component_number::backend_type ;
  569. 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>());
  570. }
  571. template <class To, class From>
  572. 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*/)
  573. {
  574. using component_number = typename component_type<number<From> >::type;
  575. using component_backend = typename component_number::backend_type ;
  576. 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>());
  577. }
  578. template <class To, class From>
  579. 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*/)
  580. {
  581. using component_number = typename component_type<number<From> >::type;
  582. using component_backend = typename component_number::backend_type ;
  583. 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>());
  584. }
  585. template <class To, class From>
  586. 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*/)
  587. {
  588. using component_number = typename component_type<number<To> >::type;
  589. scoped_source_precision<number<From> > scope1;
  590. scoped_precision_options<component_number> scope2(number<To>::thread_default_precision(), number<To>::thread_default_variable_precision_options());
  591. (void)scope1;
  592. (void)scope2;
  593. number<From> f(from);
  594. component_number scalar(f);
  595. number<To> result(scalar);
  596. to = result.backend();
  597. }
  598. template <class To, class From>
  599. 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*/)
  600. {
  601. using component_number = typename component_type<number<To> >::type;
  602. scoped_source_precision<number<From> > scope1;
  603. scoped_precision_options<component_number> scope2(number<To>::thread_default_precision(), number<To>::thread_default_variable_precision_options());
  604. (void)scope1;
  605. (void)scope2;
  606. number<From> f(from);
  607. component_number scalar(f);
  608. number<To> result(scalar);
  609. to = result.backend();
  610. }
  611. template <class To, class From>
  612. 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*/)
  613. {
  614. using component_number = typename component_type<number<To> >::type;
  615. scoped_source_precision<number<From> > scope1;
  616. scoped_precision_options<component_number> scope2(number<To>::thread_default_precision(), number<To>::thread_default_variable_precision_options());
  617. (void)scope1;
  618. (void)scope2;
  619. number<From> f(from);
  620. component_number scalar(f);
  621. number<To> result(scalar);
  622. to = result.backend();
  623. }
  624. template <class To, class From, int Tag1, int Tag2>
  625. void generic_interconvert(To& /*to*/, const From& /*from*/, const std::integral_constant<int, Tag1>& /*to_type*/, const std::integral_constant<int, Tag2>& /*from_type*/)
  626. {
  627. static_assert(sizeof(To) == 0, "Sorry, you asked for a conversion bewteen types that hasn't been implemented yet!!");
  628. }
  629. }
  630. }
  631. } // namespace boost::multiprecision::detail
  632. #ifdef BOOST_MSVC
  633. #pragma warning(pop)
  634. #endif
  635. #endif // BOOST_MP_GENERIC_INTERCONVERT_HPP