number.hpp 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795
  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_MATH_EXTENDED_REAL_HPP
  6. #define BOOST_MATH_EXTENDED_REAL_HPP
  7. #include <boost/cstdint.hpp>
  8. #include <boost/mpl/max.hpp>
  9. #include <boost/mpl/plus.hpp>
  10. #include <boost/mpl/or.hpp>
  11. #include <boost/mpl/find_if.hpp>
  12. #include <boost/assert.hpp>
  13. #include <boost/type_traits/remove_pointer.hpp>
  14. #include <boost/type_traits/is_signed.hpp>
  15. #include <boost/type_traits/is_unsigned.hpp>
  16. #include <boost/type_traits/is_floating_point.hpp>
  17. #include <boost/type_traits/is_integral.hpp>
  18. #include <boost/type_traits/make_unsigned.hpp>
  19. #include <boost/throw_exception.hpp>
  20. #include <boost/multiprecision/detail/generic_interconvert.hpp>
  21. #include <boost/multiprecision/detail/number_compare.hpp>
  22. #include <boost/multiprecision/traits/is_restricted_conversion.hpp>
  23. #include <istream> // stream operators
  24. #include <cstdio> // EOF
  25. namespace boost{ namespace multiprecision{
  26. #ifdef BOOST_MSVC
  27. // warning C4127: conditional expression is constant
  28. // warning C4714: function marked as __forceinline not inlined
  29. #pragma warning(push)
  30. #pragma warning(disable:4127 4714)
  31. #endif
  32. template <class Backend, expression_template_option ExpressionTemplates>
  33. class number
  34. {
  35. typedef number<Backend, ExpressionTemplates> self_type;
  36. public:
  37. typedef Backend backend_type;
  38. BOOST_MP_FORCEINLINE BOOST_CONSTEXPR number() BOOST_NOEXCEPT_IF(noexcept(Backend())) {}
  39. BOOST_MP_FORCEINLINE BOOST_CONSTEXPR number(const number& e) BOOST_NOEXCEPT_IF(noexcept(Backend(static_cast<const Backend&>(std::declval<Backend>())))) : m_backend(e.m_backend){}
  40. template <class V>
  41. BOOST_MP_FORCEINLINE number(const V& v, typename boost::enable_if_c<
  42. (boost::is_arithmetic<V>::value || is_same<std::string, V>::value || is_convertible<V, const char*>::value)
  43. && !is_convertible<typename detail::canonical<V, Backend>::type, Backend>::value
  44. && !detail::is_restricted_conversion<typename detail::canonical<V, Backend>::type, Backend>::value
  45. >::type* = 0)
  46. {
  47. m_backend = canonical_value(v);
  48. }
  49. template <class V>
  50. BOOST_MP_FORCEINLINE BOOST_CONSTEXPR number(const V& v, typename boost::enable_if_c<
  51. is_convertible<typename detail::canonical<V, Backend>::type, Backend>::value
  52. && !detail::is_restricted_conversion<typename detail::canonical<V, Backend>::type, Backend>::value
  53. >::type* = 0)
  54. : m_backend(canonical_value(v)) {}
  55. BOOST_MP_FORCEINLINE BOOST_CONSTEXPR number(const number& e, unsigned digits10)
  56. : m_backend(e.m_backend, digits10){}
  57. template <class V>
  58. explicit BOOST_MP_FORCEINLINE number(const V& v, typename boost::enable_if_c<
  59. (boost::is_arithmetic<V>::value || is_same<std::string, V>::value || is_convertible<V, const char*>::value)
  60. && !detail::is_explicitly_convertible<typename detail::canonical<V, Backend>::type, Backend>::value
  61. && detail::is_restricted_conversion<typename detail::canonical<V, Backend>::type, Backend>::value
  62. >::type* = 0)
  63. {
  64. m_backend = canonical_value(v);
  65. }
  66. template <class V>
  67. explicit BOOST_MP_FORCEINLINE BOOST_CONSTEXPR number(const V& v, typename boost::enable_if_c<
  68. detail::is_explicitly_convertible<typename detail::canonical<V, Backend>::type, Backend>::value
  69. && (detail::is_restricted_conversion<typename detail::canonical<V, Backend>::type, Backend>::value
  70. || !is_convertible<typename detail::canonical<V, Backend>::type, Backend>::value)
  71. >::type* = 0)
  72. : m_backend(canonical_value(v)) {}
  73. /*
  74. //
  75. // This conflicts with component based initialization (for rational and complex types)
  76. // which is arguably more useful. Disabled for now.
  77. //
  78. template <class V>
  79. number(V v, unsigned digits10, typename boost::enable_if<mpl::or_<boost::is_arithmetic<V>, is_same<std::string, V>, is_convertible<V, const char*> > >::type* dummy1 = 0)
  80. {
  81. m_backend.precision(digits10);
  82. m_backend = canonical_value(v);
  83. }
  84. */
  85. template<expression_template_option ET>
  86. BOOST_MP_FORCEINLINE BOOST_CONSTEXPR number(const number<Backend, ET>& val)
  87. BOOST_NOEXCEPT_IF(noexcept(Backend(static_cast<const Backend&>(std::declval<Backend>())))) : m_backend(val.backend()) {}
  88. template <class Other, expression_template_option ET>
  89. BOOST_MP_FORCEINLINE number(const number<Other, ET>& val,
  90. typename boost::enable_if_c<(boost::is_convertible<Other, Backend>::value && !detail::is_restricted_conversion<Other, Backend>::value)>::type* = 0)
  91. BOOST_NOEXCEPT_IF(noexcept(Backend(static_cast<const Other&>(std::declval<Other>()))))
  92. : m_backend(val.backend()) {}
  93. template <class Other, expression_template_option ET>
  94. explicit number(const number<Other, ET>& val, typename boost::enable_if_c<
  95. (!detail::is_explicitly_convertible<Other, Backend>::value)
  96. >::type* = 0)
  97. {
  98. //
  99. // Attempt a generic interconvertion:
  100. //
  101. detail::generic_interconvert(backend(), val.backend(), number_category<Backend>(), number_category<Other>());
  102. }
  103. template <class Other, expression_template_option ET>
  104. explicit BOOST_MP_FORCEINLINE number(const number<Other, ET>& val, typename boost::enable_if_c<
  105. (detail::is_explicitly_convertible<Other, Backend>::value
  106. && (detail::is_restricted_conversion<Other, Backend>::value || !boost::is_convertible<Other, Backend>::value))
  107. >::type* = 0) BOOST_NOEXCEPT_IF(noexcept(Backend(static_cast<const Other&>(std::declval<Other>()))))
  108. : m_backend(val.backend()) {}
  109. template <class V>
  110. BOOST_MP_FORCEINLINE number(V v1, V v2, typename boost::enable_if<mpl::or_<boost::is_arithmetic<V>, is_same<std::string, V>, is_convertible<V, const char*> > >::type* = 0)
  111. {
  112. using default_ops::assign_components;
  113. assign_components(m_backend, canonical_value(v1), canonical_value(v2));
  114. }
  115. template <class Other, expression_template_option ET>
  116. BOOST_MP_FORCEINLINE number(const number<Other, ET>& v1, const number<Other, ET>& v2, typename boost::enable_if<boost::is_convertible<Other, Backend> >::type* = 0)
  117. {
  118. using default_ops::assign_components;
  119. assign_components(m_backend, v1.backend(), v2.backend());
  120. }
  121. template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
  122. typename boost::enable_if<is_convertible<typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type, self_type>, number&>::type operator=(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& e)
  123. {
  124. typedef typename is_same<number, typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type>::type tag_type;
  125. do_assign(e, tag_type());
  126. return *this;
  127. }
  128. template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
  129. number& assign(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& e)
  130. {
  131. typedef typename is_same<number, typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type>::type tag_type;
  132. do_assign(e, tag_type());
  133. return *this;
  134. }
  135. BOOST_MP_FORCEINLINE number& operator=(const number& e)
  136. BOOST_NOEXCEPT_IF(noexcept(std::declval<Backend>() = static_cast<const Backend&>(std::declval<Backend>())))
  137. {
  138. m_backend = e.m_backend;
  139. return *this;
  140. }
  141. template <class V>
  142. BOOST_MP_FORCEINLINE typename boost::enable_if<is_convertible<V, self_type>, number<Backend, ExpressionTemplates>& >::type
  143. operator=(const V& v)
  144. BOOST_NOEXCEPT_IF(noexcept(std::declval<Backend>() = static_cast<typename boost::multiprecision::detail::canonical<V, Backend>::type const&>(std::declval<typename boost::multiprecision::detail::canonical<V, Backend>::type>())))
  145. {
  146. m_backend = canonical_value(v);
  147. return *this;
  148. }
  149. template <class V>
  150. BOOST_MP_FORCEINLINE number<Backend, ExpressionTemplates>& assign(const V& v)
  151. BOOST_NOEXCEPT_IF(noexcept(std::declval<Backend>() = static_cast<typename boost::multiprecision::detail::canonical<V, Backend>::type const&>(std::declval<typename boost::multiprecision::detail::canonical<V, Backend>::type>())))
  152. {
  153. m_backend = canonical_value(v);
  154. return *this;
  155. }
  156. template <class Other, expression_template_option ET>
  157. typename boost::disable_if<boost::multiprecision::detail::is_explicitly_convertible<Other, Backend>, number<Backend, ExpressionTemplates>& >::type
  158. assign(const number<Other, ET>& v)
  159. {
  160. //
  161. // Attempt a generic interconvertion:
  162. //
  163. detail::generic_interconvert(backend(), v.backend(), number_category<Backend>(), number_category<Other>());
  164. return *this;
  165. }
  166. template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
  167. number(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& e, typename boost::enable_if_c<is_convertible<typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type, self_type>::value>::type* = 0)
  168. {
  169. *this = e;
  170. }
  171. template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
  172. explicit number(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& e,
  173. typename boost::enable_if_c<!is_convertible<typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type, self_type>::value
  174. && boost::multiprecision::detail::is_explicitly_convertible<typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type, self_type>::value>::type* = 0)
  175. {
  176. assign(e);
  177. }
  178. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  179. BOOST_MP_FORCEINLINE BOOST_CONSTEXPR number(number&& r)
  180. BOOST_NOEXCEPT_IF(noexcept(Backend(std::declval<Backend>())))
  181. : m_backend(static_cast<Backend&&>(r.m_backend)){}
  182. BOOST_MP_FORCEINLINE number& operator=(number&& r) BOOST_NOEXCEPT
  183. {
  184. m_backend = static_cast<Backend&&>(r.m_backend);
  185. return *this;
  186. }
  187. #endif
  188. number& operator+=(const self_type& val)
  189. {
  190. do_add(detail::expression<detail::terminal, self_type>(val), detail::terminal());
  191. return *this;
  192. }
  193. template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
  194. number& operator+=(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& e)
  195. {
  196. // Create a copy if e contains this, but not if we're just doing a
  197. // x += x
  198. if(contains_self(e) && !is_self(e))
  199. {
  200. self_type temp(e);
  201. do_add(detail::expression<detail::terminal, self_type>(temp), detail::terminal());
  202. }
  203. else
  204. {
  205. do_add(e, tag());
  206. }
  207. return *this;
  208. }
  209. template <class Arg1, class Arg2, class Arg3, class Arg4>
  210. number& operator+=(const detail::expression<detail::multiply_immediates, Arg1, Arg2, Arg3, Arg4>& e)
  211. {
  212. //
  213. // Fused multiply-add:
  214. //
  215. using default_ops::eval_multiply_add;
  216. eval_multiply_add(m_backend, canonical_value(e.left_ref()), canonical_value(e.right_ref()));
  217. return *this;
  218. }
  219. template <class V>
  220. typename boost::enable_if<boost::is_convertible<V, self_type>, number<Backend, ExpressionTemplates>& >::type
  221. operator+=(const V& v)
  222. {
  223. using default_ops::eval_add;
  224. eval_add(m_backend, canonical_value(v));
  225. return *this;
  226. }
  227. number& operator-=(const self_type& val)
  228. {
  229. do_subtract(detail::expression<detail::terminal, self_type>(val), detail::terminal());
  230. return *this;
  231. }
  232. template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
  233. number& operator-=(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& e)
  234. {
  235. // Create a copy if e contains this:
  236. if(contains_self(e))
  237. {
  238. self_type temp(e);
  239. do_subtract(detail::expression<detail::terminal, self_type>(temp), detail::terminal());
  240. }
  241. else
  242. {
  243. do_subtract(e, typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::tag_type());
  244. }
  245. return *this;
  246. }
  247. template <class V>
  248. typename boost::enable_if<boost::is_convertible<V, self_type>, number<Backend, ExpressionTemplates>& >::type
  249. operator-=(const V& v)
  250. {
  251. using default_ops::eval_subtract;
  252. eval_subtract(m_backend, canonical_value(v));
  253. return *this;
  254. }
  255. template <class Arg1, class Arg2, class Arg3, class Arg4>
  256. number& operator-=(const detail::expression<detail::multiply_immediates, Arg1, Arg2, Arg3, Arg4>& e)
  257. {
  258. //
  259. // Fused multiply-subtract:
  260. //
  261. using default_ops::eval_multiply_subtract;
  262. eval_multiply_subtract(m_backend, canonical_value(e.left_ref()), canonical_value(e.right_ref()));
  263. return *this;
  264. }
  265. number& operator *= (const self_type& e)
  266. {
  267. do_multiplies(detail::expression<detail::terminal, self_type>(e), detail::terminal());
  268. return *this;
  269. }
  270. template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
  271. number& operator*=(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& e)
  272. {
  273. // Create a temporary if the RHS references *this, but not
  274. // if we're just doing an x *= x;
  275. if(contains_self(e) && !is_self(e))
  276. {
  277. self_type temp(e);
  278. do_multiplies(detail::expression<detail::terminal, self_type>(temp), detail::terminal());
  279. }
  280. else
  281. {
  282. do_multiplies(e, typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::tag_type());
  283. }
  284. return *this;
  285. }
  286. template <class V>
  287. typename boost::enable_if<boost::is_convertible<V, self_type>, number<Backend, ExpressionTemplates>& >::type
  288. operator*=(const V& v)
  289. {
  290. using default_ops::eval_multiply;
  291. eval_multiply(m_backend, canonical_value(v));
  292. return *this;
  293. }
  294. number& operator%=(const self_type& e)
  295. {
  296. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The modulus operation is only valid for integer types");
  297. do_modulus(detail::expression<detail::terminal, self_type>(e), detail::terminal());
  298. return *this;
  299. }
  300. template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
  301. number& operator%=(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& e)
  302. {
  303. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The modulus operation is only valid for integer types");
  304. // Create a temporary if the RHS references *this:
  305. if(contains_self(e))
  306. {
  307. self_type temp(e);
  308. do_modulus(detail::expression<detail::terminal, self_type>(temp), detail::terminal());
  309. }
  310. else
  311. {
  312. do_modulus(e, typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::tag_type());
  313. }
  314. return *this;
  315. }
  316. template <class V>
  317. typename boost::enable_if<boost::is_convertible<V, self_type>, number<Backend, ExpressionTemplates>& >::type
  318. operator%=(const V& v)
  319. {
  320. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The modulus operation is only valid for integer types");
  321. using default_ops::eval_modulus;
  322. eval_modulus(m_backend, canonical_value(v));
  323. return *this;
  324. }
  325. //
  326. // These operators are *not* proto-ized.
  327. // The issue is that the increment/decrement must happen
  328. // even if the result of the operator *is never used*.
  329. // Possibly we could modify our expression wrapper to
  330. // execute the increment/decrement on destruction, but
  331. // correct implementation will be tricky, so defered for now...
  332. //
  333. BOOST_MP_FORCEINLINE number& operator++()
  334. {
  335. using default_ops::eval_increment;
  336. eval_increment(m_backend);
  337. return *this;
  338. }
  339. BOOST_MP_FORCEINLINE number& operator--()
  340. {
  341. using default_ops::eval_decrement;
  342. eval_decrement(m_backend);
  343. return *this;
  344. }
  345. inline number operator++(int)
  346. {
  347. using default_ops::eval_increment;
  348. self_type temp(*this);
  349. eval_increment(m_backend);
  350. return BOOST_MP_MOVE(temp);
  351. }
  352. inline number operator--(int)
  353. {
  354. using default_ops::eval_decrement;
  355. self_type temp(*this);
  356. eval_decrement(m_backend);
  357. return BOOST_MP_MOVE(temp);
  358. }
  359. template <class V>
  360. BOOST_MP_FORCEINLINE typename boost::enable_if<is_integral<V>, number&>::type operator <<= (V val)
  361. {
  362. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The left-shift operation is only valid for integer types");
  363. detail::check_shift_range(val, mpl::bool_<(sizeof(V) > sizeof(std::size_t))>(), is_signed<V>());
  364. eval_left_shift(m_backend, static_cast<std::size_t>(canonical_value(val)));
  365. return *this;
  366. }
  367. template <class V>
  368. BOOST_MP_FORCEINLINE typename boost::enable_if<is_integral<V>, number&>::type operator >>= (V val)
  369. {
  370. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The right-shift operation is only valid for integer types");
  371. detail::check_shift_range(val, mpl::bool_<(sizeof(V) > sizeof(std::size_t))>(), is_signed<V>());
  372. eval_right_shift(m_backend, static_cast<std::size_t>(canonical_value(val)));
  373. return *this;
  374. }
  375. BOOST_MP_FORCEINLINE number& operator /= (const self_type& e)
  376. {
  377. do_divide(detail::expression<detail::terminal, self_type>(e), detail::terminal());
  378. return *this;
  379. }
  380. template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
  381. number& operator/=(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& e)
  382. {
  383. // Create a temporary if the RHS references *this:
  384. if(contains_self(e))
  385. {
  386. self_type temp(e);
  387. do_divide(detail::expression<detail::terminal, self_type>(temp), detail::terminal());
  388. }
  389. else
  390. {
  391. do_divide(e, typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::tag_type());
  392. }
  393. return *this;
  394. }
  395. template <class V>
  396. BOOST_MP_FORCEINLINE typename boost::enable_if<boost::is_convertible<V, self_type>, number<Backend, ExpressionTemplates>& >::type
  397. operator/=(const V& v)
  398. {
  399. using default_ops::eval_divide;
  400. eval_divide(m_backend, canonical_value(v));
  401. return *this;
  402. }
  403. BOOST_MP_FORCEINLINE number& operator&=(const self_type& e)
  404. {
  405. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise & operation is only valid for integer types");
  406. do_bitwise_and(detail::expression<detail::terminal, self_type>(e), detail::terminal());
  407. return *this;
  408. }
  409. template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
  410. number& operator&=(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& e)
  411. {
  412. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise & operation is only valid for integer types");
  413. // Create a temporary if the RHS references *this, but not
  414. // if we're just doing an x &= x;
  415. if(contains_self(e) && !is_self(e))
  416. {
  417. self_type temp(e);
  418. do_bitwise_and(detail::expression<detail::terminal, self_type>(temp), detail::terminal());
  419. }
  420. else
  421. {
  422. do_bitwise_and(e, typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::tag_type());
  423. }
  424. return *this;
  425. }
  426. template <class V>
  427. BOOST_MP_FORCEINLINE typename boost::enable_if<boost::is_convertible<V, self_type>, number<Backend, ExpressionTemplates>& >::type
  428. operator&=(const V& v)
  429. {
  430. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise & operation is only valid for integer types");
  431. using default_ops::eval_bitwise_and;
  432. eval_bitwise_and(m_backend, canonical_value(v));
  433. return *this;
  434. }
  435. BOOST_MP_FORCEINLINE number& operator|=(const self_type& e)
  436. {
  437. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise | operation is only valid for integer types");
  438. do_bitwise_or(detail::expression<detail::terminal, self_type>(e), detail::terminal());
  439. return *this;
  440. }
  441. template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
  442. number& operator|=(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& e)
  443. {
  444. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise | operation is only valid for integer types");
  445. // Create a temporary if the RHS references *this, but not
  446. // if we're just doing an x |= x;
  447. if(contains_self(e) && !is_self(e))
  448. {
  449. self_type temp(e);
  450. do_bitwise_or(detail::expression<detail::terminal, self_type>(temp), detail::terminal());
  451. }
  452. else
  453. {
  454. do_bitwise_or(e, typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::tag_type());
  455. }
  456. return *this;
  457. }
  458. template <class V>
  459. BOOST_MP_FORCEINLINE typename boost::enable_if<boost::is_convertible<V, self_type>, number<Backend, ExpressionTemplates>& >::type
  460. operator|=(const V& v)
  461. {
  462. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise | operation is only valid for integer types");
  463. using default_ops::eval_bitwise_or;
  464. eval_bitwise_or(m_backend, canonical_value(v));
  465. return *this;
  466. }
  467. BOOST_MP_FORCEINLINE number& operator^=(const self_type& e)
  468. {
  469. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise ^ operation is only valid for integer types");
  470. do_bitwise_xor(detail::expression<detail::terminal, self_type>(e), detail::terminal());
  471. return *this;
  472. }
  473. template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
  474. number& operator^=(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& e)
  475. {
  476. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise ^ operation is only valid for integer types");
  477. if(contains_self(e))
  478. {
  479. self_type temp(e);
  480. do_bitwise_xor(detail::expression<detail::terminal, self_type>(temp), detail::terminal());
  481. }
  482. else
  483. {
  484. do_bitwise_xor(e, typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::tag_type());
  485. }
  486. return *this;
  487. }
  488. template <class V>
  489. BOOST_MP_FORCEINLINE typename boost::enable_if<boost::is_convertible<V, self_type>, number<Backend, ExpressionTemplates>& >::type
  490. operator^=(const V& v)
  491. {
  492. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise ^ operation is only valid for integer types");
  493. using default_ops::eval_bitwise_xor;
  494. eval_bitwise_xor(m_backend, canonical_value(v));
  495. return *this;
  496. }
  497. //
  498. // swap:
  499. //
  500. BOOST_MP_FORCEINLINE void swap(self_type& other) BOOST_NOEXCEPT
  501. {
  502. m_backend.swap(other.backend());
  503. }
  504. //
  505. // Zero and sign:
  506. //
  507. BOOST_MP_FORCEINLINE bool is_zero()const
  508. {
  509. using default_ops::eval_is_zero;
  510. return eval_is_zero(m_backend);
  511. }
  512. BOOST_MP_FORCEINLINE int sign()const
  513. {
  514. using default_ops::eval_get_sign;
  515. return eval_get_sign(m_backend);
  516. }
  517. //
  518. // String conversion functions:
  519. //
  520. std::string str(std::streamsize digits = 0, std::ios_base::fmtflags f = std::ios_base::fmtflags(0))const
  521. {
  522. return m_backend.str(digits, f);
  523. }
  524. template<class Archive>
  525. void serialize(Archive & ar, const unsigned int /*version*/)
  526. {
  527. ar & m_backend;
  528. }
  529. private:
  530. template <class T>
  531. void convert_to_imp(T* result)const
  532. {
  533. using default_ops::eval_convert_to;
  534. eval_convert_to(result, m_backend);
  535. }
  536. template <class B2, expression_template_option ET>
  537. void convert_to_imp(number<B2, ET>* result)const
  538. {
  539. result->assign(*this);
  540. }
  541. void convert_to_imp(std::string* result)const
  542. {
  543. *result = this->str();
  544. }
  545. public:
  546. template <class T>
  547. T convert_to()const
  548. {
  549. T result;
  550. convert_to_imp(&result);
  551. return result;
  552. }
  553. //
  554. // Use in boolean context, and explicit conversion operators:
  555. //
  556. #ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
  557. # if defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ < 7)
  558. //
  559. // Horrible workaround for gcc-4.6.x which always prefers the template
  560. // operator bool() rather than the non-template operator when converting to
  561. // an arithmetic type:
  562. //
  563. template <class T, typename boost::enable_if<is_same<T, bool>, int>::type = 0>
  564. explicit operator T ()const
  565. {
  566. using default_ops::eval_is_zero;
  567. return !eval_is_zero(backend());
  568. }
  569. template <class T, typename boost::disable_if_c<is_same<T, bool>::value || is_void<T>::value, int>::type = 0>
  570. explicit operator T ()const
  571. {
  572. return this->template convert_to<T>();
  573. }
  574. # else
  575. template <class T>
  576. explicit operator T()const
  577. {
  578. return this->template convert_to<T>();
  579. }
  580. BOOST_MP_FORCEINLINE explicit operator bool()const
  581. {
  582. return !is_zero();
  583. }
  584. explicit operator void()const {}
  585. # endif
  586. #else
  587. typedef bool (self_type::*unmentionable_type)()const;
  588. BOOST_MP_FORCEINLINE operator unmentionable_type()const
  589. {
  590. return is_zero() ? 0 : &self_type::is_zero;
  591. }
  592. #endif
  593. //
  594. // Default precision:
  595. //
  596. static unsigned default_precision() BOOST_NOEXCEPT
  597. {
  598. return Backend::default_precision();
  599. }
  600. static void default_precision(unsigned digits10)
  601. {
  602. Backend::default_precision(digits10);
  603. }
  604. unsigned precision()const BOOST_NOEXCEPT
  605. {
  606. return m_backend.precision();
  607. }
  608. void precision(unsigned digits10)
  609. {
  610. m_backend.precision(digits10);
  611. }
  612. //
  613. // Comparison:
  614. //
  615. BOOST_MP_FORCEINLINE int compare(const number<Backend, ExpressionTemplates>& o)const
  616. BOOST_NOEXCEPT_IF(noexcept(std::declval<Backend>().compare(std::declval<Backend>())))
  617. {
  618. return m_backend.compare(o.m_backend);
  619. }
  620. template <class V>
  621. BOOST_MP_FORCEINLINE typename boost::enable_if<is_arithmetic<V>, int>::type compare(const V& o)const
  622. {
  623. using default_ops::eval_get_sign;
  624. if(o == 0)
  625. return eval_get_sign(m_backend);
  626. return m_backend.compare(canonical_value(o));
  627. }
  628. BOOST_MP_FORCEINLINE Backend& backend() BOOST_NOEXCEPT
  629. {
  630. return m_backend;
  631. }
  632. BOOST_MP_FORCEINLINE BOOST_CONSTEXPR const Backend& backend()const BOOST_NOEXCEPT
  633. {
  634. return m_backend;
  635. }
  636. private:
  637. template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
  638. void do_assign(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& e, const mpl::true_&)
  639. {
  640. do_assign(e, tag());
  641. }
  642. template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
  643. void do_assign(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& e, const mpl::false_&)
  644. {
  645. // The result of the expression isn't the same type as this -
  646. // create a temporary result and assign it to *this:
  647. typedef typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type temp_type;
  648. temp_type t(e);
  649. this->assign(t);
  650. }
  651. template <class Exp>
  652. void do_assign(const Exp& e, const detail::add_immediates&)
  653. {
  654. using default_ops::eval_add;
  655. eval_add(m_backend, canonical_value(e.left().value()), canonical_value(e.right().value()));
  656. }
  657. template <class Exp>
  658. void do_assign(const Exp& e, const detail::subtract_immediates&)
  659. {
  660. using default_ops::eval_subtract;
  661. eval_subtract(m_backend, canonical_value(e.left().value()), canonical_value(e.right().value()));
  662. }
  663. template <class Exp>
  664. void do_assign(const Exp& e, const detail::multiply_immediates&)
  665. {
  666. using default_ops::eval_multiply;
  667. eval_multiply(m_backend, canonical_value(e.left().value()), canonical_value(e.right().value()));
  668. }
  669. template <class Exp>
  670. void do_assign(const Exp& e, const detail::multiply_add&)
  671. {
  672. using default_ops::eval_multiply_add;
  673. eval_multiply_add(m_backend, canonical_value(e.left().value()), canonical_value(e.middle().value()), canonical_value(e.right().value()));
  674. }
  675. template <class Exp>
  676. void do_assign(const Exp& e, const detail::multiply_subtract&)
  677. {
  678. using default_ops::eval_multiply_subtract;
  679. eval_multiply_subtract(m_backend, canonical_value(e.left().value()), canonical_value(e.middle().value()), canonical_value(e.right().value()));
  680. }
  681. template <class Exp>
  682. void do_assign(const Exp& e, const detail::divide_immediates&)
  683. {
  684. using default_ops::eval_divide;
  685. eval_divide(m_backend, canonical_value(e.left().value()), canonical_value(e.right().value()));
  686. }
  687. template <class Exp>
  688. void do_assign(const Exp& e, const detail::negate&)
  689. {
  690. typedef typename Exp::left_type left_type;
  691. do_assign(e.left(), typename left_type::tag_type());
  692. m_backend.negate();
  693. }
  694. template <class Exp>
  695. void do_assign(const Exp& e, const detail::plus&)
  696. {
  697. typedef typename Exp::left_type left_type;
  698. typedef typename Exp::right_type right_type;
  699. static int const left_depth = left_type::depth;
  700. static int const right_depth = right_type::depth;
  701. bool bl = contains_self(e.left());
  702. bool br = contains_self(e.right());
  703. if(bl && is_self(e.left()))
  704. {
  705. // Ignore the left node, it's *this, just add the right:
  706. do_add(e.right(), typename right_type::tag_type());
  707. }
  708. else if(br && is_self(e.right()))
  709. {
  710. // Ignore the right node, it's *this, just add the left:
  711. do_add(e.left(), typename left_type::tag_type());
  712. }
  713. else if(bl && br)
  714. {
  715. self_type temp(e);
  716. temp.m_backend.swap(this->m_backend);
  717. }
  718. else if(!br && (bl || (left_depth >= right_depth)))
  719. { // br is always false, but if bl is true we must take the this branch:
  720. do_assign(e.left(), typename left_type::tag_type());
  721. do_add(e.right(), typename right_type::tag_type());
  722. }
  723. else
  724. {
  725. do_assign(e.right(), typename right_type::tag_type());
  726. do_add(e.left(), typename left_type::tag_type());
  727. }
  728. }
  729. template <class Exp>
  730. void do_assign(const Exp& e, const detail::minus&)
  731. {
  732. typedef typename Exp::left_type left_type;
  733. typedef typename Exp::right_type right_type;
  734. static int const left_depth = left_type::depth;
  735. static int const right_depth = right_type::depth;
  736. bool bl = contains_self(e.left());
  737. bool br = contains_self(e.right());
  738. if(bl && is_self(e.left()))
  739. {
  740. // Ignore the left node, it's *this, just subtract the right:
  741. do_subtract(e.right(), typename right_type::tag_type());
  742. }
  743. else if(br && is_self(e.right()))
  744. {
  745. // Ignore the right node, it's *this, just subtract the left and negate the result:
  746. do_subtract(e.left(), typename left_type::tag_type());
  747. m_backend.negate();
  748. }
  749. else if(bl && br)
  750. {
  751. self_type temp(e);
  752. temp.m_backend.swap(this->m_backend);
  753. }
  754. else if(!br && (bl || (left_depth >= right_depth)))
  755. { // br is always false, but if bl is true we must take the this branch:
  756. do_assign(e.left(), typename left_type::tag_type());
  757. do_subtract(e.right(), typename right_type::tag_type());
  758. }
  759. else
  760. {
  761. do_assign(e.right(), typename right_type::tag_type());
  762. do_subtract(e.left(), typename left_type::tag_type());
  763. m_backend.negate();
  764. }
  765. }
  766. template <class Exp>
  767. void do_assign(const Exp& e, const detail::multiplies&)
  768. {
  769. typedef typename Exp::left_type left_type;
  770. typedef typename Exp::right_type right_type;
  771. static int const left_depth = left_type::depth;
  772. static int const right_depth = right_type::depth;
  773. bool bl = contains_self(e.left());
  774. bool br = contains_self(e.right());
  775. if(bl && is_self(e.left()))
  776. {
  777. // Ignore the left node, it's *this, just add the right:
  778. do_multiplies(e.right(), typename right_type::tag_type());
  779. }
  780. else if(br && is_self(e.right()))
  781. {
  782. // Ignore the right node, it's *this, just add the left:
  783. do_multiplies(e.left(), typename left_type::tag_type());
  784. }
  785. else if(bl && br)
  786. {
  787. self_type temp(e);
  788. temp.m_backend.swap(this->m_backend);
  789. }
  790. else if(!br && (bl || (left_depth >= right_depth)))
  791. { // br is always false, but if bl is true we must take the this branch:
  792. do_assign(e.left(), typename left_type::tag_type());
  793. do_multiplies(e.right(), typename right_type::tag_type());
  794. }
  795. else
  796. {
  797. do_assign(e.right(), typename right_type::tag_type());
  798. do_multiplies(e.left(), typename left_type::tag_type());
  799. }
  800. }
  801. template <class Exp>
  802. void do_assign(const Exp& e, const detail::divides&)
  803. {
  804. typedef typename Exp::left_type left_type;
  805. typedef typename Exp::right_type right_type;
  806. bool bl = contains_self(e.left());
  807. bool br = contains_self(e.right());
  808. if(bl && is_self(e.left()))
  809. {
  810. // Ignore the left node, it's *this, just add the right:
  811. do_divide(e.right(), typename right_type::tag_type());
  812. }
  813. else if(br)
  814. {
  815. self_type temp(e);
  816. temp.m_backend.swap(this->m_backend);
  817. }
  818. else
  819. {
  820. do_assign(e.left(), typename left_type::tag_type());
  821. do_divide(e.right(), typename right_type::tag_type());
  822. }
  823. }
  824. template <class Exp>
  825. void do_assign(const Exp& e, const detail::modulus&)
  826. {
  827. //
  828. // This operation is only valid for integer backends:
  829. //
  830. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The modulus operation is only valid for integer types");
  831. typedef typename Exp::left_type left_type;
  832. typedef typename Exp::right_type right_type;
  833. bool bl = contains_self(e.left());
  834. bool br = contains_self(e.right());
  835. if(bl && is_self(e.left()))
  836. {
  837. // Ignore the left node, it's *this, just add the right:
  838. do_modulus(e.right(), typename right_type::tag_type());
  839. }
  840. else if(br)
  841. {
  842. self_type temp(e);
  843. temp.m_backend.swap(this->m_backend);
  844. }
  845. else
  846. {
  847. do_assign(e.left(), typename left_type::tag_type());
  848. do_modulus(e.right(), typename right_type::tag_type());
  849. }
  850. }
  851. template <class Exp>
  852. void do_assign(const Exp& e, const detail::modulus_immediates&)
  853. {
  854. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The modulus operation is only valid for integer types");
  855. using default_ops::eval_modulus;
  856. eval_modulus(m_backend, canonical_value(e.left().value()), canonical_value(e.right().value()));
  857. }
  858. template <class Exp>
  859. void do_assign(const Exp& e, const detail::bitwise_and&)
  860. {
  861. //
  862. // This operation is only valid for integer backends:
  863. //
  864. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "Bitwise operations are only valid for integer types");
  865. typedef typename Exp::left_type left_type;
  866. typedef typename Exp::right_type right_type;
  867. static int const left_depth = left_type::depth;
  868. static int const right_depth = right_type::depth;
  869. bool bl = contains_self(e.left());
  870. bool br = contains_self(e.right());
  871. if(bl && is_self(e.left()))
  872. {
  873. // Ignore the left node, it's *this, just add the right:
  874. do_bitwise_and(e.right(), typename right_type::tag_type());
  875. }
  876. else if(br && is_self(e.right()))
  877. {
  878. do_bitwise_and(e.left(), typename left_type::tag_type());
  879. }
  880. else if(!br && (bl || (left_depth >= right_depth)))
  881. {
  882. do_assign(e.left(), typename left_type::tag_type());
  883. do_bitwise_and(e.right(), typename right_type::tag_type());
  884. }
  885. else
  886. {
  887. do_assign(e.right(), typename right_type::tag_type());
  888. do_bitwise_and(e.left(), typename left_type::tag_type());
  889. }
  890. }
  891. template <class Exp>
  892. void do_assign(const Exp& e, const detail::bitwise_and_immediates&)
  893. {
  894. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "Bitwise operations are only valid for integer types");
  895. using default_ops::eval_bitwise_and;
  896. eval_bitwise_and(m_backend, canonical_value(e.left().value()), canonical_value(e.right().value()));
  897. }
  898. template <class Exp>
  899. void do_assign(const Exp& e, const detail::bitwise_or&)
  900. {
  901. //
  902. // This operation is only valid for integer backends:
  903. //
  904. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "Bitwise operations are only valid for integer types");
  905. typedef typename Exp::left_type left_type;
  906. typedef typename Exp::right_type right_type;
  907. static int const left_depth = left_type::depth;
  908. static int const right_depth = right_type::depth;
  909. bool bl = contains_self(e.left());
  910. bool br = contains_self(e.right());
  911. if(bl && is_self(e.left()))
  912. {
  913. // Ignore the left node, it's *this, just add the right:
  914. do_bitwise_or(e.right(), typename right_type::tag_type());
  915. }
  916. else if(br && is_self(e.right()))
  917. {
  918. do_bitwise_or(e.left(), typename left_type::tag_type());
  919. }
  920. else if(!br && (bl || (left_depth >= right_depth)))
  921. {
  922. do_assign(e.left(), typename left_type::tag_type());
  923. do_bitwise_or(e.right(), typename right_type::tag_type());
  924. }
  925. else
  926. {
  927. do_assign(e.right(), typename right_type::tag_type());
  928. do_bitwise_or(e.left(), typename left_type::tag_type());
  929. }
  930. }
  931. template <class Exp>
  932. void do_assign(const Exp& e, const detail::bitwise_or_immediates&)
  933. {
  934. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "Bitwise operations are only valid for integer types");
  935. using default_ops::eval_bitwise_or;
  936. eval_bitwise_or(m_backend, canonical_value(e.left().value()), canonical_value(e.right().value()));
  937. }
  938. template <class Exp>
  939. void do_assign(const Exp& e, const detail::bitwise_xor&)
  940. {
  941. //
  942. // This operation is only valid for integer backends:
  943. //
  944. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "Bitwise operations are only valid for integer types");
  945. typedef typename Exp::left_type left_type;
  946. typedef typename Exp::right_type right_type;
  947. static int const left_depth = left_type::depth;
  948. static int const right_depth = right_type::depth;
  949. bool bl = contains_self(e.left());
  950. bool br = contains_self(e.right());
  951. if(bl && is_self(e.left()))
  952. {
  953. // Ignore the left node, it's *this, just add the right:
  954. do_bitwise_xor(e.right(), typename right_type::tag_type());
  955. }
  956. else if(br && is_self(e.right()))
  957. {
  958. do_bitwise_xor(e.left(), typename left_type::tag_type());
  959. }
  960. else if(!br && (bl || (left_depth >= right_depth)))
  961. {
  962. do_assign(e.left(), typename left_type::tag_type());
  963. do_bitwise_xor(e.right(), typename right_type::tag_type());
  964. }
  965. else
  966. {
  967. do_assign(e.right(), typename right_type::tag_type());
  968. do_bitwise_xor(e.left(), typename left_type::tag_type());
  969. }
  970. }
  971. template <class Exp>
  972. void do_assign(const Exp& e, const detail::bitwise_xor_immediates&)
  973. {
  974. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "Bitwise operations are only valid for integer types");
  975. using default_ops::eval_bitwise_xor;
  976. eval_bitwise_xor(m_backend, canonical_value(e.left().value()), canonical_value(e.right().value()));
  977. }
  978. template <class Exp>
  979. void do_assign(const Exp& e, const detail::terminal&)
  980. {
  981. if(!is_self(e))
  982. {
  983. m_backend = canonical_value(e.value());
  984. }
  985. }
  986. template <class Exp>
  987. void do_assign(const Exp& e, const detail::function&)
  988. {
  989. typedef typename Exp::arity tag_type;
  990. do_assign_function(e, tag_type());
  991. }
  992. template <class Exp>
  993. void do_assign(const Exp& e, const detail::shift_left&)
  994. {
  995. // We can only shift by an integer value, not an arbitrary expression:
  996. typedef typename Exp::left_type left_type;
  997. typedef typename Exp::right_type right_type;
  998. typedef typename right_type::arity right_arity;
  999. BOOST_STATIC_ASSERT_MSG(right_arity::value == 0, "The left shift operator requires an integer value for the shift operand.");
  1000. typedef typename right_type::result_type right_value_type;
  1001. BOOST_STATIC_ASSERT_MSG(is_integral<right_value_type>::value, "The left shift operator requires an integer value for the shift operand.");
  1002. typedef typename left_type::tag_type tag_type;
  1003. do_assign_left_shift(e.left(), canonical_value(e.right().value()), tag_type());
  1004. }
  1005. template <class Exp>
  1006. void do_assign(const Exp& e, const detail::shift_right&)
  1007. {
  1008. // We can only shift by an integer value, not an arbitrary expression:
  1009. typedef typename Exp::left_type left_type;
  1010. typedef typename Exp::right_type right_type;
  1011. typedef typename right_type::arity right_arity;
  1012. BOOST_STATIC_ASSERT_MSG(right_arity::value == 0, "The left shift operator requires an integer value for the shift operand.");
  1013. typedef typename right_type::result_type right_value_type;
  1014. BOOST_STATIC_ASSERT_MSG(is_integral<right_value_type>::value, "The left shift operator requires an integer value for the shift operand.");
  1015. typedef typename left_type::tag_type tag_type;
  1016. do_assign_right_shift(e.left(), canonical_value(e.right().value()), tag_type());
  1017. }
  1018. template <class Exp>
  1019. void do_assign(const Exp& e, const detail::bitwise_complement&)
  1020. {
  1021. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise ~ operation is only valid for integer types");
  1022. using default_ops::eval_complement;
  1023. self_type temp(e.left());
  1024. eval_complement(m_backend, temp.backend());
  1025. }
  1026. template <class Exp>
  1027. void do_assign(const Exp& e, const detail::complement_immediates&)
  1028. {
  1029. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise ~ operation is only valid for integer types");
  1030. using default_ops::eval_complement;
  1031. eval_complement(m_backend, canonical_value(e.left().value()));
  1032. }
  1033. template <class Exp, class Val>
  1034. void do_assign_right_shift(const Exp& e, const Val& val, const detail::terminal&)
  1035. {
  1036. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The right shift operation is only valid for integer types");
  1037. using default_ops::eval_right_shift;
  1038. detail::check_shift_range(val, mpl::bool_<(sizeof(Val) > sizeof(std::size_t))>(), is_signed<Val>());
  1039. eval_right_shift(m_backend, canonical_value(e.value()), static_cast<std::size_t>(val));
  1040. }
  1041. template <class Exp, class Val>
  1042. void do_assign_left_shift(const Exp& e, const Val& val, const detail::terminal&)
  1043. {
  1044. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The left shift operation is only valid for integer types");
  1045. using default_ops::eval_left_shift;
  1046. detail::check_shift_range(val, mpl::bool_<(sizeof(Val) > sizeof(std::size_t))>(), is_signed<Val>());
  1047. eval_left_shift(m_backend, canonical_value(e.value()), static_cast<std::size_t>(val));
  1048. }
  1049. template <class Exp, class Val, class Tag>
  1050. void do_assign_right_shift(const Exp& e, const Val& val, const Tag&)
  1051. {
  1052. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The right shift operation is only valid for integer types");
  1053. using default_ops::eval_right_shift;
  1054. self_type temp(e);
  1055. detail::check_shift_range(val, mpl::bool_<(sizeof(Val) > sizeof(std::size_t))>(), is_signed<Val>());
  1056. eval_right_shift(m_backend, temp.backend(), static_cast<std::size_t>(val));
  1057. }
  1058. template <class Exp, class Val, class Tag>
  1059. void do_assign_left_shift(const Exp& e, const Val& val, const Tag&)
  1060. {
  1061. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The left shift operation is only valid for integer types");
  1062. using default_ops::eval_left_shift;
  1063. self_type temp(e);
  1064. detail::check_shift_range(val, mpl::bool_<(sizeof(Val) > sizeof(std::size_t))>(), is_signed<Val>());
  1065. eval_left_shift(m_backend, temp.backend(), static_cast<std::size_t>(val));
  1066. }
  1067. template <class Exp>
  1068. void do_assign_function(const Exp& e, const mpl::int_<1>&)
  1069. {
  1070. e.left().value()(&m_backend);
  1071. }
  1072. template <class Exp>
  1073. void do_assign_function(const Exp& e, const mpl::int_<2>&)
  1074. {
  1075. typedef typename Exp::right_type right_type;
  1076. typedef typename right_type::tag_type tag_type;
  1077. do_assign_function_1(e.left().value(), e.right_ref(), tag_type());
  1078. }
  1079. template <class F, class Exp>
  1080. void do_assign_function_1(const F& f, const Exp& val, const detail::terminal&)
  1081. {
  1082. f(m_backend, function_arg_value(val));
  1083. }
  1084. template <class F, class Exp, class Tag>
  1085. void do_assign_function_1(const F& f, const Exp& val, const Tag&)
  1086. {
  1087. number t(val);
  1088. f(m_backend, t.backend());
  1089. }
  1090. template <class Exp>
  1091. void do_assign_function(const Exp& e, const mpl::int_<3>&)
  1092. {
  1093. typedef typename Exp::middle_type middle_type;
  1094. typedef typename middle_type::tag_type tag_type;
  1095. typedef typename Exp::right_type end_type;
  1096. typedef typename end_type::tag_type end_tag;
  1097. do_assign_function_2(e.left().value(), e.middle_ref(), e.right_ref(), tag_type(), end_tag());
  1098. }
  1099. template <class F, class Exp1, class Exp2>
  1100. void do_assign_function_2(const F& f, const Exp1& val1, const Exp2& val2, const detail::terminal&, const detail::terminal&)
  1101. {
  1102. f(m_backend, function_arg_value(val1), function_arg_value(val2));
  1103. }
  1104. template <class F, class Exp1, class Exp2, class Tag1>
  1105. void do_assign_function_2(const F& f, const Exp1& val1, const Exp2& val2, const Tag1&, const detail::terminal&)
  1106. {
  1107. self_type temp1(val1);
  1108. f(m_backend, BOOST_MP_MOVE(temp1.backend()), function_arg_value(val2));
  1109. }
  1110. template <class F, class Exp1, class Exp2, class Tag2>
  1111. void do_assign_function_2(const F& f, const Exp1& val1, const Exp2& val2, const detail::terminal&, const Tag2&)
  1112. {
  1113. self_type temp2(val2);
  1114. f(m_backend, function_arg_value(val1), BOOST_MP_MOVE(temp2.backend()));
  1115. }
  1116. template <class F, class Exp1, class Exp2, class Tag1, class Tag2>
  1117. void do_assign_function_2(const F& f, const Exp1& val1, const Exp2& val2, const Tag1&, const Tag2&)
  1118. {
  1119. self_type temp1(val1);
  1120. self_type temp2(val2);
  1121. f(m_backend, BOOST_MP_MOVE(temp1.backend()), BOOST_MP_MOVE(temp2.backend()));
  1122. }
  1123. template <class Exp>
  1124. void do_assign_function(const Exp& e, const mpl::int_<4>&)
  1125. {
  1126. typedef typename Exp::left_middle_type left_type;
  1127. typedef typename left_type::tag_type left_tag_type;
  1128. typedef typename Exp::right_middle_type middle_type;
  1129. typedef typename middle_type::tag_type middle_tag_type;
  1130. typedef typename Exp::right_type right_type;
  1131. typedef typename right_type::tag_type right_tag_type;
  1132. do_assign_function_3a(e.left().value(), e.left_middle_ref(), e.right_middle_ref(), e.right_ref(), left_tag_type(), middle_tag_type(), right_tag_type());
  1133. }
  1134. template <class F, class Exp1, class Exp2, class Exp3, class Tag2, class Tag3>
  1135. void do_assign_function_3a(const F& f, const Exp1& val1, const Exp2& val2, const Exp3& val3, const detail::terminal&, const Tag2& t2, const Tag3& t3)
  1136. {
  1137. do_assign_function_3b(f, val1, val2, val3, t2, t3);
  1138. }
  1139. template <class F, class Exp1, class Exp2, class Exp3, class Tag1, class Tag2, class Tag3>
  1140. void do_assign_function_3a(const F& f, const Exp1& val1, const Exp2& val2, const Exp3& val3, const Tag1&, const Tag2& t2, const Tag3& t3)
  1141. {
  1142. number t(val1);
  1143. do_assign_function_3b(f, BOOST_MP_MOVE(t), val2, val3, t2, t3);
  1144. }
  1145. template <class F, class Exp1, class Exp2, class Exp3, class Tag3>
  1146. void do_assign_function_3b(const F& f, const Exp1& val1, const Exp2& val2, const Exp3& val3, const detail::terminal&, const Tag3& t3)
  1147. {
  1148. do_assign_function_3c(f, val1, val2, val3, t3);
  1149. }
  1150. template <class F, class Exp1, class Exp2, class Exp3, class Tag2, class Tag3>
  1151. void do_assign_function_3b(const F& f, const Exp1& val1, const Exp2& val2, const Exp3& val3, const Tag2& /*t2*/, const Tag3& t3)
  1152. {
  1153. number t(val2);
  1154. do_assign_function_3c(f, val1, BOOST_MP_MOVE(t), val3, t3);
  1155. }
  1156. template <class F, class Exp1, class Exp2, class Exp3>
  1157. void do_assign_function_3c(const F& f, const Exp1& val1, const Exp2& val2, const Exp3& val3, const detail::terminal&)
  1158. {
  1159. f(m_backend, function_arg_value(val1), function_arg_value(val2), function_arg_value(val3));
  1160. }
  1161. template <class F, class Exp1, class Exp2, class Exp3, class Tag3>
  1162. void do_assign_function_3c(const F& f, const Exp1& val1, const Exp2& val2, const Exp3& val3, const Tag3& /*t3*/)
  1163. {
  1164. number t(val3);
  1165. do_assign_function_3c(f, val1, val2, BOOST_MP_MOVE(t), detail::terminal());
  1166. }
  1167. template <class Exp>
  1168. void do_add(const Exp& e, const detail::terminal&)
  1169. {
  1170. using default_ops::eval_add;
  1171. eval_add(m_backend, canonical_value(e.value()));
  1172. }
  1173. template <class Exp>
  1174. void do_add(const Exp& e, const detail::negate&)
  1175. {
  1176. typedef typename Exp::left_type left_type;
  1177. do_subtract(e.left(), typename left_type::tag_type());
  1178. }
  1179. template <class Exp>
  1180. void do_add(const Exp& e, const detail::plus&)
  1181. {
  1182. typedef typename Exp::left_type left_type;
  1183. typedef typename Exp::right_type right_type;
  1184. do_add(e.left(), typename left_type::tag_type());
  1185. do_add(e.right(), typename right_type::tag_type());
  1186. }
  1187. template <class Exp>
  1188. void do_add(const Exp& e, const detail::minus&)
  1189. {
  1190. typedef typename Exp::left_type left_type;
  1191. typedef typename Exp::right_type right_type;
  1192. do_add(e.left(), typename left_type::tag_type());
  1193. do_subtract(e.right(), typename right_type::tag_type());
  1194. }
  1195. template <class Exp, class unknown>
  1196. void do_add(const Exp& e, const unknown&)
  1197. {
  1198. self_type temp(e);
  1199. do_add(detail::expression<detail::terminal, self_type>(temp), detail::terminal());
  1200. }
  1201. template <class Exp>
  1202. void do_add(const Exp& e, const detail::add_immediates&)
  1203. {
  1204. using default_ops::eval_add;
  1205. eval_add(m_backend, canonical_value(e.left().value()));
  1206. eval_add(m_backend, canonical_value(e.right().value()));
  1207. }
  1208. template <class Exp>
  1209. void do_add(const Exp& e, const detail::subtract_immediates&)
  1210. {
  1211. using default_ops::eval_add;
  1212. using default_ops::eval_subtract;
  1213. eval_add(m_backend, canonical_value(e.left().value()));
  1214. eval_subtract(m_backend, canonical_value(e.right().value()));
  1215. }
  1216. template <class Exp>
  1217. void do_subtract(const Exp& e, const detail::terminal&)
  1218. {
  1219. using default_ops::eval_subtract;
  1220. eval_subtract(m_backend, canonical_value(e.value()));
  1221. }
  1222. template <class Exp>
  1223. void do_subtract(const Exp& e, const detail::negate&)
  1224. {
  1225. typedef typename Exp::left_type left_type;
  1226. do_add(e.left(), typename left_type::tag_type());
  1227. }
  1228. template <class Exp>
  1229. void do_subtract(const Exp& e, const detail::plus&)
  1230. {
  1231. typedef typename Exp::left_type left_type;
  1232. typedef typename Exp::right_type right_type;
  1233. do_subtract(e.left(), typename left_type::tag_type());
  1234. do_subtract(e.right(), typename right_type::tag_type());
  1235. }
  1236. template <class Exp>
  1237. void do_subtract(const Exp& e, const detail::minus&)
  1238. {
  1239. typedef typename Exp::left_type left_type;
  1240. typedef typename Exp::right_type right_type;
  1241. do_subtract(e.left(), typename left_type::tag_type());
  1242. do_add(e.right(), typename right_type::tag_type());
  1243. }
  1244. template <class Exp>
  1245. void do_subtract(const Exp& e, const detail::add_immediates&)
  1246. {
  1247. using default_ops::eval_subtract;
  1248. eval_subtract(m_backend, canonical_value(e.left().value()));
  1249. eval_subtract(m_backend, canonical_value(e.right().value()));
  1250. }
  1251. template <class Exp>
  1252. void do_subtract(const Exp& e, const detail::subtract_immediates&)
  1253. {
  1254. using default_ops::eval_add;
  1255. using default_ops::eval_subtract;
  1256. eval_subtract(m_backend, canonical_value(e.left().value()));
  1257. eval_add(m_backend, canonical_value(e.right().value()));
  1258. }
  1259. template <class Exp, class unknown>
  1260. void do_subtract(const Exp& e, const unknown&)
  1261. {
  1262. self_type temp(e);
  1263. do_subtract(detail::expression<detail::terminal, self_type>(temp), detail::terminal());
  1264. }
  1265. template <class Exp>
  1266. void do_multiplies(const Exp& e, const detail::terminal&)
  1267. {
  1268. using default_ops::eval_multiply;
  1269. eval_multiply(m_backend, canonical_value(e.value()));
  1270. }
  1271. template <class Exp>
  1272. void do_multiplies(const Exp& e, const detail::negate&)
  1273. {
  1274. typedef typename Exp::left_type left_type;
  1275. do_multiplies(e.left(), typename left_type::tag_type());
  1276. m_backend.negate();
  1277. }
  1278. template <class Exp>
  1279. void do_multiplies(const Exp& e, const detail::multiplies&)
  1280. {
  1281. typedef typename Exp::left_type left_type;
  1282. typedef typename Exp::right_type right_type;
  1283. do_multiplies(e.left(), typename left_type::tag_type());
  1284. do_multiplies(e.right(), typename right_type::tag_type());
  1285. }
  1286. template <class Exp>
  1287. void do_multiplies(const Exp& e, const detail::divides&)
  1288. {
  1289. typedef typename Exp::left_type left_type;
  1290. typedef typename Exp::right_type right_type;
  1291. do_multiplies(e.left(), typename left_type::tag_type());
  1292. do_divide(e.right(), typename right_type::tag_type());
  1293. }
  1294. template <class Exp>
  1295. void do_multiplies(const Exp& e, const detail::multiply_immediates&)
  1296. {
  1297. using default_ops::eval_multiply;
  1298. eval_multiply(m_backend, canonical_value(e.left().value()));
  1299. eval_multiply(m_backend, canonical_value(e.right().value()));
  1300. }
  1301. template <class Exp>
  1302. void do_multiplies(const Exp& e, const detail::divide_immediates&)
  1303. {
  1304. using default_ops::eval_multiply;
  1305. using default_ops::eval_divide;
  1306. eval_multiply(m_backend, canonical_value(e.left().value()));
  1307. eval_divide(m_backend, canonical_value(e.right().value()));
  1308. }
  1309. template <class Exp, class unknown>
  1310. void do_multiplies(const Exp& e, const unknown&)
  1311. {
  1312. using default_ops::eval_multiply;
  1313. self_type temp(e);
  1314. eval_multiply(m_backend, temp.m_backend);
  1315. }
  1316. template <class Exp>
  1317. void do_divide(const Exp& e, const detail::terminal&)
  1318. {
  1319. using default_ops::eval_divide;
  1320. eval_divide(m_backend, canonical_value(e.value()));
  1321. }
  1322. template <class Exp>
  1323. void do_divide(const Exp& e, const detail::negate&)
  1324. {
  1325. typedef typename Exp::left_type left_type;
  1326. do_divide(e.left(), typename left_type::tag_type());
  1327. m_backend.negate();
  1328. }
  1329. template <class Exp>
  1330. void do_divide(const Exp& e, const detail::multiplies&)
  1331. {
  1332. typedef typename Exp::left_type left_type;
  1333. typedef typename Exp::right_type right_type;
  1334. do_divide(e.left(), typename left_type::tag_type());
  1335. do_divide(e.right(), typename right_type::tag_type());
  1336. }
  1337. template <class Exp>
  1338. void do_divide(const Exp& e, const detail::divides&)
  1339. {
  1340. typedef typename Exp::left_type left_type;
  1341. typedef typename Exp::right_type right_type;
  1342. do_divide(e.left(), typename left_type::tag_type());
  1343. do_multiplies(e.right(), typename right_type::tag_type());
  1344. }
  1345. template <class Exp>
  1346. void do_divides(const Exp& e, const detail::multiply_immediates&)
  1347. {
  1348. using default_ops::eval_divide;
  1349. eval_divide(m_backend, canonical_value(e.left().value()));
  1350. eval_divide(m_backend, canonical_value(e.right().value()));
  1351. }
  1352. template <class Exp>
  1353. void do_divides(const Exp& e, const detail::divide_immediates&)
  1354. {
  1355. using default_ops::eval_multiply;
  1356. using default_ops::eval_divide;
  1357. eval_divide(m_backend, canonical_value(e.left().value()));
  1358. mutiply(m_backend, canonical_value(e.right().value()));
  1359. }
  1360. template <class Exp, class unknown>
  1361. void do_divide(const Exp& e, const unknown&)
  1362. {
  1363. using default_ops::eval_multiply;
  1364. self_type temp(e);
  1365. eval_divide(m_backend, temp.m_backend);
  1366. }
  1367. template <class Exp>
  1368. void do_modulus(const Exp& e, const detail::terminal&)
  1369. {
  1370. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The modulus operation is only valid for integer types");
  1371. using default_ops::eval_modulus;
  1372. eval_modulus(m_backend, canonical_value(e.value()));
  1373. }
  1374. template <class Exp, class Unknown>
  1375. void do_modulus(const Exp& e, const Unknown&)
  1376. {
  1377. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The modulus operation is only valid for integer types");
  1378. using default_ops::eval_modulus;
  1379. self_type temp(e);
  1380. eval_modulus(m_backend, canonical_value(temp));
  1381. }
  1382. template <class Exp>
  1383. void do_bitwise_and(const Exp& e, const detail::terminal&)
  1384. {
  1385. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise & operation is only valid for integer types");
  1386. using default_ops::eval_bitwise_and;
  1387. eval_bitwise_and(m_backend, canonical_value(e.value()));
  1388. }
  1389. template <class Exp>
  1390. void do_bitwise_and(const Exp& e, const detail::bitwise_and&)
  1391. {
  1392. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise & operation is only valid for integer types");
  1393. typedef typename Exp::left_type left_type;
  1394. typedef typename Exp::right_type right_type;
  1395. do_bitwise_and(e.left(), typename left_type::tag_type());
  1396. do_bitwise_and(e.right(), typename right_type::tag_type());
  1397. }
  1398. template <class Exp, class unknown>
  1399. void do_bitwise_and(const Exp& e, const unknown&)
  1400. {
  1401. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise & operation is only valid for integer types");
  1402. using default_ops::eval_bitwise_and;
  1403. self_type temp(e);
  1404. eval_bitwise_and(m_backend, temp.m_backend);
  1405. }
  1406. template <class Exp>
  1407. void do_bitwise_or(const Exp& e, const detail::terminal&)
  1408. {
  1409. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise | operation is only valid for integer types");
  1410. using default_ops::eval_bitwise_or;
  1411. eval_bitwise_or(m_backend, canonical_value(e.value()));
  1412. }
  1413. template <class Exp>
  1414. void do_bitwise_or(const Exp& e, const detail::bitwise_or&)
  1415. {
  1416. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise | operation is only valid for integer types");
  1417. typedef typename Exp::left_type left_type;
  1418. typedef typename Exp::right_type right_type;
  1419. do_bitwise_or(e.left(), typename left_type::tag_type());
  1420. do_bitwise_or(e.right(), typename right_type::tag_type());
  1421. }
  1422. template <class Exp, class unknown>
  1423. void do_bitwise_or(const Exp& e, const unknown&)
  1424. {
  1425. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise | operation is only valid for integer types");
  1426. using default_ops::eval_bitwise_or;
  1427. self_type temp(e);
  1428. eval_bitwise_or(m_backend, temp.m_backend);
  1429. }
  1430. template <class Exp>
  1431. void do_bitwise_xor(const Exp& e, const detail::terminal&)
  1432. {
  1433. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise ^ operation is only valid for integer types");
  1434. using default_ops::eval_bitwise_xor;
  1435. eval_bitwise_xor(m_backend, canonical_value(e.value()));
  1436. }
  1437. template <class Exp>
  1438. void do_bitwise_xor(const Exp& e, const detail::bitwise_xor&)
  1439. {
  1440. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise ^ operation is only valid for integer types");
  1441. typedef typename Exp::left_type left_type;
  1442. typedef typename Exp::right_type right_type;
  1443. do_bitwise_xor(e.left(), typename left_type::tag_type());
  1444. do_bitwise_xor(e.right(), typename right_type::tag_type());
  1445. }
  1446. template <class Exp, class unknown>
  1447. void do_bitwise_xor(const Exp& e, const unknown&)
  1448. {
  1449. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise ^ operation is only valid for integer types");
  1450. using default_ops::eval_bitwise_xor;
  1451. self_type temp(e);
  1452. eval_bitwise_xor(m_backend, temp.m_backend);
  1453. }
  1454. // Tests if the expression contains a reference to *this:
  1455. template <class Exp>
  1456. BOOST_MP_FORCEINLINE bool contains_self(const Exp& e)const BOOST_NOEXCEPT
  1457. {
  1458. return contains_self(e, typename Exp::arity());
  1459. }
  1460. template <class Exp>
  1461. BOOST_MP_FORCEINLINE bool contains_self(const Exp& e, mpl::int_<0> const&)const BOOST_NOEXCEPT
  1462. {
  1463. return is_realy_self(e.value());
  1464. }
  1465. template <class Exp>
  1466. BOOST_MP_FORCEINLINE bool contains_self(const Exp& e, mpl::int_<1> const&)const BOOST_NOEXCEPT
  1467. {
  1468. typedef typename Exp::left_type child_type;
  1469. return contains_self(e.left(), typename child_type::arity());
  1470. }
  1471. template <class Exp>
  1472. BOOST_MP_FORCEINLINE bool contains_self(const Exp& e, mpl::int_<2> const&)const BOOST_NOEXCEPT
  1473. {
  1474. typedef typename Exp::left_type child0_type;
  1475. typedef typename Exp::right_type child1_type;
  1476. return contains_self(e.left(), typename child0_type::arity())
  1477. || contains_self(e.right(), typename child1_type::arity());
  1478. }
  1479. template <class Exp>
  1480. BOOST_MP_FORCEINLINE bool contains_self(const Exp& e, mpl::int_<3> const&)const BOOST_NOEXCEPT
  1481. {
  1482. typedef typename Exp::left_type child0_type;
  1483. typedef typename Exp::middle_type child1_type;
  1484. typedef typename Exp::right_type child2_type;
  1485. return contains_self(e.left(), typename child0_type::arity())
  1486. || contains_self(e.middle(), typename child1_type::arity())
  1487. || contains_self(e.right(), typename child2_type::arity());
  1488. }
  1489. // Test if the expression is a reference to *this:
  1490. template <class Exp>
  1491. BOOST_MP_FORCEINLINE BOOST_CONSTEXPR bool is_self(const Exp& e)const BOOST_NOEXCEPT
  1492. {
  1493. return is_self(e, typename Exp::arity());
  1494. }
  1495. template <class Exp>
  1496. BOOST_MP_FORCEINLINE BOOST_CONSTEXPR bool is_self(const Exp& e, mpl::int_<0> const&)const BOOST_NOEXCEPT
  1497. {
  1498. return is_realy_self(e.value());
  1499. }
  1500. template <class Exp, int v>
  1501. BOOST_MP_FORCEINLINE BOOST_CONSTEXPR bool is_self(const Exp&, mpl::int_<v> const&)const BOOST_NOEXCEPT
  1502. {
  1503. return false;
  1504. }
  1505. template <class Val>
  1506. BOOST_MP_FORCEINLINE BOOST_CONSTEXPR bool is_realy_self(const Val&)const BOOST_NOEXCEPT{ return false; }
  1507. BOOST_MP_FORCEINLINE BOOST_CONSTEXPR bool is_realy_self(const self_type& v)const BOOST_NOEXCEPT{ return &v == this; }
  1508. static BOOST_MP_FORCEINLINE BOOST_CONSTEXPR const Backend& function_arg_value(const self_type& v) BOOST_NOEXCEPT { return v.backend(); }
  1509. template <class V>
  1510. static BOOST_MP_FORCEINLINE BOOST_CONSTEXPR const V& function_arg_value(const V& v) BOOST_NOEXCEPT { return v; }
  1511. template <class A1, class A2, class A3, class A4>
  1512. static BOOST_MP_FORCEINLINE const A1& function_arg_value(const detail::expression<detail::terminal, A1, A2, A3, A4>& exp) BOOST_NOEXCEPT { return exp.value(); }
  1513. template <class A2, class A3, class A4>
  1514. static BOOST_MP_FORCEINLINE BOOST_CONSTEXPR const Backend& function_arg_value(const detail::expression<detail::terminal, number<Backend>, A2, A3, A4>& exp) BOOST_NOEXCEPT { return exp.value().backend(); }
  1515. Backend m_backend;
  1516. public:
  1517. //
  1518. // These shouldn't really need to be public, or even member functions, but it makes implementing
  1519. // the non-member operators way easier if they are:
  1520. //
  1521. static BOOST_MP_FORCEINLINE BOOST_CONSTEXPR const Backend& canonical_value(const self_type& v) BOOST_NOEXCEPT { return v.m_backend; }
  1522. template <class B2, expression_template_option ET>
  1523. static BOOST_MP_FORCEINLINE BOOST_CONSTEXPR const B2& canonical_value(const number<B2, ET>& v) BOOST_NOEXCEPT { return v.backend(); }
  1524. template <class V>
  1525. static BOOST_MP_FORCEINLINE BOOST_CONSTEXPR typename boost::disable_if<is_same<typename detail::canonical<V, Backend>::type, V>, typename detail::canonical<V, Backend>::type>::type
  1526. canonical_value(const V& v) BOOST_NOEXCEPT { return static_cast<typename detail::canonical<V, Backend>::type>(v); }
  1527. template <class V>
  1528. static BOOST_MP_FORCEINLINE BOOST_CONSTEXPR typename boost::enable_if<is_same<typename detail::canonical<V, Backend>::type, V>, const V&>::type
  1529. canonical_value(const V& v) BOOST_NOEXCEPT { return v; }
  1530. static BOOST_MP_FORCEINLINE typename detail::canonical<std::string, Backend>::type canonical_value(const std::string& v) BOOST_NOEXCEPT { return v.c_str(); }
  1531. };
  1532. template <class Backend, expression_template_option ExpressionTemplates>
  1533. inline std::ostream& operator << (std::ostream& os, const number<Backend, ExpressionTemplates>& r)
  1534. {
  1535. std::streamsize d = os.precision();
  1536. std::string s = r.str(d, os.flags());
  1537. std::streamsize ss = os.width();
  1538. if(ss > static_cast<std::streamsize>(s.size()))
  1539. {
  1540. char fill = os.fill();
  1541. if((os.flags() & std::ios_base::left) == std::ios_base::left)
  1542. s.append(static_cast<std::string::size_type>(ss - s.size()), fill);
  1543. else
  1544. s.insert(0, static_cast<std::string::size_type>(ss - s.size()), fill);
  1545. }
  1546. return os << s;
  1547. }
  1548. namespace detail{
  1549. template <class tag, class A1, class A2, class A3, class A4>
  1550. inline std::ostream& operator << (std::ostream& os, const expression<tag, A1, A2, A3, A4>& r)
  1551. {
  1552. typedef typename expression<tag, A1, A2, A3, A4>::result_type value_type;
  1553. value_type temp(r);
  1554. return os << temp;
  1555. }
  1556. } // namespace detail
  1557. template <class Backend, expression_template_option ExpressionTemplates>
  1558. inline std::istream& operator >> (std::istream& is, number<Backend, ExpressionTemplates>& r)
  1559. {
  1560. bool hex_format = (is.flags() & std::ios_base::hex) == std::ios_base::hex;
  1561. bool oct_format = (is.flags() & std::ios_base::oct) == std::ios_base::oct;
  1562. std::string s;
  1563. is >> s;
  1564. if(hex_format && (number_category<Backend>::value == number_kind_integer) && ((s[0] != '0') || (s[1] != 'x')))
  1565. s.insert(s.find_first_not_of("+-"), "0x");
  1566. if(oct_format && (number_category<Backend>::value == number_kind_integer) && (s[0] != '0'))
  1567. s.insert(s.find_first_not_of("+-"), "0");
  1568. r.assign(s);
  1569. return is;
  1570. }
  1571. template <class Backend, expression_template_option ExpressionTemplates>
  1572. BOOST_MP_FORCEINLINE void swap(number<Backend, ExpressionTemplates>& a, number<Backend, ExpressionTemplates>& b)
  1573. {
  1574. a.swap(b);
  1575. }
  1576. } // namespace multiprecision
  1577. template <class T>
  1578. class rational;
  1579. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  1580. inline std::istream& operator >> (std::istream& is, rational<multiprecision::number<Backend, ExpressionTemplates> >& r)
  1581. {
  1582. std::string s1;
  1583. multiprecision::number<Backend, ExpressionTemplates> v1, v2;
  1584. char c;
  1585. bool have_hex = false;
  1586. bool hex_format = (is.flags() & std::ios_base::hex) == std::ios_base::hex;
  1587. bool oct_format = (is.flags() & std::ios_base::oct) == std::ios_base::oct;
  1588. while((EOF != (c = static_cast<char>(is.peek()))) && (c == 'x' || c == 'X' || c == '-' || c == '+' || (c >= '0' && c <= '9') || (have_hex && (c >= 'a' && c <= 'f')) || (have_hex && (c >= 'A' && c <= 'F'))))
  1589. {
  1590. if(c == 'x' || c == 'X')
  1591. have_hex = true;
  1592. s1.append(1, c);
  1593. is.get();
  1594. }
  1595. if(hex_format && ((s1[0] != '0') || (s1[1] != 'x')))
  1596. s1.insert(0, "0x");
  1597. if(oct_format && (s1[0] != '0'))
  1598. s1.insert(0, "0");
  1599. v1.assign(s1);
  1600. s1.erase();
  1601. if(c == '/')
  1602. {
  1603. is.get();
  1604. while((EOF != (c = static_cast<char>(is.peek()))) && (c == 'x' || c == 'X' || c == '-' || c == '+' || (c >= '0' && c <= '9') || (have_hex && (c >= 'a' && c <= 'f')) || (have_hex && (c >= 'A' && c <= 'F'))))
  1605. {
  1606. if(c == 'x' || c == 'X')
  1607. have_hex = true;
  1608. s1.append(1, c);
  1609. is.get();
  1610. }
  1611. if(hex_format && ((s1[0] != '0') || (s1[1] != 'x')))
  1612. s1.insert(0, "0x");
  1613. if(oct_format && (s1[0] != '0'))
  1614. s1.insert(0, "0");
  1615. v2.assign(s1);
  1616. }
  1617. else
  1618. v2 = 1;
  1619. r.assign(v1, v2);
  1620. return is;
  1621. }
  1622. template <class T, multiprecision::expression_template_option ExpressionTemplates, class Arithmetic>
  1623. typename boost::enable_if<boost::is_arithmetic<Arithmetic>, bool>::type operator == (const rational<multiprecision::number<T, ExpressionTemplates> >& a, const Arithmetic& b)
  1624. {
  1625. return a == multiprecision::number<T, ExpressionTemplates>(b);
  1626. }
  1627. template <class T, multiprecision::expression_template_option ExpressionTemplates, class Arithmetic>
  1628. typename boost::enable_if<boost::is_arithmetic<Arithmetic>, bool>::type operator == (const Arithmetic& b, const rational<multiprecision::number<T, ExpressionTemplates> >& a)
  1629. {
  1630. return a == multiprecision::number<T, ExpressionTemplates>(b);
  1631. }
  1632. template <class T, multiprecision::expression_template_option ExpressionTemplates, class Arithmetic>
  1633. typename boost::enable_if<boost::is_arithmetic<Arithmetic>, bool>::type operator != (const rational<multiprecision::number<T, ExpressionTemplates> >& a, const Arithmetic& b)
  1634. {
  1635. return a != multiprecision::number<T, ExpressionTemplates>(b);
  1636. }
  1637. template <class T, multiprecision::expression_template_option ExpressionTemplates, class Arithmetic>
  1638. typename boost::enable_if<boost::is_arithmetic<Arithmetic>, bool>::type operator != (const Arithmetic& b, const rational<multiprecision::number<T, ExpressionTemplates> >& a)
  1639. {
  1640. return a != multiprecision::number<T, ExpressionTemplates>(b);
  1641. }
  1642. template <class T, multiprecision::expression_template_option ExpressionTemplates>
  1643. inline multiprecision::number<T, ExpressionTemplates> numerator(const rational<multiprecision::number<T, ExpressionTemplates> >& a)
  1644. {
  1645. return a.numerator();
  1646. }
  1647. template <class T, multiprecision::expression_template_option ExpressionTemplates>
  1648. inline multiprecision::number<T, ExpressionTemplates> denominator(const rational<multiprecision::number<T, ExpressionTemplates> >& a)
  1649. {
  1650. return a.denominator();
  1651. }
  1652. #ifdef BOOST_MSVC
  1653. #pragma warning(pop)
  1654. #endif
  1655. } // namespaces
  1656. #include <boost/multiprecision/detail/ublas_interop.hpp>
  1657. #endif