mpc.hpp 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright 2018 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_MPC_HPP
  6. #define BOOST_MP_MPC_HPP
  7. #include <cstdint>
  8. #include <boost/multiprecision/detail/standalone_config.hpp>
  9. #include <boost/multiprecision/detail/fpclassify.hpp>
  10. #include <boost/multiprecision/number.hpp>
  11. #include <boost/multiprecision/detail/digits.hpp>
  12. #include <boost/multiprecision/detail/atomic.hpp>
  13. #include <boost/multiprecision/traits/is_variable_precision.hpp>
  14. #include <boost/multiprecision/mpfr.hpp>
  15. #include <boost/multiprecision/detail/hash.hpp>
  16. #include <boost/multiprecision/detail/no_exceptions_support.hpp>
  17. #include <boost/multiprecision/detail/assert.hpp>
  18. #include <mpc.h>
  19. #include <cmath>
  20. #include <algorithm>
  21. #include <complex>
  22. #ifndef BOOST_MULTIPRECISION_MPFI_DEFAULT_PRECISION
  23. #define BOOST_MULTIPRECISION_MPFI_DEFAULT_PRECISION 20
  24. #endif
  25. namespace boost {
  26. namespace multiprecision {
  27. template <unsigned digits10>
  28. struct number_category<backends::mpc_complex_backend<digits10> > : public std::integral_constant<int, number_kind_complex>
  29. {};
  30. namespace backends {
  31. namespace detail {
  32. inline void mpc_copy_precision(mpc_t dest, const mpc_t src)
  33. {
  34. mpfr_prec_t p_dest = mpc_get_prec(dest);
  35. mpfr_prec_t p_src = mpc_get_prec(src);
  36. if (p_dest != p_src)
  37. mpc_set_prec(dest, p_src);
  38. }
  39. inline void mpc_copy_precision(mpc_t dest, const mpc_t src1, const mpc_t src2)
  40. {
  41. mpfr_prec_t p_dest = mpc_get_prec(dest);
  42. mpfr_prec_t p_src1 = mpc_get_prec(src1);
  43. mpfr_prec_t p_src2 = mpc_get_prec(src2);
  44. if (p_src2 > p_src1)
  45. p_src1 = p_src2;
  46. if (p_dest != p_src1)
  47. mpc_set_prec(dest, p_src1);
  48. }
  49. template <unsigned digits10>
  50. struct mpc_complex_imp
  51. {
  52. #ifdef BOOST_HAS_LONG_LONG
  53. using signed_types = std::tuple<long, long long> ;
  54. using unsigned_types = std::tuple<unsigned long, unsigned long long>;
  55. #else
  56. using signed_types = std::tuple<long> ;
  57. using unsigned_types = std::tuple<unsigned long>;
  58. #endif
  59. using float_types = std::tuple<double, long double>;
  60. using exponent_type = long ;
  61. mpc_complex_imp()
  62. {
  63. mpc_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  64. mpc_set_ui(m_data, 0u, GMP_RNDN);
  65. }
  66. mpc_complex_imp(unsigned digits2)
  67. {
  68. mpc_init2(m_data, digits2);
  69. mpc_set_ui(m_data, 0u, GMP_RNDN);
  70. }
  71. mpc_complex_imp(const mpc_complex_imp& o)
  72. {
  73. mpc_init2(m_data, preserve_source_precision() ? mpc_get_prec(o.data()) : boost::multiprecision::detail::digits10_2_2(get_default_precision()));
  74. if (o.m_data[0].re[0]._mpfr_d)
  75. mpc_set(m_data, o.m_data, GMP_RNDN);
  76. }
  77. // rvalue copy
  78. mpc_complex_imp(mpc_complex_imp&& o) noexcept
  79. {
  80. mpfr_prec_t binary_default_precision = boost::multiprecision::detail::digits10_2_2(get_default_precision());
  81. if ((this->get_default_options() != variable_precision_options::preserve_target_precision) || (mpc_get_prec(o.data()) == binary_default_precision))
  82. {
  83. m_data[0] = o.m_data[0];
  84. o.m_data[0].re[0]._mpfr_d = nullptr;
  85. }
  86. else
  87. {
  88. // NOTE: C allocation interface must not throw:
  89. mpc_init2(m_data, binary_default_precision);
  90. if (o.m_data[0].re[0]._mpfr_d)
  91. mpc_set(m_data, o.m_data, GMP_RNDN);
  92. }
  93. }
  94. mpc_complex_imp& operator=(const mpc_complex_imp& o)
  95. {
  96. if ((o.m_data[0].re[0]._mpfr_d) && (this != &o))
  97. {
  98. if (m_data[0].re[0]._mpfr_d == nullptr)
  99. mpc_init2(m_data, preserve_source_precision() ? mpc_get_prec(o.m_data) : boost::multiprecision::detail::digits10_2_2(get_default_precision()));
  100. else if (preserve_source_precision() && (mpc_get_prec(o.data()) != mpc_get_prec(data())))
  101. {
  102. mpc_set_prec(m_data, mpc_get_prec(o.m_data));
  103. }
  104. mpc_set(m_data, o.m_data, GMP_RNDN);
  105. }
  106. return *this;
  107. }
  108. // rvalue assign
  109. mpc_complex_imp& operator=(mpc_complex_imp&& o) noexcept
  110. {
  111. if ((this->get_default_options() != variable_precision_options::preserve_target_precision) || (mpc_get_prec(o.data()) == mpc_get_prec(data())))
  112. mpc_swap(m_data, o.m_data);
  113. else
  114. *this = static_cast<const mpc_complex_imp&>(o);
  115. return *this;
  116. }
  117. #ifdef BOOST_HAS_LONG_LONG
  118. #ifdef _MPFR_H_HAVE_INTMAX_T
  119. mpc_complex_imp& operator=(unsigned long long i)
  120. {
  121. if (m_data[0].re[0]._mpfr_d == nullptr)
  122. mpc_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  123. mpc_set_uj(data(), i, GMP_RNDN);
  124. return *this;
  125. }
  126. mpc_complex_imp& operator=(long long i)
  127. {
  128. if (m_data[0].re[0]._mpfr_d == nullptr)
  129. mpc_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  130. mpc_set_sj(data(), i, GMP_RNDN);
  131. return *this;
  132. }
  133. #else
  134. mpc_complex_imp& operator=(unsigned long long i)
  135. {
  136. mpfr_float_backend<digits10> f(0uL, mpc_get_prec(m_data));
  137. f = i;
  138. mpc_set_fr(this->data(), f.data(), GMP_RNDN);
  139. return *this;
  140. }
  141. mpc_complex_imp& operator=(long long i)
  142. {
  143. mpfr_float_backend<digits10> f(0uL, mpc_get_prec(m_data));
  144. f = i;
  145. mpc_set_fr(this->data(), f.data(), GMP_RNDN);
  146. return *this;
  147. }
  148. #endif
  149. #endif
  150. mpc_complex_imp& operator=(unsigned long i)
  151. {
  152. if (m_data[0].re[0]._mpfr_d == nullptr)
  153. mpc_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  154. mpc_set_ui(m_data, i, GMP_RNDN);
  155. return *this;
  156. }
  157. mpc_complex_imp& operator=(long i)
  158. {
  159. if (m_data[0].re[0]._mpfr_d == nullptr)
  160. mpc_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  161. mpc_set_si(m_data, i, GMP_RNDN);
  162. return *this;
  163. }
  164. mpc_complex_imp& operator=(double d)
  165. {
  166. if (m_data[0].re[0]._mpfr_d == nullptr)
  167. mpc_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  168. mpc_set_d(m_data, d, GMP_RNDN);
  169. return *this;
  170. }
  171. mpc_complex_imp& operator=(long double d)
  172. {
  173. if (m_data[0].re[0]._mpfr_d == nullptr)
  174. mpc_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  175. mpc_set_ld(m_data, d, GMP_RNDN);
  176. return *this;
  177. }
  178. mpc_complex_imp& operator=(mpz_t i)
  179. {
  180. if (m_data[0].re[0]._mpfr_d == nullptr)
  181. mpc_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  182. mpc_set_z(m_data, i, GMP_RNDN);
  183. return *this;
  184. }
  185. mpc_complex_imp& operator=(gmp_int i)
  186. {
  187. if (m_data[0].re[0]._mpfr_d == nullptr)
  188. mpc_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  189. mpc_set_z(m_data, i.data(), GMP_RNDN);
  190. return *this;
  191. }
  192. #ifdef BOOST_HAS_INT128
  193. mpc_complex_imp& operator=(int128_type val)
  194. {
  195. gmp_int i;
  196. i = val;
  197. return *this = i.data();
  198. }
  199. mpc_complex_imp& operator=(uint128_type val)
  200. {
  201. gmp_int i;
  202. i = val;
  203. return *this = i.data();
  204. }
  205. #endif
  206. #ifdef BOOST_HAS_FLOAT128
  207. mpc_complex_imp& operator=(float128_type val)
  208. {
  209. mpfr_float_backend<digits10> f;
  210. f = val;
  211. mpc_set_fr(this->m_data, f.data(), GMP_RNDN);
  212. return *this;
  213. }
  214. #endif
  215. mpc_complex_imp& operator=(const char* s)
  216. {
  217. using default_ops::eval_fpclassify;
  218. if (m_data[0].re[0]._mpfr_d == nullptr)
  219. mpc_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  220. mpfr_float_backend<digits10> a(0uL, mpc_get_prec(m_data)), b(0uL, mpc_get_prec(m_data));
  221. if (s && (*s == '('))
  222. {
  223. std::string part;
  224. const char* p = ++s;
  225. while (*p && (*p != ',') && (*p != ')'))
  226. ++p;
  227. part.assign(s, p);
  228. if (part.size())
  229. a = part.c_str();
  230. else
  231. a = 0uL;
  232. s = p;
  233. if (*p && (*p != ')'))
  234. {
  235. ++p;
  236. while (*p && (*p != ')'))
  237. ++p;
  238. part.assign(s + 1, p);
  239. }
  240. else
  241. part.erase();
  242. if (part.size())
  243. b = part.c_str();
  244. else
  245. b = 0uL;
  246. }
  247. else
  248. {
  249. a = s;
  250. b = 0uL;
  251. }
  252. if (eval_fpclassify(a) == static_cast<int>(FP_NAN))
  253. {
  254. mpc_set_fr(this->data(), a.data(), GMP_RNDN);
  255. }
  256. else if (eval_fpclassify(b) == static_cast<int>(FP_NAN))
  257. {
  258. mpc_set_fr(this->data(), b.data(), GMP_RNDN);
  259. }
  260. else
  261. {
  262. mpc_set_fr_fr(m_data, a.data(), b.data(), GMP_RNDN);
  263. }
  264. return *this;
  265. }
  266. void swap(mpc_complex_imp& o) noexcept
  267. {
  268. mpc_swap(m_data, o.m_data);
  269. }
  270. std::string str(std::streamsize digits, std::ios_base::fmtflags f) const
  271. {
  272. BOOST_MP_ASSERT(m_data[0].re[0]._mpfr_d);
  273. mpfr_float_backend<digits10> a(0uL, mpc_get_prec(m_data)), b(0uL, mpc_get_prec(m_data));
  274. mpc_real(a.data(), m_data, GMP_RNDN);
  275. mpc_imag(b.data(), m_data, GMP_RNDN);
  276. if (eval_is_zero(b))
  277. return a.str(digits, f);
  278. return "(" + a.str(digits, f) + "," + b.str(digits, f) + ")";
  279. }
  280. ~mpc_complex_imp() noexcept
  281. {
  282. if (m_data[0].re[0]._mpfr_d)
  283. mpc_clear(m_data);
  284. }
  285. void negate() noexcept
  286. {
  287. BOOST_MP_ASSERT(m_data[0].re[0]._mpfr_d);
  288. mpc_neg(m_data, m_data, GMP_RNDN);
  289. }
  290. int compare(const mpc_complex_imp& o) const noexcept
  291. {
  292. BOOST_MP_ASSERT(m_data[0].re[0]._mpfr_d && o.m_data[0].re[0]._mpfr_d);
  293. return mpc_cmp(m_data, o.m_data);
  294. }
  295. int compare(const mpc_complex_backend<digits10>& o) const noexcept
  296. {
  297. BOOST_MP_ASSERT(m_data[0].re[0]._mpfr_d && o.m_data[0].re[0]._mpfr_d);
  298. return mpc_cmp(m_data, o.data());
  299. }
  300. int compare(long int i) const noexcept
  301. {
  302. BOOST_MP_ASSERT(m_data[0].re[0]._mpfr_d);
  303. return mpc_cmp_si(m_data, i);
  304. }
  305. int compare(unsigned long int i) const noexcept
  306. {
  307. BOOST_MP_ASSERT(m_data[0].re[0]._mpfr_d);
  308. constexpr const unsigned long int max_val = (std::numeric_limits<long>::max)();
  309. if (i > max_val)
  310. {
  311. mpc_complex_imp d(mpc_get_prec(m_data));
  312. d = i;
  313. return compare(d);
  314. }
  315. return mpc_cmp_si(m_data, static_cast<long>(i));
  316. }
  317. template <class V>
  318. int compare(const V& v) const noexcept
  319. {
  320. mpc_complex_imp d(mpc_get_prec(m_data));
  321. d = v;
  322. return compare(d);
  323. }
  324. mpc_t& data() noexcept
  325. {
  326. BOOST_MP_ASSERT(m_data[0].re[0]._mpfr_d);
  327. return m_data;
  328. }
  329. const mpc_t& data() const noexcept
  330. {
  331. BOOST_MP_ASSERT(m_data[0].re[0]._mpfr_d);
  332. return m_data;
  333. }
  334. protected:
  335. mpc_t m_data;
  336. static boost::multiprecision::detail::precision_type& get_global_default_precision() noexcept
  337. {
  338. static boost::multiprecision::detail::precision_type val(BOOST_MULTIPRECISION_MPFI_DEFAULT_PRECISION);
  339. return val;
  340. }
  341. static unsigned& get_default_precision() noexcept
  342. {
  343. static BOOST_MP_THREAD_LOCAL unsigned val(get_global_default_precision());
  344. return val;
  345. }
  346. #ifndef BOOST_MT_NO_ATOMIC_INT
  347. static std::atomic<variable_precision_options>& get_global_default_options() noexcept
  348. #else
  349. static variable_precision_options& get_global_default_options() noexcept
  350. #endif
  351. {
  352. #ifndef BOOST_MT_NO_ATOMIC_INT
  353. static std::atomic<variable_precision_options> val{variable_precision_options::preserve_related_precision};
  354. #else
  355. static variable_precision_options val{variable_precision_options::preserve_related_precision};
  356. #endif
  357. return val;
  358. }
  359. static variable_precision_options& get_default_options() noexcept
  360. {
  361. static BOOST_MP_THREAD_LOCAL variable_precision_options val(get_global_default_options());
  362. return val;
  363. }
  364. static bool preserve_source_precision() noexcept
  365. {
  366. return get_default_options() >= variable_precision_options::preserve_source_precision;
  367. }
  368. static bool preserve_component_precision() noexcept
  369. {
  370. return get_default_options() >= variable_precision_options::preserve_component_precision;
  371. }
  372. static bool preserve_related_precision() noexcept
  373. {
  374. return get_default_options() >= variable_precision_options::preserve_related_precision;
  375. }
  376. static bool preserve_all_precision() noexcept
  377. {
  378. return get_default_options() >= variable_precision_options::preserve_all_precision;
  379. }
  380. };
  381. } // namespace detail
  382. template <unsigned digits10>
  383. struct mpc_complex_backend : public detail::mpc_complex_imp<digits10>
  384. {
  385. mpc_complex_backend() : detail::mpc_complex_imp<digits10>() {}
  386. mpc_complex_backend(const mpc_complex_backend& o) : detail::mpc_complex_imp<digits10>(o) {}
  387. // rvalue copy
  388. mpc_complex_backend(mpc_complex_backend&& o) : detail::mpc_complex_imp<digits10>(static_cast<detail::mpc_complex_imp<digits10>&&>(o))
  389. {}
  390. template <unsigned D>
  391. mpc_complex_backend(const mpc_complex_backend<D>& val, typename std::enable_if<D <= digits10>::type* = nullptr)
  392. : detail::mpc_complex_imp<digits10>()
  393. {
  394. mpc_set(this->m_data, val.data(), GMP_RNDN);
  395. }
  396. template <unsigned D>
  397. explicit mpc_complex_backend(const mpc_complex_backend<D>& val, typename std::enable_if<!(D <= digits10)>::type* = nullptr)
  398. : detail::mpc_complex_imp<digits10>()
  399. {
  400. mpc_set(this->m_data, val.data(), GMP_RNDN);
  401. }
  402. mpc_complex_backend(const mpc_t val)
  403. : detail::mpc_complex_imp<digits10>()
  404. {
  405. mpc_set(this->m_data, val, GMP_RNDN);
  406. }
  407. mpc_complex_backend(const std::complex<float>& val)
  408. : detail::mpc_complex_imp<digits10>()
  409. {
  410. mpc_set_d_d(this->m_data, val.real(), val.imag(), GMP_RNDN);
  411. }
  412. mpc_complex_backend(const std::complex<double>& val)
  413. : detail::mpc_complex_imp<digits10>()
  414. {
  415. mpc_set_d_d(this->m_data, val.real(), val.imag(), GMP_RNDN);
  416. }
  417. mpc_complex_backend(const std::complex<long double>& val)
  418. : detail::mpc_complex_imp<digits10>()
  419. {
  420. mpc_set_ld_ld(this->m_data, val.real(), val.imag(), GMP_RNDN);
  421. }
  422. mpc_complex_backend(mpz_srcptr val) : detail::mpc_complex_imp<digits10>()
  423. {
  424. mpc_set_z(this->m_data, val, GMP_RNDN);
  425. }
  426. mpc_complex_backend& operator=(mpz_srcptr val)
  427. {
  428. mpc_set_z(this->m_data, val, GMP_RNDN);
  429. return *this;
  430. }
  431. mpc_complex_backend(gmp_int const& val) : detail::mpc_complex_imp<digits10>()
  432. {
  433. mpc_set_z(this->m_data, val.data(), GMP_RNDN);
  434. }
  435. mpc_complex_backend& operator=(gmp_int const& val)
  436. {
  437. mpc_set_z(this->m_data, val.data(), GMP_RNDN);
  438. return *this;
  439. }
  440. mpc_complex_backend(mpf_srcptr val) : detail::mpc_complex_imp<digits10>()
  441. {
  442. mpc_set_f(this->m_data, val, GMP_RNDN);
  443. }
  444. mpc_complex_backend& operator=(mpf_srcptr val)
  445. {
  446. mpc_set_f(this->m_data, val, GMP_RNDN);
  447. return *this;
  448. }
  449. template <unsigned D10>
  450. mpc_complex_backend(gmp_float<D10> const& val) : detail::mpc_complex_imp<digits10>()
  451. {
  452. mpc_set_f(this->m_data, val.data(), GMP_RNDN);
  453. }
  454. template <unsigned D10>
  455. mpc_complex_backend& operator=(gmp_float<D10> const& val)
  456. {
  457. mpc_set_f(this->m_data, val.data(), GMP_RNDN);
  458. return *this;
  459. }
  460. mpc_complex_backend(mpq_srcptr val) : detail::mpc_complex_imp<digits10>()
  461. {
  462. mpc_set_q(this->m_data, val, GMP_RNDN);
  463. }
  464. mpc_complex_backend& operator=(mpq_srcptr val)
  465. {
  466. mpc_set_q(this->m_data, val, GMP_RNDN);
  467. return *this;
  468. }
  469. mpc_complex_backend(gmp_rational const& val) : detail::mpc_complex_imp<digits10>()
  470. {
  471. mpc_set_q(this->m_data, val.data(), GMP_RNDN);
  472. }
  473. mpc_complex_backend& operator=(gmp_rational const& val)
  474. {
  475. mpc_set_q(this->m_data, val.data(), GMP_RNDN);
  476. return *this;
  477. }
  478. mpc_complex_backend(mpfr_srcptr val) : detail::mpc_complex_imp<digits10>()
  479. {
  480. mpc_set_fr(this->m_data, val, GMP_RNDN);
  481. }
  482. mpc_complex_backend& operator=(mpfr_srcptr val)
  483. {
  484. mpc_set_fr(this->m_data, val, GMP_RNDN);
  485. return *this;
  486. }
  487. template <unsigned D10, mpfr_allocation_type AllocationType>
  488. mpc_complex_backend(mpfr_float_backend<D10, AllocationType> const& val, typename std::enable_if<D10 <= digits10>::type* = nullptr) : detail::mpc_complex_imp<digits10>()
  489. {
  490. mpc_set_fr(this->m_data, val.data(), GMP_RNDN);
  491. }
  492. template <unsigned D10, mpfr_allocation_type AllocationType>
  493. explicit mpc_complex_backend(mpfr_float_backend<D10, AllocationType> const& val, typename std::enable_if<!(D10 <= digits10)>::type* = nullptr) : detail::mpc_complex_imp<digits10>()
  494. {
  495. mpc_set_fr(this->m_data, val.data(), GMP_RNDN);
  496. }
  497. template <unsigned D10, mpfr_allocation_type AllocationType>
  498. mpc_complex_backend& operator=(mpfr_float_backend<D10, AllocationType> const& val)
  499. {
  500. mpc_set_fr(this->m_data, val.data(), GMP_RNDN);
  501. return *this;
  502. }
  503. mpc_complex_backend& operator=(const mpc_complex_backend& o)
  504. {
  505. *static_cast<detail::mpc_complex_imp<digits10>*>(this) = static_cast<detail::mpc_complex_imp<digits10> const&>(o);
  506. return *this;
  507. }
  508. // rvalue assign
  509. mpc_complex_backend& operator=(mpc_complex_backend&& o) noexcept
  510. {
  511. *static_cast<detail::mpc_complex_imp<digits10>*>(this) = static_cast<detail::mpc_complex_imp<digits10>&&>(o);
  512. return *this;
  513. }
  514. template <class V>
  515. typename std::enable_if<std::is_assignable<detail::mpc_complex_imp<digits10>, V>::value, mpc_complex_backend&>::type operator=(const V& v)
  516. {
  517. *static_cast<detail::mpc_complex_imp<digits10>*>(this) = v;
  518. return *this;
  519. }
  520. mpc_complex_backend& operator=(const mpc_t val)
  521. {
  522. mpc_set(this->m_data, val, GMP_RNDN);
  523. return *this;
  524. }
  525. mpc_complex_backend& operator=(const std::complex<float>& val)
  526. {
  527. mpc_set_d_d(this->m_data, val.real(), val.imag(), GMP_RNDN);
  528. return *this;
  529. }
  530. mpc_complex_backend& operator=(const std::complex<double>& val)
  531. {
  532. mpc_set_d_d(this->m_data, val.real(), val.imag(), GMP_RNDN);
  533. return *this;
  534. }
  535. mpc_complex_backend& operator=(const std::complex<long double>& val)
  536. {
  537. mpc_set_ld_ld(this->m_data, val.real(), val.imag(), GMP_RNDN);
  538. return *this;
  539. }
  540. // We don't change our precision here, this is a fixed precision type:
  541. template <unsigned D>
  542. mpc_complex_backend& operator=(const mpc_complex_backend<D>& val)
  543. {
  544. mpc_set(this->m_data, val.data(), GMP_RNDN);
  545. return *this;
  546. }
  547. };
  548. template <>
  549. struct mpc_complex_backend<0> : public detail::mpc_complex_imp<0>
  550. {
  551. mpc_complex_backend() : detail::mpc_complex_imp<0>() {}
  552. mpc_complex_backend(const mpc_t val)
  553. : detail::mpc_complex_imp<0>(mpc_get_prec(val))
  554. {
  555. mpc_set(this->m_data, val, GMP_RNDN);
  556. }
  557. mpc_complex_backend(const mpc_complex_backend& o) : detail::mpc_complex_imp<0>(o) {}
  558. // rvalue copy
  559. mpc_complex_backend(mpc_complex_backend&& o) noexcept : detail::mpc_complex_imp<0>(static_cast<detail::mpc_complex_imp<0>&&>(o))
  560. {}
  561. mpc_complex_backend(const mpc_complex_backend& o, unsigned digits10)
  562. : detail::mpc_complex_imp<0>(multiprecision::detail::digits10_2_2(digits10))
  563. {
  564. mpc_set(this->m_data, o.data(), GMP_RNDN);
  565. }
  566. template <unsigned D>
  567. mpc_complex_backend(const mpc_complex_backend<D>& val)
  568. : detail::mpc_complex_imp<0>(preserve_related_precision() ? mpc_get_prec(val.data()) : multiprecision::detail::digits10_2_2(get_default_precision()))
  569. {
  570. mpc_set(this->m_data, val.data(), GMP_RNDN);
  571. }
  572. template <unsigned D>
  573. mpc_complex_backend(const mpfr_float_backend<D>& val)
  574. : detail::mpc_complex_imp<0>((D == 0 ? this->preserve_component_precision() : this->preserve_related_precision()) ? mpfr_get_prec(val.data()) : multiprecision::detail::digits10_2_2(this->get_default_precision()))
  575. {
  576. mpc_set_fr(this->m_data, val.data(), GMP_RNDN);
  577. }
  578. mpc_complex_backend(mpz_srcptr val) : detail::mpc_complex_imp<0>()
  579. {
  580. mpc_set_z(this->m_data, val, GMP_RNDN);
  581. }
  582. mpc_complex_backend& operator=(mpz_srcptr val)
  583. {
  584. mpc_set_z(this->m_data, val, GMP_RNDN);
  585. return *this;
  586. }
  587. mpc_complex_backend(gmp_int const& val) : detail::mpc_complex_imp<0>(preserve_all_precision() ? used_gmp_int_bits(val) : boost::multiprecision::detail::digits10_2_2(thread_default_precision()))
  588. {
  589. mpc_set_z(this->m_data, val.data(), GMP_RNDN);
  590. }
  591. mpc_complex_backend& operator=(gmp_int const& val)
  592. {
  593. if (this->m_data[0].im->_mpfr_d == nullptr)
  594. {
  595. unsigned requested_precision = this->thread_default_precision();
  596. if (thread_default_variable_precision_options() >= variable_precision_options::preserve_all_precision)
  597. {
  598. unsigned d2 = used_gmp_int_bits(val);
  599. unsigned d10 = 1 + multiprecision::detail::digits2_2_10(d2);
  600. if (d10 > requested_precision)
  601. requested_precision = d10;
  602. }
  603. mpc_init2(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
  604. }
  605. else if (thread_default_variable_precision_options() >= variable_precision_options::preserve_all_precision)
  606. {
  607. unsigned requested_precision = this->thread_default_precision();
  608. unsigned d2 = used_gmp_int_bits(val);
  609. unsigned d10 = 1 + multiprecision::detail::digits2_2_10(d2);
  610. if (d10 > requested_precision)
  611. this->precision(d10);
  612. }
  613. mpc_set_z(this->m_data, val.data(), GMP_RNDN);
  614. return *this;
  615. }
  616. mpc_complex_backend(mpf_srcptr val) : detail::mpc_complex_imp<0>((unsigned)mpf_get_prec(val))
  617. {
  618. mpc_set_f(this->m_data, val, GMP_RNDN);
  619. }
  620. mpc_complex_backend& operator=(mpf_srcptr val)
  621. {
  622. if ((mp_bitcnt_t)mpc_get_prec(data()) != mpf_get_prec(val))
  623. {
  624. mpc_complex_backend t(val);
  625. t.swap(*this);
  626. }
  627. else
  628. mpc_set_f(this->m_data, val, GMP_RNDN);
  629. return *this;
  630. }
  631. template <unsigned digits10>
  632. mpc_complex_backend(gmp_float<digits10> const& val) : detail::mpc_complex_imp<0>(preserve_all_precision() ? (unsigned)mpf_get_prec(val.data()) : multiprecision::detail::digits10_2_2(get_default_precision()))
  633. {
  634. mpc_set_f(this->m_data, val.data(), GMP_RNDN);
  635. }
  636. template <unsigned digits10>
  637. mpc_complex_backend& operator=(gmp_float<digits10> const& val)
  638. {
  639. if (preserve_all_precision() && (mpc_get_prec(data()) != (mpfr_prec_t)mpf_get_prec(val.data())))
  640. {
  641. mpc_complex_backend t(val);
  642. t.swap(*this);
  643. }
  644. else
  645. mpc_set_f(this->m_data, val.data(), GMP_RNDN);
  646. return *this;
  647. }
  648. mpc_complex_backend(mpq_srcptr val) : detail::mpc_complex_imp<0>()
  649. {
  650. mpc_set_q(this->m_data, val, GMP_RNDN);
  651. }
  652. mpc_complex_backend& operator=(mpq_srcptr val)
  653. {
  654. mpc_set_q(this->m_data, val, GMP_RNDN);
  655. return *this;
  656. }
  657. mpc_complex_backend(gmp_rational const& val) : detail::mpc_complex_imp<0>(preserve_all_precision() ? used_gmp_rational_bits(val) : boost::multiprecision::detail::digits10_2_2(thread_default_precision()))
  658. {
  659. mpc_set_q(this->m_data, val.data(), GMP_RNDN);
  660. }
  661. mpc_complex_backend& operator=(gmp_rational const& val)
  662. {
  663. if (this->m_data[0].im->_mpfr_d == nullptr)
  664. {
  665. unsigned requested_precision = this->get_default_precision();
  666. if (thread_default_variable_precision_options() >= variable_precision_options::preserve_all_precision)
  667. {
  668. unsigned d10 = 1 + multiprecision::detail::digits2_2_10(used_gmp_rational_bits(val));
  669. if (d10 > requested_precision)
  670. requested_precision = d10;
  671. }
  672. mpc_init2(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
  673. }
  674. else if (thread_default_variable_precision_options() >= variable_precision_options::preserve_all_precision)
  675. {
  676. unsigned requested_precision = this->get_default_precision();
  677. unsigned d10 = 1 + multiprecision::detail::digits2_2_10(used_gmp_rational_bits(val));
  678. if (d10 > requested_precision)
  679. this->precision(d10);
  680. }
  681. mpc_set_q(this->m_data, val.data(), GMP_RNDN);
  682. return *this;
  683. }
  684. mpc_complex_backend(mpfr_srcptr val) : detail::mpc_complex_imp<0>(mpfr_get_prec(val))
  685. {
  686. mpc_set_fr(this->m_data, val, GMP_RNDN);
  687. }
  688. mpc_complex_backend& operator=(mpfr_srcptr val)
  689. {
  690. if (mpc_get_prec(data()) != mpfr_get_prec(val))
  691. {
  692. mpc_complex_backend t(val);
  693. t.swap(*this);
  694. }
  695. else
  696. mpc_set_fr(this->m_data, val, GMP_RNDN);
  697. return *this;
  698. }
  699. mpc_complex_backend(const std::complex<float>& val)
  700. : detail::mpc_complex_imp<0>()
  701. {
  702. mpc_set_d_d(this->m_data, val.real(), val.imag(), GMP_RNDN);
  703. }
  704. mpc_complex_backend(const std::complex<double>& val)
  705. : detail::mpc_complex_imp<0>()
  706. {
  707. mpc_set_d_d(this->m_data, val.real(), val.imag(), GMP_RNDN);
  708. }
  709. mpc_complex_backend(const std::complex<long double>& val)
  710. : detail::mpc_complex_imp<0>()
  711. {
  712. mpc_set_ld_ld(this->m_data, val.real(), val.imag(), GMP_RNDN);
  713. }
  714. // Construction with precision:
  715. template <class T, class U>
  716. mpc_complex_backend(const T& a, const U& b, unsigned digits10)
  717. : detail::mpc_complex_imp<0>(multiprecision::detail::digits10_2_2(digits10))
  718. {
  719. // We can't use assign_components here because it copies the precision of
  720. // a and b, not digits10....
  721. boost::multiprecision::detail::scoped_precision_options<mpfr_float> scoped(*this);
  722. (void)scoped;
  723. mpfr_float ca(a), cb(b);
  724. mpc_set_fr_fr(this->data(), ca.backend().data(), cb.backend().data(), GMP_RNDN);
  725. }
  726. template <unsigned N>
  727. mpc_complex_backend(const mpfr_float_backend<N>& a, const mpfr_float_backend<N>& b, unsigned digits10)
  728. : detail::mpc_complex_imp<0>(multiprecision::detail::digits10_2_2(digits10))
  729. {
  730. mpc_set_fr_fr(this->data(), a.data(), b.data(), GMP_RNDN);
  731. }
  732. mpc_complex_backend& operator=(const mpc_complex_backend& o) = default;
  733. // rvalue assign
  734. mpc_complex_backend& operator=(mpc_complex_backend&& o) noexcept = default;
  735. template <class V>
  736. mpc_complex_backend& operator=(const V& v)
  737. {
  738. constexpr unsigned d10 = std::is_floating_point<V>::value ?
  739. std::numeric_limits<V>::digits10 :
  740. std::numeric_limits<V>::digits10 ? 1 + std::numeric_limits<V>::digits10 :
  741. 1 + boost::multiprecision::detail::digits2_2_10(std::numeric_limits<V>::digits);
  742. if (thread_default_variable_precision_options() >= variable_precision_options::preserve_all_precision)
  743. {
  744. BOOST_IF_CONSTEXPR(std::is_floating_point<V>::value)
  745. {
  746. if (std::numeric_limits<V>::digits > mpc_get_prec(this->data()))
  747. mpc_set_prec(this->data(), std::numeric_limits<V>::digits);
  748. }
  749. else
  750. {
  751. if (precision() < d10)
  752. this->precision(d10);
  753. }
  754. }
  755. *static_cast<detail::mpc_complex_imp<0>*>(this) = v;
  756. return *this;
  757. }
  758. mpc_complex_backend& operator=(const mpc_t val)
  759. {
  760. mpc_set_prec(this->m_data, mpc_get_prec(val));
  761. mpc_set(this->m_data, val, GMP_RNDN);
  762. return *this;
  763. }
  764. template <unsigned D>
  765. mpc_complex_backend& operator=(const mpc_complex_backend<D>& val)
  766. {
  767. mpc_set_prec(this->m_data, mpc_get_prec(val.data()));
  768. mpc_set(this->m_data, val.data(), GMP_RNDN);
  769. return *this;
  770. }
  771. template <unsigned D>
  772. mpc_complex_backend& operator=(const mpfr_float_backend<D>& val)
  773. {
  774. if (D == 0 ? this->preserve_component_precision() : this->preserve_related_precision())
  775. mpc_set_prec(this->m_data, mpfr_get_prec(val.data()));
  776. mpc_set_fr(this->m_data, val.data(), GMP_RNDN);
  777. return *this;
  778. }
  779. mpc_complex_backend& operator=(const std::complex<float>& val)
  780. {
  781. mpc_set_d_d(this->m_data, val.real(), val.imag(), GMP_RNDN);
  782. return *this;
  783. }
  784. mpc_complex_backend& operator=(const std::complex<double>& val)
  785. {
  786. mpc_set_d_d(this->m_data, val.real(), val.imag(), GMP_RNDN);
  787. return *this;
  788. }
  789. mpc_complex_backend& operator=(const std::complex<long double>& val)
  790. {
  791. mpc_set_ld_ld(this->m_data, val.real(), val.imag(), GMP_RNDN);
  792. return *this;
  793. }
  794. static unsigned default_precision() noexcept
  795. {
  796. return get_global_default_precision();
  797. }
  798. static void default_precision(unsigned v) noexcept
  799. {
  800. get_global_default_precision() = v;
  801. }
  802. static unsigned thread_default_precision() noexcept
  803. {
  804. return get_default_precision();
  805. }
  806. static void thread_default_precision(unsigned v) noexcept
  807. {
  808. get_default_precision() = v;
  809. }
  810. unsigned precision() const noexcept
  811. {
  812. return multiprecision::detail::digits2_2_10(mpc_get_prec(this->m_data));
  813. }
  814. void precision(unsigned digits10) noexcept
  815. {
  816. mpfr_prec_round(mpc_realref(this->m_data), multiprecision::detail::digits10_2_2((digits10)), GMP_RNDN);
  817. mpfr_prec_round(mpc_imagref(this->m_data), multiprecision::detail::digits10_2_2((digits10)), GMP_RNDN);
  818. }
  819. //
  820. // Variable precision options:
  821. //
  822. static variable_precision_options default_variable_precision_options() noexcept
  823. {
  824. return get_global_default_options();
  825. }
  826. static variable_precision_options thread_default_variable_precision_options() noexcept
  827. {
  828. return get_default_options();
  829. }
  830. static void default_variable_precision_options(variable_precision_options opts)
  831. {
  832. get_global_default_options() = opts;
  833. }
  834. static void thread_default_variable_precision_options(variable_precision_options opts)
  835. {
  836. get_default_options() = opts;
  837. }
  838. };
  839. template <unsigned digits10, class T>
  840. inline typename std::enable_if<boost::multiprecision::detail::is_arithmetic<T>::value, bool>::type eval_eq(const mpc_complex_backend<digits10>& a, const T& b) noexcept
  841. {
  842. return a.compare(b) == 0;
  843. }
  844. template <unsigned digits10, class T>
  845. inline typename std::enable_if<boost::multiprecision::detail::is_arithmetic<T>::value, bool>::type eval_lt(const mpc_complex_backend<digits10>& a, const T& b) noexcept
  846. {
  847. return a.compare(b) < 0;
  848. }
  849. template <unsigned digits10, class T>
  850. inline typename std::enable_if<boost::multiprecision::detail::is_arithmetic<T>::value, bool>::type eval_gt(const mpc_complex_backend<digits10>& a, const T& b) noexcept
  851. {
  852. return a.compare(b) > 0;
  853. }
  854. template <unsigned D1, unsigned D2>
  855. inline void eval_add(mpc_complex_backend<D1>& result, const mpc_complex_backend<D2>& o)
  856. {
  857. mpc_add(result.data(), result.data(), o.data(), GMP_RNDN);
  858. }
  859. template <unsigned D1, unsigned D2>
  860. inline void eval_add(mpc_complex_backend<D1>& result, const mpfr_float_backend<D2>& o)
  861. {
  862. mpc_add_fr(result.data(), result.data(), o.data(), GMP_RNDN);
  863. }
  864. template <unsigned D1, unsigned D2>
  865. inline void eval_subtract(mpc_complex_backend<D1>& result, const mpc_complex_backend<D2>& o)
  866. {
  867. mpc_sub(result.data(), result.data(), o.data(), GMP_RNDN);
  868. }
  869. template <unsigned D1, unsigned D2>
  870. inline void eval_subtract(mpc_complex_backend<D1>& result, const mpfr_float_backend<D2>& o)
  871. {
  872. mpc_sub_fr(result.data(), result.data(), o.data(), GMP_RNDN);
  873. }
  874. template <unsigned D1, unsigned D2>
  875. inline void eval_multiply(mpc_complex_backend<D1>& result, const mpc_complex_backend<D2>& o)
  876. {
  877. if ((void*)&result == (void*)&o)
  878. mpc_sqr(result.data(), o.data(), GMP_RNDN);
  879. else
  880. mpc_mul(result.data(), result.data(), o.data(), GMP_RNDN);
  881. }
  882. template <unsigned D1, unsigned D2>
  883. inline void eval_multiply(mpc_complex_backend<D1>& result, const mpfr_float_backend<D2>& o)
  884. {
  885. mpc_mul_fr(result.data(), result.data(), o.data(), GMP_RNDN);
  886. }
  887. template <unsigned D1, unsigned D2>
  888. inline void eval_divide(mpc_complex_backend<D1>& result, const mpc_complex_backend<D2>& o)
  889. {
  890. mpc_div(result.data(), result.data(), o.data(), GMP_RNDN);
  891. }
  892. template <unsigned D1, unsigned D2>
  893. inline void eval_divide(mpc_complex_backend<D1>& result, const mpfr_float_backend<D2>& o)
  894. {
  895. mpc_div_fr(result.data(), result.data(), o.data(), GMP_RNDN);
  896. }
  897. template <unsigned digits10>
  898. inline void eval_add(mpc_complex_backend<digits10>& result, unsigned long i)
  899. {
  900. mpc_add_ui(result.data(), result.data(), i, GMP_RNDN);
  901. }
  902. template <unsigned digits10>
  903. inline void eval_subtract(mpc_complex_backend<digits10>& result, unsigned long i)
  904. {
  905. mpc_sub_ui(result.data(), result.data(), i, GMP_RNDN);
  906. }
  907. template <unsigned digits10>
  908. inline void eval_multiply(mpc_complex_backend<digits10>& result, unsigned long i)
  909. {
  910. mpc_mul_ui(result.data(), result.data(), i, GMP_RNDN);
  911. }
  912. template <unsigned digits10>
  913. inline void eval_divide(mpc_complex_backend<digits10>& result, unsigned long i)
  914. {
  915. mpc_div_ui(result.data(), result.data(), i, GMP_RNDN);
  916. }
  917. template <unsigned digits10>
  918. inline void eval_add(mpc_complex_backend<digits10>& result, long i)
  919. {
  920. if (i > 0)
  921. mpc_add_ui(result.data(), result.data(), i, GMP_RNDN);
  922. else
  923. mpc_sub_ui(result.data(), result.data(), boost::multiprecision::detail::unsigned_abs(i), GMP_RNDN);
  924. }
  925. template <unsigned digits10>
  926. inline void eval_subtract(mpc_complex_backend<digits10>& result, long i)
  927. {
  928. if (i > 0)
  929. mpc_sub_ui(result.data(), result.data(), i, GMP_RNDN);
  930. else
  931. mpc_add_ui(result.data(), result.data(), boost::multiprecision::detail::unsigned_abs(i), GMP_RNDN);
  932. }
  933. template <unsigned digits10>
  934. inline void eval_multiply(mpc_complex_backend<digits10>& result, long i)
  935. {
  936. mpc_mul_ui(result.data(), result.data(), boost::multiprecision::detail::unsigned_abs(i), GMP_RNDN);
  937. if (i < 0)
  938. mpc_neg(result.data(), result.data(), GMP_RNDN);
  939. }
  940. template <unsigned digits10>
  941. inline void eval_divide(mpc_complex_backend<digits10>& result, long i)
  942. {
  943. mpc_div_ui(result.data(), result.data(), boost::multiprecision::detail::unsigned_abs(i), GMP_RNDN);
  944. if (i < 0)
  945. mpc_neg(result.data(), result.data(), GMP_RNDN);
  946. }
  947. //
  948. // Specialised 3 arg versions of the basic operators:
  949. //
  950. template <unsigned D1, unsigned D2, unsigned D3>
  951. inline void eval_add(mpc_complex_backend<D1>& a, const mpc_complex_backend<D2>& x, const mpc_complex_backend<D3>& y)
  952. {
  953. mpc_add(a.data(), x.data(), y.data(), GMP_RNDN);
  954. }
  955. template <unsigned D1, unsigned D2, unsigned D3>
  956. inline void eval_add(mpc_complex_backend<D1>& a, const mpc_complex_backend<D2>& x, const mpfr_float_backend<D3>& y)
  957. {
  958. mpc_add_fr(a.data(), x.data(), y.data(), GMP_RNDN);
  959. }
  960. template <unsigned D1, unsigned D2, unsigned D3>
  961. inline void eval_add(mpc_complex_backend<D1>& a, const mpfr_float_backend<D2>& x, const mpc_complex_backend<D3>& y)
  962. {
  963. mpc_add_fr(a.data(), y.data(), x.data(), GMP_RNDN);
  964. }
  965. template <unsigned D1, unsigned D2>
  966. inline void eval_add(mpc_complex_backend<D1>& a, const mpc_complex_backend<D2>& x, unsigned long y)
  967. {
  968. mpc_add_ui(a.data(), x.data(), y, GMP_RNDN);
  969. }
  970. template <unsigned D1, unsigned D2>
  971. inline void eval_add(mpc_complex_backend<D1>& a, const mpc_complex_backend<D2>& x, long y)
  972. {
  973. if (y < 0)
  974. mpc_sub_ui(a.data(), x.data(), boost::multiprecision::detail::unsigned_abs(y), GMP_RNDN);
  975. else
  976. mpc_add_ui(a.data(), x.data(), y, GMP_RNDN);
  977. }
  978. template <unsigned D1, unsigned D2>
  979. inline void eval_add(mpc_complex_backend<D1>& a, unsigned long x, const mpc_complex_backend<D2>& y)
  980. {
  981. mpc_add_ui(a.data(), y.data(), x, GMP_RNDN);
  982. }
  983. template <unsigned D1, unsigned D2>
  984. inline void eval_add(mpc_complex_backend<D1>& a, long x, const mpc_complex_backend<D2>& y)
  985. {
  986. if (x < 0)
  987. {
  988. mpc_ui_sub(a.data(), boost::multiprecision::detail::unsigned_abs(x), y.data(), GMP_RNDN);
  989. mpc_neg(a.data(), a.data(), GMP_RNDN);
  990. }
  991. else
  992. mpc_add_ui(a.data(), y.data(), x, GMP_RNDN);
  993. }
  994. template <unsigned D1, unsigned D2, unsigned D3>
  995. inline void eval_subtract(mpc_complex_backend<D1>& a, const mpc_complex_backend<D2>& x, const mpc_complex_backend<D3>& y)
  996. {
  997. mpc_sub(a.data(), x.data(), y.data(), GMP_RNDN);
  998. }
  999. template <unsigned D1, unsigned D2, unsigned D3>
  1000. inline void eval_subtract(mpc_complex_backend<D1>& a, const mpc_complex_backend<D2>& x, const mpfr_float_backend<D3>& y)
  1001. {
  1002. mpc_sub_fr(a.data(), x.data(), y.data(), GMP_RNDN);
  1003. }
  1004. template <unsigned D1, unsigned D2, unsigned D3>
  1005. inline void eval_subtract(mpc_complex_backend<D1>& a, const mpfr_float_backend<D2>& x, const mpc_complex_backend<D3>& y)
  1006. {
  1007. mpc_fr_sub(a.data(), x.data(), y.data(), GMP_RNDN);
  1008. }
  1009. template <unsigned D1, unsigned D2>
  1010. inline void eval_subtract(mpc_complex_backend<D1>& a, const mpc_complex_backend<D2>& x, unsigned long y)
  1011. {
  1012. mpc_sub_ui(a.data(), x.data(), y, GMP_RNDN);
  1013. }
  1014. template <unsigned D1, unsigned D2>
  1015. inline void eval_subtract(mpc_complex_backend<D1>& a, const mpc_complex_backend<D2>& x, long y)
  1016. {
  1017. if (y < 0)
  1018. mpc_add_ui(a.data(), x.data(), boost::multiprecision::detail::unsigned_abs(y), GMP_RNDN);
  1019. else
  1020. mpc_sub_ui(a.data(), x.data(), y, GMP_RNDN);
  1021. }
  1022. template <unsigned D1, unsigned D2>
  1023. inline void eval_subtract(mpc_complex_backend<D1>& a, unsigned long x, const mpc_complex_backend<D2>& y)
  1024. {
  1025. mpc_ui_sub(a.data(), x, y.data(), GMP_RNDN);
  1026. }
  1027. template <unsigned D1, unsigned D2>
  1028. inline void eval_subtract(mpc_complex_backend<D1>& a, long x, const mpc_complex_backend<D2>& y)
  1029. {
  1030. if (x < 0)
  1031. {
  1032. mpc_add_ui(a.data(), y.data(), boost::multiprecision::detail::unsigned_abs(x), GMP_RNDN);
  1033. mpc_neg(a.data(), a.data(), GMP_RNDN);
  1034. }
  1035. else
  1036. mpc_ui_sub(a.data(), x, y.data(), GMP_RNDN);
  1037. }
  1038. template <unsigned D1, unsigned D2, unsigned D3>
  1039. inline void eval_multiply(mpc_complex_backend<D1>& a, const mpc_complex_backend<D2>& x, const mpc_complex_backend<D3>& y)
  1040. {
  1041. if ((void*)&x == (void*)&y)
  1042. mpc_sqr(a.data(), x.data(), GMP_RNDN);
  1043. else
  1044. mpc_mul(a.data(), x.data(), y.data(), GMP_RNDN);
  1045. }
  1046. template <unsigned D1, unsigned D2, unsigned D3>
  1047. inline void eval_multiply(mpc_complex_backend<D1>& a, const mpc_complex_backend<D2>& x, const mpfr_float_backend<D3>& y)
  1048. {
  1049. mpc_mul_fr(a.data(), x.data(), y.data(), GMP_RNDN);
  1050. }
  1051. template <unsigned D1, unsigned D2, unsigned D3>
  1052. inline void eval_multiply(mpc_complex_backend<D1>& a, const mpfr_float_backend<D2>& x, const mpc_complex_backend<D3>& y)
  1053. {
  1054. mpc_mul_fr(a.data(), y.data(), x.data(), GMP_RNDN);
  1055. }
  1056. template <unsigned D1, unsigned D2>
  1057. inline void eval_multiply(mpc_complex_backend<D1>& a, const mpc_complex_backend<D2>& x, unsigned long y)
  1058. {
  1059. mpc_mul_ui(a.data(), x.data(), y, GMP_RNDN);
  1060. }
  1061. template <unsigned D1, unsigned D2>
  1062. inline void eval_multiply(mpc_complex_backend<D1>& a, const mpc_complex_backend<D2>& x, long y)
  1063. {
  1064. if (y < 0)
  1065. {
  1066. mpc_mul_ui(a.data(), x.data(), boost::multiprecision::detail::unsigned_abs(y), GMP_RNDN);
  1067. a.negate();
  1068. }
  1069. else
  1070. mpc_mul_ui(a.data(), x.data(), y, GMP_RNDN);
  1071. }
  1072. template <unsigned D1, unsigned D2>
  1073. inline void eval_multiply(mpc_complex_backend<D1>& a, unsigned long x, const mpc_complex_backend<D2>& y)
  1074. {
  1075. mpc_mul_ui(a.data(), y.data(), x, GMP_RNDN);
  1076. }
  1077. template <unsigned D1, unsigned D2>
  1078. inline void eval_multiply(mpc_complex_backend<D1>& a, long x, const mpc_complex_backend<D2>& y)
  1079. {
  1080. if (x < 0)
  1081. {
  1082. mpc_mul_ui(a.data(), y.data(), boost::multiprecision::detail::unsigned_abs(x), GMP_RNDN);
  1083. mpc_neg(a.data(), a.data(), GMP_RNDN);
  1084. }
  1085. else
  1086. mpc_mul_ui(a.data(), y.data(), x, GMP_RNDN);
  1087. }
  1088. template <unsigned D1, unsigned D2, unsigned D3>
  1089. inline void eval_divide(mpc_complex_backend<D1>& a, const mpc_complex_backend<D2>& x, const mpc_complex_backend<D3>& y)
  1090. {
  1091. mpc_div(a.data(), x.data(), y.data(), GMP_RNDN);
  1092. }
  1093. template <unsigned D1, unsigned D2, unsigned D3>
  1094. inline void eval_divide(mpc_complex_backend<D1>& a, const mpc_complex_backend<D2>& x, const mpfr_float_backend<D3>& y)
  1095. {
  1096. mpc_div_fr(a.data(), x.data(), y.data(), GMP_RNDN);
  1097. }
  1098. template <unsigned D1, unsigned D2, unsigned D3>
  1099. inline void eval_divide(mpc_complex_backend<D1>& a, const mpfr_float_backend<D2>& x, const mpc_complex_backend<D3>& y)
  1100. {
  1101. mpc_fr_div(a.data(), x.data(), y.data(), GMP_RNDN);
  1102. }
  1103. template <unsigned D1, unsigned D2>
  1104. inline void eval_divide(mpc_complex_backend<D1>& a, const mpc_complex_backend<D2>& x, unsigned long y)
  1105. {
  1106. mpc_div_ui(a.data(), x.data(), y, GMP_RNDN);
  1107. }
  1108. template <unsigned D1, unsigned D2>
  1109. inline void eval_divide(mpc_complex_backend<D1>& a, const mpc_complex_backend<D2>& x, long y)
  1110. {
  1111. if (y < 0)
  1112. {
  1113. mpc_div_ui(a.data(), x.data(), boost::multiprecision::detail::unsigned_abs(y), GMP_RNDN);
  1114. a.negate();
  1115. }
  1116. else
  1117. mpc_div_ui(a.data(), x.data(), y, GMP_RNDN);
  1118. }
  1119. template <unsigned D1, unsigned D2>
  1120. inline void eval_divide(mpc_complex_backend<D1>& a, unsigned long x, const mpc_complex_backend<D2>& y)
  1121. {
  1122. mpc_ui_div(a.data(), x, y.data(), GMP_RNDN);
  1123. }
  1124. template <unsigned D1, unsigned D2>
  1125. inline void eval_divide(mpc_complex_backend<D1>& a, long x, const mpc_complex_backend<D2>& y)
  1126. {
  1127. if (x < 0)
  1128. {
  1129. mpc_ui_div(a.data(), boost::multiprecision::detail::unsigned_abs(x), y.data(), GMP_RNDN);
  1130. mpc_neg(a.data(), a.data(), GMP_RNDN);
  1131. }
  1132. else
  1133. mpc_ui_div(a.data(), x, y.data(), GMP_RNDN);
  1134. }
  1135. template <unsigned digits10>
  1136. inline bool eval_is_zero(const mpc_complex_backend<digits10>& val) noexcept
  1137. {
  1138. return (0 != mpfr_zero_p(mpc_realref(val.data()))) && (0 != mpfr_zero_p(mpc_imagref(val.data())));
  1139. }
  1140. template <unsigned digits10>
  1141. inline int eval_get_sign(const mpc_complex_backend<digits10>&)
  1142. {
  1143. static_assert(digits10 == UINT_MAX, "Complex numbers have no sign bit."); // designed to always fail
  1144. return 0;
  1145. }
  1146. template <unsigned digits10>
  1147. inline void eval_convert_to(unsigned long* result, const mpc_complex_backend<digits10>& val)
  1148. {
  1149. if (0 == mpfr_zero_p(mpc_imagref(val.data())))
  1150. {
  1151. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Could not convert imaginary number to scalar."));
  1152. }
  1153. mpfr_float_backend<digits10> t;
  1154. mpc_real(t.data(), val.data(), GMP_RNDN);
  1155. eval_convert_to(result, t);
  1156. }
  1157. template <unsigned digits10>
  1158. inline void eval_convert_to(long* result, const mpc_complex_backend<digits10>& val)
  1159. {
  1160. if (0 == mpfr_zero_p(mpc_imagref(val.data())))
  1161. {
  1162. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Could not convert imaginary number to scalar."));
  1163. }
  1164. mpfr_float_backend<digits10> t;
  1165. mpc_real(t.data(), val.data(), GMP_RNDN);
  1166. eval_convert_to(result, t);
  1167. }
  1168. #ifdef _MPFR_H_HAVE_INTMAX_T
  1169. template <unsigned digits10>
  1170. inline void eval_convert_to(unsigned long long* result, const mpc_complex_backend<digits10>& val)
  1171. {
  1172. if (0 == mpfr_zero_p(mpc_imagref(val.data())))
  1173. {
  1174. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Could not convert imaginary number to scalar."));
  1175. }
  1176. mpfr_float_backend<digits10> t;
  1177. mpc_real(t.data(), val.data(), GMP_RNDN);
  1178. eval_convert_to(result, t);
  1179. }
  1180. template <unsigned digits10>
  1181. inline void eval_convert_to(long long* result, const mpc_complex_backend<digits10>& val)
  1182. {
  1183. if (0 == mpfr_zero_p(mpc_imagref(val.data())))
  1184. {
  1185. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Could not convert imaginary number to scalar."));
  1186. }
  1187. mpfr_float_backend<digits10> t;
  1188. mpc_real(t.data(), val.data(), GMP_RNDN);
  1189. eval_convert_to(result, t);
  1190. }
  1191. #endif
  1192. template <unsigned digits10>
  1193. inline void eval_convert_to(double* result, const mpc_complex_backend<digits10>& val) noexcept
  1194. {
  1195. if (0 == mpfr_zero_p(mpc_imagref(val.data())))
  1196. {
  1197. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Could not convert imaginary number to scalar."));
  1198. }
  1199. mpfr_float_backend<digits10> t;
  1200. mpc_real(t.data(), val.data(), GMP_RNDN);
  1201. eval_convert_to(result, t);
  1202. }
  1203. template <unsigned digits10>
  1204. inline void eval_convert_to(long double* result, const mpc_complex_backend<digits10>& val) noexcept
  1205. {
  1206. if (0 == mpfr_zero_p(mpc_imagref(val.data())))
  1207. {
  1208. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Could not convert imaginary number to scalar."));
  1209. }
  1210. mpfr_float_backend<digits10> t;
  1211. mpc_real(t.data(), val.data(), GMP_RNDN);
  1212. eval_convert_to(result, t);
  1213. }
  1214. #ifdef BOOST_HAS_INT128
  1215. template <unsigned digits10>
  1216. inline void eval_convert_to(uint128_type* result, const mpc_complex_backend<digits10>& val)
  1217. {
  1218. using default_ops::eval_convert_to;
  1219. if (0 == mpfr_zero_p(mpc_imagref(val.data())))
  1220. {
  1221. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Could not convert imaginary number to scalar."));
  1222. }
  1223. mpfr_float_backend<digits10> t;
  1224. mpc_real(t.data(), val.data(), GMP_RNDN);
  1225. eval_convert_to(result, t);
  1226. }
  1227. template <unsigned digits10>
  1228. inline void eval_convert_to(int128_type* result, const mpc_complex_backend<digits10>& val)
  1229. {
  1230. using default_ops::eval_convert_to;
  1231. if (0 == mpfr_zero_p(mpc_imagref(val.data())))
  1232. {
  1233. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Could not convert imaginary number to scalar."));
  1234. }
  1235. mpfr_float_backend<digits10> t;
  1236. mpc_real(t.data(), val.data(), GMP_RNDN);
  1237. eval_convert_to(result, t);
  1238. }
  1239. #endif
  1240. #ifdef BOOST_HAS_FLOAT128
  1241. template <unsigned digits10>
  1242. inline void eval_convert_to(float128_type* result, const mpc_complex_backend<digits10>& val)
  1243. {
  1244. using default_ops::eval_convert_to;
  1245. if (0 == mpfr_zero_p(mpc_imagref(val.data())))
  1246. {
  1247. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Could not convert imaginary number to scalar."));
  1248. }
  1249. mpfr_float_backend<digits10> t;
  1250. mpc_real(t.data(), val.data(), GMP_RNDN);
  1251. eval_convert_to(result, t);
  1252. }
  1253. #endif
  1254. template <mpfr_allocation_type AllocationType>
  1255. inline void assign_components_set_precision(mpc_complex_backend<0>& result, const mpfr_float_backend<0, AllocationType>& a, const mpfr_float_backend<0, AllocationType>& b)
  1256. {
  1257. if (result.thread_default_variable_precision_options() >= variable_precision_options::preserve_component_precision)
  1258. {
  1259. unsigned long prec = (std::max)(mpfr_get_prec(a.data()), mpfr_get_prec(b.data()));
  1260. mpc_set_prec(result.data(), prec);
  1261. }
  1262. }
  1263. template <unsigned D2, mpfr_allocation_type AllocationType>
  1264. inline void assign_components_set_precision(mpc_complex_backend<0>& result, const mpfr_float_backend<D2, AllocationType>& a, const mpfr_float_backend<D2, AllocationType>& b)
  1265. {
  1266. if (result.thread_default_variable_precision_options() >= variable_precision_options::preserve_related_precision)
  1267. {
  1268. unsigned long prec = (std::max)(mpfr_get_prec(a.data()), mpfr_get_prec(b.data()));
  1269. mpc_set_prec(result.data(), prec);
  1270. }
  1271. }
  1272. template <unsigned D1, unsigned D2, mpfr_allocation_type AllocationType>
  1273. inline void assign_components_set_precision(mpc_complex_backend<D1>&, const mpfr_float_backend<D2, AllocationType>&, const mpfr_float_backend<D2, AllocationType>&)
  1274. {
  1275. }
  1276. template <unsigned D1, unsigned D2, mpfr_allocation_type AllocationType>
  1277. inline void assign_components(mpc_complex_backend<D1>& result, const mpfr_float_backend<D2, AllocationType>& a, const mpfr_float_backend<D2, AllocationType>& b)
  1278. {
  1279. //
  1280. // This is called from class number's constructors, so if we have variable
  1281. // precision, then copy the precision of the source variables.
  1282. //
  1283. assign_components_set_precision(result, a, b);
  1284. using default_ops::eval_fpclassify;
  1285. if (eval_fpclassify(a) == static_cast<int>(FP_NAN))
  1286. {
  1287. mpc_set_fr(result.data(), a.data(), GMP_RNDN);
  1288. }
  1289. else if (eval_fpclassify(b) == static_cast<int>(FP_NAN))
  1290. {
  1291. mpc_set_fr(result.data(), b.data(), GMP_RNDN);
  1292. }
  1293. else
  1294. {
  1295. mpc_set_fr_fr(result.data(), a.data(), b.data(), GMP_RNDN);
  1296. }
  1297. }
  1298. template <unsigned D1, unsigned D2, mpfr_allocation_type AllocationType>
  1299. inline void assign_components(mpc_complex_backend<D1>& result, unsigned long a, unsigned long b)
  1300. {
  1301. mpc_set_ui_ui(result.data(), a, b, GMP_RNDN);
  1302. }
  1303. template <unsigned D1, unsigned D2, mpfr_allocation_type AllocationType>
  1304. inline void assign_components(mpc_complex_backend<D1>& result, long a, long b)
  1305. {
  1306. mpc_set_si_si(result.data(), a, b, GMP_RNDN);
  1307. }
  1308. #if defined(BOOST_HAS_LONG_LONG) && defined(_MPFR_H_HAVE_INTMAX_T)
  1309. template <unsigned D1, unsigned D2, mpfr_allocation_type AllocationType>
  1310. inline void assign_components(mpc_complex_backend<D1>& result, unsigned long long a, unsigned long long b)
  1311. {
  1312. mpc_set_uj_uj(result.data(), a, b, GMP_RNDN);
  1313. }
  1314. template <unsigned D1, unsigned D2, mpfr_allocation_type AllocationType>
  1315. inline void assign_components(mpc_complex_backend<D1>& result, long long a, long long b)
  1316. {
  1317. mpc_set_sj_sj(result.data(), a, b, GMP_RNDN);
  1318. }
  1319. #endif
  1320. template <unsigned D1, unsigned D2, mpfr_allocation_type AllocationType>
  1321. inline void assign_components(mpc_complex_backend<D1>& result, double a, double b)
  1322. {
  1323. if (BOOST_MP_ISNAN(a))
  1324. {
  1325. mpc_set_d(result.data(), a, GMP_RNDN);
  1326. }
  1327. else if (BOOST_MP_ISNAN(b))
  1328. {
  1329. mpc_set_d(result.data(), b, GMP_RNDN);
  1330. }
  1331. else
  1332. {
  1333. mpc_set_d_d(result.data(), a, b, GMP_RNDN);
  1334. }
  1335. }
  1336. template <unsigned D1, unsigned D2, mpfr_allocation_type AllocationType>
  1337. inline void assign_components(mpc_complex_backend<D1>& result, long double a, long double b)
  1338. {
  1339. if (BOOST_MP_ISNAN(a))
  1340. {
  1341. mpc_set_d(result.data(), a, GMP_RNDN);
  1342. }
  1343. else if (BOOST_MP_ISNAN(b))
  1344. {
  1345. mpc_set_d(result.data(), b, GMP_RNDN);
  1346. }
  1347. else
  1348. {
  1349. mpc_set_ld_ld(result.data(), a, b, GMP_RNDN);
  1350. }
  1351. }
  1352. //
  1353. // Native non-member operations:
  1354. //
  1355. template <unsigned Digits10>
  1356. inline void eval_sqrt(mpc_complex_backend<Digits10>& result, const mpc_complex_backend<Digits10>& val)
  1357. {
  1358. mpc_sqrt(result.data(), val.data(), GMP_RNDN);
  1359. }
  1360. template <unsigned Digits10>
  1361. inline void eval_pow(mpc_complex_backend<Digits10>& result, const mpc_complex_backend<Digits10>& b, const mpc_complex_backend<Digits10>& e)
  1362. {
  1363. mpc_pow(result.data(), b.data(), e.data(), GMP_RNDN);
  1364. }
  1365. template <unsigned Digits10>
  1366. inline void eval_exp(mpc_complex_backend<Digits10>& result, const mpc_complex_backend<Digits10>& arg)
  1367. {
  1368. mpc_exp(result.data(), arg.data(), GMP_RNDN);
  1369. }
  1370. template <unsigned Digits10>
  1371. inline void eval_log(mpc_complex_backend<Digits10>& result, const mpc_complex_backend<Digits10>& arg)
  1372. {
  1373. mpc_log(result.data(), arg.data(), GMP_RNDN);
  1374. }
  1375. template <unsigned Digits10>
  1376. inline void eval_log10(mpc_complex_backend<Digits10>& result, const mpc_complex_backend<Digits10>& arg)
  1377. {
  1378. mpc_log10(result.data(), arg.data(), GMP_RNDN);
  1379. }
  1380. template <unsigned Digits10>
  1381. inline void eval_sin(mpc_complex_backend<Digits10>& result, const mpc_complex_backend<Digits10>& arg)
  1382. {
  1383. mpc_sin(result.data(), arg.data(), GMP_RNDN);
  1384. }
  1385. template <unsigned Digits10>
  1386. inline void eval_cos(mpc_complex_backend<Digits10>& result, const mpc_complex_backend<Digits10>& arg)
  1387. {
  1388. mpc_cos(result.data(), arg.data(), GMP_RNDN);
  1389. }
  1390. template <unsigned Digits10>
  1391. inline void eval_tan(mpc_complex_backend<Digits10>& result, const mpc_complex_backend<Digits10>& arg)
  1392. {
  1393. mpc_tan(result.data(), arg.data(), GMP_RNDN);
  1394. }
  1395. template <unsigned Digits10>
  1396. inline void eval_asin(mpc_complex_backend<Digits10>& result, const mpc_complex_backend<Digits10>& arg)
  1397. {
  1398. mpc_asin(result.data(), arg.data(), GMP_RNDN);
  1399. }
  1400. template <unsigned Digits10>
  1401. inline void eval_acos(mpc_complex_backend<Digits10>& result, const mpc_complex_backend<Digits10>& arg)
  1402. {
  1403. mpc_acos(result.data(), arg.data(), GMP_RNDN);
  1404. }
  1405. template <unsigned Digits10>
  1406. inline void eval_atan(mpc_complex_backend<Digits10>& result, const mpc_complex_backend<Digits10>& arg)
  1407. {
  1408. mpc_atan(result.data(), arg.data(), GMP_RNDN);
  1409. }
  1410. template <unsigned Digits10>
  1411. inline void eval_sinh(mpc_complex_backend<Digits10>& result, const mpc_complex_backend<Digits10>& arg)
  1412. {
  1413. mpc_sinh(result.data(), arg.data(), GMP_RNDN);
  1414. }
  1415. template <unsigned Digits10>
  1416. inline void eval_cosh(mpc_complex_backend<Digits10>& result, const mpc_complex_backend<Digits10>& arg)
  1417. {
  1418. mpc_cosh(result.data(), arg.data(), GMP_RNDN);
  1419. }
  1420. template <unsigned Digits10>
  1421. inline void eval_tanh(mpc_complex_backend<Digits10>& result, const mpc_complex_backend<Digits10>& arg)
  1422. {
  1423. mpc_tanh(result.data(), arg.data(), GMP_RNDN);
  1424. }
  1425. template <unsigned Digits10>
  1426. inline void eval_asinh(mpc_complex_backend<Digits10>& result, const mpc_complex_backend<Digits10>& arg)
  1427. {
  1428. mpc_asinh(result.data(), arg.data(), GMP_RNDN);
  1429. }
  1430. template <unsigned Digits10>
  1431. inline void eval_acosh(mpc_complex_backend<Digits10>& result, const mpc_complex_backend<Digits10>& arg)
  1432. {
  1433. mpc_acosh(result.data(), arg.data(), GMP_RNDN);
  1434. }
  1435. template <unsigned Digits10>
  1436. inline void eval_atanh(mpc_complex_backend<Digits10>& result, const mpc_complex_backend<Digits10>& arg)
  1437. {
  1438. mpc_atanh(result.data(), arg.data(), GMP_RNDN);
  1439. }
  1440. template <unsigned Digits10>
  1441. inline void eval_conj(mpc_complex_backend<Digits10>& result, const mpc_complex_backend<Digits10>& arg)
  1442. {
  1443. mpc_conj(result.data(), arg.data(), GMP_RNDN);
  1444. }
  1445. template <unsigned Digits10>
  1446. inline void eval_proj(mpc_complex_backend<Digits10>& result, const mpc_complex_backend<Digits10>& arg)
  1447. {
  1448. mpc_proj(result.data(), arg.data(), GMP_RNDN);
  1449. }
  1450. template <unsigned Digits10>
  1451. inline void eval_real(mpfr_float_backend<Digits10>& result, const mpc_complex_backend<Digits10>& arg)
  1452. {
  1453. mpfr_set_prec(result.data(), mpfr_get_prec(mpc_realref(arg.data())));
  1454. mpfr_set(result.data(), mpc_realref(arg.data()), GMP_RNDN);
  1455. }
  1456. template <unsigned Digits10>
  1457. inline void eval_imag(mpfr_float_backend<Digits10>& result, const mpc_complex_backend<Digits10>& arg)
  1458. {
  1459. mpfr_set_prec(result.data(), mpfr_get_prec(mpc_imagref(arg.data())));
  1460. mpfr_set(result.data(), mpc_imagref(arg.data()), GMP_RNDN);
  1461. }
  1462. template <unsigned Digits10>
  1463. inline void eval_set_imag(mpc_complex_backend<Digits10>& result, const mpfr_float_backend<Digits10>& arg)
  1464. {
  1465. mpfr_set(mpc_imagref(result.data()), arg.data(), GMP_RNDN);
  1466. }
  1467. template <unsigned Digits10>
  1468. inline void eval_set_real(mpc_complex_backend<Digits10>& result, const mpfr_float_backend<Digits10>& arg)
  1469. {
  1470. mpfr_set(mpc_realref(result.data()), arg.data(), GMP_RNDN);
  1471. }
  1472. template <unsigned Digits10>
  1473. inline void eval_set_real(mpc_complex_backend<Digits10>& result, const gmp_int& arg)
  1474. {
  1475. mpfr_set_z(mpc_realref(result.data()), arg.data(), GMP_RNDN);
  1476. }
  1477. template <unsigned Digits10>
  1478. inline void eval_set_real(mpc_complex_backend<Digits10>& result, const gmp_rational& arg)
  1479. {
  1480. mpfr_set_q(mpc_realref(result.data()), arg.data(), GMP_RNDN);
  1481. }
  1482. template <unsigned Digits10>
  1483. inline void eval_set_real(mpc_complex_backend<Digits10>& result, const unsigned& arg)
  1484. {
  1485. mpfr_set_ui(mpc_realref(result.data()), arg, GMP_RNDN);
  1486. }
  1487. template <unsigned Digits10>
  1488. inline void eval_set_real(mpc_complex_backend<Digits10>& result, const unsigned long& arg)
  1489. {
  1490. mpfr_set_ui(mpc_realref(result.data()), arg, GMP_RNDN);
  1491. }
  1492. template <unsigned Digits10>
  1493. inline void eval_set_real(mpc_complex_backend<Digits10>& result, const int& arg)
  1494. {
  1495. mpfr_set_si(mpc_realref(result.data()), arg, GMP_RNDN);
  1496. }
  1497. template <unsigned Digits10>
  1498. inline void eval_set_real(mpc_complex_backend<Digits10>& result, const long& arg)
  1499. {
  1500. mpfr_set_si(mpc_realref(result.data()), arg, GMP_RNDN);
  1501. }
  1502. template <unsigned Digits10>
  1503. inline void eval_set_real(mpc_complex_backend<Digits10>& result, const float& arg)
  1504. {
  1505. mpfr_set_flt(mpc_realref(result.data()), arg, GMP_RNDN);
  1506. }
  1507. template <unsigned Digits10>
  1508. inline void eval_set_real(mpc_complex_backend<Digits10>& result, const double& arg)
  1509. {
  1510. mpfr_set_d(mpc_realref(result.data()), arg, GMP_RNDN);
  1511. }
  1512. template <unsigned Digits10>
  1513. inline void eval_set_real(mpc_complex_backend<Digits10>& result, const long double& arg)
  1514. {
  1515. mpfr_set_ld(mpc_realref(result.data()), arg, GMP_RNDN);
  1516. }
  1517. #if defined(BOOST_HAS_LONG_LONG) && defined(_MPFR_H_HAVE_INTMAX_T)
  1518. template <unsigned Digits10>
  1519. inline void eval_set_real(mpc_complex_backend<Digits10>& result, const unsigned long long& arg)
  1520. {
  1521. mpfr_set_uj(mpc_realref(result.data()), arg, GMP_RNDN);
  1522. }
  1523. template <unsigned Digits10>
  1524. inline void eval_set_real(mpc_complex_backend<Digits10>& result, const long long& arg)
  1525. {
  1526. mpfr_set_sj(mpc_realref(result.data()), arg, GMP_RNDN);
  1527. }
  1528. #endif
  1529. template <unsigned Digits10>
  1530. inline void eval_set_imag(mpc_complex_backend<Digits10>& result, const gmp_int& arg)
  1531. {
  1532. mpfr_set_z(mpc_imagref(result.data()), arg.data(), GMP_RNDN);
  1533. }
  1534. template <unsigned Digits10>
  1535. inline void eval_set_imag(mpc_complex_backend<Digits10>& result, const gmp_rational& arg)
  1536. {
  1537. mpfr_set_q(mpc_imagref(result.data()), arg.data(), GMP_RNDN);
  1538. }
  1539. template <unsigned Digits10>
  1540. inline void eval_set_imag(mpc_complex_backend<Digits10>& result, const unsigned& arg)
  1541. {
  1542. mpfr_set_ui(mpc_imagref(result.data()), arg, GMP_RNDN);
  1543. }
  1544. template <unsigned Digits10>
  1545. inline void eval_set_imag(mpc_complex_backend<Digits10>& result, const unsigned long& arg)
  1546. {
  1547. mpfr_set_ui(mpc_imagref(result.data()), arg, GMP_RNDN);
  1548. }
  1549. template <unsigned Digits10>
  1550. inline void eval_set_imag(mpc_complex_backend<Digits10>& result, const int& arg)
  1551. {
  1552. mpfr_set_si(mpc_imagref(result.data()), arg, GMP_RNDN);
  1553. }
  1554. template <unsigned Digits10>
  1555. inline void eval_set_imag(mpc_complex_backend<Digits10>& result, const long& arg)
  1556. {
  1557. mpfr_set_si(mpc_imagref(result.data()), arg, GMP_RNDN);
  1558. }
  1559. template <unsigned Digits10>
  1560. inline void eval_set_imag(mpc_complex_backend<Digits10>& result, const float& arg)
  1561. {
  1562. mpfr_set_flt(mpc_imagref(result.data()), arg, GMP_RNDN);
  1563. }
  1564. template <unsigned Digits10>
  1565. inline void eval_set_imag(mpc_complex_backend<Digits10>& result, const double& arg)
  1566. {
  1567. mpfr_set_d(mpc_imagref(result.data()), arg, GMP_RNDN);
  1568. }
  1569. template <unsigned Digits10>
  1570. inline void eval_set_imag(mpc_complex_backend<Digits10>& result, const long double& arg)
  1571. {
  1572. mpfr_set_ld(mpc_imagref(result.data()), arg, GMP_RNDN);
  1573. }
  1574. #if defined(BOOST_HAS_LONG_LONG) && defined(_MPFR_H_HAVE_INTMAX_T)
  1575. template <unsigned Digits10>
  1576. inline void eval_set_imag(mpc_complex_backend<Digits10>& result, const unsigned long long& arg)
  1577. {
  1578. mpfr_set_uj(mpc_imagref(result.data()), arg, GMP_RNDN);
  1579. }
  1580. template <unsigned Digits10>
  1581. inline void eval_set_imag(mpc_complex_backend<Digits10>& result, const long long& arg)
  1582. {
  1583. mpfr_set_sj(mpc_imagref(result.data()), arg, GMP_RNDN);
  1584. }
  1585. #endif
  1586. template <unsigned Digits10>
  1587. inline std::size_t hash_value(const mpc_complex_backend<Digits10>& val)
  1588. {
  1589. std::size_t result = 0;
  1590. std::size_t len = val.data()[0].re[0]._mpfr_prec / mp_bits_per_limb;
  1591. if (val.data()[0].re[0]._mpfr_prec % mp_bits_per_limb)
  1592. ++len;
  1593. for (std::size_t i = 0; i < len; ++i)
  1594. boost::multiprecision::detail::hash_combine(result, val.data()[0].re[0]._mpfr_d[i]);
  1595. boost::multiprecision::detail::hash_combine(result, val.data()[0].re[0]._mpfr_exp, val.data()[0].re[0]._mpfr_sign);
  1596. len = val.data()[0].im[0]._mpfr_prec / mp_bits_per_limb;
  1597. if (val.data()[0].im[0]._mpfr_prec % mp_bits_per_limb)
  1598. ++len;
  1599. for (std::size_t i = 0; i < len; ++i)
  1600. boost::multiprecision::detail::hash_combine(result, val.data()[0].im[0]._mpfr_d[i]);
  1601. boost::multiprecision::detail::hash_combine(result, val.data()[0].im[0]._mpfr_exp, val.data()[0].im[0]._mpfr_sign);
  1602. return result;
  1603. }
  1604. } // namespace backends
  1605. namespace detail {
  1606. template <>
  1607. struct is_variable_precision<backends::mpc_complex_backend<0> > : public std::integral_constant<bool, true>
  1608. {};
  1609. } // namespace detail
  1610. template <>
  1611. struct number_category<detail::canonical<mpc_t, backends::mpc_complex_backend<0> >::type> : public std::integral_constant<int, number_kind_floating_point>
  1612. {};
  1613. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  1614. struct component_type<number<mpc_complex_backend<Digits10>, ExpressionTemplates> >
  1615. {
  1616. using type = number<mpfr_float_backend<Digits10>, ExpressionTemplates>;
  1617. };
  1618. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  1619. struct component_type<number<backends::logged_adaptor<mpc_complex_backend<Digits10> >, ExpressionTemplates> >
  1620. {
  1621. using type = number<mpfr_float_backend<Digits10>, ExpressionTemplates>;
  1622. };
  1623. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  1624. struct component_type<number<backends::debug_adaptor<mpc_complex_backend<Digits10> >, ExpressionTemplates> >
  1625. {
  1626. using type = number<backends::debug_adaptor<mpfr_float_backend<Digits10> >, ExpressionTemplates>;
  1627. };
  1628. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  1629. struct complex_result_from_scalar<number<mpfr_float_backend<Digits10>, ExpressionTemplates> >
  1630. {
  1631. using type = number<mpc_complex_backend<Digits10>, ExpressionTemplates>;
  1632. };
  1633. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  1634. struct complex_result_from_scalar<number<backends::logged_adaptor<mpfr_float_backend<Digits10>>, ExpressionTemplates> >
  1635. {
  1636. using type = number<mpc_complex_backend<Digits10>, ExpressionTemplates>;
  1637. };
  1638. template <unsigned Digits10, expression_template_option ExpressionTemplates>
  1639. struct complex_result_from_scalar<number<backends::debug_adaptor<mpfr_float_backend<Digits10>>, ExpressionTemplates> >
  1640. {
  1641. using type = number<backends::debug_adaptor<mpc_complex_backend<Digits10> >, ExpressionTemplates>;
  1642. };
  1643. }
  1644. } // namespace boost::multiprecision
  1645. #endif