cpp_double_fp.hpp 102 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright 2021 - 2025 Fahad Syed.
  3. // Copyright 2021 - 2025 Christopher Kormanyos.
  4. // Copyright 2021 - 2025 Janek Kozicki.
  5. // Copyright 2025 Matt Borland.
  6. // Distributed under the Boost Software License, Version 1.0.
  7. // (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_MP_CPP_DOUBLE_FP_2021_06_05_HPP
  11. #define BOOST_MP_CPP_DOUBLE_FP_2021_06_05_HPP
  12. #include <boost/multiprecision/cpp_bin_float.hpp>
  13. #include <boost/multiprecision/cpp_df_qf/cpp_df_qf_detail.hpp>
  14. #include <boost/multiprecision/detail/hash.hpp>
  15. #include <boost/multiprecision/traits/max_digits10.hpp>
  16. #include <boost/multiprecision/traits/std_integer_traits.hpp>
  17. #ifdef BOOST_MP_MATH_AVAILABLE
  18. //
  19. // Headers required for Boost.Math integration:
  20. //
  21. #include <boost/math/policies/policy.hpp>
  22. //
  23. // Some includes we need from Boost.Math, since we rely on that library to provide these functions:
  24. //
  25. #include <boost/math/special_functions/acosh.hpp>
  26. #include <boost/math/special_functions/asinh.hpp>
  27. #include <boost/math/special_functions/atanh.hpp>
  28. #include <boost/math/special_functions/cbrt.hpp>
  29. #include <boost/math/special_functions/expm1.hpp>
  30. #include <boost/math/special_functions/gamma.hpp>
  31. #endif
  32. #include <limits>
  33. #include <string>
  34. #include <type_traits>
  35. #if (defined(BOOST_CLANG) && defined(BOOST_CLANG_VERSION) && (BOOST_CLANG_VERSION <= 90000))
  36. #define BOOST_MP_DF_QF_NUM_LIMITS_CLASS_TYPE struct
  37. #else
  38. #define BOOST_MP_DF_QF_NUM_LIMITS_CLASS_TYPE class
  39. #endif
  40. namespace boost { namespace multiprecision { namespace backends {
  41. template <typename FloatingPointType>
  42. class cpp_double_fp_backend;
  43. template <typename FloatingPointType>
  44. constexpr auto eval_add(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& x) -> void;
  45. template <typename FloatingPointType>
  46. constexpr auto eval_add(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& a, const cpp_double_fp_backend<FloatingPointType>& b) -> void;
  47. template <typename FloatingPointType>
  48. constexpr auto eval_subtract(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& x) -> void;
  49. template <typename FloatingPointType>
  50. constexpr auto eval_subtract(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& a, const cpp_double_fp_backend<FloatingPointType>& b) -> void;
  51. template <typename FloatingPointType>
  52. constexpr auto eval_multiply(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& x) -> void;
  53. template <typename FloatingPointType>
  54. constexpr auto eval_multiply(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& a, const cpp_double_fp_backend<FloatingPointType>& b) -> void;
  55. template <typename FloatingPointType>
  56. constexpr auto eval_divide(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& x) -> void;
  57. template <typename FloatingPointType>
  58. constexpr auto eval_divide(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& a, const cpp_double_fp_backend<FloatingPointType>& b) -> void;
  59. template <typename FloatingPointType>
  60. constexpr auto eval_eq(const cpp_double_fp_backend<FloatingPointType>& a, const cpp_double_fp_backend<FloatingPointType>& b) -> bool;
  61. template <typename FloatingPointType>
  62. constexpr auto eval_lt(const cpp_double_fp_backend<FloatingPointType>& a, const cpp_double_fp_backend<FloatingPointType>& b) -> bool;
  63. template <typename FloatingPointType>
  64. constexpr auto eval_gt(const cpp_double_fp_backend<FloatingPointType>& a, const cpp_double_fp_backend<FloatingPointType>& b) -> bool;
  65. template <typename FloatingPointType>
  66. constexpr auto eval_is_zero(const cpp_double_fp_backend<FloatingPointType>& x) -> bool;
  67. template <typename FloatingPointType>
  68. constexpr auto eval_signbit(const cpp_double_fp_backend<FloatingPointType>& x) -> int;
  69. template <typename FloatingPointType>
  70. constexpr auto eval_fabs(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& a) -> void;
  71. template <typename FloatingPointType>
  72. constexpr auto eval_frexp(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& a, int* v) -> void;
  73. template <typename FloatingPointType>
  74. constexpr auto eval_ldexp(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& a, int v) -> void;
  75. template <typename FloatingPointType>
  76. constexpr auto eval_floor(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& x) -> void;
  77. template <typename FloatingPointType>
  78. constexpr auto eval_ceil(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& x) -> void;
  79. template <typename FloatingPointType>
  80. constexpr auto eval_fpclassify(const cpp_double_fp_backend<FloatingPointType>& o) -> int;
  81. template <typename FloatingPointType>
  82. constexpr auto eval_sqrt(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& o) -> void;
  83. template <typename FloatingPointType>
  84. constexpr auto eval_pow(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& x, const cpp_double_fp_backend<FloatingPointType>& a) -> void;
  85. template <typename FloatingPointType,
  86. typename IntegralType>
  87. constexpr auto eval_pow(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& x, IntegralType n) -> typename ::std::enable_if<boost::multiprecision::detail::is_integral<IntegralType>::value, void>::type;
  88. template <typename FloatingPointType,
  89. typename ::std::enable_if<(cpp_df_qf_detail::is_floating_point<FloatingPointType>::value && ((cpp_df_qf_detail::ccmath::numeric_limits<FloatingPointType>::digits10 * 2) < 16))>::type const* = nullptr>
  90. constexpr auto eval_exp(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& x) -> void;
  91. template <typename FloatingPointType,
  92. typename ::std::enable_if<(cpp_df_qf_detail::is_floating_point<FloatingPointType>::value && (((cpp_df_qf_detail::ccmath::numeric_limits<FloatingPointType>::digits10 * 2) >= 16) && ((cpp_df_qf_detail::ccmath::numeric_limits<FloatingPointType>::digits10 * 2) <= 36)))>::type const* = nullptr>
  93. constexpr auto eval_exp(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& x) -> void;
  94. template <typename FloatingPointType,
  95. typename ::std::enable_if<(cpp_df_qf_detail::is_floating_point<FloatingPointType>::value && ((cpp_df_qf_detail::ccmath::numeric_limits<FloatingPointType>::digits10 * 2) > 36))>::type const* = nullptr>
  96. constexpr auto eval_exp(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& x) -> void;
  97. template <typename FloatingPointType>
  98. constexpr auto eval_log(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& x) -> void;
  99. template <typename FloatingPointType>
  100. constexpr auto eval_convert_to(signed long long* result, const cpp_double_fp_backend<FloatingPointType>& backend) -> void;
  101. template <typename FloatingPointType>
  102. constexpr auto eval_convert_to(unsigned long long* result, const cpp_double_fp_backend<FloatingPointType>& backend) -> void;
  103. #ifdef BOOST_HAS_INT128
  104. template <typename FloatingPointType>
  105. constexpr auto eval_convert_to(boost::int128_type* result, const cpp_double_fp_backend<FloatingPointType>& backend) -> typename std::enable_if<(cpp_df_qf_detail::ccmath::numeric_limits<FloatingPointType>::digits > 24), void>::type;
  106. template <typename FloatingPointType>
  107. constexpr auto eval_convert_to(boost::int128_type* result, const cpp_double_fp_backend<FloatingPointType>& backend) -> typename std::enable_if<!(cpp_df_qf_detail::ccmath::numeric_limits<FloatingPointType>::digits > 24), void>::type;
  108. template <typename FloatingPointType>
  109. constexpr auto eval_convert_to(boost::uint128_type* result, const cpp_double_fp_backend<FloatingPointType>& backend) -> typename std::enable_if<(cpp_df_qf_detail::ccmath::numeric_limits<FloatingPointType>::digits > 24), void>::type;
  110. template <typename FloatingPointType>
  111. constexpr auto eval_convert_to(boost::uint128_type* result, const cpp_double_fp_backend<FloatingPointType>& backend) -> typename std::enable_if<!(cpp_df_qf_detail::ccmath::numeric_limits<FloatingPointType>::digits > 24), void>::type;
  112. #endif
  113. template <typename FloatingPointType,
  114. typename OtherFloatingPointType>
  115. constexpr auto eval_convert_to(OtherFloatingPointType* result, const cpp_double_fp_backend<FloatingPointType>& backend) -> typename ::std::enable_if<cpp_df_qf_detail::is_floating_point<OtherFloatingPointType>::value>::type;
  116. template <typename FloatingPointType>
  117. constexpr auto hash_value(const cpp_double_fp_backend<FloatingPointType>& a) -> ::std::size_t;
  118. template <typename FloatingPointType>
  119. constexpr auto fabs(const cpp_double_fp_backend<FloatingPointType>& a) -> cpp_double_fp_backend<FloatingPointType>;
  120. } } } // namespace boost::multiprecision::backends
  121. namespace std {
  122. // Foward declarations of various specializations of std::numeric_limits
  123. template <typename FloatingPointType,
  124. const boost::multiprecision::expression_template_option ExpressionTemplatesOption>
  125. BOOST_MP_DF_QF_NUM_LIMITS_CLASS_TYPE numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::cpp_double_fp_backend<FloatingPointType>, ExpressionTemplatesOption> >;
  126. } // namespace std
  127. namespace boost { namespace multiprecision {
  128. template <typename FloatingPointType>
  129. struct number_category<backends::cpp_double_fp_backend<FloatingPointType>> : public std::integral_constant<int, number_kind_floating_point> { };
  130. namespace backends {
  131. // A cpp_double_fp_backend is represented by an unevaluated sum of two
  132. // floating-point units, a0 and a1, which satisfy |a1| <= (1 / 2) * ulp(a0).
  133. // The type of the floating-point constituents should adhere to IEEE754.
  134. // Although the constituent parts (a0 and a1) satisfy |a1| <= (1 / 2) * ulp(a0),
  135. // the composite type does not adhere to these strict error bounds. Its error
  136. // bounds are larger.
  137. // This class has been tested with floats having single-precision (4 byte),
  138. // double-precision (8 byte) and quad precision (16 byte, such as GCC's __float128).
  139. template <typename FloatingPointType>
  140. class cpp_double_fp_backend
  141. {
  142. public:
  143. using float_type = FloatingPointType;
  144. static_assert
  145. (
  146. cpp_df_qf_detail::is_floating_point<float_type>::value
  147. && bool
  148. {
  149. ((cpp_df_qf_detail::ccmath::numeric_limits<float_type>::digits == 24) && cpp_df_qf_detail::ccmath::numeric_limits<float_type>::is_specialized && cpp_df_qf_detail::ccmath::numeric_limits<float_type>::is_iec559)
  150. || ((cpp_df_qf_detail::ccmath::numeric_limits<float_type>::digits == 53) && cpp_df_qf_detail::ccmath::numeric_limits<float_type>::is_specialized && cpp_df_qf_detail::ccmath::numeric_limits<float_type>::is_iec559)
  151. || ((cpp_df_qf_detail::ccmath::numeric_limits<float_type>::digits == 64) && cpp_df_qf_detail::ccmath::numeric_limits<float_type>::is_specialized && cpp_df_qf_detail::ccmath::numeric_limits<float_type>::is_iec559)
  152. || (cpp_df_qf_detail::ccmath::numeric_limits<float_type>::digits == 113)
  153. }, "Error: float_type does not fulfill the backend requirements of cpp_double_fp_backend"
  154. );
  155. using rep_type = cpp_df_qf_detail::pair<float_type, float_type>;
  156. using arithmetic = cpp_df_qf_detail::exact_arithmetic<float_type>;
  157. using signed_types = std::tuple<signed char, signed short, signed int, signed long, signed long long, std::intmax_t>;
  158. using unsigned_types = std::tuple<unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long, std::uintmax_t>;
  159. using float_types = std::tuple<float, double, long double>;
  160. using exponent_type = int;
  161. static constexpr int my_digits = static_cast<int>(cpp_df_qf_detail::ccmath::numeric_limits<float_type>::digits * static_cast<int>(INT8_C(2)));
  162. static constexpr int my_digits10 = static_cast<int>(boost::multiprecision::detail::calc_digits10 <static_cast<unsigned>(my_digits)>::value);
  163. static constexpr int my_max_digits10 = static_cast<int>(boost::multiprecision::detail::calc_max_digits10<static_cast<unsigned>(my_digits)>::value);
  164. static constexpr int my_max_exponent = cpp_df_qf_detail::ccmath::numeric_limits<float_type>::max_exponent;
  165. static constexpr int my_max_exponent10 = cpp_df_qf_detail::ccmath::numeric_limits<float_type>::max_exponent10;
  166. static constexpr int my_min_exponent =
  167. static_cast<int>
  168. (
  169. cpp_df_qf_detail::ccmath::numeric_limits<float_type>::min_exponent
  170. + cpp_df_qf_detail::ccmath::numeric_limits<float_type>::digits
  171. );
  172. static constexpr int my_min_exponent10 =
  173. static_cast<int>
  174. (
  175. -static_cast<int>
  176. (
  177. boost::multiprecision::detail::calc_digits10<static_cast<unsigned>(-my_min_exponent)>::value
  178. )
  179. );
  180. // Default constructor.
  181. constexpr cpp_double_fp_backend() noexcept { }
  182. // Copy constructor.
  183. constexpr cpp_double_fp_backend(const cpp_double_fp_backend& other) : data(other.data) { }
  184. // Move constructor.
  185. constexpr cpp_double_fp_backend(cpp_double_fp_backend&& other) noexcept : data(static_cast<rep_type&&>(other.data)) { }
  186. // Constructors from other floating-point types.
  187. template <typename OtherFloatType,
  188. typename ::std::enable_if<( cpp_df_qf_detail::is_floating_point<OtherFloatType>::value
  189. && (cpp_df_qf_detail::ccmath::numeric_limits<OtherFloatType>::digits <= cpp_df_qf_detail::ccmath::numeric_limits<float_type>::digits))>::type const* = nullptr>
  190. constexpr cpp_double_fp_backend(const OtherFloatType& f)
  191. : data(f, static_cast<float_type>(0.0F)) { }
  192. template <typename OtherFloatType,
  193. typename ::std::enable_if<( cpp_df_qf_detail::is_floating_point<OtherFloatType>::value
  194. && (cpp_df_qf_detail::ccmath::numeric_limits<OtherFloatType>::digits > cpp_df_qf_detail::ccmath::numeric_limits<float_type>::digits))>::type const* = nullptr>
  195. constexpr cpp_double_fp_backend(const OtherFloatType& f)
  196. : data(static_cast<float_type>(f),
  197. static_cast<float_type>(f - static_cast<OtherFloatType>(static_cast<float_type>(f)))) { }
  198. // Construtor from another kind of cpp_double_fp_backend<> object.
  199. template <typename OtherFloatType,
  200. typename ::std::enable_if<( cpp_df_qf_detail::is_floating_point<OtherFloatType>::value
  201. && (!std::is_same<FloatingPointType, OtherFloatType>::value))>::type const* = nullptr>
  202. constexpr cpp_double_fp_backend(const cpp_double_fp_backend<OtherFloatType>& a)
  203. : cpp_double_fp_backend(cpp_double_fp_backend(a.my_first()) += a.my_second()) { }
  204. // Constructors from integers.
  205. template <typename SignedIntegralType,
  206. typename ::std::enable_if<( boost::multiprecision::detail::is_integral<SignedIntegralType>::value
  207. && (!boost::multiprecision::detail::is_unsigned<SignedIntegralType>::value)
  208. && ((static_cast<int>(sizeof(SignedIntegralType) * 8) - 1) <= cpp_df_qf_detail::ccmath::numeric_limits<float_type>::digits))>::type const* = nullptr>
  209. constexpr cpp_double_fp_backend(const SignedIntegralType& n)
  210. : data(static_cast<float_type>(n), static_cast<float_type>(0.0F)) { }
  211. template <typename UnsignedIntegralType,
  212. typename ::std::enable_if<( boost::multiprecision::detail::is_integral<UnsignedIntegralType>::value
  213. && boost::multiprecision::detail::is_unsigned<UnsignedIntegralType>::value
  214. && (static_cast<int>(sizeof(UnsignedIntegralType) * 8u) <= cpp_df_qf_detail::ccmath::numeric_limits<float_type>::digits))>::type const* = nullptr>
  215. constexpr cpp_double_fp_backend(const UnsignedIntegralType& u)
  216. : data(static_cast<float_type>(u), static_cast<float_type>(0.0F)) { }
  217. // Constructors from integers which hold more information than *this can contain.
  218. template <typename UnsignedIntegralType,
  219. typename ::std::enable_if<( boost::multiprecision::detail::is_integral<UnsignedIntegralType>::value
  220. && boost::multiprecision::detail::is_unsigned<UnsignedIntegralType>::value
  221. && (static_cast<int>(sizeof(UnsignedIntegralType) * 8u) > cpp_df_qf_detail::ccmath::numeric_limits<float_type>::digits))>::type const* = nullptr>
  222. constexpr cpp_double_fp_backend(UnsignedIntegralType u)
  223. : data(static_cast<float_type>(u & cpp_df_qf_detail::float_mask<UnsignedIntegralType, float_type>()),
  224. static_cast<float_type>(0.0F))
  225. {
  226. using local_unsigned_integral_type = UnsignedIntegralType;
  227. if (u > cpp_df_qf_detail::float_mask<local_unsigned_integral_type, float_type>())
  228. {
  229. local_unsigned_integral_type
  230. local_flt_mask
  231. {
  232. cpp_df_qf_detail::float_mask<local_unsigned_integral_type, float_type>()
  233. };
  234. for (int index_mask_lsb = cpp_df_qf_detail::ccmath::numeric_limits<float_type>::digits;
  235. (index_mask_lsb < static_cast<int>(sizeof(local_unsigned_integral_type) * 8u))
  236. && (local_flt_mask != local_unsigned_integral_type { UINT8_C(0) });
  237. index_mask_lsb += cpp_df_qf_detail::ccmath::numeric_limits<float_type>::digits)
  238. {
  239. local_flt_mask <<= static_cast<unsigned>(cpp_df_qf_detail::ccmath::numeric_limits<float_type>::digits);
  240. add_unchecked_limb(static_cast<float_type>(u & local_flt_mask));
  241. }
  242. }
  243. }
  244. template <typename SignedIntegralType,
  245. typename ::std::enable_if<( boost::multiprecision::detail::is_integral<SignedIntegralType>::value
  246. && (!boost::multiprecision::detail::is_unsigned<SignedIntegralType>::value)
  247. && ((static_cast<int>(sizeof(SignedIntegralType) * 8) - 1) > cpp_df_qf_detail::ccmath::numeric_limits<float_type>::digits))>::type const* = nullptr>
  248. constexpr cpp_double_fp_backend(SignedIntegralType n)
  249. {
  250. const bool is_neg { (n < SignedIntegralType { INT8_C(0) }) };
  251. using local_unsigned_integral_type = typename boost::multiprecision::detail::make_unsigned<SignedIntegralType>::type;
  252. const local_unsigned_integral_type
  253. u_val
  254. {
  255. (!is_neg)
  256. ? static_cast<local_unsigned_integral_type>(n)
  257. : static_cast<local_unsigned_integral_type>
  258. (
  259. static_cast<local_unsigned_integral_type>(~n)
  260. + static_cast<local_unsigned_integral_type>(UINT8_C(1))
  261. )
  262. };
  263. data = cpp_double_fp_backend(u_val).data;
  264. if(is_neg) { negate(); }
  265. }
  266. constexpr cpp_double_fp_backend(const float_type& a, const float_type& b) noexcept : data(a, b) { }
  267. constexpr cpp_double_fp_backend(const cpp_df_qf_detail::pair<float_type, float_type>& p) noexcept : data(p) { }
  268. cpp_double_fp_backend(const char* p_str)
  269. {
  270. *this = p_str;
  271. }
  272. // Assignment operator.
  273. constexpr auto operator=(const cpp_double_fp_backend& other) -> cpp_double_fp_backend&
  274. {
  275. if (this != &other)
  276. {
  277. data = other.data;
  278. }
  279. return *this;
  280. }
  281. // Move assignment operator.
  282. constexpr auto operator=(cpp_double_fp_backend&& other) noexcept -> cpp_double_fp_backend&
  283. {
  284. data = static_cast<rep_type&&>(other.data);
  285. return *this;
  286. }
  287. // Assignment operator from another kind of cpp_double_fp_backend<> object.
  288. template <typename OtherFloatType,
  289. typename ::std::enable_if<( cpp_df_qf_detail::is_floating_point<OtherFloatType>::value
  290. && (!std::is_same<FloatingPointType, OtherFloatType>::value))>::type const* = nullptr>
  291. constexpr auto operator=(const cpp_double_fp_backend<OtherFloatType>& other) -> cpp_double_fp_backend&
  292. {
  293. return operator=(cpp_double_fp_backend(other));
  294. }
  295. template <typename OtherFloatType>
  296. constexpr auto operator=(const OtherFloatType f) -> typename ::std::enable_if<cpp_df_qf_detail::is_floating_point<OtherFloatType>::value, cpp_double_fp_backend&>::type
  297. {
  298. return operator=(cpp_double_fp_backend(f));
  299. }
  300. template <typename IntegralType>
  301. constexpr auto operator=(const IntegralType n) -> typename ::std::enable_if<boost::multiprecision::detail::is_integral<IntegralType>::value, cpp_double_fp_backend&>::type
  302. {
  303. return operator=(cpp_double_fp_backend(n));
  304. }
  305. auto operator=(const char* p_str) -> cpp_double_fp_backend&
  306. {
  307. rd_string(p_str);
  308. return *this;
  309. }
  310. constexpr auto hash() const -> ::std::size_t
  311. {
  312. #if defined(BOOST_MP_CPP_DOUBLE_FP_HAS_FLOAT128)
  313. using local_float_type = typename std::conditional<::std::is_same<float_type, ::boost::float128_type>::value,
  314. long double,
  315. float_type>::type;
  316. #else
  317. using local_float_type = float_type;
  318. #endif
  319. std::size_t result { UINT8_C(0) };
  320. int n_first { };
  321. int n_second { };
  322. boost::multiprecision::detail::hash_combine(result, static_cast<local_float_type>(cpp_df_qf_detail::ccmath::frexp(data.first, &n_first)));
  323. boost::multiprecision::detail::hash_combine(result, static_cast<local_float_type>(cpp_df_qf_detail::ccmath::frexp(data.second, &n_second)));
  324. boost::multiprecision::detail::hash_combine(result, n_first);
  325. boost::multiprecision::detail::hash_combine(result, n_second);
  326. return result;
  327. }
  328. // The public methods follow.
  329. constexpr auto isneg_unchecked() const noexcept -> bool { return (data.first < 0); }
  330. constexpr auto iszero_unchecked() const noexcept -> bool { return (data.first == float_type { 0.0F }); }
  331. constexpr auto is_one() const noexcept -> bool
  332. {
  333. return
  334. (
  335. (data.second == float_type { 0.0F })
  336. && (data.first == float_type { 1.0F })
  337. );
  338. }
  339. constexpr auto negate() -> void
  340. {
  341. const bool isinf_u { (cpp_df_qf_detail::ccmath::isinf)(data.first) };
  342. const bool isnan_u { (cpp_df_qf_detail::ccmath::isnan)(data.first) };
  343. if (isnan_u) { }
  344. else if (isinf_u)
  345. {
  346. data.first = -data.first;
  347. }
  348. else
  349. {
  350. if (!iszero_unchecked())
  351. {
  352. data = arithmetic::normalize(-data.first, -data.second);
  353. }
  354. }
  355. }
  356. // Getters/Setters
  357. constexpr auto my_first () const noexcept -> const float_type& { return data.first; }
  358. constexpr auto my_second() const noexcept -> const float_type& { return data.second; }
  359. constexpr auto rep() noexcept -> rep_type& { return data; }
  360. constexpr auto rep() const noexcept -> const rep_type& { return data; }
  361. constexpr auto crep() const noexcept -> const rep_type& { return data; }
  362. // Unary add/sub/mul/div follow in the upcoming paragraphs.
  363. constexpr auto operator+=(const cpp_double_fp_backend& v) -> cpp_double_fp_backend&
  364. {
  365. const int fpc_u { eval_fpclassify(*this) };
  366. const int fpc_v { eval_fpclassify(v) };
  367. if ((fpc_u != FP_NORMAL) || (fpc_v != FP_NORMAL))
  368. {
  369. // Handle special cases like zero, inf and NaN.
  370. if (fpc_u == FP_NAN)
  371. {
  372. return *this;
  373. }
  374. const bool isinf_v { (fpc_v == FP_INFINITE) };
  375. if (fpc_u == FP_INFINITE)
  376. {
  377. if (isinf_v && (isneg_unchecked() != v.isneg_unchecked()))
  378. {
  379. *this = cpp_double_fp_backend::my_value_nan();
  380. }
  381. return *this;
  382. }
  383. const bool iszero_u { ((fpc_u == FP_ZERO) || (fpc_u == FP_SUBNORMAL)) };
  384. const bool isnan_v { (fpc_v == FP_NAN) };
  385. if (iszero_u || (isnan_v || isinf_v))
  386. {
  387. if (iszero_u)
  388. {
  389. data.first = float_type { 0.0F };
  390. data.second = float_type { 0.0F };
  391. }
  392. const bool iszero_v { ((fpc_v == FP_ZERO) || (fpc_v == FP_SUBNORMAL)) };
  393. return ((!iszero_v) ? operator=(v) : *this);
  394. }
  395. }
  396. add_unchecked(v);
  397. return *this;
  398. }
  399. constexpr auto operator-=(const cpp_double_fp_backend& v) -> cpp_double_fp_backend&
  400. {
  401. const int fpc_u { eval_fpclassify(*this) };
  402. const int fpc_v { eval_fpclassify(v) };
  403. if ((fpc_u != FP_NORMAL) || (fpc_v != FP_NORMAL))
  404. {
  405. // Handle special cases like zero, inf and NaN.
  406. if (fpc_u == FP_NAN)
  407. {
  408. return *this;
  409. }
  410. const bool isinf_v { (fpc_v == FP_INFINITE) };
  411. if (fpc_u == FP_INFINITE)
  412. {
  413. if (isinf_v && (isneg_unchecked() == v.isneg_unchecked()))
  414. {
  415. *this = cpp_double_fp_backend::my_value_nan();
  416. }
  417. return *this;
  418. }
  419. const bool iszero_u { ((fpc_u == FP_ZERO) || (fpc_u == FP_SUBNORMAL)) };
  420. const bool isnan_v { (fpc_v == FP_NAN) };
  421. if (iszero_u || (isnan_v || isinf_v))
  422. {
  423. if (iszero_u)
  424. {
  425. data.first = float_type { 0.0F };
  426. data.second = float_type { 0.0F };
  427. }
  428. const bool iszero_v { ((fpc_v == FP_ZERO) || (fpc_v == FP_SUBNORMAL)) };
  429. return ((!iszero_v) ? operator=(-v) : *this);
  430. }
  431. }
  432. if (this == &v)
  433. {
  434. data.first = float_type { 0.0F };
  435. data.second = float_type { 0.0F };
  436. return *this;
  437. }
  438. const rep_type thi_tlo { arithmetic::two_diff(data.second, v.data.second) };
  439. data = arithmetic::two_diff(data.first, v.data.first);
  440. if (cpp_df_qf_detail::ccmath::isinf(data.first))
  441. {
  442. // Handle overflow.
  443. const bool b_neg { (data.first < float_type { 0.0F }) };
  444. *this = cpp_double_fp_backend::my_value_inf();
  445. if (b_neg)
  446. {
  447. negate();
  448. }
  449. return *this;
  450. }
  451. data = arithmetic::two_hilo_sum(data.first, data.second + thi_tlo.first);
  452. data = arithmetic::two_hilo_sum(data.first, thi_tlo.second + data.second);
  453. return *this;
  454. }
  455. constexpr auto operator*=(const cpp_double_fp_backend& v) -> cpp_double_fp_backend&
  456. {
  457. // Evaluate the sign of the result.
  458. const int fpc_u { eval_fpclassify(*this) };
  459. const int fpc_v { eval_fpclassify(v) };
  460. if ((fpc_u != FP_NORMAL) || (fpc_v != FP_NORMAL))
  461. {
  462. // Handle special cases like zero, inf and NaN.
  463. const bool isinf_u { (fpc_u == FP_INFINITE) };
  464. const bool isinf_v { (fpc_v == FP_INFINITE) };
  465. const bool iszero_u { (fpc_u == FP_ZERO) };
  466. const bool iszero_v { (fpc_v == FP_ZERO) };
  467. if (((fpc_u == FP_NAN) || (fpc_v == FP_NAN)) || (isinf_u && iszero_v) || (isinf_v && iszero_u))
  468. {
  469. return operator=( cpp_double_fp_backend::my_value_nan());
  470. }
  471. if (isinf_u || isinf_v)
  472. {
  473. const bool b_neg { (isneg_unchecked() != v.isneg_unchecked()) };
  474. *this = cpp_double_fp_backend::my_value_inf();
  475. if (b_neg)
  476. {
  477. negate();
  478. }
  479. return *this;
  480. }
  481. if (iszero_u || iszero_v)
  482. {
  483. return operator=(cpp_double_fp_backend(0));
  484. }
  485. }
  486. mul_unchecked(v);
  487. return *this;
  488. }
  489. constexpr auto operator/=(const cpp_double_fp_backend& v) -> cpp_double_fp_backend&
  490. {
  491. const int fpc_u { eval_fpclassify(*this) };
  492. const int fpc_v { eval_fpclassify(v) };
  493. if ((fpc_u != FP_NORMAL) || (fpc_v != FP_NORMAL))
  494. {
  495. // Handle special cases like zero, inf and NaN.
  496. const bool isnan_u { (fpc_u == FP_NAN) };
  497. const bool isnan_v { (fpc_v == FP_NAN) };
  498. if (isnan_u || isnan_v)
  499. {
  500. return operator=(cpp_double_fp_backend::my_value_nan());
  501. }
  502. const bool iszero_u { (fpc_u == FP_ZERO) };
  503. const bool iszero_v { (fpc_v == FP_ZERO) };
  504. if (iszero_u)
  505. {
  506. if (iszero_v)
  507. {
  508. return operator=(cpp_double_fp_backend::my_value_nan());
  509. }
  510. else
  511. {
  512. return operator=(cpp_double_fp_backend(0));
  513. }
  514. }
  515. // Handle more special cases like zero, inf and NaN.
  516. if (iszero_v)
  517. {
  518. const bool b_neg = isneg_unchecked();
  519. *this = cpp_double_fp_backend::my_value_inf();
  520. if (b_neg)
  521. {
  522. negate();
  523. }
  524. return *this;
  525. }
  526. const bool isinf_v { (fpc_v == FP_INFINITE) };
  527. const bool isinf_u { (fpc_u == FP_INFINITE) };
  528. if (isinf_u)
  529. {
  530. if (isinf_v)
  531. {
  532. return operator=(cpp_double_fp_backend::my_value_nan());
  533. }
  534. else
  535. {
  536. const bool b_neg { isneg_unchecked() };
  537. return operator=((!b_neg) ? cpp_double_fp_backend::my_value_inf() : -cpp_double_fp_backend::my_value_inf());
  538. }
  539. }
  540. if (isinf_v)
  541. {
  542. return operator=(cpp_double_fp_backend(0));
  543. }
  544. }
  545. if (this == &v)
  546. {
  547. data.first = float_type { 1.0F };
  548. data.second = float_type { 0.0F };
  549. return *this;
  550. }
  551. // The division algorithm has been taken from Victor Shoup,
  552. // package WinNTL-5_3_2. It might originally be related to the
  553. // K. Briggs work. The algorithm has been significantly simplified
  554. // while still attempting to retain proper rounding corrections.
  555. // Checks for overflow and underflow have been added.
  556. const float_type C { data.first / v.data.first };
  557. float_type c { cpp_df_qf_detail::split_maker<float_type>::value * C };
  558. float_type hc { };
  559. if (cpp_df_qf_detail::ccmath::isinf(c))
  560. {
  561. // Handle overflow by scaling down (and then back up) with the split.
  562. hc =
  563. cpp_df_qf_detail::ccmath::ldexp
  564. (
  565. C - float_type { C - cpp_df_qf_detail::ccmath::ldexp(C, -cpp_df_qf_detail::split_maker<float_type>::n_shl) },
  566. cpp_df_qf_detail::split_maker<float_type>::n_shl
  567. );
  568. }
  569. else
  570. {
  571. hc = c - float_type { c - C };
  572. }
  573. float_type u { cpp_df_qf_detail::split_maker<float_type>::value * v.data.first };
  574. const float_type hv =
  575. (
  576. cpp_df_qf_detail::ccmath::isinf(u)
  577. ? cpp_df_qf_detail::ccmath::ldexp
  578. (
  579. // Handle overflow by scaling down (and then back up) with the split.
  580. v.data.first - float_type { v.data.first - cpp_df_qf_detail::ccmath::ldexp(v.data.first, -cpp_df_qf_detail::split_maker<float_type>::n_shl) },
  581. cpp_df_qf_detail::split_maker<float_type>::n_shl
  582. )
  583. : u - float_type { u - v.data.first }
  584. );
  585. const float_type U { C * v.data.first };
  586. u = cpp_df_qf_detail::ccmath::unsafe::fma(hc, hv, -U);
  587. {
  588. const float_type tv { v.data.first - hv };
  589. u = cpp_df_qf_detail::ccmath::unsafe::fma(hc, tv, u);
  590. const float_type tc { C - hc };
  591. u = cpp_df_qf_detail::ccmath::unsafe::fma(tc, hv, u);
  592. u = cpp_df_qf_detail::ccmath::unsafe::fma(tc, tv, u);
  593. }
  594. c = float_type { (data.first - U) - u } + data.second;
  595. c = (c - float_type { C * v.data.second }) / v.data.first;
  596. // Perform even more simplifications compared to Victor Shoup.
  597. data.first = C + c;
  598. data.second = float_type { C - data.first } + c;
  599. return *this;
  600. }
  601. // Unary minus operator.
  602. constexpr auto operator-() const -> cpp_double_fp_backend
  603. {
  604. cpp_double_fp_backend v { *this };
  605. v.negate();
  606. return v;
  607. }
  608. // Public helper functions.
  609. static constexpr auto pown(cpp_double_fp_backend& result, const cpp_double_fp_backend& x, int p) -> void
  610. {
  611. using local_float_type = cpp_double_fp_backend;
  612. if (p == 2)
  613. {
  614. result = x; result.mul_unchecked(x);
  615. }
  616. else if (p == 3)
  617. {
  618. result = x; result.mul_unchecked(x); result.mul_unchecked(x);
  619. }
  620. else if (p == 4)
  621. {
  622. local_float_type x2 { x };
  623. x2.mul_unchecked(x);
  624. result = x2;
  625. result.mul_unchecked(x2);
  626. }
  627. else if (p == 1)
  628. {
  629. result = x;
  630. }
  631. else if (p < 0)
  632. {
  633. // The case p == 0 is checked in a higher calling layer.
  634. pown(result, local_float_type(1U) / x, -p);
  635. }
  636. else
  637. {
  638. result = local_float_type(1U);
  639. local_float_type y(x);
  640. auto p_local = static_cast<std::uint32_t>(p);
  641. for (;;)
  642. {
  643. if (static_cast<std::uint_fast8_t>(p_local & static_cast<std::uint32_t>(UINT8_C(1))) != static_cast<std::uint_fast8_t>(UINT8_C(0)))
  644. {
  645. result.mul_unchecked(y);
  646. }
  647. p_local >>= 1U;
  648. if (p_local == static_cast<std::uint32_t>(UINT8_C(0)))
  649. {
  650. break;
  651. }
  652. else
  653. {
  654. y.mul_unchecked(y);
  655. }
  656. }
  657. }
  658. }
  659. constexpr auto swap(cpp_double_fp_backend& other) -> void
  660. {
  661. if (this != &other)
  662. {
  663. const rep_type tmp { data };
  664. data = other.data;
  665. other.data = tmp;
  666. }
  667. }
  668. constexpr auto swap(cpp_double_fp_backend&& other) noexcept -> void
  669. {
  670. const rep_type tmp { static_cast<typename cpp_double_fp_backend::rep_type&&>(data) };
  671. data = other.data;
  672. other.data = tmp;
  673. }
  674. constexpr auto compare(const cpp_double_fp_backend& other) const noexcept -> int
  675. {
  676. // Return 1 for *this > other, -1 for *this < other, 0 for *this = other.
  677. if ((cpp_df_qf_detail::ccmath::isnan)(data.first))
  678. {
  679. return -1;
  680. }
  681. else
  682. {
  683. return (my_first() > other.my_first()) ? 1 : (my_first() < other.my_first())
  684. ? -1 : (my_second() > other.my_second())
  685. ? 1 : (my_second() < other.my_second())
  686. ? -1 : 0;
  687. }
  688. }
  689. // TBD: Exactly what compilers/language-standards are needed to make this constexpr?
  690. // TBD: It odes not really become constexpr until we stop using an intermediate
  691. // cpp_bin_float anyway. But I will leave this comment for future library evolution.
  692. auto str(std::streamsize number_of_digits, const std::ios::fmtflags format_flags) const -> std::string
  693. {
  694. // Use cpp_bin_float when writing to string. This is similar
  695. // to the use of cpp_bin_float when reading from string.
  696. cpp_bin_float_read_write_backend_type f_bin { data.first };
  697. eval_add(f_bin, cpp_bin_float_read_write_backend_type(data.second));
  698. return f_bin.str(number_of_digits, format_flags);
  699. }
  700. constexpr auto order02() const -> int
  701. {
  702. // TBD: Is there another option to get the base-2 log
  703. // that's more unequivocally closer to constexpr?
  704. // TBD: Either way, integrate this (or something like it)
  705. // into any potential implementation of eval_ilogb().
  706. int e2 { };
  707. cpp_double_fp_backend dummy { };
  708. eval_frexp(dummy, *this, &e2);
  709. return e2;
  710. }
  711. static constexpr auto my_value_max() noexcept -> cpp_double_fp_backend
  712. {
  713. // Use the non-normalized sum of two maximum values, where the lower
  714. // value is "shifted" right in the sense of floating-point ldexp.
  715. return
  716. cpp_double_fp_backend
  717. (
  718. arithmetic::two_hilo_sum
  719. (
  720. float_type
  721. (
  722. (cpp_df_qf_detail::ccmath::numeric_limits<float_type>::max)()
  723. * (
  724. static_cast<float_type>(1.0F)
  725. - static_cast<float_type>(1.5F) * cpp_df_qf_detail::ccmath::sqrt(cpp_df_qf_detail::ccmath::numeric_limits<float_type>::epsilon())
  726. )
  727. ),
  728. float_type
  729. (
  730. cpp_df_qf_detail::ccmath::ldexp
  731. (
  732. (cpp_df_qf_detail::ccmath::numeric_limits<float_type>::max)(),
  733. -cpp_df_qf_detail::ccmath::numeric_limits<float_type>::digits
  734. )
  735. )
  736. )
  737. );
  738. }
  739. static constexpr auto my_value_min() noexcept -> cpp_double_fp_backend
  740. {
  741. // Use the non-normalized minimum value, where the lower value
  742. // is "shifted" left in the sense of floating-point ldexp.
  743. return
  744. cpp_double_fp_backend
  745. (
  746. float_type
  747. (
  748. cpp_df_qf_detail::ccmath::ldexp
  749. (
  750. (cpp_df_qf_detail::ccmath::numeric_limits<float_type>::min)(),
  751. cpp_df_qf_detail::ccmath::numeric_limits<float_type>::digits
  752. )
  753. )
  754. );
  755. }
  756. static constexpr auto my_value_eps() noexcept -> cpp_double_fp_backend
  757. {
  758. return
  759. cpp_double_fp_backend
  760. (
  761. float_type(cpp_df_qf_detail::ccmath::ldexp(float_type { 1 }, int { 3 - my_digits }))
  762. );
  763. }
  764. static constexpr auto my_value_nan() noexcept -> cpp_double_fp_backend
  765. {
  766. return cpp_double_fp_backend(static_cast<float_type>(NAN), static_cast<float_type>(0.0F));
  767. }
  768. static constexpr auto my_value_inf() noexcept -> cpp_double_fp_backend
  769. {
  770. return cpp_double_fp_backend(static_cast<float_type>(HUGE_VAL), static_cast<float_type>(0.0F)); // conversion from double infinity OK
  771. }
  772. static constexpr auto my_value_logmax() -> cpp_double_fp_backend
  773. {
  774. return
  775. cpp_double_fp_backend
  776. (
  777. cpp_df_qf_detail::ccmath::log
  778. (
  779. float_type
  780. (
  781. (cpp_df_qf_detail::ccmath::numeric_limits<float_type>::max)()
  782. * (
  783. static_cast<float_type>(1.0F)
  784. - static_cast<float_type>(1.5F) * cpp_df_qf_detail::ccmath::sqrt(cpp_df_qf_detail::ccmath::numeric_limits<float_type>::epsilon())
  785. )
  786. )
  787. )
  788. );
  789. }
  790. static constexpr auto my_value_logmin() -> cpp_double_fp_backend
  791. {
  792. return
  793. cpp_double_fp_backend
  794. (
  795. cpp_df_qf_detail::ccmath::log
  796. (
  797. float_type
  798. (
  799. cpp_df_qf_detail::ccmath::ldexp
  800. (
  801. (cpp_df_qf_detail::ccmath::numeric_limits<float_type>::min)(),
  802. cpp_df_qf_detail::ccmath::numeric_limits<float_type>::digits
  803. )
  804. )
  805. )
  806. );
  807. }
  808. constexpr auto add_unchecked_limb(const float_type v_first) -> void
  809. {
  810. const float_type thi { data.second };
  811. data = arithmetic::two_sum(data.first, v_first);
  812. data = arithmetic::two_hilo_sum(data.first, data.second + thi);
  813. data = arithmetic::two_hilo_sum(data.first, data.second);
  814. }
  815. private:
  816. rep_type data;
  817. using cpp_bin_float_read_write_backend_type = boost::multiprecision::backends::cpp_bin_float<static_cast<unsigned>(my_digits), digit_base_2, void, int, cpp_df_qf_detail::ccmath::numeric_limits<float_type>::min_exponent, cpp_df_qf_detail::ccmath::numeric_limits<float_type>::max_exponent>;
  818. constexpr auto rd_string(const char* p_str) -> bool;
  819. constexpr auto add_unchecked(const cpp_double_fp_backend& v) -> void
  820. {
  821. const rep_type thi_tlo { arithmetic::two_sum(data.second, v.data.second) };
  822. data = arithmetic::two_sum(data.first, v.data.first);
  823. if (cpp_df_qf_detail::ccmath::isinf(data.first))
  824. {
  825. // Handle overflow.
  826. const bool b_neg { (data.first < float_type { 0.0F }) };
  827. *this = cpp_double_fp_backend::my_value_inf();
  828. if (b_neg)
  829. {
  830. negate();
  831. }
  832. return;
  833. }
  834. data = arithmetic::two_hilo_sum(data.first, data.second + thi_tlo.first);
  835. data = arithmetic::two_hilo_sum(data.first, thi_tlo.second + data.second);
  836. }
  837. constexpr auto mul_unchecked(const cpp_double_fp_backend& v) -> void
  838. {
  839. // The multiplication algorithm has been taken from Victor Shoup,
  840. // package WinNTL-5_3_2. It might originally be related to the
  841. // K. Briggs work. The algorithm has been significantly simplified
  842. // while still attempting to retain proper rounding corrections.
  843. // Checks for overflow and underflow have been added.
  844. float_type C { cpp_df_qf_detail::split_maker<float_type>::value * data.first };
  845. float_type hu { };
  846. if (cpp_df_qf_detail::ccmath::isinf(C))
  847. {
  848. // Handle overflow by scaling down (and then back up) with the split.
  849. C = data.first - cpp_df_qf_detail::ccmath::ldexp(data.first, -cpp_df_qf_detail::split_maker<float_type>::n_shl);
  850. hu = cpp_df_qf_detail::ccmath::ldexp(data.first - C, cpp_df_qf_detail::split_maker<float_type>::n_shl);
  851. }
  852. else
  853. {
  854. hu = C - float_type { C - data.first };
  855. }
  856. C = data.first * v.data.first;
  857. if (cpp_df_qf_detail::ccmath::isinf(C))
  858. {
  859. // Handle overflow.
  860. const bool b_neg { (isneg_unchecked() != v.isneg_unchecked()) };
  861. *this = cpp_double_fp_backend::my_value_inf();
  862. if (b_neg)
  863. {
  864. negate();
  865. }
  866. return;
  867. }
  868. float_type c { cpp_df_qf_detail::split_maker<float_type>::value * v.data.first };
  869. float_type hv { };
  870. if (cpp_df_qf_detail::ccmath::isinf(c))
  871. {
  872. // Handle overflow by scaling down (and then back up) with the split.
  873. c = v.data.first - cpp_df_qf_detail::ccmath::ldexp(v.data.first, -cpp_df_qf_detail::split_maker<float_type>::n_shl);
  874. hv = cpp_df_qf_detail::ccmath::ldexp(v.data.first - c, cpp_df_qf_detail::split_maker<float_type>::n_shl);
  875. }
  876. else
  877. {
  878. hv = c - float_type { c - v.data.first };
  879. }
  880. {
  881. const float_type tv { v.data.first - hv };
  882. const float_type
  883. t1
  884. {
  885. cpp_df_qf_detail::ccmath::unsafe::fma
  886. (
  887. hu,
  888. tv,
  889. cpp_df_qf_detail::ccmath::unsafe::fma(hu, hv, -C)
  890. )
  891. };
  892. const float_type tu { data.first - hu };
  893. c = cpp_df_qf_detail::ccmath::unsafe::fma(tu, tv, cpp_df_qf_detail::ccmath::unsafe::fma(tu, hv, t1))
  894. + (data.first * v.data.second)
  895. + (data.second * v.data.first);
  896. }
  897. // Perform even more simplifications compared to Victor Shoup.
  898. data.first = C + c;
  899. data.second = float_type { C - data.first } + c;
  900. }
  901. template <typename OtherFloatingPointType,
  902. typename ::std::enable_if<(cpp_df_qf_detail::is_floating_point<OtherFloatingPointType>::value && ((cpp_df_qf_detail::ccmath::numeric_limits<OtherFloatingPointType>::digits10 * 2) < 16))>::type const*>
  903. friend constexpr auto eval_exp(cpp_double_fp_backend<OtherFloatingPointType>& result, const cpp_double_fp_backend<OtherFloatingPointType>& x) -> void;
  904. template <typename OtherFloatingPointType,
  905. typename ::std::enable_if<(cpp_df_qf_detail::is_floating_point<OtherFloatingPointType>::value && (((cpp_df_qf_detail::ccmath::numeric_limits<OtherFloatingPointType>::digits10 * 2) >= 16) && ((cpp_df_qf_detail::ccmath::numeric_limits<OtherFloatingPointType>::digits10 * 2) <= 36)))>::type const*>
  906. friend constexpr auto eval_exp(cpp_double_fp_backend<OtherFloatingPointType>& result, const cpp_double_fp_backend<OtherFloatingPointType>& x) -> void;
  907. template <typename OtherFloatingPointType,
  908. typename ::std::enable_if<(cpp_df_qf_detail::is_floating_point<OtherFloatingPointType>::value && ((cpp_df_qf_detail::ccmath::numeric_limits<OtherFloatingPointType>::digits10 * 2) > 36))>::type const*>
  909. friend constexpr auto eval_exp(cpp_double_fp_backend<OtherFloatingPointType>& result, const cpp_double_fp_backend<OtherFloatingPointType>& x) -> void;
  910. };
  911. template <typename FloatingPointType>
  912. constexpr auto cpp_double_fp_backend<FloatingPointType>::rd_string(const char* p_str) -> bool
  913. {
  914. // Use an intermediate cpp_bin_float backend type for reading string input.
  915. cpp_bin_float_read_write_backend_type f_bin { };
  916. f_bin = p_str;
  917. const int fpc { eval_fpclassify(f_bin) };
  918. const bool is_definitely_nan { (fpc == FP_NAN) };
  919. using local_double_fp_type = cpp_double_fp_backend<FloatingPointType>;
  920. if (is_definitely_nan)
  921. {
  922. static_cast<void>(operator=(local_double_fp_type::my_value_nan()));
  923. return true;
  924. }
  925. const bool b_neg { (eval_signbit(f_bin) == 1) };
  926. if (b_neg) { f_bin.negate(); }
  927. const int
  928. expval_from_f_bin
  929. {
  930. [&f_bin]()
  931. {
  932. int expval { };
  933. cpp_bin_float_read_write_backend_type dummy { };
  934. eval_frexp(dummy, f_bin, &expval);
  935. return expval;
  936. }()
  937. };
  938. const auto is_zero_or_subnormal =
  939. (
  940. (fpc == FP_ZERO)
  941. || (expval_from_f_bin < static_cast<typename cpp_bin_float_read_write_backend_type::exponent_type>(local_double_fp_type::my_min_exponent))
  942. );
  943. if (is_zero_or_subnormal)
  944. {
  945. data.first = float_type { 0.0F };
  946. data.second = float_type { 0.0F };
  947. return true;
  948. }
  949. float_type flt_inf_check_first { };
  950. eval_convert_to(&flt_inf_check_first, f_bin);
  951. bool is_definitely_inf { ((fpc == FP_INFINITE) || cpp_df_qf_detail::ccmath::isinf(flt_inf_check_first)) };
  952. if ((!is_definitely_inf) && (flt_inf_check_first > my_value_max().my_first()))
  953. {
  954. cpp_bin_float_read_write_backend_type f_bin_inf_check(f_bin);
  955. eval_subtract(f_bin_inf_check, cpp_bin_float_read_write_backend_type(flt_inf_check_first));
  956. float_type flt_inf_check_second { };
  957. eval_convert_to(&flt_inf_check_second, f_bin_inf_check);
  958. is_definitely_inf = eval_gt(local_double_fp_type(flt_inf_check_first, flt_inf_check_second), my_value_max());
  959. };
  960. if (is_definitely_inf)
  961. {
  962. static_cast<void>(operator=(local_double_fp_type::my_value_inf()));
  963. if (b_neg)
  964. {
  965. negate();
  966. }
  967. return true;
  968. }
  969. // The input string is normal. We will now extract its value.
  970. data.first = float_type { 0.0F };
  971. data.second = float_type { 0.0F };
  972. // Handle small input values. Scale (and re-scale them below) if possible.
  973. constexpr int pow2_scaling_for_small_input { cpp_df_qf_detail::ccmath::numeric_limits<float_type>::digits };
  974. const bool
  975. has_pow2_scaling_for_small_input
  976. {
  977. (expval_from_f_bin < static_cast<int>(local_double_fp_type::my_min_exponent + pow2_scaling_for_small_input))
  978. };
  979. if (has_pow2_scaling_for_small_input)
  980. {
  981. eval_ldexp(f_bin, f_bin, pow2_scaling_for_small_input);
  982. }
  983. using local_builtin_float_type = typename std::conditional<(sizeof(float_type) <= sizeof(double)), double, float_type>::type;
  984. constexpr unsigned
  985. digit_limit
  986. {
  987. static_cast<unsigned>
  988. (
  989. static_cast<int>
  990. (
  991. (local_double_fp_type::my_digits / cpp_df_qf_detail::ccmath::numeric_limits<local_builtin_float_type>::digits)
  992. + (((local_double_fp_type::my_digits % cpp_df_qf_detail::ccmath::numeric_limits<local_builtin_float_type>::digits) != 0) ? 1 : 0)
  993. )
  994. * cpp_df_qf_detail::ccmath::numeric_limits<local_builtin_float_type>::digits
  995. )
  996. };
  997. for(auto i = static_cast<unsigned>(UINT8_C(0));
  998. i < digit_limit;
  999. i = static_cast<unsigned>(i + static_cast<unsigned>(cpp_df_qf_detail::ccmath::numeric_limits<local_builtin_float_type>::digits)))
  1000. {
  1001. local_builtin_float_type flt_part { };
  1002. eval_convert_to(&flt_part, f_bin);
  1003. eval_subtract(f_bin, cpp_bin_float_read_write_backend_type(flt_part));
  1004. eval_add(*this, local_double_fp_type { flt_part });
  1005. }
  1006. if (has_pow2_scaling_for_small_input)
  1007. {
  1008. eval_ldexp(*this, *this, -pow2_scaling_for_small_input);
  1009. }
  1010. if (b_neg) { negate(); }
  1011. return true;
  1012. }
  1013. } } } // namespace boost::multiprecision::backends
  1014. #include <boost/multiprecision/cpp_df_qf/cpp_df_qf_detail_constants.hpp>
  1015. namespace boost { namespace multiprecision { namespace backends {
  1016. template <typename FloatingPointType>
  1017. constexpr int cpp_double_fp_backend<FloatingPointType>::my_digits;
  1018. template <typename FloatingPointType>
  1019. constexpr int cpp_double_fp_backend<FloatingPointType>::my_digits10;
  1020. template <typename FloatingPointType>
  1021. constexpr int cpp_double_fp_backend<FloatingPointType>::my_max_digits10;
  1022. template <typename FloatingPointType>
  1023. constexpr int cpp_double_fp_backend<FloatingPointType>::my_max_exponent;
  1024. template <typename FloatingPointType>
  1025. constexpr int cpp_double_fp_backend<FloatingPointType>::my_min_exponent;
  1026. template <typename FloatingPointType>
  1027. constexpr int cpp_double_fp_backend<FloatingPointType>::my_max_exponent10;
  1028. template <typename FloatingPointType>
  1029. constexpr int cpp_double_fp_backend<FloatingPointType>::my_min_exponent10;
  1030. template <typename FloatingPointType>
  1031. constexpr cpp_double_fp_backend<FloatingPointType> operator+(const cpp_double_fp_backend<FloatingPointType>& a, const cpp_double_fp_backend<FloatingPointType>& b) { return cpp_double_fp_backend<FloatingPointType>(a) += b; }
  1032. template <typename FloatingPointType>
  1033. constexpr cpp_double_fp_backend<FloatingPointType> operator-(const cpp_double_fp_backend<FloatingPointType>& a, const cpp_double_fp_backend<FloatingPointType>& b) { return cpp_double_fp_backend<FloatingPointType>(a) -= b; }
  1034. template <typename FloatingPointType>
  1035. constexpr cpp_double_fp_backend<FloatingPointType> operator*(const cpp_double_fp_backend<FloatingPointType>& a, const cpp_double_fp_backend<FloatingPointType>& b) { return cpp_double_fp_backend<FloatingPointType>(a) *= b; }
  1036. template <typename FloatingPointType>
  1037. constexpr cpp_double_fp_backend<FloatingPointType> operator/(const cpp_double_fp_backend<FloatingPointType>& a, const cpp_double_fp_backend<FloatingPointType>& b) { return cpp_double_fp_backend<FloatingPointType>(a) /= b; }
  1038. template <typename FloatingPointType>
  1039. constexpr auto eval_add(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& x) -> void { result += x; }
  1040. template <typename FloatingPointType>
  1041. constexpr auto eval_add(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& a, const cpp_double_fp_backend<FloatingPointType>& b) -> void { result = cpp_double_fp_backend<FloatingPointType>(a) += b; }
  1042. template <typename FloatingPointType>
  1043. constexpr auto eval_subtract(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& x) -> void { result -= x; }
  1044. template <typename FloatingPointType>
  1045. constexpr auto eval_subtract(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& a, const cpp_double_fp_backend<FloatingPointType>& b) -> void { result = cpp_double_fp_backend<FloatingPointType>(a) -= b; }
  1046. template <typename FloatingPointType>
  1047. constexpr auto eval_multiply(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& x) -> void { result *= x; }
  1048. template <typename FloatingPointType>
  1049. constexpr auto eval_multiply(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& a, const cpp_double_fp_backend<FloatingPointType>& b) -> void { result = cpp_double_fp_backend<FloatingPointType>(a) *= b; }
  1050. template <typename FloatingPointType>
  1051. constexpr auto eval_divide(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& x) -> void { result /= x; }
  1052. template <typename FloatingPointType>
  1053. constexpr auto eval_divide(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& a, const cpp_double_fp_backend<FloatingPointType>& b) -> void { result = cpp_double_fp_backend<FloatingPointType>(a) /= b; }
  1054. template <typename FloatingPointType>
  1055. constexpr auto eval_eq(const cpp_double_fp_backend<FloatingPointType>& a, const cpp_double_fp_backend<FloatingPointType>& b) -> bool { return (a.compare(b) == 0); }
  1056. template <typename FloatingPointType>
  1057. constexpr auto eval_lt(const cpp_double_fp_backend<FloatingPointType>& a, const cpp_double_fp_backend<FloatingPointType>& b) -> bool { return (a.compare(b) == -1); }
  1058. template <typename FloatingPointType>
  1059. constexpr auto eval_gt(const cpp_double_fp_backend<FloatingPointType>& a, const cpp_double_fp_backend<FloatingPointType>& b) -> bool { return (a.compare(b) == 1); }
  1060. template <typename FloatingPointType>
  1061. constexpr auto eval_is_zero(const cpp_double_fp_backend<FloatingPointType>& x) -> bool
  1062. {
  1063. return x.iszero_unchecked();
  1064. }
  1065. template <typename FloatingPointType>
  1066. constexpr auto eval_signbit(const cpp_double_fp_backend<FloatingPointType>& x) -> int
  1067. {
  1068. return (x.isneg_unchecked() ? 1 : 0);
  1069. }
  1070. template <typename FloatingPointType>
  1071. constexpr auto eval_fabs(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& a) -> void
  1072. {
  1073. result = a;
  1074. if (a.isneg_unchecked())
  1075. {
  1076. result.negate();
  1077. }
  1078. }
  1079. template <typename FloatingPointType>
  1080. constexpr auto eval_frexp(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& a, int* v) -> void
  1081. {
  1082. using local_backend_type = cpp_double_fp_backend<FloatingPointType>;
  1083. using local_float_type = typename local_backend_type::float_type;
  1084. int expptr { };
  1085. const local_float_type fhi { cpp_df_qf_detail::ccmath::frexp(a.rep().first, &expptr) };
  1086. const local_float_type flo { cpp_df_qf_detail::ccmath::ldexp(a.rep().second, -expptr) };
  1087. if (v != nullptr)
  1088. {
  1089. *v = expptr;
  1090. }
  1091. result.rep() = local_backend_type::arithmetic::normalize(fhi, flo);
  1092. }
  1093. template <typename FloatingPointType>
  1094. constexpr auto eval_ldexp(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& a, int v) -> void
  1095. {
  1096. using local_backend_type = cpp_double_fp_backend<FloatingPointType>;
  1097. using local_float_type = typename local_backend_type::float_type;
  1098. const local_float_type fhi { cpp_df_qf_detail::ccmath::ldexp(a.crep().first, v) };
  1099. const local_float_type flo { cpp_df_qf_detail::ccmath::ldexp(a.crep().second, v) };
  1100. result.rep() = local_backend_type::arithmetic::normalize(fhi, flo);
  1101. }
  1102. template <typename FloatingPointType>
  1103. constexpr auto eval_floor(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& x) -> void
  1104. {
  1105. using local_backend_type = cpp_double_fp_backend<FloatingPointType>;
  1106. using local_float_type = typename local_backend_type::float_type;
  1107. const local_float_type fhi { cpp_df_qf_detail::ccmath::floor(x.my_first()) };
  1108. if (fhi != x.my_first())
  1109. {
  1110. result.rep().first = fhi;
  1111. result.rep().second = local_float_type { 0 };
  1112. }
  1113. else
  1114. {
  1115. const local_float_type flo = { cpp_df_qf_detail::ccmath::floor(x.my_second()) };
  1116. result.rep() = local_backend_type::arithmetic::normalize(fhi, flo);
  1117. }
  1118. }
  1119. template <typename FloatingPointType>
  1120. constexpr auto eval_ceil(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& x) -> void
  1121. {
  1122. // Compute -floor(-x);
  1123. eval_floor(result, -x);
  1124. result.negate();
  1125. }
  1126. template <typename FloatingPointType>
  1127. constexpr auto eval_fpclassify(const cpp_double_fp_backend<FloatingPointType>& o) -> int
  1128. {
  1129. if (cpp_df_qf_detail::ccmath::isnan(o.crep().first)) { return FP_NAN; }
  1130. else if (cpp_df_qf_detail::ccmath::isinf(o.crep().first)) { return FP_INFINITE; }
  1131. else if (eval_is_zero(o)) { return FP_ZERO; }
  1132. else
  1133. {
  1134. using local_backend_type = cpp_double_fp_backend<FloatingPointType>;
  1135. using local_float_type = typename local_backend_type::float_type;
  1136. const local_float_type fabs_x { cpp_df_qf_detail::ccmath::fabs(o.crep().first) };
  1137. if ((fabs_x > 0) && (fabs_x < (cpp_df_qf_detail::ccmath::numeric_limits<local_float_type>::min)()))
  1138. {
  1139. return FP_SUBNORMAL;
  1140. }
  1141. else
  1142. {
  1143. return FP_NORMAL;
  1144. }
  1145. }
  1146. }
  1147. template <typename FloatingPointType>
  1148. constexpr auto eval_sqrt(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& o) -> void
  1149. {
  1150. using double_float_type = cpp_double_fp_backend<FloatingPointType>;
  1151. using local_float_type = typename double_float_type::float_type;
  1152. const int fpc { eval_fpclassify(o) };
  1153. const bool isneg_o { o.isneg_unchecked() };
  1154. if ((fpc != FP_NORMAL) || isneg_o)
  1155. {
  1156. if (fpc == FP_ZERO)
  1157. {
  1158. result = double_float_type(0);
  1159. return;
  1160. }
  1161. else if ((fpc == FP_NAN) || isneg_o)
  1162. {
  1163. result = double_float_type::my_value_nan();
  1164. return;
  1165. }
  1166. else if (fpc == FP_INFINITE)
  1167. {
  1168. result = double_float_type::my_value_inf();
  1169. return;
  1170. }
  1171. }
  1172. // TBD: Do we need any overflow/underflow guards when multiplying
  1173. // by the split or when multiplying (hx * tx) and/or (hx * hx)?
  1174. const local_float_type c { cpp_df_qf_detail::ccmath::sqrt(o.crep().first) };
  1175. local_float_type p { cpp_df_qf_detail::split_maker<local_float_type>::value * c };
  1176. const local_float_type hx { local_float_type { c - p } + p };
  1177. const local_float_type tx { c - hx };
  1178. local_float_type q = hx * tx;
  1179. q = q + q;
  1180. p = hx * hx;
  1181. const local_float_type u { p + q };
  1182. const local_float_type uu { cpp_df_qf_detail::ccmath::unsafe::fma(tx, tx, local_float_type { p - u } + q) };
  1183. const local_float_type
  1184. cc
  1185. {
  1186. local_float_type { local_float_type { o.crep().first - u } - uu + o.crep().second } / local_float_type { c + c }
  1187. };
  1188. result.rep().first = c + cc;
  1189. result.rep().second = local_float_type { c - result.my_first() } + cc;
  1190. }
  1191. template <typename FloatingPointType>
  1192. constexpr auto eval_pow(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& x, const cpp_double_fp_backend<FloatingPointType>& a) -> void
  1193. {
  1194. using double_float_type = cpp_double_fp_backend<FloatingPointType>;
  1195. constexpr double_float_type zero { 0 };
  1196. result = zero;
  1197. signed long long val_nll { };
  1198. eval_convert_to(&val_nll, a);
  1199. const int na { static_cast<int>(val_nll) };
  1200. const int fpc_a { eval_fpclassify(a) };
  1201. if((fpc_a == FP_NORMAL) && (a.compare(double_float_type(na)) == 0))
  1202. {
  1203. eval_pow(result, x, na);
  1204. }
  1205. else
  1206. {
  1207. constexpr double_float_type one { 1 };
  1208. const int fpc_x { eval_fpclassify(x) };
  1209. if (fpc_a == FP_ZERO)
  1210. {
  1211. // pow(base, +/-0) returns 1 for any base, even when base is NaN.
  1212. result = one;
  1213. }
  1214. else if (fpc_x == FP_ZERO)
  1215. {
  1216. if ((fpc_a == FP_NORMAL) || (fpc_a == FP_INFINITE))
  1217. {
  1218. // pow(+/-0, exp), where exp is negative and finite, returns +infinity.
  1219. // pow(+/-0, exp), where exp is positive non-integer, returns +0.
  1220. // pow(+/-0, -infinity) returns +infinity.
  1221. // pow(+/-0, +infinity) returns +0.
  1222. result = (eval_signbit(a) ? double_float_type::my_value_inf() : zero);
  1223. }
  1224. else if (fpc_a == FP_NAN)
  1225. {
  1226. result = double_float_type::my_value_nan();
  1227. }
  1228. }
  1229. else if (fpc_x == FP_INFINITE)
  1230. {
  1231. if ((fpc_a == FP_NORMAL) || (fpc_a == FP_INFINITE))
  1232. {
  1233. // pow(+infinity, exp) returns +0 for any negative exp.
  1234. // pow(-infinity, exp) returns +infinity for any positive exp.
  1235. result = (eval_signbit(a) ? zero : double_float_type::my_value_inf());
  1236. }
  1237. else if (fpc_a == FP_NAN)
  1238. {
  1239. result = double_float_type::my_value_nan();
  1240. }
  1241. }
  1242. else if (fpc_x != FP_NORMAL)
  1243. {
  1244. result = x;
  1245. }
  1246. else
  1247. {
  1248. if (fpc_a == FP_INFINITE)
  1249. {
  1250. constexpr double_float_type one_minus { -1 };
  1251. if (x.compare(one_minus) == 0)
  1252. {
  1253. result = one;
  1254. }
  1255. else
  1256. {
  1257. double_float_type xabs { };
  1258. eval_fabs(xabs, x);
  1259. const int compare_one_result { xabs.compare(one) };
  1260. result =
  1261. (
  1262. (compare_one_result < 0) ? (eval_signbit(a) ? double_float_type::my_value_inf() : zero)
  1263. : (compare_one_result > 0) ? (eval_signbit(a) ? zero : double_float_type::my_value_inf())
  1264. : one
  1265. );
  1266. }
  1267. }
  1268. else if (fpc_a == FP_NAN)
  1269. {
  1270. result = (x.compare(one) == 0) ? one : double_float_type::my_value_nan();
  1271. }
  1272. else
  1273. {
  1274. double_float_type log_x { };
  1275. eval_log(log_x, x);
  1276. double_float_type a_log_x { };
  1277. eval_multiply(a_log_x, a, log_x);
  1278. eval_exp(result, a_log_x);
  1279. }
  1280. }
  1281. }
  1282. }
  1283. template <typename FloatingPointType,
  1284. typename IntegralType>
  1285. constexpr auto eval_pow(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& x, IntegralType p) -> typename ::std::enable_if<::boost::multiprecision::detail::is_integral<IntegralType>::value, void>::type
  1286. {
  1287. const int fpc { eval_fpclassify(x) };
  1288. using double_float_type = cpp_double_fp_backend<FloatingPointType>;
  1289. using local_integral_type = IntegralType;
  1290. const bool p_is_odd { (static_cast<local_integral_type>(p & 1) != static_cast<local_integral_type>(0)) };
  1291. if (p == static_cast<local_integral_type>(0))
  1292. {
  1293. // pow(base, +/-0) returns 1 for any base, even when base is NaN.
  1294. result = double_float_type { unsigned { UINT8_C(1) } };
  1295. }
  1296. else if (fpc != FP_NORMAL)
  1297. {
  1298. if (fpc == FP_ZERO)
  1299. {
  1300. // pow( +0, exp), where exp is a negative odd integer, returns +infinity.
  1301. // pow( -0, exp), where exp is a negative odd integer, returns +infinity.
  1302. // pow(+/-0, exp), where exp is a negative even integer, returns +infinity.
  1303. // pow( +0, exp), where exp is a positive odd integer, returns +0.
  1304. // pow( -0, exp), where exp is a positive odd integer, returns -0.
  1305. // pow(+/-0, exp), where exp is a positive even integer, returns +0.
  1306. result = ((p < static_cast<local_integral_type>(0)) ? double_float_type::my_value_inf() : double_float_type { unsigned { UINT8_C(0) } });
  1307. }
  1308. else if (fpc == FP_INFINITE)
  1309. {
  1310. if (eval_signbit(x))
  1311. {
  1312. if (p < static_cast<local_integral_type>(0))
  1313. {
  1314. // pow(-infinity, exp) returns -0 if exp is a negative odd integer.
  1315. // pow(-infinity, exp) returns +0 if exp is a negative even integer.
  1316. result = double_float_type { unsigned { UINT8_C(0) } };
  1317. }
  1318. else
  1319. {
  1320. // pow(-infinity, exp) returns -infinity if exp is a positive odd integer.
  1321. // pow(-infinity, exp) returns +infinity if exp is a positive even integer.
  1322. result = (p_is_odd ? -double_float_type::my_value_inf() : double_float_type::my_value_inf());
  1323. }
  1324. }
  1325. else
  1326. {
  1327. // pow(+infinity, exp) returns +0 for any negative exp.
  1328. // pow(+infinity, exp) returns +infinity for any positive exp.
  1329. result = ((p < static_cast<local_integral_type>(0)) ? double_float_type { unsigned { UINT8_C(0) } } : double_float_type::my_value_inf());
  1330. }
  1331. }
  1332. else
  1333. {
  1334. result = double_float_type::my_value_nan();
  1335. }
  1336. }
  1337. else
  1338. {
  1339. double_float_type::pown(result, x, p);
  1340. }
  1341. }
  1342. template <typename FloatingPointType,
  1343. typename ::std::enable_if<(cpp_df_qf_detail::is_floating_point<FloatingPointType>::value && ((cpp_df_qf_detail::ccmath::numeric_limits<FloatingPointType>::digits10 * 2) < 16))>::type const*>
  1344. constexpr auto eval_exp(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& x) -> void
  1345. {
  1346. using double_float_type = cpp_double_fp_backend<FloatingPointType>;
  1347. constexpr double_float_type one { 1 };
  1348. const int fpc { eval_fpclassify(x) };
  1349. const bool b_neg { x.isneg_unchecked() };
  1350. if (fpc == FP_ZERO)
  1351. {
  1352. result = one;
  1353. }
  1354. else if (fpc != FP_NORMAL)
  1355. {
  1356. if (fpc == FP_INFINITE)
  1357. {
  1358. result = (b_neg ? double_float_type { 0.0F } : double_float_type::my_value_inf());
  1359. }
  1360. else if (fpc == FP_NAN)
  1361. {
  1362. result = x;
  1363. }
  1364. }
  1365. else
  1366. {
  1367. using local_float_type = typename double_float_type::float_type;
  1368. // Get a local copy of the argument and force it to be positive.
  1369. const double_float_type xx { (!b_neg) ? x : -x };
  1370. // Check the range of the input.
  1371. if (eval_lt(x, double_float_type::my_value_logmin()))
  1372. {
  1373. result = double_float_type(0U);
  1374. }
  1375. else if (eval_gt(xx, double_float_type::my_value_logmax()))
  1376. {
  1377. result = double_float_type::my_value_inf();
  1378. }
  1379. else if (xx.is_one())
  1380. {
  1381. if(!b_neg)
  1382. {
  1383. result = cpp_df_qf_detail::constant_df_exp1<local_float_type>();
  1384. }
  1385. else
  1386. {
  1387. eval_divide(result, one, cpp_df_qf_detail::constant_df_exp1<local_float_type>());
  1388. }
  1389. }
  1390. else
  1391. {
  1392. // Use an argument reduction algorithm for exp() in classic MPFUN-style.
  1393. double_float_type nf { };
  1394. // Prepare the scaled variables.
  1395. const bool b_scale { (xx.order02() > -1) };
  1396. double_float_type r { };
  1397. if (b_scale)
  1398. {
  1399. eval_floor(nf, xx / cpp_df_qf_detail::constant_df_ln_two<local_float_type>());
  1400. eval_ldexp(r, xx - (nf * cpp_df_qf_detail::constant_df_ln_two<local_float_type>()), -2);
  1401. }
  1402. else
  1403. {
  1404. r = xx;
  1405. }
  1406. // PadeApproximant[Exp[x] - 1, {x, 0, {6, 6}}]
  1407. // FullSimplify[%]
  1408. // (84 x (7920 + 240 x^2 + x^4))
  1409. // / (665280 + x (-332640 + x (75600 + x (-10080 + x (840 + (-42 + x) x)))))
  1410. constexpr double_float_type n84(84);
  1411. constexpr double_float_type n240(240);
  1412. constexpr double_float_type n7920(7920);
  1413. constexpr double_float_type n665280(665280);
  1414. constexpr double_float_type n332640(332640);
  1415. constexpr double_float_type n75600(75600);
  1416. constexpr double_float_type n10080(10080);
  1417. constexpr double_float_type n840(840);
  1418. constexpr double_float_type n42(42);
  1419. const double_float_type r2 { r * r };
  1420. // Use the small-argument Pade approximation having coefficients shown above.
  1421. result = (n84 * r * (n7920 + (n240 + r2) * r2));
  1422. const double_float_type bot = (n665280 + r * (-n332640 + r * (n75600 + r * (-n10080 + r * (n840 + (-n42 + r) * r)))));
  1423. eval_divide(result, bot);
  1424. result.add_unchecked_limb(local_float_type { 1.0F });
  1425. // Rescale the result.
  1426. if (b_scale)
  1427. {
  1428. result *= result;
  1429. result *= result;
  1430. signed long long lln { };
  1431. eval_convert_to(&lln, nf);
  1432. const int n { static_cast<int>(lln) };
  1433. if (n > 0)
  1434. {
  1435. eval_ldexp(result, result, n);
  1436. }
  1437. }
  1438. if (b_neg)
  1439. {
  1440. eval_divide(result, one, result);
  1441. }
  1442. }
  1443. }
  1444. }
  1445. template <typename FloatingPointType,
  1446. typename ::std::enable_if<(cpp_df_qf_detail::is_floating_point<FloatingPointType>::value && (((cpp_df_qf_detail::ccmath::numeric_limits<FloatingPointType>::digits10 * 2) >= 16) && ((cpp_df_qf_detail::ccmath::numeric_limits<FloatingPointType>::digits10 * 2) <= 36)))>::type const*>
  1447. constexpr auto eval_exp(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& x) -> void
  1448. {
  1449. using double_float_type = cpp_double_fp_backend<FloatingPointType>;
  1450. constexpr double_float_type one { 1 };
  1451. const int fpc { eval_fpclassify(x) };
  1452. const bool b_neg { x.isneg_unchecked() };
  1453. if (fpc == FP_ZERO)
  1454. {
  1455. result = one;
  1456. }
  1457. else if (fpc != FP_NORMAL)
  1458. {
  1459. if (fpc == FP_INFINITE)
  1460. {
  1461. result = (b_neg ? double_float_type(0) : double_float_type::my_value_inf());
  1462. }
  1463. else if (fpc == FP_NAN)
  1464. {
  1465. result = x;
  1466. }
  1467. }
  1468. else
  1469. {
  1470. using local_float_type = typename double_float_type::float_type;
  1471. // Get a local copy of the argument and force it to be positive.
  1472. const double_float_type xx { (!b_neg) ? x : -x };
  1473. // Check the range of the input.
  1474. if (eval_lt(x, double_float_type::my_value_logmin()))
  1475. {
  1476. result = double_float_type(0U);
  1477. }
  1478. else if (eval_gt(xx, double_float_type::my_value_logmax()))
  1479. {
  1480. result = double_float_type::my_value_inf();
  1481. }
  1482. else if (xx.is_one())
  1483. {
  1484. if(!b_neg)
  1485. {
  1486. result = cpp_df_qf_detail::constant_df_exp1<local_float_type>();
  1487. }
  1488. else
  1489. {
  1490. eval_divide(result, one, cpp_df_qf_detail::constant_df_exp1<local_float_type>());
  1491. }
  1492. }
  1493. else
  1494. {
  1495. // Use an argument reduction algorithm for exp() in classic MPFUN-style.
  1496. double_float_type nf { };
  1497. // Prepare the scaled variables.
  1498. const bool b_scale { (xx.order02() > -4) };
  1499. double_float_type r { };
  1500. if (b_scale)
  1501. {
  1502. eval_floor(nf, xx / cpp_df_qf_detail::constant_df_ln_two<local_float_type>());
  1503. eval_ldexp(r, xx - (nf * cpp_df_qf_detail::constant_df_ln_two<local_float_type>()), -4);
  1504. }
  1505. else
  1506. {
  1507. r = xx;
  1508. }
  1509. // PadeApproximant[Exp[r], {r, 0, 8, 8}]
  1510. // FullSimplify[%]
  1511. constexpr double_float_type n144(144U);
  1512. constexpr double_float_type n3603600(3603600UL);
  1513. constexpr double_float_type n120120(120120UL);
  1514. constexpr double_float_type n770(770U);
  1515. constexpr double_float_type n518918400(518918400UL);
  1516. constexpr double_float_type n259459200(259459200UL);
  1517. constexpr double_float_type n60540480(60540480UL);
  1518. constexpr double_float_type n8648640(8648640UL);
  1519. constexpr double_float_type n831600(831600UL);
  1520. constexpr double_float_type n55440(55440U);
  1521. constexpr double_float_type n2520(2520U);
  1522. constexpr double_float_type n72(72U);
  1523. const double_float_type r2 { r * r };
  1524. result = (n144 * r) * (n3603600 + r2 * (n120120 + r2 * (n770 + r2)));
  1525. const double_float_type bot = (n518918400 + r * (-n259459200 + r * (n60540480 + r * (-n8648640 + r * (n831600 + r * (-n55440 + r * (n2520 + r * (-n72 + r))))))));
  1526. eval_divide(result, bot);
  1527. result.add_unchecked_limb(local_float_type { 1.0F });
  1528. // Rescale the result.
  1529. if (b_scale)
  1530. {
  1531. result *= result;
  1532. result *= result;
  1533. result *= result;
  1534. result *= result;
  1535. signed long long lln { };
  1536. eval_convert_to(&lln, nf);
  1537. const int n { static_cast<int>(lln) };
  1538. if (n > 0)
  1539. {
  1540. eval_ldexp(result, result, n);
  1541. }
  1542. }
  1543. if (b_neg)
  1544. {
  1545. eval_divide(result, one, result);
  1546. }
  1547. }
  1548. }
  1549. }
  1550. template <typename FloatingPointType,
  1551. typename ::std::enable_if<(cpp_df_qf_detail::is_floating_point<FloatingPointType>::value && ((cpp_df_qf_detail::ccmath::numeric_limits<FloatingPointType>::digits10 * 2) > 36))>::type const*>
  1552. constexpr auto eval_exp(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& x) -> void
  1553. {
  1554. using double_float_type = cpp_double_fp_backend<FloatingPointType>;
  1555. constexpr double_float_type one { 1 };
  1556. const int fpc { eval_fpclassify(x) };
  1557. const bool b_neg { x.isneg_unchecked() };
  1558. if (fpc == FP_ZERO)
  1559. {
  1560. result = one;
  1561. }
  1562. else if (fpc != FP_NORMAL)
  1563. {
  1564. if (fpc == FP_INFINITE)
  1565. {
  1566. result = (b_neg ? double_float_type(0) : double_float_type::my_value_inf());
  1567. }
  1568. else if (fpc == FP_NAN)
  1569. {
  1570. result = x;
  1571. }
  1572. }
  1573. else
  1574. {
  1575. using local_float_type = typename double_float_type::float_type;
  1576. // Get a local copy of the argument and force it to be positive.
  1577. const double_float_type xx { (!b_neg) ? x : -x };
  1578. // Check the range of the input.
  1579. if (eval_lt(x, double_float_type::my_value_logmin()))
  1580. {
  1581. result = double_float_type(0U);
  1582. }
  1583. else if (eval_gt(xx, double_float_type::my_value_logmax()))
  1584. {
  1585. result = double_float_type::my_value_inf();
  1586. }
  1587. else if (xx.is_one())
  1588. {
  1589. if(!b_neg)
  1590. {
  1591. result = cpp_df_qf_detail::constant_df_exp1<local_float_type>();
  1592. }
  1593. else
  1594. {
  1595. eval_divide(result, one, cpp_df_qf_detail::constant_df_exp1<local_float_type>());
  1596. }
  1597. }
  1598. else
  1599. {
  1600. // Use an argument reduction algorithm for exp() in classic MPFUN-style.
  1601. double_float_type nf { };
  1602. // Prepare the scaled variables.
  1603. const bool b_scale { (xx.order02() > -4) };
  1604. double_float_type xh { };
  1605. if (b_scale)
  1606. {
  1607. eval_floor(nf, xx / cpp_df_qf_detail::constant_df_ln_two<local_float_type>());
  1608. eval_ldexp(xh, xx - (nf * cpp_df_qf_detail::constant_df_ln_two<local_float_type>()), -4);
  1609. }
  1610. else
  1611. {
  1612. xh = xx;
  1613. }
  1614. double_float_type x_pow_n_div_n_fact(xh);
  1615. result = x_pow_n_div_n_fact;
  1616. result.add_unchecked_limb(local_float_type { 1.0F });
  1617. double_float_type dummy { };
  1618. // Use the Taylor series expansion of hypergeometric_0f0(; ; x).
  1619. // For this high(er) digit count, a scaled argument with subsequent
  1620. // Taylor series expansion is actually more precise than Pade approximation.
  1621. for (unsigned n { UINT8_C(2) }; n < unsigned { UINT8_C(64) }; ++n)
  1622. {
  1623. eval_multiply(x_pow_n_div_n_fact, xh);
  1624. eval_divide(x_pow_n_div_n_fact, double_float_type(n));
  1625. int n_tol { };
  1626. eval_frexp(dummy, x_pow_n_div_n_fact, &n_tol);
  1627. if ((n > 4U) && (n_tol < -(double_float_type::my_digits - 1)))
  1628. {
  1629. break;
  1630. }
  1631. eval_add(result, x_pow_n_div_n_fact);
  1632. }
  1633. // Rescale the result.
  1634. if (b_scale)
  1635. {
  1636. result *= result;
  1637. result *= result;
  1638. result *= result;
  1639. result *= result;
  1640. signed long long lln { };
  1641. eval_convert_to(&lln, nf);
  1642. const int n { static_cast<int>(lln) };
  1643. if (n > 0)
  1644. {
  1645. eval_ldexp(result, result, n);
  1646. }
  1647. }
  1648. if (b_neg)
  1649. {
  1650. eval_divide(result, one, result);
  1651. }
  1652. }
  1653. }
  1654. }
  1655. template <typename FloatingPointType>
  1656. constexpr auto eval_log(cpp_double_fp_backend<FloatingPointType>& result, const cpp_double_fp_backend<FloatingPointType>& x) -> void
  1657. {
  1658. using double_float_type = cpp_double_fp_backend<FloatingPointType>;
  1659. constexpr double_float_type one { 1 };
  1660. const int result_compare_with_one { x.compare(one) };
  1661. const int fpc { eval_fpclassify(x) };
  1662. if (fpc == FP_ZERO)
  1663. {
  1664. result = -double_float_type::my_value_inf();
  1665. }
  1666. else if (x.isneg_unchecked() || (fpc == FP_NAN))
  1667. {
  1668. result = double_float_type::my_value_nan();
  1669. }
  1670. else if (fpc == FP_INFINITE)
  1671. {
  1672. result = double_float_type::my_value_inf();
  1673. }
  1674. else if (result_compare_with_one == -1)
  1675. {
  1676. // Use argument inversion and negation of the result.
  1677. double_float_type x_inv { };
  1678. eval_divide(x_inv, one, x);
  1679. eval_log(result, x_inv);
  1680. result.negate();
  1681. }
  1682. else if (result_compare_with_one == 1)
  1683. {
  1684. // Optimize to only use eval_frexp if (and only if) the exponent is
  1685. // actually large/small in the sense of above/below a defined cutoff.
  1686. double_float_type x2 { };
  1687. int n2 { };
  1688. eval_frexp(x2, x, &n2);
  1689. // Get initial estimate using the self-written, detail math function log.
  1690. using local_float_type = typename double_float_type::float_type;
  1691. const local_float_type s { cpp_df_qf_detail::ccmath::log(x2.my_first()) };
  1692. double_float_type E { };
  1693. eval_exp(E, double_float_type(s));
  1694. // Perform one single step of Newton-Raphson iteration.
  1695. // result = s + (x2 - E) / E;
  1696. eval_subtract(result, x2, E);
  1697. eval_divide(result, E);
  1698. result.add_unchecked_limb(s);
  1699. double_float_type xn2 { n2 };
  1700. eval_multiply(xn2, cpp_df_qf_detail::constant_df_ln_two<local_float_type>());
  1701. eval_add(result, xn2);
  1702. }
  1703. else
  1704. {
  1705. result = double_float_type { 0 };
  1706. }
  1707. }
  1708. namespace detail {
  1709. template<typename DestType, typename FloatingPointType>
  1710. constexpr auto extract(cpp_double_fp_backend<FloatingPointType>& source) -> DestType
  1711. {
  1712. using double_float_type = cpp_double_fp_backend<FloatingPointType>;
  1713. using local_float_type = typename double_float_type::float_type;
  1714. using destination_type = DestType;
  1715. destination_type result { };
  1716. constexpr double_float_type zero { 0 };
  1717. result = static_cast<destination_type>(0);
  1718. unsigned fail_safe { UINT32_C(32) };
  1719. constexpr bool
  1720. destination_type_is_longer
  1721. {
  1722. (std::numeric_limits<signed long long>::digits > cpp_df_qf_detail::ccmath::numeric_limits<local_float_type>::digits)
  1723. };
  1724. using float_extract_type = typename std::conditional<destination_type_is_longer, local_float_type, float>::type;
  1725. while((source.compare(zero) != 0) && (fail_safe > unsigned { UINT8_C(0) }))
  1726. {
  1727. const float_extract_type next_flt_val { static_cast<float_extract_type>(source.my_first()) };
  1728. result += static_cast<destination_type>(next_flt_val);
  1729. eval_subtract(source, double_float_type(next_flt_val));
  1730. --fail_safe;
  1731. }
  1732. return result;
  1733. }
  1734. } // namespace detail
  1735. template <typename FloatingPointType>
  1736. constexpr auto eval_convert_to(signed long long* result, const cpp_double_fp_backend<FloatingPointType>& backend) -> void
  1737. {
  1738. const auto fpc = eval_fpclassify(backend);
  1739. if (fpc != FP_NORMAL)
  1740. {
  1741. *result = static_cast<signed long long>(backend.crep().first);
  1742. }
  1743. else
  1744. {
  1745. using double_float_type = cpp_double_fp_backend<FloatingPointType>;
  1746. using local_float_type = typename double_float_type::float_type;
  1747. static_assert(std::is_same<local_float_type, FloatingPointType>::value, "Error something went wrong with the limb type");
  1748. constexpr bool
  1749. long_long_is_longer
  1750. {
  1751. (std::numeric_limits<signed long long>::digits > cpp_df_qf_detail::ccmath::numeric_limits<local_float_type>::digits)
  1752. };
  1753. using longer_type = typename ::std::conditional<long_long_is_longer, signed long long, double_float_type>::type;
  1754. constexpr longer_type my_max_val { static_cast<longer_type>((std::numeric_limits<signed long long>::max)()) };
  1755. constexpr longer_type my_min_val { static_cast<longer_type>((std::numeric_limits<signed long long>::min)()) };
  1756. constexpr double_float_type my_max_val_dd { static_cast<double_float_type>(my_max_val) };
  1757. constexpr double_float_type my_min_val_dd { static_cast<double_float_type>(my_min_val) };
  1758. if (backend.compare(my_max_val_dd) >= 0)
  1759. {
  1760. *result = (std::numeric_limits<signed long long>::max)();
  1761. }
  1762. else if (backend.compare(my_min_val_dd) <= 0)
  1763. {
  1764. *result = (std::numeric_limits<signed long long>::min)();
  1765. }
  1766. else
  1767. {
  1768. double_float_type source { backend };
  1769. *result = detail::extract<signed long long>(source);
  1770. #if !defined(__x86_64__) && !defined(_M_X64)
  1771. // It has been "empirically found" that non-X64 needs this workaround.
  1772. // Even though the same conditions are met for x86_64 on GCC and MSVC,
  1773. // this workaround will actually break the long long conversion tests
  1774. // on those platforms.
  1775. //
  1776. // Our assumption is that on x64 there is x87 math (double -> long double)
  1777. // being performed in the background. Seemingly these might "aid" the conversion
  1778. // of double value to long long. Somehow I get the feeling this issue will arise
  1779. // in future evolution of the cpp_double_fp_backend.
  1780. //
  1781. // This workaround has been tested on: ARM64 (linux and mac), s390x and PPC64LE.
  1782. constexpr bool
  1783. needs_workaround
  1784. {
  1785. (sizeof(signed long long) == 8U)
  1786. && (std::is_same<local_float_type, double>::value || (std::is_same<local_float_type, long double>::value))
  1787. };
  1788. BOOST_IF_CONSTEXPR (needs_workaround)
  1789. {
  1790. // This is the last value stored in a double as 9223372036854775808
  1791. constexpr signed long long upper_bound = 9223372036854775296LL;
  1792. if (!eval_signbit(backend) && *result >= upper_bound)
  1793. {
  1794. // LONG_MAX is stored with .second = -1, so we compensate for the offset
  1795. // We also only need this at the upper end where the values aren't exactly
  1796. // representable in double. Below a certain point we are fine
  1797. *result += static_cast<signed long long>(1);
  1798. }
  1799. }
  1800. #endif // non-x64
  1801. }
  1802. }
  1803. }
  1804. template <typename FloatingPointType>
  1805. constexpr auto eval_convert_to(unsigned long long* result, const cpp_double_fp_backend<FloatingPointType>& backend) -> void
  1806. {
  1807. const auto fpc = eval_fpclassify(backend);
  1808. if (fpc != FP_NORMAL)
  1809. {
  1810. *result = static_cast<unsigned long long>(backend.crep().first);
  1811. }
  1812. else
  1813. {
  1814. using double_float_type = cpp_double_fp_backend<FloatingPointType>;
  1815. using local_float_type = typename double_float_type::float_type;
  1816. static_assert(std::is_same<local_float_type, FloatingPointType>::value, "Error something went wrong with the limb type");
  1817. constexpr bool
  1818. ulong_long_is_longer
  1819. {
  1820. (std::numeric_limits<unsigned long long>::digits > cpp_df_qf_detail::ccmath::numeric_limits<local_float_type>::digits)
  1821. };
  1822. using longer_type = typename ::std::conditional<ulong_long_is_longer, unsigned long long, double_float_type>::type;
  1823. constexpr longer_type my_max_val { static_cast<longer_type>((std::numeric_limits<unsigned long long>::max)()) };
  1824. constexpr double_float_type my_max_val_dd { static_cast<double_float_type>(my_max_val) };
  1825. if (backend.compare(my_max_val_dd) >= 0)
  1826. {
  1827. *result = (std::numeric_limits<unsigned long long>::max)();
  1828. }
  1829. else
  1830. {
  1831. double_float_type source { backend };
  1832. *result = detail::extract<unsigned long long>(source);
  1833. }
  1834. }
  1835. }
  1836. #ifdef BOOST_HAS_INT128
  1837. template <typename FloatingPointType>
  1838. constexpr auto eval_convert_to(boost::int128_type* result, const cpp_double_fp_backend<FloatingPointType>& backend) -> typename std::enable_if<(cpp_df_qf_detail::ccmath::numeric_limits<FloatingPointType>::digits > 24), void>::type
  1839. {
  1840. const auto fpc = eval_fpclassify(backend);
  1841. if (fpc != FP_NORMAL)
  1842. {
  1843. *result = static_cast<boost::int128_type>(backend.crep().first);
  1844. }
  1845. else
  1846. {
  1847. using double_float_type = cpp_double_fp_backend<FloatingPointType>;
  1848. using local_float_type = typename double_float_type::float_type;
  1849. static_assert(std::is_same<local_float_type, FloatingPointType>::value, "Error something went wrong with the limb type");
  1850. constexpr bool
  1851. n128_is_longer
  1852. {
  1853. ((static_cast<int>(sizeof(boost::int128_type) * static_cast<std::size_t>(CHAR_BIT)) - 1) > cpp_df_qf_detail::ccmath::numeric_limits<local_float_type>::digits)
  1854. };
  1855. using longer_type = typename ::std::conditional<n128_is_longer, boost::int128_type, double_float_type>::type;
  1856. constexpr boost::int128_type my_max_val_n128 = (((static_cast<boost::int128_type>(1) << (sizeof(boost::int128_type) * CHAR_BIT - 2)) - 1) << 1) + 1;
  1857. constexpr boost::int128_type my_min_val_n128 = static_cast<boost::int128_type>(-my_max_val_n128 - 1);
  1858. constexpr longer_type my_max_val(static_cast<longer_type>(my_max_val_n128));
  1859. constexpr longer_type my_min_val(static_cast<longer_type>(my_min_val_n128));
  1860. constexpr double_float_type my_max_val_dd(static_cast<double_float_type>(my_max_val));
  1861. constexpr double_float_type my_min_val_dd(static_cast<double_float_type>(my_min_val));
  1862. if (backend.compare(my_max_val_dd) >= 0)
  1863. {
  1864. *result = my_max_val;
  1865. }
  1866. else if (backend.compare(my_min_val_dd) <= 0)
  1867. {
  1868. *result = my_min_val;
  1869. }
  1870. else
  1871. {
  1872. double_float_type source { backend };
  1873. *result = detail::extract<boost::int128_type>(source);
  1874. }
  1875. }
  1876. }
  1877. template <typename FloatingPointType>
  1878. constexpr auto eval_convert_to(boost::int128_type* result, const cpp_double_fp_backend<FloatingPointType>& backend) -> typename std::enable_if<!(cpp_df_qf_detail::ccmath::numeric_limits<FloatingPointType>::digits > 24), void>::type
  1879. {
  1880. const auto fpc = eval_fpclassify(backend);
  1881. if (fpc != FP_NORMAL)
  1882. {
  1883. *result = static_cast<boost::int128_type>(backend.crep().first);
  1884. }
  1885. else
  1886. {
  1887. using double_float_type = cpp_double_fp_backend<FloatingPointType>;
  1888. double_float_type source { backend };
  1889. *result = detail::extract<boost::int128_type>(source);
  1890. }
  1891. }
  1892. template <typename FloatingPointType>
  1893. constexpr auto eval_convert_to(boost::uint128_type* result, const cpp_double_fp_backend<FloatingPointType>& backend) -> typename std::enable_if<(cpp_df_qf_detail::ccmath::numeric_limits<FloatingPointType>::digits > 24), void>::type
  1894. {
  1895. const auto fpc = eval_fpclassify(backend);
  1896. if (fpc != FP_NORMAL)
  1897. {
  1898. *result = static_cast<boost::int128_type>(backend.crep().first);
  1899. }
  1900. else
  1901. {
  1902. using double_float_type = cpp_double_fp_backend<FloatingPointType>;
  1903. using local_float_type = typename double_float_type::float_type;
  1904. static_assert(std::is_same<local_float_type, FloatingPointType>::value, "Error something went wrong with the limb type");
  1905. constexpr bool
  1906. u128_is_longer
  1907. {
  1908. (static_cast<int>(sizeof(boost::uint128_type) * static_cast<std::size_t>(CHAR_BIT)) > cpp_df_qf_detail::ccmath::numeric_limits<local_float_type>::digits)
  1909. };
  1910. using longer_type = typename ::std::conditional<u128_is_longer, boost::uint128_type, double_float_type>::type;
  1911. constexpr boost::uint128_type my_max_val_u128 = static_cast<boost::uint128_type>(~static_cast<boost::uint128_type>(0));
  1912. constexpr longer_type my_max_val(static_cast<longer_type>(my_max_val_u128));
  1913. constexpr double_float_type my_max_val_dd(static_cast<double_float_type>(my_max_val));
  1914. if (backend.compare(my_max_val_dd) >= 0)
  1915. {
  1916. *result = my_max_val;
  1917. }
  1918. else
  1919. {
  1920. double_float_type source { backend };
  1921. *result = detail::extract<boost::uint128_type>(source);
  1922. }
  1923. }
  1924. }
  1925. template <typename FloatingPointType>
  1926. constexpr auto eval_convert_to(boost::uint128_type* result, const cpp_double_fp_backend<FloatingPointType>& backend) -> typename std::enable_if<!(cpp_df_qf_detail::ccmath::numeric_limits<FloatingPointType>::digits > 24), void>::type
  1927. {
  1928. const auto fpc = eval_fpclassify(backend);
  1929. if (fpc != FP_NORMAL)
  1930. {
  1931. *result = static_cast<boost::uint128_type>(backend.crep().first);
  1932. }
  1933. else
  1934. {
  1935. using double_float_type = cpp_double_fp_backend<FloatingPointType>;
  1936. double_float_type source { backend };
  1937. *result = detail::extract<boost::uint128_type>(source);
  1938. }
  1939. }
  1940. #endif
  1941. template <typename FloatingPointType,
  1942. typename OtherFloatingPointType>
  1943. constexpr auto eval_convert_to(OtherFloatingPointType* result, const cpp_double_fp_backend<FloatingPointType>& backend) -> typename ::std::enable_if<cpp_df_qf_detail::is_floating_point<OtherFloatingPointType>::value>::type
  1944. {
  1945. const auto fpc = eval_fpclassify(backend);
  1946. // TBD: Implement min/max check for the destination floating-point type result.
  1947. if (fpc != FP_NORMAL)
  1948. {
  1949. *result = static_cast<OtherFloatingPointType>(backend.my_first());
  1950. }
  1951. else
  1952. {
  1953. BOOST_IF_CONSTEXPR(cpp_df_qf_detail::ccmath::numeric_limits<OtherFloatingPointType>::digits > cpp_df_qf_detail::ccmath::numeric_limits<FloatingPointType>::digits)
  1954. {
  1955. *result = static_cast<OtherFloatingPointType>(backend.my_first());
  1956. *result += static_cast<OtherFloatingPointType>(backend.my_second());
  1957. }
  1958. else
  1959. {
  1960. cpp_double_fp_backend<FloatingPointType> source = backend;
  1961. *result = 0;
  1962. for(auto digit_count = static_cast<int>(0);
  1963. digit_count < cpp_double_fp_backend<FloatingPointType>::my_digits;
  1964. digit_count += cpp_df_qf_detail::ccmath::numeric_limits<OtherFloatingPointType>::digits)
  1965. {
  1966. const auto next = static_cast<OtherFloatingPointType>(source.my_first());
  1967. *result += next;
  1968. eval_subtract(source, cpp_double_fp_backend<FloatingPointType>(next));
  1969. }
  1970. }
  1971. }
  1972. }
  1973. template <typename FloatingPointType>
  1974. constexpr auto hash_value(const cpp_double_fp_backend<FloatingPointType>& a) -> ::std::size_t
  1975. {
  1976. return a.hash();
  1977. }
  1978. } // namespace backends
  1979. using backends::cpp_double_fp_backend;
  1980. using cpp_double_float = number<cpp_double_fp_backend<float>, ::boost::multiprecision::et_off>;
  1981. using cpp_double_double = number<cpp_double_fp_backend<double>, ::boost::multiprecision::et_off>;
  1982. using cpp_double_long_double = number<cpp_double_fp_backend<long double>, ::boost::multiprecision::et_off>;
  1983. #ifdef BOOST_MP_CPP_DOUBLE_FP_HAS_FLOAT128
  1984. using cpp_double_float128 = number<cpp_double_fp_backend<::boost::float128_type>, ::boost::multiprecision::et_off>;
  1985. #endif
  1986. } } // namespace boost::multiprecision
  1987. namespace std {
  1988. // Specialization of numeric_limits for boost::multiprecision::number<cpp_double_fp_backend<>>
  1989. template <typename FloatingPointType,
  1990. const boost::multiprecision::expression_template_option ExpressionTemplatesOption>
  1991. BOOST_MP_DF_QF_NUM_LIMITS_CLASS_TYPE numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_double_fp_backend<FloatingPointType>, ExpressionTemplatesOption> >
  1992. {
  1993. private:
  1994. using local_float_type = FloatingPointType;
  1995. using inner_self_type = boost::multiprecision::cpp_double_fp_backend<local_float_type>;
  1996. using self_type = boost::multiprecision::number<inner_self_type, ExpressionTemplatesOption>;
  1997. public:
  1998. static constexpr bool is_specialized = true;
  1999. static constexpr bool is_signed = true;
  2000. static constexpr bool is_integer = false;
  2001. static constexpr bool is_exact = false;
  2002. static constexpr bool is_bounded = true;
  2003. static constexpr bool is_modulo = false;
  2004. static constexpr bool is_iec559 = false;
  2005. static constexpr bool has_infinity = true;
  2006. static constexpr bool has_quiet_NaN = true;
  2007. static constexpr bool has_signaling_NaN = false;
  2008. // These members were deprecated in C++23, but only MSVC warns (as of June 25)
  2009. #ifdef BOOST_MSVC
  2010. #pragma warning(push)
  2011. #pragma warning(disable:4996)
  2012. #endif
  2013. static constexpr float_denorm_style has_denorm = denorm_absent;
  2014. static constexpr bool has_denorm_loss = true;
  2015. #ifdef BOOST_MSVC
  2016. #pragma warning(pop)
  2017. #endif // deprecated members
  2018. static constexpr bool traps = false;
  2019. static constexpr bool tinyness_before = false;
  2020. static constexpr float_round_style round_style = round_to_nearest;
  2021. static constexpr int radix = 2;
  2022. static constexpr int digits = inner_self_type::my_digits;
  2023. static constexpr int digits10 = inner_self_type::my_digits10;
  2024. static constexpr int max_digits10 = inner_self_type::my_max_digits10;
  2025. static constexpr int max_exponent = inner_self_type::my_max_exponent;
  2026. static constexpr int min_exponent = inner_self_type::my_min_exponent;
  2027. static constexpr int max_exponent10 = inner_self_type::my_max_exponent10;
  2028. static constexpr int min_exponent10 = inner_self_type::my_min_exponent10;
  2029. static constexpr auto(min) () noexcept -> self_type { return static_cast<self_type>(inner_self_type::my_value_min()); }
  2030. static constexpr auto(max) () noexcept -> self_type { return static_cast<self_type>(inner_self_type::my_value_max()); }
  2031. static constexpr auto lowest () noexcept -> self_type { return static_cast<self_type>(-(max)()); }
  2032. static constexpr auto epsilon () noexcept -> self_type { return static_cast<self_type>(inner_self_type::my_value_eps()); }
  2033. static constexpr auto round_error () noexcept -> self_type { return static_cast<self_type>(static_cast<local_float_type>(0.5)); }
  2034. static constexpr auto denorm_min () noexcept -> self_type { return static_cast<self_type>((min)()); }
  2035. static constexpr auto infinity () noexcept -> self_type { return static_cast<self_type>(inner_self_type::my_value_inf()); }
  2036. static constexpr auto quiet_NaN () noexcept -> self_type { return static_cast<self_type>(inner_self_type::my_value_nan()); }
  2037. static constexpr auto signaling_NaN() noexcept -> self_type { return static_cast<self_type>(static_cast<local_float_type>(0.0)); }
  2038. };
  2039. } // namespace std
  2040. template <typename FloatingPointType, const boost::multiprecision::expression_template_option ExpressionTemplatesOption>
  2041. constexpr bool std::numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_double_fp_backend<FloatingPointType>, ExpressionTemplatesOption> >::is_specialized;
  2042. template <typename FloatingPointType, const boost::multiprecision::expression_template_option ExpressionTemplatesOption>
  2043. constexpr bool std::numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_double_fp_backend<FloatingPointType>, ExpressionTemplatesOption> >::is_signed;
  2044. template <typename FloatingPointType, const boost::multiprecision::expression_template_option ExpressionTemplatesOption>
  2045. constexpr bool std::numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_double_fp_backend<FloatingPointType>, ExpressionTemplatesOption> >::is_integer;
  2046. template <typename FloatingPointType, const boost::multiprecision::expression_template_option ExpressionTemplatesOption>
  2047. constexpr bool std::numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_double_fp_backend<FloatingPointType>, ExpressionTemplatesOption> >::is_exact;
  2048. template <typename FloatingPointType, const boost::multiprecision::expression_template_option ExpressionTemplatesOption>
  2049. constexpr bool std::numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_double_fp_backend<FloatingPointType>, ExpressionTemplatesOption> >::is_bounded;
  2050. template <typename FloatingPointType, const boost::multiprecision::expression_template_option ExpressionTemplatesOption>
  2051. constexpr bool std::numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_double_fp_backend<FloatingPointType>, ExpressionTemplatesOption> >::is_modulo;
  2052. template <typename FloatingPointType, const boost::multiprecision::expression_template_option ExpressionTemplatesOption>
  2053. constexpr bool std::numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_double_fp_backend<FloatingPointType>, ExpressionTemplatesOption> >::is_iec559;
  2054. #ifdef BOOST_MSVC
  2055. #pragma warning(push)
  2056. #pragma warning(disable:4996)
  2057. #endif
  2058. template <typename FloatingPointType, const boost::multiprecision::expression_template_option ExpressionTemplatesOption>
  2059. constexpr std::float_denorm_style std::numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_double_fp_backend<FloatingPointType>, ExpressionTemplatesOption> >::has_denorm;
  2060. template <typename FloatingPointType, const boost::multiprecision::expression_template_option ExpressionTemplatesOption>
  2061. constexpr bool std::numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_double_fp_backend<FloatingPointType>, ExpressionTemplatesOption> >::has_denorm_loss;
  2062. #ifdef BOOST_MSVC
  2063. #pragma warning(pop)
  2064. #endif // deprecated members
  2065. template <typename FloatingPointType, const boost::multiprecision::expression_template_option ExpressionTemplatesOption>
  2066. constexpr bool std::numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_double_fp_backend<FloatingPointType>, ExpressionTemplatesOption> >::has_infinity;
  2067. template <typename FloatingPointType, const boost::multiprecision::expression_template_option ExpressionTemplatesOption>
  2068. constexpr bool std::numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_double_fp_backend<FloatingPointType>, ExpressionTemplatesOption> >::has_quiet_NaN;
  2069. template <typename FloatingPointType, const boost::multiprecision::expression_template_option ExpressionTemplatesOption>
  2070. constexpr bool std::numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_double_fp_backend<FloatingPointType>, ExpressionTemplatesOption> >::has_signaling_NaN;
  2071. template <typename FloatingPointType, const boost::multiprecision::expression_template_option ExpressionTemplatesOption>
  2072. constexpr bool std::numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_double_fp_backend<FloatingPointType>, ExpressionTemplatesOption> >::traps;
  2073. template <typename FloatingPointType, const boost::multiprecision::expression_template_option ExpressionTemplatesOption>
  2074. constexpr bool std::numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_double_fp_backend<FloatingPointType>, ExpressionTemplatesOption> >::tinyness_before;
  2075. template <typename FloatingPointType, const boost::multiprecision::expression_template_option ExpressionTemplatesOption>
  2076. constexpr std::float_round_style std::numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_double_fp_backend<FloatingPointType>, ExpressionTemplatesOption> >::round_style;
  2077. template <typename FloatingPointType, const boost::multiprecision::expression_template_option ExpressionTemplatesOption>
  2078. constexpr int std::numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_double_fp_backend<FloatingPointType>, ExpressionTemplatesOption> >::radix;
  2079. template <typename FloatingPointType, const boost::multiprecision::expression_template_option ExpressionTemplatesOption>
  2080. constexpr int std::numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_double_fp_backend<FloatingPointType>, ExpressionTemplatesOption> >::digits;
  2081. template <typename FloatingPointType, const boost::multiprecision::expression_template_option ExpressionTemplatesOption>
  2082. constexpr int std::numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_double_fp_backend<FloatingPointType>, ExpressionTemplatesOption> >::digits10;
  2083. template <typename FloatingPointType, const boost::multiprecision::expression_template_option ExpressionTemplatesOption>
  2084. constexpr int std::numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_double_fp_backend<FloatingPointType>, ExpressionTemplatesOption> >::max_digits10;
  2085. template <typename FloatingPointType, const boost::multiprecision::expression_template_option ExpressionTemplatesOption>
  2086. constexpr int std::numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_double_fp_backend<FloatingPointType>, ExpressionTemplatesOption> >::max_exponent;
  2087. template <typename FloatingPointType, const boost::multiprecision::expression_template_option ExpressionTemplatesOption>
  2088. constexpr int std::numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_double_fp_backend<FloatingPointType>, ExpressionTemplatesOption> >::min_exponent;
  2089. template <typename FloatingPointType, const boost::multiprecision::expression_template_option ExpressionTemplatesOption>
  2090. constexpr int std::numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_double_fp_backend<FloatingPointType>, ExpressionTemplatesOption> >::max_exponent10;
  2091. template <typename FloatingPointType, const boost::multiprecision::expression_template_option ExpressionTemplatesOption>
  2092. constexpr int std::numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_double_fp_backend<FloatingPointType>, ExpressionTemplatesOption> >::min_exponent10;
  2093. #if defined(BOOST_MP_MATH_AVAILABLE)
  2094. namespace boost { namespace math { namespace policies {
  2095. template <class FloatingPointType, class Policy, boost::multiprecision::expression_template_option ExpressionTemplates>
  2096. struct precision<boost::multiprecision::number<boost::multiprecision::cpp_double_fp_backend<FloatingPointType>, ExpressionTemplates>, Policy>
  2097. {
  2098. private:
  2099. using my_multiprecision_backend_type = boost::multiprecision::cpp_double_fp_backend<FloatingPointType>;
  2100. using digits2_type = digits2<my_multiprecision_backend_type::my_digits>;
  2101. static constexpr auto use_full_precision() noexcept -> bool
  2102. {
  2103. return ((digits2_type::value <= precision_type::value) || (precision_type::value <= 0));
  2104. }
  2105. public:
  2106. using precision_type = typename Policy::precision_type;
  2107. using type =
  2108. typename std::conditional<use_full_precision(),
  2109. digits2_type, // This is the default case: Use full precision for FloatingPointType.
  2110. precision_type>::type; // Here we find (and use) user-customized precision.
  2111. };
  2112. } } } // namespace boost::math::policies
  2113. #endif
  2114. #endif // BOOST_MP_CPP_DOUBLE_FP_2021_06_05_HPP