cpp_bin_float.hpp 105 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294
  1. ////////////////////////////////////////////////////////////////
  2. // Copyright 2013 - 2022 John Maddock.
  3. // Copyright 2022 Christopher Kormanyos.
  4. // Distributed under the Boost Software License,
  5. // Version 1.0. (See accompanying file LICENSE_1_0.txt
  6. // or copy at https://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_MP_CPP_BIN_FLOAT_HPP
  8. #define BOOST_MP_CPP_BIN_FLOAT_HPP
  9. #include <cmath>
  10. #include <cstdint>
  11. #include <limits>
  12. #include <type_traits>
  13. #include <boost/multiprecision/cpp_int.hpp>
  14. #include <boost/multiprecision/integer.hpp>
  15. #include <boost/multiprecision/detail/standalone_config.hpp>
  16. #include <boost/multiprecision/detail/fpclassify.hpp>
  17. #include <boost/multiprecision/detail/float_string_cvt.hpp>
  18. #include <boost/multiprecision/traits/max_digits10.hpp>
  19. #include <boost/multiprecision/detail/hash.hpp>
  20. #include <boost/multiprecision/detail/no_exceptions_support.hpp>
  21. #include <boost/multiprecision/detail/assert.hpp>
  22. #include <boost/multiprecision/detail/float128_functions.hpp>
  23. #include <boost/multiprecision/detail/functions/trunc.hpp>
  24. //
  25. // Some includes we need from Boost.Math, since we rely on that library to provide these functions:
  26. //
  27. #ifdef BOOST_MP_MATH_AVAILABLE
  28. #include <boost/math/special_functions/asinh.hpp>
  29. #include <boost/math/special_functions/acosh.hpp>
  30. #include <boost/math/special_functions/atanh.hpp>
  31. #include <boost/math/special_functions/cbrt.hpp>
  32. #include <boost/math/special_functions/expm1.hpp>
  33. #include <boost/math/special_functions/gamma.hpp>
  34. #endif
  35. #ifdef BOOST_HAS_FLOAT128
  36. #include <quadmath.h>
  37. #endif
  38. namespace boost {
  39. namespace multiprecision {
  40. namespace backends {
  41. #ifdef BOOST_MSVC
  42. #pragma warning(push)
  43. #pragma warning(disable : 4522 6326) // multiple assignment operators specified, comparison of two constants
  44. #endif
  45. namespace detail {
  46. template <class U>
  47. inline typename std::enable_if<boost::multiprecision::detail::is_unsigned<U>::value, bool>::type is_negative(U) { return false; }
  48. template <class S>
  49. inline typename std::enable_if< !boost::multiprecision::detail::is_unsigned<S>::value, bool>::type is_negative(S s) { return s < 0; }
  50. template <class Float, std::ptrdiff_t, bool = number_category<Float>::value == number_kind_floating_point>
  51. struct is_cpp_bin_float_implicitly_constructible_from_type
  52. {
  53. static constexpr bool value = false;
  54. };
  55. template <class Float, std::ptrdiff_t bit_count>
  56. struct is_cpp_bin_float_implicitly_constructible_from_type<Float, bit_count, true>
  57. {
  58. static constexpr bool value = (std::numeric_limits<Float>::digits <= static_cast<int>(bit_count)) && (std::numeric_limits<Float>::radix == 2) && std::numeric_limits<Float>::is_specialized
  59. #ifdef BOOST_HAS_FLOAT128
  60. && !std::is_same<Float, float128_type>::value
  61. #endif
  62. && (std::is_floating_point<Float>::value || is_number<Float>::value);
  63. };
  64. template <class Float, std::ptrdiff_t, bool = number_category<Float>::value == number_kind_floating_point>
  65. struct is_cpp_bin_float_explicitly_constructible_from_type
  66. {
  67. static constexpr bool value = false;
  68. };
  69. template <class Float, std::ptrdiff_t bit_count>
  70. struct is_cpp_bin_float_explicitly_constructible_from_type<Float, bit_count, true>
  71. {
  72. static constexpr bool value = (std::numeric_limits<Float>::digits > static_cast<int>(bit_count)) && (std::numeric_limits<Float>::radix == 2) && std::numeric_limits<Float>::is_specialized
  73. #ifdef BOOST_HAS_FLOAT128
  74. && !std::is_same<Float, float128_type>::value
  75. #endif
  76. ;
  77. };
  78. } // namespace detail
  79. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinExponent, Exponent MaxExponent>
  80. class cpp_bin_float
  81. {
  82. public:
  83. static constexpr unsigned bit_count = DigitBase == digit_base_2 ? Digits : (Digits * 1000uL) / 301uL + (((Digits * 1000uL) % 301) ? 2u : 1u);
  84. using rep_type = cpp_int_backend<std::is_void<Allocator>::value ? bit_count : 0, bit_count, std::is_void<Allocator>::value ? unsigned_magnitude : signed_magnitude, unchecked, Allocator>;
  85. using double_rep_type = cpp_int_backend<std::is_void<Allocator>::value ? 2 * bit_count : 0, 2 * bit_count, std::is_void<Allocator>::value ? unsigned_magnitude : signed_magnitude, unchecked, Allocator>;
  86. using signed_types = typename rep_type::signed_types;
  87. using unsigned_types = typename rep_type::unsigned_types;
  88. using float_types = std::tuple<float, double, long double>;
  89. using exponent_type = Exponent;
  90. static constexpr exponent_type max_exponent_limit = (std::numeric_limits<exponent_type>::max)()- 2 * static_cast<exponent_type>(bit_count);
  91. static constexpr exponent_type min_exponent_limit = (std::numeric_limits<exponent_type>::min)() + 2 * static_cast<exponent_type>(bit_count);
  92. static_assert(MinExponent >= min_exponent_limit, "Template parameter MinExponent is too negative for our internal logic to function correctly, sorry!");
  93. static_assert(MaxExponent <= max_exponent_limit, "Template parameter MaxExponent is too large for our internal logic to function correctly, sorry!");
  94. static_assert(MinExponent <= 0, "Template parameter MinExponent can not be positive!");
  95. static_assert(MaxExponent >= 0, "Template parameter MaxExponent can not be negative!");
  96. static constexpr exponent_type max_exponent = MaxExponent == 0 ? max_exponent_limit : MaxExponent;
  97. static constexpr exponent_type min_exponent = MinExponent == 0 ? min_exponent_limit : MinExponent;
  98. static constexpr exponent_type exponent_zero = max_exponent + 1;
  99. static constexpr exponent_type exponent_infinity = max_exponent + 2;
  100. static constexpr exponent_type exponent_nan = max_exponent + 3;
  101. private:
  102. rep_type m_data;
  103. exponent_type m_exponent;
  104. bool m_sign;
  105. public:
  106. cpp_bin_float() noexcept(noexcept(rep_type())) : m_data(), m_exponent(exponent_zero), m_sign(false) {}
  107. cpp_bin_float(const cpp_bin_float& o) noexcept(noexcept(rep_type(std::declval<const rep_type&>())))
  108. : m_data(o.m_data), m_exponent(o.m_exponent), m_sign(o.m_sign) {}
  109. template <unsigned D, digit_base_type B, class A, class E, E MinE, E MaxE>
  110. cpp_bin_float(const cpp_bin_float<D, B, A, E, MinE, MaxE>& o, typename std::enable_if<(bit_count >= cpp_bin_float<D, B, A, E, MinE, MaxE>::bit_count)>::type const* = nullptr)
  111. {
  112. *this = o;
  113. }
  114. template <unsigned D, digit_base_type B, class A, class E, E MinE, E MaxE>
  115. explicit cpp_bin_float(const cpp_bin_float<D, B, A, E, MinE, MaxE>& o, typename std::enable_if< !(bit_count >= cpp_bin_float<D, B, A, E, MinE, MaxE>::bit_count)>::type const* = nullptr)
  116. : m_exponent(o.exponent()), m_sign(o.sign())
  117. {
  118. *this = o;
  119. }
  120. // rvalue copy:
  121. template <unsigned D, digit_base_type B, class A, class E, E MinE, E MaxE>
  122. cpp_bin_float(cpp_bin_float<D, B, A, E, MinE, MaxE>&& o, typename std::enable_if<(bit_count >= cpp_bin_float<D, B, A, E, MinE, MaxE>::bit_count)>::type const* = nullptr)noexcept(noexcept(rep_type(std::declval<rep_type&&>())))
  123. {
  124. *this = std::move(o);
  125. }
  126. template <unsigned D, digit_base_type B, class A, class E, E MinE, E MaxE>
  127. explicit cpp_bin_float(cpp_bin_float<D, B, A, E, MinE, MaxE>&& o, typename std::enable_if< !(bit_count >= cpp_bin_float<D, B, A, E, MinE, MaxE>::bit_count)>::type const* = nullptr) noexcept(noexcept(rep_type(std::declval<rep_type&&>())))
  128. : m_exponent(o.exponent()), m_sign(o.sign())
  129. {
  130. *this = std::move(o);
  131. }
  132. template <class Float>
  133. cpp_bin_float(const Float& f,
  134. typename std::enable_if<detail::is_cpp_bin_float_implicitly_constructible_from_type<Float, static_cast<std::ptrdiff_t>(bit_count)>::value>::type const* = nullptr)
  135. : m_data(), m_exponent(0), m_sign(false)
  136. {
  137. this->assign_float(f);
  138. }
  139. template <class Float>
  140. explicit cpp_bin_float(const Float& f,
  141. typename std::enable_if<detail::is_cpp_bin_float_explicitly_constructible_from_type<Float, static_cast<std::ptrdiff_t>(bit_count)>::value>::type const* = nullptr)
  142. : m_data(), m_exponent(0), m_sign(false)
  143. {
  144. this->assign_float(f);
  145. }
  146. #ifdef BOOST_HAS_FLOAT128
  147. template <class Float>
  148. cpp_bin_float(const Float& f,
  149. typename std::enable_if<
  150. std::is_same<Float, float128_type>::value && (static_cast<int>(bit_count) >= 113)>::type const* = nullptr)
  151. : m_data(), m_exponent(0), m_sign(false)
  152. {
  153. this->assign_float(f);
  154. }
  155. template <class Float>
  156. explicit cpp_bin_float(const Float& f,
  157. typename std::enable_if<
  158. std::is_same<Float, float128_type>::value && (static_cast<int>(bit_count) < 113)>::type const* = nullptr)
  159. : m_data(), m_exponent(0), m_sign(false)
  160. {
  161. this->assign_float(f);
  162. }
  163. #endif
  164. cpp_bin_float& operator=(const cpp_bin_float& o) noexcept(noexcept(std::declval<rep_type&>() = std::declval<const rep_type&>()))
  165. {
  166. m_data = o.m_data;
  167. m_exponent = o.m_exponent;
  168. m_sign = o.m_sign;
  169. return *this;
  170. }
  171. template <class A, class E, E MinE, E MaxE>
  172. cpp_bin_float& operator=(const cpp_bin_float<Digits, DigitBase, A, E, MinE, MaxE>& o) noexcept(noexcept(std::declval<rep_type&>() = std::declval<const rep_type&>()))
  173. {
  174. m_data = o.bits();
  175. m_sign = o.sign();
  176. if (o.exponent() == cpp_bin_float<Digits, DigitBase, A, E, MinE, MaxE>::exponent_zero)
  177. m_exponent = exponent_zero;
  178. else if (o.exponent() == cpp_bin_float<Digits, DigitBase, A, E, MinE, MaxE>::exponent_nan)
  179. m_exponent = exponent_nan;
  180. else if (o.exponent() == cpp_bin_float<Digits, DigitBase, A, E, MinE, MaxE>::exponent_infinity)
  181. m_exponent = exponent_infinity;
  182. else if (o.exponent() > cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::max_exponent)
  183. {
  184. // Overflow:
  185. exponent() = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity;
  186. bits() = static_cast<limb_type>(0u);
  187. }
  188. else if (o.exponent() < cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::min_exponent)
  189. {
  190. // Underflow:
  191. exponent() = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero;
  192. bits() = static_cast<limb_type>(0u);
  193. }
  194. else
  195. m_exponent = o.exponent();
  196. return *this;
  197. }
  198. // rvalue copy:
  199. template <class A, class E, E MinE, E MaxE>
  200. cpp_bin_float& operator=(cpp_bin_float<Digits, DigitBase, A, E, MinE, MaxE>&& o) noexcept(noexcept(std::declval<rep_type&>() = std::declval<rep_type&&>()))
  201. {
  202. m_data = std::move(o.bits());
  203. m_sign = o.sign();
  204. if (o.exponent() == cpp_bin_float<Digits, DigitBase, A, E, MinE, MaxE>::exponent_zero)
  205. m_exponent = exponent_zero;
  206. else if (o.exponent() == cpp_bin_float<Digits, DigitBase, A, E, MinE, MaxE>::exponent_nan)
  207. m_exponent = exponent_nan;
  208. else if (o.exponent() == cpp_bin_float<Digits, DigitBase, A, E, MinE, MaxE>::exponent_infinity)
  209. m_exponent = exponent_infinity;
  210. else if (o.exponent() > cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::max_exponent)
  211. {
  212. // Overflow:
  213. exponent() = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity;
  214. bits() = static_cast<limb_type>(0u);
  215. }
  216. else if (o.exponent() < cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::min_exponent)
  217. {
  218. // Underflow:
  219. exponent() = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero;
  220. bits() = static_cast<limb_type>(0u);
  221. }
  222. else
  223. m_exponent = o.exponent();
  224. return *this;
  225. }
  226. template <unsigned D, digit_base_type B, class A, class E, E MinE, E MaxE>
  227. cpp_bin_float& operator=(const cpp_bin_float<D, B, A, E, MinE, MaxE>& f)
  228. {
  229. switch (eval_fpclassify(f))
  230. {
  231. case FP_ZERO:
  232. m_data = limb_type(0);
  233. m_sign = f.sign();
  234. m_exponent = exponent_zero;
  235. break;
  236. case FP_NAN:
  237. m_data = limb_type(0);
  238. m_sign = false;
  239. m_exponent = exponent_nan;
  240. break;
  241. ;
  242. case FP_INFINITE:
  243. m_data = limb_type(0);
  244. m_sign = f.sign();
  245. m_exponent = exponent_infinity;
  246. break;
  247. default:
  248. typename cpp_bin_float<D, B, A, E, MinE, MaxE>::rep_type b(f.bits());
  249. this->exponent() = f.exponent() + (E)bit_count - (E)cpp_bin_float<D, B, A, E, MinE, MaxE>::bit_count;
  250. this->sign() = f.sign();
  251. copy_and_round(*this, b);
  252. }
  253. return *this;
  254. }
  255. #ifdef BOOST_HAS_FLOAT128
  256. template <class Float>
  257. typename std::enable_if<
  258. (number_category<Float>::value == number_kind_floating_point)
  259. //&& (std::numeric_limits<Float>::digits <= static_cast<int>(bit_count))
  260. && ((std::numeric_limits<Float>::radix == 2) || (std::is_same<Float, float128_type>::value)),
  261. cpp_bin_float&>::type
  262. operator=(const Float& f)
  263. #else
  264. template <class Float>
  265. typename std::enable_if<
  266. (number_category<Float>::value == number_kind_floating_point)
  267. //&& (std::numeric_limits<Float>::digits <= static_cast<int>(bit_count))
  268. && (std::numeric_limits<Float>::radix == 2),
  269. cpp_bin_float&>::type
  270. operator=(const Float& f)
  271. #endif
  272. {
  273. return assign_float(f);
  274. }
  275. #ifdef BOOST_HAS_FLOAT128
  276. template <class Float>
  277. typename std::enable_if<std::is_same<Float, float128_type>::value && (std::numeric_limits<Float>::digits > Digits), cpp_bin_float&>::type assign_float(Float f)
  278. {
  279. cpp_bin_float<113, DigitBase, Allocator, Exponent, MinExponent, MaxExponent> bf(f);
  280. return *this = bf;
  281. }
  282. template <class Float>
  283. typename std::enable_if<std::is_same<Float, float128_type>::value && (std::numeric_limits<Float>::digits <= Digits), cpp_bin_float&>::type assign_float(Float f)
  284. {
  285. using default_ops::eval_add;
  286. using bf_int_type = typename boost::multiprecision::detail::canonical<int, cpp_bin_float>::type;
  287. if (f == 0)
  288. {
  289. m_data = limb_type(0);
  290. m_sign = (signbitq(f) > 0);
  291. m_exponent = exponent_zero;
  292. return *this;
  293. }
  294. else if (isnanq(f))
  295. {
  296. m_data = limb_type(0);
  297. m_sign = false;
  298. m_exponent = exponent_nan;
  299. return *this;
  300. }
  301. else if (isinfq(f))
  302. {
  303. m_data = limb_type(0);
  304. m_sign = (f < 0);
  305. m_exponent = exponent_infinity;
  306. return *this;
  307. }
  308. if (f < 0)
  309. {
  310. *this = -f;
  311. this->negate();
  312. return *this;
  313. }
  314. using ui_type = typename std::tuple_element<0, unsigned_types>::type;
  315. m_data = static_cast<ui_type>(0u);
  316. m_sign = false;
  317. m_exponent = 0;
  318. constexpr std::ptrdiff_t bits = sizeof(int) * CHAR_BIT - 1 < MaxExponent - 1 ? sizeof(int) * CHAR_BIT - 1 : 3;
  319. int e;
  320. f = frexpq(f, &e);
  321. while (f)
  322. {
  323. f = ldexpq(f, bits);
  324. e -= bits;
  325. int ipart = static_cast<int>(truncq(f));
  326. f -= ipart;
  327. m_exponent += bits;
  328. cpp_bin_float t;
  329. t = static_cast<bf_int_type>(ipart);
  330. eval_add(*this, t);
  331. }
  332. m_exponent += static_cast<Exponent>(e);
  333. if (m_exponent > max_exponent)
  334. {
  335. m_exponent = exponent_infinity;
  336. m_data = static_cast<ui_type>(0u);
  337. }
  338. else if (m_exponent < min_exponent)
  339. {
  340. m_exponent = exponent_zero;
  341. m_data = static_cast<ui_type>(0u);
  342. }
  343. return *this;
  344. }
  345. #endif
  346. #ifdef BOOST_HAS_FLOAT128
  347. template <class Float>
  348. typename std::enable_if<std::is_floating_point<Float>::value && !std::is_same<Float, float128_type>::value && (std::numeric_limits<Float>::digits > Digits), cpp_bin_float&>::type assign_float(Float f)
  349. #else
  350. template <class Float>
  351. typename std::enable_if<std::is_floating_point<Float>::value && (std::numeric_limits<Float>::digits > Digits), cpp_bin_float&>::type assign_float(Float f)
  352. #endif
  353. {
  354. cpp_bin_float<std::numeric_limits<Float>::digits, DigitBase, Allocator, Exponent, MinExponent, MaxExponent> bf(f);
  355. return *this = bf;
  356. }
  357. #ifdef BOOST_HAS_FLOAT128
  358. template <class Float>
  359. typename std::enable_if<std::is_floating_point<Float>::value && !std::is_same<Float, float128_type>::value && (std::numeric_limits<Float>::digits <= Digits), cpp_bin_float&>::type assign_float(Float f)
  360. #else
  361. template <class Float>
  362. typename std::enable_if<std::is_floating_point<Float>::value && (std::numeric_limits<Float>::digits <= Digits), cpp_bin_float&>::type assign_float(Float f)
  363. #endif
  364. {
  365. using std::frexp;
  366. using std::ldexp;
  367. using std::signbit;
  368. using default_ops::eval_add;
  369. using bf_int_type = typename boost::multiprecision::detail::canonical<int, cpp_bin_float>::type;
  370. switch (BOOST_MP_FPCLASSIFY(f))
  371. {
  372. case FP_ZERO:
  373. m_data = limb_type(0);
  374. m_sign = ((signbit)(f));
  375. m_exponent = exponent_zero;
  376. return *this;
  377. case FP_NAN:
  378. m_data = limb_type(0);
  379. m_sign = false;
  380. m_exponent = exponent_nan;
  381. return *this;
  382. case FP_INFINITE:
  383. m_data = limb_type(0);
  384. m_sign = (f < 0);
  385. m_exponent = exponent_infinity;
  386. return *this;
  387. }
  388. if (f < 0)
  389. {
  390. *this = -f;
  391. this->negate();
  392. return *this;
  393. }
  394. using ui_type = typename std::tuple_element<0, unsigned_types>::type;
  395. m_data = static_cast<ui_type>(0u);
  396. m_sign = false;
  397. m_exponent = 0;
  398. //
  399. // This code picks off the bits in f a few at a time and injects them into *this.
  400. // It does not do roundingm so we must have more digits precision in *this than
  401. // in the floating point value (the normal situation, unless we're emulating another
  402. // type like float16_t).
  403. //
  404. constexpr std::ptrdiff_t bits = static_cast<std::ptrdiff_t>(sizeof(int) * CHAR_BIT - 1) < static_cast<std::ptrdiff_t>(MaxExponent - 1) ? static_cast<std::ptrdiff_t>(sizeof(int) * CHAR_BIT - 1) : 3;
  405. int e;
  406. f = frexp(f, &e);
  407. while (f != static_cast<Float>(0.0F))
  408. {
  409. f = ldexp(f, bits);
  410. e -= static_cast<int>(bits);
  411. int ipart = boost::multiprecision::detail::itrunc(f);
  412. f -= static_cast<Float>(ipart);
  413. m_exponent += static_cast<exponent_type>(bits);
  414. cpp_bin_float t;
  415. t = static_cast<bf_int_type>(ipart);
  416. eval_add(*this, t);
  417. }
  418. m_exponent += static_cast<Exponent>(e);
  419. if (m_exponent > max_exponent)
  420. {
  421. m_exponent = exponent_infinity;
  422. m_data = static_cast<ui_type>(0u);
  423. }
  424. else if(m_exponent < min_exponent)
  425. {
  426. m_exponent = exponent_zero;
  427. m_data = static_cast<ui_type>(0u);
  428. }
  429. return *this;
  430. }
  431. template <class Float>
  432. typename std::enable_if<
  433. (number_category<Float>::value == number_kind_floating_point) && !std::is_floating_point<Float>::value && (number_category<Float>::value == number_kind_floating_point),
  434. cpp_bin_float&>::type
  435. assign_float(Float f)
  436. {
  437. using default_ops::eval_add;
  438. using default_ops::eval_convert_to;
  439. using default_ops::eval_get_sign;
  440. using default_ops::eval_subtract;
  441. using f_int_type = typename boost::multiprecision::detail::canonical<int, Float>::type ;
  442. using bf_int_type = typename boost::multiprecision::detail::canonical<int, cpp_bin_float>::type;
  443. switch (eval_fpclassify(f))
  444. {
  445. case FP_ZERO:
  446. m_data = limb_type(0);
  447. m_sign = (eval_get_sign(f) > 0);
  448. m_exponent = exponent_zero;
  449. return *this;
  450. case FP_NAN:
  451. m_data = limb_type(0);
  452. m_sign = false;
  453. m_exponent = exponent_nan;
  454. return *this;
  455. case FP_INFINITE:
  456. m_data = limb_type(0);
  457. m_sign = eval_get_sign(f) < 0;
  458. m_exponent = exponent_infinity;
  459. return *this;
  460. }
  461. if (eval_get_sign(f) < 0)
  462. {
  463. f.negate();
  464. assign_float(f);
  465. this->negate();
  466. return *this;
  467. }
  468. using ui_type = typename std::tuple_element<0, unsigned_types>::type;
  469. m_data = static_cast<ui_type>(0u);
  470. m_sign = false;
  471. m_exponent = 0;
  472. constexpr std::ptrdiff_t bits = sizeof(int) * CHAR_BIT - 1;
  473. int e;
  474. eval_frexp(f, f, &e);
  475. while (eval_get_sign(f) != 0)
  476. {
  477. eval_ldexp(f, f, bits);
  478. e -= bits;
  479. int ipart;
  480. eval_convert_to(&ipart, f);
  481. eval_subtract(f, static_cast<f_int_type>(ipart));
  482. m_exponent += bits;
  483. eval_add(*this, static_cast<bf_int_type>(ipart));
  484. }
  485. m_exponent += e;
  486. if (m_exponent > max_exponent)
  487. m_exponent = exponent_infinity;
  488. if (m_exponent < min_exponent)
  489. {
  490. m_data = limb_type(0u);
  491. m_exponent = exponent_zero;
  492. m_sign = (eval_get_sign(f) > 0);
  493. }
  494. else if (eval_get_sign(m_data) == 0)
  495. {
  496. m_exponent = exponent_zero;
  497. m_sign = (eval_get_sign(f) > 0);
  498. }
  499. return *this;
  500. }
  501. template <class B, expression_template_option et>
  502. cpp_bin_float& assign_float(const number<B, et>& f)
  503. {
  504. return assign_float(f.backend());
  505. }
  506. template <class I>
  507. typename std::enable_if<boost::multiprecision::detail::is_integral<I>::value, cpp_bin_float&>::type operator=(const I& i)
  508. {
  509. using default_ops::eval_bit_test;
  510. if (!i)
  511. {
  512. m_data = static_cast<limb_type>(0);
  513. m_exponent = exponent_zero;
  514. m_sign = false;
  515. }
  516. else
  517. {
  518. using ui_type = typename boost::multiprecision::detail::make_unsigned<I>::type ;
  519. ui_type fi = static_cast<ui_type>(boost::multiprecision::detail::unsigned_abs(i));
  520. using ar_type = typename boost::multiprecision::detail::canonical<ui_type, rep_type>::type;
  521. m_data = static_cast<ar_type>(fi);
  522. std::size_t shift = msb(fi);
  523. if (shift > max_exponent)
  524. {
  525. m_exponent = exponent_infinity;
  526. m_data = static_cast<limb_type>(0);
  527. }
  528. else if (shift >= bit_count)
  529. {
  530. m_exponent = static_cast<Exponent>(shift);
  531. m_data = static_cast<ar_type>(fi >> (shift + 1 - bit_count));
  532. }
  533. else
  534. {
  535. m_exponent = static_cast<Exponent>(shift);
  536. eval_left_shift(m_data, bit_count - shift - 1);
  537. }
  538. BOOST_MP_ASSERT((m_exponent == exponent_infinity) || eval_bit_test(m_data, bit_count - 1));
  539. m_sign = detail::is_negative(i);
  540. }
  541. return *this;
  542. }
  543. cpp_bin_float& operator=(const char* s);
  544. void swap(cpp_bin_float& o) noexcept
  545. {
  546. m_data.swap(o.m_data);
  547. std::swap(m_exponent, o.m_exponent);
  548. std::swap(m_sign, o.m_sign);
  549. }
  550. std::string str(std::streamsize dig, std::ios_base::fmtflags f) const;
  551. void negate()
  552. {
  553. if (m_exponent != exponent_nan)
  554. m_sign = !m_sign;
  555. }
  556. int compare(const cpp_bin_float& o) const noexcept
  557. {
  558. if (m_sign != o.m_sign)
  559. return (m_exponent == exponent_zero) && (m_exponent == o.m_exponent) ? 0 : m_sign ? -1 : 1;
  560. int result;
  561. if (m_exponent == exponent_nan)
  562. return -1;
  563. else if (m_exponent != o.m_exponent)
  564. {
  565. if (m_exponent == exponent_zero)
  566. result = -1;
  567. else if (o.m_exponent == exponent_zero)
  568. result = 1;
  569. else
  570. result = m_exponent > o.m_exponent ? 1 : -1;
  571. }
  572. else
  573. result = m_data.compare(o.m_data);
  574. if (m_sign)
  575. result = -result;
  576. return result;
  577. }
  578. template <class A>
  579. int compare(const A& o) const noexcept
  580. {
  581. cpp_bin_float b;
  582. b = o;
  583. return compare(b);
  584. }
  585. rep_type& bits() { return m_data; }
  586. const rep_type& bits() const { return m_data; }
  587. exponent_type& exponent() { return m_exponent; }
  588. const exponent_type& exponent() const { return m_exponent; }
  589. bool& sign() { return m_sign; }
  590. const bool& sign() const { return m_sign; }
  591. void check_invariants()
  592. {
  593. using default_ops::eval_bit_test;
  594. using default_ops::eval_is_zero;
  595. if ((m_exponent <= max_exponent) && (m_exponent >= min_exponent))
  596. {
  597. BOOST_MP_ASSERT(eval_bit_test(m_data, bit_count - 1));
  598. }
  599. else
  600. {
  601. BOOST_MP_ASSERT(m_exponent > max_exponent);
  602. BOOST_MP_ASSERT(m_exponent <= exponent_nan);
  603. BOOST_MP_ASSERT(eval_is_zero(m_data));
  604. }
  605. }
  606. #ifndef BOOST_MP_STANDALONE
  607. template <class Archive>
  608. void serialize(Archive& ar, const unsigned int /*version*/)
  609. {
  610. ar& boost::make_nvp("data", m_data);
  611. ar& boost::make_nvp("exponent", m_exponent);
  612. ar& boost::make_nvp("sign", m_sign);
  613. }
  614. #endif
  615. };
  616. #ifdef BOOST_MSVC
  617. #pragma warning(pop)
  618. #endif
  619. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, class Int>
  620. inline void copy_and_round(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& res, Int& arg, std::ptrdiff_t bits_to_keep = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count)
  621. {
  622. // Precondition: exponent of res must have been set before this function is called
  623. // as we may need to adjust it based on how many bits_to_keep in arg are set.
  624. using default_ops::eval_bit_test;
  625. using default_ops::eval_get_sign;
  626. using default_ops::eval_increment;
  627. using default_ops::eval_left_shift;
  628. using default_ops::eval_lsb;
  629. using default_ops::eval_msb;
  630. using default_ops::eval_right_shift;
  631. // cancellation may have resulted in arg being all zeros:
  632. if (eval_get_sign(arg) == 0)
  633. {
  634. res.exponent() = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero;
  635. res.sign() = false;
  636. res.bits() = static_cast<limb_type>(0u);
  637. return;
  638. }
  639. std::ptrdiff_t msb = static_cast<std::ptrdiff_t>(eval_msb(arg));
  640. if (static_cast<std::ptrdiff_t >(bits_to_keep) > msb + 1)
  641. {
  642. // Must have had cancellation in subtraction,
  643. // or be converting from a narrower type, so shift left:
  644. res.bits() = arg;
  645. eval_left_shift(res.bits(), static_cast<double_limb_type>(bits_to_keep - msb - 1));
  646. res.exponent() -= static_cast<Exponent>(bits_to_keep - msb - 1);
  647. }
  648. else if (static_cast<std::ptrdiff_t >(bits_to_keep) < msb + 1)
  649. {
  650. // We have more bits_to_keep than we need, so round as required,
  651. // first get the rounding bit:
  652. bool roundup = eval_bit_test(arg, static_cast<std::size_t>(msb - bits_to_keep));
  653. // Then check for a tie:
  654. if (roundup && (msb - bits_to_keep == static_cast<std::ptrdiff_t>(eval_lsb(arg))))
  655. {
  656. // Ties round towards even:
  657. if (!eval_bit_test(arg, static_cast<std::size_t>(msb - bits_to_keep + 1)))
  658. roundup = false;
  659. }
  660. // Shift off the bits_to_keep we don't need:
  661. eval_right_shift(arg, static_cast<double_limb_type>(msb - bits_to_keep + 1));
  662. res.exponent() += static_cast<Exponent>(msb - bits_to_keep + 1);
  663. if (roundup)
  664. {
  665. eval_increment(arg);
  666. if (bits_to_keep)
  667. {
  668. if (eval_bit_test(arg, static_cast<std::size_t>(bits_to_keep)))
  669. {
  670. // This happens very very rairly, all the bits left after
  671. // truncation must be 1's and we're rounding up an order of magnitude:
  672. eval_right_shift(arg, 1u);
  673. ++res.exponent();
  674. }
  675. }
  676. else
  677. {
  678. // We get here when bits_to_keep is zero but we're rounding up,
  679. // as a result we end up with a single digit that is a 1:
  680. ++bits_to_keep;
  681. }
  682. }
  683. if (bits_to_keep != cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count)
  684. {
  685. // Normalize result when we're rounding to fewer bits than we can hold, only happens in conversions
  686. // to narrower types:
  687. eval_left_shift(arg, static_cast<double_limb_type>(static_cast<std::ptrdiff_t>(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count) - bits_to_keep));
  688. res.exponent() -= static_cast<Exponent>(static_cast<std::ptrdiff_t>(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count) - bits_to_keep);
  689. }
  690. res.bits() = arg;
  691. }
  692. else
  693. {
  694. res.bits() = arg;
  695. }
  696. if (!bits_to_keep && !res.bits().limbs()[0])
  697. {
  698. // We're keeping zero bits and did not round up, so result is zero:
  699. res.exponent() = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero;
  700. return;
  701. }
  702. // Result must be normalized:
  703. BOOST_MP_ASSERT(((std::ptrdiff_t )eval_msb(res.bits()) == cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - 1));
  704. if (res.exponent() > cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::max_exponent)
  705. {
  706. // Overflow:
  707. res.exponent() = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity;
  708. res.bits() = static_cast<limb_type>(0u);
  709. }
  710. else if (res.exponent() < cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::min_exponent)
  711. {
  712. // Underflow:
  713. res.exponent() = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero;
  714. res.bits() = static_cast<limb_type>(0u);
  715. }
  716. }
  717. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, class BinFloat2, class BinFloat3>
  718. inline void do_eval_add(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& res,
  719. const BinFloat2& a, const BinFloat3& b)
  720. {
  721. if (a.exponent() < b.exponent())
  722. {
  723. bool s = a.sign();
  724. do_eval_add(res, b, a);
  725. if (res.sign() != s)
  726. res.negate();
  727. return;
  728. }
  729. using default_ops::eval_add;
  730. using default_ops::eval_bit_test;
  731. using exponent_type = typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type;
  732. typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::double_rep_type dt;
  733. // Special cases first:
  734. switch (a.exponent())
  735. {
  736. case BinFloat2::exponent_zero:
  737. {
  738. bool s = a.sign();
  739. res = b;
  740. res.sign() = s;
  741. return;
  742. }
  743. case BinFloat2::exponent_infinity:
  744. if (b.exponent() == cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan)
  745. res = b;
  746. else
  747. res = a;
  748. return; // result is still infinite.
  749. case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan:
  750. res = a;
  751. return; // result is still a NaN.
  752. }
  753. switch (b.exponent())
  754. {
  755. case BinFloat3::exponent_zero:
  756. res = a;
  757. return;
  758. case BinFloat3::exponent_infinity:
  759. res = b;
  760. if (res.sign())
  761. res.negate();
  762. return; // result is infinite.
  763. case BinFloat3::exponent_nan:
  764. res = b;
  765. return; // result is a NaN.
  766. }
  767. static_assert((std::numeric_limits<exponent_type>::max)() - cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count > cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::max_exponent, "Exponent range check failed");
  768. bool s = a.sign();
  769. dt = a.bits();
  770. if (a.exponent() > (std::ptrdiff_t )cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count + b.exponent())
  771. {
  772. res.exponent() = a.exponent();
  773. }
  774. else
  775. {
  776. exponent_type e_diff = a.exponent() - b.exponent();
  777. BOOST_MP_ASSERT(e_diff >= 0);
  778. eval_left_shift(dt, static_cast<double_limb_type>(e_diff));
  779. res.exponent() = a.exponent() - e_diff;
  780. eval_add(dt, b.bits());
  781. }
  782. copy_and_round(res, dt);
  783. res.check_invariants();
  784. if (res.sign() != s)
  785. res.negate();
  786. }
  787. template <class BinFloat1, class BinFloat2, class BinFloat3>
  788. inline void do_eval_subtract(BinFloat1& res, const BinFloat2& a, const BinFloat3& b)
  789. {
  790. using default_ops::eval_bit_test;
  791. using default_ops::eval_decrement;
  792. using default_ops::eval_subtract;
  793. typename BinFloat1::double_rep_type dt;
  794. // Special cases first:
  795. switch (a.exponent())
  796. {
  797. case BinFloat2::exponent_zero:
  798. if (b.exponent() == BinFloat3::exponent_nan)
  799. res = std::numeric_limits<number<BinFloat1> >::quiet_NaN().backend();
  800. else
  801. {
  802. bool s = a.sign();
  803. res = b;
  804. if (res.exponent() == BinFloat1::exponent_zero)
  805. res.sign() = false;
  806. else if (res.sign() == s)
  807. res.negate();
  808. }
  809. return;
  810. case BinFloat2::exponent_infinity:
  811. if ((b.exponent() == BinFloat3::exponent_nan) || (b.exponent() == BinFloat3::exponent_infinity))
  812. res = std::numeric_limits<number<BinFloat1> >::quiet_NaN().backend();
  813. else
  814. res = a;
  815. return;
  816. case BinFloat2::exponent_nan:
  817. res = a;
  818. return; // result is still a NaN.
  819. }
  820. switch (b.exponent())
  821. {
  822. case BinFloat3::exponent_zero:
  823. res = a;
  824. return;
  825. case BinFloat3::exponent_infinity:
  826. res.exponent() = BinFloat1::exponent_infinity;
  827. res.sign() = !a.sign();
  828. res.bits() = static_cast<limb_type>(0u);
  829. return; // result is a NaN.
  830. case BinFloat3::exponent_nan:
  831. res = b;
  832. return; // result is still a NaN.
  833. }
  834. bool s = a.sign();
  835. if ((a.exponent() > b.exponent()) || ((a.exponent() == b.exponent()) && a.bits().compare(b.bits()) >= 0))
  836. {
  837. dt = a.bits();
  838. if (a.exponent() <= (std::ptrdiff_t )BinFloat1::bit_count + b.exponent())
  839. {
  840. typename BinFloat1::exponent_type e_diff = a.exponent() - b.exponent();
  841. eval_left_shift(dt, static_cast<double_limb_type>(e_diff));
  842. res.exponent() = a.exponent() - e_diff;
  843. eval_subtract(dt, b.bits());
  844. }
  845. else if (a.exponent() == (std::ptrdiff_t )BinFloat1::bit_count + b.exponent() + 1)
  846. {
  847. if ((eval_lsb(a.bits()) == BinFloat1::bit_count - 1)
  848. && (eval_lsb(b.bits()) != BinFloat1::bit_count - 1))
  849. {
  850. eval_left_shift(dt, 1);
  851. eval_decrement(dt);
  852. res.exponent() = a.exponent() - 1;
  853. }
  854. else
  855. res.exponent() = a.exponent();
  856. }
  857. else
  858. res.exponent() = a.exponent();
  859. }
  860. else
  861. {
  862. dt = b.bits();
  863. if (b.exponent() <= (std::ptrdiff_t )BinFloat1::bit_count + a.exponent())
  864. {
  865. typename BinFloat1::exponent_type e_diff = a.exponent() - b.exponent();
  866. eval_left_shift(dt, static_cast<double_limb_type>(-e_diff));
  867. res.exponent() = b.exponent() + e_diff;
  868. eval_subtract(dt, a.bits());
  869. }
  870. else if (b.exponent() == (std::ptrdiff_t )BinFloat1::bit_count + a.exponent() + 1)
  871. {
  872. if ((eval_lsb(a.bits()) != BinFloat1::bit_count - 1)
  873. && eval_lsb(b.bits()))
  874. {
  875. eval_left_shift(dt, 1);
  876. eval_decrement(dt);
  877. res.exponent() = b.exponent() - 1;
  878. }
  879. else
  880. res.exponent() = b.exponent();
  881. }
  882. else
  883. res.exponent() = b.exponent();
  884. s = !s;
  885. }
  886. copy_and_round(res, dt);
  887. if (res.exponent() == BinFloat1::exponent_zero)
  888. res.sign() = false;
  889. else if (res.sign() != s)
  890. res.negate();
  891. res.check_invariants();
  892. }
  893. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE,
  894. class Allocator2, class Exponent2, Exponent MinE2, Exponent MaxE2,
  895. class Allocator3, class Exponent3, Exponent MinE3, Exponent MaxE3>
  896. inline void eval_add(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& res,
  897. const cpp_bin_float<Digits, DigitBase, Allocator2, Exponent2, MinE2, MaxE2>& a,
  898. const cpp_bin_float<Digits, DigitBase, Allocator3, Exponent3, MinE3, MaxE3>& b)
  899. {
  900. if (a.sign() == b.sign())
  901. do_eval_add(res, a, b);
  902. else
  903. do_eval_subtract(res, a, b);
  904. }
  905. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE,
  906. class Allocator2, class Exponent2, Exponent MinE2, Exponent MaxE2>
  907. inline void eval_add(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& res,
  908. const cpp_bin_float<Digits, DigitBase, Allocator2, Exponent2, MinE2, MaxE2>& a)
  909. {
  910. return eval_add(res, res, a);
  911. }
  912. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE,
  913. class Allocator2, class Exponent2, Exponent MinE2, Exponent MaxE2,
  914. class Allocator3, class Exponent3, Exponent MinE3, Exponent MaxE3>
  915. inline void eval_subtract(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& res,
  916. const cpp_bin_float<Digits, DigitBase, Allocator2, Exponent2, MinE2, MaxE2>& a,
  917. const cpp_bin_float<Digits, DigitBase, Allocator3, Exponent3, MinE3, MaxE3>& b)
  918. {
  919. if (a.sign() != b.sign())
  920. do_eval_add(res, a, b);
  921. else
  922. do_eval_subtract(res, a, b);
  923. }
  924. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE,
  925. class Allocator2, class Exponent2, Exponent MinE2, Exponent MaxE2>
  926. inline void eval_subtract(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& res,
  927. const cpp_bin_float<Digits, DigitBase, Allocator2, Exponent2, MinE2, MaxE2>& a)
  928. {
  929. return eval_subtract(res, res, a);
  930. }
  931. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE,
  932. class Allocator2, class Exponent2, Exponent MinE2, Exponent MaxE2,
  933. class Allocator3, class Exponent3, Exponent MinE3, Exponent MaxE3>
  934. inline void eval_multiply(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& res,
  935. const cpp_bin_float<Digits, DigitBase, Allocator2, Exponent2, MinE2, MaxE2>& a,
  936. const cpp_bin_float<Digits, DigitBase, Allocator3, Exponent3, MinE3, MaxE3>& b)
  937. {
  938. using default_ops::eval_bit_test;
  939. using default_ops::eval_multiply;
  940. // Special cases first:
  941. switch (a.exponent())
  942. {
  943. case cpp_bin_float<Digits, DigitBase, Allocator2, Exponent2, MinE2, MaxE2>::exponent_zero:
  944. {
  945. if (b.exponent() == cpp_bin_float<Digits, DigitBase, Allocator3, Exponent3, MinE3, MaxE3>::exponent_nan)
  946. res = b;
  947. else if (b.exponent() == cpp_bin_float<Digits, DigitBase, Allocator3, Exponent3, MinE3, MaxE3>::exponent_infinity)
  948. res = std::numeric_limits<number<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::quiet_NaN().backend();
  949. else
  950. {
  951. bool s = a.sign() != b.sign();
  952. res = a;
  953. res.sign() = s;
  954. }
  955. return;
  956. }
  957. case cpp_bin_float<Digits, DigitBase, Allocator2, Exponent2, MinE2, MaxE2>::exponent_infinity:
  958. switch (b.exponent())
  959. {
  960. case cpp_bin_float<Digits, DigitBase, Allocator3, Exponent3, MinE3, MaxE3>::exponent_zero:
  961. res = std::numeric_limits<number<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::quiet_NaN().backend();
  962. break;
  963. case cpp_bin_float<Digits, DigitBase, Allocator3, Exponent3, MinE3, MaxE3>::exponent_nan:
  964. res = b;
  965. break;
  966. default:
  967. bool s = a.sign() != b.sign();
  968. res = a;
  969. res.sign() = s;
  970. break;
  971. }
  972. return;
  973. case cpp_bin_float<Digits, DigitBase, Allocator2, Exponent2, MinE2, MaxE2>::exponent_nan:
  974. res = a;
  975. return;
  976. }
  977. if (b.exponent() > cpp_bin_float<Digits, DigitBase, Allocator3, Exponent3, MinE3, MaxE3>::max_exponent)
  978. {
  979. bool s = a.sign() != b.sign();
  980. res = b;
  981. res.sign() = s;
  982. return;
  983. }
  984. if ((a.exponent() > 0) && (b.exponent() > 0))
  985. {
  986. if (cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::max_exponent + 2 - a.exponent() < b.exponent())
  987. {
  988. // We will certainly overflow:
  989. bool s = a.sign() != b.sign();
  990. res.exponent() = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity;
  991. res.sign() = s;
  992. res.bits() = static_cast<limb_type>(0u);
  993. return;
  994. }
  995. }
  996. if ((a.exponent() < 0) && (b.exponent() < 0))
  997. {
  998. if (cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::min_exponent - 2 - a.exponent() > b.exponent())
  999. {
  1000. // We will certainly underflow:
  1001. res.exponent() = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero;
  1002. res.sign() = a.sign() != b.sign();
  1003. res.bits() = static_cast<limb_type>(0u);
  1004. return;
  1005. }
  1006. }
  1007. typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::double_rep_type dt;
  1008. eval_multiply(dt, a.bits(), b.bits());
  1009. res.exponent() = a.exponent() + b.exponent() - (Exponent)cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count + 1;
  1010. copy_and_round(res, dt);
  1011. res.check_invariants();
  1012. res.sign() = a.sign() != b.sign();
  1013. }
  1014. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE,
  1015. class Allocator2, class Exponent2, Exponent MinE2, Exponent MaxE2>
  1016. inline void eval_multiply(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& res,
  1017. const cpp_bin_float<Digits, DigitBase, Allocator2, Exponent2, MinE2, MaxE2>& a)
  1018. {
  1019. eval_multiply(res, res, a);
  1020. }
  1021. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE,
  1022. class Allocator2, class Exponent2, Exponent MinE2, Exponent MaxE2, class U>
  1023. inline typename std::enable_if<boost::multiprecision::detail::is_unsigned<U>::value>::type eval_multiply(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& res,
  1024. const cpp_bin_float<Digits, DigitBase, Allocator2, Exponent2, MinE2, MaxE2>& a, const U& b)
  1025. {
  1026. using default_ops::eval_bit_test;
  1027. using default_ops::eval_multiply;
  1028. bool s = a.sign(); // saved for later in case a and res are the same object.
  1029. // Special cases first:
  1030. switch (a.exponent())
  1031. {
  1032. case cpp_bin_float<Digits, DigitBase, Allocator2, Exponent2, MinE2, MaxE2>::exponent_zero:
  1033. {
  1034. res = a;
  1035. res.sign() = s;
  1036. return;
  1037. }
  1038. case cpp_bin_float<Digits, DigitBase, Allocator2, Exponent2, MinE2, MaxE2>::exponent_infinity:
  1039. if (b == 0)
  1040. res = std::numeric_limits<number<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::quiet_NaN().backend();
  1041. else
  1042. res = a;
  1043. return;
  1044. case cpp_bin_float<Digits, DigitBase, Allocator2, Exponent2, MinE2, MaxE2>::exponent_nan:
  1045. res = a;
  1046. return;
  1047. }
  1048. typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::double_rep_type dt;
  1049. using canon_ui_type = typename boost::multiprecision::detail::canonical<U, typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::double_rep_type>::type;
  1050. eval_multiply(dt, a.bits(), static_cast<canon_ui_type>(b));
  1051. res.exponent() = a.exponent();
  1052. copy_and_round(res, dt);
  1053. res.check_invariants();
  1054. res.sign() = s;
  1055. }
  1056. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, class U>
  1057. inline typename std::enable_if<boost::multiprecision::detail::is_unsigned<U>::value>::type eval_multiply(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& res, const U& b)
  1058. {
  1059. eval_multiply(res, res, b);
  1060. }
  1061. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE,
  1062. class Allocator2, class Exponent2, Exponent MinE2, Exponent MaxE2, class S>
  1063. inline typename std::enable_if<boost::multiprecision::detail::is_signed<S>::value && boost::multiprecision::detail::is_integral<S>::value>::type eval_multiply(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& res,
  1064. const cpp_bin_float<Digits, DigitBase, Allocator2, Exponent2, MinE2, MaxE2>& a, const S& b)
  1065. {
  1066. using ui_type = typename boost::multiprecision::detail::make_unsigned<S>::type;
  1067. eval_multiply(res, a, static_cast<ui_type>(boost::multiprecision::detail::unsigned_abs(b)));
  1068. if (b < 0)
  1069. res.negate();
  1070. }
  1071. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, class S>
  1072. inline typename std::enable_if<boost::multiprecision::detail::is_signed<S>::value && boost::multiprecision::detail::is_integral<S>::value>::type eval_multiply(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& res, const S& b)
  1073. {
  1074. eval_multiply(res, res, b);
  1075. }
  1076. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE,
  1077. class Allocator2, class Exponent2, Exponent MinE2, Exponent MaxE2,
  1078. class Allocator3, class Exponent3, Exponent MinE3, Exponent MaxE3>
  1079. inline void eval_divide(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& res,
  1080. const cpp_bin_float<Digits, DigitBase, Allocator2, Exponent2, MinE2, MaxE2>& u,
  1081. const cpp_bin_float<Digits, DigitBase, Allocator3, Exponent3, MinE3, MaxE3>& v)
  1082. {
  1083. #ifdef BOOST_MSVC
  1084. #pragma warning(push)
  1085. #pragma warning(disable : 6326) // comparison of two constants
  1086. #endif
  1087. using default_ops::eval_bit_test;
  1088. using default_ops::eval_get_sign;
  1089. using default_ops::eval_increment;
  1090. using default_ops::eval_qr;
  1091. using default_ops::eval_subtract;
  1092. //
  1093. // Special cases first:
  1094. //
  1095. switch (u.exponent())
  1096. {
  1097. case cpp_bin_float<Digits, DigitBase, Allocator2, Exponent2, MinE2, MaxE2>::exponent_zero:
  1098. {
  1099. switch (v.exponent())
  1100. {
  1101. case cpp_bin_float<Digits, DigitBase, Allocator3, Exponent3, MinE3, MaxE3>::exponent_zero:
  1102. case cpp_bin_float<Digits, DigitBase, Allocator3, Exponent3, MinE3, MaxE3>::exponent_nan:
  1103. res = std::numeric_limits<number<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::quiet_NaN().backend();
  1104. return;
  1105. }
  1106. bool s = u.sign() != v.sign();
  1107. res = u;
  1108. res.sign() = s;
  1109. return;
  1110. }
  1111. case cpp_bin_float<Digits, DigitBase, Allocator2, Exponent2, MinE2, MaxE2>::exponent_infinity:
  1112. {
  1113. switch (v.exponent())
  1114. {
  1115. case cpp_bin_float<Digits, DigitBase, Allocator3, Exponent3, MinE3, MaxE3>::exponent_infinity:
  1116. case cpp_bin_float<Digits, DigitBase, Allocator3, Exponent3, MinE3, MaxE3>::exponent_nan:
  1117. res = std::numeric_limits<number<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::quiet_NaN().backend();
  1118. return;
  1119. }
  1120. bool s = u.sign() != v.sign();
  1121. res = u;
  1122. res.sign() = s;
  1123. return;
  1124. }
  1125. case cpp_bin_float<Digits, DigitBase, Allocator2, Exponent2, MinE2, MaxE2>::exponent_nan:
  1126. res = std::numeric_limits<number<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::quiet_NaN().backend();
  1127. return;
  1128. }
  1129. switch (v.exponent())
  1130. {
  1131. case cpp_bin_float<Digits, DigitBase, Allocator3, Exponent3, MinE3, MaxE3>::exponent_zero:
  1132. {
  1133. bool s = u.sign() != v.sign();
  1134. res = std::numeric_limits<number<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::infinity().backend();
  1135. res.sign() = s;
  1136. return;
  1137. }
  1138. case cpp_bin_float<Digits, DigitBase, Allocator3, Exponent3, MinE3, MaxE3>::exponent_infinity:
  1139. res.exponent() = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero;
  1140. res.bits() = limb_type(0);
  1141. res.sign() = u.sign() != v.sign();
  1142. return;
  1143. case cpp_bin_float<Digits, DigitBase, Allocator3, Exponent3, MinE3, MaxE3>::exponent_nan:
  1144. res = std::numeric_limits<number<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::quiet_NaN().backend();
  1145. return;
  1146. }
  1147. // We can scale u and v so that both are integers, then perform integer
  1148. // division to obtain quotient q and remainder r, such that:
  1149. //
  1150. // q * v + r = u
  1151. //
  1152. // and hense:
  1153. //
  1154. // q + r/v = u/v
  1155. //
  1156. // From this, assuming q has cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count
  1157. // bits we only need to determine whether
  1158. // r/v is less than, equal to, or greater than 0.5 to determine rounding -
  1159. // this we can do with a shift and comparison.
  1160. //
  1161. // We can set the exponent and sign of the result up front:
  1162. //
  1163. if ((v.exponent() < 0) && (u.exponent() > 0))
  1164. {
  1165. // Check for overflow:
  1166. if (cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::max_exponent + v.exponent() < u.exponent() - 1)
  1167. {
  1168. res.exponent() = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity;
  1169. res.sign() = u.sign() != v.sign();
  1170. res.bits() = static_cast<limb_type>(0u);
  1171. return;
  1172. }
  1173. }
  1174. else if ((v.exponent() > 0) && (u.exponent() < 0))
  1175. {
  1176. // Check for underflow:
  1177. if (cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::min_exponent + v.exponent() > u.exponent())
  1178. {
  1179. // We will certainly underflow:
  1180. res.exponent() = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero;
  1181. res.sign() = u.sign() != v.sign();
  1182. res.bits() = static_cast<limb_type>(0u);
  1183. return;
  1184. }
  1185. }
  1186. res.exponent() = u.exponent() - v.exponent() - 1;
  1187. res.sign() = u.sign() != v.sign();
  1188. //
  1189. // Now get the quotient and remainder:
  1190. //
  1191. typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::double_rep_type t(u.bits()), t2(v.bits()), q, r;
  1192. eval_left_shift(t, cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count);
  1193. eval_qr(t, t2, q, r);
  1194. //
  1195. // We now have either "cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count"
  1196. // or "cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count+1" significant
  1197. // bits in q.
  1198. //
  1199. constexpr unsigned limb_bits = sizeof(limb_type) * CHAR_BIT;
  1200. if (eval_bit_test(q, cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count))
  1201. {
  1202. //
  1203. // OK we have cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count+1 bits,
  1204. // so we already have rounding info,
  1205. // we just need to changes things if the last bit is 1 and either the
  1206. // remainder is non-zero (ie we do not have a tie) or the quotient would
  1207. // be odd if it were shifted to the correct number of bits (ie a tiebreak).
  1208. //
  1209. BOOST_MP_ASSERT((eval_msb(q) == cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count));
  1210. if ((q.limbs()[0] & 1u) && (eval_get_sign(r) || (q.limbs()[0] & 2u)))
  1211. {
  1212. eval_increment(q);
  1213. }
  1214. }
  1215. else
  1216. {
  1217. //
  1218. // We have exactly "cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count" bits in q.
  1219. // Get rounding info, which we can get by comparing 2r with v.
  1220. // We want to call copy_and_round to handle rounding and general cleanup,
  1221. // so we'll left shift q and add some fake digits on the end to represent
  1222. // how we'll be rounding.
  1223. //
  1224. using local_exponent_type = typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type;
  1225. BOOST_MP_ASSERT((eval_msb(q) == cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - 1));
  1226. constexpr unsigned lshift = (cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count < limb_bits) ? 2 : limb_bits;
  1227. eval_left_shift(q, lshift);
  1228. res.exponent() -= static_cast<local_exponent_type>(lshift);
  1229. eval_left_shift(r, 1u);
  1230. int c = r.compare(v.bits());
  1231. if (c == 0)
  1232. q.limbs()[0] |= static_cast<limb_type>(1u) << (lshift - 1);
  1233. else if (c > 0)
  1234. q.limbs()[0] |= (static_cast<limb_type>(1u) << (lshift - 1)) + static_cast<limb_type>(1u);
  1235. }
  1236. copy_and_round(res, q);
  1237. #ifdef BOOST_MSVC
  1238. #pragma warning(pop)
  1239. #endif
  1240. }
  1241. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE,
  1242. class Allocator2, class Exponent2, Exponent MinE2, Exponent MaxE2>
  1243. inline void eval_divide(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& res,
  1244. const cpp_bin_float<Digits, DigitBase, Allocator2, Exponent2, MinE2, MaxE2>& arg)
  1245. {
  1246. eval_divide(res, res, arg);
  1247. }
  1248. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE,
  1249. class Allocator2, class Exponent2, Exponent MinE2, Exponent MaxE2, class U>
  1250. inline typename std::enable_if<boost::multiprecision::detail::is_unsigned<U>::value && (std::numeric_limits<U>::digits <= Digits)>::type eval_divide(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& res,
  1251. const cpp_bin_float<Digits, DigitBase, Allocator2, Exponent2, MinE2, MaxE2>& u, const U& v)
  1252. {
  1253. #ifdef BOOST_MSVC
  1254. #pragma warning(push)
  1255. #pragma warning(disable : 6326) // comparison of two constants
  1256. #endif
  1257. using default_ops::eval_bit_test;
  1258. using default_ops::eval_get_sign;
  1259. using default_ops::eval_increment;
  1260. using default_ops::eval_qr;
  1261. using default_ops::eval_subtract;
  1262. //
  1263. // Special cases first:
  1264. //
  1265. switch (u.exponent())
  1266. {
  1267. case cpp_bin_float<Digits, DigitBase, Allocator2, Exponent2, MinE2, MaxE2>::exponent_zero:
  1268. {
  1269. if (v == 0)
  1270. {
  1271. res = std::numeric_limits<number<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::quiet_NaN().backend();
  1272. return;
  1273. }
  1274. bool s = u.sign() != (v < 0);
  1275. res = u;
  1276. res.sign() = s;
  1277. return;
  1278. }
  1279. case cpp_bin_float<Digits, DigitBase, Allocator2, Exponent2, MinE2, MaxE2>::exponent_infinity:
  1280. res = u;
  1281. return;
  1282. case cpp_bin_float<Digits, DigitBase, Allocator2, Exponent2, MinE2, MaxE2>::exponent_nan:
  1283. res = std::numeric_limits<number<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::quiet_NaN().backend();
  1284. return;
  1285. }
  1286. if (v == 0)
  1287. {
  1288. bool s = u.sign();
  1289. res = std::numeric_limits<number<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::infinity().backend();
  1290. res.sign() = s;
  1291. return;
  1292. }
  1293. // We can scale u and v so that both are integers, then perform integer
  1294. // division to obtain quotient q and remainder r, such that:
  1295. //
  1296. // q * v + r = u
  1297. //
  1298. // and hense:
  1299. //
  1300. // q + r/v = u/v
  1301. //
  1302. // From this, assuming q has "cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count" cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count, we only need to determine whether
  1303. // r/v is less than, equal to, or greater than 0.5 to determine rounding -
  1304. // this we can do with a shift and comparison.
  1305. //
  1306. // We can set the exponent and sign of the result up front:
  1307. //
  1308. std::ptrdiff_t gb = static_cast<std::ptrdiff_t>(msb(v));
  1309. res.exponent() = u.exponent() - static_cast<Exponent>(gb) - static_cast<Exponent>(1);
  1310. res.sign() = u.sign();
  1311. //
  1312. // Now get the quotient and remainder:
  1313. //
  1314. typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::double_rep_type t(u.bits()), q, r;
  1315. eval_left_shift(t, static_cast<double_limb_type>(gb + 1));
  1316. eval_qr(t, number<typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::double_rep_type>::canonical_value(v), q, r);
  1317. //
  1318. // We now have either "cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count" or "cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count+1" significant cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count in q.
  1319. //
  1320. constexpr unsigned limb_bits = sizeof(limb_type) * CHAR_BIT;
  1321. if (eval_bit_test(q, cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count))
  1322. {
  1323. //
  1324. // OK we have cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count+1 cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count, so we already have rounding info,
  1325. // we just need to changes things if the last bit is 1 and the
  1326. // remainder is non-zero (ie we do not have a tie).
  1327. //
  1328. BOOST_MP_ASSERT((eval_msb(q) == cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count));
  1329. if ((q.limbs()[0] & 1u) && eval_get_sign(r))
  1330. {
  1331. eval_increment(q);
  1332. }
  1333. }
  1334. else
  1335. {
  1336. //
  1337. // We have exactly "cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count" cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count in q.
  1338. // Get rounding info, which we can get by comparing 2r with v.
  1339. // We want to call copy_and_round to handle rounding and general cleanup,
  1340. // so we'll left shift q and add some fake cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count on the end to represent
  1341. // how we'll be rounding.
  1342. //
  1343. using local_exponent_type = typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type;
  1344. BOOST_MP_ASSERT((eval_msb(q) == cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - 1));
  1345. constexpr unsigned lshift = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count < limb_bits ? 2 : limb_bits;
  1346. eval_left_shift(q, lshift);
  1347. res.exponent() -= static_cast<local_exponent_type>(lshift);
  1348. eval_left_shift(r, 1u);
  1349. int c = r.compare(number<typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::double_rep_type>::canonical_value(v));
  1350. if (c == 0)
  1351. q.limbs()[0] |= static_cast<limb_type>(1u) << (lshift - 1);
  1352. else if (c > 0)
  1353. q.limbs()[0] |= (static_cast<limb_type>(1u) << (lshift - 1)) + static_cast<limb_type>(1u);
  1354. }
  1355. copy_and_round(res, q);
  1356. #ifdef BOOST_MSVC
  1357. #pragma warning(pop)
  1358. #endif
  1359. }
  1360. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, class U>
  1361. inline typename std::enable_if<boost::multiprecision::detail::is_unsigned<U>::value && (std::numeric_limits<U>::digits <= Digits)>::type eval_divide(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& res, const U& v)
  1362. {
  1363. eval_divide(res, res, v);
  1364. }
  1365. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE,
  1366. class Allocator2, class Exponent2, Exponent MinE2, Exponent MaxE2, class S>
  1367. inline typename std::enable_if<boost::multiprecision::detail::is_signed<S>::value && boost::multiprecision::detail::is_integral<S>::value && (std::numeric_limits<S>::digits <= Digits)>::type eval_divide(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& res,
  1368. const cpp_bin_float<Digits, DigitBase, Allocator2, Exponent2, MinE2, MaxE2>& u, const S& v)
  1369. {
  1370. using ui_type = typename boost::multiprecision::detail::make_unsigned<S>::type;
  1371. eval_divide(res, u, static_cast<ui_type>(boost::multiprecision::detail::unsigned_abs(v)));
  1372. if (v < 0)
  1373. res.negate();
  1374. }
  1375. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, class S>
  1376. inline typename std::enable_if<boost::multiprecision::detail::is_signed<S>::value && boost::multiprecision::detail::is_integral<S>::value && (std::numeric_limits<S>::digits <= Digits)>::type eval_divide(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& res, const S& v)
  1377. {
  1378. eval_divide(res, res, v);
  1379. }
  1380. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
  1381. inline int eval_get_sign(const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& arg)
  1382. {
  1383. return arg.exponent() == cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero ? 0 : arg.sign() ? -1 : 1;
  1384. }
  1385. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
  1386. inline bool eval_is_zero(const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& arg)
  1387. {
  1388. return arg.exponent() == cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero;
  1389. }
  1390. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
  1391. inline bool eval_eq(const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& a, cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& b)
  1392. {
  1393. if (a.exponent() == b.exponent())
  1394. {
  1395. if (a.exponent() == cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero)
  1396. return true;
  1397. return (a.sign() == b.sign()) && (a.bits().compare(b.bits()) == 0) && (a.exponent() != cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan);
  1398. }
  1399. return false;
  1400. }
  1401. template <class I, unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
  1402. inline void convert_to_signed_int(I* res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& arg)
  1403. {
  1404. static constexpr int digits = std::numeric_limits<I>::is_specialized ? std::numeric_limits<I>::digits : sizeof(I) * CHAR_BIT - 1;
  1405. static constexpr I max_val = std::numeric_limits<I>::is_specialized ? (std::numeric_limits<I>::max)() : (((I(1) << (sizeof(I) * CHAR_BIT - 2)) - 1) << 1) + 1;
  1406. static constexpr I min_val = std::numeric_limits<I>::is_specialized ? (std::numeric_limits<I>::min)() : -max_val - 1;
  1407. switch (arg.exponent())
  1408. {
  1409. case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero:
  1410. *res = 0;
  1411. return;
  1412. case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan:
  1413. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Could not convert NaN to integer."));
  1414. return;
  1415. case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity:
  1416. *res = max_val;
  1417. if (arg.sign())
  1418. *res = -*res;
  1419. return;
  1420. }
  1421. using shift_type = typename std::conditional<sizeof(typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type) < sizeof(int), int, typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type>::type;
  1422. typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::rep_type man(arg.bits());
  1423. shift_type shift = (shift_type)cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - 1 - arg.exponent();
  1424. if (shift > (shift_type)cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - 1)
  1425. {
  1426. *res = 0;
  1427. return;
  1428. }
  1429. if (arg.sign() && (arg.compare(min_val) <= 0))
  1430. {
  1431. *res = min_val;
  1432. return;
  1433. }
  1434. else if (!arg.sign() && (arg.compare(max_val) >= 0))
  1435. {
  1436. *res = max_val;
  1437. return;
  1438. }
  1439. if (shift < 0)
  1440. {
  1441. if (static_cast<int>(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count) - static_cast<int>(shift) <= digits)
  1442. {
  1443. // We have more bits in long_long_type than the float, so it's OK to left shift:
  1444. eval_convert_to(res, man);
  1445. *res <<= -shift;
  1446. }
  1447. else
  1448. {
  1449. *res = (std::numeric_limits<I>::max)();
  1450. return;
  1451. }
  1452. }
  1453. else
  1454. {
  1455. eval_right_shift(man, static_cast<double_limb_type>(shift));
  1456. eval_convert_to(res, man);
  1457. }
  1458. if (arg.sign())
  1459. {
  1460. *res = -*res;
  1461. }
  1462. }
  1463. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
  1464. inline void eval_convert_to(long long* res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& arg)
  1465. {
  1466. convert_to_signed_int(res, arg);
  1467. }
  1468. #ifdef BOOST_HAS_INT128
  1469. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
  1470. inline void eval_convert_to(int128_type* res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& arg)
  1471. {
  1472. convert_to_signed_int(res, arg);
  1473. }
  1474. #endif
  1475. template <class I, unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
  1476. inline void convert_to_unsigned_int(I* res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& arg)
  1477. {
  1478. static constexpr int digits = std::numeric_limits<I>::is_specialized ? std::numeric_limits<I>::digits : sizeof(I) * CHAR_BIT;
  1479. static constexpr I max_val = std::numeric_limits<I>::is_specialized ? (std::numeric_limits<I>::max)() : ~static_cast<I>(0);
  1480. switch (arg.exponent())
  1481. {
  1482. case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero:
  1483. *res = 0;
  1484. return;
  1485. case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan:
  1486. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Could not convert NaN to integer."));
  1487. case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity:
  1488. *res = max_val;
  1489. return;
  1490. }
  1491. typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::rep_type man(arg.bits());
  1492. using shift_type = typename std::conditional<sizeof(typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type) < sizeof(int), int, typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type>::type;
  1493. shift_type shift = (shift_type)cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - 1 - arg.exponent();
  1494. if (shift > (shift_type)cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - 1)
  1495. {
  1496. *res = 0;
  1497. return;
  1498. }
  1499. else if (shift < 0)
  1500. {
  1501. if (cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - shift <= digits)
  1502. {
  1503. // We have more bits in ulong_long_type than the float, so it's OK to left shift:
  1504. eval_convert_to(res, man);
  1505. *res <<= -shift;
  1506. return;
  1507. }
  1508. *res = max_val;
  1509. return;
  1510. }
  1511. eval_right_shift(man, shift);
  1512. eval_convert_to(res, man);
  1513. }
  1514. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
  1515. inline void eval_convert_to(unsigned long long* res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& arg)
  1516. {
  1517. convert_to_unsigned_int(res, arg);
  1518. }
  1519. #ifdef BOOST_HAS_INT128
  1520. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
  1521. inline void eval_convert_to(uint128_type* res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& arg)
  1522. {
  1523. convert_to_unsigned_int(res, arg);
  1524. }
  1525. #endif
  1526. template <class Float, unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
  1527. inline typename std::enable_if<std::is_floating_point<Float>::value>::type eval_convert_to(Float* res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& original_arg)
  1528. {
  1529. using conv_type = cpp_bin_float<std::numeric_limits<Float>::digits, digit_base_2, void, Exponent, MinE, MaxE>;
  1530. using common_exp_type = typename std::common_type<typename conv_type::exponent_type, int>::type;
  1531. static constexpr int float_digits = boost::multiprecision::detail::is_float128<Float>::value ? 113 : std::numeric_limits<Float>::digits;
  1532. BOOST_MP_FLOAT128_USING using std::ldexp;
  1533. //
  1534. // Special cases first:
  1535. //
  1536. switch (original_arg.exponent())
  1537. {
  1538. case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero:
  1539. *res = 0;
  1540. if (original_arg.sign())
  1541. *res = -*res;
  1542. return;
  1543. case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan:
  1544. BOOST_IF_CONSTEXPR(boost::multiprecision::detail::is_float128<Float>::value)
  1545. {
  1546. *res = static_cast<Float>(std::numeric_limits<double>::quiet_NaN());
  1547. }
  1548. else
  1549. {
  1550. *res = std::numeric_limits<Float>::quiet_NaN();
  1551. }
  1552. return;
  1553. case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity:
  1554. BOOST_IF_CONSTEXPR(boost::multiprecision::detail::is_float128<Float>::value)
  1555. {
  1556. *res = static_cast<Float>((std::numeric_limits<double>::infinity)());
  1557. }
  1558. else
  1559. {
  1560. *res = (std::numeric_limits<Float>::infinity)();
  1561. }
  1562. if (original_arg.sign())
  1563. *res = -*res;
  1564. return;
  1565. }
  1566. //
  1567. // Check for super large exponent that must be converted to infinity:
  1568. //
  1569. if (original_arg.exponent() > (boost::multiprecision::detail::is_float128<Float>::value ? 16384 : std::numeric_limits<Float>::max_exponent))
  1570. {
  1571. BOOST_IF_CONSTEXPR(boost::multiprecision::detail::is_float128<Float>::value)
  1572. {
  1573. *res = static_cast<Float>(std::numeric_limits<double>::infinity());
  1574. }
  1575. else
  1576. {
  1577. *res = std::numeric_limits<Float>::has_infinity ? std::numeric_limits<Float>::infinity() : (std::numeric_limits<Float>::max)();
  1578. }
  1579. if (original_arg.sign())
  1580. *res = -*res;
  1581. return;
  1582. }
  1583. //
  1584. // Figure out how many digits we will have in our result,
  1585. // allowing for a possibly denormalized result:
  1586. //
  1587. common_exp_type digits_to_round_to = float_digits;
  1588. if (original_arg.exponent() < std::numeric_limits<Float>::min_exponent - 1)
  1589. {
  1590. common_exp_type diff = original_arg.exponent();
  1591. diff -= boost::multiprecision::detail::is_float128<Float>::value ? -16382 : std::numeric_limits<Float>::min_exponent - 1;
  1592. digits_to_round_to += diff;
  1593. }
  1594. if (digits_to_round_to < 0)
  1595. {
  1596. // Result must be zero:
  1597. *res = 0;
  1598. if (original_arg.sign())
  1599. *res = -*res;
  1600. return;
  1601. }
  1602. //
  1603. // Perform rounding first, then afterwards extract the digits:
  1604. //
  1605. cpp_bin_float<static_cast<unsigned>(float_digits), digit_base_2, Allocator, Exponent, 0, 0> arg;
  1606. typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::rep_type bits(original_arg.bits());
  1607. arg.exponent() = original_arg.exponent();
  1608. copy_and_round(arg, bits, (std::ptrdiff_t)digits_to_round_to);
  1609. common_exp_type e = arg.exponent();
  1610. e -= static_cast<common_exp_type>(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count) - 1;
  1611. constexpr std::size_t limbs_needed = static_cast<std::size_t>(float_digits) / (sizeof(*arg.bits().limbs()) * CHAR_BIT) + (static_cast<std::size_t>(float_digits) % (sizeof(*arg.bits().limbs()) * CHAR_BIT) ? 1 : 0);
  1612. std::size_t first_limb_needed = arg.bits().size() - limbs_needed;
  1613. *res = 0;
  1614. e += static_cast<common_exp_type>(first_limb_needed * sizeof(*arg.bits().limbs()) * CHAR_BIT);
  1615. while (first_limb_needed < arg.bits().size())
  1616. {
  1617. *res += ldexp(static_cast<Float>(arg.bits().limbs()[first_limb_needed]), static_cast<int>(e));
  1618. ++first_limb_needed;
  1619. e += static_cast<common_exp_type>(sizeof(*arg.bits().limbs()) * CHAR_BIT);
  1620. }
  1621. if (original_arg.sign())
  1622. *res = -*res;
  1623. }
  1624. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
  1625. inline void eval_frexp(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& arg, Exponent* e)
  1626. {
  1627. switch (arg.exponent())
  1628. {
  1629. case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero:
  1630. case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan:
  1631. case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity:
  1632. *e = 0;
  1633. res = arg;
  1634. return;
  1635. }
  1636. res = arg;
  1637. *e = arg.exponent() + 1;
  1638. res.exponent() = -1;
  1639. }
  1640. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, class I>
  1641. inline void eval_frexp(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& arg, I* pe)
  1642. {
  1643. Exponent e;
  1644. eval_frexp(res, arg, &e);
  1645. if ((e > (std::numeric_limits<I>::max)()) || (e < (std::numeric_limits<I>::min)()))
  1646. {
  1647. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Exponent was outside of the range of the argument type to frexp."));
  1648. }
  1649. *pe = static_cast<I>(e);
  1650. }
  1651. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
  1652. inline void eval_ldexp(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& arg, Exponent e)
  1653. {
  1654. switch (arg.exponent())
  1655. {
  1656. case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero:
  1657. case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan:
  1658. case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity:
  1659. res = arg;
  1660. return;
  1661. }
  1662. if ((e > 0) && (cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::max_exponent - e < arg.exponent()))
  1663. {
  1664. // Overflow:
  1665. res = std::numeric_limits<number<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::infinity().backend();
  1666. res.sign() = arg.sign();
  1667. }
  1668. else if ((e < 0) && (cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::min_exponent - e > arg.exponent()))
  1669. {
  1670. // Underflow:
  1671. res = limb_type(0);
  1672. }
  1673. else
  1674. {
  1675. res = arg;
  1676. res.exponent() += e;
  1677. }
  1678. }
  1679. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, class I>
  1680. inline typename std::enable_if<boost::multiprecision::detail::is_unsigned<I>::value>::type eval_ldexp(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& arg, I e)
  1681. {
  1682. using si_type = typename boost::multiprecision::detail::make_signed<I>::type;
  1683. if (e > static_cast<I>((std::numeric_limits<si_type>::max)()))
  1684. res = std::numeric_limits<number<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::infinity().backend();
  1685. else
  1686. eval_ldexp(res, arg, static_cast<si_type>(e));
  1687. }
  1688. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, class I>
  1689. inline typename std::enable_if<boost::multiprecision::detail::is_signed<I>::value && boost::multiprecision::detail::is_integral<I>::value>::type eval_ldexp(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& arg, I e)
  1690. {
  1691. if ((e > (std::numeric_limits<Exponent>::max)()) || (e < (std::numeric_limits<Exponent>::min)()))
  1692. {
  1693. res = std::numeric_limits<number<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::infinity().backend();
  1694. if (e < 0)
  1695. res.negate();
  1696. }
  1697. else
  1698. eval_ldexp(res, arg, static_cast<Exponent>(e));
  1699. }
  1700. /*
  1701. * Sign manipulation
  1702. */
  1703. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE,
  1704. unsigned Digits2, digit_base_type DigitBase2, class Allocator2, class Exponent2, Exponent MinE2, Exponent MaxE2>
  1705. inline void eval_abs(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& res, const cpp_bin_float<Digits2, DigitBase2, Allocator2, Exponent2, MinE2, MaxE2>& arg)
  1706. {
  1707. res = arg;
  1708. res.sign() = false;
  1709. }
  1710. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
  1711. inline void eval_abs(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& arg)
  1712. {
  1713. res = arg;
  1714. res.sign() = false;
  1715. }
  1716. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE,
  1717. unsigned Digits2, digit_base_type DigitBase2, class Allocator2, class Exponent2, Exponent MinE2, Exponent MaxE2>
  1718. inline void eval_fabs(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& res, const cpp_bin_float<Digits2, DigitBase2, Allocator2, Exponent2, MinE2, MaxE2>& arg)
  1719. {
  1720. res = arg;
  1721. res.sign() = false;
  1722. }
  1723. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
  1724. inline void eval_fabs(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& arg)
  1725. {
  1726. res = arg;
  1727. res.sign() = false;
  1728. }
  1729. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
  1730. inline int eval_fpclassify(const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& arg)
  1731. {
  1732. switch (arg.exponent())
  1733. {
  1734. case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero:
  1735. return FP_ZERO;
  1736. case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity:
  1737. return FP_INFINITE;
  1738. case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan:
  1739. return FP_NAN;
  1740. }
  1741. return FP_NORMAL;
  1742. }
  1743. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
  1744. inline void eval_sqrt(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& arg)
  1745. {
  1746. using default_ops::eval_bit_test;
  1747. using default_ops::eval_increment;
  1748. using default_ops::eval_integer_sqrt;
  1749. switch (arg.exponent())
  1750. {
  1751. case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan:
  1752. errno = EDOM;
  1753. // fallthrough...
  1754. case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero:
  1755. res = arg;
  1756. return;
  1757. case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity:
  1758. if (arg.sign())
  1759. {
  1760. res = std::numeric_limits<number<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::quiet_NaN().backend();
  1761. errno = EDOM;
  1762. }
  1763. else
  1764. res = arg;
  1765. return;
  1766. }
  1767. if (arg.sign())
  1768. {
  1769. res = std::numeric_limits<number<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::quiet_NaN().backend();
  1770. errno = EDOM;
  1771. return;
  1772. }
  1773. typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::double_rep_type t(arg.bits()), r, s;
  1774. eval_left_shift(t, arg.exponent() & 1 ? cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count : cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - 1);
  1775. eval_integer_sqrt(s, r, t);
  1776. if (!eval_bit_test(s, cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count))
  1777. {
  1778. // We have exactly the right number of cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count in the result, round as required:
  1779. if (s.compare(r) < 0)
  1780. {
  1781. eval_increment(s);
  1782. }
  1783. }
  1784. typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type ae = arg.exponent();
  1785. res.exponent() = ae / 2;
  1786. res.sign() = false;
  1787. if ((ae & 1) && (ae < 0))
  1788. --res.exponent();
  1789. copy_and_round(res, s);
  1790. }
  1791. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
  1792. inline void eval_floor(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& arg)
  1793. {
  1794. using default_ops::eval_increment;
  1795. switch (arg.exponent())
  1796. {
  1797. case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan:
  1798. errno = EDOM;
  1799. // fallthrough...
  1800. case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero:
  1801. case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity:
  1802. res = arg;
  1803. return;
  1804. }
  1805. using shift_type = typename std::conditional<sizeof(typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type) < sizeof(int), int, typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type>::type;
  1806. shift_type shift =
  1807. (shift_type)cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - arg.exponent() - 1;
  1808. if ((arg.exponent() > (shift_type)cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::max_exponent) || (shift <= 0))
  1809. {
  1810. // Either arg is already an integer, or a special value:
  1811. res = arg;
  1812. return;
  1813. }
  1814. if (shift >= (shift_type)cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count)
  1815. {
  1816. res = static_cast<signed_limb_type>(arg.sign() ? -1 : 0);
  1817. return;
  1818. }
  1819. bool fractional = (shift_type)eval_lsb(arg.bits()) < shift;
  1820. res = arg;
  1821. eval_right_shift(res.bits(), static_cast<double_limb_type>(shift));
  1822. if (fractional && res.sign())
  1823. {
  1824. eval_increment(res.bits());
  1825. const std::ptrdiff_t shift_check =
  1826. static_cast<std::ptrdiff_t>(static_cast<std::ptrdiff_t>(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count) - 1 - static_cast<std::ptrdiff_t>(shift));
  1827. if (static_cast<std::ptrdiff_t>(eval_msb(res.bits())) != shift_check)
  1828. {
  1829. // Must have extended result by one bit in the increment:
  1830. --shift;
  1831. ++res.exponent();
  1832. }
  1833. }
  1834. eval_left_shift(res.bits(), static_cast<double_limb_type>(shift));
  1835. }
  1836. template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
  1837. inline void eval_ceil(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>& arg)
  1838. {
  1839. using default_ops::eval_increment;
  1840. switch (arg.exponent())
  1841. {
  1842. case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity:
  1843. errno = EDOM;
  1844. // fallthrough...
  1845. case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero:
  1846. case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan:
  1847. res = arg;
  1848. return;
  1849. }
  1850. using shift_type = typename std::conditional<sizeof(typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type) < sizeof(int), int, typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type>::type;
  1851. shift_type shift = (shift_type)cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - arg.exponent() - 1;
  1852. if ((arg.exponent() > (shift_type)cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::max_exponent) || (shift <= 0))
  1853. {
  1854. // Either arg is already an integer, or a special value:
  1855. res = arg;
  1856. return;
  1857. }
  1858. if (shift >= (shift_type)cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count)
  1859. {
  1860. bool s = arg.sign(); // takes care of signed zeros
  1861. res = static_cast<signed_limb_type>(arg.sign() ? 0 : 1);
  1862. res.sign() = s;
  1863. return;
  1864. }
  1865. bool fractional = (shift_type)eval_lsb(arg.bits()) < shift;
  1866. res = arg;
  1867. eval_right_shift(res.bits(), shift);
  1868. if (fractional && !res.sign())
  1869. {
  1870. eval_increment(res.bits());
  1871. if ((std::ptrdiff_t)eval_msb(res.bits()) != cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - 1 - shift)
  1872. {
  1873. // Must have extended result by one bit in the increment:
  1874. --shift;
  1875. ++res.exponent();
  1876. }
  1877. }
  1878. eval_left_shift(res.bits(), shift);
  1879. }
  1880. template <unsigned D1, backends::digit_base_type B1, class A1, class E1, E1 M1, E1 M2>
  1881. int eval_signbit(const cpp_bin_float<D1, B1, A1, E1, M1, M2>& val)
  1882. {
  1883. return val.sign();
  1884. }
  1885. template <unsigned D1, backends::digit_base_type B1, class A1, class E1, E1 M1, E1 M2>
  1886. inline std::size_t hash_value(const cpp_bin_float<D1, B1, A1, E1, M1, M2>& val)
  1887. {
  1888. std::size_t result = hash_value(val.bits());
  1889. boost::multiprecision::detail::hash_combine(result, val.exponent(), val.sign());
  1890. return result;
  1891. }
  1892. } // namespace backends
  1893. namespace detail {
  1894. template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinExponent, Exponent MaxExponent>
  1895. struct transcendental_reduction_type<boost::multiprecision::backends::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinExponent, MaxExponent> >
  1896. {
  1897. //
  1898. // The type used for trigonometric reduction needs 3 times the precision of the base type.
  1899. // This is double the precision of the original type, plus the largest exponent supported.
  1900. // As a practical measure the largest argument supported is 1/eps, as supporting larger
  1901. // arguments requires the division of argument by PI/2 to also be done at higher precision,
  1902. // otherwise the result (an integer) can not be represented exactly.
  1903. //
  1904. // See ARGUMENT REDUCTION FOR HUGE ARGUMENTS. K C Ng.
  1905. //
  1906. using type = boost::multiprecision::backends::cpp_bin_float<
  1907. boost::multiprecision::backends::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinExponent, MaxExponent>::bit_count * 3,
  1908. boost::multiprecision::backends::digit_base_2,
  1909. Allocator, Exponent, MinExponent, MaxExponent>;
  1910. };
  1911. #ifdef BOOST_HAS_INT128
  1912. template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinExponent, Exponent MaxExponent>
  1913. struct is_convertible_arithmetic<int128_type, boost::multiprecision::backends::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinExponent, MaxExponent> > : public std::true_type
  1914. {};
  1915. template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinExponent, Exponent MaxExponent>
  1916. struct is_convertible_arithmetic<uint128_type, boost::multiprecision::backends::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinExponent, MaxExponent> > : public std::true_type
  1917. {};
  1918. #endif
  1919. } // namespace detail
  1920. template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Exponent, Exponent MinE, Exponent MaxE, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  1921. inline boost::multiprecision::number<boost::multiprecision::backends::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates>
  1922. copysign BOOST_PREVENT_MACRO_SUBSTITUTION(
  1923. const boost::multiprecision::number<boost::multiprecision::backends::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates>& a,
  1924. const boost::multiprecision::number<boost::multiprecision::backends::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates>& b)
  1925. {
  1926. boost::multiprecision::number<boost::multiprecision::backends::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> res(a);
  1927. res.backend().sign() = b.backend().sign();
  1928. return res;
  1929. }
  1930. template <unsigned Digits, backends::digit_base_type DigitBase, class Exponent, Exponent MinE, Exponent MaxE, class Allocator>
  1931. struct number_category<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > : public std::integral_constant<int, boost::multiprecision::number_kind_floating_point>
  1932. {};
  1933. template <unsigned Digits, backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, class Allocator2, class Exponent2, Exponent MinE2, Exponent MaxE2>
  1934. struct is_equivalent_number_type<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, cpp_bin_float<Digits, DigitBase, Allocator2, Exponent2, MinE2, MaxE2> >
  1935. : public std::integral_constant<bool, true> {};
  1936. } // namespace multiprecision
  1937. namespace math {
  1938. using boost::multiprecision::copysign;
  1939. using boost::multiprecision::signbit;
  1940. } // namespace math
  1941. } // namespace boost
  1942. #include <boost/multiprecision/cpp_bin_float/io.hpp>
  1943. #include <boost/multiprecision/cpp_bin_float/transcendental.hpp>
  1944. namespace std {
  1945. //
  1946. // numeric_limits [partial] specializations for the types declared in this header:
  1947. //
  1948. template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
  1949. class numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >
  1950. {
  1951. using number_type = boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates>;
  1952. private:
  1953. //
  1954. // Functions to calculate cached values stored in static values:
  1955. //
  1956. static number_type get_min()
  1957. {
  1958. using ui_type = typename std::tuple_element<0, typename number_type::backend_type::unsigned_types>::type;
  1959. number_type value(ui_type(1u));
  1960. value.backend().exponent() = boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::min_exponent;
  1961. return value;
  1962. }
  1963. #ifdef BOOST_MSVC
  1964. #pragma warning(push)
  1965. #pragma warning(disable : 4127) // conditional expression is constant
  1966. #endif
  1967. static number_type get_max()
  1968. {
  1969. number_type value;
  1970. BOOST_IF_CONSTEXPR(std::is_void<Allocator>::value)
  1971. eval_complement(value.backend().bits(), value.backend().bits());
  1972. else
  1973. {
  1974. // We jump through hoops here using the backend type directly just to keep VC12 happy
  1975. // (ie compiler workaround, for very strange compiler bug):
  1976. using boost::multiprecision::default_ops::eval_add;
  1977. using boost::multiprecision::default_ops::eval_decrement;
  1978. using boost::multiprecision::default_ops::eval_left_shift;
  1979. using int_backend_type = typename number_type::backend_type::rep_type;
  1980. using ui_type = typename std::tuple_element<0, typename int_backend_type::unsigned_types>::type;
  1981. int_backend_type i;
  1982. i = ui_type(1u);
  1983. eval_left_shift(i, boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - 1);
  1984. int_backend_type j(i);
  1985. eval_decrement(i);
  1986. eval_add(j, i);
  1987. value.backend().bits() = j;
  1988. }
  1989. value.backend().exponent() = boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::max_exponent;
  1990. return value;
  1991. }
  1992. #ifdef BOOST_MSVC
  1993. #pragma warning(pop)
  1994. #endif
  1995. static number_type get_epsilon()
  1996. {
  1997. using ui_type = typename std::tuple_element<0, typename number_type::backend_type::unsigned_types>::type;
  1998. number_type value(ui_type(1u));
  1999. return ldexp(value, 1 - static_cast<int>(digits));
  2000. }
  2001. // What value should this be????
  2002. static number_type get_round_error()
  2003. {
  2004. // returns 0.5
  2005. return ldexp(number_type(1u), -1);
  2006. }
  2007. static number_type get_infinity()
  2008. {
  2009. number_type value;
  2010. value.backend().exponent() = boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity;
  2011. return value;
  2012. }
  2013. static number_type get_quiet_NaN()
  2014. {
  2015. number_type value;
  2016. value.backend().exponent() = boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan;
  2017. return value;
  2018. }
  2019. public:
  2020. static constexpr bool is_specialized = true;
  2021. static number_type(min)()
  2022. {
  2023. // C++11 thread safe static initialization:
  2024. static number_type value = get_min();
  2025. return value;
  2026. }
  2027. static number_type(max)()
  2028. {
  2029. // C++11 thread safe static initialization:
  2030. static number_type value = get_max();
  2031. return value;
  2032. }
  2033. static constexpr number_type lowest()
  2034. {
  2035. return -(max)();
  2036. }
  2037. static constexpr int digits = boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count;
  2038. static constexpr int digits10 = boost::multiprecision::detail::calc_digits10<static_cast<unsigned>(digits)>::value;
  2039. // Is this really correct???
  2040. static constexpr int max_digits10 = boost::multiprecision::detail::calc_max_digits10<static_cast<unsigned>(digits)>::value;
  2041. static constexpr bool is_signed = true;
  2042. static constexpr bool is_integer = false;
  2043. static constexpr bool is_exact = false;
  2044. static constexpr int radix = 2;
  2045. static number_type epsilon()
  2046. {
  2047. // C++11 thread safe static initialization:
  2048. static number_type value = get_epsilon();
  2049. return value;
  2050. }
  2051. // What value should this be????
  2052. static number_type round_error()
  2053. {
  2054. // returns 0.5
  2055. // C++11 thread safe static initialization:
  2056. static number_type value = get_round_error();
  2057. return value;
  2058. }
  2059. static constexpr typename boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type min_exponent = boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::min_exponent;
  2060. static constexpr typename boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type min_exponent10 = (min_exponent / 1000) * 301L;
  2061. static constexpr typename boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type max_exponent = boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::max_exponent;
  2062. static constexpr typename boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type max_exponent10 = (max_exponent / 1000) * 301L;
  2063. static constexpr bool has_infinity = true;
  2064. static constexpr bool has_quiet_NaN = true;
  2065. static constexpr bool has_signaling_NaN = false;
  2066. #ifdef _MSC_VER
  2067. #pragma warning(push)
  2068. #pragma warning(disable:4996)
  2069. #endif
  2070. static constexpr float_denorm_style has_denorm = denorm_absent;
  2071. #ifdef _MSC_VER
  2072. #pragma warning(pop)
  2073. #endif
  2074. static constexpr bool has_denorm_loss = false;
  2075. static number_type infinity()
  2076. {
  2077. // C++11 thread safe static initialization:
  2078. static number_type value = get_infinity();
  2079. return value;
  2080. }
  2081. static number_type quiet_NaN()
  2082. {
  2083. // C++11 thread safe static initialization:
  2084. static number_type value = get_quiet_NaN();
  2085. return value;
  2086. }
  2087. static constexpr number_type signaling_NaN()
  2088. {
  2089. return number_type(0);
  2090. }
  2091. static constexpr number_type denorm_min() { return get_min(); }
  2092. static constexpr bool is_iec559 = false;
  2093. static constexpr bool is_bounded = true;
  2094. static constexpr bool is_modulo = false;
  2095. static constexpr bool traps = true;
  2096. static constexpr bool tinyness_before = false;
  2097. static constexpr float_round_style round_style = round_to_nearest;
  2098. };
  2099. template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
  2100. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::digits;
  2101. template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
  2102. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::digits10;
  2103. template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
  2104. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::max_digits10;
  2105. template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
  2106. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::is_signed;
  2107. template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
  2108. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::is_integer;
  2109. template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
  2110. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::is_exact;
  2111. template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
  2112. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::radix;
  2113. template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
  2114. constexpr typename boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::min_exponent;
  2115. template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
  2116. constexpr typename boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::min_exponent10;
  2117. template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
  2118. constexpr typename boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::max_exponent;
  2119. template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
  2120. constexpr typename boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::max_exponent10;
  2121. template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
  2122. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::has_infinity;
  2123. template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
  2124. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::has_quiet_NaN;
  2125. template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
  2126. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::has_signaling_NaN;
  2127. #ifdef _MSC_VER
  2128. #pragma warning(push)
  2129. #pragma warning(disable:4996)
  2130. #endif
  2131. template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
  2132. constexpr float_denorm_style numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::has_denorm;
  2133. #ifdef _MSC_VER
  2134. #pragma warning(pop)
  2135. #endif
  2136. template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
  2137. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::has_denorm_loss;
  2138. template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
  2139. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::is_iec559;
  2140. template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
  2141. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::is_bounded;
  2142. template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
  2143. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::is_modulo;
  2144. template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
  2145. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::traps;
  2146. template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
  2147. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::tinyness_before;
  2148. template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
  2149. constexpr float_round_style numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::round_style;
  2150. } // namespace std
  2151. #endif