cpp_bin_float.hpp 105 KB

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