cpp_dec_float.hpp 167 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright Christopher Kormanyos 2002 - 2025.
  3. // Copyright John Maddock 2011 -2025.
  4. // Distributed under the Boost
  5. // Software License, Version 1.0. (See accompanying file
  6. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // This work is based on an earlier work:
  9. // "Algorithm 910: A Portable C++ Multiple-Precision System for Special-Function Calculations",
  10. // in ACM TOMS, {VOL 37, ISSUE 4, (February 2011)} (C) ACM, 2011. http://doi.acm.org/10.1145/1916461.1916469
  11. //
  12. #ifndef BOOST_MP_CPP_DEC_FLOAT_HPP
  13. #define BOOST_MP_CPP_DEC_FLOAT_HPP
  14. #include <boost/multiprecision/detail/standalone_config.hpp>
  15. #include <boost/multiprecision/number.hpp>
  16. #include <boost/multiprecision/detail/fpclassify.hpp>
  17. #include <boost/multiprecision/detail/dynamic_array.hpp>
  18. #include <boost/multiprecision/detail/hash.hpp>
  19. #include <boost/multiprecision/detail/float128_functions.hpp>
  20. #include <boost/multiprecision/detail/itos.hpp>
  21. #include <boost/multiprecision/detail/static_array.hpp>
  22. #include <boost/multiprecision/detail/tables.hpp>
  23. #include <boost/multiprecision/detail/no_exceptions_support.hpp>
  24. #include <boost/multiprecision/detail/assert.hpp>
  25. #ifdef BOOST_MP_MATH_AVAILABLE
  26. //
  27. // Headers required for Boost.Math integration:
  28. //
  29. #include <boost/math/policies/policy.hpp>
  30. //
  31. // Some includes we need from Boost.Math, since we rely on that library to provide these functions:
  32. //
  33. #include <boost/math/special_functions/acosh.hpp>
  34. #include <boost/math/special_functions/asinh.hpp>
  35. #include <boost/math/special_functions/atanh.hpp>
  36. #include <boost/math/special_functions/cbrt.hpp>
  37. #include <boost/math/special_functions/expm1.hpp>
  38. #include <boost/math/special_functions/gamma.hpp>
  39. #endif
  40. #include <algorithm>
  41. #include <array>
  42. #include <cmath>
  43. #include <cstdint>
  44. #include <cstdlib>
  45. #include <initializer_list>
  46. #include <iomanip>
  47. #include <ios>
  48. #include <limits>
  49. #include <locale>
  50. #include <stdexcept>
  51. #include <sstream>
  52. #include <string>
  53. #ifdef BOOST_MSVC
  54. #pragma warning(push)
  55. #pragma warning(disable : 6326) // comparison of two constants
  56. #endif
  57. namespace boost {
  58. namespace multiprecision {
  59. template <unsigned Digits10, class ExponentType, class Allocator>
  60. struct number_category<backends::cpp_dec_float<Digits10, ExponentType, Allocator> > : public std::integral_constant<int, number_kind_floating_point>
  61. {};
  62. namespace backends {
  63. template <unsigned Digits10, class ExponentType, class Allocator>
  64. class cpp_dec_float // LCOV_EXCL_LINE This causes a false negative on lcov coverage test.
  65. {
  66. private:
  67. // Perform some static sanity checks.
  68. static_assert(boost::multiprecision::detail::is_signed<ExponentType>::value,
  69. "ExponentType must be a signed built in integer type.");
  70. static_assert(sizeof(ExponentType) > 1,
  71. "ExponentType is too small.");
  72. static_assert(Digits10 < UINT32_C(0x80000000),
  73. "Digits10 exceeds the maximum.");
  74. // Private class-local constants.
  75. static constexpr std::int32_t cpp_dec_float_digits10_limit_lo = INT32_C(9);
  76. static constexpr std::int32_t cpp_dec_float_digits10_limit_hi = static_cast<std::int32_t>((std::numeric_limits<std::int32_t>::max)() - 100);
  77. static constexpr std::int32_t cpp_dec_float_elem_digits10 = INT32_C(8);
  78. static constexpr std::int32_t cpp_dec_float_elem_mask = INT32_C(100000000);
  79. static constexpr std::int32_t cpp_dec_float_elems_for_kara = static_cast<std::int32_t>(128 + 1);
  80. public:
  81. using signed_types = std::tuple<long long> ;
  82. using unsigned_types = std::tuple<unsigned long long>;
  83. using float_types = std::tuple<double, long double>;
  84. using exponent_type = ExponentType;
  85. // Public class-local constants.
  86. static constexpr std::int32_t cpp_dec_float_radix = INT32_C(10);
  87. static constexpr std::int32_t cpp_dec_float_digits10 = ((static_cast<std::int32_t>(Digits10) < cpp_dec_float_digits10_limit_lo) ? cpp_dec_float_digits10_limit_lo : ((static_cast<std::int32_t>(Digits10) > cpp_dec_float_digits10_limit_hi) ? cpp_dec_float_digits10_limit_hi : static_cast<std::int32_t>(Digits10)));
  88. static constexpr exponent_type cpp_dec_float_max_exp10 = (static_cast<exponent_type>(1) << (std::numeric_limits<exponent_type>::digits - 5));
  89. static constexpr exponent_type cpp_dec_float_min_exp10 = -cpp_dec_float_max_exp10;
  90. static constexpr exponent_type cpp_dec_float_max_exp = cpp_dec_float_max_exp10;
  91. static constexpr exponent_type cpp_dec_float_min_exp = cpp_dec_float_min_exp10;
  92. static_assert(cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_max_exp10 == -cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_min_exp10, "Failed exponent range check");
  93. static_assert(0 == cpp_dec_float_max_exp10 % cpp_dec_float_elem_digits10, "Failed digit sanity check");
  94. private:
  95. // There are three guard limbs.
  96. // 1) The first limb has 'play' from 1...8 decimal digits.
  97. // 2) The last limb also has 'play' from 1...8 decimal digits.
  98. // 3) One limb can get lost when justifying after multiply.
  99. static constexpr std::int32_t cpp_dec_float_elem_number = static_cast<std::int32_t>(((Digits10 / cpp_dec_float_elem_digits10) + (((Digits10 % cpp_dec_float_elem_digits10) != 0) ? 1 : 0)) + 3);
  100. public:
  101. static constexpr std::int32_t cpp_dec_float_max_digits10 = static_cast<std::int32_t>(cpp_dec_float_elem_number * cpp_dec_float_elem_digits10);
  102. private:
  103. using array_type =
  104. typename std::conditional<std::is_void<Allocator>::value,
  105. detail::static_array <std::uint32_t, static_cast<std::uint32_t>(cpp_dec_float_elem_number)>,
  106. detail::dynamic_array<std::uint32_t, static_cast<std::uint32_t>(cpp_dec_float_elem_number), Allocator> >::type;
  107. typedef enum enum_fpclass_type
  108. {
  109. cpp_dec_float_finite,
  110. cpp_dec_float_inf,
  111. cpp_dec_float_NaN
  112. } fpclass_type;
  113. array_type data { };
  114. exponent_type exp { };
  115. bool neg { };
  116. fpclass_type fpclass { cpp_dec_float_finite };
  117. std::int32_t prec_elem { cpp_dec_float_elem_number };
  118. // Private constructor from the floating-point class type.
  119. explicit cpp_dec_float(fpclass_type c) : fpclass(c) { }
  120. // Constructor from an initializer_list, an optional
  121. // (value-aligned) exponent and a Boolean sign.
  122. static cpp_dec_float from_lst(std::initializer_list<std::uint32_t> lst,
  123. const exponent_type e = 0,
  124. const bool n = false)
  125. {
  126. cpp_dec_float a;
  127. a.data = array_type(lst);
  128. a.exp = e;
  129. a.neg = n;
  130. a.fpclass = cpp_dec_float_finite;
  131. a.prec_elem = cpp_dec_float_elem_number;
  132. return a;
  133. } // LCOV_EXCL_LINE This causes a false negative on lcov coverage test.
  134. public:
  135. // Public Constructors
  136. cpp_dec_float() noexcept(noexcept(array_type())) { }
  137. cpp_dec_float(const char* s)
  138. {
  139. *this = s;
  140. }
  141. template <class I>
  142. cpp_dec_float(I i, typename std::enable_if<( boost::multiprecision::detail::is_unsigned<I>::value
  143. && (sizeof(I) <= sizeof(long long)))>::type* = nullptr)
  144. {
  145. from_unsigned_long_long(i);
  146. }
  147. template <class I>
  148. cpp_dec_float(I i,
  149. typename std::enable_if<( boost::multiprecision::detail::is_signed<I>::value
  150. && boost::multiprecision::detail::is_integral<I>::value
  151. && (sizeof(I) <= sizeof(long long)))>::type* = nullptr)
  152. {
  153. if (i < 0)
  154. {
  155. from_unsigned_long_long(boost::multiprecision::detail::unsigned_abs(i));
  156. negate();
  157. }
  158. else
  159. from_unsigned_long_long(static_cast<unsigned long long>(i));
  160. }
  161. cpp_dec_float(const cpp_dec_float& f) noexcept(noexcept(array_type(std::declval<const array_type&>())))
  162. : data(f.data),
  163. exp(f.exp),
  164. neg(f.neg),
  165. fpclass(static_cast<enum_fpclass_type>(static_cast<int>(f.fpclass))),
  166. prec_elem(f.prec_elem) {}
  167. template <unsigned D, class ET, class A>
  168. cpp_dec_float(const cpp_dec_float<D, ET, A>& f, typename std::enable_if<D <= Digits10>::type* = nullptr)
  169. : exp(f.exp),
  170. neg(f.neg),
  171. fpclass(static_cast<enum_fpclass_type>(static_cast<int>(f.fpclass)))
  172. {
  173. std::copy(f.data.begin(), f.data.begin() + f.prec_elem, data.begin());
  174. }
  175. template <unsigned D, class ET, class A>
  176. explicit cpp_dec_float(const cpp_dec_float<D, ET, A>& f, typename std::enable_if< !(D <= Digits10)>::type* = nullptr)
  177. : exp(f.exp),
  178. neg(f.neg),
  179. fpclass(static_cast<enum_fpclass_type>(static_cast<int>(f.fpclass)))
  180. {
  181. // TODO: this doesn't round!
  182. std::copy(f.data.begin(), f.data.begin() + prec_elem, data.begin());
  183. }
  184. template <class F>
  185. cpp_dec_float(const F val, typename std::enable_if<std::is_floating_point<F>::value>::type* = nullptr)
  186. {
  187. *this = val;
  188. }
  189. cpp_dec_float(const double mantissa, const exponent_type exp10);
  190. std::size_t hash() const
  191. {
  192. std::size_t result = 0;
  193. for (int i = 0; i < prec_elem; ++i)
  194. boost::multiprecision::detail::hash_combine(result, data[i]);
  195. boost::multiprecision::detail::hash_combine(result, exp, neg, static_cast<std::size_t>(fpclass));
  196. return result;
  197. }
  198. // Specific special values.
  199. static const cpp_dec_float& nan () { static const cpp_dec_float val(cpp_dec_float_NaN); return val; }
  200. static const cpp_dec_float& inf () { static const cpp_dec_float val(cpp_dec_float_inf); return val; }
  201. static const cpp_dec_float& (max)() { static const cpp_dec_float val(from_lst({ std::uint32_t(1u) }, cpp_dec_float_max_exp10)); return val; }
  202. static const cpp_dec_float& (min)() { static const cpp_dec_float val(from_lst({ std::uint32_t(1u) }, cpp_dec_float_min_exp10)); return val; }
  203. static const cpp_dec_float& zero() { static const cpp_dec_float val(from_lst({ std::uint32_t(0u) })); return val; }
  204. static const cpp_dec_float& one () { static const cpp_dec_float val(from_lst({ std::uint32_t(1u) })); return val; }
  205. static const cpp_dec_float& two () { static const cpp_dec_float val(from_lst({ std::uint32_t(2u) })); return val; }
  206. static const cpp_dec_float& half() { static const cpp_dec_float val(from_lst({ std::uint32_t(cpp_dec_float_elem_mask / 2)}, -8)); return val; }
  207. static const cpp_dec_float& double_min() { static const cpp_dec_float val((std::numeric_limits<double>::min)()); return val; }
  208. static const cpp_dec_float& double_max() { static const cpp_dec_float val((std::numeric_limits<double>::max)()); return val; }
  209. static const cpp_dec_float& long_double_min()
  210. {
  211. #ifdef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  212. static const cpp_dec_float val(static_cast<long double>((std::numeric_limits<double>::min)()));
  213. #else
  214. static const cpp_dec_float val((std::numeric_limits<long double>::min)());
  215. #endif
  216. return val;
  217. }
  218. static const cpp_dec_float& long_double_max()
  219. {
  220. #ifdef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  221. static const cpp_dec_float val(static_cast<long double>((std::numeric_limits<double>::max)()));
  222. #else
  223. static const cpp_dec_float val((std::numeric_limits<long double>::max)());
  224. #endif
  225. return val;
  226. }
  227. static const cpp_dec_float& long_long_max () { static const cpp_dec_float val((std::numeric_limits<long long>::max)()); return val; }
  228. static const cpp_dec_float& long_long_min () { static const cpp_dec_float val((std::numeric_limits<long long>::min)()); return val; }
  229. static const cpp_dec_float& ulong_long_max() { static const cpp_dec_float val((std::numeric_limits<unsigned long long>::max)()); return val; }
  230. static const cpp_dec_float& eps()
  231. {
  232. static const cpp_dec_float val
  233. (
  234. from_lst
  235. (
  236. {
  237. (std::uint32_t) detail::pow10_maker((std::uint32_t) ((std::int32_t) (INT32_C(1) + (std::int32_t) (((cpp_dec_float_digits10 / cpp_dec_float_elem_digits10) + ((cpp_dec_float_digits10 % cpp_dec_float_elem_digits10) != 0 ? 1 : 0)) * cpp_dec_float_elem_digits10)) - cpp_dec_float_digits10))
  238. },
  239. -(exponent_type) (((cpp_dec_float_digits10 / cpp_dec_float_elem_digits10) + ((cpp_dec_float_digits10 % cpp_dec_float_elem_digits10) != 0 ? 1 : 0)) * cpp_dec_float_elem_digits10)
  240. )
  241. );
  242. return val;
  243. }
  244. // Basic operations.
  245. cpp_dec_float& operator=(const cpp_dec_float& v) noexcept(noexcept(std::declval<array_type&>() = std::declval<const array_type&>()))
  246. {
  247. data = v.data;
  248. exp = v.exp;
  249. neg = v.neg;
  250. fpclass = v.fpclass;
  251. prec_elem = v.prec_elem;
  252. return *this;
  253. }
  254. template <unsigned D>
  255. cpp_dec_float& operator=(const cpp_dec_float<D>& f)
  256. {
  257. exp = f.exp;
  258. neg = f.neg;
  259. fpclass = static_cast<enum_fpclass_type>(static_cast<int>(f.fpclass));
  260. unsigned elems = (std::min)(f.prec_elem, cpp_dec_float_elem_number);
  261. std::copy(f.data.begin(), f.data.begin() + elems, data.begin());
  262. std::fill(data.begin() + elems, data.end(), 0);
  263. prec_elem = cpp_dec_float_elem_number;
  264. return *this;
  265. }
  266. cpp_dec_float& operator=(long long v)
  267. {
  268. if (v < 0)
  269. {
  270. from_unsigned_long_long(boost::multiprecision::detail::unsigned_abs(v));
  271. negate();
  272. }
  273. else
  274. {
  275. using local_ulonglong_type = typename boost::multiprecision::detail::make_unsigned<long long>::type;
  276. from_unsigned_long_long(static_cast<local_ulonglong_type>(v));
  277. }
  278. return *this;
  279. }
  280. cpp_dec_float& operator=(unsigned long long v)
  281. {
  282. from_unsigned_long_long(v);
  283. return *this;
  284. }
  285. #ifdef BOOST_HAS_INT128
  286. cpp_dec_float& operator=(int128_type v)
  287. {
  288. *this = boost::multiprecision::detail::unsigned_abs(v);
  289. if (v < 0)
  290. negate();
  291. return *this;
  292. }
  293. cpp_dec_float& operator=(uint128_type v)
  294. {
  295. using default_ops::eval_add;
  296. using default_ops::eval_multiply;
  297. constexpr unsigned bit_shift = sizeof(unsigned long long) * CHAR_BIT;
  298. constexpr uint128_type mask = (static_cast<uint128_type>(1u) << bit_shift) - 1;
  299. *this = static_cast<unsigned long long>(v & mask);
  300. v >>= bit_shift;
  301. while (v)
  302. {
  303. cpp_dec_float t(static_cast<unsigned long long>(v & mask));
  304. eval_multiply(t, cpp_dec_float::pow2(bit_shift));
  305. eval_add(*this, t);
  306. v >>= bit_shift;
  307. }
  308. return *this;
  309. }
  310. #endif
  311. template <class Float>
  312. typename std::enable_if<std::is_floating_point<Float>::value, cpp_dec_float&>::type operator=(Float v);
  313. cpp_dec_float& operator=(const char* v)
  314. {
  315. rd_string(v);
  316. return *this;
  317. }
  318. cpp_dec_float& operator+=(const cpp_dec_float& v);
  319. cpp_dec_float& operator-=(const cpp_dec_float& v);
  320. cpp_dec_float& operator*=(const cpp_dec_float& v);
  321. cpp_dec_float& operator/=(const cpp_dec_float& v);
  322. cpp_dec_float& add_unsigned_long_long(const unsigned long long n)
  323. {
  324. cpp_dec_float t;
  325. t.from_unsigned_long_long(n);
  326. return *this += t;
  327. }
  328. cpp_dec_float& sub_unsigned_long_long(const unsigned long long n)
  329. {
  330. cpp_dec_float t;
  331. t.from_unsigned_long_long(n);
  332. return *this -= t;
  333. }
  334. cpp_dec_float& mul_unsigned_long_long(const unsigned long long n);
  335. cpp_dec_float& div_unsigned_long_long(const unsigned long long n);
  336. // Elementary primitives.
  337. cpp_dec_float& calculate_sqrt();
  338. void negate()
  339. {
  340. if (!iszero())
  341. neg = !neg;
  342. }
  343. // Comparison functions
  344. bool isnan BOOST_PREVENT_MACRO_SUBSTITUTION() const { return (fpclass == cpp_dec_float_NaN); }
  345. bool isinf BOOST_PREVENT_MACRO_SUBSTITUTION() const { return (fpclass == cpp_dec_float_inf); }
  346. bool isfinite BOOST_PREVENT_MACRO_SUBSTITUTION() const { return (fpclass == cpp_dec_float_finite); }
  347. bool iszero() const
  348. {
  349. return ((fpclass == cpp_dec_float_finite) && (data[0u] == 0u));
  350. }
  351. bool isint() const;
  352. bool isneg() const { return neg; }
  353. // Operators pre-increment and pre-decrement
  354. cpp_dec_float& operator++()
  355. {
  356. return *this += one();
  357. }
  358. cpp_dec_float& operator--()
  359. {
  360. return *this -= one();
  361. }
  362. std::string str(std::intmax_t digits, std::ios_base::fmtflags f) const;
  363. int compare(const cpp_dec_float& v) const;
  364. template <class V>
  365. int compare(const V& v) const
  366. {
  367. cpp_dec_float<Digits10, ExponentType, Allocator> t;
  368. t = v;
  369. return compare(t);
  370. }
  371. void swap(cpp_dec_float& v)
  372. {
  373. data.swap(v.data);
  374. std::swap(exp, v.exp);
  375. std::swap(neg, v.neg);
  376. std::swap(fpclass, v.fpclass);
  377. std::swap(prec_elem, v.prec_elem);
  378. }
  379. double extract_double() const;
  380. long double extract_long_double() const;
  381. long long extract_signed_long_long() const;
  382. unsigned long long extract_unsigned_long_long() const;
  383. #ifdef BOOST_HAS_INT128
  384. int128_type extract_signed_int128() const;
  385. uint128_type extract_unsigned_int128() const;
  386. #endif
  387. void extract_parts(double& mantissa, exponent_type& exponent) const;
  388. cpp_dec_float extract_integer_part() const;
  389. void precision(const std::int32_t prec_digits)
  390. {
  391. const std::int32_t elems =
  392. static_cast<std::int32_t>( static_cast<std::int32_t>(prec_digits / cpp_dec_float_elem_digits10)
  393. + (((prec_digits % cpp_dec_float_elem_digits10) != 0) ? 1 : 0));
  394. prec_elem = (std::min)(cpp_dec_float_elem_number, (std::max)(elems, static_cast<std::int32_t>(2)));
  395. }
  396. static cpp_dec_float pow2(long long i);
  397. exponent_type order() const
  398. {
  399. const bool
  400. bo_order_is_zero
  401. {
  402. ((!(isfinite)()) || (data[0] == static_cast<std::uint32_t>(0u)))
  403. };
  404. return (bo_order_is_zero ? static_cast<exponent_type>(0)
  405. : static_cast<exponent_type>(exp + limb_order(data[static_cast<std::size_t>(UINT8_C(0))])));
  406. }
  407. #ifndef BOOST_MP_STANDALONE
  408. template <class Archive>
  409. void serialize(Archive& ar, const unsigned int /*version*/)
  410. {
  411. for (unsigned i = 0; i < data.size(); ++i)
  412. ar & boost::make_nvp("digit", data[i]);
  413. ar & boost::make_nvp("exponent", exp);
  414. ar & boost::make_nvp("sign", neg);
  415. ar & boost::make_nvp("class-type", fpclass);
  416. ar & boost::make_nvp("precision", prec_elem);
  417. }
  418. #endif
  419. private:
  420. static bool data_elem_is_non_zero_predicate(const std::uint32_t& d) { return (d != static_cast<std::uint32_t>(0u)); }
  421. static bool data_elem_is_non_nine_predicate(const std::uint32_t& d) { return (d != static_cast<std::uint32_t>(cpp_dec_float::cpp_dec_float_elem_mask - 1)); }
  422. static bool char_is_nonzero_predicate(const char& c) { return (c != static_cast<char>('0')); }
  423. // Inversion.
  424. cpp_dec_float& calculate_inv();
  425. static exponent_type limb_order(const limb_type limb)
  426. {
  427. //
  428. // Binary search to find the order of the leading term:
  429. //
  430. exponent_type prefix = 0;
  431. if (limb >= 100000UL)
  432. {
  433. if (limb >= 10000000UL)
  434. {
  435. if (limb >= 100000000UL)
  436. prefix = 8;
  437. else
  438. prefix = 7;
  439. }
  440. else
  441. {
  442. if (limb >= 1000000UL)
  443. prefix = 6;
  444. else
  445. prefix = 5;
  446. }
  447. }
  448. else
  449. {
  450. if (limb >= 1000UL)
  451. {
  452. if (limb >= 10000UL)
  453. prefix = 4;
  454. else
  455. prefix = 3;
  456. }
  457. else
  458. {
  459. if (limb >= 100)
  460. prefix = 2;
  461. else if (limb >= 10)
  462. prefix = 1;
  463. }
  464. }
  465. return prefix;
  466. }
  467. bool isone() const
  468. {
  469. bool b_neg;
  470. const bool result_one_is_ok = is_one_or_minus_one(&b_neg);
  471. return (result_one_is_ok && (!b_neg));
  472. }
  473. bool is_one_or_minus_one(bool* p_sign) const;
  474. void from_unsigned_long_long(const unsigned long long u);
  475. template <typename InputIteratorTypeLeft,
  476. typename InputIteratorTypeRight>
  477. static int compare_ranges(InputIteratorTypeLeft a,
  478. InputIteratorTypeRight b,
  479. const std::uint32_t count = cpp_dec_float_elem_number);
  480. static std::uint32_t eval_add_n( std::uint32_t* r,
  481. const std::uint32_t* u,
  482. const std::uint32_t* v,
  483. const std::int32_t count);
  484. static std::uint32_t eval_subtract_n( std::uint32_t* r,
  485. const std::uint32_t* u,
  486. const std::uint32_t* v,
  487. const std::int32_t count);
  488. static void eval_multiply_n_by_n_to_2n( std::uint32_t* r,
  489. const std::uint32_t* a,
  490. const std::uint32_t* b,
  491. const std::uint32_t count);
  492. static std::uint32_t mul_loop_n(std::uint32_t* const u, std::uint32_t n, const std::int32_t p);
  493. static std::uint32_t div_loop_n(std::uint32_t* const u, std::uint32_t n, const std::int32_t p);
  494. static void eval_multiply_kara_propagate_carry (std::uint32_t* t, const std::uint32_t n, const std::uint32_t carry);
  495. static void eval_multiply_kara_propagate_borrow(std::uint32_t* t, const std::uint32_t n, const bool has_borrow);
  496. static void eval_multiply_kara_n_by_n_to_2n ( std::uint32_t* r,
  497. const std::uint32_t* a,
  498. const std::uint32_t* b,
  499. const std::uint32_t n,
  500. std::uint32_t* t);
  501. template<unsigned D>
  502. void eval_mul_dispatch_multiplication_method(
  503. const cpp_dec_float<D, ExponentType, Allocator>& v,
  504. const std::int32_t prec_elems_for_multiply,
  505. const typename std::enable_if< (D == Digits10)
  506. && (cpp_dec_float<D, ExponentType, Allocator>::cpp_dec_float_elem_number < cpp_dec_float_elems_for_kara)>::type* = nullptr)
  507. {
  508. // Use school multiplication.
  509. using array_for_mul_result_type =
  510. typename std::conditional<std::is_void<Allocator>::value,
  511. detail::static_array <std::uint32_t, std::uint32_t(cpp_dec_float_elem_number * 2)>,
  512. detail::dynamic_array<std::uint32_t, std::uint32_t(cpp_dec_float_elem_number * 2), Allocator> >::type;
  513. array_for_mul_result_type result;
  514. eval_multiply_n_by_n_to_2n(result.data(), data.data(), v.data.data(), static_cast<std::uint32_t>(prec_elems_for_multiply));
  515. // Handle a potential carry.
  516. if(result[0U] != static_cast<std::uint32_t>(0U))
  517. {
  518. exp += static_cast<exponent_type>(cpp_dec_float_elem_digits10);
  519. // Shift the result of the multiplication one element to the right.
  520. std::copy(result.cbegin(),
  521. result.cbegin() + static_cast<std::ptrdiff_t>(prec_elems_for_multiply),
  522. data.begin());
  523. }
  524. else
  525. {
  526. std::copy(result.cbegin() + static_cast<std::ptrdiff_t>(1),
  527. result.cbegin() + static_cast<std::ptrdiff_t>(1 + (std::min)(prec_elems_for_multiply, cpp_dec_float_elem_number)),
  528. data.begin());
  529. }
  530. }
  531. template<unsigned D>
  532. void eval_mul_dispatch_multiplication_method(
  533. const cpp_dec_float<D, ExponentType, Allocator>& v,
  534. const std::int32_t prec_elems_for_multiply,
  535. const typename std::enable_if< (D == Digits10)
  536. && !(cpp_dec_float<D, ExponentType, Allocator>::cpp_dec_float_elem_number < cpp_dec_float_elems_for_kara)>::type* = nullptr)
  537. {
  538. if(prec_elems_for_multiply < cpp_dec_float_elems_for_kara)
  539. {
  540. // Use school multiplication.
  541. using array_for_mul_result_type =
  542. typename std::conditional<std::is_void<Allocator>::value,
  543. detail::static_array <std::uint32_t, std::uint32_t(cpp_dec_float_elem_number * 2)>,
  544. detail::dynamic_array<std::uint32_t, std::uint32_t(cpp_dec_float_elem_number * 2), Allocator> >::type;
  545. array_for_mul_result_type result;
  546. eval_multiply_n_by_n_to_2n(result.data(), data.data(), v.data.data(), static_cast<std::uint32_t>(prec_elems_for_multiply));
  547. // Handle a potential carry.
  548. if(result[0U] != static_cast<std::uint32_t>(0U))
  549. {
  550. exp += static_cast<exponent_type>(cpp_dec_float_elem_digits10);
  551. // Shift the result of the multiplication one element to the right.
  552. std::copy(result.cbegin(),
  553. result.cbegin() + static_cast<std::ptrdiff_t>(prec_elems_for_multiply),
  554. data.begin());
  555. }
  556. else
  557. {
  558. std::copy(result.cbegin() + static_cast<std::ptrdiff_t>(1),
  559. result.cbegin() + static_cast<std::ptrdiff_t>(1 + (std::min)(prec_elems_for_multiply, cpp_dec_float_elem_number)),
  560. data.begin());
  561. }
  562. }
  563. else
  564. {
  565. // Use Karatsuba multiplication.
  566. using array_for_kara_tmp_type =
  567. typename std::conditional<std::is_void<Allocator>::value,
  568. detail::static_array <std::uint32_t, detail::a029750::a029750_as_constexpr(static_cast<std::uint32_t>(cpp_dec_float_elem_number)) * 8U>,
  569. detail::dynamic_array<std::uint32_t, detail::a029750::a029750_as_constexpr(static_cast<std::uint32_t>(cpp_dec_float_elem_number)) * 8U, Allocator> >::type;
  570. // Sloanes's A029747: Numbers of the form 2^k times 1, 3 or 5.
  571. const std::uint32_t kara_elems_for_multiply =
  572. detail::a029750::a029750_as_runtime_value(static_cast<std::uint32_t>(prec_elems_for_multiply));
  573. array_for_kara_tmp_type my_kara_mul_pool;
  574. std::uint32_t* result = my_kara_mul_pool.data() + (kara_elems_for_multiply * 0U);
  575. std::uint32_t* t = my_kara_mul_pool.data() + (kara_elems_for_multiply * 2U);
  576. std::uint32_t* u_local = my_kara_mul_pool.data() + (kara_elems_for_multiply * 6U);
  577. std::uint32_t* v_local = my_kara_mul_pool.data() + (kara_elems_for_multiply * 7U);
  578. std::copy( data.cbegin(), data.cbegin() + prec_elems_for_multiply, u_local);
  579. std::copy(v.data.cbegin(), v.data.cbegin() + prec_elems_for_multiply, v_local);
  580. eval_multiply_kara_n_by_n_to_2n(result,
  581. u_local,
  582. v_local,
  583. kara_elems_for_multiply,
  584. t);
  585. // Handle a potential carry.
  586. if(result[0U] != static_cast<std::uint32_t>(0U))
  587. {
  588. exp += static_cast<exponent_type>(cpp_dec_float_elem_digits10);
  589. // Shift the result of the multiplication one element to the right.
  590. std::copy(result,
  591. result + static_cast<std::ptrdiff_t>(prec_elems_for_multiply),
  592. data.begin());
  593. }
  594. else
  595. {
  596. std::copy(result + static_cast<std::ptrdiff_t>(1),
  597. result + static_cast<std::ptrdiff_t>(1 + (std::min)(prec_elems_for_multiply, cpp_dec_float_elem_number)),
  598. data.begin());
  599. }
  600. }
  601. }
  602. bool rd_string(const char* const s);
  603. template <unsigned D, class ET, class A>
  604. friend class cpp_dec_float;
  605. };
  606. template <unsigned Digits10, class ExponentType, class Allocator>
  607. constexpr std::int32_t cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_radix;
  608. template <unsigned Digits10, class ExponentType, class Allocator>
  609. constexpr std::int32_t cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_digits10_limit_lo;
  610. template <unsigned Digits10, class ExponentType, class Allocator>
  611. constexpr std::int32_t cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_digits10_limit_hi;
  612. template <unsigned Digits10, class ExponentType, class Allocator>
  613. constexpr std::int32_t cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_digits10;
  614. template <unsigned Digits10, class ExponentType, class Allocator>
  615. constexpr ExponentType cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_max_exp;
  616. template <unsigned Digits10, class ExponentType, class Allocator>
  617. constexpr ExponentType cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_min_exp;
  618. template <unsigned Digits10, class ExponentType, class Allocator>
  619. constexpr ExponentType cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_max_exp10;
  620. template <unsigned Digits10, class ExponentType, class Allocator>
  621. constexpr ExponentType cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_min_exp10;
  622. template <unsigned Digits10, class ExponentType, class Allocator>
  623. constexpr std::int32_t cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_elem_digits10;
  624. template <unsigned Digits10, class ExponentType, class Allocator>
  625. constexpr std::int32_t cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_elem_number;
  626. template <unsigned Digits10, class ExponentType, class Allocator>
  627. constexpr std::int32_t cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_elem_mask;
  628. template <unsigned Digits10, class ExponentType, class Allocator>
  629. cpp_dec_float<Digits10, ExponentType, Allocator>& cpp_dec_float<Digits10, ExponentType, Allocator>::operator+=(const cpp_dec_float<Digits10, ExponentType, Allocator>& v)
  630. {
  631. if ((isnan)())
  632. {
  633. return *this;
  634. }
  635. if ((isinf)())
  636. {
  637. if ((v.isinf)() && (isneg() != v.isneg()))
  638. {
  639. *this = nan();
  640. }
  641. return *this;
  642. }
  643. if (iszero())
  644. {
  645. return operator=(v);
  646. }
  647. if ((v.isnan)() || (v.isinf)())
  648. {
  649. *this = v;
  650. return *this;
  651. }
  652. // Get the offset for the add/sub operation.
  653. constexpr exponent_type max_delta_exp =
  654. static_cast<exponent_type>((cpp_dec_float_elem_number - 1) * cpp_dec_float_elem_digits10);
  655. const exponent_type ofs_exp = static_cast<exponent_type>(exp - v.exp);
  656. // Check if the operation is out of range, requiring special handling.
  657. if (v.iszero() || (ofs_exp > max_delta_exp))
  658. {
  659. // Result is *this unchanged since v is negligible compared to *this.
  660. return *this;
  661. }
  662. else if (ofs_exp < -max_delta_exp)
  663. {
  664. // Result is *this = v since *this is negligible compared to v.
  665. return operator=(v);
  666. }
  667. // Do the add/sub operation.
  668. typename array_type::pointer p_u = data.data();
  669. typename array_type::const_pointer p_v = v.data.data();
  670. bool b_copy = false;
  671. const std::int32_t ofs = static_cast<std::int32_t>(static_cast<std::int32_t>(ofs_exp) / cpp_dec_float_elem_digits10);
  672. array_type n_data;
  673. if (neg == v.neg)
  674. {
  675. // Add v to *this, where the data array of either *this or v
  676. // might have to be treated with a positive, negative or zero offset.
  677. // The result is stored in *this. The data are added one element
  678. // at a time, each element with carry.
  679. if (ofs >= static_cast<std::int32_t>(0))
  680. {
  681. std::copy(v.data.cbegin(), v.data.cend() - static_cast<std::ptrdiff_t>(ofs), n_data.begin() + static_cast<std::ptrdiff_t>(ofs));
  682. std::fill(n_data.begin(), n_data.begin() + static_cast<std::ptrdiff_t>(ofs), static_cast<std::uint32_t>(0u));
  683. p_v = n_data.data();
  684. }
  685. else
  686. {
  687. std::copy(data.cbegin(), data.cend() - static_cast<std::ptrdiff_t>(-ofs), n_data.begin() + static_cast<std::ptrdiff_t>(-ofs));
  688. std::fill(n_data.begin(), n_data.begin() + static_cast<std::ptrdiff_t>(-ofs), static_cast<std::uint32_t>(0u));
  689. p_u = n_data.data();
  690. b_copy = true;
  691. }
  692. // Addition algorithm
  693. const std::uint32_t carry = eval_add_n(p_u, p_u, p_v, cpp_dec_float_elem_number);
  694. if (b_copy)
  695. {
  696. data = n_data;
  697. exp = v.exp;
  698. }
  699. // There needs to be a carry into the element -1 of the array data
  700. if (carry != static_cast<std::uint32_t>(0u))
  701. {
  702. std::copy_backward(data.cbegin(), data.cend() - static_cast<std::size_t>(1u), data.end());
  703. data[0] = carry;
  704. exp += static_cast<exponent_type>(cpp_dec_float_elem_digits10);
  705. }
  706. }
  707. else
  708. {
  709. // Subtract v from *this, where the data array of either *this or v
  710. // might have to be treated with a positive, negative or zero offset.
  711. if ((ofs > static_cast<std::int32_t>(0)) || ((ofs == static_cast<std::int32_t>(0)) && (compare_ranges(data.cbegin(), v.data.cbegin()) > static_cast<std::int32_t>(0))))
  712. {
  713. // In this case, |u| > |v| and ofs is positive.
  714. // Copy the data of v, shifted down to a lower value
  715. // into the data array m_n. Set the operand pointer p_v
  716. // to point to the copied, shifted data m_n.
  717. std::copy(v.data.cbegin(), v.data.cend() - static_cast<std::ptrdiff_t>(ofs), n_data.begin() + static_cast<std::ptrdiff_t>(ofs));
  718. std::fill(n_data.begin(), n_data.begin() + static_cast<std::ptrdiff_t>(ofs), static_cast<std::uint32_t>(0u));
  719. p_v = n_data.data();
  720. }
  721. else
  722. {
  723. if (ofs != static_cast<std::int32_t>(0))
  724. {
  725. // In this case, |u| < |v| and ofs is negative.
  726. // Shift the data of u down to a lower value.
  727. std::copy_backward(data.cbegin(), data.cend() - static_cast<std::ptrdiff_t>(-ofs), data.end());
  728. std::fill(data.begin(), data.begin() + static_cast<std::ptrdiff_t>(-ofs), static_cast<std::uint32_t>(0u));
  729. }
  730. // Copy the data of v into the data array n_data.
  731. // Set the u-pointer p_u to point to m_n and the
  732. // operand pointer p_v to point to the shifted
  733. // data m_data.
  734. n_data = v.data;
  735. p_u = n_data.data();
  736. p_v = data.data();
  737. b_copy = true;
  738. }
  739. // Subtraction algorithm
  740. static_cast<void>(eval_subtract_n(p_u, p_u, p_v, cpp_dec_float_elem_number));
  741. if (b_copy)
  742. {
  743. data = n_data;
  744. exp = v.exp;
  745. neg = v.neg;
  746. }
  747. // Is it necessary to justify the data?
  748. const typename array_type::const_iterator first_nonzero_elem = std::find_if(data.begin(), data.end(), data_elem_is_non_zero_predicate);
  749. if (first_nonzero_elem != data.begin())
  750. {
  751. if (first_nonzero_elem == data.end())
  752. {
  753. // This result of the subtraction is exactly zero.
  754. // Reset the sign and the exponent.
  755. neg = false;
  756. exp = static_cast<exponent_type>(0);
  757. }
  758. else
  759. {
  760. // Justify the data
  761. const std::size_t sj = static_cast<std::size_t>(std::distance<typename array_type::const_iterator>(data.begin(), first_nonzero_elem));
  762. std::copy(data.begin() + static_cast<std::ptrdiff_t>(sj), data.end(), data.begin());
  763. std::fill(data.end() - static_cast<std::ptrdiff_t>(sj), data.end(), static_cast<std::uint32_t>(0u));
  764. exp -= static_cast<exponent_type>(sj * static_cast<std::size_t>(cpp_dec_float_elem_digits10));
  765. }
  766. }
  767. }
  768. // Handle underflow.
  769. if (iszero())
  770. return (*this = zero());
  771. // Check for potential overflow.
  772. const bool b_result_might_overflow = (exp >= static_cast<exponent_type>(cpp_dec_float_max_exp10));
  773. // Handle overflow.
  774. if (b_result_might_overflow)
  775. {
  776. const bool b_result_is_neg = neg;
  777. neg = false;
  778. if (compare((cpp_dec_float::max)()) > 0)
  779. *this = inf();
  780. neg = b_result_is_neg;
  781. }
  782. return *this;
  783. }
  784. template <unsigned Digits10, class ExponentType, class Allocator>
  785. cpp_dec_float<Digits10, ExponentType, Allocator>& cpp_dec_float<Digits10, ExponentType, Allocator>::operator-=(const cpp_dec_float<Digits10, ExponentType, Allocator>& v)
  786. {
  787. // Use *this - v = -(-*this + v).
  788. negate();
  789. *this += v;
  790. negate();
  791. return *this;
  792. }
  793. template <unsigned Digits10, class ExponentType, class Allocator>
  794. cpp_dec_float<Digits10, ExponentType, Allocator>& cpp_dec_float<Digits10, ExponentType, Allocator>::operator*=(const cpp_dec_float<Digits10, ExponentType, Allocator>& v)
  795. {
  796. // Evaluate the sign of the result.
  797. const bool b_result_is_neg = (neg != v.neg);
  798. // Artificially set the sign of the result to be positive.
  799. neg = false;
  800. // Handle special cases like zero, inf and NaN.
  801. const bool b_u_is_inf = (isinf)();
  802. const bool b_v_is_inf = (v.isinf)();
  803. const bool b_u_is_zero = iszero();
  804. const bool b_v_is_zero = v.iszero();
  805. if (((isnan)() || (v.isnan)()) || (b_u_is_inf && b_v_is_zero) || (b_v_is_inf && b_u_is_zero))
  806. {
  807. *this = nan();
  808. return *this;
  809. }
  810. if (b_u_is_inf || b_v_is_inf)
  811. {
  812. *this = inf();
  813. if (b_result_is_neg)
  814. negate();
  815. return *this;
  816. }
  817. if (b_u_is_zero || b_v_is_zero)
  818. {
  819. return *this = zero();
  820. }
  821. // Check for potential overflow or underflow.
  822. const bool b_result_might_overflow = ((exp + v.exp) >= static_cast<exponent_type>(cpp_dec_float_max_exp10));
  823. const bool b_result_might_underflow = ((exp + v.exp) <= static_cast<exponent_type>(cpp_dec_float_min_exp10));
  824. // Set the exponent of the result.
  825. exp += v.exp;
  826. const std::int32_t prec_mul = (std::min)(prec_elem, v.prec_elem);
  827. eval_mul_dispatch_multiplication_method(v, prec_mul);
  828. // Handle overflow.
  829. if (b_result_might_overflow && (compare((cpp_dec_float::max)()) > 0))
  830. {
  831. *this = inf();
  832. }
  833. // Handle underflow.
  834. if (b_result_might_underflow && (compare((cpp_dec_float::min)()) < 0))
  835. {
  836. *this = zero();
  837. return *this;
  838. }
  839. // Set the sign of the result.
  840. neg = b_result_is_neg;
  841. return *this;
  842. }
  843. template <unsigned Digits10, class ExponentType, class Allocator>
  844. cpp_dec_float<Digits10, ExponentType, Allocator>& cpp_dec_float<Digits10, ExponentType, Allocator>::operator/=(const cpp_dec_float<Digits10, ExponentType, Allocator>& v)
  845. {
  846. if (iszero())
  847. {
  848. if ((v.isnan)())
  849. {
  850. return *this = v;
  851. }
  852. else if (v.iszero())
  853. {
  854. return *this = nan();
  855. }
  856. }
  857. const bool u_and_v_are_finite_and_identical = ((isfinite)() && (fpclass == v.fpclass) && (exp == v.exp) && (compare_ranges(data.cbegin(), v.data.cbegin()) == static_cast<std::int32_t>(0)));
  858. if (u_and_v_are_finite_and_identical)
  859. {
  860. if (neg != v.neg)
  861. {
  862. *this = one();
  863. negate();
  864. }
  865. else
  866. *this = one();
  867. return *this;
  868. }
  869. else
  870. {
  871. cpp_dec_float t(v);
  872. t.calculate_inv();
  873. return operator*=(t);
  874. }
  875. }
  876. template <unsigned Digits10, class ExponentType, class Allocator>
  877. cpp_dec_float<Digits10, ExponentType, Allocator>& cpp_dec_float<Digits10, ExponentType, Allocator>::mul_unsigned_long_long(const unsigned long long n)
  878. {
  879. // Multiply *this with a constant unsigned long long.
  880. // Evaluate the sign of the result.
  881. const bool b_neg = neg;
  882. // Artificially set the sign of the result to be positive.
  883. neg = false;
  884. // Handle special cases like zero, inf and NaN.
  885. const bool b_u_is_inf = (isinf)();
  886. const bool b_n_is_zero = (n == static_cast<std::int32_t>(0));
  887. if ((isnan)() || (b_u_is_inf && b_n_is_zero))
  888. {
  889. return (*this = nan());
  890. }
  891. if (b_u_is_inf)
  892. {
  893. *this = inf();
  894. if (b_neg)
  895. negate();
  896. return *this;
  897. }
  898. if (iszero() || b_n_is_zero)
  899. {
  900. // Multiplication by zero.
  901. return *this = zero();
  902. }
  903. if (n >= static_cast<unsigned long long>(cpp_dec_float_elem_mask))
  904. {
  905. neg = b_neg;
  906. cpp_dec_float t;
  907. t = n;
  908. return operator*=(t);
  909. }
  910. if (n == static_cast<unsigned long long>(1u))
  911. {
  912. neg = b_neg;
  913. return *this;
  914. }
  915. // Set up the multiplication loop.
  916. const std::uint32_t nn = static_cast<std::uint32_t>(n);
  917. const std::uint32_t carry = mul_loop_n(data.data(), nn, prec_elem);
  918. // Handle the carry and adjust the exponent.
  919. if (carry != static_cast<std::uint32_t>(0u))
  920. {
  921. exp += static_cast<exponent_type>(cpp_dec_float_elem_digits10);
  922. // Shift the result of the multiplication one element to the right.
  923. std::copy_backward(data.begin(),
  924. data.begin() + static_cast<std::ptrdiff_t>(prec_elem - static_cast<std::int32_t>(1)),
  925. data.begin() + static_cast<std::ptrdiff_t>(prec_elem));
  926. data.front() = static_cast<std::uint32_t>(carry);
  927. }
  928. // Check for potential overflow.
  929. const bool b_result_might_overflow = (exp >= cpp_dec_float_max_exp10);
  930. // Handle overflow.
  931. if (b_result_might_overflow && (compare((cpp_dec_float::max)()) > 0))
  932. {
  933. *this = inf();
  934. }
  935. // Set the sign.
  936. neg = b_neg;
  937. return *this;
  938. }
  939. template <unsigned Digits10, class ExponentType, class Allocator>
  940. cpp_dec_float<Digits10, ExponentType, Allocator>& cpp_dec_float<Digits10, ExponentType, Allocator>::div_unsigned_long_long(const unsigned long long n)
  941. {
  942. // Divide *this by a constant unsigned long long.
  943. // Evaluate the sign of the result.
  944. const bool b_neg = neg;
  945. // Artificially set the sign of the result to be positive.
  946. neg = false;
  947. // Handle special cases like zero, inf and NaN.
  948. if ((isnan)())
  949. {
  950. return *this;
  951. }
  952. if ((isinf)())
  953. {
  954. *this = inf();
  955. if (b_neg)
  956. negate();
  957. return *this;
  958. }
  959. if (n == static_cast<unsigned long long>(0u))
  960. {
  961. // Divide by 0.
  962. if (iszero())
  963. {
  964. *this = nan();
  965. return *this;
  966. }
  967. else
  968. {
  969. *this = inf();
  970. if (b_neg)
  971. negate();
  972. return *this;
  973. }
  974. }
  975. if (iszero())
  976. {
  977. return *this;
  978. }
  979. if (n >= static_cast<unsigned long long>(cpp_dec_float_elem_mask))
  980. {
  981. neg = b_neg;
  982. cpp_dec_float t;
  983. t = n;
  984. return operator/=(t);
  985. } // LCOV_EXCL_LINE This causes a false negative on lcov coverage test.
  986. const std::uint32_t nn = static_cast<std::uint32_t>(n);
  987. if (nn > static_cast<std::uint32_t>(1u))
  988. {
  989. // Do the division loop.
  990. const std::uint32_t prev = div_loop_n(data.data(), nn, prec_elem);
  991. // Determine if one leading zero is in the result data.
  992. if (data[0] == static_cast<std::uint32_t>(0u))
  993. {
  994. // Adjust the exponent
  995. exp -= static_cast<exponent_type>(cpp_dec_float_elem_digits10);
  996. // Shift result of the division one element to the left.
  997. std::copy(data.begin() + static_cast<std::ptrdiff_t>(1),
  998. data.begin() + static_cast<std::ptrdiff_t>(prec_elem - static_cast<std::int32_t>(1)),
  999. data.begin());
  1000. data[static_cast<std::size_t>(prec_elem - static_cast<std::int32_t>(1))] = static_cast<std::uint32_t>(static_cast<std::uint64_t>(prev * static_cast<std::uint64_t>(cpp_dec_float_elem_mask)) / nn);
  1001. }
  1002. }
  1003. // Check for potential underflow.
  1004. const bool b_result_might_underflow = (exp <= cpp_dec_float_min_exp10);
  1005. // Handle underflow.
  1006. if (b_result_might_underflow && (compare((cpp_dec_float::min)()) < 0))
  1007. return (*this = zero());
  1008. // Set the sign of the result.
  1009. neg = b_neg;
  1010. return *this;
  1011. }
  1012. template <unsigned Digits10, class ExponentType, class Allocator>
  1013. cpp_dec_float<Digits10, ExponentType, Allocator>& cpp_dec_float<Digits10, ExponentType, Allocator>::calculate_inv()
  1014. {
  1015. // Handle the special case of zero.
  1016. if (iszero())
  1017. {
  1018. *this = inf();
  1019. return *this;
  1020. }
  1021. // Handle the special cases of inf, NaN, and +1 or -1.
  1022. if ((isinf)())
  1023. {
  1024. return *this = zero();
  1025. }
  1026. bool b_neg;
  1027. if ((isnan)() || is_one_or_minus_one(&b_neg))
  1028. {
  1029. return *this;
  1030. }
  1031. // Compute the inverse of *this.
  1032. neg = false;
  1033. // Save the original (absolute value of) *this.
  1034. cpp_dec_float<Digits10, ExponentType, Allocator> x(*this);
  1035. // Generate the initial estimate using division.
  1036. // Extract the mantissa and exponent for a "manual"
  1037. // computation of the estimate.
  1038. double dd;
  1039. exponent_type ne;
  1040. x.extract_parts(dd, ne);
  1041. // Setup the iteration.
  1042. // Calculate the initial estimate of the inverse using double
  1043. // precision approximations of mantissa and exponent.
  1044. operator=(cpp_dec_float<Digits10, ExponentType, Allocator>(1.0 / dd, -ne));
  1045. // Compute the inverse of *this. Quadratically convergent Newton-Raphson iteration
  1046. // is used. During the iterative steps, the precision of the calculation is limited
  1047. // to the minimum required in order to minimize the run-time.
  1048. constexpr std::int32_t double_digits10_minus_a_few = std::numeric_limits<double>::digits10 - 3;
  1049. for (std::int32_t digits = double_digits10_minus_a_few; digits <= cpp_dec_float_max_digits10; digits *= static_cast<std::int32_t>(2))
  1050. {
  1051. // Adjust precision of the terms.
  1052. precision(static_cast<std::int32_t>((digits + 10) * static_cast<std::int32_t>(2)));
  1053. x.precision(static_cast<std::int32_t>((digits + 10) * static_cast<std::int32_t>(2)));
  1054. // Next iteration.
  1055. cpp_dec_float t(*this);
  1056. t *= x;
  1057. t -= two();
  1058. t.negate();
  1059. *this *= t;
  1060. }
  1061. neg = b_neg;
  1062. prec_elem = cpp_dec_float_elem_number;
  1063. return *this;
  1064. }
  1065. template <unsigned Digits10, class ExponentType, class Allocator>
  1066. cpp_dec_float<Digits10, ExponentType, Allocator>& cpp_dec_float<Digits10, ExponentType, Allocator>::calculate_sqrt()
  1067. {
  1068. // Compute the square root of *this.
  1069. if ((isinf)() && (!isneg()))
  1070. {
  1071. return *this;
  1072. }
  1073. if (isneg() || (!(isfinite)()))
  1074. {
  1075. *this = nan();
  1076. errno = EDOM;
  1077. return *this;
  1078. }
  1079. if (iszero() || isone())
  1080. {
  1081. return *this;
  1082. }
  1083. // Save the original *this.
  1084. cpp_dec_float<Digits10, ExponentType, Allocator> x(*this);
  1085. // Generate the initial estimate using division.
  1086. // Extract the mantissa and exponent for a "manual"
  1087. // computation of the estimate.
  1088. double dd;
  1089. exponent_type ne;
  1090. extract_parts(dd, ne);
  1091. // Force the exponent to be an even multiple of two.
  1092. if ((ne % static_cast<exponent_type>(2)) != static_cast<exponent_type>(0))
  1093. {
  1094. ++ne;
  1095. dd /= 10.0;
  1096. }
  1097. // Setup the iteration.
  1098. // Calculate the initial estimate of the root using double
  1099. // precision approximations of mantissa and exponent.
  1100. const double sqd = std::sqrt(dd);
  1101. *this = cpp_dec_float<Digits10, ExponentType, Allocator>(sqd, static_cast<ExponentType>(ne / static_cast<ExponentType>(2)));
  1102. // Estimate 1.0 / (2.0 * x0) using simple manipulations.
  1103. cpp_dec_float<Digits10, ExponentType, Allocator> vi(0.5 / sqd, static_cast<ExponentType>(-ne / static_cast<ExponentType>(2)));
  1104. // Compute the square root of x. Coupled Newton iteration
  1105. // as described in "Pi Unleashed" is used. During the
  1106. // iterative steps, the precision of the calculation is
  1107. // limited to the minimum required in order to minimize
  1108. // the run-time.
  1109. //
  1110. // Book reference to "Pi Unleashed:
  1111. // https://www.springer.com/gp/book/9783642567353
  1112. constexpr std::uint32_t double_digits10_minus_a_few = std::numeric_limits<double>::digits10 - 3;
  1113. for (std::int32_t digits = double_digits10_minus_a_few; digits <= cpp_dec_float_max_digits10; digits *= 2)
  1114. {
  1115. // Adjust precision of the terms.
  1116. precision((digits + 10) * 2);
  1117. vi.precision((digits + 10) * 2);
  1118. // Next iteration of vi
  1119. cpp_dec_float t(*this);
  1120. t *= vi;
  1121. t.negate();
  1122. t.mul_unsigned_long_long(2u);
  1123. t += one();
  1124. t *= vi;
  1125. vi += t;
  1126. // Next iteration of *this
  1127. t = *this;
  1128. t *= *this;
  1129. t.negate();
  1130. t += x;
  1131. t *= vi;
  1132. *this += t;
  1133. }
  1134. prec_elem = cpp_dec_float_elem_number;
  1135. return *this;
  1136. }
  1137. template <unsigned Digits10, class ExponentType, class Allocator>
  1138. int cpp_dec_float<Digits10, ExponentType, Allocator>::compare(const cpp_dec_float& v) const
  1139. {
  1140. // Compare v with *this.
  1141. // Return +1 for *this > v
  1142. // 0 for *this = v
  1143. // -1 for *this < v
  1144. // Handle all non-finite cases.
  1145. if ((!(isfinite)()) || (!(v.isfinite)()))
  1146. {
  1147. // NaN can never equal NaN. Return an implementation-dependent
  1148. // signed result. Also note that comparison of NaN with NaN
  1149. // using operators greater-than or less-than is undefined.
  1150. if ((isnan)() || (v.isnan)())
  1151. {
  1152. return ((isnan)() ? 1 : -1);
  1153. }
  1154. if ((isinf)() && (v.isinf)())
  1155. {
  1156. // Both *this and v are infinite. They are equal if they have the same sign.
  1157. // Otherwise, *this is less than v if and only if *this is negative.
  1158. return ((neg == v.neg) ? 0 : (neg ? -1 : 1));
  1159. }
  1160. if ((isinf)())
  1161. {
  1162. // *this is infinite, but v is finite.
  1163. // So negative infinite *this is less than any finite v.
  1164. // Whereas positive infinite *this is greater than any finite v.
  1165. return (isneg() ? -1 : 1);
  1166. }
  1167. else
  1168. {
  1169. // *this is finite, and v is infinite.
  1170. // So any finite *this is greater than negative infinite v.
  1171. // Whereas any finite *this is less than positive infinite v.
  1172. return (v.neg ? 1 : -1);
  1173. }
  1174. }
  1175. // And now handle all *finite* cases.
  1176. if (iszero())
  1177. {
  1178. // The value of *this is zero and v is either zero or non-zero.
  1179. return (v.iszero() ? 0 : (v.neg ? 1 : -1));
  1180. }
  1181. else if (v.iszero())
  1182. {
  1183. // The value of v is zero and *this is non-zero.
  1184. return (neg ? -1 : 1);
  1185. }
  1186. else
  1187. {
  1188. // Both *this and v are non-zero.
  1189. if (neg != v.neg)
  1190. {
  1191. // The signs are different.
  1192. return (neg ? -1 : 1);
  1193. }
  1194. else if (exp != v.exp)
  1195. {
  1196. // The signs are the same and the exponents are different.
  1197. const int val_cexpression = ((exp < v.exp) ? 1 : -1);
  1198. return (neg ? val_cexpression : -val_cexpression);
  1199. }
  1200. else
  1201. {
  1202. // The signs are the same and the exponents are the same.
  1203. // Compare the data.
  1204. const int val_cmp_data = compare_ranges(data.cbegin(), v.data.cbegin());
  1205. return ((!neg) ? val_cmp_data : -val_cmp_data);
  1206. }
  1207. }
  1208. }
  1209. template <unsigned Digits10, class ExponentType, class Allocator>
  1210. bool cpp_dec_float<Digits10, ExponentType, Allocator>::is_one_or_minus_one(bool* p_sign) const
  1211. {
  1212. // Check if the value of *this is identically 1 or very close to 1.
  1213. if(p_sign != nullptr)
  1214. *p_sign = neg;
  1215. if ((isfinite)())
  1216. {
  1217. if ((data[0u] == static_cast<std::uint32_t>(1u)) && (exp == static_cast<exponent_type>(0)))
  1218. {
  1219. const typename array_type::const_iterator it_non_zero = std::find_if(data.begin(), data.end(), data_elem_is_non_zero_predicate);
  1220. const bool result_one_is_ok = (it_non_zero == data.end());
  1221. return result_one_is_ok;
  1222. }
  1223. else if ((data[0u] == static_cast<std::uint32_t>(cpp_dec_float_elem_mask - 1)) && (exp == static_cast<exponent_type>(-cpp_dec_float_elem_digits10)))
  1224. {
  1225. const typename array_type::const_iterator it_non_nine = std::find_if(data.begin(), data.end(), data_elem_is_non_nine_predicate);
  1226. const bool result_one_is_ok = (it_non_nine == data.end());
  1227. return result_one_is_ok;
  1228. }
  1229. }
  1230. return false;
  1231. }
  1232. template <unsigned Digits10, class ExponentType, class Allocator>
  1233. bool cpp_dec_float<Digits10, ExponentType, Allocator>::isint() const
  1234. {
  1235. if (fpclass != cpp_dec_float_finite)
  1236. {
  1237. return false;
  1238. }
  1239. if (iszero())
  1240. {
  1241. return true;
  1242. }
  1243. if (exp < static_cast<exponent_type>(0))
  1244. {
  1245. return false;
  1246. } // |*this| < 1.
  1247. const typename array_type::size_type offset_decimal_part = static_cast<typename array_type::size_type>(exp / cpp_dec_float_elem_digits10) + 1u;
  1248. if (offset_decimal_part >= static_cast<typename array_type::size_type>(cpp_dec_float_elem_number))
  1249. {
  1250. // The number is too large to resolve the integer part.
  1251. // It considered to be a pure integer.
  1252. return true;
  1253. }
  1254. typename array_type::const_iterator it_non_zero = std::find_if(data.begin() + static_cast<std::ptrdiff_t>(offset_decimal_part), data.end(), data_elem_is_non_zero_predicate);
  1255. return (it_non_zero == data.end());
  1256. }
  1257. template <unsigned Digits10, class ExponentType, class Allocator>
  1258. void cpp_dec_float<Digits10, ExponentType, Allocator>::extract_parts(double& mantissa, ExponentType& exponent) const
  1259. {
  1260. // Extract the approximate parts mantissa and base-10 exponent from
  1261. // the input cpp_dec_float<Digits10, ExponentType, Allocator> value x.
  1262. // This subroutine is designed to be fast and does not round.
  1263. // Extract the mantissa and exponent.
  1264. using local_exponent_type = ExponentType;
  1265. const local_exponent_type my_order_limb0 { limb_order(data.front()) };
  1266. exponent = static_cast<local_exponent_type>(exp + my_order_limb0);
  1267. // Extract into the mantissa the first limb, extracted as a double.
  1268. mantissa = ((!neg) ? static_cast<double>(data.front()) : -static_cast<double>(data.front()));
  1269. int digit_counter { static_cast<int>(my_order_limb0) };
  1270. // Keep a running power-of-ten scale in the variable p10.
  1271. double p10 { static_cast<double>(detail::pow10_maker(static_cast<std::uint32_t>(my_order_limb0))) };
  1272. // Scale the first limb with its order.
  1273. mantissa /= p10;
  1274. // Extract the rest of the mantissa piecewise from the limbs.
  1275. // This loop does not round. For finite values, the absolute
  1276. // value of the mantissa is scaled and between 1 and 10.
  1277. auto itr_data = data.cbegin() + static_cast<std::size_t>(UINT8_C(1));
  1278. while ((itr_data != data.cend()) && (digit_counter <= std::numeric_limits<double>::max_digits10))
  1279. {
  1280. p10 *= static_cast<double>(cpp_dec_float_elem_mask);
  1281. mantissa += static_cast<double>(static_cast<double>(*itr_data++) / p10);
  1282. digit_counter += static_cast<int>(cpp_dec_float_elem_digits10);
  1283. }
  1284. }
  1285. template <unsigned Digits10, class ExponentType, class Allocator>
  1286. double cpp_dec_float<Digits10, ExponentType, Allocator>::extract_double() const
  1287. {
  1288. // Returns the double conversion of a cpp_dec_float<Digits10, ExponentType, Allocator>.
  1289. // Check for non-normal cpp_dec_float<Digits10, ExponentType, Allocator>.
  1290. if (!(isfinite)())
  1291. {
  1292. if ((isnan)())
  1293. {
  1294. return std::numeric_limits<double>::quiet_NaN();
  1295. }
  1296. else
  1297. {
  1298. return ((!neg) ? +std::numeric_limits<double>::infinity()
  1299. : -std::numeric_limits<double>::infinity());
  1300. }
  1301. }
  1302. cpp_dec_float<Digits10, ExponentType, Allocator> xx(*this);
  1303. if (xx.isneg())
  1304. xx.negate();
  1305. // Check if *this cpp_dec_float<Digits10, ExponentType, Allocator> is zero.
  1306. if (iszero() || (xx.compare(double_min()) < 0))
  1307. {
  1308. return 0.0;
  1309. }
  1310. // Check if *this cpp_dec_float<Digits10, ExponentType, Allocator> exceeds the maximum of double.
  1311. if (xx.compare(double_max()) > 0)
  1312. {
  1313. return ((!neg) ? +std::numeric_limits<double>::infinity()
  1314. : -std::numeric_limits<double>::infinity());
  1315. }
  1316. std::stringstream strm { };
  1317. strm.imbue(std::locale::classic());
  1318. strm << str(std::numeric_limits<double>::max_digits10, std::ios_base::scientific);
  1319. double d;
  1320. strm >> d;
  1321. return d;
  1322. }
  1323. template <unsigned Digits10, class ExponentType, class Allocator>
  1324. long double cpp_dec_float<Digits10, ExponentType, Allocator>::extract_long_double() const
  1325. {
  1326. // Returns the long double conversion of a cpp_dec_float<Digits10, ExponentType, Allocator>.
  1327. // Check if *this cpp_dec_float<Digits10, ExponentType, Allocator> is subnormal.
  1328. if (!(isfinite)())
  1329. {
  1330. if ((isnan)())
  1331. {
  1332. return std::numeric_limits<long double>::quiet_NaN();
  1333. }
  1334. else
  1335. {
  1336. return ((!neg) ? std::numeric_limits<long double>::infinity()
  1337. : -std::numeric_limits<long double>::infinity());
  1338. }
  1339. }
  1340. cpp_dec_float<Digits10, ExponentType, Allocator> xx(*this);
  1341. if (xx.isneg())
  1342. xx.negate();
  1343. // Check if *this cpp_dec_float<Digits10, ExponentType, Allocator> is zero.
  1344. if (iszero() || (xx.compare(long_double_min()) < 0))
  1345. {
  1346. return static_cast<long double>(0.0);
  1347. }
  1348. // Check if *this cpp_dec_float<Digits10, ExponentType, Allocator> exceeds the maximum of double.
  1349. if (xx.compare(long_double_max()) > 0)
  1350. {
  1351. return ((!neg) ? std::numeric_limits<long double>::infinity()
  1352. : -std::numeric_limits<long double>::infinity());
  1353. }
  1354. std::stringstream strm;
  1355. strm.imbue(std::locale::classic());
  1356. strm << str(std::numeric_limits<long double>::max_digits10, std::ios_base::scientific);
  1357. long double ld;
  1358. strm >> ld;
  1359. return ld;
  1360. }
  1361. template <unsigned Digits10, class ExponentType, class Allocator>
  1362. long long cpp_dec_float<Digits10, ExponentType, Allocator>::extract_signed_long_long() const
  1363. {
  1364. // Extracts a signed long long from *this.
  1365. // If (x > maximum of long long) or (x < minimum of long long),
  1366. // then the maximum or minimum of long long is returned accordingly.
  1367. if (exp < static_cast<exponent_type>(0))
  1368. {
  1369. return static_cast<long long>(0);
  1370. }
  1371. const bool b_neg = isneg();
  1372. unsigned long long val;
  1373. if ((!b_neg) && (compare(long_long_max()) > 0))
  1374. {
  1375. return (std::numeric_limits<long long>::max)();
  1376. }
  1377. else if (b_neg && (compare(long_long_min()) < 0))
  1378. {
  1379. return (std::numeric_limits<long long>::min)();
  1380. }
  1381. else
  1382. {
  1383. // Extract the data into an unsigned long long value.
  1384. cpp_dec_float<Digits10, ExponentType, Allocator> xn(extract_integer_part());
  1385. if (xn.isneg())
  1386. xn.negate();
  1387. val = static_cast<unsigned long long>(xn.data[0]);
  1388. const std::int32_t imax = (std::min)(static_cast<std::int32_t>(static_cast<std::int32_t>(xn.exp) / cpp_dec_float_elem_digits10), static_cast<std::int32_t>(cpp_dec_float_elem_number - static_cast<std::int32_t>(1)));
  1389. for (std::int32_t i = static_cast<std::int32_t>(1); i <= imax; i++)
  1390. {
  1391. val *= static_cast<unsigned long long>(cpp_dec_float_elem_mask);
  1392. val += static_cast<unsigned long long>(xn.data[static_cast<std::size_t>(i)]);
  1393. }
  1394. }
  1395. if (!b_neg)
  1396. {
  1397. return static_cast<long long>(val);
  1398. }
  1399. else
  1400. {
  1401. // This strange expression avoids a hardware trap in the corner case
  1402. // that val is the most negative value permitted in long long.
  1403. // See https://svn.boost.org/trac/boost/ticket/9740.
  1404. //
  1405. long long sval = static_cast<long long>(val - 1);
  1406. sval = -sval;
  1407. --sval;
  1408. return sval;
  1409. }
  1410. }
  1411. template <unsigned Digits10, class ExponentType, class Allocator>
  1412. unsigned long long cpp_dec_float<Digits10, ExponentType, Allocator>::extract_unsigned_long_long() const
  1413. {
  1414. // Extracts an unsigned long long from *this.
  1415. // If x exceeds the maximum of unsigned long long,
  1416. // then the maximum of unsigned long long is returned.
  1417. // If x is negative, then the unsigned long long cast of
  1418. // the long long extracted value is returned.
  1419. if (isneg())
  1420. {
  1421. return static_cast<unsigned long long>(extract_signed_long_long());
  1422. }
  1423. if (exp < static_cast<exponent_type>(0))
  1424. {
  1425. return static_cast<unsigned long long>(0u);
  1426. }
  1427. const cpp_dec_float<Digits10, ExponentType, Allocator> xn(extract_integer_part());
  1428. unsigned long long val;
  1429. if (xn.compare(ulong_long_max()) > 0)
  1430. {
  1431. return (std::numeric_limits<unsigned long long>::max)();
  1432. }
  1433. else
  1434. {
  1435. // Extract the data into an unsigned long long value.
  1436. val = static_cast<unsigned long long>(xn.data[std::size_t { 0U }]);
  1437. const std::int32_t imax = (std::min)(static_cast<std::int32_t>(static_cast<std::int32_t>(xn.exp) / cpp_dec_float_elem_digits10), static_cast<std::int32_t>(cpp_dec_float_elem_number - static_cast<std::int32_t>(1)));
  1438. for (std::int32_t i = static_cast<std::int32_t>(1); i <= imax; i++)
  1439. {
  1440. val *= static_cast<unsigned long long>(cpp_dec_float_elem_mask);
  1441. val += static_cast<unsigned long long>(xn.data[static_cast<std::size_t>(i)]);
  1442. }
  1443. }
  1444. return val;
  1445. }
  1446. #ifdef BOOST_HAS_INT128
  1447. template <unsigned Digits10, class ExponentType, class Allocator>
  1448. int128_type cpp_dec_float<Digits10, ExponentType, Allocator>::extract_signed_int128() const
  1449. {
  1450. // Extracts a signed __int128 from *this.
  1451. // If (x > maximum of __int128) or (x < minimum of __int128),
  1452. // then the maximum or minimum of long long is returned accordingly.
  1453. if (exp < static_cast<exponent_type>(0))
  1454. {
  1455. return static_cast<int128_type>(0);
  1456. }
  1457. const bool b_neg = isneg();
  1458. cpp_dec_float<Digits10, ExponentType, Allocator> i128max;
  1459. i128max = ((~static_cast<uint128_type>(0)) >> 1);
  1460. cpp_dec_float<Digits10, ExponentType, Allocator> i128min;
  1461. i128min = (-1 - static_cast<int128_type>((static_cast<uint128_type>(1) << 127) - 1));
  1462. uint128_type val;
  1463. if ((!b_neg) && (compare(i128max) > 0))
  1464. {
  1465. return ((~static_cast<uint128_type>(0)) >> 1);
  1466. }
  1467. else if (b_neg && (compare(i128min) < 0))
  1468. {
  1469. return (-1 - static_cast<int128_type>((static_cast<uint128_type>(1) << 127) - 1));
  1470. }
  1471. else
  1472. {
  1473. // Extract the data into an (unsigned) boost::uint128_type value.
  1474. cpp_dec_float<Digits10, ExponentType, Allocator> xn(extract_integer_part());
  1475. if (xn.isneg())
  1476. xn.negate();
  1477. val = static_cast<uint128_type>(xn.data[0]);
  1478. const std::int32_t imax = (std::min)(static_cast<std::int32_t>(static_cast<std::int32_t>(xn.exp) / cpp_dec_float_elem_digits10), static_cast<std::int32_t>(cpp_dec_float_elem_number - static_cast<std::int32_t>(1)));
  1479. for (std::int32_t i = static_cast<std::int32_t>(1); i <= imax; i++)
  1480. {
  1481. val *= static_cast<uint128_type>(cpp_dec_float_elem_mask);
  1482. val += static_cast<uint128_type>(xn.data[static_cast<std::size_t>(i)]);
  1483. }
  1484. }
  1485. if (!b_neg)
  1486. {
  1487. return static_cast<int128_type>(val);
  1488. }
  1489. else
  1490. {
  1491. // This strange expression avoids a hardware trap in the corner case that
  1492. // val is the most negative value permitted in (signed) boost::int128_type.
  1493. // See also https://svn.boost.org/trac/boost/ticket/9740.
  1494. //
  1495. int128_type sval = static_cast<int128_type>(val - 1);
  1496. sval = -sval;
  1497. --sval;
  1498. return sval;
  1499. }
  1500. }
  1501. template <unsigned Digits10, class ExponentType, class Allocator>
  1502. uint128_type cpp_dec_float<Digits10, ExponentType, Allocator>::extract_unsigned_int128() const
  1503. {
  1504. // Extracts an unsigned __int128 from *this.
  1505. // If x exceeds the maximum of unsigned __int128,
  1506. // then the maximum of unsigned __int128 is returned.
  1507. // If x is negative, then the unsigned __int128 cast of
  1508. // the __int128 extracted value is returned.
  1509. if (isneg())
  1510. {
  1511. return static_cast<uint128_type>(extract_signed_int128());
  1512. }
  1513. if (exp < static_cast<exponent_type>(0))
  1514. {
  1515. return 0u;
  1516. }
  1517. const cpp_dec_float<Digits10, ExponentType, Allocator> xn(extract_integer_part());
  1518. cpp_dec_float<Digits10, ExponentType, Allocator> i128max;
  1519. i128max = (~static_cast<uint128_type>(0));
  1520. uint128_type val;
  1521. if (xn.compare(i128max) > 0)
  1522. {
  1523. return (~static_cast<uint128_type>(0));
  1524. }
  1525. else
  1526. {
  1527. // Extract the data into an unsigned long long value.
  1528. val = static_cast<uint128_type>(xn.data[0]);
  1529. const std::int32_t imax = (std::min)(static_cast<std::int32_t>(static_cast<std::int32_t>(xn.exp) / cpp_dec_float_elem_digits10), static_cast<std::int32_t>(cpp_dec_float_elem_number - static_cast<std::int32_t>(1)));
  1530. for (std::int32_t i = static_cast<std::int32_t>(1); i <= imax; i++)
  1531. {
  1532. val *= static_cast<uint128_type>(cpp_dec_float_elem_mask);
  1533. val += static_cast<uint128_type>(xn.data[i]);
  1534. }
  1535. }
  1536. return val;
  1537. }
  1538. #endif
  1539. template <unsigned Digits10, class ExponentType, class Allocator>
  1540. cpp_dec_float<Digits10, ExponentType, Allocator> cpp_dec_float<Digits10, ExponentType, Allocator>::extract_integer_part() const
  1541. {
  1542. // Compute the signed integer part of x.
  1543. if (!(isfinite)())
  1544. {
  1545. return *this;
  1546. }
  1547. if (exp < static_cast<ExponentType>(0))
  1548. {
  1549. // The absolute value of the number is smaller than 1.
  1550. // Thus the integer part is zero.
  1551. return zero();
  1552. }
  1553. // Truncate the digits from the decimal part, including guard digits
  1554. // that do not belong to the integer part.
  1555. // Make a local copy.
  1556. cpp_dec_float<Digits10, ExponentType, Allocator> x = *this;
  1557. // Clear out the decimal portion
  1558. const std::size_t first_clear = (static_cast<std::size_t>(x.exp) / static_cast<std::size_t>(cpp_dec_float_elem_digits10)) + 1u;
  1559. const std::size_t last_clear = static_cast<std::size_t>(cpp_dec_float_elem_number);
  1560. if (first_clear < last_clear)
  1561. std::fill(x.data.begin() + static_cast<std::ptrdiff_t>(first_clear), x.data.begin() + static_cast<std::ptrdiff_t>(last_clear), static_cast<std::uint32_t>(0u));
  1562. return x;
  1563. }
  1564. template <unsigned Digits10, class ExponentType, class Allocator>
  1565. std::string cpp_dec_float<Digits10, ExponentType, Allocator>::str(std::intmax_t number_of_digits, std::ios_base::fmtflags f) const
  1566. {
  1567. if ((this->isinf)())
  1568. {
  1569. if (this->isneg())
  1570. return "-inf";
  1571. else if (f & std::ios_base::showpos)
  1572. return "+inf";
  1573. else
  1574. return "inf";
  1575. }
  1576. else if ((this->isnan)())
  1577. {
  1578. return "nan";
  1579. }
  1580. std::string my_str;
  1581. std::intmax_t org_digits(number_of_digits);
  1582. exponent_type my_exp = order();
  1583. if (!(f & std::ios_base::fixed) && (number_of_digits == 0))
  1584. number_of_digits = cpp_dec_float_max_digits10;
  1585. if (f & std::ios_base::fixed)
  1586. {
  1587. number_of_digits += my_exp + 1;
  1588. }
  1589. else if (f & std::ios_base::scientific)
  1590. ++number_of_digits;
  1591. // Determine the number of elements needed to provide the requested
  1592. // digits from cpp_dec_float<Digits10, ExponentType, Allocator>.
  1593. const std::intmax_t
  1594. number_of_elements_signed
  1595. {
  1596. (std::max)(static_cast<std::intmax_t>(number_of_digits / static_cast<std::intmax_t>(cpp_dec_float_elem_digits10) + 2),
  1597. std::intmax_t { INT8_C(0) })
  1598. };
  1599. const std::size_t number_of_elements = (std::min)(static_cast<std::size_t>(number_of_elements_signed),
  1600. static_cast<std::size_t>(cpp_dec_float_elem_number));
  1601. // Extract the remaining digits from cpp_dec_float<Digits10, ExponentType, Allocator> after the decimal point.
  1602. std::stringstream strm;
  1603. strm.imbue(std::locale::classic());
  1604. strm << data[0];
  1605. // Extract all of the digits from cpp_dec_float<Digits10, ExponentType, Allocator>, beginning with the first data element.
  1606. for (std::size_t i = static_cast<std::size_t>(1u); i < number_of_elements; i++)
  1607. {
  1608. strm << std::setw(static_cast<std::streamsize>(cpp_dec_float_elem_digits10))
  1609. << std::setfill(static_cast<char>('0'))
  1610. << data[i];
  1611. }
  1612. my_str += strm.str();
  1613. bool have_leading_zeros = false;
  1614. if (number_of_digits == 0)
  1615. {
  1616. // We only get here if the output format is "fixed" and we just need to
  1617. // round the first non-zero digit.
  1618. number_of_digits -= my_exp + 1; // reset to original value
  1619. if (number_of_digits)
  1620. {
  1621. my_str.insert(static_cast<std::string::size_type>(0), std::string::size_type(number_of_digits), '0');
  1622. have_leading_zeros = true; // LCOV_EXCL_LINE This causes a false negative on lcov coverage test.
  1623. }
  1624. }
  1625. if (number_of_digits < 0)
  1626. {
  1627. my_str = "0"; // LCOV_EXCL_LINE This causes a false negative on lcov coverage test.
  1628. if (isneg())
  1629. my_str.insert(static_cast<std::string::size_type>(0), 1, '-');
  1630. boost::multiprecision::detail::format_float_string(my_str, 0, number_of_digits - my_exp - 1, f, this->iszero());
  1631. return my_str;
  1632. }
  1633. else
  1634. {
  1635. // Cut the output to the size of the precision.
  1636. if (my_str.length() > static_cast<std::string::size_type>(number_of_digits))
  1637. {
  1638. // Get the digit after the last needed digit for rounding
  1639. const std::uint32_t round = static_cast<std::uint32_t>(static_cast<std::uint32_t>(my_str[static_cast<std::string::size_type>(number_of_digits)]) - static_cast<std::uint32_t>('0'));
  1640. bool need_round_up = round >= 5u;
  1641. if (round == 5u)
  1642. {
  1643. const std::uint32_t ix = number_of_digits == 0 ? 0 : static_cast<std::uint32_t>(static_cast<std::uint32_t>(my_str[static_cast<std::string::size_type>(number_of_digits - 1)]) - static_cast<std::uint32_t>('0'));
  1644. if ((ix & 1u) == 0)
  1645. {
  1646. // We have an even digit followed by a 5, so we might not actually
  1647. // need to round up if all the remaining digits are zero:
  1648. if (my_str.find_first_not_of('0', static_cast<std::string::size_type>(number_of_digits + 1)) == std::string::npos)
  1649. {
  1650. bool all_zeros = true;
  1651. // There are no non-zero trailing digits in the string,
  1652. // now check whatever parts we didn't convert to the string:
  1653. for (std::size_t i = number_of_elements; i < data.size(); i++)
  1654. {
  1655. if (data[i])
  1656. {
  1657. all_zeros = false;
  1658. break;
  1659. }
  1660. }
  1661. if (all_zeros)
  1662. need_round_up = false; // tie break - round to even.
  1663. }
  1664. }
  1665. }
  1666. // Truncate the string
  1667. my_str.erase(static_cast<std::string::size_type>(number_of_digits));
  1668. if (need_round_up)
  1669. {
  1670. if (my_str.size())
  1671. {
  1672. std::size_t ix = static_cast<std::size_t>(my_str.length() - 1u);
  1673. // Every trailing 9 must be rounded up
  1674. while (ix && (static_cast<std::int32_t>(my_str.at(ix)) - static_cast<std::int32_t>('0') == static_cast<std::int32_t>(9)))
  1675. {
  1676. my_str.at(ix) = static_cast<char>('0');
  1677. --ix;
  1678. }
  1679. if (!ix)
  1680. {
  1681. // There were nothing but trailing nines.
  1682. if (static_cast<std::int32_t>(static_cast<std::int32_t>(my_str.at(ix)) - static_cast<std::int32_t>(0x30)) == static_cast<std::int32_t>(9))
  1683. {
  1684. // Increment up to the next order and adjust exponent.
  1685. my_str.at(ix) = static_cast<char>('1');
  1686. ++my_exp;
  1687. }
  1688. else
  1689. {
  1690. // Round up this digit.
  1691. ++my_str.at(ix);
  1692. }
  1693. }
  1694. else
  1695. {
  1696. // Round up the last digit.
  1697. ++my_str[ix];
  1698. }
  1699. }
  1700. else
  1701. {
  1702. my_str = "1"; // LCOV_EXCL_LINE This causes a false negative on lcov coverage test.
  1703. ++my_exp;
  1704. }
  1705. }
  1706. }
  1707. }
  1708. if (have_leading_zeros)
  1709. {
  1710. // We need to take the zeros back out again, and correct the exponent
  1711. // if we rounded up:
  1712. if (my_str[std::string::size_type(number_of_digits - 1)] != '0')
  1713. {
  1714. ++my_exp;
  1715. my_str.erase(0, std::string::size_type(number_of_digits - 1));
  1716. }
  1717. else
  1718. my_str.erase(0, std::string::size_type(number_of_digits));
  1719. }
  1720. if (isneg())
  1721. my_str.insert(static_cast<std::string::size_type>(0), 1, '-');
  1722. boost::multiprecision::detail::format_float_string(my_str, my_exp, org_digits, f, this->iszero());
  1723. return my_str;
  1724. }
  1725. template <unsigned Digits10, class ExponentType, class Allocator>
  1726. bool cpp_dec_float<Digits10, ExponentType, Allocator>::rd_string(const char* const s)
  1727. {
  1728. #ifndef BOOST_NO_EXCEPTIONS
  1729. try
  1730. {
  1731. #endif
  1732. std::string my_str(s);
  1733. static const std::string valid_characters{"0123456789"};
  1734. // TBD: Using several regular expressions may significantly reduce
  1735. // the code complexity (and perhaps the run-time) of rd_string().
  1736. // Get a possible exponent and remove it.
  1737. exp = static_cast<exponent_type>(0);
  1738. std::size_t pos;
  1739. if (((pos = my_str.find('e')) != std::string::npos) || ((pos = my_str.find('E')) != std::string::npos))
  1740. {
  1741. // Remove the exponent part from the string.
  1742. #ifndef BOOST_MP_STANDALONE
  1743. exp = boost::lexical_cast<exponent_type>(static_cast<const char*>(my_str.c_str() + (pos + 1u)));
  1744. #else
  1745. if (my_str.find_first_not_of(valid_characters, ((my_str[pos + 1] == '+') || (my_str[pos + 1] == '-')) ? pos + 2 : pos + 1) != std::string::npos)
  1746. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Can not construct a floating point with non-numeric content")); // LCOV_EXCL_LINE
  1747. exp = static_cast<exponent_type>(std::atoll(static_cast<const char*>(my_str.c_str() + (pos + 1u))));
  1748. #endif
  1749. my_str = my_str.substr(static_cast<std::size_t>(0u), pos);
  1750. }
  1751. // Get a possible +/- sign and remove it.
  1752. neg = false;
  1753. if (!my_str.empty())
  1754. {
  1755. if (my_str[0] == '-')
  1756. {
  1757. neg = true;
  1758. my_str.erase(0, 1);
  1759. }
  1760. else if (my_str[0] == '+')
  1761. {
  1762. my_str.erase(0, 1);
  1763. }
  1764. }
  1765. //
  1766. // Special cases for infinities and NaN's:
  1767. //
  1768. if ((my_str == "inf") || (my_str == "INF") || (my_str == "infinity") || (my_str == "INFINITY"))
  1769. {
  1770. const bool tmp_neg { neg };
  1771. *this = this->inf();
  1772. neg = tmp_neg;
  1773. return true;
  1774. }
  1775. if ((my_str.size() >= 3) && ((my_str.substr(0, 3) == "nan") || (my_str.substr(0, 3) == "NAN") || (my_str.substr(0, 3) == "NaN")))
  1776. {
  1777. *this = this->nan();
  1778. return true;
  1779. }
  1780. // Remove the leading zeros for all input types.
  1781. const std::string::iterator fwd_it_leading_zero = std::find_if(my_str.begin(), my_str.end(), char_is_nonzero_predicate);
  1782. if (fwd_it_leading_zero != my_str.begin())
  1783. {
  1784. if (fwd_it_leading_zero == my_str.end())
  1785. {
  1786. // The string contains nothing but leading zeros.
  1787. // This string represents zero.
  1788. operator=(zero());
  1789. return true;
  1790. }
  1791. else
  1792. {
  1793. my_str.erase(my_str.begin(), fwd_it_leading_zero);
  1794. }
  1795. }
  1796. // Put the input string into the standard cpp_dec_float<Digits10, ExponentType, Allocator> input form
  1797. // aaa.bbbbE+/-n, where aaa has 1...cpp_dec_float_elem_digits10, bbbb has an
  1798. // even multiple of cpp_dec_float_elem_digits10 which are possibly zero padded
  1799. // on the right-end, and n is a signed 64-bit integer which is an
  1800. // even multiple of cpp_dec_float_elem_digits10.
  1801. // Find a possible decimal point.
  1802. pos = my_str.find(static_cast<char>('.'));
  1803. if (pos != std::string::npos)
  1804. {
  1805. // Check we have only digits either side of the point:
  1806. if (my_str.find_first_not_of(valid_characters) != pos)
  1807. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Can not construct a floating point with non-numeric content")); // LCOV_EXCL_LINE
  1808. if (my_str.find_first_not_of(valid_characters, pos + 1) != std::string::npos)
  1809. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Can not construct a floating point with non-numeric content")); // LCOV_EXCL_LINE
  1810. // Remove all trailing insignificant zeros.
  1811. const std::string::const_reverse_iterator rit_non_zero = std::find_if(my_str.rbegin(), my_str.rend(), char_is_nonzero_predicate);
  1812. if (rit_non_zero != static_cast<std::string::const_reverse_iterator>(my_str.rbegin()))
  1813. {
  1814. const std::string::size_type ofs =
  1815. static_cast<std::string::size_type>
  1816. (
  1817. static_cast<std::ptrdiff_t>(my_str.length())
  1818. - std::distance<std::string::const_reverse_iterator>(my_str.rbegin(), rit_non_zero)
  1819. );
  1820. my_str.erase(my_str.begin() + static_cast<std::ptrdiff_t>(ofs), my_str.end());
  1821. }
  1822. // Check if the input is identically zero.
  1823. if (my_str == std::string("."))
  1824. {
  1825. operator=(zero());
  1826. return true;
  1827. }
  1828. // Remove leading significant zeros just after the decimal point
  1829. // and adjust the exponent accordingly.
  1830. // Note that the while-loop operates only on strings of the form ".000abcd..."
  1831. // and peels away the zeros just after the decimal point.
  1832. if (my_str.at(static_cast<std::size_t>(0u)) == static_cast<char>('.'))
  1833. {
  1834. const std::string::iterator it_non_zero = std::find_if(my_str.begin() + 1u, my_str.end(), char_is_nonzero_predicate);
  1835. std::size_t delta_exp = static_cast<std::size_t>(0u);
  1836. if (my_str.at(static_cast<std::size_t>(1u)) == static_cast<char>('0'))
  1837. {
  1838. delta_exp = static_cast<std::size_t>(std::distance<std::string::const_iterator>(my_str.begin() + 1u, it_non_zero));
  1839. }
  1840. // Bring one single digit into the mantissa and adjust the exponent accordingly.
  1841. my_str.erase(my_str.begin(), it_non_zero);
  1842. my_str.insert(static_cast<std::string::size_type>(1u), ".");
  1843. exp -= static_cast<exponent_type>(delta_exp + 1u);
  1844. }
  1845. }
  1846. else
  1847. {
  1848. // We should have only digits:
  1849. if (my_str.find_first_not_of(valid_characters) != std::string::npos)
  1850. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Can not construct a floating point with non-numeric content")); // LCOV_EXCL_LINE
  1851. // Input string has no decimal point: Append decimal point.
  1852. my_str.append(".");
  1853. }
  1854. // Shift the decimal point such that the exponent is an even multiple of cpp_dec_float_elem_digits10.
  1855. std::ptrdiff_t n_shift = static_cast<std::ptrdiff_t>(0);
  1856. const std::ptrdiff_t n_exp_rem = static_cast<std::ptrdiff_t>(exp % static_cast<exponent_type>(cpp_dec_float_elem_digits10));
  1857. if((exp % static_cast<exponent_type>(cpp_dec_float_elem_digits10)) != static_cast<exponent_type>(0))
  1858. {
  1859. n_shift = ((exp < static_cast<exponent_type>(0))
  1860. ? static_cast<std::ptrdiff_t>(n_exp_rem + static_cast<std::ptrdiff_t>(cpp_dec_float_elem_digits10))
  1861. : static_cast<std::ptrdiff_t>(n_exp_rem));
  1862. }
  1863. // Make sure that there are enough digits for the decimal point shift.
  1864. pos = my_str.find(static_cast<char>('.'));
  1865. std::ptrdiff_t pos_plus_one = static_cast<std::ptrdiff_t>(pos + 1);
  1866. if ((static_cast<std::ptrdiff_t>(my_str.length()) - pos_plus_one) < n_shift)
  1867. {
  1868. const std::ptrdiff_t sz = static_cast<std::ptrdiff_t>(n_shift - (static_cast<std::ptrdiff_t>(my_str.length()) - pos_plus_one));
  1869. my_str.append(std::string(static_cast<std::string::size_type>(sz), static_cast<char>('0')));
  1870. }
  1871. // Do the decimal point shift.
  1872. if (n_shift != static_cast<std::ptrdiff_t>(0))
  1873. {
  1874. my_str.insert(static_cast<std::string::size_type>(pos_plus_one + n_shift), ".");
  1875. my_str.erase(pos, static_cast<std::ptrdiff_t>(1));
  1876. exp -= static_cast<exponent_type>(n_shift);
  1877. }
  1878. // Cut the size of the mantissa to <= cpp_dec_float_elem_digits10.
  1879. pos = my_str.find(static_cast<char>('.'));
  1880. pos_plus_one = static_cast<std::ptrdiff_t>(pos + 1u);
  1881. if (pos > static_cast<std::size_t>(cpp_dec_float_elem_digits10))
  1882. {
  1883. const std::int32_t n_pos = static_cast<std::int32_t>(pos);
  1884. const std::int32_t n_rem_is_zero = ((static_cast<std::int32_t>(n_pos % cpp_dec_float_elem_digits10) == static_cast<std::int32_t>(0)) ? static_cast<std::int32_t>(1) : static_cast<std::int32_t>(0));
  1885. const std::int32_t n = static_cast<std::int32_t>(static_cast<std::int32_t>(n_pos / cpp_dec_float_elem_digits10) - n_rem_is_zero);
  1886. my_str.insert(static_cast<std::size_t>(static_cast<std::int32_t>(n_pos - static_cast<std::int32_t>(n * cpp_dec_float_elem_digits10))), ".");
  1887. my_str.erase(static_cast<std::size_t>(pos_plus_one), static_cast<std::size_t>(1u));
  1888. exp += static_cast<exponent_type>(static_cast<exponent_type>(n) * static_cast<exponent_type>(cpp_dec_float_elem_digits10));
  1889. }
  1890. // Pad the decimal part such that its value is an even
  1891. // multiple of cpp_dec_float_elem_digits10.
  1892. pos = my_str.find(static_cast<char>('.'));
  1893. pos_plus_one = static_cast<std::ptrdiff_t>(pos + 1u);
  1894. // Throws an error for a strange construction like 3.14L
  1895. if(pos != std::string::npos && (my_str.back() == 'L' || my_str.back() == 'l' || my_str.back() == 'u' || my_str.back() == 'U'))
  1896. {
  1897. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Can not construct a floating point with an integer literal")); // LCOV_EXCL_LINE
  1898. }
  1899. const std::int32_t n_dec = static_cast<std::int32_t>(static_cast<std::int32_t>(my_str.length() - 1u) - static_cast<std::int32_t>(pos));
  1900. const std::int32_t n_rem = static_cast<std::int32_t>(n_dec % cpp_dec_float_elem_digits10);
  1901. std::int32_t n_cnt = ((n_rem != static_cast<std::int32_t>(0))
  1902. ? static_cast<std::int32_t>(cpp_dec_float_elem_digits10 - n_rem)
  1903. : static_cast<std::int32_t>(0));
  1904. if (n_cnt != static_cast<std::int32_t>(0))
  1905. {
  1906. my_str.append(static_cast<std::size_t>(n_cnt), static_cast<char>('0'));
  1907. }
  1908. // Truncate decimal part if it is too long.
  1909. const std::size_t max_dec = static_cast<std::size_t>((cpp_dec_float_elem_number - 1) * cpp_dec_float_elem_digits10);
  1910. if (static_cast<std::size_t>(my_str.length() - pos) > max_dec)
  1911. {
  1912. my_str = my_str.substr(static_cast<std::size_t>(0u),
  1913. static_cast<std::size_t>(pos_plus_one + static_cast<std::ptrdiff_t>(max_dec)));
  1914. }
  1915. // Now the input string has the standard cpp_dec_float<Digits10, ExponentType, Allocator> input form.
  1916. // (See the comment above.)
  1917. // Set all the data elements to 0.
  1918. std::fill(data.begin(), data.end(), static_cast<std::uint32_t>(0u));
  1919. // Extract the data.
  1920. // First get the digits to the left of the decimal point...
  1921. data[0u] = static_cast<std::uint32_t>(std::stol(my_str.substr(static_cast<std::size_t>(0u), pos)));
  1922. // ...then get the remaining digits to the right of the decimal point.
  1923. const std::string::size_type i_end =
  1924. (
  1925. static_cast<std::string::size_type>(my_str.length() - static_cast<std::string::size_type>(pos_plus_one))
  1926. / static_cast<std::string::size_type>(cpp_dec_float_elem_digits10)
  1927. );
  1928. for (std::string::size_type i = static_cast<std::string::size_type>(0u); i < i_end; i++)
  1929. {
  1930. const std::string::const_iterator it =
  1931. my_str.begin()
  1932. + static_cast<std::ptrdiff_t>
  1933. (
  1934. static_cast<std::string::size_type>(pos_plus_one)
  1935. + static_cast<std::string::size_type>(i * static_cast<std::string::size_type>(cpp_dec_float_elem_digits10))
  1936. );
  1937. data[i + 1u] = static_cast<std::uint32_t>(std::stol(std::string(it, it + static_cast<std::string::size_type>(cpp_dec_float_elem_digits10))));
  1938. }
  1939. // Check for overflow...
  1940. if (exp >= cpp_dec_float_max_exp10)
  1941. {
  1942. const bool neg_tmp { neg };
  1943. neg = false;
  1944. const int compare_result { compare((cpp_dec_float::max)()) };
  1945. if(compare_result > 0)
  1946. {
  1947. *this = inf();
  1948. }
  1949. neg = neg_tmp;
  1950. }
  1951. // ...and check for underflow.
  1952. if (exp <= cpp_dec_float_min_exp10)
  1953. {
  1954. const bool neg_tmp { neg };
  1955. neg = false;
  1956. const int compare_result { compare((cpp_dec_float::min)()) };
  1957. if(compare_result < 0)
  1958. {
  1959. *this = zero();
  1960. }
  1961. else
  1962. {
  1963. neg = neg_tmp;
  1964. }
  1965. }
  1966. #ifndef BOOST_NO_EXCEPTIONS
  1967. }
  1968. #ifndef BOOST_MP_STANDALONE
  1969. catch (const bad_lexical_cast&)
  1970. #else
  1971. catch (const std::exception&)
  1972. #endif
  1973. {
  1974. // Rethrow with better error message:
  1975. std::string msg = "Unable to parse the string \"";
  1976. msg += s;
  1977. msg += "\" as a floating point value.";
  1978. throw std::runtime_error(msg);
  1979. }
  1980. #endif
  1981. return true;
  1982. }
  1983. template <unsigned Digits10, class ExponentType, class Allocator>
  1984. cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float(const double mantissa, const ExponentType exp10)
  1985. {
  1986. // Create *this cpp_dec_float<Digits10, ExponentType, Allocator>
  1987. // from a given mantissa and base-10 exponent. It is important
  1988. // to note that this constructor does *not* maintain the full
  1989. // precision of double. It is used internally (privately), mostly
  1990. // as a first guess for Newton iteration in square root and inversion.
  1991. std::fill(data.begin(), data.end(), static_cast<std::uint32_t>(0u));
  1992. constexpr double
  1993. dbl_min_check
  1994. {
  1995. static_cast<double>
  1996. (
  1997. (std::numeric_limits<double>::min)() * (1.0 + std::numeric_limits<double>::epsilon())
  1998. )
  1999. };
  2000. const bool b_neg { (mantissa < 0.0) };
  2001. double d_mant { ((!b_neg) ? mantissa : -mantissa) };
  2002. const bool mantissa_is_non_zero { (d_mant > dbl_min_check) };
  2003. if (mantissa_is_non_zero)
  2004. {
  2005. exponent_type exp10_val { exp10 };
  2006. // This subroutine is used after a call to extract_parts().
  2007. // The corresponding scaling loop for (d_mant > 10.0) is not
  2008. // needed here since extract_parts() returns a mantissa value
  2009. // between 1 and 10. The loop for (d_mant < 1.0) is needed
  2010. // since the initial guess of the square root in tghe subroutine
  2011. // calculate_root() might have been divided by 10 in order
  2012. // to obtain an exponent value that is a multiple of 2.
  2013. while (d_mant < 1.0)
  2014. {
  2015. d_mant *= 10.0;
  2016. --exp10_val;
  2017. }
  2018. std::int32_t shift = static_cast<std::int32_t>(exp10_val % static_cast<std::int32_t>(cpp_dec_float_elem_digits10));
  2019. while (static_cast<std::int32_t>(shift-- % cpp_dec_float_elem_digits10) != static_cast<std::int32_t>(0))
  2020. {
  2021. d_mant *= 10.0;
  2022. --exp10_val;
  2023. }
  2024. exp = exp10_val;
  2025. neg = b_neg;
  2026. constexpr std::int32_t digit_ratio = static_cast<std::int32_t>(static_cast<std::int32_t>(std::numeric_limits<double>::digits10) / static_cast<std::int32_t>(cpp_dec_float_elem_digits10));
  2027. constexpr std::int32_t digit_loops = static_cast<std::int32_t>(digit_ratio + static_cast<std::int32_t>(2));
  2028. for (std::int32_t i = static_cast<std::int32_t>(0); i < digit_loops; i++)
  2029. {
  2030. std::uint32_t n = static_cast<std::uint32_t>(static_cast<std::uint64_t>(d_mant));
  2031. data[static_cast<std::size_t>(i)] = static_cast<std::uint32_t>(n);
  2032. d_mant -= static_cast<double>(n);
  2033. d_mant *= static_cast<double>(cpp_dec_float_elem_mask);
  2034. }
  2035. }
  2036. } // LCOV_EXCL_LINE This causes a false negative on lcov coverage test.
  2037. template <unsigned Digits10, class ExponentType, class Allocator>
  2038. template <class Float>
  2039. typename std::enable_if<std::is_floating_point<Float>::value, cpp_dec_float<Digits10, ExponentType, Allocator>&>::type cpp_dec_float<Digits10, ExponentType, Allocator>::operator=(Float a)
  2040. {
  2041. // Christopher Kormanyos's original code used a cast to long long here,
  2042. // but that fails when long double has more digits than a long long.
  2043. BOOST_MP_FLOAT128_USING
  2044. using std::floor;
  2045. using std::frexp;
  2046. using std::ldexp;
  2047. if (a == 0)
  2048. return *this = zero();
  2049. if (a == 1)
  2050. return *this = one();
  2051. if (BOOST_MP_ISINF(a))
  2052. {
  2053. *this = inf();
  2054. if (a < 0)
  2055. this->negate();
  2056. return *this;
  2057. }
  2058. if (BOOST_MP_ISNAN(a))
  2059. return *this = nan();
  2060. int e;
  2061. Float f, term;
  2062. *this = zero();
  2063. f = frexp(a, &e);
  2064. // See https://svn.boost.org/trac/boost/ticket/10924 for an example of why this may go wrong:
  2065. BOOST_MP_ASSERT(!BOOST_MP_ISNAN(f) && !BOOST_MP_ISINF(f));
  2066. constexpr int shift = std::numeric_limits<int>::digits - 1;
  2067. while (f != static_cast<Float>(0.0f))
  2068. {
  2069. // extract int sized bits from f:
  2070. f = ldexp(f, shift);
  2071. BOOST_MP_ASSERT(!BOOST_MP_ISNAN(f) && !BOOST_MP_ISINF(f));
  2072. term = floor(f);
  2073. e -= shift;
  2074. *this *= pow2(shift);
  2075. if (term > 0)
  2076. add_unsigned_long_long(static_cast<unsigned>(term));
  2077. else
  2078. sub_unsigned_long_long(static_cast<unsigned>(-term));
  2079. f -= term;
  2080. }
  2081. if (e != 0)
  2082. *this *= pow2(e);
  2083. return *this;
  2084. }
  2085. template <unsigned Digits10, class ExponentType, class Allocator>
  2086. void cpp_dec_float<Digits10, ExponentType, Allocator>::from_unsigned_long_long(const unsigned long long u)
  2087. {
  2088. std::fill(data.begin(), data.end(), static_cast<std::uint32_t>(0u));
  2089. exp = static_cast<exponent_type>(0);
  2090. neg = false;
  2091. fpclass = cpp_dec_float_finite;
  2092. prec_elem = cpp_dec_float_elem_number;
  2093. if (u == 0)
  2094. {
  2095. return;
  2096. }
  2097. std::size_t index { static_cast<std::size_t>(0u) };
  2098. unsigned long long uu { u };
  2099. constexpr std::size_t
  2100. local_storage_size
  2101. {
  2102. static_cast<std::size_t>
  2103. (
  2104. (std::numeric_limits<unsigned long long>::digits10 / static_cast<int>(cpp_dec_float_elem_digits10)) + 3
  2105. )
  2106. };
  2107. using local_limb_type = std::uint32_t;
  2108. using local_tmp_array_type = std::array<local_limb_type, local_storage_size>;
  2109. local_tmp_array_type temp { };
  2110. while (uu != static_cast<unsigned long long>(0u))
  2111. {
  2112. temp[index] = static_cast<local_limb_type>(uu % static_cast<unsigned long long>(cpp_dec_float_elem_mask));
  2113. uu = static_cast<unsigned long long>(uu / static_cast<unsigned long long>(cpp_dec_float_elem_mask));
  2114. ++index;
  2115. }
  2116. if (index > static_cast<std::size_t>(1u))
  2117. {
  2118. exp += static_cast<exponent_type>((index - 1u) * static_cast<std::size_t>(cpp_dec_float_elem_digits10));
  2119. }
  2120. const std::size_t
  2121. copy_size
  2122. {
  2123. (std::min)(index, static_cast<std::size_t>(cpp_dec_float_elem_number))
  2124. };
  2125. using local_const_reverse_iterator_type = std::reverse_iterator<const local_limb_type*>;
  2126. std::copy
  2127. (
  2128. local_const_reverse_iterator_type(temp.data() + copy_size),
  2129. local_const_reverse_iterator_type(temp.data()),
  2130. data.begin()
  2131. );
  2132. }
  2133. template <unsigned Digits10, class ExponentType, class Allocator>
  2134. template <typename InputIteratorTypeLeft, typename InputIteratorTypeRight>
  2135. int cpp_dec_float<Digits10, ExponentType, Allocator>::compare_ranges(InputIteratorTypeLeft a,
  2136. InputIteratorTypeRight b,
  2137. const std::uint32_t count)
  2138. {
  2139. using local_iterator_left_type = InputIteratorTypeLeft;
  2140. using local_iterator_right_type = InputIteratorTypeRight;
  2141. local_iterator_left_type begin_a(a);
  2142. local_iterator_left_type end_a (a + static_cast<typename std::iterator_traits<local_iterator_left_type >::difference_type>(count));
  2143. local_iterator_right_type begin_b(b);
  2144. local_iterator_right_type end_b (b + static_cast<typename std::iterator_traits<local_iterator_right_type>::difference_type>(count));
  2145. const auto mismatch_pair = std::mismatch(begin_a, end_a, begin_b);
  2146. int n_return;
  2147. if((mismatch_pair.first != end_a) || (mismatch_pair.second != end_b))
  2148. {
  2149. const typename std::iterator_traits<local_iterator_left_type >::value_type left = *mismatch_pair.first;
  2150. const typename std::iterator_traits<local_iterator_right_type>::value_type right = *mismatch_pair.second;
  2151. n_return = ((left > right) ? 1 : -1);
  2152. }
  2153. else
  2154. {
  2155. n_return = 0;
  2156. }
  2157. return n_return;
  2158. }
  2159. template <unsigned Digits10, class ExponentType, class Allocator>
  2160. std::uint32_t cpp_dec_float<Digits10, ExponentType, Allocator>::eval_add_n( std::uint32_t* r,
  2161. const std::uint32_t* u,
  2162. const std::uint32_t* v,
  2163. const std::int32_t count)
  2164. {
  2165. // Addition algorithm
  2166. std::uint_fast8_t carry = static_cast<std::uint_fast8_t>(0U);
  2167. for(std::int32_t j = static_cast<std::int32_t>(count - static_cast<std::int32_t>(1)); j >= static_cast<std::int32_t>(0); --j)
  2168. {
  2169. const std::uint32_t t = static_cast<std::uint32_t>(static_cast<std::uint32_t>(u[j] + v[j]) + carry);
  2170. carry = ((t >= static_cast<std::uint32_t>(cpp_dec_float_elem_mask)) ? static_cast<std::uint_fast8_t>(1U)
  2171. : static_cast<std::uint_fast8_t>(0U));
  2172. r[j] = static_cast<std::uint32_t>(t - ((carry != 0U) ? static_cast<std::uint32_t>(cpp_dec_float_elem_mask)
  2173. : static_cast<std::uint32_t>(0U)));
  2174. }
  2175. return static_cast<std::uint32_t>(carry);
  2176. }
  2177. template <unsigned Digits10, class ExponentType, class Allocator>
  2178. std::uint32_t cpp_dec_float<Digits10, ExponentType, Allocator>::eval_subtract_n( std::uint32_t* r,
  2179. const std::uint32_t* u,
  2180. const std::uint32_t* v,
  2181. const std::int32_t count)
  2182. {
  2183. // Subtraction algorithm
  2184. std::int_fast8_t borrow = static_cast<std::int_fast8_t>(0);
  2185. for(std::uint32_t j = static_cast<std::uint32_t>(count - static_cast<std::int32_t>(1)); static_cast<std::int32_t>(j) >= static_cast<std::int32_t>(0); --j)
  2186. {
  2187. std::int32_t t = static_cast<std::int32_t>( static_cast<std::int32_t>(u[j])
  2188. - static_cast<std::int32_t>(v[j])) - borrow;
  2189. // Underflow? Borrow?
  2190. if(t < 0)
  2191. {
  2192. // Yes, underflow and borrow
  2193. t += static_cast<std::int32_t>(cpp_dec_float_elem_mask);
  2194. borrow = static_cast<std::int_fast8_t>(1);
  2195. }
  2196. else
  2197. {
  2198. borrow = static_cast<std::int_fast8_t>(0);
  2199. }
  2200. r[j] = static_cast<std::uint32_t>(t);
  2201. }
  2202. return static_cast<std::uint32_t>(borrow);
  2203. }
  2204. template <unsigned Digits10, class ExponentType, class Allocator>
  2205. void cpp_dec_float<Digits10, ExponentType, Allocator>::eval_multiply_n_by_n_to_2n( std::uint32_t* r,
  2206. const std::uint32_t* a,
  2207. const std::uint32_t* b,
  2208. const std::uint32_t count)
  2209. {
  2210. using local_limb_type = std::uint32_t;
  2211. using local_double_limb_type = std::uint64_t;
  2212. using local_reverse_iterator_type = std::reverse_iterator<local_limb_type*>;
  2213. local_reverse_iterator_type ir(r + (count * 2));
  2214. local_double_limb_type carry = 0U;
  2215. for(std::int32_t j = static_cast<std::int32_t>(count - 1); j >= static_cast<std::int32_t>(1); --j)
  2216. {
  2217. local_double_limb_type sum = carry;
  2218. for(std::int32_t i = static_cast<std::int32_t>(count - 1); i >= j; --i)
  2219. {
  2220. sum += local_double_limb_type(
  2221. local_double_limb_type(a[i]) * b[ static_cast<std::int32_t>(count - 1)
  2222. - static_cast<std::int32_t>(i - j)]);
  2223. }
  2224. carry = static_cast<local_double_limb_type>(sum / static_cast<local_limb_type> (cpp_dec_float_elem_mask));
  2225. *ir++ = static_cast<local_limb_type> (sum - static_cast<local_double_limb_type>(static_cast<local_double_limb_type>(carry) * static_cast<local_limb_type>(cpp_dec_float_elem_mask)));
  2226. }
  2227. for(std::int32_t j = static_cast<std::int32_t>(count - 1); j >= static_cast<std::int32_t>(0); --j)
  2228. {
  2229. local_double_limb_type sum = carry;
  2230. for(std::int32_t i = j; i >= static_cast<std::int32_t>(0); --i)
  2231. {
  2232. sum += static_cast<local_double_limb_type>(a[j - i] * static_cast<local_double_limb_type>(b[i]));
  2233. }
  2234. carry = static_cast<local_double_limb_type>(sum / static_cast<local_limb_type>(cpp_dec_float_elem_mask));
  2235. *ir++ = static_cast<local_limb_type> (sum - static_cast<local_double_limb_type>(static_cast<local_double_limb_type>(carry) * static_cast<local_limb_type>(cpp_dec_float_elem_mask)));
  2236. }
  2237. *ir = static_cast<local_limb_type>(carry);
  2238. }
  2239. template <unsigned Digits10, class ExponentType, class Allocator>
  2240. std::uint32_t cpp_dec_float<Digits10, ExponentType, Allocator>::mul_loop_n(std::uint32_t* const u, std::uint32_t n, const std::int32_t p)
  2241. {
  2242. std::uint64_t carry = static_cast<std::uint64_t>(0u);
  2243. // Multiplication loop.
  2244. for (std::int32_t j = p - 1; j >= static_cast<std::int32_t>(0); j--)
  2245. {
  2246. const std::uint64_t t = static_cast<std::uint64_t>(carry + static_cast<std::uint64_t>(u[j] * static_cast<std::uint64_t>(n)));
  2247. carry = static_cast<std::uint64_t>(t / static_cast<std::uint32_t>(cpp_dec_float_elem_mask));
  2248. u[j] = static_cast<std::uint32_t>(t - static_cast<std::uint64_t>(static_cast<std::uint32_t>(cpp_dec_float_elem_mask) * static_cast<std::uint64_t>(carry)));
  2249. }
  2250. return static_cast<std::uint32_t>(carry);
  2251. }
  2252. template <unsigned Digits10, class ExponentType, class Allocator>
  2253. std::uint32_t cpp_dec_float<Digits10, ExponentType, Allocator>::div_loop_n(std::uint32_t* const u, std::uint32_t n, const std::int32_t p)
  2254. {
  2255. std::uint64_t prev = static_cast<std::uint64_t>(0u);
  2256. for (std::int32_t j = static_cast<std::int32_t>(0); j < p; j++)
  2257. {
  2258. const std::uint64_t t = static_cast<std::uint64_t>(u[j] + static_cast<std::uint64_t>(prev * static_cast<std::uint32_t>(cpp_dec_float_elem_mask)));
  2259. u[j] = static_cast<std::uint32_t>(t / n);
  2260. prev = static_cast<std::uint64_t>(t - static_cast<std::uint64_t>(n * static_cast<std::uint64_t>(u[j])));
  2261. }
  2262. return static_cast<std::uint32_t>(prev);
  2263. }
  2264. template <unsigned Digits10, class ExponentType, class Allocator>
  2265. void cpp_dec_float<Digits10, ExponentType, Allocator>::eval_multiply_kara_propagate_carry(std::uint32_t* t, const std::uint32_t n, const std::uint32_t carry)
  2266. {
  2267. std::uint_fast8_t carry_out = ((carry != 0U) ? static_cast<std::uint_fast8_t>(1U)
  2268. : static_cast<std::uint_fast8_t>(0U));
  2269. using local_reverse_iterator_type = std::reverse_iterator<std::uint32_t*>;
  2270. local_reverse_iterator_type ri_t (t + n);
  2271. local_reverse_iterator_type rend_t(t);
  2272. while((carry_out != 0U) && (ri_t != rend_t))
  2273. {
  2274. const std::uint64_t tt = *ri_t + carry_out;
  2275. carry_out = ((tt >= static_cast<std::uint32_t>(cpp_dec_float_elem_mask)) ? static_cast<std::uint_fast8_t>(1U)
  2276. : static_cast<std::uint_fast8_t>(0U));
  2277. *ri_t++ = static_cast<std::uint32_t>(tt - ((carry_out != 0U) ? static_cast<std::uint32_t>(cpp_dec_float_elem_mask)
  2278. : static_cast<std::uint32_t>(0U)));
  2279. }
  2280. }
  2281. template <unsigned Digits10, class ExponentType, class Allocator>
  2282. void cpp_dec_float<Digits10, ExponentType, Allocator>::eval_multiply_kara_propagate_borrow(std::uint32_t* t, const std::uint32_t n, const bool has_borrow)
  2283. {
  2284. std::int_fast8_t borrow = (has_borrow ? static_cast<std::int_fast8_t>(1)
  2285. : static_cast<std::int_fast8_t>(0));
  2286. using local_reverse_iterator_type = std::reverse_iterator<std::uint32_t*>;
  2287. local_reverse_iterator_type ri_t (t + n);
  2288. local_reverse_iterator_type rend_t(t);
  2289. while((borrow != 0U) && (ri_t != rend_t))
  2290. {
  2291. std::int32_t tt = static_cast<std::int32_t>(static_cast<std::int32_t>(*ri_t) - borrow);
  2292. // Underflow? Borrow?
  2293. if(tt < 0)
  2294. {
  2295. // Yes, underflow and borrow
  2296. tt += static_cast<std::int32_t>(cpp_dec_float_elem_mask);
  2297. borrow = static_cast<int_fast8_t>(1);
  2298. }
  2299. else
  2300. {
  2301. borrow = static_cast<int_fast8_t>(0);
  2302. }
  2303. *ri_t++ = static_cast<std::uint32_t>(tt);
  2304. }
  2305. }
  2306. template <unsigned Digits10, class ExponentType, class Allocator>
  2307. void cpp_dec_float<Digits10, ExponentType, Allocator>::eval_multiply_kara_n_by_n_to_2n( std::uint32_t* r,
  2308. const std::uint32_t* a,
  2309. const std::uint32_t* b,
  2310. const std::uint32_t n,
  2311. std::uint32_t* t)
  2312. {
  2313. if(n <= 32U)
  2314. {
  2315. static_cast<void>(t);
  2316. eval_multiply_n_by_n_to_2n(r, a, b, n);
  2317. }
  2318. else
  2319. {
  2320. // Based on "Algorithm 1.3 KaratsubaMultiply", Sect. 1.3.2, page 5
  2321. // of R.P. Brent and P. Zimmermann, "Modern Computer Arithmetic",
  2322. // Cambridge University Press (2011).
  2323. // The Karatsuba multipliation computes the product of a*b as:
  2324. // [b^N + b^(N/2)] a1*b1 + [b^(N/2)](a1 - a0)(b0 - b1) + [b^(N/2) + 1] a0*b0
  2325. // Here we visualize a and b in two components 1,0 corresponding
  2326. // to the high and low order parts, respectively.
  2327. // Step 1
  2328. // Calculate a1*b1 and store it in the upper-order part of r.
  2329. // Calculate a0*b0 and store it in the lower-order part of r.
  2330. // copy r to t0.
  2331. // Step 2
  2332. // Add a1*b1 (which is t2) to the middle two-quarters of r (which is r1)
  2333. // Add a0*b0 (which is t0) to the middle two-quarters of r (which is r1)
  2334. // Step 3
  2335. // Calculate |a1-a0| in t0 and note the sign (i.e., the borrow flag)
  2336. // Step 4
  2337. // Calculate |b0-b1| in t1 and note the sign (i.e., the borrow flag)
  2338. // Step 5
  2339. // Call kara mul to calculate |a1-a0|*|b0-b1| in (t2),
  2340. // while using temporary storage in t4 along the way.
  2341. // Step 6
  2342. // Check the borrow signs. If a1-a0 and b0-b1 have the same signs,
  2343. // then add |a1-a0|*|b0-b1| to r1, otherwise subtract it from r1.
  2344. const std::uint_fast32_t nh = n / 2U;
  2345. const std::uint32_t* a0 = a + nh;
  2346. const std::uint32_t* a1 = a + 0U;
  2347. const std::uint32_t* b0 = b + nh;
  2348. const std::uint32_t* b1 = b + 0U;
  2349. std::uint32_t* r0 = r + 0U;
  2350. std::uint32_t* r1 = r + nh;
  2351. std::uint32_t* r2 = r + n;
  2352. std::uint32_t* t0 = t + 0U;
  2353. std::uint32_t* t1 = t + nh;
  2354. std::uint32_t* t2 = t + n;
  2355. std::uint32_t* t4 = t + (n + n);
  2356. // Step 1
  2357. eval_multiply_kara_n_by_n_to_2n(r0, a1, b1, static_cast<std::uint32_t>(nh), t);
  2358. eval_multiply_kara_n_by_n_to_2n(r2, a0, b0, static_cast<std::uint32_t>(nh), t);
  2359. std::copy(r0, r0 + (2U * n), t0);
  2360. // Step 2
  2361. std::uint32_t carry;
  2362. carry = eval_add_n(r1, r1, t0, static_cast<std::int32_t>(n));
  2363. eval_multiply_kara_propagate_carry(r0, static_cast<std::uint32_t>(nh), carry);
  2364. carry = eval_add_n(r1, r1, t2, static_cast<std::int32_t>(n));
  2365. eval_multiply_kara_propagate_carry(r0, static_cast<std::uint32_t>(nh), carry);
  2366. // Step 3
  2367. const int cmp_result_a1a0 = compare_ranges(a1, a0, static_cast<std::uint32_t>(nh));
  2368. if(cmp_result_a1a0 == 1)
  2369. static_cast<void>(eval_subtract_n(t0, a1, a0, static_cast<std::int32_t>(nh)));
  2370. else if(cmp_result_a1a0 == -1)
  2371. static_cast<void>(eval_subtract_n(t0, a0, a1, static_cast<std::int32_t>(nh)));
  2372. // Step 4
  2373. const int cmp_result_b0b1 = compare_ranges(b0, b1, static_cast<std::uint32_t>(nh));
  2374. if(cmp_result_b0b1 == 1)
  2375. static_cast<void>(eval_subtract_n(t1, b0, b1, static_cast<std::int32_t>(nh)));
  2376. else if(cmp_result_b0b1 == -1)
  2377. static_cast<void>(eval_subtract_n(t1, b1, b0, static_cast<std::int32_t>(nh)));
  2378. // Step 5
  2379. eval_multiply_kara_n_by_n_to_2n(t2, t0, t1, static_cast<std::uint32_t>(nh), t4);
  2380. // Step 6
  2381. if((cmp_result_a1a0 * cmp_result_b0b1) == 1)
  2382. {
  2383. carry = eval_add_n(r1, r1, t2, static_cast<std::int32_t>(n));
  2384. eval_multiply_kara_propagate_carry(r0, static_cast<std::uint32_t>(nh), carry);
  2385. }
  2386. else if((cmp_result_a1a0 * cmp_result_b0b1) == -1)
  2387. {
  2388. const bool has_borrow = eval_subtract_n(r1, r1, t2, static_cast<std::int32_t>(n));
  2389. eval_multiply_kara_propagate_borrow(r0, static_cast<std::uint32_t>(nh), has_borrow);
  2390. }
  2391. }
  2392. }
  2393. template <unsigned Digits10, class ExponentType, class Allocator>
  2394. cpp_dec_float<Digits10, ExponentType, Allocator> cpp_dec_float<Digits10, ExponentType, Allocator>::pow2(const long long p)
  2395. {
  2396. static const std::array<cpp_dec_float<Digits10, ExponentType, Allocator>, 256u> local_pow2_data =
  2397. {{
  2398. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 29u, 38735877u, 5571876u, 99218413u, 43055614u, 19454666u, 38919302u, 18803771u, 87926569u, 60431486u, 36817932u, 12890625u }, -40 ),
  2399. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 58u, 77471754u, 11143753u, 98436826u, 86111228u, 38909332u, 77838604u, 37607543u, 75853139u, 20862972u, 73635864u, 25781250u }, -40 ),
  2400. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 117u, 54943508u, 22287507u, 96873653u, 72222456u, 77818665u, 55677208u, 75215087u, 51706278u, 41725945u, 47271728u, 51562500u }, -40 ),
  2401. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 235u, 9887016u, 44575015u, 93747307u, 44444913u, 55637331u, 11354417u, 50430175u, 3412556u, 83451890u, 94543457u, 3125000u }, -40 ),
  2402. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 470u, 19774032u, 89150031u, 87494614u, 88889827u, 11274662u, 22708835u, 860350u, 6825113u, 66903781u, 89086914u, 6250000u }, -40 ),
  2403. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 940u, 39548065u, 78300063u, 74989229u, 77779654u, 22549324u, 45417670u, 1720700u, 13650227u, 33807563u, 78173828u, 12500000u }, -40 ),
  2404. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1880u, 79096131u, 56600127u, 49978459u, 55559308u, 45098648u, 90835340u, 3441400u, 27300454u, 67615127u, 56347656u, 25000000u }, -40 ),
  2405. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 3761u, 58192263u, 13200254u, 99956919u, 11118616u, 90197297u, 81670680u, 6882800u, 54600909u, 35230255u, 12695312u, 50000000u }, -40 ),
  2406. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 7523u, 16384526u, 26400509u, 99913838u, 22237233u, 80394595u, 63341360u, 13765601u, 9201818u, 70460510u, 25390625u }, -40 ),
  2407. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 15046u, 32769052u, 52801019u, 99827676u, 44474467u, 60789191u, 26682720u, 27531202u, 18403637u, 40921020u, 50781250u }, -40 ),
  2408. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 30092u, 65538105u, 5602039u, 99655352u, 88948935u, 21578382u, 53365440u, 55062404u, 36807274u, 81842041u, 1562500u }, -40 ),
  2409. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 60185u, 31076210u, 11204079u, 99310705u, 77897870u, 43156765u, 6730881u, 10124808u, 73614549u, 63684082u, 3125000u }, -40 ),
  2410. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 120370u, 62152420u, 22408159u, 98621411u, 55795740u, 86313530u, 13461762u, 20249617u, 47229099u, 27368164u, 6250000u }, -40 ),
  2411. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 240741u, 24304840u, 44816319u, 97242823u, 11591481u, 72627060u, 26923524u, 40499234u, 94458198u, 54736328u, 12500000u }, -40 ),
  2412. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 481482u, 48609680u, 89632639u, 94485646u, 23182963u, 45254120u, 53847048u, 80998469u, 88916397u, 9472656u, 25000000u }, -40 ),
  2413. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 962964u, 97219361u, 79265279u, 88971292u, 46365926u, 90508241u, 7694097u, 61996939u, 77832794u, 18945312u, 50000000u }, -40 ),
  2414. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1925929u, 94438723u, 58530559u, 77942584u, 92731853u, 81016482u, 15388195u, 23993879u, 55665588u, 37890625u }, -40 ),
  2415. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 3851859u, 88877447u, 17061119u, 55885169u, 85463707u, 62032964u, 30776390u, 47987759u, 11331176u, 75781250u }, -40 ),
  2416. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 7703719u, 77754894u, 34122239u, 11770339u, 70927415u, 24065928u, 61552780u, 95975518u, 22662353u, 51562500u }, -40 ),
  2417. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 15407439u, 55509788u, 68244478u, 23540679u, 41854830u, 48131857u, 23105561u, 91951036u, 45324707u, 3125000u }, -40 ),
  2418. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 30814879u, 11019577u, 36488956u, 47081358u, 83709660u, 96263714u, 46211123u, 83902072u, 90649414u, 6250000u }, -40 ),
  2419. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 61629758u, 22039154u, 72977912u, 94162717u, 67419321u, 92527428u, 92422247u, 67804145u, 81298828u, 12500000u }, -40 ),
  2420. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1u, 23259516u, 44078309u, 45955825u, 88325435u, 34838643u, 85054857u, 84844495u, 35608291u, 62597656u, 25000000u }, -32 ),
  2421. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 2u, 46519032u, 88156618u, 91911651u, 76650870u, 69677287u, 70109715u, 69688990u, 71216583u, 25195312u, 50000000u }, -32 ),
  2422. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 4u, 93038065u, 76313237u, 83823303u, 53301741u, 39354575u, 40219431u, 39377981u, 42433166u, 50390625u }, -32 ),
  2423. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 9u, 86076131u, 52626475u, 67646607u, 6603482u, 78709150u, 80438862u, 78755962u, 84866333u, 781250u }, -32 ),
  2424. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 19u, 72152263u, 5252951u, 35293214u, 13206965u, 57418301u, 60877725u, 57511925u, 69732666u, 1562500u }, -32 ),
  2425. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 39u, 44304526u, 10505902u, 70586428u, 26413931u, 14836603u, 21755451u, 15023851u, 39465332u, 3125000u }, -32 ),
  2426. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 78u, 88609052u, 21011805u, 41172856u, 52827862u, 29673206u, 43510902u, 30047702u, 78930664u, 6250000u }, -32 ),
  2427. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 157u, 77218104u, 42023610u, 82345713u, 5655724u, 59346412u, 87021804u, 60095405u, 57861328u, 12500000u }, -32 ),
  2428. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 315u, 54436208u, 84047221u, 64691426u, 11311449u, 18692825u, 74043609u, 20190811u, 15722656u, 25000000u }, -32 ),
  2429. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 631u, 8872417u, 68094443u, 29382852u, 22622898u, 37385651u, 48087218u, 40381622u, 31445312u, 50000000u }, -32 ),
  2430. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1262u, 17744835u, 36188886u, 58765704u, 45245796u, 74771302u, 96174436u, 80763244u, 62890625u }, -32 ),
  2431. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 2524u, 35489670u, 72377773u, 17531408u, 90491593u, 49542605u, 92348873u, 61526489u, 25781250u }, -32 ),
  2432. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 5048u, 70979341u, 44755546u, 35062817u, 80983186u, 99085211u, 84697747u, 23052978u, 51562500u }, -32 ),
  2433. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 10097u, 41958682u, 89511092u, 70125635u, 61966373u, 98170423u, 69395494u, 46105957u, 3125000u }, -32 ),
  2434. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 20194u, 83917365u, 79022185u, 40251271u, 23932747u, 96340847u, 38790988u, 92211914u, 6250000u }, -32 ),
  2435. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 40389u, 67834731u, 58044370u, 80502542u, 47865495u, 92681694u, 77581977u, 84423828u, 12500000u }, -32 ),
  2436. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 80779u, 35669463u, 16088741u, 61005084u, 95730991u, 85363389u, 55163955u, 68847656u, 25000000u }, -32 ),
  2437. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 161558u, 71338926u, 32177483u, 22010169u, 91461983u, 70726779u, 10327911u, 37695312u, 50000000u }, -32 ),
  2438. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 323117u, 42677852u, 64354966u, 44020339u, 82923967u, 41453558u, 20655822u, 75390625u }, -32 ),
  2439. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 646234u, 85355705u, 28709932u, 88040679u, 65847934u, 82907116u, 41311645u, 50781250u }, -32 ),
  2440. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1292469u, 70711410u, 57419865u, 76081359u, 31695869u, 65814232u, 82623291u, 1562500u }, -32 ),
  2441. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 2584939u, 41422821u, 14839731u, 52162718u, 63391739u, 31628465u, 65246582u, 3125000u }, -32 ),
  2442. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 5169878u, 82845642u, 29679463u, 4325437u, 26783478u, 63256931u, 30493164u, 6250000u }, -32 ),
  2443. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 10339757u, 65691284u, 59358926u, 8650874u, 53566957u, 26513862u, 60986328u, 12500000u }, -32 ),
  2444. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 20679515u, 31382569u, 18717852u, 17301749u, 7133914u, 53027725u, 21972656u, 25000000u }, -32 ),
  2445. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 41359030u, 62765138u, 37435704u, 34603498u, 14267829u, 6055450u, 43945312u, 50000000u }, -32 ),
  2446. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 82718061u, 25530276u, 74871408u, 69206996u, 28535658u, 12110900u, 87890625u }, -32 ),
  2447. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1u, 65436122u, 51060553u, 49742817u, 38413992u, 57071316u, 24221801u, 75781250u }, -24 ),
  2448. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 3u, 30872245u, 2121106u, 99485634u, 76827985u, 14142632u, 48443603u, 51562500u }, -24 ),
  2449. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 6u, 61744490u, 4242213u, 98971269u, 53655970u, 28285264u, 96887207u, 3125000u }, -24 ),
  2450. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 13u, 23488980u, 8484427u, 97942539u, 7311940u, 56570529u, 93774414u, 6250000u }, -24 ),
  2451. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 26u, 46977960u, 16968855u, 95885078u, 14623881u, 13141059u, 87548828u, 12500000u }, -24 ),
  2452. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 52u, 93955920u, 33937711u, 91770156u, 29247762u, 26282119u, 75097656u, 25000000u }, -24 ),
  2453. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 105u, 87911840u, 67875423u, 83540312u, 58495524u, 52564239u, 50195312u, 50000000u }, -24 ),
  2454. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 211u, 75823681u, 35750847u, 67080625u, 16991049u, 5128479u, 390625u }, -24 ),
  2455. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 423u, 51647362u, 71501695u, 34161250u, 33982098u, 10256958u, 781250u }, -24 ),
  2456. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 847u, 3294725u, 43003390u, 68322500u, 67964196u, 20513916u, 1562500u }, -24 ),
  2457. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1694u, 6589450u, 86006781u, 36645001u, 35928392u, 41027832u, 3125000u }, -24 ),
  2458. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 3388u, 13178901u, 72013562u, 73290002u, 71856784u, 82055664u, 6250000u }, -24 ),
  2459. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 6776u, 26357803u, 44027125u, 46580005u, 43713569u, 64111328u, 12500000u }, -24 ),
  2460. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 13552u, 52715606u, 88054250u, 93160010u, 87427139u, 28222656u, 25000000u }, -24 ),
  2461. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 27105u, 5431213u, 76108501u, 86320021u, 74854278u, 56445312u, 50000000u }, -24 ),
  2462. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 54210u, 10862427u, 52217003u, 72640043u, 49708557u, 12890625u }, -24 ),
  2463. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 108420u, 21724855u, 4434007u, 45280086u, 99417114u, 25781250u }, -24 ),
  2464. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 216840u, 43449710u, 8868014u, 90560173u, 98834228u, 51562500u }, -24 ),
  2465. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 433680u, 86899420u, 17736029u, 81120347u, 97668457u, 3125000u }, -24 ),
  2466. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 867361u, 73798840u, 35472059u, 62240695u, 95336914u, 6250000u }, -24 ),
  2467. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1734723u, 47597680u, 70944119u, 24481391u, 90673828u, 12500000u }, -24 ),
  2468. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 3469446u, 95195361u, 41888238u, 48962783u, 81347656u, 25000000u }, -24 ),
  2469. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 6938893u, 90390722u, 83776476u, 97925567u, 62695312u, 50000000u }, -24 ),
  2470. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 13877787u, 80781445u, 67552953u, 95851135u, 25390625u }, -24 ),
  2471. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 27755575u, 61562891u, 35105907u, 91702270u, 50781250u }, -24 ),
  2472. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 55511151u, 23125782u, 70211815u, 83404541u, 1562500u }, -24 ),
  2473. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1u, 11022302u, 46251565u, 40423631u, 66809082u, 3125000u }, -16 ),
  2474. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 2u, 22044604u, 92503130u, 80847263u, 33618164u, 6250000u }, -16 ),
  2475. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 4u, 44089209u, 85006261u, 61694526u, 67236328u, 12500000u }, -16 ),
  2476. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 8u, 88178419u, 70012523u, 23389053u, 34472656u, 25000000u }, -16 ),
  2477. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 17u, 76356839u, 40025046u, 46778106u, 68945312u, 50000000u }, -16 ),
  2478. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 35u, 52713678u, 80050092u, 93556213u, 37890625u }, -16 ),
  2479. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 71u, 5427357u, 60100185u, 87112426u, 75781250u }, -16 ),
  2480. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 142u, 10854715u, 20200371u, 74224853u, 51562500u }, -16 ),
  2481. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 284u, 21709430u, 40400743u, 48449707u, 3125000u }, -16 ),
  2482. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 568u, 43418860u, 80801486u, 96899414u, 6250000u }, -16 ),
  2483. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1136u, 86837721u, 61602973u, 93798828u, 12500000u }, -16 ),
  2484. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 2273u, 73675443u, 23205947u, 87597656u, 25000000u }, -16 ),
  2485. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 4547u, 47350886u, 46411895u, 75195312u, 50000000u }, -16 ),
  2486. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 9094u, 94701772u, 92823791u, 50390625u }, -16 ),
  2487. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 18189u, 89403545u, 85647583u, 781250u }, -16 ),
  2488. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 36379u, 78807091u, 71295166u, 1562500u }, -16 ),
  2489. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 72759u, 57614183u, 42590332u, 3125000u }, -16 ),
  2490. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 145519u, 15228366u, 85180664u, 6250000u }, -16 ),
  2491. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 291038u, 30456733u, 70361328u, 12500000u }, -16 ),
  2492. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 582076u, 60913467u, 40722656u, 25000000u }, -16 ),
  2493. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1164153u, 21826934u, 81445312u, 50000000u }, -16 ),
  2494. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 2328306u, 43653869u, 62890625u }, -16 ),
  2495. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 4656612u, 87307739u, 25781250u }, -16 ),
  2496. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 9313225u, 74615478u, 51562500u }, -16 ),
  2497. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 18626451u, 49230957u, 3125000u }, -16 ),
  2498. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 37252902u, 98461914u, 6250000u }, -16 ),
  2499. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 74505805u, 96923828u, 12500000u }, -16 ),
  2500. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1u, 49011611u, 93847656u, 25000000u }, -8 ),
  2501. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 2u, 98023223u, 87695312u, 50000000u }, -8 ),
  2502. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 5u, 96046447u, 75390625u }, -8 ),
  2503. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 11u, 92092895u, 50781250u }, -8 ),
  2504. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 23u, 84185791u, 1562500u }, -8 ),
  2505. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 47u, 68371582u, 3125000u }, -8 ),
  2506. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 95u, 36743164u, 6250000u }, -8 ),
  2507. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 190u, 73486328u, 12500000u }, -8 ),
  2508. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 381u, 46972656u, 25000000u }, -8 ),
  2509. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 762u, 93945312u, 50000000u }, -8 ),
  2510. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1525u, 87890625u }, -8 ),
  2511. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 3051u, 75781250u }, -8 ),
  2512. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 6103u, 51562500u }, -8 ),
  2513. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 12207u, 3125000u }, -8 ),
  2514. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 24414u, 6250000u }, -8 ),
  2515. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 48828u, 12500000u }, -8 ),
  2516. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 97656u, 25000000u }, -8 ),
  2517. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 195312u, 50000000u }, -8 ),
  2518. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 390625u }, -8 ),
  2519. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 781250u }, -8 ),
  2520. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1562500u }, -8 ),
  2521. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 3125000u }, -8 ),
  2522. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 6250000u }, -8 ),
  2523. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 12500000u }, -8 ),
  2524. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 25000000u }, -8 ),
  2525. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 50000000u }, -8 ),
  2526. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1u }, 0 ),
  2527. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 2u }, 0 ),
  2528. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 4u }, 0 ),
  2529. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 8u }, 0 ),
  2530. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 16u }, 0 ),
  2531. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 32u }, 0 ),
  2532. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 64u }, 0 ),
  2533. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 128u }, 0 ),
  2534. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 256u }, 0 ),
  2535. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 512u }, 0 ),
  2536. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1024u }, 0 ),
  2537. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 2048u }, 0 ),
  2538. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 4096u }, 0 ),
  2539. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 8192u }, 0 ),
  2540. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 16384u }, 0 ),
  2541. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 32768u }, 0 ),
  2542. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 65536u }, 0 ),
  2543. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 131072u }, 0 ),
  2544. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 262144u }, 0 ),
  2545. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 524288u }, 0 ),
  2546. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1048576u }, 0 ),
  2547. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 2097152u }, 0 ),
  2548. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 4194304u }, 0 ),
  2549. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 8388608u }, 0 ),
  2550. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 16777216u }, 0 ),
  2551. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 33554432u }, 0 ),
  2552. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 67108864u }, 0 ),
  2553. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1u, 34217728u }, 8 ),
  2554. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 2u, 68435456u }, 8 ),
  2555. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 5u, 36870912u }, 8 ),
  2556. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 10u, 73741824u }, 8 ),
  2557. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 21u, 47483648u }, 8 ),
  2558. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 42u, 94967296u }, 8 ),
  2559. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 85u, 89934592u }, 8 ),
  2560. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 171u, 79869184u }, 8 ),
  2561. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 343u, 59738368u }, 8 ),
  2562. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 687u, 19476736u }, 8 ),
  2563. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1374u, 38953472u }, 8 ),
  2564. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 2748u, 77906944u }, 8 ),
  2565. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 5497u, 55813888u }, 8 ),
  2566. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 10995u, 11627776u }, 8 ),
  2567. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 21990u, 23255552u }, 8 ),
  2568. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 43980u, 46511104u }, 8 ),
  2569. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 87960u, 93022208u }, 8 ),
  2570. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 175921u, 86044416u }, 8 ),
  2571. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 351843u, 72088832u }, 8 ),
  2572. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 703687u, 44177664u }, 8 ),
  2573. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1407374u, 88355328u }, 8 ),
  2574. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 2814749u, 76710656u }, 8 ),
  2575. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 5629499u, 53421312u }, 8 ),
  2576. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 11258999u, 6842624u }, 8 ),
  2577. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 22517998u, 13685248u }, 8 ),
  2578. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 45035996u, 27370496u }, 8 ),
  2579. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 90071992u, 54740992u }, 8 ),
  2580. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1u, 80143985u, 9481984u }, 16 ),
  2581. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 3u, 60287970u, 18963968u }, 16 ),
  2582. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 7u, 20575940u, 37927936u }, 16 ),
  2583. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 14u, 41151880u, 75855872u }, 16 ),
  2584. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 28u, 82303761u, 51711744u }, 16 ),
  2585. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 57u, 64607523u, 3423488u }, 16 ),
  2586. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 115u, 29215046u, 6846976u }, 16 ),
  2587. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 230u, 58430092u, 13693952u }, 16 ),
  2588. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 461u, 16860184u, 27387904u }, 16 ),
  2589. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 922u, 33720368u, 54775808u }, 16 ),
  2590. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1844u, 67440737u, 9551616u }, 16 ),
  2591. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 3689u, 34881474u, 19103232u }, 16 ),
  2592. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 7378u, 69762948u, 38206464u }, 16 ),
  2593. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 14757u, 39525896u, 76412928u }, 16 ),
  2594. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 29514u, 79051793u, 52825856u }, 16 ),
  2595. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 59029u, 58103587u, 5651712u }, 16 ),
  2596. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 118059u, 16207174u, 11303424u }, 16 ),
  2597. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 236118u, 32414348u, 22606848u }, 16 ),
  2598. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 472236u, 64828696u, 45213696u }, 16 ),
  2599. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 944473u, 29657392u, 90427392u }, 16 ),
  2600. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1888946u, 59314785u, 80854784u }, 16 ),
  2601. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 3777893u, 18629571u, 61709568u }, 16 ),
  2602. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 7555786u, 37259143u, 23419136u }, 16 ),
  2603. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 15111572u, 74518286u, 46838272u }, 16 ),
  2604. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 30223145u, 49036572u, 93676544u }, 16 ),
  2605. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 60446290u, 98073145u, 87353088u }, 16 ),
  2606. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1u, 20892581u, 96146291u, 74706176u }, 24 ),
  2607. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 2u, 41785163u, 92292583u, 49412352u }, 24 ),
  2608. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 4u, 83570327u, 84585166u, 98824704u }, 24 ),
  2609. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 9u, 67140655u, 69170333u, 97649408u }, 24 ),
  2610. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 19u, 34281311u, 38340667u, 95298816u }, 24 ),
  2611. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 38u, 68562622u, 76681335u, 90597632u }, 24 ),
  2612. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 77u, 37125245u, 53362671u, 81195264u }, 24 ),
  2613. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 154u, 74250491u, 6725343u, 62390528u }, 24 ),
  2614. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 309u, 48500982u, 13450687u, 24781056u }, 24 ),
  2615. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 618u, 97001964u, 26901374u, 49562112u }, 24 ),
  2616. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1237u, 94003928u, 53802748u, 99124224u }, 24 ),
  2617. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 2475u, 88007857u, 7605497u, 98248448u }, 24 ),
  2618. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 4951u, 76015714u, 15210995u, 96496896u }, 24 ),
  2619. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 9903u, 52031428u, 30421991u, 92993792u }, 24 ),
  2620. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 19807u, 4062856u, 60843983u, 85987584u }, 24 ),
  2621. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 39614u, 8125713u, 21687967u, 71975168u }, 24 ),
  2622. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 79228u, 16251426u, 43375935u, 43950336u }, 24 ),
  2623. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 158456u, 32502852u, 86751870u, 87900672u }, 24 ),
  2624. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 316912u, 65005705u, 73503741u, 75801344u }, 24 ),
  2625. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 633825u, 30011411u, 47007483u, 51602688u }, 24 ),
  2626. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1267650u, 60022822u, 94014967u, 3205376u }, 24 ),
  2627. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 2535301u, 20045645u, 88029934u, 6410752u }, 24 ),
  2628. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 5070602u, 40091291u, 76059868u, 12821504u }, 24 ),
  2629. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 10141204u, 80182583u, 52119736u, 25643008u }, 24 ),
  2630. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 20282409u, 60365167u, 4239472u, 51286016u }, 24 ),
  2631. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 40564819u, 20730334u, 8478945u, 2572032u }, 24 ),
  2632. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 81129638u, 41460668u, 16957890u, 5144064u }, 24 ),
  2633. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1u, 62259276u, 82921336u, 33915780u, 10288128u }, 32 ),
  2634. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 3u, 24518553u, 65842672u, 67831560u, 20576256u }, 32 ),
  2635. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 6u, 49037107u, 31685345u, 35663120u, 41152512u }, 32 ),
  2636. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 12u, 98074214u, 63370690u, 71326240u, 82305024u }, 32 ),
  2637. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 25u, 96148429u, 26741381u, 42652481u, 64610048u }, 32 ),
  2638. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 51u, 92296858u, 53482762u, 85304963u, 29220096u }, 32 ),
  2639. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 103u, 84593717u, 6965525u, 70609926u, 58440192u }, 32 ),
  2640. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 207u, 69187434u, 13931051u, 41219853u, 16880384u }, 32 ),
  2641. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 415u, 38374868u, 27862102u, 82439706u, 33760768u }, 32 ),
  2642. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 830u, 76749736u, 55724205u, 64879412u, 67521536u }, 32 ),
  2643. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1661u, 53499473u, 11448411u, 29758825u, 35043072u }, 32 ),
  2644. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 3323u, 6998946u, 22896822u, 59517650u, 70086144u }, 32 ),
  2645. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 6646u, 13997892u, 45793645u, 19035301u, 40172288u }, 32 ),
  2646. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 13292u, 27995784u, 91587290u, 38070602u, 80344576u }, 32 ),
  2647. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 26584u, 55991569u, 83174580u, 76141205u, 60689152u }, 32 ),
  2648. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 53169u, 11983139u, 66349161u, 52282411u, 21378304u }, 32 ),
  2649. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 106338u, 23966279u, 32698323u, 4564822u, 42756608u }, 32 ),
  2650. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 212676u, 47932558u, 65396646u, 9129644u, 85513216u }, 32 ),
  2651. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 425352u, 95865117u, 30793292u, 18259289u, 71026432u }, 32 ),
  2652. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 850705u, 91730234u, 61586584u, 36518579u, 42052864u }, 32 ),
  2653. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1701411u, 83460469u, 23173168u, 73037158u, 84105728u }, 32 ),
  2654. }};
  2655. cpp_dec_float<Digits10, ExponentType, Allocator> t;
  2656. if(p < -128L)
  2657. default_ops::detail::pow_imp(t, cpp_dec_float<Digits10, ExponentType, Allocator>::half(), static_cast<unsigned long long>(-p), std::integral_constant<bool, false>());
  2658. else if ((p >= -128L) && (p <= 127L))
  2659. t = local_pow2_data[std::size_t(p + 128)];
  2660. else
  2661. default_ops::detail::pow_imp(t, cpp_dec_float<Digits10, ExponentType, Allocator>::two(), static_cast<unsigned long long>(p), std::integral_constant<bool, false>());
  2662. return t;
  2663. } // LCOV_EXCL_LINE This causes a false negative on lcov coverage test.
  2664. template <unsigned Digits10, class ExponentType, class Allocator>
  2665. inline void eval_add(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& o)
  2666. {
  2667. result += o;
  2668. }
  2669. template <unsigned Digits10, class ExponentType, class Allocator>
  2670. inline void eval_add(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& a, const cpp_dec_float<Digits10, ExponentType, Allocator>& b)
  2671. {
  2672. result = cpp_dec_float<Digits10, ExponentType, Allocator>(a) += b;
  2673. }
  2674. template <unsigned Digits10, class ExponentType, class Allocator>
  2675. inline void eval_subtract(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& o)
  2676. {
  2677. result -= o;
  2678. }
  2679. template <unsigned Digits10, class ExponentType, class Allocator>
  2680. inline void eval_subtract(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& a, const cpp_dec_float<Digits10, ExponentType, Allocator>& b)
  2681. {
  2682. result = cpp_dec_float<Digits10, ExponentType, Allocator>(a) -= b;
  2683. }
  2684. template <unsigned Digits10, class ExponentType, class Allocator>
  2685. inline void eval_multiply(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& o)
  2686. {
  2687. result *= o;
  2688. }
  2689. template <unsigned Digits10, class ExponentType, class Allocator>
  2690. inline void eval_multiply(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& a, const cpp_dec_float<Digits10, ExponentType, Allocator>& b)
  2691. {
  2692. result = cpp_dec_float<Digits10, ExponentType, Allocator>(a) *= b;
  2693. }
  2694. template <unsigned Digits10, class ExponentType, class Allocator>
  2695. inline void eval_divide(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& o)
  2696. {
  2697. result /= o;
  2698. }
  2699. template <unsigned Digits10, class ExponentType, class Allocator>
  2700. inline void eval_divide(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& a, const cpp_dec_float<Digits10, ExponentType, Allocator>& b)
  2701. {
  2702. result = cpp_dec_float<Digits10, ExponentType, Allocator>(a) /= b;
  2703. }
  2704. template <unsigned Digits10, class ExponentType, class Allocator>
  2705. inline void eval_add(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const unsigned long long& o)
  2706. {
  2707. result.add_unsigned_long_long(o);
  2708. }
  2709. template <unsigned Digits10, class ExponentType, class Allocator>
  2710. inline void eval_subtract(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const unsigned long long& o)
  2711. {
  2712. result.sub_unsigned_long_long(o);
  2713. }
  2714. template <unsigned Digits10, class ExponentType, class Allocator>
  2715. inline void eval_multiply(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const unsigned long long& o)
  2716. {
  2717. result.mul_unsigned_long_long(o);
  2718. }
  2719. template <unsigned Digits10, class ExponentType, class Allocator>
  2720. inline void eval_divide(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const unsigned long long& o)
  2721. {
  2722. result.div_unsigned_long_long(o);
  2723. }
  2724. template <unsigned Digits10, class ExponentType, class Allocator>
  2725. inline void eval_add(cpp_dec_float<Digits10, ExponentType, Allocator>& result, long long o)
  2726. {
  2727. if (o < 0)
  2728. result.sub_unsigned_long_long(boost::multiprecision::detail::unsigned_abs(o));
  2729. else
  2730. {
  2731. using local_ulonglong_type = typename boost::multiprecision::detail::make_unsigned<long long>::type;
  2732. result.add_unsigned_long_long(static_cast<local_ulonglong_type>(o));
  2733. }
  2734. }
  2735. template <unsigned Digits10, class ExponentType, class Allocator>
  2736. inline void eval_subtract(cpp_dec_float<Digits10, ExponentType, Allocator>& result, long long o)
  2737. {
  2738. if (o < 0)
  2739. result.add_unsigned_long_long(boost::multiprecision::detail::unsigned_abs(o));
  2740. else
  2741. {
  2742. using local_ulonglong_type = typename boost::multiprecision::detail::make_unsigned<long long>::type;
  2743. result.sub_unsigned_long_long(static_cast<local_ulonglong_type>(o));
  2744. }
  2745. }
  2746. template <unsigned Digits10, class ExponentType, class Allocator>
  2747. inline void eval_multiply(cpp_dec_float<Digits10, ExponentType, Allocator>& result, long long o)
  2748. {
  2749. if (o < 0)
  2750. {
  2751. result.mul_unsigned_long_long(boost::multiprecision::detail::unsigned_abs(o));
  2752. result.negate();
  2753. }
  2754. else
  2755. {
  2756. using local_ulonglong_type = typename boost::multiprecision::detail::make_unsigned<long long>::type;
  2757. result.mul_unsigned_long_long(static_cast<local_ulonglong_type>(o));
  2758. }
  2759. }
  2760. template <unsigned Digits10, class ExponentType, class Allocator>
  2761. inline void eval_divide(cpp_dec_float<Digits10, ExponentType, Allocator>& result, long long o)
  2762. {
  2763. if (o < 0)
  2764. {
  2765. result.div_unsigned_long_long(boost::multiprecision::detail::unsigned_abs(o));
  2766. result.negate();
  2767. }
  2768. else
  2769. {
  2770. using local_ulonglong_type = typename boost::multiprecision::detail::make_unsigned<long long>::type;
  2771. result.div_unsigned_long_long(static_cast<local_ulonglong_type>(o));
  2772. }
  2773. }
  2774. template <unsigned Digits10, class ExponentType, class Allocator>
  2775. inline void eval_convert_to(unsigned long long* result, const cpp_dec_float<Digits10, ExponentType, Allocator>& val)
  2776. {
  2777. *result = val.extract_unsigned_long_long();
  2778. }
  2779. template <unsigned Digits10, class ExponentType, class Allocator>
  2780. inline void eval_convert_to(long long* result, const cpp_dec_float<Digits10, ExponentType, Allocator>& val)
  2781. {
  2782. *result = val.extract_signed_long_long();
  2783. }
  2784. #ifdef BOOST_HAS_INT128
  2785. template <unsigned Digits10, class ExponentType, class Allocator>
  2786. inline void eval_convert_to(uint128_type* result, const cpp_dec_float<Digits10, ExponentType, Allocator>& val)
  2787. {
  2788. *result = val.extract_unsigned_int128();
  2789. }
  2790. template <unsigned Digits10, class ExponentType, class Allocator>
  2791. inline void eval_convert_to(int128_type* result, const cpp_dec_float<Digits10, ExponentType, Allocator>& val)
  2792. {
  2793. *result = val.extract_signed_int128();
  2794. }
  2795. #endif
  2796. template <unsigned Digits10, class ExponentType, class Allocator>
  2797. inline void eval_convert_to(long double* result, const cpp_dec_float<Digits10, ExponentType, Allocator>& val)
  2798. {
  2799. *result = val.extract_long_double();
  2800. }
  2801. template <unsigned Digits10, class ExponentType, class Allocator>
  2802. inline void eval_convert_to(double* result, const cpp_dec_float<Digits10, ExponentType, Allocator>& val)
  2803. {
  2804. *result = val.extract_double();
  2805. }
  2806. #if defined(BOOST_HAS_FLOAT128)
  2807. template <unsigned Digits10, class ExponentType, class Allocator>
  2808. inline void eval_convert_to(float128_type* result, const cpp_dec_float<Digits10, ExponentType, Allocator>& val)
  2809. {
  2810. *result = float128_procs::strtoflt128(val.str(0, std::ios_base::scientific).c_str(), nullptr);
  2811. }
  2812. #endif
  2813. //
  2814. // Non member function support:
  2815. //
  2816. template <unsigned Digits10, class ExponentType, class Allocator>
  2817. inline int eval_fpclassify(const cpp_dec_float<Digits10, ExponentType, Allocator>& x)
  2818. {
  2819. if ((x.isinf)())
  2820. return FP_INFINITE;
  2821. if ((x.isnan)())
  2822. return FP_NAN;
  2823. if (x.iszero())
  2824. return FP_ZERO;
  2825. return FP_NORMAL;
  2826. }
  2827. template <unsigned Digits10, class ExponentType, class Allocator>
  2828. inline void eval_abs(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& x)
  2829. {
  2830. result = x;
  2831. if (x.isneg())
  2832. result.negate();
  2833. }
  2834. template <unsigned Digits10, class ExponentType, class Allocator>
  2835. inline void eval_fabs(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& x)
  2836. {
  2837. result = x;
  2838. if (x.isneg())
  2839. result.negate();
  2840. }
  2841. template <unsigned Digits10, class ExponentType, class Allocator>
  2842. inline void eval_sqrt(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& x)
  2843. {
  2844. result = x;
  2845. result.calculate_sqrt();
  2846. }
  2847. template <unsigned Digits10, class ExponentType, class Allocator>
  2848. inline void eval_floor(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& x)
  2849. {
  2850. result = x;
  2851. if (!(x.isfinite)() || x.isint())
  2852. {
  2853. if ((x.isnan)())
  2854. errno = EDOM;
  2855. return;
  2856. }
  2857. if (x.isneg())
  2858. result -= cpp_dec_float<Digits10, ExponentType, Allocator>::one();
  2859. result = result.extract_integer_part();
  2860. }
  2861. template <unsigned Digits10, class ExponentType, class Allocator>
  2862. inline void eval_ceil(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& x)
  2863. {
  2864. result = x;
  2865. if (!(x.isfinite)() || x.isint())
  2866. {
  2867. if ((x.isnan)())
  2868. errno = EDOM;
  2869. return;
  2870. }
  2871. if (!x.isneg())
  2872. result += cpp_dec_float<Digits10, ExponentType, Allocator>::one();
  2873. result = result.extract_integer_part();
  2874. }
  2875. template <unsigned Digits10, class ExponentType, class Allocator>
  2876. inline void eval_trunc(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& x)
  2877. {
  2878. if (x.isint() || !(x.isfinite)())
  2879. {
  2880. result = x;
  2881. if ((x.isnan)())
  2882. errno = EDOM;
  2883. return;
  2884. }
  2885. result = x.extract_integer_part();
  2886. }
  2887. template <unsigned Digits10, class ExponentType, class Allocator>
  2888. inline ExponentType eval_ilogb(const cpp_dec_float<Digits10, ExponentType, Allocator>& val)
  2889. {
  2890. if (val.iszero())
  2891. return (std::numeric_limits<typename cpp_dec_float<Digits10, ExponentType, Allocator>::exponent_type>::min)();
  2892. if ((val.isinf)())
  2893. return INT_MAX;
  2894. if ((val.isnan)())
  2895. #ifdef FP_ILOGBNAN
  2896. return FP_ILOGBNAN;
  2897. #else
  2898. return INT_MAX;
  2899. #endif
  2900. // Set result, to the exponent of val:
  2901. return val.order();
  2902. }
  2903. template <unsigned Digits10, class ExponentType, class Allocator, class ArgType>
  2904. inline void eval_scalbn(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& val, ArgType e_)
  2905. {
  2906. using default_ops::eval_multiply;
  2907. const typename cpp_dec_float<Digits10, ExponentType, Allocator>::exponent_type e = static_cast<typename cpp_dec_float<Digits10, ExponentType, Allocator>::exponent_type>(e_);
  2908. cpp_dec_float<Digits10, ExponentType, Allocator> t(1.0, e);
  2909. eval_multiply(result, val, t);
  2910. }
  2911. template <unsigned Digits10, class ExponentType, class Allocator, class ArgType>
  2912. inline void eval_ldexp(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& x, ArgType e)
  2913. {
  2914. const long long the_exp = static_cast<long long>(e);
  2915. using local_cpp_dec_float_type = cpp_dec_float<Digits10, ExponentType, Allocator>;
  2916. using local_exponent_type = typename local_cpp_dec_float_type::exponent_type;
  2917. using local_common_type = typename std::common_type<local_exponent_type, long long>::type;
  2918. if ( (static_cast<local_common_type>(the_exp) > static_cast<local_common_type>((std::numeric_limits<local_exponent_type>::max)()))
  2919. || (static_cast<local_common_type>(the_exp) < static_cast<local_common_type>((std::numeric_limits<local_exponent_type>::min)())))
  2920. {
  2921. BOOST_MP_THROW_EXCEPTION(std::runtime_error(std::string("Exponent value is out of range."))); // LCOV_EXCL_LINE
  2922. }
  2923. result = x;
  2924. if ( (static_cast<local_common_type>(the_exp) > static_cast<local_common_type>(-std::numeric_limits<long long>::digits))
  2925. && (static_cast<local_common_type>(the_exp) < static_cast<local_common_type>(0)))
  2926. {
  2927. result.div_unsigned_long_long(1ULL << static_cast<long long>(-the_exp));
  2928. }
  2929. else if ( (static_cast<local_common_type>(the_exp) < static_cast<local_common_type>(std::numeric_limits<long long>::digits))
  2930. && (static_cast<local_common_type>(the_exp) > static_cast<local_common_type>(0)))
  2931. {
  2932. result.mul_unsigned_long_long(1ULL << the_exp);
  2933. }
  2934. else if (the_exp != static_cast<long long>(0))
  2935. {
  2936. if ((the_exp < local_cpp_dec_float_type::cpp_dec_float_min_exp / 2) && (x.order() > 0))
  2937. {
  2938. long long half_exp = e / 2;
  2939. local_cpp_dec_float_type t { local_cpp_dec_float_type::pow2(half_exp) };
  2940. result *= t;
  2941. if (2 * half_exp != e)
  2942. t *= 2;
  2943. result *= t;
  2944. } // LCOV_EXCL_LINE This causes a false negative on lcov coverage test.
  2945. else
  2946. result *= local_cpp_dec_float_type::pow2(e);
  2947. }
  2948. }
  2949. template <unsigned Digits10, class ExponentType, class Allocator>
  2950. inline void eval_frexp(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& x, ExponentType* e)
  2951. {
  2952. const bool b_neg { x.isneg() };
  2953. result = x;
  2954. if (result.iszero() || (result.isinf)() || (result.isnan)())
  2955. {
  2956. *e = 0;
  2957. return;
  2958. }
  2959. if (b_neg)
  2960. result.negate();
  2961. using local_cpp_dec_float_type = cpp_dec_float<Digits10, ExponentType, Allocator>;
  2962. using local_exponent_type = typename local_cpp_dec_float_type::exponent_type;
  2963. local_exponent_type t { result.order() };
  2964. BOOST_MP_USING_ABS
  2965. if (abs(t) < ((std::numeric_limits<local_exponent_type>::max)() / 1000))
  2966. {
  2967. t *= 1000;
  2968. t /= 301;
  2969. }
  2970. else
  2971. {
  2972. t /= 301;
  2973. t *= 1000;
  2974. }
  2975. result *= local_cpp_dec_float_type::pow2(-t);
  2976. if (result.iszero() || (result.isinf)() || (result.isnan)())
  2977. {
  2978. // pow2 overflowed, slip the calculation up:
  2979. result = x;
  2980. if (b_neg)
  2981. result.negate();
  2982. t /= 2;
  2983. result *= local_cpp_dec_float_type::pow2(-t);
  2984. }
  2985. BOOST_MP_USING_ABS
  2986. if (abs(result.order()) > 5)
  2987. {
  2988. // If our first estimate doesn't get close enough then try recursion until we do:
  2989. local_exponent_type e2;
  2990. cpp_dec_float<Digits10, ExponentType, Allocator> r2;
  2991. eval_frexp(r2, result, &e2);
  2992. // overflow protection:
  2993. if ((t > 0) && (e2 > 0) && (t > (std::numeric_limits<local_exponent_type>::max)() - e2))
  2994. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Exponent is too large to be represented as a power of 2.")); // LCOV_EXCL_LINE
  2995. if ((t < 0) && (e2 < 0) && (t < (std::numeric_limits<local_exponent_type>::min)() - e2))
  2996. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Exponent is too large to be represented as a power of 2.")); // LCOV_EXCL_LINE
  2997. t += e2;
  2998. result = r2;
  2999. }
  3000. while (result.compare(local_cpp_dec_float_type::one()) >= 0)
  3001. {
  3002. result.div_unsigned_long_long(2ULL);
  3003. ++t;
  3004. }
  3005. while (result.compare(local_cpp_dec_float_type::half()) < 0)
  3006. {
  3007. result.mul_unsigned_long_long(2ULL);
  3008. --t;
  3009. }
  3010. *e = t;
  3011. if (b_neg)
  3012. result.negate();
  3013. }
  3014. template <unsigned Digits10, class ExponentType, class Allocator>
  3015. inline typename std::enable_if< !std::is_same<ExponentType, int>::value>::type eval_frexp(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& x, int* e)
  3016. {
  3017. using local_exponent_type = typename cpp_dec_float<Digits10, ExponentType, Allocator>::exponent_type;
  3018. local_exponent_type t { };
  3019. eval_frexp(result, x, &t);
  3020. using local_common_type = typename std::common_type<local_exponent_type, int>::type;
  3021. if ( (static_cast<local_common_type>(t) > static_cast<local_common_type>((std::numeric_limits<int>::max)()))
  3022. || (static_cast<local_common_type>(t) < static_cast<local_common_type>((std::numeric_limits<int>::min)())))
  3023. {
  3024. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Exponent is outside the range of an int")); // LCOV_EXCL_LINE
  3025. }
  3026. *e = static_cast<int>(t);
  3027. }
  3028. template <unsigned Digits10, class ExponentType, class Allocator>
  3029. inline bool eval_is_zero(const cpp_dec_float<Digits10, ExponentType, Allocator>& val)
  3030. {
  3031. return val.iszero();
  3032. }
  3033. template <unsigned Digits10, class ExponentType, class Allocator>
  3034. inline int eval_get_sign(const cpp_dec_float<Digits10, ExponentType, Allocator>& val)
  3035. {
  3036. return (val.iszero() ? 0 : (val.isneg() ? -1 : 1));
  3037. }
  3038. template <unsigned Digits10, class ExponentType, class Allocator>
  3039. inline int eval_signbit(const cpp_dec_float<Digits10, ExponentType, Allocator>& val)
  3040. {
  3041. return ((eval_get_sign(val) == -1) ? 1 : 0);
  3042. }
  3043. template <unsigned Digits10, class ExponentType, class Allocator>
  3044. inline std::size_t hash_value(const cpp_dec_float<Digits10, ExponentType, Allocator>& val)
  3045. {
  3046. return val.hash();
  3047. }
  3048. } // namespace backends
  3049. namespace detail {
  3050. template <unsigned Digits10, class ExponentType, class Allocator>
  3051. struct transcendental_reduction_type<boost::multiprecision::backends::cpp_dec_float<Digits10, ExponentType, Allocator> >
  3052. {
  3053. //
  3054. // The type used for trigonometric reduction needs 3 times the precision of the base type.
  3055. // This is double the precision of the original type, plus the largest exponent supported.
  3056. // As a practical measure the largest argument supported is 1/eps, as supporting larger
  3057. // arguments requires the division of argument by PI/2 to also be done at higher precision,
  3058. // otherwise the result (an integer) can not be represented exactly.
  3059. //
  3060. // See ARGUMENT REDUCTION FOR HUGE ARGUMENTS. K C Ng.
  3061. //
  3062. using type = boost::multiprecision::backends::cpp_dec_float<Digits10 * 3, ExponentType, Allocator>;
  3063. };
  3064. } // namespace detail
  3065. }} // namespace boost::multiprecision
  3066. namespace std {
  3067. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3068. class numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >
  3069. {
  3070. public:
  3071. static constexpr bool is_specialized = true;
  3072. static constexpr bool is_signed = true;
  3073. static constexpr bool is_integer = false;
  3074. static constexpr bool is_exact = false;
  3075. static constexpr bool is_bounded = true;
  3076. static constexpr bool is_modulo = false;
  3077. static constexpr bool is_iec559 = false;
  3078. static constexpr int digits = boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_digits10;
  3079. static constexpr int digits10 = boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_digits10;
  3080. static constexpr int max_digits10 = boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_max_digits10;
  3081. static constexpr typename boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::exponent_type min_exponent = boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_min_exp; // Type differs from int.
  3082. static constexpr typename boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::exponent_type min_exponent10 = boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_min_exp10; // Type differs from int.
  3083. static constexpr typename boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::exponent_type max_exponent = boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_max_exp; // Type differs from int.
  3084. static constexpr typename boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::exponent_type max_exponent10 = boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_max_exp10; // Type differs from int.
  3085. static constexpr int radix = boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_radix;
  3086. static constexpr std::float_round_style round_style = std::round_indeterminate;
  3087. static constexpr bool has_infinity = true;
  3088. static constexpr bool has_quiet_NaN = true;
  3089. static constexpr bool has_signaling_NaN = false;
  3090. #ifdef _MSC_VER
  3091. #pragma warning(push)
  3092. #pragma warning(disable : 4996)
  3093. #endif
  3094. static constexpr std::float_denorm_style has_denorm = std::denorm_absent;
  3095. #ifdef _MSC_VER
  3096. #pragma warning(pop)
  3097. #endif
  3098. static constexpr bool has_denorm_loss = false;
  3099. static constexpr bool traps = false;
  3100. static constexpr bool tinyness_before = false;
  3101. static constexpr boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates>(min)() { return (boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::min)(); }
  3102. static constexpr boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates>(max)() { return (boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::max)(); }
  3103. static constexpr boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> lowest() { return -(max)(); }
  3104. static constexpr boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> epsilon() { return boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::eps(); }
  3105. static constexpr boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> round_error() { return 0.5L; }
  3106. static constexpr boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> infinity() { return boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::inf(); }
  3107. static constexpr boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> quiet_NaN() { return boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::nan(); }
  3108. static constexpr boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> signaling_NaN() { return boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::zero(); }
  3109. static constexpr boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> denorm_min() { return (boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::min)(); }
  3110. };
  3111. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3112. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::digits;
  3113. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3114. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::digits10;
  3115. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3116. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::max_digits10;
  3117. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3118. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::is_signed;
  3119. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3120. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::is_integer;
  3121. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3122. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::is_exact;
  3123. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3124. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::radix;
  3125. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3126. constexpr typename boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::exponent_type numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::min_exponent;
  3127. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3128. constexpr typename boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::exponent_type numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::min_exponent10;
  3129. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3130. constexpr typename boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::exponent_type numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::max_exponent;
  3131. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3132. constexpr typename boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::exponent_type numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::max_exponent10;
  3133. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3134. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::has_infinity;
  3135. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3136. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::has_quiet_NaN;
  3137. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3138. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::has_signaling_NaN;
  3139. #ifdef _MSC_VER
  3140. #pragma warning(push)
  3141. #pragma warning(disable : 4996)
  3142. #endif
  3143. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3144. constexpr float_denorm_style numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::has_denorm;
  3145. #ifdef _MSC_VER
  3146. #pragma warning(pop)
  3147. #endif
  3148. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3149. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::has_denorm_loss;
  3150. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3151. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::is_iec559;
  3152. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3153. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::is_bounded;
  3154. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3155. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::is_modulo;
  3156. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3157. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::traps;
  3158. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3159. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::tinyness_before;
  3160. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3161. constexpr float_round_style numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::round_style;
  3162. } // namespace std
  3163. #ifdef BOOST_MP_MATH_AVAILABLE
  3164. namespace boost {
  3165. namespace math {
  3166. namespace policies {
  3167. template <unsigned Digits10, class ExponentType, class Allocator, class Policy, boost::multiprecision::expression_template_option ExpressionTemplates>
  3168. struct precision<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates>, Policy>
  3169. {
  3170. // Define a local copy of cpp_dec_float_digits10 because it might differ
  3171. // from the template parameter Digits10 for small or large digit counts.
  3172. static constexpr std::int32_t cpp_dec_float_digits10 = boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_digits10;
  3173. using precision_type = typename Policy::precision_type ;
  3174. using digits_2 = digits2<static_cast<int>(((cpp_dec_float_digits10 + 1LL) * 1000LL) / 301LL)>;
  3175. using type = typename std::conditional<
  3176. ((digits_2::value <= precision_type::value) || (Policy::precision_type::value <= 0)),
  3177. // Default case, full precision for RealType:
  3178. digits_2,
  3179. // User customized precision:
  3180. precision_type>::type;
  3181. };
  3182. }
  3183. }} // namespace boost::math::policies
  3184. #endif
  3185. #ifdef BOOST_MSVC
  3186. #pragma warning(pop)
  3187. #endif
  3188. #endif